(ispell-point): Do nothing if there's no word at START.
[bpt/emacs.git] / lisp / textmodes / spell.el
1 ;;; spell.el --- spelling correction interface for Emacs.
2
3 ;; Copyright (C) 1985 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: wp, unix
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 ;;; Commentary:
25
26 ;; This mode provides an Emacs interface to the UNIX spell(1) program.
27 ;; Entry points are `spell-buffer', `spell-word', `spell-region' and
28 ;; `spell-string'. These facilities are documented in the Emacs user's
29 ;; manual.
30
31 ;;; Code:
32
33 (defvar spell-command "spell"
34 "*Command to run the spell program.")
35
36 (defvar spell-filter nil
37 "*Filter function to process text before passing it to spell program.
38 This function might remove text-processor commands.
39 nil means don't alter the text before checking it.")
40
41 ;;;###autoload
42 (defun spell-buffer ()
43 "Check spelling of every word in the buffer.
44 For each incorrect word, you are asked for the correct spelling
45 and then put into a query-replace to fix some or all occurrences.
46 If you do not want to change a word, just give the same word
47 as its \"correct\" spelling; then the query replace is skipped."
48 (interactive)
49 (spell-region (point-min) (point-max) "buffer"))
50
51 ;;;###autoload
52 (defun spell-word ()
53 "Check spelling of word at or before point.
54 If it is not correct, ask user for the correct spelling
55 and `query-replace' the entire buffer to substitute it."
56 (interactive)
57 (let (beg end spell-filter)
58 (save-excursion
59 (if (not (looking-at "\\<"))
60 (forward-word -1))
61 (setq beg (point))
62 (forward-word 1)
63 (setq end (point)))
64 (spell-region beg end (buffer-substring beg end))))
65
66 ;;;###autoload
67 (defun spell-region (start end &optional description)
68 "Like `spell-buffer' but applies only to region.
69 Used in a program, applies from START to END.
70 DESCRIPTION is an optional string naming the unit being checked:
71 for example, \"word\"."
72 (interactive "r")
73 (let ((filter spell-filter)
74 (buf (get-buffer-create " *temp*")))
75 (save-excursion
76 (set-buffer buf)
77 (widen)
78 (erase-buffer))
79 (message "Checking spelling of %s..." (or description "region"))
80 (if (and (null filter) (= ?\n (char-after (1- end))))
81 (if (string= "spell" spell-command)
82 (call-process-region start end "spell" nil buf)
83 (call-process-region start end shell-file-name
84 nil buf nil "-c" spell-command))
85 (let ((oldbuf (current-buffer)))
86 (save-excursion
87 (set-buffer buf)
88 (insert-buffer-substring oldbuf start end)
89 (or (bolp) (insert ?\n))
90 (if filter (funcall filter))
91 (if (string= "spell" spell-command)
92 (call-process-region (point-min) (point-max) "spell" t buf)
93 (call-process-region (point-min) (point-max) shell-file-name
94 t buf nil "-c" spell-command)))))
95 (message "Checking spelling of %s...%s"
96 (or description "region")
97 (if (save-excursion
98 (set-buffer buf)
99 (> (buffer-size) 0))
100 "not correct"
101 "correct"))
102 (let (word newword
103 (case-fold-search t)
104 (case-replace t))
105 (while (save-excursion
106 (set-buffer buf)
107 (> (buffer-size) 0))
108 (save-excursion
109 (set-buffer buf)
110 (goto-char (point-min))
111 (setq word (downcase
112 (buffer-substring (point)
113 (progn (end-of-line) (point)))))
114 (forward-char 1)
115 (delete-region (point-min) (point))
116 (setq newword
117 (read-input (concat "`" word
118 "' not recognized; edit a replacement: ")
119 word))
120 (flush-lines (concat "^" (regexp-quote word) "$")))
121 (if (not (equal word newword))
122 (progn
123 (goto-char (point-min))
124 (query-replace-regexp (concat "\\b" (regexp-quote word) "\\b")
125 newword)))))))
126
127
128 ;;;###autoload
129 (defun spell-string (string)
130 "Check spelling of string supplied as argument."
131 (interactive "sSpell string: ")
132 (let ((buf (get-buffer-create " *temp*")))
133 (save-excursion
134 (set-buffer buf)
135 (widen)
136 (erase-buffer)
137 (insert string "\n")
138 (if (string= "spell" spell-command)
139 (call-process-region (point-min) (point-max) "spell"
140 t t)
141 (call-process-region (point-min) (point-max) shell-file-name
142 t t nil "-c" spell-command))
143 (if (= 0 (buffer-size))
144 (message "%s is correct" string)
145 (goto-char (point-min))
146 (while (search-forward "\n" nil t)
147 (replace-match " "))
148 (message "%sincorrect" (buffer-substring 1 (point-max)))))))
149
150 ;;; spell.el ends here