Allow saving ispell-local-words in .dir-locals.el

TODO: Automatically move local words to .dir-locals on save
This commit is contained in:
Case Duckworth 2022-01-25 16:56:48 -06:00
parent d799b1cded
commit 3586cecd8b
2 changed files with 86 additions and 0 deletions

View File

@ -400,6 +400,10 @@
(:bind "c" #'+Info-copy-current-node-name
"w" #'+Info-copy-current-node-name)))
(setup ispell
(:also-load +ispell)
(put 'ispell-buffer-session-localwords 'safe-local-variable #'+ispell-safe-local-p))
(setup kmacro
(:also-load +kmacro)
(with-eval-after-load '+kmacro

82
lisp/+ispell.el Normal file
View File

@ -0,0 +1,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