Debian Codegraph @

texk/web2c/cwebdir/examples/kspell.el

raw
1 ;; Alternative spelling enterface for Emacs, contributed by Don Knuth
2 ;; (Uses the wordtest and extex programs, which are
3 ;; supplied as examples with the CWEB distribution,
4 ;; available by anonymous ftp from ftp.cs.stanford.edu)
5
6 ;; Based in part on "spell.el" from GNU Emacs; redistributed under
7 ;; the terms of the GNU General Public License; NO WARRANTY implied.
8
9 ;; To install this, using the default directories defined below,
10 ;; install wordtest and extex in /usr/local/bin, then say
11 ;; "ln /usr/local/bin/extex /usr/local/bin/excweb", then install
12 ;; a suitable dictionary in /usr/local/lib/dict/words; one such
13 ;; dictionary can be found in ~ftp/pub/dict/words at ftp.cs.stanford.edu.
14 ;; Also create an empty file called .words in your home directory.
15 ;; Finally, add (load-library "kspell") to your .emacs file, or
16 ;; include such a line in site-init.el if kspell is to be used by everybody.
17 ;; If you get a message like "Checking spelling of buffer...not correct"
18 ;; and nothing else, the probable cause is that the wordtest program
19 ;; could not open /usr/local/lib/dict/words or ~/.words for reading.
20
21 ;; Note (10 Feb 1998): The spell-check logic failed to catch "obso1ete"
22 ;; as a scanner-typo for "obsolete". The reason is that, although "obso"
23 ;; and "ete" are non-words, they couldn't be found in query-replace
24 ;; for the regexps "\bobso\b" and "\bete\b".
25
26 (provide 'kspell)
27
28 (defvar wordtest-command "wordtest" ;; maybe "/usr/local/bin/wordtest" better?
29 "*Command to run the wordtest program; can include command-line options.")
30
31 ;; "wordtest [options] [dictionaries] <infile >outfile" outputs all
32 ;; lines of infile that don't appear in the dictionaries. The options
33 ;; can define arbitrary character code mappings of 8-bit characters.
34 ;; The default mapping takes a-z into A-Z, otherwise is ASCII.
35
36 (defvar wordtest-filter "extex" ;; maybe "/usr/local/bin/extex" is better?
37 "*Command to run the filter needed by wordtest.")
38 (make-variable-buffer-local 'wordtest-filter)
39
40 ;; The extex filter extracts words from its input and outputs them on
41 ;; separate lines as required by wordtest. It removes TeX control
42 ;; sequences except those used to make accents and special characters.
43 ;; There's a companion filter excweb that also removes C code from CWEBs.
44 (setq cweb-mode-hook '(lambda () (setq wordtest-filter "excweb")))
45
46 (defvar wordtest-system-dictionary "/usr/local/lib/dict/words"
47 "*Sorted dictionary containing all \"correct\" words,
48 including all variant forms obtained by prefix and suffix transformations.")
49 ;; The standard UNIX dictionary /usr/dict/words is NOT satisfactory.
50
51 (defvar wordtest-personal-dictionary "~/.words"
52 "*Default dictionary to supplement the words in the system dictionary.
53 If nil, no supplementary dictionary will be used.
54 This dictionary must be in alphabetic order as defined by wordtest.
55 Inserting any word with the + option to kspell-region will sort the file.")
56 (make-variable-buffer-local 'wordtest-personal-dictionary)
57
58 (defun set-personal-dictionary (filename)
59 "Defines the supplementary personal dictionary for kspell to use in the
60 current buffer, overriding the default value of wordtest-personal-dictionary."
61 (interactive "FPersonal dictionary file name: ")
62 (setq wordtest-personal-dictionary filename))
63
64 (defun unset-personal-dictionary ()
65 "Tells kspell not to use personal spelling dictionary with current buffer."
66 (interactive)
67 (setq wordtest-personal-dictionary nil))
68
69 (defun insert-into-personal-dictionary (word)
70 "Put WORD into user's dictionary and sort that dictionary."
71 (interactive "sword: ")
72 (let ((xword (concat word "\n")))
73 (if (null wordtest-personal-dictionary)
74 (setq wordtest-personal-dictionary
75 (read-string "Personal dictionary file name: " "~/.words")))
76 (set-buffer (find-file-noselect wordtest-personal-dictionary))
77 (goto-char (point-min))
78 (insert xword)
79 (call-process-region (point-min) (point-max) shell-file-name
80 t t nil "-c" wordtest-command)
81 (search-backward xword (point-min) 1) ;; in case the user is watching
82 (while (not (bolp)) (search-backward xword (point-min) 1))
83 (save-buffer)))
84
85 (defun kspell-buffer ()
86 "Check spelling of every word in the buffer.
87 For each incorrect word, you are asked for the correct spelling
88 and then put into a query-replace to fix some or all occurrences.
89
90 If you do not want to change a word, just give the same word
91 as its \"incorrect\" spelling; then the query replace is skipped.
92 Words are given in lowercase, but they will be Capitalized when
93 replacing Capitalized words, ALL_CAPS when replacing ALL_CAPS words.
94 If you type ? after a replacement, your correction will first be
95 looked up in the dictionary, and the query-replace will occur
96 only if the replacement is found. If you type + after a replacement,
97 your replacement will be inserted into the current personal dictionary.
98
99 You can leave the minibuffer to do some other editing and then come
100 back again to the query-replace loop by typing \\[other-window]."
101 (interactive)
102 (save-excursion (kspell-region (point-min) (point-max) "buffer")))
103
104 (defun kspell-word ()
105 "Check spelling of the word at or before point.
106 If it is not correct, ask user for the correct spelling and
107 query-replace the entire buffer to substitute it as with kspell-buffer."
108 (interactive)
109 (let (beg end wordtest-filter)
110 (save-excursion
111 (if (not (looking-at "\\<"))
112 (forward-word -1))
113 (setq beg (point))
114 (forward-word 1)
115 (setq end (point))
116 (kspell-region beg end (buffer-substring beg end)))))
117
118 (defun kspell-region (start end &optional description)
119 "Like kspell-buffer but checks only words in the current region.
120 Used in a program, applies from START to END.
121 DESCRIPTION is an optional string naming the unit being checked:
122 for example, \"buffer\"."
123 (interactive "r")
124 (let (correct
125 (filter wordtest-filter)
126 (buf (get-buffer-create " *kspell*")) ;; hidden by list-buffers
127 (dicts wordtest-system-dictionary))
128 (if wordtest-personal-dictionary
129 (setq dicts (concat dicts " " wordtest-personal-dictionary)))
130 (save-excursion
131 (save-excursion
132 (set-buffer buf)
133 (widen)
134 (erase-buffer))
135 (message "Checking spelling of %s..." (or description "region"))
136 (if (and (null filter)
137 (< end (point-max))
138 (= ?\n (char-after end)))
139 (call-process-region start (1+ end) shell-file-name
140 nil buf nil "-c"
141 (concat wordtest-command " " dicts))
142 (let ((oldbuf (current-buffer)))
143 (save-excursion
144 (set-buffer buf)
145 (insert-buffer-substring oldbuf start end)
146 (or (bolp) (insert ?\n))
147 (if filter
148 (call-process-region (point-min) (point-max) shell-file-name
149 t t nil "-c" filter))
150 (call-process-region (point-min) (point-max) shell-file-name
151 t t nil "-c"
152 (concat wordtest-command " " dicts)))))
153 (setq correct (save-excursion (set-buffer buf) (= (buffer-size) 0)))
154 (message "Checking spelling of %s...%scorrect"
155 (or description "region")
156 (if correct "" "not "))
157 (if correct t
158 (let (word newword qtext lastchar
159 (case-fold-search t)
160 (case-replace t))
161 (while (save-excursion
162 (set-buffer buf)
163 (> (buffer-size) 0))
164 (save-excursion
165 (set-buffer buf)
166 (goto-char (point-min))
167 (setq word (downcase
168 (buffer-substring (point)
169 (progn (end-of-line) (point)))))
170 (forward-char 1) ;; pass the newline
171 (delete-region (point-min) (point))
172 (setq qtext (concat "\\b" (regexp-quote word) "\\b")))
173 (goto-char (point-min))
174 (setq lastchar nil)
175 (if (re-search-forward qtext nil t)
176 (while (null lastchar)
177 (setq newword
178 (read-string
179 (concat "edit a replacement for `" word "': ")
180 word))
181 (if (null newword) (setq lastchar 0)
182 (setq lastchar (string-to-char (substring newword -1)))
183 (if (memq lastchar '(?? ?+))
184 (setq newword (substring newword 0 -1))))
185 (cond ((= lastchar ??)
186 (cond ((or (string= word newword) (string= "" newword))
187 (describe-function 'kspell-buffer)
188 (setq lastchar nil))
189 ((not (kspelt-right newword))
190 (setq lastchar nil))))
191 ((= lastchar ?+)
192 (save-excursion
193 (insert-into-personal-dictionary newword))))
194 (cond ((string= word newword))
195 ((null lastchar))
196 (t
197 (goto-char (point-min))
198 (if (or (equal word newword) (null lastchar)) t
199 (query-replace-regexp qtext newword))))))))))))
200
201 (defun kspelt-right (word)
202 "T if WORD is in the system dictionary or user's personal dictionary."
203 (let ((buf (get-buffer-create " *temp*"))
204 (pdict wordtest-personal-dictionary))
205 (message "Checking spelling of %s..." word)
206 (save-excursion
207 (set-buffer buf)
208 (widen)
209 (erase-buffer)
210 (insert word "\n")
211 (if pdict
212 (call-process-region (point-min) (point-max) shell-file-name
213 t t nil "-c"
214 (concat wordtest-command " "
215 wordtest-system-dictionary " "
216 pdict))
217 (call-process-region (point-min) (point-max) shell-file-name
218 t t nil "-c"
219 (concat wordtest-command " "
220 wordtest-system-dictionary)))
221 (= 0 (buffer-size)))))
222
223 (defun kspell-string (string)
224 "Check spelling of string supplied as argument."
225 (interactive "sSpell string: ")
226 (message "%s is %scorrect" string
227 (if (kspelt-right string) "" "in")))
228