Hot answers tagged

37 votes

Optional parameter defaults

Unless you use Common Lisp extensions as suggested by @legoscia, you need to check if the optional argument was specified. Note that you don't really need to use let here. This seems more idiomatic to ...
glucas's user avatar
  • 20.2k
33 votes
Accepted

How do I use nadvice?

All information you need is included in C-h f add-function which describes the underlying mechanism of advice-add. The new advice system basically acts like replacing the current definition of a ...
kdb's user avatar
  • 1,561
30 votes

Optional parameter defaults

You can use cl-defun, which lets you specify a default value for optional arguments: (cl-defun command (a &optional (b default-b)) (command-body a b)) The default value, in this case default-b,...
legoscia's user avatar
  • 6,022
26 votes

How to check in elisp if a string is a substring of another string?

cl-search can do that (and also returns the index of the substring, if found): ELISP> (cl-search "f t" "df tj") 1 (#o1, #x1, ?\C-a) ELISP> (cl-search "ft" "df tj") nil
legoscia's user avatar
  • 6,022
26 votes
Accepted

How to properly use defcustom?

The customization system is a built-in feature of Emacs designed to solve precisely the problem you describe—programming may not be the ideal way for the average user to configure their editor. The ...
camdez's user avatar
  • 396
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,944
21 votes

Swap two variables in Elisp

This is the elegant idiom I use ;-). (setq a (prog1 b (setq b a)))
Drew's user avatar
  • 75.8k
20 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
  • 311
18 votes
Accepted

Move selected lines up and down

drag-stuff Check out the drag-stuff package (also available on Melpa). You can then select a region and use drag-stuff-up/drag-stuff-down to move that region up/down. Alternative behavior when ...
Kaushal Modi's user avatar
  • 25.2k
18 votes
Accepted

Swap two variables in Elisp

If memory serves me well and you're willing to use cl-lib then: (cl-rotatef a b) Note that this is Common Lisp way of solving the problem.
Mark Karpov's user avatar
  • 4,913
17 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
  • 75.8k
16 votes
Accepted

Emacs Lisp comment conventions

Actually, 3-and-more semi-colons stand for headings, where the more semi-colons you put the deeper the nesting of the heading. So it should look like ;;; Main heading ;;;; Sub heading ;;;;; Sub sub ...
Stefan's user avatar
  • 26.2k
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
  • 21.8k
14 votes
Accepted

Simple request.el usage

This is because '(("address" . address)) is quoted (i.e. prefixed by the ' operator), which means that what is inside does not get evaluated. The request function thus gets called with an argument ...
François Févotte's user avatar
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.2k
12 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
12 votes
Accepted

Run code right after compilation

Try using compilation-finish-functions: (defun my-compilation-finish-function (buffer desc) (message "Buffer %s: %s" buffer desc)) (add-hook 'compilation-finish-functions 'my-compilation-finish-...
wasamasa's user avatar
  • 21.8k
12 votes

How to edit elisp without getting lost in the parentheses

There are a number of add-on packages that might help, such as paredit, smartparens, and lispy. These packages make it easier to navigate and manipulate lisp code so that you can think in s-...
glucas's user avatar
  • 20.2k
12 votes
Accepted

How can I modify Elisp's reader?

Turns out that the manual implies that you can't actually do reader macros. According to Appendix C Porting Common Lisp: Reader macros. Common Lisp includes a second type of macro that works at ...
Dan's user avatar
  • 32.6k
12 votes

How can I modify Elisp's reader?

Apparently it can be done, but prepare for a lot of work. (or don't, because I'm gonna tackle it some weekend ;-). (defvar *orig-read* (symbol-function 'read)) (defun read (&optional in) (...
mishoo's user avatar
  • 249
12 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
  • 5,948
11 votes
Accepted

Why doesn't "complement" work as a function?

Yes, this behavior is explained by differences in Variable Scoping between Emacs Lisp and Common Lisp. In Common Lisp (a lexically scoped lisp) a lambda returned by complement you defined is turned ...
Constantine's user avatar
  • 9,072
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.2k
10 votes

`cond` with less redundancy

Traditional Emacs Lisp "in my time we didn't have that new-fangled CL stuff" style: (catch 'foo (dolist (str (list str1 str2 str3 str4)) (if (firstFunction str) (throw 'foo (...
jch's user avatar
  • 5,680
10 votes
Accepted

Why does `eval-when-compile` run at file load and byte-compiled to .elc?

Did you ask Emacs what eval-when-compile is supposed to do? C-h f eval-when-compile tells you that it evaluates the arg also at load time: eval-when-compile is a Lisp macro in `byte-run.el'. (eval-...
Drew's user avatar
  • 75.8k
10 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.2k
9 votes
Accepted

How to get exit code of command supplied to `compile' function?

There is no variable set to the last error code. Such a thing can be emulated though by setting compilation-exit-message-function to something custom: (defvar compilation-exit-message-function nil "\...
wasamasa's user avatar
  • 21.8k
9 votes
Accepted

How to select text found by `re-search-forward`?

One way to do it: (defun my-search-forward (str) (interactive "smy-search-forward: ") (when (re-search-forward str nil t) (setf (point) (match-beginning 0) (mark) (match-end 0))))
PythonNut's user avatar
  • 10.3k

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