Hot answers tagged

25

You should ask Emacs this question: C-h i, choose the Elisp manual (m Elisp), and search the index for "defvar" (i defvar). That takes you to node Defining Variables. Emacs tells you that defvar allows attaching a doc string, and that if the variable already has a value then it does nothing. It also tells you about the use of defvar with no value to be ...


21

setq returns the value, so you can just: (setq f-loc1 (setq f-loc2 "/foo/bar")) Of if you don't want to rely on that, then use: (setq f-loc1 "/foo/bar" f-loc2 f-loc1) Personally I would avoid the latter and instead write: (setq f-loc1 "/foo/bar" f-loc2 f-loc1) or even (setq f-loc1 "/foo/bar") (setq f-loc2 f-loc1) And the very first approach I ...


17

This is documented behaviour. The (much improved) explanation in the Emacs 25.1 elisp manual is as follows: Note that unlike dynamic variables which are tied to the symbol object itself, the relationship between lexical variables and symbols is only present in the interpreter (or compiler). Therefore, functions which take a symbol argument (like ‘...


13

A macro to do what you want As an exercise of a sort: (defmacro setq-every (value &rest vars) "Set every variable from VARS to value VALUE." `(progn ,@(mapcar (lambda (x) (list 'setq x value)) vars))) Now try it: (setq-every "/foo/bar" f-loc1 f-loc2) How does it work Since people are curious how it works (according to comments), here is an ...


7

Since you can use and manipulate symbols in lisp, you could simply loop over a list of symbols and use set. (dolist (var '(foo bar baz)) (set var 10)) (mapc (lambda (var) (set var 10)) '(foo bar baz)) (loop for var in '(foo bar baz) do (set var 11)) (--each '(foo bar baz) (set it 10))


7

In the emacs init file in one case I use: (show-paren-mode t) show-paren-mode is a function. It accepts t as an argument. Using C-h f show-paren-mode, the description says, With a prefix argument ARG, enable Show Paren mode if ARG is positive, and disable it otherwise. Behind the scenes, t is used as a toggle for a mode (some set of behaviors). ...


6

Quoth the docstring of special-variable-p: special-variable-p is a built-in function in `src/eval.c'. (special-variable-p SYMBOL) Probably introduced at or before Emacs version 24.1. Return non-nil if SYMBOL's global binding has been declared special. A special variable is one that will be bound dynamically, even in a context where binding is lexical by ...


6

Emacs comes with the Elisp manual. Try C-h i m Elisp RET. Type i setq RET to go to the place where the index topic setq is explained. There you find the following info: Special Form: (setq [symbol form]...) This special form is the most common method of changing a variable’s value. Each SYMBOL is given a new value, which is the ...


5

make-symbol returns an uninterned symbol. That means you get a symbol with name, function cell, value cell, and property list but the symbol is not registered in the global obarray. Therefore, you cannot use it for function evaluation with the usual parenthesis notation. If you replace make-symbol by intern the symbol is also registered in the obarray and ...


5

Use customize-set-variable or custom-set-variables, depending on just what you want/need. Both of those make use of any initialization or setter functions (and so on) with which the user option might have been defined. See (elisp) Variable Definitions for information about these things. If a given user option is defined without specifying any particular ...


5

BTW, one way to think about why it can't work is to remember that lexical scoping enjoys the so-called "α-renaming" property: variable names do not matter, and you (or the compiler) can trivially (i.e. without having to understand what the code does) rename a variable (as long as the new name doesn't collide with some other local variable) without ...


5

setq is doing like expected, the thing here is, that (1 2 3 4) is not a value, so it is not what you think it is. a Place is a location in memory. x and y are Symbols. a Symbol merely points to a place. So x points to (the first cons of) your list. (1 2 3 4) is a List of conses (aka a "chain" of conses). (1 2 3 4) is not a value, but multiple chained ...


5

When writing list or cons constants in source code there is actually a subtle difference between e. g. (cons 'first 'second) and '(first . second). The latter denotes a constant cons cell in your code, the former creates a new cons cell each time the function is called. What happens in init-cache is that the global variable cache is set to point to the same ...


5

As I understand it, Daniel's question was not only about the difference between set and setq, but also about why to use set/setq in some (and not all) cases. The fundamental difference between the two examples you provide is the following: To inhibit the startup message, you need to set the variable inhibit-startup-message to t. You can do this by using one ...


4

Lexical scope (recently added to emacs) changes the rules, and some documentation is still out of date. The current documentation for setq is this manual page which says When dynamic variable binding is in effect (the default), set has the same effect as setq, apart from the fact that set evaluates its symbol argument whereas setq does not. But when a ...


2

You seem to want the variable counter to hold two things at once: instructions to increment the counter, and the current value of the counter. You can't have both at the same time. There are a few programming languages where you can define “magic” variables that do something whenever they're read, but Emacs Lisp isn't one of them. While you can store ...


2

The effect that (eval counter) increments counter in some sense can be reached by setting counter to a quoted closure call. The eval evaluates the closure, the closure captures the actual counter and increments it at the eval. This solution does not address the setq part of your question. There follows example code together with usage instructions. Put the ...


2

When the code in your init file runs, there are no open files at all. vc-mode is only set when a file is finally opened and determined to be in version–controlled. This means that when it runs your init file and evaluates vc-mode, it puts the nil value into the list that goes into mode-line-format. That nil is then skipped whenever the mode line is displayed....


2

Put the make-local-variable after the setq-default: (with-temp-buffer (setq-default bar "xyz") (make-local-variable 'bar) (symbol-value 'bar)) setq-default sets the global value. Doing what you did says there is a local value for bar, but it doesn't set that local value. And neither does setq-default set that local value. Doing it with what ...


2

Looking at Using Lexical Binding it appears that a needs to be defined special which in using setq it is not. Modifying your example to use defvar gives the answers expected. ;;; -*- lexical-binding: t; -*- (defvar b 5) (let ((b nil)) (setq b t) (print b)) ;; prints t (print b) ;; prints 5 (let ((b nil)) (set 'b t) (print b)) ;; prints t (...


2

Compare this. The essential difference is using set instead of setq. (defun activate-mode-in-list (minor-mode minor-mode-list) (dolist (mm minor-mode-list) (set mm (eq mm minor-mode)))) Your feeling that "I'm getting lost in quoting stuff because evaluating" was not far off. You don't want to set the values of the local variables mode (...


1

Yes. setq sets a variable to whatever value is returned by the second argument. (setq VAR VALUE) sets variable VAR to whatever VALUE is, and the second argument, VALUE is always evaluated. (The first argument, VAR, is never evaluated. For a function that evaluates both args, see set.)


1

You ask, "assuming the local variable takes precedence, why would changing the default value have an effect, but not changing the local value?" If you use setq in your init file, and if the variable is buffer-local in the buffer that is current when your init file is evaluated, then only the value in that buffer gets set. Probably the problem is that the ...


1

Is it possible to use require (or similar) to organize my config into a modular structure of directories, without running into this issue? Yes. Don't add custom-set-variables to multiple places in your init. There should be exactly one instance of custom-set-variables. By default, Emacs creates this in your init.el file. If it's already there, you cannot use ...


1

It sounds like you have done both of the following for some options, i.e., you've done them both for at least one option: Used Customize to set and save the option. Tried to call customize functions to set the option in your init file. If so, it's you who introduced the redundancy. For a given option, choose one or the other. For example, if you want to ...


1

vc-git-grep is an interactive function which prompts, so when you want to call it from your own code you need to use call-interactively, otherwise it expects you to have supplied its required arguments in the call: (defun Pfedj/vc-git-grep-noncase() (interactive) (setq vc-git-grep-template "git --no-pager grep -n -i -e <R> -- <F>") (call-...


1

@Tobias nice. Here still a command making new entries: (defun vl/new-pathsfunc () (interactive) (let ((directory (read-from-minibuffer "directory: " (substring default-directory 2))) (name (intern (read-from-minibuffer "Name: ")))) (unless (map-contains-key vl/paths name) (push (cons name directory) vl/paths) (vl/create-path-funcs vl/...


Only top voted, non community-wiki answers of a minimum length are eligible