1 ;;; ispell.el --- this is the GNU EMACS interface to GNU ISPELL version 3.
3 ;;Copyright (C) 1990, 1991 Free Software Foundation, Inc.
5 ;;This file is part of GNU ISPELL.
7 ;;GNU ISPELL is free software; you can redistribute it and/or modify
8 ;;it under the terms of the GNU General Public License as published by
9 ;;the Free Software Foundation; either version 2, or (at your option)
12 ;;GNU ISPELL is distributed in the hope that it will be useful,
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.
17 ;;You should have received a copy of the GNU General Public License
18 ;;along with GNU ISPELL; see the file COPYING. If not, write to
19 ;;the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
21 (defvar ispell-have-new-look t
22 "T if default 'look' program has the -r flag.")
24 (defvar ispell-enable-tex-parser nil
25 "T to enable experimental tex parser in ispell for tex buffers.")
27 (defvar ispell-process nil
"The process running ISPELL")
28 (defvar ispell-next-message nil
29 "An integer telling where in the *ispell* buffer where
30 to look for the next message from the ISPELL program.")
32 ;Each marker in this list points to the start of a word that
33 ;ispell thought was bad last time it did the :file command.
34 ;Notice that if the user accepts or inserts a word into his
35 ;private dictionary, then some "good" words will be on the list.
36 ;We would like to deal with this by looking up the words again just before
37 ;presenting them to the user, but that is too slow on machines
38 ;without the select system call. Therefore, see the variable
39 ;ispell-recently-accepted.
40 (defvar ispell-bad-words nil
41 "A list of markers corresponding to the output of the ISPELL :file command.")
43 ;list of words that the user has accepted, but that might still
44 ;be on the bad-words list
45 (defvar ispell-recently-accepted nil
)
47 ;t when :dump command needed
48 (defvar ispell-dump-needed nil
)
50 (defun ispell-flush-bad-words ()
51 (while ispell-bad-words
52 (if (markerp (car ispell-bad-words
))
53 (set-marker (car ispell-bad-words
) nil
))
54 (setq ispell-bad-words
(cdr ispell-bad-words
)))
55 (setq ispell-recently-accepted nil
))
58 "Kill the ispell process.
59 Any changes the your private dictionay
60 that have not already been dumped will be lost."
63 (delete-process ispell-process
))
64 (setq ispell-process nil
)
65 (ispell-flush-bad-words))
67 (put 'ispell-startup-error
'error-conditions
68 '(ispell-startup-error error
))
69 (put 'ispell-startup-error
'error-message
70 "Problem starting ispell - see buffer *ispell*")
72 (defun start-ispell ()
73 "Start an ispell subprocess; check the version; and display the greeting."
74 (message "Starting ispell ...")
75 (let ((buf (get-buffer "*ispell*")))
79 (setq ispell-process
(start-process "ispell" "*ispell*" "ispell" "-S"))
80 (file-error (signal 'ispell-startup-error nil
)))
81 (process-kill-without-query ispell-process
)
82 (buffer-disable-undo (process-buffer ispell-process
))
83 (accept-process-output ispell-process
)
86 (set-buffer (process-buffer ispell-process
))
87 (bury-buffer (current-buffer))
88 (setq last-char
(- (point-max) 1))
89 (while (not (eq (char-after last-char
) ?
=))
90 (cond ((not (eq (process-status ispell-process
) 'run
))
92 (signal 'ispell-startup-error nil
)))
93 (accept-process-output ispell-process
)
94 (setq last-char
(- (point-max) 1)))
95 (goto-char (point-min))
96 (let ((greeting (read (current-buffer))))
97 (if (not (= (car greeting
) 1))
98 (error "Bad ispell version: wanted 1, got %d" (car greeting
)))
99 (message (car (cdr greeting
))))
100 (delete-region (point-min) last-char
))))
102 ;leaves buffer set to *ispell*, point at '='
103 (defun ispell-sync (intr)
104 "Make sure ispell is ready for a command."
105 (if (or (null ispell-process
)
106 (not (eq (process-status ispell-process
) 'run
)))
109 (interrupt-process ispell-process
))
111 (set-buffer (process-buffer ispell-process
))
112 (bury-buffer (current-buffer))
113 (setq last-char
(- (point-max) 1))
114 (while (not (eq (char-after last-char
) ?
=))
115 (accept-process-output ispell-process
)
116 (setq last-char
(- (point-max) 1)))
117 (goto-char last-char
)))
119 (defun ispell-cmd (&rest strings
)
120 "Send a command to ispell. Choices are:
122 word any word is checked for spelling. Result is
126 list of strings near misses
128 :file filename scan the named file, and print the file offsets of
131 :insert word put word in private dictonary
133 :accept word don't complain about word any more this session
135 :dump write out the current private dictionary, if necessary.
137 :reload reread ~/ispell.words
141 :generic set type of parser to use when scanning whole files
145 (set-buffer (process-buffer ispell-process
))
146 (bury-buffer (current-buffer))
148 (setq ispell-next-message
(point-min))
150 (process-send-string ispell-process
(car strings
))
151 (setq strings
(cdr strings
)))
152 (process-send-string ispell-process
"\n")
153 (accept-process-output ispell-process
)
156 (defun ispell-dump ()
157 (cond (ispell-dump-needed
158 (setq ispell-dump-needed nil
)
159 (ispell-cmd ":dump"))))
161 (defun ispell-insert (word)
162 (ispell-cmd ":insert " word
)
164 (setq ispell-recently-accepted
(cons word ispell-recently-accepted
)))
165 (setq ispell-dump-needed t
))
167 (defun ispell-accept (word)
168 (ispell-cmd ":accept " word
)
170 (setq ispell-recently-accepted
(cons word ispell-recently-accepted
))))
173 (defun ispell-next-message ()
174 "Return the next message sent by the ispell subprocess."
176 (set-buffer (process-buffer ispell-process
))
177 (bury-buffer (current-buffer))
179 (goto-char ispell-next-message
)
180 (narrow-to-region (point)
181 (progn (forward-sexp 1) (point)))
182 (setq ispell-next-message
(point))
183 (goto-char (point-min))
184 (read (current-buffer)))))
186 (defun ispell-tex-buffer-p ()
187 (memq major-mode
'(plain-TeX-mode LaTeX-mode
)))
190 (defun ispell (&optional buf start end
)
191 "Run ispell over current buffer's visited file.
192 First the file is scanned for misspelled words, then ispell
193 enters a loop with the following commands for every misspelled word:
195 DIGIT Near miss selector. If the misspelled word is close to
196 some words in the dictionary, they are offered as near misses.
197 r Replace. Replace the word with a string you type. Each word
198 of your new string is also checked.
199 i Insert. Insert this word in your private dictonary (kept in
200 `$HOME/ispell.words').
201 a Accept. Accept this word for the rest of this editing session,
202 but don't put it in your private dictonary.
203 l Lookup. Look for a word in the dictionary by fast binary
204 search, or search for a regular expression in the dictionary
206 SPACE Accept the word this time, but complain if it is seen again.
207 q, \\[keyboard-quit] Leave the command loop. You can come back later with \\[ispell-next]."
215 (setq buf
(current-buffer)))
216 (setq buf
(get-buffer buf
))
218 (error "Can't find buffer"))
221 (let ((filename buffer-file-name
)
225 (cond ((null filename
)
226 (setq filename
(make-temp-name "/usr/tmp/ispell"))
228 (write-region (point-min) (point-max) filename
))
229 ((and (buffer-modified-p buf
)
230 (y-or-n-p (format "Save file %s? " filename
)))
232 (message "Ispell scanning file...")
233 (if (and ispell-enable-tex-parser
234 (ispell-tex-buffer-p))
236 (ispell-cmd ":generic"))
237 (ispell-cmd (format ":file %s %d %d" filename start end
)))
240 (delete-file filename
)
242 (message "Parsing ispell output ...")
243 (ispell-flush-bad-words)
245 (while (numberp (setq pos
(ispell-next-message)))
246 ;;ispell may check the words on the line following the end
247 ;;of the region - therefore, don't record anything out of range
250 (setq bad-words
(cons (set-marker (make-marker) (+ pos
1))
252 (setq bad-words
(cons pos bad-words
))
253 (setq ispell-bad-words
(nreverse bad-words
))))
254 (cond ((not (markerp (car ispell-bad-words
)))
255 (setq ispell-bad-words nil
)
256 (message "No misspellings."))
258 (message "Ispell parsing done.")
262 (fset 'ispell-buffer
'ispell
)
264 (defun ispell-next ()
265 "Resume command loop for most recent ispell command."
269 (save-window-excursion
272 (while (markerp (setq next
(car ispell-bad-words
)))
273 (switch-to-buffer (marker-buffer next
))
275 (ispell-point next
"at saved position.")
276 (setq ispell-bad-words
(cdr ispell-bad-words
))
277 (set-marker next nil
))))))
278 (cond ((null ispell-bad-words
)
279 (error "Ispell has not yet been run."))
280 ((markerp (car ispell-bad-words
))
281 (message (substitute-command-keys
282 "Type \\[ispell-next] to continue.")))
283 ((eq (car ispell-bad-words
) nil
)
284 (setq ispell-bad-words nil
)
285 (message "No more misspellings (but checker was interrupted.)"))
286 ((eq (car ispell-bad-words
) t
)
287 (setq ispell-bad-words nil
)
288 (message "Ispell done."))
290 (setq ispell-bad-words nil
)
291 (message "Bad ispell internal list"))))
296 (defun ispell-word ()
297 "Check the spelling of the word under the cursor.
298 See `ispell' for more information."
302 (save-window-excursion
303 (ispell-point (point) "at point."))
305 (ispell-startup-error
306 (cond ((y-or-n-p "Problem starting ispell, use old-style spell instead? ")
307 (load-library "spell")
308 (define-key esc-map
"$" 'spell-word
)
311 (define-key esc-map
"$" 'ispell-word
)
314 (defun ispell-region (start &optional end
)
315 "Check the spelling for all of the words in the region."
317 (ispell (current-buffer) start end
))
319 (defun ispell-letterp (c)
321 (or (and (>= c ?A
) (<= c ?Z
))
322 (and (>= c ?a
) (<= c ?z
))
325 (defun ispell-letter-or-quotep (c)
327 (or (and (>= c ?A
) (<= c ?Z
))
328 (and (>= c ?a
) (<= c ?z
))
332 (defun ispell-find-word-start ()
333 ;;backward to a letter
334 (if (not (ispell-letterp (char-after (point))))
335 (while (and (not (bobp))
336 (not (ispell-letterp (char-after (- (point) 1)))))
338 ;;backward to beginning of word
339 (while (ispell-letter-or-quotep (char-after (- (point) 1)))
341 (skip-chars-forward "'"))
343 (defun ispell-find-word-end ()
344 (while (ispell-letter-or-quotep (char-after (point)))
346 (skip-chars-backward "'"))
348 (defun ispell-next-word ()
349 (while (and (not (eobp))
350 (not (ispell-letterp (char-after (point)))))
353 ;if end is nil, then do one word at start
354 ;otherwise, do all words from the beginning of the word where
355 ;start points, to the end of the word where end points
356 (defun ispell-point (start message
)
357 (let ((wend (make-marker))
362 (ispell-find-word-start) ;find correct word start
363 (setq start
(point-marker))
364 (ispell-find-word-end) ;now find correct end
365 (setq end
(point-marker))
367 (error "No word %s" message
))
370 (ispell-find-word-end) ;find end of current word
371 ;could be before 'end' if
372 ;user typed replacement
373 ;that is more than one word
374 (set-marker wend
(point))
376 (setq word
(buffer-substring start wend
))
377 (cond ((ispell-still-bad word
)
378 (goto-char start
);just to show user where we are working
380 (message (format "Ispell checking %s" word
))
382 (let ((message (ispell-next-message)))
383 (cond ((eq message t
)
384 (message "%s: ok" word
))
388 (ispell-command-loop word start wend message
)))
390 (error "unknown ispell response %s" message
))))))
394 (set-marker start
(point)))))
395 ;;clear the choices buffer; otherwise it's hard for the user to tell
396 ;;when we get back to the command loop
397 (let ((buf (get-buffer "*ispell choices*")))
401 (set-marker start nil
)
403 (set-marker wend nil
))))
405 (defun ispell-still-bad (word)
406 (let ((words ispell-recently-accepted
)
408 (case-fold-search t
))
410 (cond ((eq (string-match (car words
) word
) 0)
413 (setq words
(cdr words
)))
416 (defun ispell-show-choices (word message first-line
)
417 ;;if there is only one window on the screen, make the ispell
418 ;;messages winow be small. otherwise just use the other window
419 (let* ((selwin (selected-window))
420 (resize (eq selwin
(next-window)))
421 (buf (get-buffer-create "*ispell choices*"))
423 (setq w
(display-buffer buf
))
424 (buffer-disable-undo buf
)
429 (enlarge-window (- 6 (window-height w
))))
430 (select-window selwin
)))
434 (set-window-point w
(point-min))
435 (set-window-start w
(point-min))
437 (insert first-line
"\n")
439 "SPC skip; A accept; I insert; DIGIT select; R replace; \
441 (cond ((not (null message
))
446 (let* ((n (+ (* j
3) i
))
447 (choice (nth n message
)))
449 (let ((str (format "%d %s" n choice
)))
451 (insert-char ?
(- 20 (length str
)))))))
454 (setq i
(+ i
1)))))))))
456 (defun ispell-command-loop (word start end message
)
461 (setq first-line
(concat "No near misses for '" word
"'"))
462 (setq first-line
(concat "Near misses for '" word
"'")))
464 (ispell-show-choices word message first-line
)
465 (message "Ispell command: ")
466 (let ((c (downcase (read-char)))
468 (cond ((and (>= c ?
0)
470 (setq replacement
(nth (- c ?
0) message
)))
471 (ispell-replace start end replacement
)
478 (ispell-replace start end
(read-string "Replacement: "))
488 (let ((val (ispell-do-look word
)))
489 (setq first-line
(car val
))
490 (setq message
(cdr val
))))
493 "Type 'C-h d ispell' to the emacs main loop for more help")
496 (message "Bad ispell command")
500 (defun ispell-do-look (bad-word)
501 (let (regex buf words
)
502 (cond ((null ispell-have-new-look
)
503 (setq regex
(read-string "Lookup: ")))
505 (setq regex
(read-string "Lookup (regex): " "^"))))
506 (setq buf
(get-buffer-create "*ispell look*"))
509 (delete-region (point-min) (point-max))
510 (if ispell-have-new-look
511 (call-process "look" nil buf nil
"-r" regex
)
512 (call-process "look" nil buf nil regex
))
513 (goto-char (point-min))
515 (delete-region (point) (point-max))
516 (goto-char (point-min))
517 (while (not (= (point-min) (point-max)))
519 (setq words
(cons (buffer-substring (point-min) (point)) words
))
521 (delete-region (point-min) (point)))
523 (cons (format "Lookup '%s'" regex
)
526 (defun ispell-replace (start end new
)
529 (delete-region (point) end
))
531 (defun reload-ispell ()
532 "Tell ispell to re-read your private dictionary."
534 (ispell-cmd ":reload"))
536 (define-key esc-map
"$" 'ispell-word
)
537 ;; This conflicts with set-selective-display. What should we do???
538 ;;(define-key ctl-x-map "$" 'ispell-next)
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))
547 (if (get-buffer " *problems in info file*")
549 (write-region (point-min) (point-max) "ispell.info"))
551 ;;; ispell.el ends here