Hot answers tagged

9

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" ...


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/...


6

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


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

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. If two files define functions or variables with the same name then you'll get a warning when the second one is loaded. You can find out where a function was ...


4

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

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 (...


3

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


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

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 with a set of arguments may have a similar effect to evaluating the lambda's body with the appropriate let bindings in place, but their corresponding semantics ...


2

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-variable system-is-windows) error message. Instead, you need to write: (if (system-is-windows) (scroll-bar-mode 0)) The parentheses tell Emacs that you are ...


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


2

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 ‘thingatpt.el’. (thing-at-point THING &optional NO-PROPERTIES) Probably introduced at or before Emacs version 20. Return the THING at point. THING should be a ...


2

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-mark-command), then copy it M-w (kill-ring-save). (defun my-string-at-point () "Save the space-delimited string at point to the kill ring." (...


1

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 (perhaps with fset). I tried fset but that creates rather something like an alias from a new name to the old name, but it does not save the old definition... ...


1

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 namespaces. It may sound like a recipe for chaos, but in practice it works very well. What you are doing when you name your function s-join is (effectively) ...


1

In Elisp, all functions names normaly (but not necessarily) reside as an entry in a global environment (obarray). In Elisp functions can be redefined. Which means the function definition which has been evaled (or loaded) last will be the only one, which is known and used. This way you can change functions in your init file, you just need to be sure your ...


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