3

I have own minor-mode with 2 (for now) user specific variables (string and int). For now they are dropping to default values each emacs restart. Which is not convenient.
How and where it is better to save them?
For now I found 2 options:

  1. Using savehist-additional-variables function.
  2. Binding hook to kill-emacs-hook and writing it into own file.

Any other option?

To be more precise. My minor mode is running build based on custom build script and one of the variables is a target-name which user does not change while he is working on one target and changing from time to time while switching between modules.

I think savehist-additional-variables will be the best choice in my case.

  • 2
    How are these variables getting changed? Are you changing them programatically in your minor mode, or is the user setting them? – zck Nov 4 '16 at 17:02
  • 1
    If the variables are for user settings then @Tyler's answer about using Customize is good. If the variables are internal then the two approaches you mention are good. (And you need not wait until the Emacs session ends to save them.) – Drew Nov 4 '16 at 20:46
4

Emacs provides the Customize system for user-configurable variables. Your definition can include a default value, but Emacs provides an interface for the user to specify their own value. The user-set value is saved in their .emacs, or if they've set it up, a separate customization file. Either way, the value is permanently stored, so that when you restart Emacs, the user's value is used to initialize the variable.

The example from the linked manual page:

For example, the customizable user option variable text-mode-hook looks like this:

(defcustom text-mode-hook nil
  "Normal hook run when entering Text mode and many related modes."
  :type 'hook
  :options '(turn-on-auto-fill flyspell-mode)
  :group 'wp)

The name of the variable is text-mode-hook; it has no default value; and its documentation string tells you what it does.

See the linked page for more detail and options.

  • 1
    This is good, assuming that the variables in question are for user settings. – Drew Nov 4 '16 at 20:44
  • @Drew yes, the question is a bit vague on that point. If the values of the variables change dynamically, another approach will be necessary. – Tyler Nov 4 '16 at 20:47

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.