spell.el 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;; Spelling correction interface for Emacs.
  2. ;; Copyright (C) 1985 Free Software Foundation, Inc.
  3. ;; This file is part of GNU Emacs.
  4. ;; GNU Emacs is distributed in the hope that it will be useful,
  5. ;; but WITHOUT ANY WARRANTY. No author or distributor
  6. ;; accepts responsibility to anyone for the consequences of using it
  7. ;; or for whether it serves any particular purpose or works at all,
  8. ;; unless he says so in writing. Refer to the GNU Emacs General Public
  9. ;; License for full details.
  10. ;; Everyone is granted permission to copy, modify and redistribute
  11. ;; GNU Emacs, but only under the conditions described in the
  12. ;; GNU Emacs General Public License. A copy of this license is
  13. ;; supposed to have been given to you along with GNU Emacs so you
  14. ;; can know your rights and responsibilities. It should be in a
  15. ;; file named COPYING. Among other things, the copyright notice
  16. ;; and this notice must be preserved on all copies.
  17. (defun spell-buffer ()
  18. "Check spelling of every word in the buffer.
  19. For each incorrect word, you are asked for the correct spelling
  20. and then put into a query-replace to fix some or all occurrences.
  21. If you do not want to change a word, just give the same word
  22. as its \"correct\" spelling; then the query replace is skipped."
  23. (interactive)
  24. (spell-region (point-min) (point-max) "buffer"))
  25. (defun spell-word ()
  26. "Check spelling of word at or before point.
  27. If it is not correct, ask user for the correct spelling
  28. and query-replace the entire buffer to substitute it."
  29. (interactive)
  30. (let (beg end)
  31. (save-excursion
  32. (if (not (looking-at "\\<"))
  33. (forward-word -1))
  34. (setq beg (point))
  35. (forward-word 1)
  36. (setq end (point)))
  37. (spell-region beg end (buffer-substring beg end))))
  38. (defun spell-region (start end &optional description)
  39. "Like spell-buffer but applies only to region.
  40. From program, applies from START to END."
  41. (interactive "r")
  42. (let ((buf (get-buffer-create " *temp*")))
  43. (save-excursion
  44. (set-buffer buf)
  45. (widen)
  46. (erase-buffer))
  47. (message "Checking spelling of %s..." (or description "region"))
  48. (if (= ?\n (char-after (1- end)))
  49. (call-process-region start end "spell"
  50. nil buf)
  51. (let ((oldbuf (current-buffer)))
  52. (save-excursion
  53. (set-buffer buf)
  54. (insert-buffer-substring oldbuf start end)
  55. (insert ?\n)
  56. (call-process-region (point-min) (point-max) "spell"
  57. t buf))))
  58. (message "Checking spelling of %s...%s"
  59. (or description "region")
  60. (if (save-excursion
  61. (set-buffer buf)
  62. (> (buffer-size) 0))
  63. "not correct"
  64. "correct"))
  65. (let (word newword
  66. (case-fold-search t)
  67. (case-replace t))
  68. (while (save-excursion
  69. (set-buffer buf)
  70. (> (buffer-size) 0))
  71. (save-excursion
  72. (set-buffer buf)
  73. (goto-char (point-min))
  74. (setq word (buffer-substring (point)
  75. (progn (end-of-line) (point))))
  76. (forward-char 1)
  77. (delete-region (point-min) (point))
  78. (setq newword (read-input (concat "Replacement for " word ": ")
  79. word))
  80. (flush-lines (concat "^" (regexp-quote word) "$")))
  81. (if (not (equal word newword))
  82. (progn
  83. (goto-char (point-min))
  84. (query-replace-regexp (concat "\\b" (regexp-quote word) "\\b")
  85. newword)))))))
  86. (defun spell-string (string)
  87. "Check spelling of string supplied as argument."
  88. (interactive "sSpell string: ")
  89. (let ((buf (get-buffer-create " *temp*")))
  90. (save-excursion
  91. (set-buffer buf)
  92. (widen)
  93. (erase-buffer)
  94. (insert string "\n")
  95. (call-process-region (point-min) (point-max) "spell"
  96. t t)
  97. (if (= 0 (buffer-size))
  98. (message "%s is correct" string)
  99. (goto-char (point-min))
  100. (while (search-forward "\n" nil t)
  101. (replace-match " "))
  102. (message "%sincorrect" (buffer-substring 1 (point-max)))))))