erc-spelling.el 4.11 KB
Newer Older
1 2
;;; erc-spelling.el --- use flyspell in ERC

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

;; Author: Jorgen Schaefer <forcer@forcix.cx>
;; Keywords: irc
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcSpelling

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.

;;; Commentary:

;; This is an ERC module to enable flyspell mode in ERC buffers. This
;; ensures correct behavior of flyspell, and even sets up a
;; channel-local dictionary if so required.

;;; Code:

(require 'erc)
(require 'flyspell)

;;;###autoload (autoload 'erc-spelling-mode "erc-spelling" nil t)
(define-erc-module spelling nil
  "Enable flyspell mode in ERC buffers."
  ;; Use erc-connect-pre-hook instead of erc-mode-hook as pre-hook is
  ;; called AFTER the server buffer is initialized.
  ((add-hook 'erc-connect-pre-hook 'erc-spelling-init)
Miles Bader's avatar
Miles Bader committed
43 44 45
   (dolist (buffer (erc-buffer-list))
     (when (buffer-live-p buffer)
       (with-current-buffer buffer (erc-spelling-init)))))
46
  ((remove-hook 'erc-connect-pre-hook 'erc-spelling-init)
Miles Bader's avatar
Miles Bader committed
47 48 49
   (dolist (buffer (erc-buffer-list))
     (when (buffer-live-p buffer)
       (with-current-buffer buffer (flyspell-mode 0))))))
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

(defcustom erc-spelling-dictionaries nil
  "An alist mapping buffer names to dictionaries.
The `car' of every cell is a buffer name, the `cadr' is the
string name of an associated dictionary.
The dictionary is inherited from server buffers, so if you want a
default dictionary for some server, you can use a server buffer
name here."
  :type '(choice (const nil)
                 (repeat (cons (string :tag "Buffer name")
                               (string :tag "Dictionary"))))
  :group 'erc-spelling)

(defun erc-spelling-init ()
  "Enable flyspell mode in an ERC buffer."
  (let ((name (downcase (buffer-name)))
        (dicts erc-spelling-dictionaries))
67 68 69 70 71 72 73 74 75 76 77 78
    (when dicts
      (while (and dicts
                  (not (string= name (downcase (caar dicts)))))
        (setq dicts (cdr dicts)))
      (setq ispell-local-dictionary
            (if dicts
                (cadr (car dicts))
              (let ((server (erc-server-buffer)))
                (if server
                    (with-current-buffer server
                      ispell-local-dictionary)
                  nil))))))
79 80 81
  (setq flyspell-generic-check-word-p 'erc-spelling-flyspell-verify)
  (flyspell-mode 1))

82 83 84 85 86 87 88 89
(defun erc-spelling-unhighlight-word (word)
  "Unhighlight the given WORD.
The cadr is the beginning and the caddr is the end."
  (let ((beg (nth 1 word))
        (end (nth 2 word)))
    (flyspell-unhighlight-at beg)
    (when (> end beg)
      (flyspell-unhighlight-at (1- end)))))
90 91 92

(defun erc-spelling-flyspell-verify ()
  "Flyspell only the input line, nothing else."
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
  (let ((word-data (and (boundp 'flyspell-word)
                        flyspell-word)))
    (when word-data
      (cond ((< (point) erc-input-marker)
             nil)
            ;; don't spell-check names of users
            ((and erc-channel-users
                  (erc-get-channel-user (car word-data)))
             (erc-spelling-unhighlight-word word-data)
             nil)
            ;; if '/' occurs before the word, don't spell-check it
            ((eq (char-before (nth 1 word-data)) ?/)
             (erc-spelling-unhighlight-word word-data)
             nil)
            (t t)))))

(put 'erc-mode
     'flyspell-mode-predicate
     'erc-spelling-flyspell-verify)
112 113 114 115 116

(provide 'erc-spelling)

;; arch-tag: 04ae1c46-0fd1-4e1a-8b80-55bfa471c945
;;; erc-spelling.el ends here