2

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?

2
  • I think I understand what you mean, but I realize I was a bit vague about which modes I was talking about, so I edited to clarify. I'm writing a minor mode, which uses variables whose values differ across major-modes. My goal is to assign a value to these based on the major mode. But yeah, I think I understand. Care to add that as an answer?
    – erjoalgo
    Aug 9, 2015 at 1:45
  • OK, I posted it as an answer.
    – Drew
    Aug 9, 2015 at 15:44

1 Answer 1

2

Yes, make the variable buffer-local, and turn the mode on in a given buffer, to use it. Your code can of course test whether the mode is on in a given buffer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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