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
0 answers
28 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,...
user avatar
3 votes
2 answers
47 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 ...
user avatar
  • 257
0 votes
1 answer
67 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 ...
user avatar
  • 103
1 vote
1 answer
52 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 (...
user avatar
4 votes
1 answer
105 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 ...
user avatar
  • 10.9k
1 vote
2 answers
109 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 ...
user avatar
3 votes
1 answer
136 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 ...
user avatar
  • 1,754
1 vote
1 answer
58 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-...
user avatar
  • 1,754
2 votes
1 answer
66 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 ...
user avatar
  • 581
0 votes
1 answer
56 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) ...
user avatar
2 votes
2 answers
148 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 ...
user avatar
  • 1,429
1 vote
1 answer
23 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 ...
user avatar
1 vote
0 answers
41 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: (...
user avatar
1 vote
2 answers
117 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 ...
user avatar
  • 227
1 vote
1 answer
76 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 ...
user avatar
1 vote
2 answers
114 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 ...
user avatar
0 votes
1 answer
229 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 ...
user avatar
  • 123
1 vote
1 answer
170 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 ...
user avatar
  • 123
3 votes
1 answer
135 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)) ;;...
user avatar
1 vote
0 answers
42 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 ...
user avatar
1 vote
1 answer
73 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). ...
user avatar
1 vote
1 answer
421 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 ...
user avatar
  • 3,825
0 votes
1 answer
30 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 ...
user avatar
1 vote
1 answer
64 views

Use Elisp macro instead of two function arguments?

Is the following sort of shortcut macro possible to implement? (defmacro region-end-beg () "Replacement for '(region-end) (region-begin)' in source code" (...)) So that (buffer-substring (region-...
user avatar
0 votes
1 answer
318 views

Is there a clean way to splice some args to an existing Lisp macro?

I have the following non-working elisp code: (use-package eglot ;; For debug/testing, load local copy of eglot if it exists ,@(if (file-directory-p (concat user-emacs-directory "/...
user avatar
  • 406
1 vote
1 answer
148 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 ...
user avatar
  • 187
1 vote
0 answers
38 views

does this routine exist? until (while until true)

I found myself needing this control loop and was very surprised not to find it in the standard library. Does it exist by some other name? (defmacro until (form &optional guard) "Runs `while' on ...
user avatar
  • 1,690
1 vote
1 answer
121 views

How do you remove compiler warnings for macro usage when running macro with 'with-eval-after-load'?

In a package I am working on I want to define a flycheck checker only if the user has flycheck installed already. To do this, I am using the macro (with-eval-after-load "flycheck" ...) as follows: (...
user avatar
4 votes
3 answers
254 views

How do I get byte-compilation warnings about undefined variables?

The following code works fine with M-x eval-buffer, and byte-compiles without any warnings. (eval-when-compile (defconst demo-one 1)) (defvar demo-some-var `(foo bar ,demo-one)) However, if I ...
user avatar
6 votes
0 answers
106 views

Evaluate some arguments to macro immediately

I am trying to call a macro (defined somewhere else; I can't change it), but I want to evaluate some of the arguments before the macro is expanded. Is this possible? For example: (defmacro foo (&...
user avatar
  • 271
1 vote
1 answer
38 views

what is the macro "section"?

Exploring far-out init files, I found this here: (defmacro section (name &rest a) `(progn ,@a)) (put 'section 'lisp-indent-function 'defun) then it's used AFAICT as a sort of wrapper, not sure ...
user avatar
  • 2,717
2 votes
1 answer
263 views

On using Elisp macros for partial application

I want to improve my Elisp, but despite reading about the differences between functions and macros (including discussions here in Emacs SE and on Reddit), I still find examples where the distinction ...
user avatar
  • 3,313
7 votes
1 answer
451 views

Setf weird expansion

Trying to understand what setf can do, I called (macroexpand '(setf (aref vec i) val)) ⇒ (let* ((v vec) (v i)) (aset v v val)) This seems obviously wrong. However I couldn't create an actual ...
user avatar
  • 1,085
0 votes
1 answer
105 views

How safe is "with-silent-modifications" to mask (hide) strings before and after buffer modification?

In my LaTeX code I want to replace one-line eqnarray environments with the equation environment. To do that I need to "count" the new-line (\\) strings in a region and I also need to ignore some new-...
user avatar
0 votes
1 answer
45 views

Byte-compilation and "uninterned" function names

Not quite a duplicate: this appears to be very similar to the situation described in an existing question. But my question is not "why does it happen", but rather "is there any way to keep using ...
user avatar
0 votes
1 answer
108 views

Forcing lexical-scope in the middle of dynamic-scope

Today I ran head first into lexical vs dynamic scope in Emacs Lisp. Gist of it is I have a macro defined in lexical scope that expands into a `defun'. Let's say something simple like this: ;; -*- ...
user avatar
  • 335
3 votes
3 answers
637 views

How to avoid use of `lexical-let`

I have a macro that intends to creates a closure: ; -*- lexical-binding: t -*- (defmacro repro () (let ((kmap-sym (gensym "kmap-"))) `(let ((,kmap-sym (make-sparse-keymap))) (define-key ,...
user avatar
  • 813
0 votes
1 answer
52 views

with-query-replace-color: macro or function?

I'm trying to create my first macro. I often change the background color of the query-replace matched string in my script. I usually do: (let* ((face 'query-replace) (bg (face-background face))...
user avatar
0 votes
1 answer
245 views

elips macro that return's the current file's filename

I want to define a action that says open <config file> within the actual config file itself. However, this config file may be in a different location on different computers. How can I get the ...
user avatar
  • 567
1 vote
1 answer
73 views

macro takes multiple pairs of arguments

I found this snippet of code from oremacs (defmacro csetq (variable value) `(funcall (or (get ',variable 'custom-set) 'set-default) ',variable ,value)) I would like to expand this macro so that is ...
user avatar
0 votes
1 answer
55 views

Creating a macro/elisp function for HASH using calc

In my workflow, I need to do binary operations quite often. I am wondering if there is a clean way of creating macro/elisp-function to return value of following HASH function. Input 16bit hex number: ...
user avatar
0 votes
1 answer
337 views

Why doesn't this `declare-function` form suppress 'Warning: the function is not known to be defined.'?

Here's my declare-function form in file A: (declare-function 'my-function "file-B" (my-func-arg &optional args)) and here's the definition of my-function in file B: ;;;###autoload (defun my-...
user avatar
  • 6,661
1 vote
1 answer
189 views

Unconsumed commas in double-backquoted lists

I'm writing a macro which calls a helper function. I need to return a list like this: `(s -- (cons 'commentary s)) I have tried many combinations, but I can't seem to get what I need without an ...
user avatar
1 vote
0 answers
66 views

Macro expansion inside quail-define-rules

Why can't I expand a macro inside (quail-define-rules)? (defmacro quail-test () `("c" ["success!"])) (quail-define-package "experiment" "Experiment" "X" t ...
user avatar
  • 2,904
0 votes
1 answer
169 views

AUCTeX set LaTeX-default-tabular-environment back to "tabular"

When I use the package tabularx in a tex file, if I insert table environment, the inner environment will be given as tabularx instead of tabular when the package tabularx is not used. Normally this ...
user avatar
  • 125
5 votes
2 answers
181 views

How can I define new places for setf?

Q: how do I teach setf about new places it can set? In common lisp, one uses the macro defsetf to tell setf about new places it can set. In elisp, that macro exists in in the cl library, but, as far ...
user avatar
  • 31k
2 votes
1 answer
164 views

improve AUCTeX macros like `\frac` and `\stackrel`

While using AUCTeX, the macro \frac is quite intelligent that when I just type C-c C-m frac RET, it will put the cursor in the first pair of braces, as \frac{(cursor here)}{} However, when I have ...
user avatar
  • 125
7 votes
1 answer
372 views

"cl-flet" macro not allowing for recursive functions

I am used to the deprecated elisp macro flet and I was told to change to cl-flet. However upon making this change some of my elisp programs stopped working and I realized the reason is that, unlike ...
user avatar
  • 597
1 vote
1 answer
98 views

Define negating pcase patterns

I am interested in extending pcase with patterns which: Complement an existing pattern, e.g. (not (pred string-empty-p)). Such a pattern would match where the non-negated form does not match and vice ...
user avatar
  • 11.3k
2 votes
3 answers
459 views

Eval some arguments in a macro

I want to make a macro that takes a form and returns a string with some parts of the form evaluated. The form can have any number of elements (e.g. additional options for the title font, color, size,.....
user avatar
  • 10.9k