-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathspeechd-speak.el
2230 lines (1925 loc) · 89.4 KB
/
speechd-speak.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; speechd-speak.el --- simple speechd-el based Emacs client -*- lexical-binding: t -*-
;; Copyright (C) 2012-2021 Milan Zamazal <[email protected]>
;; Copyright (C) 2003-2010 Brailcom, o.p.s.
;; Author: Milan Zamazal <[email protected]>
;; COPYRIGHT NOTICE
;;
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This is an Emacs client to speechd. Some ideas taken from the Emacspeak
;; package (http://emacspeak.sourceforge.net) by T. V. Raman.
;;; Code:
(require 'cl-lib)
(require 'wid-edit)
(require 'speechd)
(require 'speechd-common)
(require 'speechd-out)
(require 'speechd-brltty)
(require 'speechd-ssip)
;;; User options
(defgroup speechd-speak ()
"Speechd-el user client customization."
:group 'speechd-el)
(defcustom speechd-speak-echo 'character
"Symbol determining how to read typed characters.
It can have one of the following values:
`character' -- read characters when they are typed
`word' -- read only whole words when they are written
`nil' -- don't echo anything on typing"
:type '(choice (const :tag "Characters" character)
(const :tag "Words" word)
(const :tag "Nothing" nil))
:group 'speechd-speak)
(defcustom speechd-speak-deleted-char t
"If non-nil, speak the deleted char, otherwise speak the adjacent char."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-buffer-name 'text
"If non-nil, speak buffer name on a buffer change.
If the value is the symbol `text', speak the text from the cursor position in
the new buffer to the end of line as well. If nil, speak the text only, not
the buffer name."
:type '(choice (const :tag "Buffer name and buffer text" text)
(const :tag "Buffer name" t)
(const :tag "Buffer text" nil))
:group 'speechd-speak)
(defcustom speechd-speak-on-minibuffer-exit t
"If non-nil, always try to speak something when exiting the minibuffer."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-auto-speak-buffers '("*Help*")
"List of names of other-window buffers to speak if nothing else fits.
If nothing else is to be spoken after a command and a visible window in the
current frame displaying a buffer with a name contained in this list is
changed, the contents of the window buffer is spoken."
:type '(repeat (string :tag "Buffer name"))
:group 'speechd-speak)
(defcustom speechd-speak-force-auto-speak-buffers '()
"List of names of other-window buffers to speak on visible changes.
Like `speechd-speak-auto-speak-buffers' except that the window content is
spoken even when there are other messages to speak."
:type '(repeat (string :tag "Buffer name"))
:group 'speechd-speak)
(defcustom speechd-speak-buffer-insertions 'one-line
"Defines whether insertions in a current buffer should be read automatically.
The value is a symbol and can be from the following set:
- nil means don't speak them
- t means speak them all
- `one-line' means speak only first line of any change
- `whole-buffer' means speak whole buffer if it was changed in any way
Only newly inserted text is read, the option doesn't affect processing of
deleted text. Also, the option doesn't affect insertions within commands
processed in a different way by speechd-speak or user definitions."
:type '(choice (const :tag "Never" nil)
(const :tag "One-line changes only" one-line)
(const :tag "Always" t)
(const :tag "Read whole buffer" whole-buffer))
:group 'speechd-speak)
(defcustom speechd-speak-insertions-in-buffers
'(" widget-choose" "*Choices*")
"List of names of buffers, in which insertions are automatically spoken.
See also `speechd-speak-buffer-insertions'."
:type '(repeat (string :tag "Buffer name"))
:group 'speechd-speak)
(defcustom speechd-speak-priority-insertions-in-buffers '()
"List of names of buffers, in which insertions are spoken immediately.
Unlike `speechd-speak-insertions-in-buffers', speaking is not delayed until a
command is completed.
This is typically useful in comint buffers."
:type '(repeat (string :tag "Buffer name"))
:group 'speechd-speak)
(defcustom speechd-speak-align-buffer-insertions t
"If non-nil, read insertions aligned to the beginning of the first word."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-movement-on-insertions 'read-only
"If t, speak the text around moved cursor even in modified buffers.
If nil, additional cursor movement doesn't cause speaking the text around the
new cursor position in modified buffers.
If `read-only', speak the text around cursor in read-only buffers only."
:type '(choice (const :tag "Always" t)
(const :tag "Never" nil)
(const :tag "In read-only buffers" read-only))
:group 'speechd-speak)
(defcustom speechd-speak-read-command-keys t
"Defines whether command keys should be read after their command.
If t, always read command keys, before the command is performed.
If nil, never read them.
Otherwise it is a list, consisting of one or more of the following symbols:
`movement' -- read the keys if the cursor has moved without any buffer change
`modification' -- read the keys if the buffer was modified without moving the
cursor
`modification-movement' -- read the keys if the buffer was modified and the
cursor has moved
and the keys are read after the command is performed."
:type '(choice (const :tag "Always" t)
(const :tag "Never" nil)
(set :tag "Sometimes"
(const movement)
(const modification)
(const modification-movement)))
:group 'speechd-speak)
(defcustom speechd-speak-allow-prompt-commands t
"If non-nil, allow speechd-speak commands in read-char prompts."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-ignore-command-keys
'(forward-char backward-char right-char left-char next-line previous-line
delete-char comint-delchar-or-maybe-eof delete-backward-char
backward-delete-char-untabify delete-forward-char
c-electric-backspace c-electric-delete-forward)
"List of commands for which their keys are never read."
:type '(repeat function)
:group 'speechd-speak)
(defcustom speechd-speak-read-command-name nil
"If non-nil, read command name instead of command keys."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-use-index-marks nil
"If non-nil, move point over text in spoken buffers."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-index-mark-regexp ""
"Regular expression defining the places where to put index marks to.
An index mark is put at the end of the regexp at each of the places.
If the regexp is empty, `sentence-end' is used. If `sentence-end' is nil,
a default regexp matching common punctuation is used."
:type 'regexp
:group 'speechd-speak)
(defcustom speechd-speak-by-properties-on-movement t
"Method of selection of the piece of text to be spoken on movement.
Unless a command provides its speechd feedback in a different way, it speaks
the current line by default if the cursor has moved. However, if this variable
is t, it speaks the uniform text around the cursor, where \"uniform\"
means the maximum amount of text without any text property change.
If the variable is a list of faces, uniform text is spoken only when the cursor
is on one of the named faces.
Speaking uniform text only works if font-lock-mode is enabled for the current
buffer.
See also `speechd-speak-by-properties-always' and
`speechd-speak-by-properties-never'."
:type '(choice (const :tag "Always" t)
(repeat :tag "On faces" (face :tag "Face")))
:group 'speechd-speak)
(defcustom speechd-speak-by-properties-always '()
"List of commands to always speak by properties on movement.
The elements of the list are command names, symbols.
See `speechd-speak-by-properties-on-movement' for more information about
property speaking."
:type '(repeat
(function :tag "Command" :match (lambda (w val) (commandp val))))
:group 'speechd-speak)
(defcustom speechd-speak-by-properties-never '()
"List of commands to never speak by properties on movement.
The elements of the list are command names, symbols.
See `speechd-speak-by-properties-on-movement' for more information about
property speaking."
:type '(repeat
(function :tag "Command" :match (lambda (w val) (commandp val))))
:group 'speechd-speak)
(defcustom speechd-speak-faces '()
"Alist of faces and speaking functions.
Each element of the list is of the form (FACE . ACTION).
If a movement command leaves the cursor on a FACE and there is no explicit
speaking bound to the command, ACTION is invoked.
If ACTION is a string, the string is spoken.
If ACTION is a function, it is invoked, with no arguments."
:type '(alist
:key-type face
:value-type (choice
(string :tag "String to speak")
(function :tag "Function to call"
:match (lambda (w val) (commandp val)))))
:group 'speechd-speak)
(defcustom speechd-speak-display-modes '(gnus-agent-mode)
"List of minor modes to be spoken by their display string rather than name."
:type '(repeat symbol)
:group 'speechd-speak)
(defcustom speechd-speak-unreadable-modes
'(doc-view-mode image-mode pdf-view-mode)
"List of major modes with unreadable content.
Typically modes for displaying images or various kinds of non-text documents."
:type '(repeat symbol)
:group 'speechd-speak)
(defcustom speechd-speak-whole-line nil
"If non-nil, speak whole line on movement by default.
Otherwise speak from the point to the end of line on movement by default."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-separator-regexp nil
"If non-nil don't read parts of a line separated by the given regexp.
This is typically useful in w3m where columns are separated only
by whitespace. \" \" may be good value of this variable in such
a case."
:type 'regexp
:group 'speechd-speak)
(defcustom speechd-speak-message-time-interval 30
"Minimum time in seconds, after which the same message may be repeated.
If the message is the same as the last one, it is not spoken unless the number
of seconds defined here has passed from the last spoken message."
:type 'integer
:group 'speechd-speak)
(defcustom speechd-speak-connections '()
"Alist mapping major modes and buffers to speechd connection.
By default, there's a single connection to speechd, named \"default\". This
variable can define special connections for particular major modes and buffers.
Each element of the alist is of the form (MODE-OR-BUFFER . CONNECTION-NAME).
MODE-OR-BUFFER may be, in the order of preference from the highest to the
lowest:
- a list, representing a function call returning non-nil iff the element should
be applied
- buffer name
- the symbol `:minibuffer', representing minibuffers
- major mode symbol
- nil, representing non-buffer areas, e.g. echo area
- t, representing the default value if nothing else matches
CONNECTION-NAME is an arbitrary non-empty string naming the corresponding
connection. If connection with such a name doesn't exist, it is automatically
created."
:type '(alist :key-type
(choice :tag "Matcher" :value nil
(const :tag "Default" t)
(const :tag "Non-buffers" nil)
(const :tag "Minibuffer" :value :minibuffer)
(symbol :tag "Major mode" :value fundamental-mode)
(regexp :tag "Buffer name regexp" :value "")
(restricted-sexp :tag "Function call"
:match-alternatives (listp)))
:value-type (string :tag "Connection name"))
:group 'speechd-speak)
(defcustom speechd-speak-signal-events
'(empty whitespace beginning-of-line end-of-line start finish minibuffer
message)
"List of symbolic names of events to signal with a standard icon.
The following actions are supported: `empty', `whitespace', `beginning-of-line',
`end-of-line', `start', `finish', `minibuffer', `message'."
:type '(set (const empty)
(const whitespace)
(const beginning-of-line)
(const end-of-line)
(const start)
(const finish)
(const minibuffer)
(const message))
:group 'speechd-speak)
(defcustom speechd-speak-input-method-languages '()
"Alist mapping input methods to languages.
Each of the alist element is of the form (INPUT-METHOD-NAME . LANGUAGE), where
INPUT-METHOD-NAME is a string naming the input method and LANGUAGE is an ISO
language code accepted by SSIP.
If the current input method is present in the alist, the corresponding language
is selected unless overridden by another setting."
:type '(alist :key-type (string :tag "Input method")
:value-type (string :tag "Language code"))
:group 'speechd-speak)
(defcustom speechd-speak-emacs-language "en"
"Language to use for texts originating from Emacs.
Messages, minibuffer prompts, completions often contain English texts,
so it is desirable to speak them in English. This variable allows to
speak them in English, in another language or in the current language.
The value is an ISO language code string accepted by SSIP.
If nil, use the current language."
:type '(choice (string :tag "Language code")
(const :tag "Current" nil))
:group 'speechd-speak)
(defcustom speechd-speak-in-debugger t
"If nil, speechd-speak functions won't speak in Elisp debuggers.
This may be useful when debugging speechd-el itself."
:type 'boolean
:group 'speechd-speak)
(defcustom speechd-speak-prefix "\C-e"
"Default prefix key used for speechd-speak commands."
:set (lambda (name value)
(set-default name value)
(speechd-speak--build-mode-map))
:initialize 'custom-initialize-default
:type 'sexp
:group 'speechd-speak)
(defcustom speechd-speak-hook nil
"Hook run in the `speechd-speak' function."
:options '((lambda () (require 'speechd-ssip))
(lambda () (require 'speechd-brltty)))
:type 'hook
:group 'speechd-speak)
;;; Internal constants and variables
(defconst speechd-speak--c-buffer-name "*Completions*")
(defvar speechd-speak--info)
;;; Debugging support
(defvar speechd-speak--debug ())
(defvar speechd-speak--max-debug-length 12)
(defvar speechd-speak--debug-all nil)
(defvar speechd-speak--debug-mark nil)
(defun speechd-speak--debug (info &optional optional)
(when (or (not optional) speechd-speak--debug-all)
(setq speechd-speak--debug
(cons info
(if (and (not speechd-speak--debug-all)
(>= (length speechd-speak--debug)
speechd-speak--max-debug-length))
(butlast speechd-speak--debug)
speechd-speak--debug)))))
;;; Control functions
(defvar speechd-speak--predefined-rates
'((1 . -100)
(2 . -75)
(3 . -50)
(4 . -25)
(5 . 0)
(6 . 25)
(7 . 50)
(8 . 75)
(9 . 100)))
(defun speechd-speak-set-predefined-rate (level)
"Set speech rate to one of nine predefined levels.
Level 1 is the slowest, level 9 is the fastest."
(interactive "nSpeech rate level (1-9): ")
(setq level (min (max level 1) 9))
(let ((rate (cdr (assoc level speechd-speak--predefined-rates))))
(speechd-set-rate rate)
(message "Speech rate set to %d" rate)))
(defvar speechd-speak--char-to-number
'((?1 . 1) (?2 . 2) (?3 . 3) (?4 . 4) (?5 . 5)
(?6 . 6) (?7 . 7) (?8 . 8) (?9 . 9)))
(defun speechd-speak-key-set-predefined-rate ()
"Set speech rate to one of nine predefined levels via a key binding.
Level 1 is the slowest, level 9 is the fastest."
(interactive)
(let ((level (cdr (assoc last-input-event speechd-speak--char-to-number))))
(when level
(speechd-speak-set-predefined-rate level))))
;;; Supporting functions and options
(defvar speechd-speak-command-done nil
"When non-nil, no post command speaking is performed, except for keys.
This variable is reset to nil before each command in pre-command-hook.")
(defun speechd-speak--name (&rest args)
(intern (mapconcat #'symbol-name args "-")))
(defvar speechd-speak-mode nil) ; forward definition to make everything happy
(defvar speechd-speak--started nil)
(defvar speechd-speak--last-buffer-mode t)
(defvar speechd-speak--last-connection-name nil)
(defvar speechd-speak--last-connections nil)
(defvar speechd-speak--default-connection-name "default")
(defvar speechd-speak--special-area nil)
(defvar speechd-speak--emulate-minibuffer nil)
(defvar speechd-speak--client-name-set nil)
(make-variable-buffer-local 'speechd-speak--client-name-set)
(defun speechd-speak--connection-name ()
(let ((buffer-mode (if speechd-speak--special-area
nil
(cons major-mode (buffer-name)))))
(cond
(speechd-speak--client-name-set
speechd-client-name)
((and (not speechd-speak--client-name-set)
(eq speechd-speak-connections speechd-speak--last-connections)
(equal buffer-mode speechd-speak--last-buffer-mode))
speechd-speak--last-connection-name)
(t
(setq speechd-speak--last-buffer-mode buffer-mode
speechd-speak--last-connections speechd-speak-connections
speechd-speak--last-connection-name
(if buffer-mode
(or (cdr (or
;; minibuffer-like prompts
(and speechd-speak--emulate-minibuffer
(assoc :minibuffer speechd-speak-connections))
;; functional test
(let ((specs speechd-speak-connections)
(result nil))
(while (and (not result) specs)
(if (and (consp (caar specs))
(eval (caar specs)))
(setq result (car specs))
(setq specs (cdr specs))))
result)
;; buffer name
(let ((buffer-name (buffer-name)))
(cl-assoc-if (lambda (key)
(when (stringp key) (string-match key buffer-name)))
speechd-speak-connections))
;; minibuffer
(and (speechd-speak--in-minibuffer-p)
(assoc :minibuffer speechd-speak-connections))
;; major mode
(assq major-mode speechd-speak-connections)
;; default
(assq t speechd-speak-connections)))
speechd-speak--default-connection-name)
(or (cdr (assq nil speechd-speak-connections))
speechd-speak--default-connection-name)))
(set (make-local-variable 'speechd-client-name)
speechd-speak--last-connection-name)))))
(defun speechd-speak--in-debugger ()
(and (not speechd-speak-in-debugger)
(or (eq major-mode 'debugger-mode)
(and (boundp 'edebug-active) edebug-active))))
(defmacro speechd-speak--maybe-speak* (&rest body)
`(when (and speechd-speak-mode
(not (speechd-speak--in-debugger)))
,@body))
(defmacro speechd-speak--maybe-speak (&rest body)
`(speechd-speak--maybe-speak*
(let ((speechd-client-name (speechd-speak--connection-name))
(speechd-language
(or (and speechd-speak-input-method-languages
current-input-method
(cdr (assoc current-input-method
speechd-speak-input-method-languages)))
speechd-language)))
,@body)))
(defmacro speechd-speak--interactive (&rest body)
`(let ((speechd-speak-mode (or (called-interactively-p 'interactive)
(and speechd-speak-mode
(not (speechd-speak--in-debugger)))))
(speechd-default-text-priority (if (called-interactively-p 'interactive)
'message
speechd-default-text-priority)))
,@body))
(defun speechd-speak--remove-invisible-text (text)
(let ((pieces '())
(max (length text))
(pos 0)
(invisibility-spec (if (eq buffer-invisibility-spec t)
t
(mapcar (lambda (s) (if (consp s) (car s) s))
buffer-invisibility-spec))))
(while (and pos (< pos max))
(let ((next-pos (next-single-char-property-change pos 'invisible text)))
(when (let ((invisibility (get-char-property pos 'invisible text)))
(not (cond
((eq invisibility-spec t)
invisibility)
((consp invisibility)
(cl-intersection invisibility-spec invisibility))
(t
(memq invisibility buffer-invisibility-spec)))))
(push (substring text pos next-pos) pieces))
(setq pos next-pos)))
(apply 'concat (nreverse pieces))))
(defun speechd-speak--text (text &rest args)
(speechd-speak--maybe-speak
;; TODO: replace repeating patterns
;; TODO: handle selective display
(setq text (speechd-speak--remove-invisible-text text))
(when speechd-speak--debug-mark
(speechd-speak--debug (cons speechd-speak--debug-mark text) t))
(apply #'speechd-out-text text args)))
(defun speechd-speak--char (char &rest args)
(speechd-speak--maybe-speak
(apply #'speechd-out-char char args)))
(defvar speechd-speak--last-report "")
(defun speechd-speak-report (message &rest args)
"Output text or icon MESSAGE.
If MESSAGE is a non-empty string, it is the text to output. If it is a non-nil
symbol present in the `speechd-speak-signal-events' variable, it is the icon to
output. Otherwise nothing happens.
ARGS are appended to the arguments of the corresponding speaking
function (`speechd-out-text' or `speechd-out-icon') without change after the
message argument."
(speechd-speak--maybe-speak
(when (and message
(not (string= message ""))
(not (equal message speechd-speak--last-report))
(or (stringp message)
(memq message speechd-speak-signal-events)))
(apply (if (symbolp message) #'speechd-out-icon #'speechd-out-text)
message args)
(setq speechd-speak--last-report message))))
(defun speechd-speak-read-char (&optional char)
"Output character CHAR.
If CHAR is nil, output the character just after current point."
(interactive)
(speechd-speak--interactive
(speechd-speak--char (or char (following-char)))))
(defun speechd-speak-read-region (&optional beg end empty-text index-marks)
"Output region of the current buffer between BEG and END.
If BEG is nil, current mark is used instead.
If END is nil, current point is used instead.
EMPTY-TEXT is a text to output if the region is empty; if nil, empty text icon
is output.
If INDEX-MARKS is non-nil, insert index marks to the spoken text."
(interactive "r")
(speechd-speak--interactive
(let* ((beg (or beg (mark)))
(end (or end (point)))
(text (speechd-speak--buffer-substring beg end)))
(cond
((or (string= text "")
(memq major-mode speechd-speak-unreadable-modes))
(speechd-speak-report (or empty-text 'empty)
:priority speechd-default-text-priority))
((save-match-data (string-match "\\`[ \t]+\\'" text))
(speechd-speak-report 'whitespace
:priority speechd-default-text-priority))
(t
(let* ((point (point))
(cursor (and (>= point beg) (<= point end) (- (point) beg)))
(markers (when index-marks (speechd-speak--markers beg end))))
(speechd-speak--text text :cursor cursor :markers markers)))))))
(defun speechd-speak-read-line (&optional rest-only)
"Output current line.
If the prefix argument is given, output the line only from the current point
to the end of the line."
(interactive "P")
(speechd-speak--interactive
(let* ((inhibit-field-text-motion t)
(in-isearch-p (and isearch-mode (not (called-interactively-p 'interactive))))
(beg (if (and rest-only (not in-isearch-p)) (point) (line-beginning-position)))
(end (if truncate-lines
(line-end-position)
(save-excursion (end-of-visual-line) (point)))))
(when speechd-speak-separator-regexp
(save-match-data
(save-excursion
(when (re-search-backward speechd-speak-separator-regexp beg t)
(setq beg (match-end 0))))
(save-excursion
(when (re-search-forward speechd-speak-separator-regexp end t)
(setq end (match-beginning 0))))))
(speechd-speak-read-region beg end
(when (speechd-speak--in-minibuffer-p) "")))))
(defun speechd-speak-read-next-line ()
"Speak the next line after the current line.
If there is no such line, play the empty text icon."
(interactive)
(speechd-speak--interactive
(save-excursion
(if (= (forward-line 1) 0)
(speechd-speak-read-line)
(speechd-speak-report 'empty)))))
(defun speechd-speak-read-previous-line ()
"Speak the previous line before the current line.
If there is no such line, play the empty text icon."
(interactive)
(speechd-speak--interactive
(save-excursion
(if (= (forward-line -1) 0)
(speechd-speak-read-line)
(speechd-speak-report 'empty)))))
(defun speechd-speak-read-buffer (&optional buffer)
"Read BUFFER.
If BUFFER is nil, read current buffer."
(interactive)
(speechd-speak--interactive
(save-excursion
(when buffer
(set-buffer buffer))
(speechd-speak-read-region (point-min) (point-max) nil
speechd-speak-use-index-marks))))
(defun speechd-speak-read-rest-of-buffer ()
"Read current buffer from the current point to the end of the buffer."
(interactive)
(speechd-speak--interactive
(speechd-speak-read-region (point) (point-max) nil
speechd-speak-use-index-marks)))
(defun speechd-speak-read-rectangle (beg end)
"Read text in the region-rectangle."
(interactive "r")
(speechd-speak--interactive
(speechd-speak--text
(mapconcat #'identity (extract-rectangle beg end) "\n"))))
(defun speechd-speak-read-other-window ()
"Read buffer of the last recently used window."
(interactive)
(speechd-speak--interactive
(speechd-speak-read-buffer (window-buffer (get-lru-window)))))
(defun speechd-speak-read-mode-line ()
"Read mode line.
This function works only in Emacs 22 or higher."
(interactive)
(when (fboundp 'format-mode-line)
(speechd-speak--interactive
(speechd-speak--text (format-mode-line mode-line-format t)))))
(defun speechd-speak--window-contents ()
(sit-for 0) ; to update window start and end
(speechd-speak-read-region (window-start) (window-end)))
(defun speechd-speak--uniform-text-around-point ()
(let ((beg (speechd-speak--previous-property-change (1+ (point))))
(end (speechd-speak--next-property-change (point))))
(speechd-speak-read-region beg end)))
(defun speechd-speak--speak-piece (start)
(let ((point (point)))
(if (> (count-lines start point) 1)
(speechd-speak-read-line)
(speechd-speak-read-region start point))))
(defun speechd-speak--speak-current-column ()
(speechd-speak--text (format "Column %d" (current-column))))
(defmacro speechd-speak--def-speak-object (type)
(let* ((function-name (speechd-speak--name 'speechd-speak-read type))
(backward-function (speechd-speak--name 'backward type))
(forward-function (speechd-speak--name 'forward type)))
`(defun ,function-name ()
,(format "Speak current %s." type)
(interactive)
(speechd-speak--interactive
(save-excursion
(let* ((point (point))
(end (progn (,forward-function 1) (point)))
(beg (progn (,backward-function 1) (point))))
(when (<= (progn (,forward-function 1) (point)) point)
(setq beg end))
(speechd-speak-read-region beg end)))))))
(speechd-speak--def-speak-object word)
(speechd-speak--def-speak-object sentence)
(speechd-speak--def-speak-object paragraph)
(speechd-speak--def-speak-object page)
(speechd-speak--def-speak-object sexp)
(cl-defstruct speechd-speak--command-info-struct
marker
modified
(changes '())
(change-end nil)
(deleted-chars nil)
(other-changes '())
other-changes-buffer
other-window
other-buffer-modified
completion-buffer-modified
minibuffer-contents
info)
(defmacro speechd-speak--cinfo (slot)
`(,(speechd-speak--name 'speechd-speak--command-info-struct slot)
speechd-speak--info))
(defun speechd-speak--command-info-struct-buffer (_info)
(let ((marker (speechd-speak--cinfo marker)))
(and marker (marker-buffer marker))))
(defun speechd-speak--command-info-struct-point (_info)
(let ((marker (speechd-speak--cinfo marker)))
(and marker (marker-position marker))))
(defvar speechd-speak--command-start-info (make-vector 5 nil))
(defmacro speechd-speak--with-minibuffer-depth (&rest body)
`(let ((depth (minibuffer-depth)))
(when (>= depth (length speechd-speak--command-start-info))
(setq speechd-speak--command-start-info
(vconcat speechd-speak--command-start-info
(make-vector
(- (1+ depth)
(length speechd-speak--command-start-info))
nil))))
,@body))
(defun speechd-speak--in-minibuffer-p ()
(window-minibuffer-p (selected-window)))
(defun speechd-speak--command-start-info ()
(speechd-speak--with-minibuffer-depth
(aref speechd-speak--command-start-info depth)))
(defun speechd-speak--set-command-start-info (&optional reset)
(speechd-speak--with-minibuffer-depth
(aset speechd-speak--command-start-info depth
(if reset
nil
(ignore-errors
(let ((other-window (next-window)))
(make-speechd-speak--command-info-struct
:marker (point-marker)
:modified (buffer-modified-tick)
:other-window other-window
:other-buffer-modified
(and other-window
(buffer-modified-tick (window-buffer other-window)))
:completion-buffer-modified
(let ((buffer (get-buffer speechd-speak--c-buffer-name)))
(and buffer (buffer-modified-tick buffer)))
:minibuffer-contents
(if (speechd-speak--in-minibuffer-p)
(minibuffer-contents)
'unset)
:info (speechd-speak--current-info)
)))))))
(defun speechd-speak--reset-command-start-info ()
(speechd-speak--set-command-start-info t))
(defmacro speechd-speak--with-command-start-info (&rest body)
`(let ((speechd-speak--info (speechd-speak--command-start-info)))
(when speechd-speak--info
,@body)))
(defmacro speechd-speak--defadvice (function class &rest body)
(let* ((function* function)
(functions (if (listp function*) function* (list function*)))
(class* class)
(where (intern (concat ":" (symbol-name class*))))
(aroundp (eq class* 'around))
(lambda-list (if aroundp '(orig-fun &rest args) '(&rest args)))
(aname (if (listp function*) 'speechd-speak 'speechd-speak-user)))
`(progn
,@(mapcar
(lambda (fname)
`(define-advice ,fname (,where ,lambda-list ,aname)
(cl-flet ,(when aroundp `((call-orig-func () (apply orig-fun args))))
(if (not speechd-speak--started)
,(if aroundp
`(call-orig-func)
'args) ;; not nil, to silence warnings about unused `args'
,@body))))
functions))))
(defmacro speechd-speak--report (feedback &rest args)
(if (stringp feedback)
`(speechd-speak-report ,feedback ,@args)
feedback))
(defmacro speechd-speak-function-feedback (function position feedback)
"Report FEEDBACK on each invocation of FUNCTION.
FUNCTION is a function name.
POSITION may be one of the symbols `before' (the feedback is run before the
function is invoked) or `after' (the feedback is run after the function is
invoked.
FEEDBACK is a string or a sexp. If it is a string, it is simply passed as an
argument to `speechd-speak-report'. If it is a sexp then it is evaluated as it
is when the feedback is run; the expression must itself deliver something to
output (by using speechd-speak-report or other function)."
`(speechd-speak--defadvice ,(list function) ,position
(speechd-speak--report ,feedback :priority 'message)))
(defmacro speechd-speak-command-feedback (function position feedback)
"Report FEEDBACK on each invocation of FUNCTION.
The arguments are the same as in `speechd-speak-function-feedback'.
Unlike `speechd-speak-function-feedback', the feedback is reported only when
FUNCTION is invoked interactively."
`(speechd-speak--defadvice ,(list function) ,position
(when (called-interactively-p 'interactive)
(speechd-speak--report ,feedback :priority 'message))))
(defmacro speechd-speak--command-feedback (commands position &rest body)
(let ((commands* (if (listp commands) commands (list commands)))
(position* position)
(body* `(progn (speechd-speak--reset-command-start-info) ,@body)))
`(progn
,@(mapcar #'(lambda (command)
`(speechd-speak--defadvice ,command ,position*
,(if (eq position* 'around)
`(if (called-interactively-p 'interactive)
,body*
(call-orig-func))
`(when (called-interactively-p 'interactive)
,body*))))
commands*)
,(unless (eq position 'before)
`(setq speechd-speak-command-done t)))))
(defmacro speechd-speak--command-feedback-region (commands)
`(speechd-speak--command-feedback ,commands around
(let ((start (point)))
(prog1 (call-orig-func)
(speechd-speak--speak-piece start)))))
(cl-defun speechd-speak--next-property-change (&optional (point (point)) (limit (point-max)))
(next-char-property-change point limit))
(cl-defun speechd-speak--previous-property-change (&optional (point (point)) (limit (point-min)))
;; Let's be careful about isearch overlays not to cut texts in isearch
(let* ((i-overlays (and isearch-mode
(cl-intersection (overlays-at (- point 2))
(append (list isearch-overlay)
isearch-opened-overlays
isearch-lazy-highlight-overlays))))
(i-overlays-start (1- (apply #'min (1+ point) (mapcar #'overlay-start i-overlays)))))
(when (>= i-overlays-start limit)
;; This may omit other property borders as well. But in isearch we
;; should retain enough context anyway.
(setq point i-overlays-start))
(previous-char-property-change point limit)))
(defmacro speechd-speak--with-updated-text (&rest body)
`(speechd-out-with-updated-text (speechd-speak--updated-text)
,@body))
(defun speechd-speak-set-language (language)
"Set default language to LANGUAGE.
Language must be an RFC 1766 language code, as a string."
(interactive "sLanguage: ")
(speechd-set-language language)
(setq speechd-language language))
(defun speechd-speak--language (&optional special)
(or (and special speechd-speak-emacs-language) speechd-language))
(defun speechd-speak--in-comint-p ()
"Return non-nil if the current buffer is any sort of a comint buffer."
(and (boundp 'comint-accum-marker)
comint-accum-marker))
(defvar speechd-speak--marker-counter 0)
(defun speechd-speak--markers (beg end)
;; Return list of (POSITION INDEX MARKER)
(let ((regexp speechd-speak-index-mark-regexp)
(markers '()))
(when (string= regexp "")
(setq regexp (or sentence-end "[.,;!?]+")))
(save-excursion
(goto-char beg)
(while (re-search-forward regexp end t)
(push (list (cl-incf speechd-speak--marker-counter) (point-marker))
markers)))
(mapcar #'(lambda (m) (cons (- (marker-position (cl-second m)) beg) m))
markers)))
;;; Basic speaking
(speechd-speak--command-feedback (next-line previous-line) after
(speechd-speak-read-line (not speechd-speak-whole-line)))
(speechd-speak--command-feedback (forward-word backward-word) after
(speechd-speak-read-word))
(speechd-speak--command-feedback (forward-sentence backward-sentence) after
(speechd-speak-read-sentence))
(speechd-speak--command-feedback (forward-paragraph backward-paragraph) after
(speechd-speak-read-paragraph))
(speechd-speak--command-feedback (forward-page backward-page) after
(speechd-speak-read-page))
(speechd-speak--command-feedback (scroll-up scroll-down) after
(speechd-speak--window-contents))
(speechd-speak--command-feedback-region
(backward-sexp forward-sexp forward-list backward-list up-list
backward-up-list down-list))
(speechd-speak--command-feedback (upcase-word downcase-word capitalize-word)
after
(speechd-speak-read-word))
(defun speechd-speak--store-deleted-chars (text)
(speechd-speak--with-command-start-info
(setf (speechd-speak--cinfo deleted-chars) text)))
(speechd-speak--defadvice (delete-backward-char backward-delete-char) around
(if (speechd-speak--command-start-info)
(speechd-speak--with-command-start-info
(let ((speechd-speak--deleted-chars
(when speechd-speak-deleted-char
(speechd-speak--buffer-substring
(max (- (point) (or (cl-first args) 1))
(point-min))
(point)))))
(apply orig-fun args)
(speechd-speak--store-deleted-chars (if speechd-speak-deleted-char
speechd-speak--deleted-chars
(format "%c" (preceding-char))))))
(call-orig-func)))
(speechd-speak--defadvice (delete-forward-char delete-char) around
(let ((speechd-speak--deleted-chars
(when speechd-speak-deleted-char
(speechd-speak--buffer-substring
(point)
(min (+ (point) (or (cl-first args) 1))
(point-max))))))
(call-orig-func)