3

Is there a way to add a list of "accepted" words for each underlying prog-mode.

By "accepted" I mean - I don't want them added to my dictionary - I just don't want them highlighted.

By prog-mode I mean (for example) that for flyspell-prog-mode in c-mode I'd like a list of words I'm likely to use in comments - jargon/slang - that I wouldn't want accepted as correct in say a markdown file.

I'd be happy for an answer requiring writing a mode-hook, but thought there might be an alist somewhere.


Example:

Words like alloc, malloc, unref are perfectly reasonable in C comments, but might not be in other modes - and especially not in regular text documents. I don't want them added to my real dictionary.

1 Answer 1

1

The variable ispell-buffer-session-localwords contains a list of accepted words for the current buffer.

You can define a list for each programming mode with the words and a couple of functions to add the right lists according to the major mode and its parents, like the following code. The three constants are simple examples. If you add the last function to the mode hook you will have all the words defined for prog-mode ("foobar" in the example) and for the major mode. If for some modes you don't want the words from the parent modes you can add the first function to the mode hook.

(defconst my/c-mode-accepted-words '("alloc" "malloc")) (defconst my/emacs-lisp-mode-accepted-words '("cdr" "alist" )) (defconst my/prog-mode-accepted-words '("foobar")) (defun my/mode-add-ispell-buffer-defs (&optional mode) (when-let* ((word-list (intern-soft (format "my/%s-accepted-words" (or mode major-mode))))) (dolist (word (symbol-value word-list)) (cl-pushnew word ispell-buffer-session-localwords)))) (defun my/all-modes-add-ispell-buffer-defs () (dolist (mode (derived-mode-all-parents major-mode)) (my/mode-add-ispell-buffer-defs mode))) 

ispell must be loaded before executing the function, otherwise the variable ispell-buffer-session-localwords is not defined. The first function requires cl-lib to work.

1
  • Brilliant! the all-modes function works perfectly as a flyspell-mode hook. Commented Oct 17 at 5:29

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.