Confusion about the special Emacs system

There are several similar configuration functions:

  • set and setq
  • set as default
  • defcustom
  • order set value
  • custom set variables
  • settings-settings-value
  • Setting a Variable Set

So what's the difference between these features?

If I want to set my own settings for the add-in, for this scenario:

  • If the parameter parameter is defcustom, which setting function would be better?
  • What about setting the defvar variable?
+42
emacs elisp
Aug 21 '12 at 15:55
source share
4 answers

Short answer to your question:

  • 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 a long answer.

The features you are going to use are as follows:

  • set is the main function for setting the value of a variable.

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

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

Features used by the author of packages:

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

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

Variables can be configured in two ways:

  • if defvar was used, values ​​can only be set by the user in .emacs using the set function (or options)

  • if defcustom used, values ​​can be set using set (see 1.) OR using Customize . When using the configuration mechanism, emacs generates some code that it will put in custom-set-variables . The user should not use this feature.

+59
Aug 21 '12 at 16:28
source share

They are basically all the way to the same thing. However, there are some important differences. The best way to get to know them is to read the manuals for Emacs and Elisp (see Ch i) ). Above the head:

  • set is the assignment to the low level variable
  • (setq foo bar) is short for (set (quote foo) bar)
  • (set-default foo bar) means "if there is no more clearly defined definition of foo in the current buffer, use the string of values" and applies to all buffers.
  • defcustom used to mark a variable as one that is expected that the user can safely modify using the customize function.
  • custom-set-value and customize-set-value are two names that point to the same function. These are convenient methods for working with the customize system.
  • custom-set-variables and customize-set-variables are used to make a certain set of custom variables active, IIRC.

In general, it is recommended that you use Mx customize to make a difference. You can set the values ​​defined with defcustom using set or setq in .emacs , the configuration system will warn you if you change it later with customize .

defcustom usually only used by people writing packages intended for distribution, and I don't think I saw anyone using custom-set-* outside files that you can configure yourself. setq very common in people's initialization files to customize how they like them, regardless of whether these things are marked for use with customize or not.

I don’t have a full understanding of all this, I hope someone else can shed more light, but I think this is a pretty good review: P

+10
Aug 21 '12 at 16:30
source share
  • set and setq are lower-level primitives used to assign any variable.
  • set-default and setq-default are extensions to emacs that come with buffered local variables, allowing you to set default values ​​for new buffers. 3-7. All "user" materials are a later addition that was developed to support the user interface for managing variables that are intended to be used as user settings.
  • defcustom is similar to defvar , but it allows you to specify a place in the parameter hierarchy, as well as information about the data type, so that the user interface can display the value in the form of a menu or automatically convert user input to the corresponding type.
  • I do not think there is a custom-set-value function.
  • custom-set-variables used to configure the user interface while saving all user parameters. It lists all the variables changed by the user by default. 6-7. custom-set-value and custom-set-variable are used by the custom user interface to query the user for the current and default values ​​of the parameter variable and assign them. Usually you do not call it yourself.
+5
Aug 21 '12 at 16:45
source share

Just like the addition, the differences between these teams have increased due to the introduction of lexical binding, although these differences will not be really relevant if you just want to configure some variables.

The def... constructs declare global variables. The set... functions define variables, global or local. If x not a local variable (a formal parameter of the current function or a declared let form or similar), or is not defined by def... and you write (setq x 0) , then the byte compiler will even show a warning

 Warning: assignment to free variable `x' 

Variables declared with defvar , defcustom , defconst are dynamically linked, i.e. when you have a design

 (let ((lisp-indent-offset 2)) (pp (some-function))) 

some-function will see a change to the global lisp-indent-offset variable.

If the variable is not dynamically bound, something like

 (let ((my-local-var 1)) (some-function)) 

where my-local-var has no global value, then some-function will not see the assigned value, because it is lexically limited.

On the other hand, dynamically copied variables will not be written to lexical closures.

More information can be found at http://www.gnu.org/software/emacs/manual/html_node/elisp/Lexical-Binding.html

+3
Jun 27 '13 at 12:50
source share



All Articles