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 ...
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 ...
17
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 ...
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 ...
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 (...
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 ...
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 ...
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:
...
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♦
- 32.7k
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)
(...
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 ...
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 ...
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 ...
10
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 ...
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-...
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))...
9
votes
How to pass function as argument in elisp
To recap what is in the comments, funcall unquotes its argument (by just accessing the argument). And before that, we also need to make sure that we don't try to evaluate or access the value of the ...
9
votes
Data structure for triplet, 3-tuple?
I'd suggest using a cl-struct. Here's an example:
(cl-defstruct record name date city)
(make-record :name "Hank" :date "01.09.1999" :city "Syracuse")
;; [cl-struct-record "Hank" "01.09.1999" "...
9
votes
Accepted
Scheme-like lambda expressions?
It's not a big deal. You can still:
(setq blah-1
(lambda (n) (* n n)))
(funcall blah-1 3)
;; => 9
(fset 'blah-2 (lambda (n) (* n n)))
(blah-2 3)
;; => 9
Basically, Elisp is LISP-2, and ...
9
votes
Why doesn't "complement" work as a function?
What @Constantine said about lexical and dynamic scoping is true, and it explains the difference from Common Lisp behavior.
However, there is something misleading in your question. This really has ...
9
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 ...
8
votes
Elisp code to check for internet connection
A simple solution that I've adopted from my shell scripts is
(defun internet-up-p (&optional host)
(= 0 (call-process "ping" nil nil nil "-c" "1" "-W" "1"
(if host ...
8
votes
String equality, ignore case?
Another short alternative:
(cl-equalp "string" "strinG") ; => t
(cl-equalp "strinG" "this string") ; => nil
(cl-equalp "word" "...
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 (...
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 ...
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♦
- 32.7k
7
votes
Accepted
how to direct an elisp defun to continue despite errors?
Yes, you can use the macro ignore-errors, it is a wrap of the special form condition-case, use the latter if you want to handle that error.
ELisp Ref: Writing Code to Handle Errors or (info "(elisp) ...
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 ...
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)
(...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
elisp × 415elisp-macros × 25
functions × 23
org-mode × 18
hooks × 15
variables × 13
init-file × 12
regular-expressions × 9
files × 9
lexical-scoping × 9
key-bindings × 8
debugging × 8
string × 8
list × 7
text-properties × 7
quote × 7
symbols × 7
common-lisp × 7
buffers × 6
package × 6
window × 6
major-mode × 6
comment × 6
byte-compilation × 6
advice × 6