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