Skip to main content

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, because most questions here are about using Elisp, and adding this tag to them is redundant. Emacs Lisp is the scripting and programming language that the Emacs editor is built on.

Filter by
Sorted by
Tagged with
90 votes
5 answers
21k 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
57 votes
6 answers
11k 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,687
56 votes
6 answers
22k 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
55 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
  • 25.7k
47 votes
3 answers
11k 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
  • 25.7k
46 votes
2 answers
5k 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
  • 33.1k
41 votes
3 answers
17k 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
  • 23.2k
40 votes
2 answers
11k 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
39 votes
3 answers
22k 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
38 votes
3 answers
13k 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,893
36 votes
4 answers
5k 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
  • 33.1k
35 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,251
35 votes
1 answer
5k 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
  • 453
34 votes
4 answers
8k 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,245
34 votes
3 answers
7k 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,096
33 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
  • 23.2k
31 votes
3 answers
5k 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.7k
31 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,239
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.4k
26 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
26 votes
6 answers
9k 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,568
25 votes
1 answer
908 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,209
24 votes
6 answers
10k 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,756
23 votes
2 answers
7k 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,733
23 votes
1 answer
4k 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,772
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
  • 23.2k
23 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
22 votes
4 answers
10k 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
  • 33.1k
22 votes
1 answer
8k 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
  • 19.2k
21 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.4k
21 votes
2 answers
9k 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,222
21 votes
5 answers
7k 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
21 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,878
20 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
  • 33.1k
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
  • 33.1k
20 votes
3 answers
9k 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
  • 2,450
19 votes
2 answers
7k 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,979
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 Ray Hamilton's user avatar
19 votes
3 answers
3k 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
  • 33.1k
18 votes
5 answers
3k 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,951
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,951
18 votes
1 answer
1k 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,009
17 votes
1 answer
596 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,951
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,222
16 votes
1 answer
861 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
  • 835
16 votes
2 answers
612 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
  • 33.1k
16 votes
4 answers
3k views

Run a Function Only Once, Though a Hook Many Times

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 ...
joe di castro's user avatar
15 votes
9 answers
3k views

How to edit elisp without getting lost in the parentheses

I'm modifying some elisp code from linum.el: (custom-set-variables '(linum-format 'dynamic)) (defadvice linum-update-window (around linum-dynamic activate) (let* ((w (length (number-to-string ...
Zhro's user avatar
  • 359

1
2 3 4 5
9