erc-spelling.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;;; erc-spelling.el --- use flyspell in ERC
  2. ;; Copyright (C) 2005-2017 Free Software Foundation, Inc.
  3. ;; Author: Jorgen Schaefer <forcer@forcix.cx>
  4. ;; Maintainer: emacs-devel@gnu.org
  5. ;; Keywords: irc
  6. ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcSpelling
  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 is an ERC module to enable flyspell mode in ERC buffers. This
  20. ;; ensures correct behavior of flyspell, and even sets up a
  21. ;; channel-local dictionary if so required.
  22. ;;; Code:
  23. (require 'erc)
  24. (require 'flyspell)
  25. ;;;###autoload (autoload 'erc-spelling-mode "erc-spelling" nil t)
  26. (define-erc-module spelling nil
  27. "Enable flyspell mode in ERC buffers."
  28. ;; Use erc-connect-pre-hook instead of erc-mode-hook as pre-hook is
  29. ;; called AFTER the server buffer is initialized.
  30. ((add-hook 'erc-connect-pre-hook 'erc-spelling-init)
  31. (dolist (buffer (erc-buffer-list))
  32. (erc-spelling-init buffer)))
  33. ((remove-hook 'erc-connect-pre-hook 'erc-spelling-init)
  34. (dolist (buffer (erc-buffer-list))
  35. (with-current-buffer buffer (flyspell-mode 0)))))
  36. (defcustom erc-spelling-dictionaries nil
  37. "An alist mapping buffer names to dictionaries.
  38. The `car' of every cell is a buffer name, the `cadr' is the
  39. string name of an associated dictionary.
  40. The dictionary is inherited from server buffers, so if you want a
  41. default dictionary for some server, you can use a server buffer
  42. name here."
  43. :type '(choice (const nil)
  44. (repeat (cons (string :tag "Buffer name")
  45. (string :tag "Dictionary"))))
  46. :group 'erc-spelling)
  47. (defun erc-spelling-init (buffer)
  48. "Enable flyspell mode in an ERC buffer.
  49. The current buffer is given by BUFFER."
  50. (with-current-buffer buffer
  51. (let ((name (downcase (buffer-name)))
  52. (dicts erc-spelling-dictionaries))
  53. (when dicts
  54. (while (and dicts
  55. (not (string= name (downcase (caar dicts)))))
  56. (setq dicts (cdr dicts)))
  57. (setq ispell-local-dictionary
  58. (if dicts
  59. (cadr (car dicts))
  60. (erc-with-server-buffer ispell-local-dictionary)))))
  61. (setq flyspell-generic-check-word-predicate #'erc-spelling-flyspell-verify)
  62. (flyspell-mode 1)))
  63. (defun erc-spelling-unhighlight-word (word)
  64. "Unhighlight the given WORD.
  65. The cadr is the beginning and the caddr is the end."
  66. (let ((beg (nth 1 word))
  67. (end (nth 2 word)))
  68. (flyspell-unhighlight-at beg)
  69. (when (> end beg)
  70. (flyspell-unhighlight-at (1- end)))))
  71. (defun erc-spelling-flyspell-verify ()
  72. "Flyspell only the input line, nothing else."
  73. ;; FIXME: Don't use `flyspell-word'!
  74. (let ((word-data (and (boundp 'flyspell-word)
  75. flyspell-word)))
  76. (when word-data
  77. (cond ((< (point) erc-input-marker)
  78. nil)
  79. ;; don't spell-check names of users
  80. ((and erc-channel-users
  81. (erc-get-channel-user (car word-data)))
  82. (erc-spelling-unhighlight-word word-data)
  83. nil)
  84. ;; if '/' occurs before the word, don't spell-check it
  85. ((eq (char-before (nth 1 word-data)) ?/)
  86. (erc-spelling-unhighlight-word word-data)
  87. nil)
  88. (t t)))))
  89. (put 'erc-mode
  90. 'flyspell-mode-predicate
  91. 'erc-spelling-flyspell-verify)
  92. (provide 'erc-spelling)
  93. ;;; erc-spelling.el ends here