summary refs log tree commit diff stats
path: root/lisp/+ispell.el
blob: e35b2f1af561a363e7e605c9bc9b1af9b8e22ba4 (plain)
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
;;; +ispell.el --- Customizations for `ispell' -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl)

;; Utility function TODO: move elsewhere
(defun +ispell-append-removing-duplicates (&rest lists)
  "Append LISTS, removing duplicates from the result.
Any keyword arguments to `cl-remove-duplicates' should come
before the LISTS."
  (let (cl-remove-duplicates-args)
    (while (keywordp (car lists))
      (push (pop lists) cl-remove-duplicates-args)
      (push (pop lists) cl-remove-duplicates-args))
    (apply #'cl-remove-duplicates (apply #'append lists)
           (nreverse cl-remove-duplicates-args))))

;;; Ispell in .dir-locals

;; Let Emacs know a list of strings is safe
(defun +ispell-safe-local-p (list)
  (and (listp list)
       (seq-every-p #'stringp list)))

;; Can I instruct ispell to insert LocalWords in a different file?
;; https://emacs.stackexchange.com/q/31396/2264

;; How can I move all my file-local LocalWords to .dir-locals.el?
;; https://emacs.stackexchange.com/q/31419

;; Adapted from ispell.el:ispell-buffer-local-words
(defun +ispell-buffer-local-words-list ()
  (let (words)
    (or ispell-buffer-local-name
        (setq ispell-buffer-local-name (buffer-name)))
    (save-excursion
      (goto-char (point-min))
      (while (search-forward ispell-words-keyword nil t)
        (let ((end (point-at-eol))
              (ispell-casechars (ispell-get-casechars))
              string)
          (while (re-search-forward " *\\([^ ]+\\)" end t)
            (setq string (match-string-no-properties 1))
            (if (and (< 1 (length string))
                     (equal 0 (string-match ispell-casechars string)))
                (push string words))))))
    words))

(defun +ispell-move-buffer-words-to-dir-locals ()
  (interactive)
  (unless (buffer-file-name)
    (user-error "Buffer not attached to file"))
  (hack-dir-local-variables)
  (let ((words (+ispell-buffer-local-words-list))
        (dir-local-words (+ispell-append-removing-duplicates
                          (alist-get 'ispell-buffer-session-localwords
                                     dir-local-variables-alist)
                          (alist-get 'ispell-buffer-session-localwords
                                     file-local-variables-alist))))
    (save-excursion
      (add-dir-local-variable
       major-mode
       'ispell-buffer-session-localwords
       (setq ispell-buffer-session-localwords
             (+ispell-append-removing-duplicates
              :test #'string=
              dir-local-words ispell-buffer-session-localwords words)))
      (when (y-or-n-p "Save .dir-locals.el?")
        (save-buffer))
      (bury-buffer))
    (or ispell-buffer-local-name
        (setq ispell-buffer-local-name (buffer-name)))
    (save-excursion
      (goto-char (point-min))
      (while (search-forward ispell-words-keyword nil t)
        (delete-region (point-at-bol) (1+ (point-at-eol)))))))

(provide '+ispell)
;;; +ispell.el ends here