(spell-buffer, spell-word, spell-region)
[emacs.git] / lisp / textmodes / spell.el
blob7cb011ec7ab3156b39dfd04736b7c423ab08ff2c
1 ;;; spell.el --- spelling correction interface for Emacs
3 ;; Copyright (C) 1985, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Maintainer: FSF
7 ;; Keywords: wp, unix
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; This mode provides an Emacs interface to the UNIX spell(1) program.
29 ;; Entry points are `spell-buffer', `spell-word', `spell-region' and
30 ;; `spell-string'.
32 ;; See also ispell.el for an interface to the ispell program.
34 ;;; Code:
36 (defgroup spell nil
37 "Interface to the UNIX spell(1) program."
38 :prefix "spell-"
39 :group 'applications)
41 (defcustom spell-command "spell"
42 "*Command to run the spell program."
43 :type 'string
44 :group 'spell)
46 (defcustom spell-filter nil
47 "*Filter function to process text before passing it to spell program.
48 This function might remove text-processor commands.
49 nil means don't alter the text before checking it."
50 :type '(choice (const nil) function)
51 :group 'spell)
53 ;;;###autoload
54 (put 'spell-filter 'risky-local-variable t)
56 ;;;###autoload
57 (defun spell-buffer ()
58 "Check spelling of every word in the buffer.
59 For each incorrect word, you are asked for the correct spelling
60 and then put into a query-replace to fix some or all occurrences.
61 If you do not want to change a word, just give the same word
62 as its \"correct\" spelling; then the query replace is skipped."
63 (interactive)
64 (spell-region (point-min) (point-max) "buffer"))
65 ;;;###autoload
66 (make-obsolete 'spell-buffer 'ispell-buffer "23.1")
68 ;;;###autoload
69 (defun spell-word ()
70 "Check spelling of word at or before point.
71 If it is not correct, ask user for the correct spelling
72 and `query-replace' the entire buffer to substitute it."
73 (interactive)
74 (let (beg end spell-filter)
75 (save-excursion
76 (if (not (looking-at "\\<"))
77 (forward-word -1))
78 (setq beg (point))
79 (forward-word 1)
80 (setq end (point)))
81 (spell-region beg end (buffer-substring beg end))))
82 ;;;###autoload
83 (make-obsolete 'spell-word 'ispell-word "23.1")
85 ;;;###autoload
86 (defun spell-region (start end &optional description)
87 "Like `spell-buffer' but applies only to region.
88 Used in a program, applies from START to END.
89 DESCRIPTION is an optional string naming the unit being checked:
90 for example, \"word\"."
91 (interactive "r")
92 (let ((filter spell-filter)
93 (buf (get-buffer-create " *temp*")))
94 (save-excursion
95 (set-buffer buf)
96 (widen)
97 (erase-buffer))
98 (message "Checking spelling of %s..." (or description "region"))
99 (if (and (null filter) (= ?\n (char-after (1- end))))
100 (if (string= "spell" spell-command)
101 (call-process-region start end "spell" nil buf)
102 (call-process-region start end shell-file-name
103 nil buf nil "-c" spell-command))
104 (let ((oldbuf (current-buffer)))
105 (save-excursion
106 (set-buffer buf)
107 (insert-buffer-substring oldbuf start end)
108 (or (bolp) (insert ?\n))
109 (if filter (funcall filter))
110 (if (string= "spell" spell-command)
111 (call-process-region (point-min) (point-max) "spell" t buf)
112 (call-process-region (point-min) (point-max) shell-file-name
113 t buf nil "-c" spell-command)))))
114 (message "Checking spelling of %s...%s"
115 (or description "region")
116 (if (save-excursion
117 (set-buffer buf)
118 (> (buffer-size) 0))
119 "not correct"
120 "correct"))
121 (let (word newword
122 (case-fold-search t)
123 (case-replace t))
124 (while (save-excursion
125 (set-buffer buf)
126 (> (buffer-size) 0))
127 (save-excursion
128 (set-buffer buf)
129 (goto-char (point-min))
130 (setq word (downcase
131 (buffer-substring (point)
132 (progn (end-of-line) (point)))))
133 (forward-char 1)
134 (delete-region (point-min) (point))
135 (setq newword
136 (read-string (concat "`" word
137 "' not recognized; edit a replacement: ")
138 word))
139 (flush-lines (concat "^" (regexp-quote word) "$")))
140 (if (not (equal word newword))
141 (progn
142 (goto-char (point-min))
143 (query-replace-regexp (concat "\\b" (regexp-quote word) "\\b")
144 newword)))))))
145 ;;;###autoload
146 (make-obsolete 'spell-region 'ispell-region "23.1")
148 ;;;###autoload
149 (defun spell-string (string)
150 "Check spelling of string supplied as argument."
151 (interactive "sSpell string: ")
152 (let ((buf (get-buffer-create " *temp*")))
153 (save-excursion
154 (set-buffer buf)
155 (widen)
156 (erase-buffer)
157 (insert string "\n")
158 (if (string= "spell" spell-command)
159 (call-process-region (point-min) (point-max) "spell"
160 t t)
161 (call-process-region (point-min) (point-max) shell-file-name
162 t t nil "-c" spell-command))
163 (if (= 0 (buffer-size))
164 (message "%s is correct" string)
165 (goto-char (point-min))
166 (while (search-forward "\n" nil t)
167 (replace-match " "))
168 (message "%sincorrect" (buffer-substring 1 (point-max)))))))
169 ;;;###autoload
170 (make-obsolete 'spell-string "The `spell' package is obsolete - use `ispell'."
171 "23.1")
173 (provide 'spell)
175 ;;; arch-tag: 7eabb848-9c76-431a-bcdb-0e0592d2db04
176 ;;; spell.el ends here