Questions tagged [defun]
37 questions
16
votes
3 answers
Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"
I want to get the effect of a static variable by using defun inside of let with lexical binding to create a closure. However, when byte-compiling the file, I get a warning. Am I doing something wrong, or if not, is there a way to suppress this…

Rose Kunkel
- 273
- 1
- 7
8
votes
3 answers
Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't
My example is simplified:
(defvar wtf 10)
(defun f (wtf)
(lambda ()
(cl-incf wtf)))
(setq f (f 20))
(setq g (let ((wtf 30))
(lambda ()
(cl-incf wtf))))
(list (funcall f)
(funcall g)
wtf)
;; ==> (21 11…

shynur
- 4,065
- 1
- 3
- 23
6
votes
1 answer
How to make local function binding for closure?
For example, when defining the natural number sequence stream, I can use
;; -*- lexical-binding: t; -*-
(defun nats (n)
(cons n (lambda () (nats (1+ n)))))
(nats 0)
=> (0 closure ((n . 0) t) nil (nats (1+ n)))
But I want to call simply…

xuchunyang
- 14,302
- 1
- 18
- 39
5
votes
2 answers
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-account ()
(defun withdraw (amount)
(print "in…

Alaneuler
- 277
- 1
- 8
4
votes
1 answer
Make a variable visible to some functions only
Consider the following metacode:
;;; -*- lexical-binding: t -*-
(defvar var1 ...)
(defvar var2 ...)
(defun main ()
"Main entry point" ...)
(defun func1 ...)
(defun func2 ...)
...
(defun funcn ...)
(defun f ...)
(defun g ...)
If a variable is…

antonio
- 1,762
- 12
- 24
3
votes
2 answers
the Name of Function defined in C
I tried to use GDB to debug Emacs.
More specifically, I want to observe the behavior of a function written in C.
vertical-motion is a built-in function in ‘C source code’.
However, the name of a C function cannot contain a - (i.e., minus sign). …

shynur
- 4,065
- 1
- 3
- 23
3
votes
2 answers
Create a function that returns a new function definition
I'm creating a modular system for my use of Emacs, so I can call modules on the fly, as I need them. Just like a lazy call, but I must explicitly call them.
Anyways, I found this riddle, I want a function lw/define-loadable such that
(defun…

BuddhiLW
- 257
- 1
- 7
3
votes
3 answers
How does scoping work in emacs lisp
A lot of emacs configurations shared publicly have this format:
;; 01-something.el
(provide 'something)
;; init.el
(require 'something)
Suppose I'm writing a defun named "s-join" inside 01-something.el
Suppose somewhere during init.el…

american-ninja-warrior
- 3,773
- 2
- 21
- 40
3
votes
2 answers
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 vivo-work-directory "~/Workspace/vivo/")
(defvar…

Vivodo
- 133
- 5
3
votes
0 answers
Interactive lambda functions
How do I create interactive lambda functions?
(use-package general
:ensure t
:config
(general-evil-setup t)
;;(setq general-default-keymaps 'evil-normal-state-map)
(general-define-key
:states '(normal motion emacs)
:prefix "SPC"
…

Maik Klein
- 161
- 2
2
votes
2 answers
Why can't I defun "nil" as a function name
I know that nil is a constant so its value cell cannot be changed,
but why its function cell cannot be set as well?
ELISP> (defun t ())
t
ELISP> (defun nil ())
*** Eval error *** Cannot define ‘nil’ as a function

shynur
- 4,065
- 1
- 3
- 23
2
votes
0 answers
Optional docstring in defun
After taking a look at the definition of defun in byte-run.el, it seems that the macro does some very interesting things regarding checking for the existence of a docstring.
First, it uses a declare form to indicate that the third argument to the…

Tianxiang Xiong
- 3,848
- 16
- 27
1
vote
1 answer
Is there a reason for having beginning-of-defun-function/end-of-defun-function global?
I was working on a mode, setq'ed them with my functions and... emacs broke (really)
For some reason these variable are global. I made them buffer-local, but is there a good reason for having them global in the first place or simply they forgot about…

Lorenzo Marcantonio
- 113
- 4
1
vote
1 answer
How can I create an alias for a function name?
Does Elisp allow the equivalent of an alias for a function name?

Dilna
- 1,173
- 3
- 10
1
vote
1 answer
How to pass a string argument to a function from global-set-key definition inside .emacs
Inside my .emacs file I have these two functions:
(defun bh/switch-to-vs ()
(interactive)
(universal-argument)
(shell "*vs*"))
(defun bh/switch-to-android ()
(interactive)
(universal-argument)
(shell "*android*"))
I bind keys to those…

W.M.
- 113
- 3