1

This is a follow-up question to Can I instruct ispell to insert LocalWords in a different file?.

So now that I've got a way to use a separate file for my local dictionary, is there any way to move the words in my file to .dir-locals.el automatically?

1

Take these functions out for a spin and use M-x *-ispell-move-buffer-words-to-dir-locals:

;; 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"))
  (let ((words (*-ispell-buffer-local-words-list)))
    (save-excursion
      (add-dir-local-variable
       major-mode
       'ispell-buffer-session-localwords
       (setq ispell-buffer-session-localwords
             (cl-remove-duplicates
              (append ispell-buffer-session-localwords words)
              :test #'string=)))
      (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)))))))

This solution won't limit your dictionary to a single file, though; changes will apply to all buffers of the same major mode.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.