I frequently use file-local variables, but after writing some minor modes I find the need to implement mode-based behavior.
Let's say I want to define a variable errors-list-regexp
that varies depending on the major mode.
My first approach was to ask major modes to define such a variable by using a symbol-name convention, by prefixing the major mode name onto the variable name. Then, I would construct the appropriate symbol name, check whether it is bound and if so obtain its value (for example, java-mode-errors-list-regexp
or "python-mode-errors-lisp-regexp")
But this is a bit cumbersome:
(let* (
(sym (intern (concat (symbol-name major-mode) "-errors-list-regexp")))
(errors-list-regexp (and (boundp sym) (symbol-value sym)))
)
...
)
Another solution might be to turn the variable into a (mode . value) alist, but this also is too cumbersome.
(let* (
(errors-list-regexp-value (cdr (assoc major-mode errors-list-regexp)))
)
...
)
My current approach is to make errors-list-regexp
buffer local, and to add a major-mode-hook (say, to java-mode-hook or python-mode-hook) that sets the appropriate mode-specific value.
I'm mostly satisfied with the latter solution but, is there a better way? Is this idiomatic?