Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

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.

0
votes
1answer
27 views

how to generate dynamic json data with emacs (macros)?

I was in a meeting yesterday where I was presenting a program that referenced a certain json file with data like this: [ { id: 1, text: '...', ... }, { id: 2, text: '...', ... } ] In ...
0
votes
1answer
28 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 ...
0
votes
1answer
39 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: ;; -*- ...
3
votes
3answers
216 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 ,...
0
votes
1answer
40 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))...
0
votes
1answer
67 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 ...
1
vote
1answer
33 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 ...
0
votes
1answer
30 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: ...
0
votes
1answer
62 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-...
1
vote
1answer
77 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 ...
1
vote
0answers
32 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 ...
0
votes
1answer
96 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 ...
4
votes
2answers
77 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 ...
2
votes
1answer
94 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 ...
7
votes
1answer
122 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 ...
1
vote
1answer
56 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 ...
2
votes
3answers
197 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,.....
0
votes
1answer
58 views

prin1 abbreviates form with “…” despite expression-print-level?

I have the following function to produce a form that can be "printed" to a ~/.gnus file to configure gnus to use a certain stmp and imap server for an account: (defun gnus-imap-smtp-form (email smtp-...
8
votes
1answer
240 views

eval-when-compile: defsubst vs defmacro vs define-inline

I defined some simple functions in init.el, for example my-cache-file: (defconst my-cache-directory (expand-file-name ".cache" user-emacs-directory)) ; ~/.emacs/.cache (defun my-cache-file (x) (...
-1
votes
1answer
44 views

Writing a macro that generates a (cons atom (cons atom … ))) list

I am relatively new to elisp, and am currently in need of a macro that will take in a list, and return, not a cons-list, but: given (1 2 3 4) returns (cons 1 (cons 2 (cons 3 4))) Any help would be ...
2
votes
2answers
149 views

Why does my function-generating macro only work when I evaluate its expansion?

I'm trying to write a macro that will define a function exactly once where I don't really care what the name of the function is. My thought was to generate a symbol, use defun with that, and then put ...
5
votes
2answers
63 views

How can I record where a function is defined if it's done indirectly?

I've got a few functions in api-wrap.el that I'll refer to as 'macro code', here simplified to clarify the question: function (generate-form prefix kind &rest args) returns an unevaluated form ...
1
vote
1answer
112 views

How can I syntax-highlight a function like a macro?

I've got a function that behaves more like a macro in that it does compile-time environment definitions. For various reasons (at this point, one of those reasons is 'because it is easier to grok in ...
1
vote
1answer
179 views

Using local variables in cl-loop

This is my first time trying to write my own elisp code, so bear with me. I have the following cl-loop: data contains the following: ((refs . [((file . "api/user.js") (start . 266) (end . 269)) ((...
1
vote
1answer
336 views

Passing a symbol as an argument to a function

I'm trying to understand why this doesn't work, and how to make it work: (defvar test/var-1 nil) (defvar test/var-2 nil) (defun make-setter (var-to-set) (lambda (val-to-set) (setq var-to-set ...
1
vote
0answers
411 views

Understanding Emacs macro and org mode tikz behavior

I found this discussion, and the last response gives this code: #+TITLE: Test #+AUTHOR: Blah #+LATEX_CLASS: article #+LATEX_CLASS_OPTIONS: [american] # # Setup tikz package for both LaTeX and HTML ...
3
votes
2answers
149 views

Deep eager macroexpansion

This works: (require 'clojure-mode) (when (member 'clojure-mode my-packages) (define-clojure-indent (-> 1) (->> 1))) This gives the error Wrong type argument: listp, 1: (when (...
4
votes
2answers
169 views

One-time advice

I've been trying to a macro to put one-time advice on a function. What I mean by one-time advice is advice which removes itself after it is called – so that when you add the advice and run the ...
5
votes
2answers
170 views

Finding the source of a function after macro expansion?

(I'm a rookie and I use Spacemacs) I'm trying to find the source of a certain function*. I can find the .el file in which it's defined by using find-function, however the defun is created ...
1
vote
0answers
63 views

How do I teach edebug to only evaluate some macro arguments?

Currently, edebug doesn't understand the ->> macro in dash.el. (defun foo () (->> (+ 1 1) (+ 2) (* 3) (format "%s"))) C-u M-x edebug-eval-defun followed by M-: (...
1
vote
1answer
157 views

Weird behavior of defmacro and intern - symbolp error

I have the following macro (defmacro test-macro (arg) `(defun (intern (concat "test-func-called" ,arg)) () (message "I do nothing"))) When I call to this macro (test-macro "test") It gives ...
0
votes
0answers
48 views

with-accessors for all accessors?

common lisp has a with-accessors macro which is pretty useful but it is still prone to lots of code duplication. Is there a convenient let/binding macro that makes all accessors from a defclass ...
12
votes
3answers
746 views

When to use macro or not to use [closed]

When should I use macro in my program or not to? This question is inspired by an informative answer by @tarsius. I believe many have great experiences to share others. I hope this question become ...
3
votes
1answer
66 views

FORM vs BODY in documentation?

Recently I was trapped by void-variable error on eval-after-load. That is not first time I forget to quote args like: (eval-after-load 'sql '(progn ...)) because eval-after-load docs say: ...
3
votes
0answers
202 views

Byte-compiling a file loads macros with `(eval-and-compile)` but not `(eval-when-compile)`

I created a buffer, test1.el: (eval-when-compile (defmacro dummy-macro (_) nil) (dummy-macro s)) Trying to compile this file, I get the following errors: In toplevel form: test1.el:3:16:Warning:...
7
votes
1answer
171 views

Understanding uninterned symbols and macro expansion?

I want to demonstrate my lack of knowledge with an example. Using the following two macro defintions, (defmacro for (var from init to final do &rest body) "Execute a simple for loop: (for i ...
2
votes
1answer
311 views

Evaluating Macro Arguments in Expansion

In Section 13.5.4 of the Elisp reference, we are shown a problem whereby a macro definition evaluates one of its argument expressions. (defmacro foo (a) (list 'setq (eval a) t)) (setq x 'b) (foo x) ...
2
votes
1answer
92 views

Dynamic generation of varlist in a let form

Let's say I have a let form and I want to generate the varlist dynamically from another function. Something like this: (let ((x 1) (y 2) (the-dyn-gen)) body... ) I have some local ...
3
votes
1answer
118 views

Calling a macro from a macro with a variable number of arguments

Say there is a defmacro defined like this: (defmacro do-something (foo &optional bar) (...)) And then there is another one that calls it: (defmacro something-else (wat baz &optional bar) ...
6
votes
1answer
527 views

Make flycheck's “reference to free variable” work with macros

The use-package is a useful and popular macro for installing packages, but flycheck always complains about it since it hides variable definitions. For example, if I attempt (use-package org), Flycheck ...
0
votes
1answer
305 views

Fix for “Invalid function:” error when external function become a macro

For: (defun my-w3m-view-url () (interactive) (browse-url (w3m-anchor))) (eval-after-load 'w3m (define-key w3m-minor-mode-map (kbd "RET") #'my-w3m-view-url)) when byte-compiled I get error on ...
5
votes
3answers
457 views

How to disable eager load-time macro expansion in emacs 24.3?

Is it possible to disable eager load-time macro expansion in emacs 24.3.1? For debugging purposes, I would like to expand macros lazily on each invocation at run-time. A working non-standard method ...
0
votes
1answer
434 views

Forgot to expand macro

I just installed emacs24 on my Ubuntu 14.04 LTS OS as follows: $ sudo apt-get install emacs24 When I open emacs, I get the following errors (duplicate messages removed): eieio/eieio.elc:Error: ...
6
votes
1answer
109 views

Function to redistribute list into columns

I have a list in an org document that is rather long - alpha a - beta b - gamma - delta d - epsilon e - zeta z - eta - theta - iota - kappa - lambda - mu I would like a function that redistributes ...
0
votes
1answer
107 views

Stdlib support for compile-time metaprogramming?

The following macro... (defmacro compile-time-eval (&rest body) (eval (cons 'progn body))) ...allows for compile-time metaprogramming like this (a small contrived example) : (compile-time-...
5
votes
2answers
75 views

How do I depend on macros from other packages?

I'm writing a package that provides some Python functionality. I want to use python-rx, which is part of Emacs' python.el. (eval-when-compile (require 'python)) (defun python-foo-starts-defun-p () ...
1
vote
0answers
159 views

CSV to Markdown table

Say we have a CSV in a buffer: foo,bar,hello 1,234,95 8923,901,145 Are there any native Emacs or Markdown-mode built-ins in Emacs that can be used to convert it to a Markdown table? It looks like ...
2
votes
1answer
340 views

How to quote for multiple levels of macro expansion

I'm new to Emacs and think I ran into some macro expansion problems. There's a function solarized-with-color-variables which is a macro that creates local variables using let* and then expands the ...
13
votes
2answers
1k views

“and” vs “when” for conditionals

This is a follow-up on the comments on this answer. The following bits of code seem to be equivalent: (and a b) (when a b) Of course and lets you put more conditions: (and a b c d) means (when (and ...
3
votes
1answer
193 views

How to give a list to cond?

We have a number of clauses (condition body) which we want to give to cond, like so: (cond (condition1 body1) (condition2 body2) (condition3 body3)) But imagine that the clauses were ...