Added or corrected Commentary sections
[bpt/emacs.git] / lisp / textmodes / ispell4.el
CommitLineData
1a06eabd
ER
1;;; ispell.el --- this is the GNU EMACS interface to GNU ISPELL version 3.
2
3;;Copyright (C) 1990, 1991 Free Software Foundation, Inc.
9750e079 4
e9571d2a
ER
5;; Keywords: wp
6
6370a710 7;;This file is part of GNU Emacs.
d299ee07 8;;
6370a710 9;;GNU Emacs is free software; you can redistribute it and/or modify
d299ee07 10;;it under the terms of the GNU General Public License as published by
c47e1198 11;;the Free Software Foundation; either version 2, or (at your option)
d299ee07
RS
12;;any later version.
13;;
6370a710 14;;GNU Emacs is distributed in the hope that it will be useful,
d299ee07
RS
15;;but WITHOUT ANY WARRANTY; without even the implied warranty of
16;;MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17;;GNU General Public License for more details.
18;;
19;;You should have received a copy of the GNU General Public License
6370a710 20;;along with GNU Emacs; see the file COPYING. If not, write to
d299ee07
RS
21;;the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
e41b2db1
ER
23;;; Commentary:
24
25;; This package provides a graceful interface to ispell, the GNU
26;; spelling checker.
27
282d89c0
ER
28;;; Code:
29
d299ee07
RS
30(defvar ispell-have-new-look t
31 "T if default 'look' program has the -r flag.")
32
33(defvar ispell-enable-tex-parser nil
34 "T to enable experimental tex parser in ispell for tex buffers.")
35
36(defvar ispell-process nil "The process running ISPELL")
37(defvar ispell-next-message nil
38 "An integer telling where in the *ispell* buffer where
39to look for the next message from the ISPELL program.")
40
41;Each marker in this list points to the start of a word that
42;ispell thought was bad last time it did the :file command.
43;Notice that if the user accepts or inserts a word into his
44;private dictionary, then some "good" words will be on the list.
45;We would like to deal with this by looking up the words again just before
46;presenting them to the user, but that is too slow on machines
47;without the select system call. Therefore, see the variable
48;ispell-recently-accepted.
49(defvar ispell-bad-words nil
50 "A list of markers corresponding to the output of the ISPELL :file command.")
51
52;list of words that the user has accepted, but that might still
53;be on the bad-words list
54(defvar ispell-recently-accepted nil)
55
56;t when :dump command needed
57(defvar ispell-dump-needed nil)
58
59(defun ispell-flush-bad-words ()
60 (while ispell-bad-words
61 (if (markerp (car ispell-bad-words))
62 (set-marker (car ispell-bad-words) nil))
63 (setq ispell-bad-words (cdr ispell-bad-words)))
64 (setq ispell-recently-accepted nil))
65
66(defun kill-ispell ()
67 "Kill the ispell process.
68Any changes the your private dictionay
69that have not already been dumped will be lost."
70 (interactive)
71 (if ispell-process
72 (delete-process ispell-process))
73 (setq ispell-process nil)
74 (ispell-flush-bad-words))
75
76(put 'ispell-startup-error 'error-conditions
77 '(ispell-startup-error error))
78(put 'ispell-startup-error 'error-message
79 "Problem starting ispell - see buffer *ispell*")
80
81(defun start-ispell ()
82 "Start an ispell subprocess; check the version; and display the greeting."
83 (message "Starting ispell ...")
84 (let ((buf (get-buffer "*ispell*")))
85 (if buf
86 (kill-buffer buf)))
87 (condition-case err
88 (setq ispell-process (start-process "ispell" "*ispell*" "ispell" "-S"))
89 (file-error (signal 'ispell-startup-error nil)))
90 (process-kill-without-query ispell-process)
91 (buffer-disable-undo (process-buffer ispell-process))
92 (accept-process-output ispell-process)
93 (let (last-char)
94 (save-excursion
95 (set-buffer (process-buffer ispell-process))
96 (bury-buffer (current-buffer))
97 (setq last-char (- (point-max) 1))
98 (while (not (eq (char-after last-char) ?=))
99 (cond ((not (eq (process-status ispell-process) 'run))
100 (kill-ispell)
101 (signal 'ispell-startup-error nil)))
102 (accept-process-output ispell-process)
103 (setq last-char (- (point-max) 1)))
104 (goto-char (point-min))
105 (let ((greeting (read (current-buffer))))
106 (if (not (= (car greeting) 1))
107 (error "Bad ispell version: wanted 1, got %d" (car greeting)))
108 (message (car (cdr greeting))))
109 (delete-region (point-min) last-char))))
110
111;leaves buffer set to *ispell*, point at '='
112(defun ispell-sync (intr)
113 "Make sure ispell is ready for a command."
114 (if (or (null ispell-process)
115 (not (eq (process-status ispell-process) 'run)))
116 (start-ispell))
117 (if intr
118 (interrupt-process ispell-process))
119 (let (last-char)
120 (set-buffer (process-buffer ispell-process))
121 (bury-buffer (current-buffer))
122 (setq last-char (- (point-max) 1))
123 (while (not (eq (char-after last-char) ?=))
124 (accept-process-output ispell-process)
125 (setq last-char (- (point-max) 1)))
126 (goto-char last-char)))
127
128(defun ispell-cmd (&rest strings)
129 "Send a command to ispell. Choices are:
130
6370a710 131WORD Check spelling of WORD. Result is
d299ee07 132
6370a710
RS
133 nil not found
134 t spelled ok
135 list of strings near misses
d299ee07 136
6370a710 137:file FILENAME scan the named file, and print the file offsets of
d299ee07
RS
138 any misspelled words
139
6370a710 140:insert WORD put word in private dictonary
d299ee07 141
6370a710 142:accept WORD don't complain about word any more this session
d299ee07
RS
143
144:dump write out the current private dictionary, if necessary.
145
6370a710 146:reload reread `~/ispell.words'
d299ee07
RS
147
148:tex
149:troff
150:generic set type of parser to use when scanning whole files
151"
152 (save-excursion
153 (ispell-sync t)
154 (set-buffer (process-buffer ispell-process))
155 (bury-buffer (current-buffer))
156 (erase-buffer)
157 (setq ispell-next-message (point-min))
158 (while strings
159 (process-send-string ispell-process (car strings))
160 (setq strings (cdr strings)))
161 (process-send-string ispell-process "\n")
162 (accept-process-output ispell-process)
163 (ispell-sync nil)))
164
165(defun ispell-dump ()
166 (cond (ispell-dump-needed
167 (setq ispell-dump-needed nil)
168 (ispell-cmd ":dump"))))
169
170(defun ispell-insert (word)
171 (ispell-cmd ":insert " word)
172 (if ispell-bad-words
173 (setq ispell-recently-accepted (cons word ispell-recently-accepted)))
174 (setq ispell-dump-needed t))
175
176(defun ispell-accept (word)
177 (ispell-cmd ":accept " word)
178 (if ispell-bad-words
179 (setq ispell-recently-accepted (cons word ispell-recently-accepted))))
180
181
182(defun ispell-next-message ()
183 "Return the next message sent by the ispell subprocess."
184 (save-excursion
185 (set-buffer (process-buffer ispell-process))
186 (bury-buffer (current-buffer))
187 (save-restriction
188 (goto-char ispell-next-message)
189 (narrow-to-region (point)
190 (progn (forward-sexp 1) (point)))
191 (setq ispell-next-message (point))
192 (goto-char (point-min))
193 (read (current-buffer)))))
194
195(defun ispell-tex-buffer-p ()
196 (memq major-mode '(plain-TeX-mode LaTeX-mode)))
197
7229064d 198;;;###autoload
d299ee07 199(defun ispell (&optional buf start end)
6370a710
RS
200 "Run Ispell over current buffer's visited file.
201First the file is scanned for misspelled words, then Ispell
d299ee07
RS
202enters a loop with the following commands for every misspelled word:
203
204DIGIT Near miss selector. If the misspelled word is close to
205 some words in the dictionary, they are offered as near misses.
206r Replace. Replace the word with a string you type. Each word
207 of your new string is also checked.
208i Insert. Insert this word in your private dictonary (kept in
209 `$HOME/ispell.words').
210a Accept. Accept this word for the rest of this editing session,
211 but don't put it in your private dictonary.
212l Lookup. Look for a word in the dictionary by fast binary
213 search, or search for a regular expression in the dictionary
214 using grep.
215SPACE Accept the word this time, but complain if it is seen again.
f30ff39f 216q, \\[keyboard-quit] Leave the command loop. You can come back later with \\[ispell-next]."
d299ee07
RS
217 (interactive)
218 (if (null start)
219 (setq start 0))
220 (if (null end)
221 (setq end 0))
222
223 (if (null buf)
224 (setq buf (current-buffer)))
225 (setq buf (get-buffer buf))
226 (if (null buf)
227 (error "Can't find buffer"))
228 (save-excursion
229 (set-buffer buf)
230 (let ((filename buffer-file-name)
231 (delete-temp nil))
232 (unwind-protect
233 (progn
234 (cond ((null filename)
235 (setq filename (make-temp-name "/usr/tmp/ispell"))
236 (setq delete-temp t)
237 (write-region (point-min) (point-max) filename))
238 ((and (buffer-modified-p buf)
239 (y-or-n-p (format "Save file %s? " filename)))
240 (save-buffer)))
241 (message "Ispell scanning file...")
242 (if (and ispell-enable-tex-parser
243 (ispell-tex-buffer-p))
244 (ispell-cmd ":tex")
245 (ispell-cmd ":generic"))
246 (ispell-cmd (format ":file %s %d %d" filename start end)))
247 (if delete-temp
248 (condition-case ()
249 (delete-file filename)
250 (file-error nil)))))
251 (message "Parsing ispell output ...")
252 (ispell-flush-bad-words)
253 (let (pos bad-words)
254 (while (numberp (setq pos (ispell-next-message)))
255 ;;ispell may check the words on the line following the end
256 ;;of the region - therefore, don't record anything out of range
257 (if (or (= end 0)
258 (< pos end))
259 (setq bad-words (cons (set-marker (make-marker) (+ pos 1))
260 bad-words))))
261 (setq bad-words (cons pos bad-words))
262 (setq ispell-bad-words (nreverse bad-words))))
263 (cond ((not (markerp (car ispell-bad-words)))
264 (setq ispell-bad-words nil)
265 (message "No misspellings."))
266 (t
267 (message "Ispell parsing done.")
268 (ispell-next))))
269
f305bd74
BP
270;;;###autoload
271(fset 'ispell-buffer 'ispell)
272
d299ee07
RS
273(defun ispell-next ()
274 "Resume command loop for most recent ispell command."
275 (interactive)
276 (unwind-protect
277 (catch 'quit
278 (save-window-excursion
279 (save-excursion
280 (let (next)
281 (while (markerp (setq next (car ispell-bad-words)))
282 (switch-to-buffer (marker-buffer next))
283 (push-mark)
284 (ispell-point next "at saved position.")
285 (setq ispell-bad-words (cdr ispell-bad-words))
286 (set-marker next nil))))))
287 (cond ((null ispell-bad-words)
288 (error "Ispell has not yet been run."))
289 ((markerp (car ispell-bad-words))
290 (message (substitute-command-keys
291 "Type \\[ispell-next] to continue.")))
292 ((eq (car ispell-bad-words) nil)
293 (setq ispell-bad-words nil)
294 (message "No more misspellings (but checker was interrupted.)"))
295 ((eq (car ispell-bad-words) t)
296 (setq ispell-bad-words nil)
297 (message "Ispell done."))
298 (t
299 (setq ispell-bad-words nil)
300 (message "Bad ispell internal list"))))
301 (ispell-dump))
302
7229064d 303;;;###autoload
6370a710 304(defun ispell-word (&optional resume)
d299ee07 305 "Check the spelling of the word under the cursor.
6370a710
RS
306See `ispell' for more information.
307With a prefix argument, resume handling of the previous Ispell command."
308 (interactive "P")
309 (if resume
310 (ispell-next)
311 (condition-case err
312 (catch 'quit
313 (save-window-excursion
314 (ispell-point (point) "at point."))
315 (ispell-dump))
316 (ispell-startup-error
317 (cond ((y-or-n-p "Problem starting ispell, use old-style spell instead? ")
318 (load-library "spell")
319 (define-key esc-map "$" 'spell-word)
320 (spell-word)))))))
f305bd74
BP
321;;;###autoload
322(define-key esc-map "$" 'ispell-word)
d299ee07 323
7229064d 324;;;###autoload
d299ee07
RS
325(defun ispell-region (start &optional end)
326 "Check the spelling for all of the words in the region."
327 (interactive "r")
328 (ispell (current-buffer) start end))
329
330(defun ispell-letterp (c)
331 (and c
332 (or (and (>= c ?A) (<= c ?Z))
333 (and (>= c ?a) (<= c ?z))
334 (>= c 128))))
335
336(defun ispell-letter-or-quotep (c)
337 (and c
338 (or (and (>= c ?A) (<= c ?Z))
339 (and (>= c ?a) (<= c ?z))
340 (= c ?')
341 (>= c 128))))
342
343(defun ispell-find-word-start ()
344 ;;backward to a letter
345 (if (not (ispell-letterp (char-after (point))))
346 (while (and (not (bobp))
347 (not (ispell-letterp (char-after (- (point) 1)))))
348 (backward-char)))
349 ;;backward to beginning of word
350 (while (ispell-letter-or-quotep (char-after (- (point) 1)))
351 (backward-char))
352 (skip-chars-forward "'"))
353
354(defun ispell-find-word-end ()
355 (while (ispell-letter-or-quotep (char-after (point)))
356 (forward-char))
357 (skip-chars-backward "'"))
358
359(defun ispell-next-word ()
360 (while (and (not (eobp))
361 (not (ispell-letterp (char-after (point)))))
362 (forward-char)))
363
364;if end is nil, then do one word at start
365;otherwise, do all words from the beginning of the word where
366;start points, to the end of the word where end points
367(defun ispell-point (start message)
368 (let ((wend (make-marker))
369 rescan
370 end)
371 (save-excursion
372 (goto-char start)
373 (ispell-find-word-start) ;find correct word start
374 (setq start (point-marker))
375 (ispell-find-word-end) ;now find correct end
376 (setq end (point-marker))
377 (if (>= start end)
378 (error "No word %s" message))
379 (while (< start end)
380 (goto-char start)
381 (ispell-find-word-end) ;find end of current word
382 ;could be before 'end' if
383 ;user typed replacement
384 ;that is more than one word
385 (set-marker wend (point))
386 (setq rescan nil)
387 (setq word (buffer-substring start wend))
388 (cond ((ispell-still-bad word)
389 (goto-char start);just to show user where we are working
390 (sit-for 0)
391 (message (format "Ispell checking %s" word))
392 (ispell-cmd word)
393 (let ((message (ispell-next-message)))
394 (cond ((eq message t)
395 (message "%s: ok" word))
396 ((or (null message)
397 (consp message))
398 (setq rescan
399 (ispell-command-loop word start wend message)))
400 (t
401 (error "unknown ispell response %s" message))))))
402 (cond ((null rescan)
403 (goto-char wend)
404 (ispell-next-word)
405 (set-marker start (point)))))
406 ;;clear the choices buffer; otherwise it's hard for the user to tell
407 ;;when we get back to the command loop
408 (let ((buf (get-buffer "*ispell choices*")))
409 (cond (buf
410 (set-buffer buf)
411 (erase-buffer))))
412 (set-marker start nil)
413 (set-marker end nil)
414 (set-marker wend nil))))
415
416(defun ispell-still-bad (word)
417 (let ((words ispell-recently-accepted)
418 (ret t)
419 (case-fold-search t))
420 (while words
421 (cond ((eq (string-match (car words) word) 0)
422 (setq ret nil)
423 (setq words nil)))
424 (setq words (cdr words)))
425 ret))
426
427(defun ispell-show-choices (word message first-line)
f98955ea 428 ;;if there is only one window on the frame, make the ispell
d299ee07
RS
429 ;;messages winow be small. otherwise just use the other window
430 (let* ((selwin (selected-window))
431 (resize (eq selwin (next-window)))
432 (buf (get-buffer-create "*ispell choices*"))
433 w)
434 (setq w (display-buffer buf))
435 (buffer-disable-undo buf)
436 (if resize
437 (unwind-protect
438 (progn
439 (select-window w)
440 (enlarge-window (- 6 (window-height w))))
441 (select-window selwin)))
442 (save-excursion
443 (set-buffer buf)
444 (bury-buffer buf)
445 (set-window-point w (point-min))
446 (set-window-start w (point-min))
447 (erase-buffer)
448 (insert first-line "\n")
449 (insert
450 "SPC skip; A accept; I insert; DIGIT select; R replace; \
451L lookup; Q quit\n")
452 (cond ((not (null message))
453 (let ((i 0))
454 (while (< i 3)
455 (let ((j 0))
456 (while (< j 3)
457 (let* ((n (+ (* j 3) i))
458 (choice (nth n message)))
459 (cond (choice
460 (let ((str (format "%d %s" n choice)))
461 (insert str)
462 (insert-char ? (- 20 (length str)))))))
463 (setq j (+ j 1))))
464 (insert "\n")
465 (setq i (+ i 1)))))))))
466
467(defun ispell-command-loop (word start end message)
468 (let ((flag t)
469 (rescan nil)
470 first-line)
471 (if (null message)
472 (setq first-line (concat "No near misses for '" word "'"))
473 (setq first-line (concat "Near misses for '" word "'")))
474 (while flag
475 (ispell-show-choices word message first-line)
476 (message "Ispell command: ")
477 (let ((c (downcase (read-char)))
478 replacement)
479 (cond ((and (>= c ?0)
480 (<= c ?9)
481 (setq replacement (nth (- c ?0) message)))
482 (ispell-replace start end replacement)
483 (setq flag nil))
484 ((= c ?q)
485 (throw 'quit nil))
486 ((= c ? )
487 (setq flag nil))
488 ((= c ?r)
489 (ispell-replace start end (read-string "Replacement: "))
490 (setq rescan t)
491 (setq flag nil))
492 ((= c ?i)
493 (ispell-insert word)
494 (setq flag nil))
495 ((= c ?a)
496 (ispell-accept word)
497 (setq flag nil))
498 ((= c ?l)
499 (let ((val (ispell-do-look word)))
500 (setq first-line (car val))
501 (setq message (cdr val))))
502 ((= c ??)
503 (message
504 "Type 'C-h d ispell' to the emacs main loop for more help")
505 (sit-for 2))
506 (t
507 (message "Bad ispell command")
508 (sit-for 2)))))
509 rescan))
510
511(defun ispell-do-look (bad-word)
512 (let (regex buf words)
513 (cond ((null ispell-have-new-look)
514 (setq regex (read-string "Lookup: ")))
515 (t
516 (setq regex (read-string "Lookup (regex): " "^"))))
517 (setq buf (get-buffer-create "*ispell look*"))
518 (save-excursion
519 (set-buffer buf)
520 (delete-region (point-min) (point-max))
521 (if ispell-have-new-look
522 (call-process "look" nil buf nil "-r" regex)
523 (call-process "look" nil buf nil regex))
524 (goto-char (point-min))
525 (forward-line 10)
526 (delete-region (point) (point-max))
527 (goto-char (point-min))
528 (while (not (= (point-min) (point-max)))
529 (end-of-line)
530 (setq words (cons (buffer-substring (point-min) (point)) words))
531 (forward-line)
532 (delete-region (point-min) (point)))
533 (kill-buffer buf)
534 (cons (format "Lookup '%s'" regex)
535 (reverse words)))))
536
537(defun ispell-replace (start end new)
538 (goto-char start)
539 (insert new)
540 (delete-region (point) end))
541
542(defun reload-ispell ()
543 "Tell ispell to re-read your private dictionary."
544 (interactive)
545 (ispell-cmd ":reload"))
546
d299ee07
RS
547(defun batch-make-ispell ()
548 (byte-compile-file "ispell.el")
549 (find-file "ispell.texinfo")
550 (let ((old-dir default-directory)
551 (default-directory "/tmp"))
552 (texinfo-format-buffer))
553 (Info-validate)
554 (if (get-buffer " *problems in info file*")
555 (kill-emacs 1))
556 (write-region (point-min) (point-max) "ispell.info"))
557
1a06eabd 558;;; ispell.el ends here