* test/automated/eshell.el (with-temp-eshell): Use a temp directory
[emacs.git] / lisp / erc / erc-spelling.el
blob4866cacb99f917d333bc3887587f221b702afbba
1 ;;; erc-spelling.el --- use flyspell in ERC
3 ;; Copyright (C) 2005-2013 Free Software Foundation, Inc.
5 ;; Author: Jorgen Schaefer <forcer@forcix.cx>
6 ;; Maintainer: FSF
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 <http://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-p '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 (let ((word-data (and (boundp 'flyspell-word)
89 flyspell-word)))
90 (when word-data
91 (cond ((< (point) erc-input-marker)
92 nil)
93 ;; don't spell-check names of users
94 ((and erc-channel-users
95 (erc-get-channel-user (car word-data)))
96 (erc-spelling-unhighlight-word word-data)
97 nil)
98 ;; if '/' occurs before the word, don't spell-check it
99 ((eq (char-before (nth 1 word-data)) ?/)
100 (erc-spelling-unhighlight-word word-data)
101 nil)
102 (t t)))))
104 (put 'erc-mode
105 'flyspell-mode-predicate
106 'erc-spelling-flyspell-verify)
108 (provide 'erc-spelling)
110 ;;; erc-spelling.el ends here