As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [elisp]

*ONLY* for questions about Emacs Lisp as a language, compared to other languages, in particular, compared to other Lisp dialects. That is, it is for questions *about the language* itself. *DO NOT USE IT* for questions about *using* Emacs Lisp. Emacs Lisp is the scripting and programming language that the Emacs editor is built on.

Filter by
Sorted by
Tagged with
37 votes
3 answers
14k views

Command that formats (prettifies) Elisp code

Let's say I have a function that looks like the following (as is often the case when printing elisp output). (defun my-example-function () (let ((a (do-something)) (b (do-something))) (setq someone ...
4 votes
3 answers
129 views

the Term “Hash Notation“ in the Elisp Manual

I'm reading GNU Emacs Lisp Reference Manual, and I see the phrase "hash notation". Two places in the document seem to have different interpretations, so I have 2 questions. 2.1 Printed ...
8 votes
3 answers
349 views

Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't

My example is simplified: (defvar wtf 10) (defun f (wtf) (lambda () (cl-incf wtf))) (setq f (f 20)) (setq g (let ((wtf 30)) (lambda () (cl-incf wtf)))) (list (funcall f) ...
7 votes
1 answer
128 views

“(equal a b)⇒t” whereas “(equal b a)⇒error”

GNU Emacs Lisp Reference Manual, 2.8 Equality Predicates: Comparing circular lists may therefore cause deep recursion that leads to an error, and this may result in counterintuitive behavior such as (...
4 votes
1 answer
459 views

Is it faster to add an element at the beginning of a list with add-to-list than at the end?

I've noticed that some people use a t at the end of this code: (add-to-list 'package-archives '("melpa" . "https://melpa.org/packages/") t) that t makes it append at the end of ...
16 votes
3 answers
3k views

Is there any way to run a hook function only once?

The context I'm using the after-make-frame-functions hook to load properly the themes in an emacs client/server configuration. Specifically this is the code snippet that I use to make that (based in ...
1 vote
2 answers
99 views

the term “constant” in Elisp

The definition of the term “constant” is clarified in the GNU Emacs Lisp Reference Manual, section 6.2: For purposes of evaluation, the array is a constant—i.e., it evaluates to itself. In the ...
20 votes
4 answers
3k views

Why does lisp-interaction-mode exist, and do we ever need it?

Q: Why does lisp-interaction-mode exist, and are there any reasons to use it instead of emacs-lisp-mode? The manual states that emacs-lisp-mode and lisp-interaction-mode are identical except that the ...
4 votes
2 answers
572 views

Is there any function that allows me to execute code on the run?

I'm a Vim user who is trying to migrate to emacs. On Vim, I've been using the following vimscript function to execute code: function! ExecuteOnTerminal(type) range if (&ft=='bash' || &ft=='...
1 vote
0 answers
32 views

Insert output of slow external process in current buffer, character-by-character

Imagine in the current buffer I have the text: Hello Bye I want to call an external program and add the output of it at the end of the buffer. The tricky thing is that the external program provides ...
46 votes
2 answers
4k views

Why does elisp not have namespaces?

Q: Why does elisp not have namespaces, and how could we get them? Elisp does not have namespaces other than the global one, which has led to the coding convention of prefixing all global functions, ...
6 votes
2 answers
766 views

Underscore in if-let

I often find myself wanting to do something like this: (if-let* ((foo (get-foo)) (_ (conditionp foo)) (bar (get-bar-from-foo foo))) ...) As an alternative to: (let ((foo (get-...
12 votes
2 answers
1k views

How can I modify Elisp's reader?

Modifying the reader would allow introducing new read-syntax (such as #(hash table) and '(quoted)). Many Lisps have this​​​​​ capability, but no such facility seems to exist for elisp.
1 vote
1 answer
113 views

Scheme-like lambda expressions?

Scheme syntax seems very lambda-expression-friendly. I can do this (define blah (lambda (n) (* n n))) But this isn't possible in elisp. I can do this (defun simple (x) ((lambda (n) (* n n)) x))...
4 votes
1 answer
187 views

Does Elisp replay on hash lookups every time code require access to symbol value?

I go across blog post: http://blog.cdleary.com/2010/09/picing-on-javascript-for-fun-and-profit/ It is about JS performance on accessing object properties and tricks that help reduce that time. Usual ...
6 votes
2 answers
1k views

How to write assertions

How does one write assertions in Emacs Lisp? I know that cl-macs.el has cl-assert and cl-check-type, but I'm wondering if there's a native Emacs way to perform something that basic.
1 vote
2 answers
1k views

Dolist not modifying the list

I'm trying to prepend each substring of split-string with numbering (1.,2.,3.,..) then return all the substrings concatenated with a newline (defun japanese-get-definition (dictentry) "Get a ...
3 votes
2 answers
274 views

How to evaluate a symbol in a lambda function when defining it?

For example, (lambda () (message "%s" x))) But I would like x to be evaluated -- the lambda function should always use that value, instead of the current value of x. How should I do it? Preferably ...
1 vote
1 answer
1k views

Passing a symbol as an argument to a function

I'm trying to understand why this doesn't work, and how to make it work: (defvar test/var-1 nil) (defvar test/var-2 nil) (defun make-setter (var-to-set) (lambda (val-to-set) (setq var-to-set ...
2 votes
2 answers
259 views

Evaluating symbol in function arguments affected by destructive operations?

While evaluating a function call, the arguments is from left to right. For example, (let ((x '(1 2 3))) (list (nreverse x) x)) ;; => ((3 2 1) (1)) The first argument (nreverse x) evaluates to (...
3 votes
1 answer
1k views

Difference between pcase-let & cl-destructuring-bind?

I noticed both pcase-let and cl-destructuring-bind seem to perform the same operation. Is there any difference or reason to use one instead of the other? eg: (pcase-let ((`(,filename ,buf) (pop ...
1 vote
4 answers
679 views

Equivalent of `continue' in `cl-loop'?

Does the cl-loop macro implement an equivalent to the continue keyword of other languages? The behavior of break can be achieved by using until or while clauses by placing them in the middle of cl-...
1 vote
1 answer
172 views

How to use face-remap-set-base to substitute a face for another?

I'd like to locally remap a face to other custom face. The docstring reads: (face-remap-set-base FACE &rest SPECS) Set the base remapping of FACE in the current buffer to SPECS. This causes the ...
56 votes
6 answers
10k views

How to replace an element of an alist?

I have this by default in my auto-mode-alist: ("\\.js\\'" . javascript-mode) (even with emacs -Q). I'd like to substitute js2-mode for javascript-mode. Of course, I could use assq-delete-all and ...
20 votes
2 answers
8k views

How do you return from a function at an arbitrary point?

How do you return early from a function before it's ended? For example: (defun my-func () "for example." (unless something (return nil)) ; continue as usual... (+ 42 1))
7 votes
2 answers
599 views

helm - adding helm-M-x to helm sources

I am able to add few extra sources to helm like this (setq helm-mini-default-sources '(helm-source-buffers-list helm-source-recentf ...
20 votes
4 answers
9k views

How to get element number in a list?

Q: how do I get the element number in a list? nth gets element number n from a list: (nth 2 '(a b c d)) ; => c I'd like to do the reverse: get the element number given the ...
12 votes
2 answers
801 views

Elisp reimplementations

Emacs implements an interpreter for elisp in C which serves as the de facto reference implementation. Are there any other implementations of elisp in other languages (particularly Javascript)? What ...
9 votes
4 answers
2k views

How to control where the org todo keywords buffer displays?

Q: how can I control where the org todo keywords buffer appears? Entering a todo keyword with C-c C-t (org-todo) opens a new buffer with the keyword options and then closes it again after I select ...
38 votes
3 answers
18k views

What's the difference between push and add-to-list?

I've found that different packages in their installation instructions use either push or add-to-list (For example adding a directory to load-path) and I was wondering what the difference is and what ...
3 votes
4 answers
2k views

How can I create block (multiline) comments in Lisp code?

How can I do multiline / block comments in Lisp code - e.g. in the init.el. In Python I would do it like this """Block comment """ In C/C++ like this /* Block comment */ ...
86 votes
4 answers
19k views

Advantages of setting variables with setq instead of custom.el?

I see a lot of people (extension authors and others) give configuration examples with setq: (setq foo 'bar) These parameters are often defined with defcustom, making them available for customization ...
1 vote
1 answer
161 views

Validating Arguments to a Function

I would like to validate the argument to a function before its action is executed. The main concern is the passing of a void variable. How can I trap this? (defun is-it-bound(item) (if (boundp 'item)...
1 vote
1 answer
184 views

Does elisp have a way to jump to (goto) labels in the code, ala common lisp's go?

Does elisp have a way to jump to (goto) labels in the code, ala common lisp's go? (tagbody (setq val 2) (go lp) (incf val 3) lp (incf val 4)) => NIL val => 6 PS: This is a ...
6 votes
3 answers
472 views

One-time advice

I've been trying to a macro to put one-time advice on a function. What I mean by one-time advice is advice which removes itself after it is called – so that when you add the advice and run the ...
1 vote
1 answer
92 views

Access hash table by value, not key?

Q: how can I access a hash table by its value, not its key? Association lists can be accessed via either their key or their value: (setq alist '((a .1) (b . 2) (c . 3))) (assoc 'a alist) ...
11 votes
3 answers
2k views

hl-line-mode hide background, how to avoid this?

With: (when window-system (set-face-background 'hl-line "light yellow") (global-hl-line-mode 1)) current line highlighted by yellow background, but if text has background properties it also ...
22 votes
3 answers
4k views

A faster method to obtain `line-number-at-pos` in large buffers

The function line-number-at-pos (when repeated about 50 times) is causing a noticeable slow-down in semi-large buffers -- e.g., 50,000 lines -- when point is near the end of the buffer. By slow-down, ...
4 votes
3 answers
504 views

Reflection on function argument signatures

Is there anything built into elisp for reflection on function argument signatures? I have used (length (cadr (symbol-function #'my/function))), but this notably fails on built-in C functions. I'm ...
33 votes
3 answers
6k views

When should I use autoload instead of require?

From what I understand require is used to load large chunks of code (something like modules) although it can also load individual functions. Autoload on the other side, only registers functions and ...
11 votes
3 answers
3k views

String equality, ignore case?

Q: how does one test string equality but ignore case? I've got a situation in which I'd like to compare strings but ignore case. Case is significant for string-equal, and apparently is insensitive ...
45 votes
3 answers
10k views

How to know when or when not to use the single quote before variable names?

I have the below: (setq some-variable "less") I am confused why I have to use the single quote with boundp but not with bound-and-true-p. Example 1: (when (boundp 'some-variable) (message "...
10 votes
4 answers
4k views

How to programmatically execute a command in eshell?

I want to execute simple commands in eshell without explicitly typing them into the prompt, using something like with-current-buffer How can I do this?
9 votes
2 answers
3k views

Passing a variable to template function in org-capture-templates

I'm using org-capture to insert expenses into a Ledger file with this template, along with auto-completion for the various expense categories: (setq ledger-expense-completions (list "" ;; ...
17 votes
2 answers
4k views

"and" vs "when" for conditionals

This is a follow-up on the comments on this answer. The following bits of code seem to be equivalent: (and a b) (when a b) Of course and lets you put more conditions: (and a b c d) means (when (and ...
31 votes
3 answers
4k views

Reset custom variable to default value programmatically

Q: Using Elisp, how can I reset a variable to its default value without knowing what that value is? I tried to figure out how to do this by bringing up the Customize interface for a variable I set to ...
2 votes
1 answer
91 views

Why can't I directly invoke the result of apply-partially?

I'm confused about return value of apply-partially. Documentation states that it returns a function, and source of the function shows that it actually retruns a lambda. But I can't invoke the return ...
32 votes
3 answers
2k views

Why is `let' faster with lexical scope?

While reading through the source code for the dolist macro, I ran into the following comment. ;; This is not a reliable test, but it does not matter because both semantics are acceptable, tho one ...
4 votes
0 answers
293 views

Is there an ELISP decompiler?

I'm thinking of writing one so that when I get those horrible tracebacks from bytecode files, I can better see where I was at. Is there one already? Are there any other tools for showing an error ...
7 votes
3 answers
2k views

Are local nested functions possible in elisp?

It it possible to declare functions whose names are only available in the scope of another function, similar to let? e.g.: (defun my-fn (x y) (let ((my-local-fn (lambda (a b) (message "%S" (- a b)...

1
2 3 4 5
9