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
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 ...
J David Smith's user avatar
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 ...
mbork's user avatar
  • 1,647
55 votes
6 answers
21k views

Can I use org-mode to structure my .emacs or other .el configuration file?

My .emacs configuration file gets bigger and bigger and I'd like to get a better overview and structuring by adding headings, subheadings and being able to hide or show them like I can do with Emacs ...
MostlyHarmless's user avatar
53 votes
3 answers
6k views

How to save a keyboard macro as a Lisp function?

Very frequently I would need to perform the set of steps requiring multiple key strokes. For the same session those steps can be recorded in a keyboard macro. An example is saving a frequently ...
Kaushal Modi's user avatar
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, ...
Dan's user avatar
  • 32.3k
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 "...
Kaushal Modi's user avatar
39 votes
2 answers
10k views

Optional parameter defaults

Emacs Lisp does not have syntactical support for non-nil defaults of optional parameters. What is the recommended idiom for supplying these parameters? To clarify my point, here is one overly ...
Matthew Piziak's user avatar
38 votes
2 answers
12k views

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

How to check if a string s1 is a substring of another string s2? For example (test-substring "f t" "df tj") --> t, (test-substring "ft" "df tj") --> nil.
Name's user avatar
  • 7,644
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 ...
shadowthief's user avatar
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 ...
Malabarba's user avatar
  • 22.8k
35 votes
4 answers
4k views

When to sharp-quote a lambda expression?

Q: When, if ever, is it useful to sharp-quote a lambda, and when, if ever, must we not sharp-quote a lambda? People use lambdas in three ways: plain: (lambda (x) x) quoted: '(lambda (x) x) sharp-...
Dan's user avatar
  • 32.3k
33 votes
4 answers
7k views

How can I simulate an arbitary key event from Elisp?

Is it possible to simulate an arbitrary key event from elisp? I am aware of ways that I can find the binding for a given key, and then call that command interactively, but what if that key event is ...
nispio's user avatar
  • 8,143
33 votes
1 answer
6k views

Why do regular expressions created with the regex builder use syntax different from the interactive regular expressions?

So, using the regular expression builder (M-x re-builder), finding lines that end in \ takes "\\$", while in search and replace by regex, it only takes "\$". I would have expected the regex builder ...
user2699's user avatar
  • 2,161
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 ...
caisah's user avatar
  • 4,026
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 ...
Malabarba's user avatar
  • 22.8k
32 votes
1 answer
4k views

What is the difference between ' and #' in front of a symbol?

I'm a little new to Emacs. When looking at some of the configurations, I found there are two types command in "add-hook". (add-hook 'LaTeX-mode-hook #'LaTeX-math-mode) and (add-hook 'LaTeX-mode-...
X.Arthur's user avatar
  • 423
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 ...
itsjeyd's user avatar
  • 14.5k
31 votes
1 answer
4k views

How do I use nadvice?

My config is full of advice, and I keep hearing about the new shiny minimalist nadvice.el package. I've searched the manuals, and I've read the source, but I'll openly admit: I still have no idea ...
PythonNut's user avatar
  • 10.2k
29 votes
2 answers
2k views

Can functions access their name?

In C there is the magic variable __func__ that holds the current function name. In Bash, there is an array FUNCNAME holding the names of all functions in the calling stack !!! Is there a similar ...
phs's user avatar
  • 1,095
26 votes
6 answers
8k views

How can I get an org-mode outline in a 2nd buffer as a dynamic table of contents?

When I edit large documents, I would like to see where I'm at by seeing the outline (with no-content) in a separate buffer. Like when you read a PDF file there is a TOC on the left. (see below) In ...
Leo Ufimtsev's user avatar
  • 4,478
25 votes
2 answers
6k views

What is the point of quote with single argument and comma? (quote ,arg)

(quote x) makes x not be evaluated, but adding a , in front of x within quote makes it evaluated. What is then the point of using quote with a single argument with a comma in front of it, like so: (...
The Unfun Cat's user avatar
25 votes
1 answer
850 views

What is the function `purecopy` needed for?

I was browing through compile.el and I noticed this construction: (defcustom compile-command (purecopy "make -k ")) The purecopy docs state: purecopy is a built-in function in `C source code'. ...
dgtized's user avatar
  • 4,159
24 votes
6 answers
9k views

Get all regexp matches in buffer as a list

On the Code Golf Stack Exchange site today, I found this answer in Clojure to the question "Get all links on a webpage". (->> (slurp "http://www.stroustrup.com") (re-seq #"(?:http://)?www(...
nanny's user avatar
  • 5,644
23 votes
2 answers
6k views

Difference between load-file and load

I went through the documentation of both the function but they don't seem to shed much information. What is the actual difference between them apart from the fact that load is a built-in function in C ...
Sibi's user avatar
  • 3,583
23 votes
1 answer
3k views

How to export top-level headings of org-mode buffer to separate files?

How can each top-level heading of an org-mode buffer be exported to a separate file named after the value of the corresponding CUSTOM_ID + (sanitized) title? Let's say a buffer contains: * Title of ...
gsl's user avatar
  • 1,722
23 votes
1 answer
2k views

What algorithm does sort use?

I need to add a single integer to a list that's already sorted, such that it goes in the right place. My first tought was something like (sort (cons newelt list) #'<) However, given that list is ...
Malabarba's user avatar
  • 22.8k
22 votes
1 answer
7k views

Get content of a buffer

So buffer-string gets the content of the current buffer. But it doesn't allow specifying other buffers. How can I get around that? Do I need something like save-window-excursion to make it work?
Maciej Goszczycki's user avatar
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, ...
lawlist's user avatar
  • 18.6k
22 votes
1 answer
4k views

Emacs Lisp comment conventions

The Emacs Lisp Reference Manual's Appendix D.7 mentions some comment tips: Single semicolons (;) should be used for inline comments. Double semicolons (;;) should be used for line comments. Triple ...
Tianxiang Xiong's user avatar
21 votes
5 answers
6k views

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

I have elisp code I'd like to run in orgmode files when they load (different for different files, and defined in the file itself). Is there a way to do this? I didn't see anything in http://orgmode....
avv's user avatar
  • 1,563
20 votes
3 answers
2k views

Swap two variables in Elisp

Suppose I have (setq a 1 b 2) How can I elegantly swap the values of a and b without using a temporary variable?
PythonNut's user avatar
  • 10.2k
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 ...
Dan's user avatar
  • 32.3k
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 ...
Dan's user avatar
  • 32.3k
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))
ocodo's user avatar
  • 1,202
20 votes
2 answers
3k views

When should sharp quotes be used?

I see sharp quotes being used in other people's eLisp code, and I use them myself, but I'm not completely clear on when they are appropriate and when not. Could anyone clarify on exactly when it's ...
izkon's user avatar
  • 1,768
20 votes
3 answers
8k views

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

My current use case is to find where the Cask executable is (it can be installed in at least two places, and there's an issue open about this). I need to require cask this way: (require 'cask "~/....
Trevoke's user avatar
  • 1,870
19 votes
5 answers
3k views

Map a function across a property list?

Q: what is the idiomatic way to map a function across a property list? The various mapping functions (mapcar and family) map a function over a sequence such as a list. How does one use these ...
Dan's user avatar
  • 32.3k
19 votes
1 answer
3k views

How to know my buffer's visible/focused status?

I'm writing an extension that talks to an outside process, so it is a given to lower the amount of 'hey whats up' requests when my buffer isn't focused. So, what is the best way to recognize: When ...
Łukasz Gruner's user avatar
19 votes
3 answers
2k views

Elisp regexps ^ and $ vs ` and '

The manual describes the regexp special characters ^ and $. Like in most regular expression dialects I know, they seem to match the start or end of a string. However, I've also discovered that there ...
Jackson's user avatar
  • 1,208
18 votes
5 answers
2k views

How can I map over a vector and get a vector?

The only thing I've found that works is (eval `(vector ,@(mapcar #'1+ [1 2 3 4]))) => [2 3 4 5] but that seems far too complicated to be the 'right' way.
Sean Allred's user avatar
  • 6,801
18 votes
4 answers
2k views

Automatically re-indenting elisp code when adding or removing code before an indented block

Say I have a elisp code like: (+ 2 3▮(+ 3 4)) Is there a way to automatically re-indent the sexp after the cursor, when I add or remove symbols? So after pressing SPC 4 SPC, I would ...
Maciej Goszczycki's user avatar
18 votes
1 answer
1k views

What are good tools for Emacs package development? [closed]

We already have a question about tools useful specifically for testing, but what about actually writing the code? Which packages do you find speed up your development? Which aids do you use to ...
Sean Allred's user avatar
  • 6,801
18 votes
1 answer
977 views

Interrupt evaluation and observe current stack in the emacs-lisp debugger?

If some emacs-lisp function, such as a major mode hook, is taking longer than expected to execute, I would sometimes like to interrupt it and look at the current stack. Ideally, I'd also like to be ...
Kirill's user avatar
  • 1,019
17 votes
2 answers
6k views

How to properly use defcustom?

Like most Emacs users I've customized a mode by changing the variables. What never occurred to me is the whole programming mentality behind having customizable this-and-that. I realized this when I ...
147pm's user avatar
  • 2,887
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 ...
Clément's user avatar
  • 3,914
17 votes
3 answers
2k views

How to create :keywords?

Q: How does one create and use :keywords? Consider a (naive, apparently) attempt to access the following toy alist: (setf alist '((:key-1 "Key no. 1") (:key-2 "Key no. 2"))) (assq :...
Dan's user avatar
  • 32.3k
17 votes
1 answer
581 views

How can I bring back `nil`?

I've accidentally run the following: (unintern variable) where variable's value was nil. How do I get nil back without restarting Emacs?
Sean Allred's user avatar
  • 6,801
17 votes
2 answers
3k views

Heredoc or equivalent multiline string syntax in Elisp?

In common-lisp we have the library cl-heredoc, is there an equivalent in EmacsLisp?
ocodo's user avatar
  • 1,202
16 votes
1 answer
819 views

Why do elisp files have end of file comments?

Why do elisp files usually end with ;;; file.el ends here? Is there some historical reason why this was useful? I've seen it recommended in elisp style guides and I still see it in modern elisp ...
Qudit's user avatar
  • 828
16 votes
2 answers
582 views

how to detect if inside a matched pair?

Q: is there a general way to detect whether or not point is inside a matched pair of characters? That is: is there a general predicate function (or something) that can determine if point is in-...
Dan's user avatar
  • 32.3k

1
2 3 4 5
9