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
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
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
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
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
9 votes
3 answers
2k views

how to get Emacs to locate the error in my init file without relaunching?

When there's an error in one of my init files, I get a vague message like this when I launch Emacs: Warning (initialization): An error occurred while loading /Users/im/.emacs.d/init.elc: (wrong-type-...
incandescentman's user avatar
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
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
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
2 votes
2 answers
259 views

Evaluating symbol in function arguments affected by destructive operations?

While evaluating a function call, the arguments is from left to right. For example, (let ((x '(1 2 3))) (list (nreverse x) x)) ;; => ((3 2 1) (1)) The first argument (nreverse x) evaluates to (...
xuchunyang's user avatar
  • 14.2k
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
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
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
13 votes
2 answers
5k views

Is there a general way to 'expand' a list for to be used as individual arguments to another function?

For example, say I have a list of strings L, perhaps from an &rest argument. What can I do to L that would have the same effect as the following? (concat (first L) (second L) ... (last L)) (I ...
Sean Allred's user avatar
  • 6,801
4 votes
1 answer
3k views

Why does load-theme reset the custom-theme-load-path?

For some reason, I need to manually update the custom-theme-load-path list after every update of a theme like smart-mode-line or leuven-theme. Looking for a solution, I came across this SO post. Here ...
Kaushal Modi's user avatar
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
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
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
15 votes
4 answers
753 views

Differences between setting major mode keys with a hook versus adding them to the mode map

Assume that I have downloaded a major mode called magical-mode, and it has its own magical keymap called magical-mode-map. This mode also provides a hook magical-mode-hook which is run each time ...
nispio's user avatar
  • 8,143
13 votes
3 answers
665 views

What's the easiest way to implement a function like format-time-string

The format-time-string function takes a string, and replaces a set of special constructs in that string (characters preceeded by %) with some specific text. I'd like to implement a functionality like ...
Malabarba's user avatar
  • 22.8k
12 votes
2 answers
3k views

Get org link to insert link description automatically?

Q: How can I get a custom org link function to insert a description automatically? I'd like to store org links to files in a dired buffer, but would prefer that the link description be the file name ...
Dan's user avatar
  • 32.3k
6 votes
1 answer
1k views

Scope in lambda

In my .emacs conf file, I've got this function that adds a hook to set the compile-command based on the mode. It looks something like, (defun set-compile-cmd (mode-hook cmd) (add-hook mode-hook (...
user1943733's user avatar
4 votes
1 answer
264 views

How to update window-start without calling redisplay?

While debugging swiper I've encountered a situation when the point is changed inside with-selected-window, but window-start isn't updated. This can be annoying, because I need the up-to-date window ...
abo-abo's user avatar
  • 13.9k
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
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
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
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
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
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
16 votes
3 answers
3k views

Is there any way to run a hook function only once?

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
14 votes
7 answers
2k views

Elisp code to check for internet connection

When I open Emacs, it evaluates my init file, which includes refreshing my package archives via internet connection. This is problematic when I don't have an internet connection, thus I need to ...
modulitos's user avatar
  • 2,402
13 votes
2 answers
2k views

Function to delete all comments from a buffer, without moving them to kill ring

I need to be able to remove all comments from a buffer from elisp code. For now, I am using: (goto-char (point-min)) (comment-kill (count-lines (point-min) (point-max))) However, comment-kill is an ...
T. Verron's user avatar
  • 4,193
12 votes
3 answers
1k views

How can I detect if the current character is a comment character for that major mode?

I am working on this little function that pulls up the next line to the current line. I want to add a functionality so that if the current line is a line comment and the next line is also a line ...
Kaushal Modi's user avatar
11 votes
3 answers
2k views

How to wrap given text around region

I often have a situation where I need to apply text around a region. For HTML and simple cases there are libraries for this already. But what about the general case? I'd like to be asked what I'm ...
mike3996's user avatar
  • 463
11 votes
2 answers
760 views

Programmatically insert files into Dired buffer

I have a list of file names (with full path) and I want to create a Dired buffer listing this set of files. How would I go about doing that? How do I create a Dired buffer from a list of file names? ...
Tu Do's user avatar
  • 6,742
11 votes
3 answers
2k views

hl-line-mode hide background, how to avoid this?

With: (when window-system (set-face-background 'hl-line "light yellow") (global-hl-line-mode 1)) current line highlighted by yellow background, but if text has background properties it also ...
gavenkoa's user avatar
  • 3,310
10 votes
4 answers
4k views

How to programmatically execute a command in eshell?

I want to execute simple commands in eshell without explicitly typing them into the prompt, using something like with-current-buffer How can I do this?
caisah's user avatar
  • 4,026
9 votes
1 answer
3k views

Is it possible to execute a function or command at a specific time?

Is it possible to execute a function or command at a specific time? As an explicit example, is it possible to kill emacs at a specific time (time determined by the OS).
Name's user avatar
  • 7,644
9 votes
1 answer
959 views

Can Emacs detect the path of its executable?

Suppose that I have two identical emacs (of the same version) on my system. One is placed at C:\Program Files\Gnu Emacs and the other one at different place, say C:\Program Files\New Gnu Emacs. Can a ...
Name's user avatar
  • 7,644
8 votes
2 answers
301 views

Argspec or arity of a bytecode function in Emacs 24

I have code that tests the arity of a function. I use it to determine whether optional arguments added in recent versions of a package are present. It calls subr-arity for built-in functions and ...
Gilles 'SO- stop being evil''s user avatar
8 votes
2 answers
2k views

Only enable whitespace mode in certain buffers

I would like to have whitespace mode turned on for all buffer except for org-mode ones. It is easy to do this when emacs starts up, but since I use a persistant copy of emacs with emacs daemon mode I ...
jcv's user avatar
  • 205
7 votes
3 answers
1k views

Is there an equivalent for defparameter on emacs-lisp?

I know we have defvar and setq. defvar has a special meaning on defining global variables because the assignment only happens once. So I was thinking about use setq to get the desired behavior of ...
Manoel Vilela's user avatar
6 votes
2 answers
435 views

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

I'm trying to implement the complement function from Common Lisp (CL) in Emacs Lisp, but I'm confused by why defining it as a function (as opposed to a macro) does not work. The macro definition is ...
Tianxiang Xiong's user avatar
5 votes
2 answers
2k views

Can I add highlighting in a minor mode?

I'm trying to understand minor modes with the goal of adding a minor mode for Jinja2 syntax highlighting (from jinja2-mode) to stuff like YAML. I'm running into problems though. When following a ...
Brian Hicks's user avatar
4 votes
3 answers
891 views

What is the easiest way to check if a character belongs to a particular set of characters?

For example suppose that I would like to show the message "yes" if the current character is "a" or "d" or "x", .... Of course I can write as below. But it is very long. I am wondering if there is a ...
Name's user avatar
  • 7,644
3 votes
1 answer
528 views

Monkey patching a function

Some context: I'm doing some linux kernel development on a Debian VM so naturally I'm using tramp. I want to use semantic-symref, but the trouble is that: Semantic will eventually call cedet-gnu-...
c-o-d's user avatar
  • 910
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
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