Skip to main content
22 votes
Accepted

How can I find the path to an executable with Emacs Lisp?

@Sigma's answer is a good start, but it doesn't filter by executability, nor does it allow for extra suffixes. On windows, for example, running a can invoke a.exe, if that's what's in your path. So ...
Clément's user avatar
  • 3,995
21 votes

How can I evaluate elisp in an orgmode file when it is opened?

This solution requires no change in init.el (with minor modifications). It involves file-local evaluations, though - but that's exactly what the OP asked for. Advantages of the solution are: asks for ...
ben's user avatar
  • 321
19 votes
Accepted

When should sharp quotes be used?

#' is just shorthand for function, just as ' is shorthand for quote. You can use it anywhere where you want to indicate to the byte-compiler or the interpreter or a human reader that its argument is ...
Drew's user avatar
  • 78k
15 votes

Meaning of period in (. 123)

It looks like Emacs simply reads (. 123) as 123, what happened? That's exactly what happened. To back it up with sources: if (ch == '.') { if (!NILP (tail)) XSETCDR (tail, read0 (...
wasamasa's user avatar
  • 22.2k
14 votes

How to replace an element of an alist?

The OP asks for a solution which handles alists that have string keys. To handle that, see this question. If by chance you only need to handle alists with symbol keys, then as of Emacs 25 you can use: ...
Resigned June 2023's user avatar
13 votes
Accepted

An efficient set data structure in elisp

The standard way to handle sets in lisps is to use hash tables. In fact, this is how sets are implemented in languages which do provide a dedicated interface (like Python). This offers O(1) access ...
sds's user avatar
  • 6,124
13 votes

When should sharp quotes be used?

#' (aka function) can be used in front of (lambda ...) but it's redundant there, so the only place where it's really meaningful is in front of a symbol, as in #'car. In ELisp, #'car and 'car are ...
Stefan's user avatar
  • 26.5k
11 votes

Command that formats (prettifies) Elisp code

Listing Elisp formatting tools here for completeness: Elisp-Autofmt Emacs integration to run on-save.Formats files following this style by default (available on melpa). lispy includes auto-formatting ...
ideasman42's user avatar
  • 8,846
11 votes

An efficient set data structure in elisp

There are many different ways to implement sets, with very different efficiency profiles. In Elisp, the two simplest ways to implement sets are probably: As a list: This tends to have poor ...
Stefan's user avatar
  • 26.5k
11 votes
Accepted

Are local nested functions possible in elisp?

You can also do it without resorting to cl-lib as follows: (defun my-fn (x y) (let ((my-local-fn (lambda (a b) (message "%S" (- a b))))) (funcall my-local-fn x y) (funcall my-local-fn y x))...
Stefan's user avatar
  • 26.5k
10 votes
Accepted

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

cl-destructuring-bind was designed more or less specifically to destructure data made of cons cells. pcase-let on the other hand is just a special case of pcase which was designed to handle arbitrary ...
Stefan's user avatar
  • 26.5k
8 votes

String equality, ignore case?

Another short alternative: (cl-equalp "string" "strinG") ; => t (cl-equalp "strinG" "this string") ; => nil (cl-equalp "word" "...
dardisco's user avatar
  • 189
8 votes
Accepted

How to make local function binding for closure?

Without cl-lib: ;; -*- lexical-binding: t; -*- (defun nats () (letrec ((inner (lambda (n) (cons n (lambda () (funcall inner (1+ n))))))) (funcall inner 0))) (let* ((stream (nats)) (i (...
wasamasa's user avatar
  • 22.2k
8 votes
Accepted

Why does defvar scoping work differently without an initvalue?

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 ...
Stefan's user avatar
  • 26.5k
8 votes

Are local nested functions possible in elisp?

You can use cl-flet or cl-labels from cl-lib. The docstrings: (cl-flet ((FUNC ARGLIST BODY...) ...) FORM...) Probably introduced at or before Emacs version 24.3. Make local function definitions. ...
Dan's user avatar
  • 33.1k
7 votes

Is there a way to comment within a line in elisp?

For the usual meaning of "comment", the answer is mostly no (see Dan answer for details), but if you just want to prevent code from being executed, then you can simply do nothing in your case, since ...
Stefan's user avatar
  • 26.5k
7 votes
Accepted

One-time advice

You don't need a macro for this. And you don't need (but you can certainly use) lexical binding. ;; Without lexical binding: ;; (defun advise-once (symbol where function &optional props) (...
Drew's user avatar
  • 78k
7 votes

Deep eager macroexpansion

(require <foo>) is treated specially when it's at top-level (it causes the compiler to require the file also, rather than only do the require at run-time). If the require is not performed at ...
Stefan's user avatar
  • 26.5k
7 votes
Accepted

Elisp backquote comma eval puts a space in between

The short answer is that this is expected behaviour. You seem to be expecting foo-,arg to be something like (intern (concat "foo-" (symbol-name ',arg))), but there's no reason why that should be the ...
phils's user avatar
  • 51.6k
7 votes

Is there an equivalent for defparameter on emacs-lisp?

Not as far as I can tell, no. I've always questioned the usefulness of having both defvar and defparameter in Common Lisp. If you don't mind, leave a comment explaining why having both forms is ...
Jeff Spaulding's user avatar
7 votes
Accepted

About closure creation

You asked: Will be useful to write/build literal closures? Should it be a good practice? any example? Why the closure is not a real type instead of cons? All three questions are answered ...
Tobias's user avatar
  • 33.3k
7 votes
Accepted

Underscore in if-let

Since Emacs 26, the underscore (or any variable symbol in general) can be omitted: if-let is a Lisp macro in `subr-x.el'. (if-let SPEC THEN &rest ELSE) Probably introduced at or before Emacs ...
Basil's user avatar
  • 12.5k
7 votes
Accepted

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

Emacs Lisp doesn't have multiline comments. Neither does Python, for that matter. """…""" in Python delimit a multiline string. Emacs has multiline string literals, but ...
Gilles 'SO- stop being evil''s user avatar
6 votes

Is using cl-lib crucial for writing good Emacs Lisp code?

No, it's definitely not crucial: A quick sed | sort | uniq -c says that within Emacs's own code we have: [...] 20 (cl-ecase 24 (cl-labels 26 (cl-callf 26 (cl-defmacro 26 (cl-dolist 26 (cl-...
Stefan's user avatar
  • 26.5k
6 votes

How to write assertions

Here's an assert macro in elisp based on an expert's advice on emacs mailing list: (defmacro assert (test-form) `(when (not ,test-form) (error "Assertion failed: %s" (format "%s" ',test-form)))...
Emacs User's user avatar
  • 5,663
6 votes
Accepted

The default value of buffer-local variable not set until first `setq`

I, as a package author, bind case-fold-search to nil in the outer let, and I want to use the default value (it might or might not be set by the user) in the with-temp-buffer, In this case I would ...
npostavs's user avatar
  • 9,213
6 votes
Accepted

practical relevance of storing value and function into one symbol

Emacs Lisp is a Lisp 2, which means only just what you described: the same symbol can have separate values as (1) a variable and (2) a function. (We call only #1 the "value" of the symbol.) Some ...
Drew's user avatar
  • 78k
6 votes
Accepted

Is there an equivalent for defparameter on emacs-lisp?

In Emacs Lisp, defconst doesn't actually enforce constness, and therefore works pretty much like Common Lisp's defparameter: it unconditionally sets the variable; it marks it as special; it avoids ...
jch's user avatar
  • 5,770

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