Remove redundant test in fns.c
[emacs.git] / lisp / erc / erc-spelling.el
blob3a34ea373972efd2634648fda5c709f286be5536
1 ;;; erc-spelling.el --- use flyspell in ERC
3 ;; Copyright (C) 2005-2018 Free Software Foundation, Inc.
5 ;; Author: Jorgen Schaefer <forcer@forcix.cx>
6 ;; Maintainer: emacs-devel@gnu.org
7 ;; Keywords: irc
8 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcSpelling
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This is an ERC module to enable flyspell mode in ERC buffers. This
28 ;; ensures correct behavior of flyspell, and even sets up a
29 ;; channel-local dictionary if so required.
31 ;;; Code:
33 (require 'erc)
34 (require 'flyspell)
36 ;;;###autoload(autoload 'erc-spelling-mode "erc-spelling" nil t)
37 (define-erc-module spelling nil
38 "Enable flyspell mode in ERC buffers."
39 ;; Use erc-connect-pre-hook instead of erc-mode-hook as pre-hook is
40 ;; called AFTER the server buffer is initialized.
41 ((add-hook 'erc-connect-pre-hook 'erc-spelling-init)
42 (dolist (buffer (erc-buffer-list))
43 (erc-spelling-init buffer)))
44 ((remove-hook 'erc-connect-pre-hook 'erc-spelling-init)
45 (dolist (buffer (erc-buffer-list))
46 (with-current-buffer buffer (flyspell-mode 0)))))
48 (defcustom erc-spelling-dictionaries nil
49 "An alist mapping buffer names to dictionaries.
50 The `car' of every cell is a buffer name, the `cadr' is the
51 string name of an associated dictionary.
52 The dictionary is inherited from server buffers, so if you want a
53 default dictionary for some server, you can use a server buffer
54 name here."
55 :type '(choice (const nil)
56 (repeat (cons (string :tag "Buffer name")
57 (string :tag "Dictionary"))))
58 :group 'erc-spelling)
60 (defun erc-spelling-init (buffer)
61 "Enable flyspell mode in an ERC buffer.
62 The current buffer is given by BUFFER."
63 (with-current-buffer buffer
64 (let ((name (downcase (buffer-name)))
65 (dicts erc-spelling-dictionaries))
66 (when dicts
67 (while (and dicts
68 (not (string= name (downcase (caar dicts)))))
69 (setq dicts (cdr dicts)))
70 (setq ispell-local-dictionary
71 (if dicts
72 (cadr (car dicts))
73 (erc-with-server-buffer ispell-local-dictionary)))))
74 (setq flyspell-generic-check-word-predicate #'erc-spelling-flyspell-verify)
75 (flyspell-mode 1)))
77 (defun erc-spelling-unhighlight-word (word)
78 "Unhighlight the given WORD.
79 The cadr is the beginning and the caddr is the end."
80 (let ((beg (nth 1 word))
81 (end (nth 2 word)))
82 (flyspell-unhighlight-at beg)
83 (when (> end beg)
84 (flyspell-unhighlight-at (1- end)))))
86 (defun erc-spelling-flyspell-verify ()
87 "Flyspell only the input line, nothing else."
88 ;; FIXME: Don't use `flyspell-word'!
89 (let ((word-data (and (boundp 'flyspell-word)
90 flyspell-word)))
91 (when word-data
92 (cond ((< (point) erc-input-marker)
93 nil)
94 ;; don't spell-check names of users
95 ((and erc-channel-users
96 (erc-get-channel-user (car word-data)))
97 (erc-spelling-unhighlight-word word-data)
98 nil)
99 ;; if '/' occurs before the word, don't spell-check it
100 ((eq (char-before (nth 1 word-data)) ?/)
101 (erc-spelling-unhighlight-word word-data)
102 nil)
103 (t t)))))
105 (put 'erc-mode
106 'flyspell-mode-predicate
107 'erc-spelling-flyspell-verify)
109 (provide 'erc-spelling)
111 ;;; erc-spelling.el ends here
113 ;; Local Variables:
114 ;; generated-autoload-file: "erc-loaddefs.el"
115 ;; End: