Episode #125 of the Stack Overflow podcast is here. We talk Tilde Club and mechanical keyboards. Listen now

Questions tagged [functions]

use this tag for Elisp `functions` for their usage, definitions, re-definitions, defining aliases, and other standard features of function execution fundamental to Lisp programming languages. Add additional Elisp tags when the functions are about specific Emacs features.

Filter by
Sorted by
Tagged with
34
votes
1answer
2k views

What is the difference between a function and a command?

When posting questions and answers here, people sometimes use the terms "function" and "command" interchangeably. In other cases, people only use one of the two terms to discuss specific pieces of ...
30
votes
4answers
3k 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-...
25
votes
2answers
1k 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 ...
22
votes
1answer
807 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-...
21
votes
1answer
3k views

Curiosity: What does progn stands for?

According to Emacs documentation: progn is a special form in `C source code'. (progn BODY...) Eval BODY forms sequentially and return value of last one. What does progn stands for (or its origin)? ...
17
votes
2answers
2k 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 ...
12
votes
2answers
4k 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))
12
votes
1answer
941 views

Why can't I bind my function to a key or call it with M-x?

I wrote a function, and I want to call it via M-x, and bind it to a key. This is my function: (defun my-function () (message "This is a great function")) If I try to call it with M-x my-function,...
10
votes
1answer
790 views

How to find out where a function is called from (backtrace/stacktrace)?

I've hit a problem that region is deactivated (in transient-mark-mode). The function deactivate-mark is called and I would like to find out where (and why) it is called from. I tried M-x debug-on-...
9
votes
2answers
435 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 ...
8
votes
2answers
237 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 ...
7
votes
2answers
734 views

How to delete a function?

For what I do I tend to define a lot of functions that I use temporarily. Life span is usually about 1-2 weeks - and then I define a new set of functions. I was not able to find where these ...
7
votes
1answer
392 views

What does `(fset SYMBOL KEYMAP)` do?

I was reading through the projectile source file and they do the following: (defvar projectile-command-map (let ((map (make-sparse-keymap))) ; define-keys map) "Keymap for Projectile ...
7
votes
3answers
355 views

#'and is an invalid function?

I'm trying to see if all the values in a list are truthy. For some reason, (apply #'and lst) errors with: Invalid function: and This also happens when I try (cl-reduce #'and lst). However, (apply ...
7
votes
1answer
1k views

Override a function locally, but allow calls to the original function

The advice feature allows modifying the behavior of a function globally. An advice definition can make calls to the original function. (defadvice foo (around foo-bar activate compile) "Always set ...
7
votes
4answers
913 views

How to obtain a list of all functions exclusively provided by a certain major mode

This question in inspired by https://stackoverflow.com/q/605785/. By M-x describe-function <TAB> I can get a list of all interactive or nonintractive functions available in the current state of ...
6
votes
3answers
523 views

How to find out which function is bound to C-u C-SPC?

C-u C-SPC is used to Move point to where the mark was, and restore the mark from the ring of former marks. When trying to get the function bound to C-u C-SPC with describe-key it just stops ...
6
votes
1answer
414 views

How to make interactive functions (simply) undoable?

This question refers exclusively to interactive functions that modify the contents of the current buffer. What does one need to do to ensure that a single execution of (undo), right after running the ...
5
votes
2answers
257 views

Define an emacs function to take arguments in the same way as something like M-x goto-line

I want to write an emacs function for debug purposes in c++ code. Basically I'm fed up typing the following: std::cout << "variable is: " << variable << std::endl; Instead I want ...
5
votes
2answers
234 views

Keybinding for buffer word count

The following in my .emacs file does not produced the desired result of showing me the word count for the current buffer (it "does nothing"): (global-set-key (kbd "<f9>") (kbd "C-u M-=")) Is ...
5
votes
2answers
178 views

How to repeat a command immediately after calling it?

I've just called an interactive function (i.e., a command). I want to call it again, with the same arguments. I know that I can call a keyboard macro again by using e right after running it the ...
5
votes
2answers
980 views

Partial function application

apply-partially lets you create a new function by fixing the first n arguments of an existing function. Is there any way to achieve unrestricted partial function application, i.e., the ability to fix ...
5
votes
1answer
2k views

Insert line above/below

I have a couple of functions for inserting blank lines above and below the current line: (defun insert-line-below () (interactive) (move-end-of-line nil) (open-line 1)) (defun insert-line-...
5
votes
2answers
1k views

How do I invoke a non-interactive lisp function interactively?

This is dim, but I've forgotten how to do this, and I can't work it out, sorry: I'm in python mode with elpy, and I want to run the function python-nav-beginning-of-defun in a python buffer, to see ...
5
votes
1answer
222 views

How to map a function over several values?

I have a function foo-function that takes the variable x and performs a specific action. The variable x comes before the definition of foo-function: (defvar x "value1") (defun foo-function () (...
5
votes
2answers
191 views

Finding the source of a function after macro expansion?

(I'm a rookie and I use Spacemacs) I'm trying to find the source of a certain function*. I can find the .el file in which it's defined by using find-function, however the defun is created ...
5
votes
1answer
49 views

Can a symbol have a function, variable, and class?

Q: can one symbol refer to a function, a variable, and a class? Elisp is a Lisp-2 in which a symbol can have separate function and variable values. So, for example, I can define the following ...
5
votes
1answer
349 views

Does Emacs Lisp functions return objects by reference or by value?

I tried to determine if Emacs returned object by reference or value, but could not find a clear answer (searching google and Emacs Lisp manual). I guess it must be "by reference", and I just ...
5
votes
1answer
64 views

How can I create an function to build a table of contents at a marker?

I'm planning to move StackMode to a literate program once it matures, but until then I'm using the conventional ^L section markers. My files look like this: ;;; Code: ^L ;;; Section 1 Title (defun ...
5
votes
1answer
547 views

How kill all hidden (non-visible) buffers?

I looked around to be sure, that none had this issue earlier. Okay, sometimes I'm having too many buffers to manage. So I would like to kill all these buffers, except the buffers that are currently ...
5
votes
1answer
272 views

Emacs pdf-tools writing function

Recently I am indulging in Emacs. I tried to use it as my LaTeX tool, so I installed pdf-tools. If I add (pdf-tools-install) in my init file, it will take about 2 seconds to start Emacs. So I want ...
4
votes
3answers
190 views

What are the situations where defining a function as non-interactive is better than interactive?

Assume that we have a function which can be defined either interactive or non-interactive. When we define it as interactive, we can use it as a command by invoking M-x so we have more freedom. I am ...
4
votes
3answers
278 views

How to bind keys to a function which can be defined per-mode?

Currently I have variables defined with setq-local, this is useful for modes, so I can define variables only for a particular mode. How can this be done for functions? I'd like to have a function, ...
4
votes
1answer
573 views

What is identity doing? [closed]

I've seen this sort of thing a few times: (mapconcat 'identity '("" "home" "alex " "elisp" "erc") "/") => "/home/alex /elisp/erc" which means mapconcatwants a function in the second position, ...
4
votes
2answers
470 views

Moving cursor to the middle of the line where it currently is

Is there a way to define a function that, when called, would move the cursor to the middle (or close to the middle) of the current line, similarly to how we can move it to the beginning and end of the ...
4
votes
1answer
48 views

Priority of the executation of two functions with the same name from two different packages

General question: two packages have the same name for a certain function. When both packages are installed, which one has the priority to execute that particular function. Particular situation: I am ...
4
votes
2answers
101 views

Understanding “It is bound to …” message in describe-function

If you ask for a function description, there is a line that starts: It is bound to <...> <...> <...> For example, for isearch it says: It is bound to <menu-bar> <search&...
4
votes
1answer
34 views

Change the behavior of a function when the point is in a table

How can I change the behavior of a function when the point is in an org-table? Is there a function like "table-p" that returns true when the point is in a table (like region-active-p returns true when ...
4
votes
2answers
245 views

defun in defun is not local?

I am reading SICP to learn Lisp, the book uses Scheme dialect. I want accommodate it to emacs lisp, I find something weird in 3.1.1 and cannot figure it out, say I have a test.el file: (defun make-...
4
votes
2answers
111 views

How to sort bash functions alphabetically?

My boss is a bit of a neat freak. He wants the functions in my functions.sh be ordered alphabetically. foo() { echo foo } baa() { echo baa } So baa should appear first in the file. Can emacs ...
4
votes
2answers
71 views

How can I record where a function is defined if it's done indirectly?

I've got a few functions in api-wrap.el that I'll refer to as 'macro code', here simplified to clarify the question: function (generate-form prefix kind &rest args) returns an unevaluated form ...
4
votes
1answer
93 views

Recover a built-in function that I accidentally unbound

I accidentally unbound a built-in function (a subr) from its usual symbol. Is there a generic way to recover it? Of course I could save my buffers and my session, exit Emacs and start another ...
3
votes
3answers
185 views

How to call another function, if one function gives an error message

Suppose I call a function which is named as foo. Sometimes that function foo will give a message such as "Example of error message". If that is the case, then something else should be executed. ...
3
votes
2answers
362 views

How interpret the Lisp way to include arguments in functions?

I have some difficulties to understand how Lisp wanted to receive the arguments. When I look into the documentation about arguments in functions, I need to name the arguments exactly as in the source ...
3
votes
1answer
203 views

How to invoke functions that are quoted in let?

I'm trying to do some processing of values passed in a plist. Processing is quite similar: get a value, convert it to number if necessary, check if within bounds and if yes, return a string with that ...
3
votes
2answers
130 views

elisp search functions

I'm trying to debug a piece of elisp that is in an orgmode template I'd like to use. (link here) The problem is occurring in the source block that is supposed define the cite links. (org-add-link-...
3
votes
2answers
989 views

How to pass a parameter together with function

This code opens 1-st & 2-nd bookmarks when 1 & 2 keys are pressed on bookmarks page: (defun jump-to-n-th-bookmark (n) (let ((bookmarks (sort (bookmark-all-names) 'string<))) (...
3
votes
3answers
145 views

Sort of autoreply for specific messages in minibuffer

Every time when I call M-x projectile-regenate-tags I get bothered with the message Tags file a:/foobar/TAGS has changed, read new contents? (y or n) which is quite cumbersome. Especially when I ...
3
votes
2answers
52 views

How to define a bundle of variable-and-function pairs?

I want to define a bundle of variable-and-function pairs, e.g.: vl/path-doc points to my often used path, and vl/open-path-doc is used for open it in dired-mode. I tried this piece of code: (setq ...
3
votes
1answer
463 views

Define a function alias within a let binding?

How can I define an alias within a script with let-binding? I tried (let (b-s) (defalias 'b-s 'buffer-string)) But it doesn't work. I figured out to let-bind the variable where aliases as stored ...