12

Functions are unbound with: (fmakunbound 'NAME) (The equivalent for variables is makunbound.)


8

(defvar my-local-variable nil "Variable used on a bufer-local basis to indicate ...") (make-variable-buffer-local 'my-local-variable) USAGE: (setq my-local-variable (lambda () (message "major-mode: %s" major-mode))) (define-key name-of-major-mode-keymap [f5] (lambda () (interactive) (funcall my-local-variable)))


7

In addition to removing the function definition with fmakunbound and variable binding with makunbound, you can also remove the name itself with unintern. Note also that, unless you save your definitions in a file (e.g., init file) and load it, your functions and variables will be gone when you restart Emacs.


7

From the docstring of C-x C-c: With prefix ARG, silently save all file-visiting buffers, then kill. So you want to use C-u C-x C-c EDIT: If you also want that the above silently kills all running processes, then you can set the variable confirm-kill-processes to nil.


6

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))


5

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. Each definition can take the form (FUNC EXP) where FUNC is the function name, and EXP is an expression that returns the function value to which it should be ...


5

This is a representation of the character whose code is 367 in octal. This "general escape representation" is described in the elisp manual. You can use the function text-char-description as: M-: (text-char-description ?\367) or M-x describe-char with point on the char to see this is the character DIVISION SIGN. Now the question is what does it stand ...


5

The short answer is no. Once you declare a class, Emacs will define a function with the name of that class as a creation function, thereby replacing your old function definition. In Emacs, eieio.el has a macro for defclass that goes like so: (defmacro defclass (name superclasses slots &rest options-and-doc) ;; A lot of stuff here... ;; Non-...


4

This is because you modify the variable binding, not the data structure. append creates a fresh list, while nconc modifies its argument. Replace (setq perfect-numbers (append perfect-numbers new-perfect-numbers)) with (nconc perfect-numbers new-perfect-numbers) and you will get the behavior you think you want. I must advise against this though because ...


3

You can use func-arity added in 26.1. C-h f func-arity: func-arity is a built-in function in `C source code'. (func-arity FUNCTION) Return minimum and maximum number of args allowed for FUNCTION. FUNCTION must be a function of some kind. The returned value is a cons cell (MIN . MAX). MIN is the minimum number of args. MAX is the maximum ...


3

Try this: (let ((name "foo")) (fset (intern name) (lambda (x) (* 2 x)))) (foo 21) ; 42 Brief explanation: (intern name) creates a symbol with the given name and adds it to the list of accessible symbols (that is what interning means). But if the symbol with that name already exists, it does nothing; just returns the already existing symbol. And (lambda …) ...


3

Use cl-labels to define local functions (cl-flet works, but recursive function reports error), and in the beginning of the .el file enable the lexical scoping: ;; -*- lexical-binding: t -*- (defun make-account () (cl-labels ((withdraw (amount) (print "in withdraw"))) (lambda () #'withdraw))) Remember the # before the quoting ...


3

All the modes you list are minor modes defined by define-minor-mode. From the doc of define-minor-mode: When called from Lisp, the mode command toggles the mode if the argument is ‘toggle’, disables the mode if the argument is a non-positive integer, and enables the mode otherwise (including if the argument is omitted or nil or a positive integer). ...


2

Note: see Stefan's anwser. It seems that local functions are not a native feature of the language. The GNU Reference Manual does not cover "local functions" or mention it. (defun foo (a b) (+ a b)) (let (foo) (setq val (symbol-value 'foo)) (symbol-function 'foo)) (symbol-function 'foo) returns (lambda (a b) (+ a b)) whereas (symbol-value 'foo) ...


2

I did C-h f C-h f and got a *Help* buffer describing describe-function as a "Lisp function" defined in help-fns.el. I clicked on the file name and searched for "Lisp function" which lead me to the function help-fns-function-description-header whose code indicated that what we want is subrp. (subrp (symbol-function 'car)) ==> nil A bit more consideration ...


2

(subrp (symbol-function 'json-serialize)) should do it. See Functions for details.


2

If you've just typed or modified the function and you want to (re)define it, press C-M-x (eval-defun) with the cursor anywhere in the definition. To run the function, use M-: (eval-expression) and type (count-words-buffer) then RET. If the function needed arguments, you'd need to add them after the function name, e.g. (my-function "first argument" 'second-...


2

Not exactly. Function buffer-substring requires two arguments. Function - will accept a single argument, but in that case it just returns the negative of that numeric argument. What you want to do is, in effect, apply such functions to a list of arguments. You can use higher-order function apply to do that. What you can do, if you want, is have a macro or ...


2

(mapcar (lambda (fn) (funcall fn 1)) '(desktop-save-mode show-paren-mode))


1

I ended up finding a suitable solution on another SO answer found here: How to evaluate the variables before adding them to a list? (quote ARG) Return the argument, without evaluating it. (quote x) yields x. Warning: quote does not construct its return value, but just returns the value that was pre-constructed by the Lisp reader... Hence, you ...


1

vc-git-grep is an interactive function which prompts, so when you want to call it from your own code you need to use call-interactively, otherwise it expects you to have supplied its required arguments in the call: (defun Pfedj/vc-git-grep-noncase() (interactive) (setq vc-git-grep-template "git --no-pager grep -n -i -e <R> -- <F>") (call-...


1

At first the general statement: There is no buffer-local setting for the function cell of a symbol. Now, I am just putting together the comments of lawlist and John Kitchin. One can call an overriding function assigned to a buffer-local variable in an :around advice of helm-swoop-pre-input-optimize: (defvar-local helm-swoop-pre-input-optimize-function nil ...


1

#' (= function) is more verbose than ' (= quote), and it provides three practical benefits. 1. Byte compiler will check if the function is defined Byte compile a file which contains the following code (mapc #'2+ nil) (mapc '3+ nil) The byte compiler will warns us that 2+ is not defined: foo.el:3:1:Warning: the function `2+' is not known to be defined....


1

quote (') returns its argument sexp - any Lisp object (thing). function (#') returns its argument sexp as a function object. This also means that in code you byte-compile it tells the byte compiler that that thing is a function. 'foobar just evaluates to the symbol foobar. #'foobar evaluates to a function. '(lambda) () (something)) evaluates to the list ...


1

Posting answer to own question, including an example. As @lawlist points out this can be done using defvar-local, this example uses macros to avoid writing boiler plate code and prints a message if the function isn't defined, runs a fallback function. (defmacro my-generic-fn (var description) `(defvar-local ,var (lambda () (interactive) (...


1

If a library developer is responsible for definining the mode dependent behavior and you do not plan to allow the user to change it per defcustom you can use mode dependent overloads. The code below is for playing around with mode local overrides. In buffers derived from text-mode [f9] prints "action in text-mode." in other buffers it prints "default ...


1

I think I solved this with the following ;; [M-x my-quit] and [C-c q] ;; save all buffers then shuts down emacs stopping all current processes (defun my-quit () (interactive) (save-some-buffers t) (kill-emacs)) (global-set-key (kbd "C-c q") 'my-quit) I bind the function to C-c q which I don't use, but you can bind the ...


1

You can force entering the debugger when a function is called, (debug-on-entry 'myf) Any calls to myf triggers the debugger. A (myf 2 3) call, for example, would result in Debugger entered--entering a function: * (myf 2 3) ... Remove it when you're done, (cancel-debug-on-entry 'list)


Only top voted, non community-wiki answers of a minimum length are eligible