diff options
Diffstat (limited to 'lisp')
-rw-r--r-- | lisp/+ispell.el | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/lisp/+ispell.el b/lisp/+ispell.el new file mode 100644 index 0000000..e35b2f1 --- /dev/null +++ b/lisp/+ispell.el | |||
@@ -0,0 +1,82 @@ | |||
1 | ;;; +ispell.el --- Customizations for `ispell' -*- lexical-binding: t; -*- | ||
2 | |||
3 | ;;; Commentary: | ||
4 | |||
5 | ;;; Code: | ||
6 | |||
7 | (require 'cl) | ||
8 | |||
9 | ;; Utility function TODO: move elsewhere | ||
10 | (defun +ispell-append-removing-duplicates (&rest lists) | ||
11 | "Append LISTS, removing duplicates from the result. | ||
12 | Any keyword arguments to `cl-remove-duplicates' should come | ||
13 | before the LISTS." | ||
14 | (let (cl-remove-duplicates-args) | ||
15 | (while (keywordp (car lists)) | ||
16 | (push (pop lists) cl-remove-duplicates-args) | ||
17 | (push (pop lists) cl-remove-duplicates-args)) | ||
18 | (apply #'cl-remove-duplicates (apply #'append lists) | ||
19 | (nreverse cl-remove-duplicates-args)))) | ||
20 | |||
21 | ;;; Ispell in .dir-locals | ||
22 | |||
23 | ;; Let Emacs know a list of strings is safe | ||
24 | (defun +ispell-safe-local-p (list) | ||
25 | (and (listp list) | ||
26 | (seq-every-p #'stringp list))) | ||
27 | |||
28 | ;; Can I instruct ispell to insert LocalWords in a different file? | ||
29 | ;; https://emacs.stackexchange.com/q/31396/2264 | ||
30 | |||
31 | ;; How can I move all my file-local LocalWords to .dir-locals.el? | ||
32 | ;; https://emacs.stackexchange.com/q/31419 | ||
33 | |||
34 | ;; Adapted from ispell.el:ispell-buffer-local-words | ||
35 | (defun +ispell-buffer-local-words-list () | ||
36 | (let (words) | ||
37 | (or ispell-buffer-local-name | ||
38 | (setq ispell-buffer-local-name (buffer-name))) | ||
39 | (save-excursion | ||
40 | (goto-char (point-min)) | ||
41 | (while (search-forward ispell-words-keyword nil t) | ||
42 | (let ((end (point-at-eol)) | ||
43 | (ispell-casechars (ispell-get-casechars)) | ||
44 | string) | ||
45 | (while (re-search-forward " *\\([^ ]+\\)" end t) | ||
46 | (setq string (match-string-no-properties 1)) | ||
47 | (if (and (< 1 (length string)) | ||
48 | (equal 0 (string-match ispell-casechars string))) | ||
49 | (push string words)))))) | ||
50 | words)) | ||
51 | |||
52 | (defun +ispell-move-buffer-words-to-dir-locals () | ||
53 | (interactive) | ||
54 | (unless (buffer-file-name) | ||
55 | (user-error "Buffer not attached to file")) | ||
56 | (hack-dir-local-variables) | ||
57 | (let ((words (+ispell-buffer-local-words-list)) | ||
58 | (dir-local-words (+ispell-append-removing-duplicates | ||
59 | (alist-get 'ispell-buffer-session-localwords | ||
60 | dir-local-variables-alist) | ||
61 | (alist-get 'ispell-buffer-session-localwords | ||
62 | file-local-variables-alist)))) | ||
63 | (save-excursion | ||
64 | (add-dir-local-variable | ||
65 | major-mode | ||
66 | 'ispell-buffer-session-localwords | ||
67 | (setq ispell-buffer-session-localwords | ||
68 | (+ispell-append-removing-duplicates | ||
69 | :test #'string= | ||
70 | dir-local-words ispell-buffer-session-localwords words))) | ||
71 | (when (y-or-n-p "Save .dir-locals.el?") | ||
72 | (save-buffer)) | ||
73 | (bury-buffer)) | ||
74 | (or ispell-buffer-local-name | ||
75 | (setq ispell-buffer-local-name (buffer-name))) | ||
76 | (save-excursion | ||
77 | (goto-char (point-min)) | ||
78 | (while (search-forward ispell-words-keyword nil t) | ||
79 | (delete-region (point-at-bol) (1+ (point-at-eol))))))) | ||
80 | |||
81 | (provide '+ispell) | ||
82 | ;;; +ispell.el ends here | ||