Questions tagged [defun]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
1 vote
2 answers
48 views

How to view defuns in file order?

I think I'm missing something about imenu. I'm interested in viewing the top-level definitions in file order. When I run imenu I get a prompt in the minibuffer that says Index item:. If I then hit TAB ...
Eric Auld's user avatar
0 votes
1 answer
56 views

How to use evil-next-line in a function when being in a visually selected mode?

Good day, I wanted to use evil to move a few lines down or up at once, less than with evil-scroll-down/up, but more than one line. I made a brief function: defun go-up-few-lines () "Insert a ...
Rareform's user avatar
  • 311
1 vote
1 answer
55 views

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 ...
Lorenzo Marcantonio's user avatar
0 votes
1 answer
82 views

Swap two pieces of text

I often find myself swapping pieces of text. How can I make a command to replace the current text with the latest kill, then put the text I just replaced at the position of the previous kill? Or is ...
Zaz's user avatar
  • 123
3 votes
2 answers
223 views

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 ...
shynur's user avatar
  • 4,638
8 votes
3 answers
410 views

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) ...
shynur's user avatar
  • 4,638
2 votes
2 answers
134 views

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 ...
shynur's user avatar
  • 4,638
0 votes
1 answer
27 views

How to create an evil-delete-previous-line command?

I am trying to create a command which deletes preceding whole lines, just like evil-delete-whole-line, but backwards. So far I have the following (evil-define-operator delete-previous-line (beg end ...
Rareform's user avatar
  • 311
1 vote
1 answer
149 views

How can I create an alias for a function name?

Does Elisp allow the equivalent of an alias for a function name?
Dilna's user avatar
  • 1
0 votes
1 answer
51 views

How to access the last-yanked text programmatically?

How can I use the last thing yanked in a command I'm writing? For example, if the last text I yanked was "test" I want to use this string somehow in my command. (I use avy and evil.)
Rareform's user avatar
  • 311
0 votes
1 answer
353 views

Use of cl-defun

What is a use case for cl-defun, where defun wouldn't work or would be hard to work with? I am using it to count the number of opening and closing bracemarks. (cl-defun rk-bracemark-count-from-...
Dilna's user avatar
  • 1
3 votes
2 answers
212 views

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 ...
BuddhiLW's user avatar
  • 267
1 vote
1 answer
242 views

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) (...
W.M.'s user avatar
  • 113
1 vote
2 answers
519 views

copy string at point func

I'm trying to build a simple proc that copies the text on which the point is set and returns the point to where it was, but can't make it work properly. Before you say "thing-at-point" - I ...
user2141046's user avatar
0 votes
1 answer
61 views

How can I save a function defintion

I want to extend a standard Emacs function (package--save-selected-packages), but to cover my tracks I want to save the old defintion under a new name and then redintion teh standard function. I tried ...
halloleo's user avatar
  • 1,311
3 votes
3 answers
302 views

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-...
american-ninja-warrior's user avatar
1 vote
0 answers
31 views

How to define interactive function that will not auto-complete?

If I do this: (defun foo-bar-baz () (interactive) (message "foo-bar-baz")) And then M-x foo-baz RET (note no "bar") then completion matches foo-bar-baz and it executes. Is ...
John H.'s user avatar
  • 123
1 vote
0 answers
271 views

Defining a function that takes keyword arguments and a variable number of positional ones

I want to define a function that takes both keyword arguments (as in &key) and a variable number of positional ones (&rest). They should be independent of each other, the keyword list should ...
Nova's user avatar
  • 1,059
0 votes
1 answer
521 views

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

I learned that a lambda and let works as if they already include a progn in their definition. That is (let ((x 1) (y 2) (z 3)) A B C) works as if it had a progn after binding the variables: (let ((x 1)...
hyiltiz's user avatar
  • 115
0 votes
1 answer
2k views

Symbol's value as variable is void [duplicate]

first, defined this interactive function: (defun system-is-windows () (interactive) (string-equal system-type "windows-nt")) then call this function in one setting.el file ; don't show the ...
YiC's user avatar
  • 105
1 vote
1 answer
262 views

Making a dynamic, interactive function

I have make a macro that creates dynamic functions: (defmacro tjb-make-register-macro (func-name out-string) (list 'defun func-name () '(interactive) (list 'insert out-string))) ;; This call ...
tjb's user avatar
  • 197
1 vote
1 answer
85 views

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

I want to write a command which uses a prefix argument and reads some strings. I know both parts individually: (defun xtest(arg) (interactive "P") ... reads the prefix arg. (defun xtest(t1 ...
halloleo's user avatar
  • 1,311
1 vote
1 answer
104 views

How to create a function with a computed name?

Let's suppose I have a string "foo" and want to create a function that would be an equivalent of (defun foo (...) ...). Is there a nice way to do it? I played with make-symbol and fset, but could ...
9000's user avatar
  • 517
5 votes
2 answers
1k views

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-...
Alaneuler's user avatar
  • 277
0 votes
1 answer
181 views

defun removes function in spacemacs config

I re-defined the function org-archive-subtree to call another function as described in the function below. I did this in spacemacs/user-config, now I can't call org-archive-subtree and C-h v (describe ...
Simon's user avatar
  • 231
4 votes
1 answer
245 views

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 ...)...
antonio's user avatar
  • 1,792
0 votes
1 answer
185 views

Trying to copy everything between double quotes, and then make a list of everything that was copied

So I have a defun that I made that is supposed to copy every thing that is between double quotes and then make a list of everything that was copied. I don't know what the problem is, but it doesn't ...
CigEmacs's user avatar
0 votes
2 answers
146 views

How to create a function which calls edit-indirect-region and then changes the mode in the buffer?

I'm beginner user of Emacs and I found trouble while trying to bind a custom function to a key. I successfully bind some keys to edit-indirect-region using following code: (global-set-key (kbd "C-c ...
FieryCod's user avatar
  • 167
3 votes
2 answers
67 views

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 ...
Vivodo's user avatar
  • 133
3 votes
0 answers
1k views

Interactive lambda functions [closed]

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 :...
Maik Klein's user avatar
0 votes
1 answer
64 views

How do I update this defun to select region

I wrote this function which takes a string and writes out self.var for each entry in the string. This helps me create python classes faster. (defun elf-python-self (str) "Parse the arguemnts in STR ...
eflanigan00's user avatar
1 vote
1 answer
220 views

How to evaluate a defun with square brackets in column 0 of its docstring?

I have problems running eval-defun for a defun when there is a square bracket [ in column 0 of its doc string. For example: (defun test () "Test function. [Some text]" (message "Hello")) When I ...
Håkon Hægland's user avatar
0 votes
2 answers
209 views

Play chess, login on a FICS server

I wish to play chess with chess.el on a FICS server using M-x chess-ics but using that logs me as a guest player. How can I login with my registered user/password? EDIT I've come with a way to login....
Javier Novoa C.'s user avatar
0 votes
1 answer
205 views

Adding to kill-buffer-hook results in "setq: Wrong type argument"

Subtract a buffer's (the file's) size from a variable before killing it. Emacs 26. (defvar cumulative-open-bytes '0 ) (defun buffer-settings-calc-bytes () (interactive) (let ((current-open-file-...
Austin's user avatar
  • 3
16 votes
3 answers
757 views

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, ...
Rose Kunkel's user avatar
6 votes
1 answer
2k views

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) ...
xuchunyang's user avatar
  • 14.4k
0 votes
1 answer
64 views

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

Suppose I have defined the following macro: (defmacro mymacro (a) "" (eval a)) If I then use eval-last-sexp (i.e. C-x C-e) to evaluate the following, (defun somefcn (b) "" (mymacro b)) I get the ...
Jonathan Ganc's user avatar
1 vote
0 answers
255 views

Sublime Text like Ctrl-Delete/Backspace

Is there any easy way (or maybe a package) which provides Ctrl-Del/Backspace behavior like in Sublime? The default rule of sublime is delete-to-next-word-boundary. I did not find similar options. ...
Daniel Hitzel's user avatar
2 votes
0 answers
193 views

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 ...
Tianxiang Xiong's user avatar