Skip to main content

Questions tagged [elisp-macros]

Emacs Lisp macros enable you to define new control constructs and other language features. A macro is defined much like a function, but instead of telling how to compute a value, it tells how to compute another Lisp expression, which in turn is evaluated to compute the value. We call this expression the "expansion" of the macro call. The macro call is first expanded and then evaluated, returning the value of that evaluation.

Filter by
Sorted by
Tagged with
0 votes
1 answer
38 views

Using macros to define functions through fixing another function's argument

I'm writing a lot of LaTeX-files and to improve the process I'm trying to give each environment a background tinge. So I wrote a list of tuples of environment types and faces, daselt-latex-colored-env-...
Alexander Praehauser's user avatar
0 votes
1 answer
33 views

Macro works with direct argument, but not when argument is set using "let"

Take this macro definition: (defmacro useless (digit) (let ((str-digit (number-to-string digit))) `(message ,str-digit))) It works when I call it directly, with a number as argument: (useless 2)...
cksk's user avatar
  • 13
0 votes
1 answer
38 views

How to correctly define functions with defun inside a macro?

I have the following code (built following my previus question https://emacs.stackexchange.com/a/80426/15606): (defvar my-list '(("my-function-one" "Message 1") ("my-...
Gabriele's user avatar
  • 1,680
1 vote
2 answers
83 views

Getting the source file line number of the currently executing init file expression (formerly: Malformed macro)

I wrote this macro for my .emacs file to trace its progress during loading: (defmacro checkpoint () `(message "Reached line %i" (1+ `,(current-line)))) So I placed (checkpoint) ...
William R. Greene -- 'Bill''s user avatar
0 votes
2 answers
44 views

custom defconst

I'm trying to build a function that is a wrapper around defconst so that creation of a constant automatically gets logged in my log file. However, I can't figure out how to pass a string in a variable ...
simon.watson.sjw's user avatar
0 votes
1 answer
74 views

Generate keyword symbols

I like to convert an alist within a macro call to a list of keyword symbols and corresponding values. E.g. given a macro make-test and an alist `((a . 1)(b . "cool")) I want to obtain a ...
dmw64's user avatar
  • 207
0 votes
2 answers
98 views

Defmacro: Create functions from a list o functions

I have this code: (defmacro with-ignore-mouse-events (&rest body) "Macro to ignore mouse events before evaluating BODY." `(progn (when (input-pending-p) (let ((event (...
Gabriele's user avatar
  • 1,680
0 votes
1 answer
54 views

Macros expanding to addhook

I have the following configuration lines in my init.el file regarding a specific mode: (add-hook 'pdf-view-mode-hook (lambda () (local-set-key (kbd "SPC h") #'evil-window-left))) (add-hook '...
Peter Petigru's user avatar
0 votes
1 answer
30 views

List isn't considered a list within macro

(setq x 1) (defmacro dowhile (body condition) (cl-flet ((seq_eval (mylist) (cl-dolist (elt mylist result) (setq result (eval elt))))) (seq_eval body) (while condition (seq_eval body)))) (...
shintuku's user avatar
  • 121
0 votes
1 answer
99 views

Is using if-let discouraged in emacs-lisp?

I wrote a piece of code using if-let. Later, when I tried to use my function, I got: Symbol’s function definition is void: if-let So I found that if-let is defined in subr-x.el and I need to use (...
Gabriele's user avatar
  • 1,680
1 vote
0 answers
69 views

Setf not expanding

I'm trying to modify a member of lsp--client after registration. So I have something like the following in use-package. (message "%s %s" (symbol-function 'lsp--client-new-connection) (...
Todd's user avatar
  • 211
3 votes
1 answer
664 views

What is the difference between `with-eval-after-load` and `eval-after-load`?

I wonder which one should I use. Here are the definitions from Emacs: with-eval-after-load is a Lisp macro in ‘subr.el’. (with-eval-after-load FILE &rest BODY) Execute BODY after FILE is loaded. ...
emacs_supporter's user avatar
2 votes
1 answer
78 views

Macro breaks only in `eval-expression` (`M-:`)

I have a macro which unpacks a list into variables, as if by setq: (unpack (a b c) '(1 2 3)) is roughly comparable to (setq a 1) (setq b 2) (setq c 3) The way I did that initially was by having the ...
mtraceur's user avatar
  • 576
0 votes
2 answers
64 views

difference between symbol-value and eval

(defmacro ek/bind-evil-keys-map (state keymap key-fn-list) (let* ((key-fns (symbol-value key-fn-list)) (body (mapcar (lambda (pair) `(evil-define-key ,state ,...
eugene's user avatar
  • 519
0 votes
2 answers
268 views

How does a Lisp macro behave?

Have read that macro provide a way to perform transformations before the code is actually evaluated. When the macro is called, its arguments are passed to the macro as unevaluated forms. The macro ...
Dilna's user avatar
  • 1
1 vote
1 answer
144 views

(apply #'and '(t t t)) is throwing an error. Why? [closed]

(apply #'and '(t t t)) gives me Debugger entered--Lisp error: (invalid-function #<subr and>) and(t t t) apply(and (t t t)) while (and t t t) gives me t. What am I missing? This is with ...
user avatar
1 vote
1 answer
109 views

Void variable when defining macro containing defvar in progn

I don't understand why I get (void-variable test-map) when trying to run: (defun something--key-map (name) (make-symbol (concat (symbol-name name) "-map"))) (defmacro defsomething (name) ...
tino415's user avatar
  • 13
0 votes
1 answer
96 views

How to use the same variable name with let when calling defmacro?

This is only an example, I understand this can easily be achieved with a function. Don't Work This is what I've come up with based on the elisp manual and a couple of answers/articles that I have ...
Rick's user avatar
  • 25
1 vote
1 answer
52 views

Can't setq a variable inside a macro

This is my first foray into emacs macros, so likely silly mistakes ahead: I'm trying to mimic the definition of pop (see the manual here), but I want to remove an element of alist indexed by key, ...
Rodney Price's user avatar
0 votes
1 answer
101 views

How do I use a while loop with a macro?

This works when I remove the loop but with the loop it does not work. (defmacro my/def-key (state map) (let ((x '( (kbd "<leader>z z") (lambda nil (interactive)(message ...
ritchie's user avatar
  • 169
4 votes
1 answer
476 views

Bug in defmacro?

I think I found a bug in defmacro so I'd like to ask for confirmation before I make a fool of myself reporting something silly! Here is how to reproduce it: Define the following macro which simply ...
Ruy's user avatar
  • 849
1 vote
1 answer
55 views

Is it possible to eval code as a transient group description?

I want to customize the title of a transient group based on the value of some variable, my--current-section. The following doesn't work (transient-define-prefix my--prefix () "Sample docstring&...
Alessandro Bertulli's user avatar
4 votes
3 answers
609 views

Does emacs have compiled/interpreted mode?

So I define a macro (defmacro macro-print (str) (print "hi") and then run (let ((i 0)) (while (< i 3) (macro-print i) (setq i (+ 1 i))) according to the Elisp manual, the ...
Isabella's user avatar
  • 143
0 votes
2 answers
330 views

How to fix byte compiler warnings for "Unused lexical variable" in a macro?

I have a macro and a function which uses it: (defmacro oauth2-auto--query-case (&rest cases) "Handle HTTP queries based on the keys present in ‘query-alist’. ‘query-alist’ is a free ...
telotortium's user avatar
0 votes
2 answers
57 views

Dynamic Keybind Without Use of Macro

I've attempted to make a function which binds the leader keys o f to switch to the current buffer: (defmacro bind-current-file () (list 'spacemacs/set-leader-keys "of" (...
George's user avatar
  • 939
1 vote
0 answers
88 views

Org-mode expands org-special-block-extras incorrectly

I've been using org-special-block-extras to insert special blocks in org-mode which I then export to PDF through LaTeX, things such as margin notes or colored blocks. It used to work until recently (...
Nathan Furnal's user avatar
2 votes
1 answer
199 views

How can I automate rescheduling overdue daily tasks?

I have a number of "daily" tasks which repeat daily. If I forget to mark each task as DONE, it is removed from the timeline, and appears as an overdue item at the top of the agenda. For ...
Ishmael7's user avatar
0 votes
0 answers
47 views

How to convert greek text to unicode using matching input method?

Good evening, I have a large TeX-file with Greek inclusions in traditional babel way: \textgreek{sunj'hkh|}. I would like to convert it to unicode for further work. Emacs has greek-babel input method,...
Vadim Radionov's user avatar
5 votes
2 answers
399 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
  • 307
0 votes
1 answer
207 views

How to define multiple key bindings more concisely?

I'm trying to write a helper macro to conveniently define keybindings in my configuration file. The idea is to replace the following configuration entry: (use-package org-roam :config (define-key ...
jubnzv's user avatar
  • 103
1 vote
1 answer
138 views

Why are `defvars` in my macro ignored?

I want to use a macro to define variables. However, evaluating the macro does not define these vars. I seem to be missing something, but I can't find it. Here's the code: (defmacro delve--build-cmp (...
Public Image Ltd.'s user avatar
4 votes
1 answer
134 views

How do you find all macro calls in an elisp file?

I am trying to make sure a package that is installed from Melpa (org-ref) byte compiles correctly, and a typical issue is related to the use of macros. For example, if I use org-with-wide-buffer which ...
John Kitchin's user avatar
1 vote
2 answers
230 views

Count number of lines between two consecutive matches

I have files such as these: 0000 0030 + Something + Another thing + One more thing 0200 + Something else + And one more thing 0230 In the above, the numbers represent time in HHMM format. I want to ...
Inspired_Blue's user avatar
4 votes
1 answer
335 views

eval-when-compile and eval-and-compile identical definitions?

How can eval-when-compile and eval-and-compile have different behavior if their definitions are identical? From byte-run.el: (defmacro eval-when-compile (&rest body) ;; ... documentation string ...
ivan's user avatar
  • 1,948
1 vote
1 answer
159 views

special form or macro? eval-when-compile, eval-and-compile

Is eval-when-compile a special form (primitive in C), or a macro? According to the elisp manual, C-h S eval-when-compile RET, Special Form: eval-when-compile body... But according to C-h f eval-when-...
ivan's user avatar
  • 1,948
2 votes
1 answer
177 views

Can a defmacro generate a string with the name of the .el file where it is expanded?

I'd like to create a Emacs Lisp macro that is able to generate a string literal that contains the name of the .el file into which the macro is expanded. Is this possible? Background: I am writing a ...
PRouleau's user avatar
  • 831
0 votes
1 answer
67 views

Issue with macro and `buffer-name`

I want to write a macro that makes a diffpdf before and after some changes in my LaTeX file (that would be compiled twice by pdflatex). I tried: (defmacro with-diffpdf-after-changes (&rest body) ...
Gabriele's user avatar
  • 1,680
3 votes
2 answers
530 views

Call apply with a macro

Is there a way to expand a macro using a list of arguments? I tried using apply but then I get an error that the "function" my/x-becomes-nil is invalid. (defmacro my/x-becomes-nil (variable ...
Tohiko's user avatar
  • 1,659
1 vote
1 answer
45 views

How to investigate defmacro* (with asterisk) error on an old emacs package

I'm trying to load the defhook package without success. The code defines a macro using defmacro* (with the asterisk at the end), and I can't find any info about it. (The fact that the '*' is ignored ...
CuriousObserver's user avatar
1 vote
0 answers
59 views

Navigating to a `defconst` variable defined by a Lisp macro

My goal is to define a const variable within a non-trivial macro that can be navigated to after calling describe-variable. The design goal of my macro works just fine if I define it like so: (...
John DeBord's user avatar
2 votes
2 answers
247 views

Evaluate docstring in macros

Macros like defun (or defmacro itself) have an optional docstring argument, but since they are macros and not functions, docstring isn't evaluated, but is taken as-is. I need the docstring to be ...
nomad's user avatar
  • 257
2 votes
1 answer
241 views

How to properly generate a concat'd function name with defmacro?

I have a macro like this: (defmacro my-fun-generator (x) `(defun ,(intern (concat "fun-" x)) () ,(concat "Print " x) (interactive) (print ,x))) This works (found ...
Overdr0ne's user avatar
1 vote
2 answers
193 views

Macro definition

I'm in the process of "modularizing" my config file and ran into a problem involving elisp macros. Because of the way in which I'm defining my configuration modules, I ended up with a bunch ...
juanerasmoe's user avatar
0 votes
1 answer
639 views

compilation sentinel: symbol's value as variable is void for quote, >, <, =, etc

I'm working on a script that can compile multiple directories and then provide the first error to a file. Currently, I am tackling the issue of running make in multiple directories portion. I am ...
LeanMan's user avatar
  • 123
1 vote
1 answer
533 views

How to code a command to `write-region` to a particular file?

I have a system that I am not allowed permissions to install software but need to copy text from the remote server to the host computer's system clipboard. Because I can't install xclip or xsel and I ...
LeanMan's user avatar
  • 123
3 votes
1 answer
227 views

Inconsistent behavior when calling #'funcall with a macro

I would have thought that both of the funcall's the follow would have yielded the same result, but they don't and I'm trying to understand why. (defmacro test/z () "z") (funcall (function test/z)) ;;...
theia-jane's user avatar
1 vote
0 answers
47 views

Autodocument function for C Code

Suppose i have this line of code presenting a C function prototype: int sum_Func(int a, int *b) I want an Emacs lisp function that adds the function description (initialization) according to its ...
Ghost_tn's user avatar
1 vote
1 answer
110 views

why is switch-to-buffer effect not visible immediately when called in a list with other functions?

I have a key bound to some program which prepares a buffer, switches to it via switch-to-buffer and afterwards calls some function which modifies the buffer (and can take a few seconds to execute). ...
F. Carbon's user avatar
1 vote
1 answer
1k views

Working with printed macro expansions

I am trying to write a macro. I have read the manual and learned about macroexpand. However, when I use it, I find the expansion difficult to read and work with. How can I expand a macro in a ...
Lorem Ipsum's user avatar
  • 4,547
0 votes
1 answer
44 views

How to use a macro append macro argument to a list?

I want use a macro to generate a package list. (defvar xx-packages '()) (genpack evil) (genpack all-the-icons-ivy-rich :recipe (:host github :repo "seagle0128/all-the-icons-ivy-rich")) this ...
xiaodong huan's user avatar