unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
blob f0a45683f4dc146e88d6a9799405fd67c7d1a220 5411 bytes (raw)
name: lisp/erc/erc-spelling.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  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
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
 
;;; erc-spelling.el --- use flyspell in ERC  -*- lexical-binding: t; -*-

;; Copyright (C) 2005-2025 Free Software Foundation, Inc.

;; Author: Jorgen Schaefer <forcer@forcix.cx>
;; Maintainer: Amin Bandali <bandali@gnu.org>, F. Jason Park <jp@neverwas.me>
;; Keywords: comm, irc
;; URL: https://www.emacswiki.org/emacs/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 3 of the License, 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.  If not, see <https://www.gnu.org/licenses/>.

;;; 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)

(defgroup erc-spelling nil
  "Flyspell integration for ERC."
  :group 'erc)

;;;###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)
   (unless erc--updating-modules-p
     (erc-with-all-buffers-of-server nil nil
       (erc-spelling-init (current-buffer)))))
  ((remove-hook 'erc-connect-pre-hook #'erc-spelling-init)
   (dolist (buffer (erc-buffer-list))
     (remove-hook 'flyspell-incorrect-hook #'erc-spelling--flyspell-check t)
     (with-current-buffer buffer (flyspell-mode 0)))))

(defcustom erc-spelling-dictionaries nil
  "An alist mapping buffer names to dictionaries.

Each element is a list of the form (KEY VALUE), where KEY is a buffer
name and VALUE a locale or dictionary name known to `ispell', for
example: ((\"Libera.Chat\" \"en_US\") (\"#esperanto\" \"esperanto\")).

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 (list (string :tag "Buffer name")
                               (string :tag "Dictionary")))))

(defun erc-spelling-init (buffer)
  "Enable flyspell mode in an ERC buffer.
The current buffer is given by BUFFER."
  (with-current-buffer buffer
    (let ((name (downcase (buffer-name)))
          (dicts erc-spelling-dictionaries))
      (when dicts
        (while (and dicts
                    (not (string= name (downcase (caar dicts)))))
          (setq dicts (cdr dicts)))
        (setq ispell-local-dictionary
              (if dicts
                  (cadr (car dicts))
                (erc-with-server-buffer ispell-local-dictionary)))))
    (add-hook 'flyspell-incorrect-hook #'erc-spelling--flyspell-check 20 t)
    (flyspell-mode 1)))

(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)))))

(defun erc-spelling-flyspell-verify ()
  "Flyspell only the input line, nothing else."
  (declare (obsolete erc-spelling--flyspell-input-p "31.1"))
  ;; FIXME: Don't use `flyspell-word'!
  (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)))))

;; Do this down here to avoid adding `with-suppressed-warnings'.
(make-obsolete 'erc-spelling-unhighlight-word
               "value from `flyspell-get-word' now unused" "31.1")

(defun erc-spelling--flyspell-check (beg end _)
  "Return non-nil and remove overlay if text between BEG and END is correct."
  (or (and erc-channel-users
           (erc-get-channel-user (buffer-substring-no-properties beg end))
           (always (flyspell-unhighlight-at beg)))
      (and erc-input-marker (> beg erc-input-marker) (eq (char-before beg) ?/)
           (or (= beg (1+ erc-input-marker)) ; allow /unknown if initial
               (erc-command-symbol (buffer-substring-no-properties beg end)))
           (always (flyspell-unhighlight-at beg)))))

(defun erc-spelling--flyspell-input-p ()
  "Return non-nil if flyspell should check the prompt input at point."
  (>= (point) erc-input-marker))

(put 'erc-mode
     'flyspell-mode-predicate
     #'erc-spelling--flyspell-input-p)

(provide 'erc-spelling)

;;; erc-spelling.el ends here

;; Local Variables:
;; generated-autoload-file: "erc-loaddefs.el"
;; End:

debug log:

solving f0a45683f4d ...
found f0a45683f4d in https://yhetil.org/emacs-bugs/874j2drr6m.fsf__24936.7764846951$1736105738$gmane$org@neverwas.me/
found 0bfb20fce2e in https://yhetil.org/emacs-bugs/87zfk7qg7w.fsf@gmail.com/
found 01e587af368 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 01e587af3680e575f8df22e42e84803b080d7415	lisp/erc/erc-spelling.el

applying [1/2] https://yhetil.org/emacs-bugs/87zfk7qg7w.fsf@gmail.com/
diff --git a/lisp/erc/erc-spelling.el b/lisp/erc/erc-spelling.el
index 01e587af368..0bfb20fce2e 100644


applying [2/2] https://yhetil.org/emacs-bugs/874j2drr6m.fsf__24936.7764846951$1736105738$gmane$org@neverwas.me/
diff --git a/lisp/erc/erc-spelling.el b/lisp/erc/erc-spelling.el
index 0bfb20fce2e..f0a45683f4d 100644

Checking patch lisp/erc/erc-spelling.el...
Applied patch lisp/erc/erc-spelling.el cleanly.
Checking patch lisp/erc/erc-spelling.el...
Applied patch lisp/erc/erc-spelling.el cleanly.

skipping https://yhetil.org/emacs-bugs/874j2drr6m.fsf__24936.7764846951$1736105738$gmane$org@neverwas.me/ for f0a45683f4d
index at:
100644 f0a45683f4dc146e88d6a9799405fd67c7d1a220	lisp/erc/erc-spelling.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).