Add 2010 to copyright years.
[bpt/emacs.git] / lisp / erc / erc-spelling.el
CommitLineData
597993cf
MB
1;;; erc-spelling.el --- use flyspell in ERC
2
114f9c96 3;; Copyright (C) 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
597993cf
MB
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
4ee57b2a 11;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 12;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
597993cf
MB
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
4ee57b2a 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
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)
2e3ef421 41 (dolist (buffer (erc-buffer-list))
ff59d266 42 (erc-spelling-init buffer)))
597993cf 43 ((remove-hook 'erc-connect-pre-hook 'erc-spelling-init)
2e3ef421 44 (dolist (buffer (erc-buffer-list))
ff59d266 45 (with-current-buffer buffer (flyspell-mode 0)))))
597993cf
MB
46
47(defcustom erc-spelling-dictionaries nil
48 "An alist mapping buffer names to dictionaries.
49The `car' of every cell is a buffer name, the `cadr' is the
50string name of an associated dictionary.
51The dictionary is inherited from server buffers, so if you want a
52default dictionary for some server, you can use a server buffer
53name here."
54 :type '(choice (const nil)
55 (repeat (cons (string :tag "Buffer name")
56 (string :tag "Dictionary"))))
57 :group 'erc-spelling)
58
ff59d266
MB
59(defun erc-spelling-init (buffer)
60 "Enable flyspell mode in an ERC buffer.
61The 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)))
597993cf 75
8508e990
MB
76(defun erc-spelling-unhighlight-word (word)
77 "Unhighlight the given WORD.
78The 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)))))
597993cf
MB
84
85(defun erc-spelling-flyspell-verify ()
86 "Flyspell only the input line, nothing else."
8508e990
MB
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)
597993cf
MB
106
107(provide 'erc-spelling)
108
109;; arch-tag: 04ae1c46-0fd1-4e1a-8b80-55bfa471c945
110;;; erc-spelling.el ends here