Hot answers tagged

8

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 (car stream))) (while (< i 10) (message "Got %i" i) (setq stream (funcall (cdr stream)) i (car stream)))) See also https://github.com/...


7

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 what happens: (defalias 'increase-count (let ((count 0)) (lambda () (interactive) (setq count (1+ count)) (message "Count is: %d" ...


5

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 cannot use it for function evaluation with the usual parenthesis notation. If you replace make-symbol by intern the symbol is also registered in the obarray and ...


5

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: declare-function is a Lisp macro in subr.el. (declare-function FN FILE &optional ARGLIST FILEONLY) Tell the byte-compiler that function FN is ...


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

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 increase-count () (interactive) (setq count (1+ count)) (message "Count is: %d" count)))) I am, however, barely familiar with the subtleties of ...


3

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 issue. The basic problem is that, when a macro is compiled as opposed to interpreted, the first evaluation of the macro is inserted into function definitions ...


2

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 per server basis, or you should be able to set chess-ics-handle and chess-ics-password from your init file.


2

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 variables are "captured" in functions is called scoping and is specific to neither of defun, defalias or fset; see (elisp) Variable Scoping. With defalias ...


2

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: \nstZwei: ") You wanted: (interactive "P\nstEins: \nstZwei: ")


1

One way to do this is to avoid the defun macro and use defalias for setting up the function: (defalias (intern "tjb-make-register") (lambda (func-name out-string) (interactive "sFunc-name:\nsfunc-out\n") (fset (intern func-name) (eval `(lambda () (interactive) (insert ,out-s\ tring))))))


1

I did not "remove" defun. You cant look up functions with describe-variable. I solved it by adding this: (setq org-archive-default-command 'org-archive-subtree-hierarchical) and using C-c C-x C-a instead of spacemacs defaults.


1

You have this (defun edit-indirect-scss (beg end &optional display-buffer) (interactive) (edit-indirect-region beg end &optional display-buffer) (scss-mode)) This sexp passes 4, not 3, arguments to function edit-indirect-region: (edit-indirect-region beg end &optional display-buffer) Get rid of the argument &optional. &...


1

@Tobias nice. Here still a command making new entries: (defun vl/new-pathsfunc () (interactive) (let ((directory (read-from-minibuffer "directory: " (substring default-directory 2))) (name (intern (read-from-minibuffer "Name: ")))) (unless (map-contains-key vl/paths name) (push (cons name directory) vl/paths) (vl/create-path-funcs vl/...


1

AFAIU fixing this would require a hack around of SPECPDL_INDEX in C-source. However, ar-backward-defun,ar-forward-defun jumps to start and end of a defun also in these cases: https://github.com/andreas-roehler/thingatpt-utils-core/blob/master/ar-subr.el Wherefrom a command to escape opening parens in column 0 might be provided: (defun ar-auto-escape () (...


1

Well, got it, I made this and setted it up on my init scripts: ;; Chess ICS (defun playchess () (interactive) (setq username (read-string "Username: ")) (if (string= username "guest") (chess-ics "freechess.org" 5000 username) (setq pass (read-passwd "Password: ")) (chess-ics "freechess.org" 5000 username pass) ) ) (provide '...


1

What happens here is that you try to use a non-number when subtracting numbers, more specifically nil. Your code assumes buffer-file-name always returns a file name pointing to a file you can query the attributes from which is wrong, buffers don't have to point at files and can return nil in this case. Your hook must handle this case to not error out ...


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