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