Commit | Line | Data |
---|---|---|
597993cf MB |
1 | ;;; erc-spelling.el --- use flyspell in ERC |
2 | ||
ba318903 | 3 | ;; Copyright (C) 2005-2014 Free Software Foundation, Inc. |
597993cf MB |
4 | |
5 | ;; Author: Jorgen Schaefer <forcer@forcix.cx> | |
34dc21db | 6 | ;; Maintainer: emacs-devel@gnu.org |
597993cf MB |
7 | ;; Keywords: irc |
8 | ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcSpelling | |
9 | ||
10 | ;; This file is part of GNU Emacs. | |
11 | ||
4ee57b2a | 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
597993cf | 13 | ;; it under the terms of the GNU General Public License as published by |
4ee57b2a GM |
14 | ;; the Free Software Foundation, either version 3 of the License, or |
15 | ;; (at your option) any later version. | |
597993cf MB |
16 | |
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. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
4ee57b2a | 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
597993cf MB |
24 | |
25 | ;;; Commentary: | |
26 | ||
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. | |
30 | ||
31 | ;;; Code: | |
32 | ||
33 | (require 'erc) | |
34 | (require 'flyspell) | |
35 | ||
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) | |
2e3ef421 | 42 | (dolist (buffer (erc-buffer-list)) |
ff59d266 | 43 | (erc-spelling-init buffer))) |
597993cf | 44 | ((remove-hook 'erc-connect-pre-hook 'erc-spelling-init) |
2e3ef421 | 45 | (dolist (buffer (erc-buffer-list)) |
ff59d266 | 46 | (with-current-buffer buffer (flyspell-mode 0))))) |
597993cf MB |
47 | |
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) | |
59 | ||
ff59d266 MB |
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))) | |
597993cf | 76 | |
8508e990 MB |
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))))) | |
597993cf MB |
85 | |
86 | (defun erc-spelling-flyspell-verify () | |
87 | "Flyspell only the input line, nothing else." | |
8508e990 MB |
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))))) | |
103 | ||
104 | (put 'erc-mode | |
105 | 'flyspell-mode-predicate | |
106 | 'erc-spelling-flyspell-verify) | |
597993cf MB |
107 | |
108 | (provide 'erc-spelling) | |
109 | ||
597993cf | 110 | ;;; erc-spelling.el ends here |