summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorCase Duckworth2022-01-25 16:56:48 -0600
committerCase Duckworth2022-01-25 16:56:48 -0600
commit3586cecd8b39a26f7f86ac78e9a23761ccb1beae (patch)
tree9b21cd66ce01213025c7346d394fe7bf6e831fe4
parentMove truncate-lines setting to a mode-hook (diff)
downloademacs-3586cecd8b39a26f7f86ac78e9a23761ccb1beae.tar.gz
emacs-3586cecd8b39a26f7f86ac78e9a23761ccb1beae.zip
Allow saving ispell-local-words in .dir-locals.el
TODO: Automatically move local words to .dir-locals on save
-rw-r--r--init.el4
-rw-r--r--lisp/+ispell.el82
2 files changed, 86 insertions, 0 deletions
diff --git a/init.el b/init.el index a7012ea..2a6dab5 100644 --- a/init.el +++ b/init.el
@@ -400,6 +400,10 @@
400 (:bind "c" #'+Info-copy-current-node-name 400 (:bind "c" #'+Info-copy-current-node-name
401 "w" #'+Info-copy-current-node-name))) 401 "w" #'+Info-copy-current-node-name)))
402 402
403(setup ispell
404 (:also-load +ispell)
405 (put 'ispell-buffer-session-localwords 'safe-local-variable #'+ispell-safe-local-p))
406
403(setup kmacro 407(setup kmacro
404 (:also-load +kmacro) 408 (:also-load +kmacro)
405 (with-eval-after-load '+kmacro 409 (with-eval-after-load '+kmacro
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.
12Any keyword arguments to `cl-remove-duplicates' should come
13before 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