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