*** empty log message ***
[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 ;; This file is part of GNU Emacs.
6
7 ;; GNU Emacs 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 1, or (at your option)
10 ;; any later version.
11
12 ;; GNU Emacs 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.
16
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with GNU Emacs; see the file COPYING. If not, write to
19 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20
21
22 (defvar spell-command "spell"
23 "*Command to run the spell program.")
24
25 (defvar spell-filter nil
26 "*Filter function to process text before passing it to spell program.
27 This function might remove text-processor commands.
28 nil means don't alter the text before checking it.")
29
30 ;;;###autoload
31 (defun spell-buffer ()
32 "Check spelling of every word in the buffer.
33 For each incorrect word, you are asked for the correct spelling
34 and then put into a query-replace to fix some or all occurrences.
35 If you do not want to change a word, just give the same word
36 as its \"correct\" spelling; then the query replace is skipped."
37 (interactive)
38 (spell-region (point-min) (point-max) "buffer"))
39
40 ;;;###autoload
41 (defun spell-word ()
42 "Check spelling of word at or before point.
43 If it is not correct, ask user for the correct spelling
44 and query-replace the entire buffer to substitute it."
45 (interactive)
46 (let (beg end spell-filter)
47 (save-excursion
48 (if (not (looking-at "\\<"))
49 (forward-word -1))
50 (setq beg (point))
51 (forward-word 1)
52 (setq end (point)))
53 (spell-region beg end (buffer-substring beg end))))
54
55 ;;;###autoload
56 (defun spell-region (start end &optional description)
57 "Like spell-buffer but applies only to region.
58 Used in a program, applies from START to END.
59 DESCRIPTION is an optional string naming the unit being checked:
60 for example, \"word\"."
61 (interactive "r")
62 (let ((filter spell-filter)
63 (buf (get-buffer-create " *temp*")))
64 (save-excursion
65 (set-buffer buf)
66 (widen)
67 (erase-buffer))
68 (message "Checking spelling of %s..." (or description "region"))
69 (if (and (null filter) (= ?\n (char-after (1- end))))
70 (if (string= "spell" spell-command)
71 (call-process-region start end "spell" nil buf)
72 (call-process-region start end shell-file-name
73 nil buf nil "-c" spell-command))
74 (let ((oldbuf (current-buffer)))
75 (save-excursion
76 (set-buffer buf)
77 (insert-buffer-substring oldbuf start end)
78 (or (bolp) (insert ?\n))
79 (if filter (funcall filter))
80 (if (string= "spell" spell-command)
81 (call-process-region (point-min) (point-max) "spell" t buf)
82 (call-process-region (point-min) (point-max) shell-file-name
83 t buf nil "-c" spell-command)))))
84 (message "Checking spelling of %s...%s"
85 (or description "region")
86 (if (save-excursion
87 (set-buffer buf)
88 (> (buffer-size) 0))
89 "not correct"
90 "correct"))
91 (let (word newword
92 (case-fold-search t)
93 (case-replace t))
94 (while (save-excursion
95 (set-buffer buf)
96 (> (buffer-size) 0))
97 (save-excursion
98 (set-buffer buf)
99 (goto-char (point-min))
100 (setq word (downcase
101 (buffer-substring (point)
102 (progn (end-of-line) (point)))))
103 (forward-char 1)
104 (delete-region (point-min) (point))
105 (setq newword
106 (read-input (concat "`" word
107 "' not recognized; edit a replacement: ")
108 word))
109 (flush-lines (concat "^" (regexp-quote word) "$")))
110 (if (not (equal word newword))
111 (progn
112 (goto-char (point-min))
113 (query-replace-regexp (concat "\\b" (regexp-quote word) "\\b")
114 newword)))))))
115
116
117 ;;;###autoload
118 (defun spell-string (string)
119 "Check spelling of string supplied as argument."
120 (interactive "sSpell string: ")
121 (let ((buf (get-buffer-create " *temp*")))
122 (save-excursion
123 (set-buffer buf)
124 (widen)
125 (erase-buffer)
126 (insert string "\n")
127 (if (string= "spell" spell-command)
128 (call-process-region (point-min) (point-max) "spell"
129 t t)
130 (call-process-region (point-min) (point-max) shell-file-name
131 t t nil "-c" spell-command))
132 (if (= 0 (buffer-size))
133 (message "%s is correct" string)
134 (goto-char (point-min))
135 (while (search-forward "\n" nil t)
136 (replace-match " "))
137 (message "%sincorrect" (buffer-substring 1 (point-max)))))))
138
139 ;;; spell.el ends here