Merge from erc--emacs--21
[bpt/emacs.git] / lisp / erc / erc-spelling.el
CommitLineData
597993cf
MB
1;;; erc-spelling.el --- use flyspell in ERC
2
92ed2bfb 3;; Copyright (C) 2005, 2006 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
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 2, or (at your option)
14;; 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; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
25
26;;; Commentary:
27
28;; This is an ERC module to enable flyspell mode in ERC buffers. This
29;; ensures correct behavior of flyspell, and even sets up a
30;; channel-local dictionary if so required.
31
32;;; Code:
33
34(require 'erc)
35(require 'flyspell)
36
37;;;###autoload (autoload 'erc-spelling-mode "erc-spelling" nil t)
38(define-erc-module spelling nil
39 "Enable flyspell mode in ERC buffers."
40 ;; Use erc-connect-pre-hook instead of erc-mode-hook as pre-hook is
41 ;; called AFTER the server buffer is initialized.
42 ((add-hook 'erc-connect-pre-hook 'erc-spelling-init)
2e3ef421
MB
43 (dolist (buffer (erc-buffer-list))
44 (when (buffer-live-p buffer)
45 (with-current-buffer buffer (erc-spelling-init)))))
597993cf 46 ((remove-hook 'erc-connect-pre-hook 'erc-spelling-init)
2e3ef421
MB
47 (dolist (buffer (erc-buffer-list))
48 (when (buffer-live-p buffer)
49 (with-current-buffer buffer (flyspell-mode 0))))))
597993cf
MB
50
51(defcustom erc-spelling-dictionaries nil
52 "An alist mapping buffer names to dictionaries.
53The `car' of every cell is a buffer name, the `cadr' is the
54string name of an associated dictionary.
55The dictionary is inherited from server buffers, so if you want a
56default dictionary for some server, you can use a server buffer
57name here."
58 :type '(choice (const nil)
59 (repeat (cons (string :tag "Buffer name")
60 (string :tag "Dictionary"))))
61 :group 'erc-spelling)
62
63(defun erc-spelling-init ()
64 "Enable flyspell mode in an ERC buffer."
65 (let ((name (downcase (buffer-name)))
66 (dicts erc-spelling-dictionaries))
8508e990
MB
67 (when dicts
68 (while (and dicts
69 (not (string= name (downcase (caar dicts)))))
70 (setq dicts (cdr dicts)))
71 (setq ispell-local-dictionary
72 (if dicts
73 (cadr (car dicts))
74 (let ((server (erc-server-buffer)))
75 (if server
76 (with-current-buffer server
77 ispell-local-dictionary)
78 nil))))))
597993cf
MB
79 (setq flyspell-generic-check-word-p 'erc-spelling-flyspell-verify)
80 (flyspell-mode 1))
81
8508e990
MB
82(defun erc-spelling-unhighlight-word (word)
83 "Unhighlight the given WORD.
84The cadr is the beginning and the caddr is the end."
85 (let ((beg (nth 1 word))
86 (end (nth 2 word)))
87 (flyspell-unhighlight-at beg)
88 (when (> end beg)
89 (flyspell-unhighlight-at (1- end)))))
597993cf
MB
90
91(defun erc-spelling-flyspell-verify ()
92 "Flyspell only the input line, nothing else."
8508e990
MB
93 (let ((word-data (and (boundp 'flyspell-word)
94 flyspell-word)))
95 (when word-data
96 (cond ((< (point) erc-input-marker)
97 nil)
98 ;; don't spell-check names of users
99 ((and erc-channel-users
100 (erc-get-channel-user (car word-data)))
101 (erc-spelling-unhighlight-word word-data)
102 nil)
103 ;; if '/' occurs before the word, don't spell-check it
104 ((eq (char-before (nth 1 word-data)) ?/)
105 (erc-spelling-unhighlight-word word-data)
106 nil)
107 (t t)))))
108
109(put 'erc-mode
110 'flyspell-mode-predicate
111 'erc-spelling-flyspell-verify)
597993cf
MB
112
113(provide 'erc-spelling)
114
115;; arch-tag: 04ae1c46-0fd1-4e1a-8b80-55bfa471c945
116;;; erc-spelling.el ends here