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 ...


22

Most of your assumptions are close. I'll mention a few later. But, first the main question. The form setq-local is merely a convenience, it is the same as doing make-local-variable followed by setq. If you had done a C-h f setq-local to see the documentation and clicked through to the source you might have seen this. That's how I verified my first ...


8

Why the two are treated differently is mostly "because that's what we needed". More specifically, the single-argument form of defvar appeared a long time ago, but later than the other and was basically a "hack" to silence compiler warnings: at execution time it had no effect at all, so as an "accident" it meant that the silencing behavior of (defvar FOO) ...


6

The following works only with lexical binding, because with dynamic binding (defvar foo) has no real effect. It checks whether let-binding the variable affects the dynamic value or not. I used the term "declared" instead of "defined", because (defvar foo) doesn't quite feel like a full definition to me (e.g., it only applies to the file it's in) so it's ...


5

You're compiling the file using lexical binding, so I suspect the problem is that your file does neither of the following: (defvar bibtex-completion-bibliography) (require 'bibtex-completion) either of which would tell the code in your file that bibtex-completion-bibliography is a dynamic variable. Without that information, the following creates a lexical ...


4

(progn (unload-feature 'your-lib) (load-library "your-lib")) This will work as long as you first loaded the defvars by loading the library through emacs, and not using eval-defun, eval-buffer etc. When you use require, load-library etc. Emacs will keep track of which variables and functions are part of your library, and will remove them entirely for you ...


4

Based on experimentation, I believe the issue is that (defvar VAR) with no init value only has an effect on the library(s) it appears in. When I added (defvar my-dynamic-var) to the *scratch* buffer, the error no longer occurred. I originally thought this was on account of evaluating that form, but I then noticed firstly that simply visiting the file with ...


3

The defvars are evaluated -- your uninterned symbols will have the values you've assigned. Change make-symbol to intern.


3

The form (defvar bootstrap-version) doesn't set the value of bootstrap-version, and will not over-write the value of bootstrap-version if it was already set. It is also not necessary to make the variable "available" to straight.el or any of the files it includes. The only thing the defvar accomplishes here is to declare bootstrap-version as "...


3

It isn't equivalent. If you test the symbol of a defvar'd variable with special-variable-p you get t, however this is not the case for your code. This bit must be set for the variable to be usable with dynamic binding. There doesn't appear to be a way to set this bit, other than by using special forms like defvar, defalias or the defcustom macro (which ...


3

(setq default-list '((station1) (station99) (stationABC))) ; Redefine. or (add-to-list 'default-list '(station23)) ; Add a station. Take a look at C-h i, Emacs Lisp Intro. See also the Elisp manual, node Setting Variables. You can also use setq or your own defvar before that defvar in the package is evaluated. That will prevent the package's defvar ...


3

defvar does not reassign a variable's value in the same way as, say setq or setf. Once a variable has a value, defvar won't touch it. From defvar's docstring: (defvar SYMBOL &optional INITVALUE DOCSTRING) Define SYMBOL as a variable, and return SYMBOL. ... The optional argument INITVALUE is evaluated, and used to set SYMBOL, only if ...


2

I think you've diagnosed the issue correctly, and you can either: Use ediff-mode-hook to set the buffer-local value. Use setq-default to set the default value. Offhand I think #2 seems like the best option.


2

Try this: (defun foo () "(Re-)evaluate all `defvar's in the buffer (or its restriction)." (interactive) (save-excursion (goto-char (point-min)) (while (not (eobp)) (when (re-search-forward "\\s-*(defvar \\([^ \t\n(\"]+\\)[ \t\n]+[^)]" nil 'MOVE) (let ((old-value (make-symbol "t")) new-value value) (let ((...


1

If you want to pass a non-evaluated expression as argument without quote you have to use a macro since the arguments of (non-special) functions are evaluated before the function call. (quote a) evaluates just to the un-evaluated expression expr. That's why (quote expr) works as argument. Your code with the defun replaced by defmacro could look like the ...


1

magit-last-seen-setup-instructions was not well received and has been removed. Also, when it was still in use, you were supposed to set it before loading magit. magit-log-edit-mode-hook is also obsolete. magit-log-edit.el was removed three years ago and support was completely removed two and a half years ago. It's place was taken by git-commit.el and the ...


1

You declare variables as global with defvar and optionally initialize them. When you use the setq the variable apparently hasn't been declared. It sounds like what your after is this construction (defvar foo) (setq foo "foo") The first tells the compiler that foo is a global (dynamic) variable. The second sets it unconditionally.


1

After I heard that there is no convenient solution for re-evaluation of buffer with reassigning of defvar's I made simple function that relays on eval-defun: (defun my/eval-buffer () "Evaluate entire buffer with re-assigning values to `defvar' / `defcustom'. Useful during package development." (interactive) (save-excursion (beginning-of-buffer) ...


1

The cwd for that shell command will be based on the default-directory for the active buffer at the time your file is loaded; not the directory in which the loaded file resides. You need to utilise the load-file-name variable. (let ((default-directory (file-name-directory load-file-name))) (shell-command-to-string ...))


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