9 votes
Accepted

Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"

The byte-compiler's way to decide whether a function will be defined or not is very "naive" and gets fooled even in your "obvious" case. But you can write it in a way that lets the compiler understand ...
Stefan's user avatar
  • 26.4k
8 votes
Accepted

How to make local function binding for closure?

Without cl-lib: ;; -*- lexical-binding: t; -*- (defun nats () (letrec ((inner (lambda (n) (cons n (lambda () (funcall inner (1+ n))))))) (funcall inner 0))) (let* ((stream (nats)) (i (...
wasamasa's user avatar
  • 22.2k
6 votes

Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"

To suppress the byte-compiler warning, try adding this before your code, starting in column 0 (leftmost): (declare-function increase-count "your-file-name.el") C-h f declare-function tells you: ...
Drew's user avatar
  • 77.5k
5 votes
Accepted

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

make-symbol returns an uninterned symbol. That means you get a symbol with name, function cell, value cell, and property list but the symbol is not registered in the global obarray. Therefore, you ...
Tobias's user avatar
  • 33.2k
5 votes

How does scoping work in emacs lisp

All functions and variables defined in a file that gets loaded are put into the global environment. Although Emacs has installable packages, those packages are not isolated from each other in any way. ...
db48x's user avatar
  • 18.1k
5 votes
Accepted

Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't

(defvar wtf 10) made wtf bound dynamically so the variable g would get a closure which captured nothing. This is correct. However, this cannot explain what (f 20) returned. It can if, under lexical ...
phils's user avatar
  • 51.1k
4 votes
Accepted

defun in defun is not local?

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 ...
Alaneuler's user avatar
  • 277
4 votes
Accepted

Create a function that returns a new function definition

I would use a macro, it is easier and this is what macros are for: (defmacro lw/define-loadable (fn-name mod-name) `(defun ,fn-name () (interactive) (print (concat "../modules/" ,...
jue's user avatar
  • 4,576
4 votes

Lambda in `defun` Captures the Lexical Environment, But in `let` It Doesn't

A long comment instead of an answer. (SE limits the length of a comment.) Hi phils, this is my comment on your answer. potentially causing other functions, called later in the stack, to obtain an ...
shynur's user avatar
  • 5,288
3 votes

How can I create an alias for a function name?

Yes. You can have any number of aliases for the same function. To do that, use defalias (or fset, but defalias is generally recommended). Example: define mop as an alias for means-of-production. (...
Drew's user avatar
  • 77.5k
3 votes
Accepted

How to create a function with a computed name?

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 ...
Harald Hanche-Olsen's user avatar
3 votes

Make a variable visible to some functions only

defalias "magically" stores the variables inside its functions, so their values can be reused with subsequent calls. This is not what defalias does; see (elisp) Defining Functions. The means by which ...
Basil's user avatar
  • 12.4k
3 votes

Defun inside let with lexical binding gives byte-compile warning "the function is not known to be defined"

I believe placing the definition in question within eval-and-compile would also superficially achieve the same result as in Stefan's correct answer: (eval-and-compile (let ((count 0)) (defun ...
Basil's user avatar
  • 12.4k
3 votes
Accepted

eval-last-sexp produces unnecessary void-variable error when using a macro containing eval

After looking through the GNU Emacs Lisp Reference Manual, I realized that there is a subsection of "Common Problems Using Macros" called "Evaluating Macro Arguments in Expansion" which explains this ...
Jonathan Ganc's user avatar
3 votes

Why can't I defun "nil" as a function name

From a language design point of view, Emacs Lisp accepts nil as a parameter to functions to mean “do nothing” or “do some sensible default” or “turn it off” in a lot of contexts. Being able to assign ...
Gilles 'SO- stop being evil''s user avatar
3 votes

the Name of Function defined in C

Provide a reference (from the manual) for @phils's answer; Supplement the conversion convention for the name of the function. E.7 Writing Emacs Primitives: Here is a template for them: DEFUN (lname, ...
shynur's user avatar
  • 5,288
2 votes

Play chess, login on a FICS server

From a quick scan of chess-ics.el (I've not used it myself so haven't verified this) it looks as though you ought to be able to configure chess-ics-server-list to include a handle and password on a ...
stevoooo's user avatar
  • 737
2 votes
Accepted

Identities between let/lambda/defun expressions and simplifying nested progn

lambda seems to have an "inherent" progn as well, such that (funcall (lambda (x y z) A B C) 1 2 3) is equivalent to (let ((x 1) (y 2) (z 3)) A B C). "Equivalent" is a strong word. Calling a lambda ...
Basil's user avatar
  • 12.4k
2 votes

Symbol's value as variable is void

The following code: (if system-is-windows (scroll-bar-mode 0)) Does not evaluate system-is-windows as a function, but tries to evaluate it as a variable. That's why you're getting the (void-...
Dan's user avatar
  • 33k
2 votes

copy string at point func

Before you say "thing-at-point" - I cannot install it (work network). thing-at-point has been built into Emacs since 1993. thing-at-point is an autoloaded compiled Lisp function in ‘...
Basil's user avatar
  • 12.4k
2 votes
Accepted

copy string at point func

You can try the following command. It has certainly lots of corner cases as it uses a simple whitespace syntax. If your intention is to copy arbitrary text, it's easier to select the region C-SPC (set-...
Firmin Martin's user avatar
2 votes
Accepted

How to write a command which uses the prefix argument AND some strings

Your string example shows that you already know how to separate multiple interactive arguments, by putting a newline between each one in the interactive spec. So rather than: (interactive "PstEins: \...
phils's user avatar
  • 51.1k
2 votes

Create a function that returns a new function definition

I too would use an Elisp macro, such as @jue's answer shows. But I'll mention that you could also do this: (defun lw/define-loadable (fn-name mod-name) (fset fn-name `(lambda () (...
Drew's user avatar
  • 77.5k
2 votes
Accepted

the Name of Function defined in C

The C function name is Fvertical_motion. DEFUN is a macro defined in lisp.h. It is used to define C functions which are also visible as lisp functions. /* Define a built-in function for calling from ...
phils's user avatar
  • 51.1k
2 votes
Accepted

Is there a reason for having beginning-of-defun-function/end-of-defun-function global?

I can't think of a particular reason why it couldn't have been automatically buffer-local. or simply they forgot about them? More likely it was simply considered fine as it was, but you'd probably ...
phils's user avatar
  • 51.1k
2 votes
Accepted

Defmacro: Create functions from a list o functions

Finally I found a solution: (defun generate-mwe-star-functions (functions) "Generate starred versions of the given functions, where original functions are wrapped with the `with-ignore-mouse-...
Gabriele's user avatar
  • 1,554
1 vote
Accepted

How to access the last-yanked text programmatically?

The last thing you yanked is typically the front of the kill-ring. You can access that programmatically as (current-kill 0 'DONT-MOVE). For example: (defun my-command () "..." (...
Drew's user avatar
  • 77.5k
1 vote
Accepted

How to pass a string argument to a function from global-set-key definition inside .emacs

(defun example-one (&optional shell-name) "Example where `current-prefix-arg' is hard-coded to be non-nil." (interactive) (let ((current-prefix-arg t) (shell-name (or shell-...
lawlist's user avatar
  • 19.1k
1 vote
Accepted

How can I save a function defintion

You can use symbol-function to obtain the current function slot value for a given symbol, which is typically what you'd be after here. You can store that value anywhere you want, and restore it later (...
phils's user avatar
  • 51.1k
1 vote

How does scoping work in emacs lisp

OP seems to be missing the point already clearly made in the two answers given: you cannot do what you are asking. Emacs uses a voluntary naming convention, not syntax or compilation rules, to define ...
Phil Hudson's user avatar
  • 1,741

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