spell.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. ;;; spell.el --- spelling correction interface for Emacs
  2. ;; Copyright (C) 1985, 2001-2012 Free Software Foundation, Inc.
  3. ;; Maintainer: FSF
  4. ;; Keywords: wp, unix
  5. ;; Obsolete-since: 23.1
  6. ;; (not in obsolete/ directory then, but all functions marked obsolete)
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  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. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This mode provides an Emacs interface to the UNIX spell(1) program.
  20. ;; Entry points are `spell-buffer', `spell-word', `spell-region' and
  21. ;; `spell-string'.
  22. ;; See also ispell.el for an interface to the ispell program.
  23. ;;; Code:
  24. (defgroup spell nil
  25. "Interface to the UNIX spell(1) program."
  26. :prefix "spell-"
  27. :group 'applications)
  28. (defcustom spell-command "spell"
  29. "Command to run the spell program."
  30. :type 'string
  31. :group 'spell)
  32. (defcustom spell-filter nil
  33. "Filter function to process text before passing it to spell program.
  34. This function might remove text-processor commands.
  35. nil means don't alter the text before checking it."
  36. :type '(choice (const nil) function)
  37. :group 'spell)
  38. ;;;###autoload
  39. (put 'spell-filter 'risky-local-variable t)
  40. ;;;###autoload
  41. (defun spell-buffer ()
  42. "Check spelling of every word in the buffer.
  43. For each incorrect word, you are asked for the correct spelling
  44. and then put into a query-replace to fix some or all occurrences.
  45. If you do not want to change a word, just give the same word
  46. as its \"correct\" spelling; then the query replace is skipped."
  47. (interactive)
  48. ;; Don't warn about spell-region being obsolete.
  49. (with-no-warnings
  50. (spell-region (point-min) (point-max) "buffer")))
  51. ;;;###autoload
  52. (make-obsolete 'spell-buffer 'ispell-buffer "23.1")
  53. ;;;###autoload
  54. (defun spell-word ()
  55. "Check spelling of word at or before point.
  56. If it is not correct, ask user for the correct spelling
  57. and `query-replace' the entire buffer to substitute it."
  58. (interactive)
  59. (let (beg end spell-filter)
  60. (save-excursion
  61. (if (not (looking-at "\\<"))
  62. (forward-word -1))
  63. (setq beg (point))
  64. (forward-word 1)
  65. (setq end (point)))
  66. ;; Don't warn about spell-region being obsolete.
  67. (with-no-warnings
  68. (spell-region beg end (buffer-substring beg end)))))
  69. ;;;###autoload
  70. (make-obsolete 'spell-word 'ispell-word "23.1")
  71. ;;;###autoload
  72. (defun spell-region (start end &optional description)
  73. "Like `spell-buffer' but applies only to region.
  74. Used in a program, applies from START to END.
  75. DESCRIPTION is an optional string naming the unit being checked:
  76. for example, \"word\"."
  77. (interactive "r")
  78. (let ((filter spell-filter)
  79. (buf (get-buffer-create " *temp*")))
  80. (with-current-buffer buf
  81. (widen)
  82. (erase-buffer))
  83. (message "Checking spelling of %s..." (or description "region"))
  84. (if (and (null filter) (= ?\n (char-after (1- end))))
  85. (if (string= "spell" spell-command)
  86. (call-process-region start end "spell" nil buf)
  87. (call-process-region start end shell-file-name
  88. nil buf nil "-c" spell-command))
  89. (let ((oldbuf (current-buffer)))
  90. (with-current-buffer buf
  91. (insert-buffer-substring oldbuf start end)
  92. (or (bolp) (insert ?\n))
  93. (if filter (funcall filter))
  94. (if (string= "spell" spell-command)
  95. (call-process-region (point-min) (point-max) "spell" t buf)
  96. (call-process-region (point-min) (point-max) shell-file-name
  97. t buf nil "-c" spell-command)))))
  98. (message "Checking spelling of %s...%s"
  99. (or description "region")
  100. (if (with-current-buffer buf
  101. (> (buffer-size) 0))
  102. "not correct"
  103. "correct"))
  104. (let (word newword
  105. (case-fold-search t)
  106. (case-replace t))
  107. (while (with-current-buffer buf
  108. (> (buffer-size) 0))
  109. (with-current-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-string (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. ;;;###autoload
  127. (make-obsolete 'spell-region 'ispell-region "23.1")
  128. ;;;###autoload
  129. (defun spell-string (string)
  130. "Check spelling of string supplied as argument."
  131. (interactive "sSpell string: ")
  132. (with-temp-buffer
  133. (widen)
  134. (erase-buffer)
  135. (insert string "\n")
  136. (if (string= "spell" spell-command)
  137. (call-process-region (point-min) (point-max) "spell"
  138. t t)
  139. (call-process-region (point-min) (point-max) shell-file-name
  140. t t nil "-c" spell-command))
  141. (if (= 0 (buffer-size))
  142. (message "%s is correct" string)
  143. (goto-char (point-min))
  144. (while (search-forward "\n" nil t)
  145. (replace-match " "))
  146. (message "%sincorrect" (buffer-substring 1 (point-max))))))
  147. ;;;###autoload
  148. (make-obsolete 'spell-string "The `spell' package is obsolete - use `ispell'."
  149. "23.1")
  150. (provide 'spell)
  151. ;;; spell.el ends here