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