Search code examples
emacselisp

Confusing about the Emacs custom system


There are several similar setting functions:

  1. set & setq
  2. set-default
  3. defcustom
  4. custom-set-value
  5. custom-set-variables
  6. customize-set-value
  7. customize-set-variable

so, what's the difference between these functions?

If I want setting my own preferences to an add-on, for these scenario:

  1. If a variable setting by defcustom, which setting-function will be better?
  2. And what about a variable setting by defvar?

Solution

  • The short answer to you question is:

    • use setq or setq-default for variables defined by defvar.

    • use setq, setq-default, or the Customize mechanism for variables defined by defcustom

    Below is the long answer.

    The functions that you are going to use are the following:

    • set is the main function to set the value of a variable.

    • setq is another version that automatically quotes its first argument. This is useful since quoting the first argument is what you want to do almost all the time.

    • Some variables cannot be set globally. Whenever you set the variable it is only set for the current buffer. If you want to simulate setting this variable globally you use set-default or setq-default.

    The functions that a package writer uses are:

    • defvar which allows the package writer to define a variable and to give some documentation. This function is not required but makes the life of users easier.

    • defcustom builds on defvar. It tells emacs that it is a variable, and it allows the developer to create a custom interface to set the value. The developer can say, things like "this variable can contain only the value 'foo or 'bar".

    Setting variables can be done two ways:

    1. if defvar was used, the values can only be set by the user in its .emacs by using the set function (or variants)

    2. if defcustom was used, the values can be set using set (see 1.) OR by using Customize. When using the customize mechanism, emacs will generate some code that it will place in custom-set-variables. The user should not use this function.