0

I'm trying to learn how to:

  1. Define a kbd-macro, (here I'm using kbd-macros, because I'm bad at elisp currently).
  2. Letting that to be executed when yas-snippet to expand. (here I'm struggling with)

My current state: I want to make following in latex equation. First write a_x / then press Tab to get \frac{a_x}{} (in here / as yas-snippet key).

# -*- mode: snippet -*-
# name: 1slash-frac_one_word
# key: /
# type: command
# --
(fset 'gry-test-frac-kbdmacro
   (kmacro-lambda-form [C-left ?\\ ?f ?r ?a ?c ?\{ C-right ?\} ?\{ ?\} left return return up tab] 0 "%d"))
(gry-test-frac-kbdmacro)

Basically, I first defined kbd-macro, and then inserted it's definition to the snippet, and then running it. This works.


My concerns/tries: Every time this snippet expands, gry-test-frac-kbdmacro gets defined again and again. Which seems to me inefficient. Also I don't want to write it's definition in other places like init.el. So tried following:

# -*- mode: snippet -*-
# name: 1slash-frac_one_word
# key: /
# type: command
# --
(kmacro-lambda-form [C-left ?\\ ?f ?r ?a ?c ?\{ C-right ?\} ?\{ ?\} left return return up tab] 0 "%d")

Basically, I tried to run the things inside of the macro definition (without defining it). However this doesn't work.


  • Could you explain me why 2-nd snippet doesn't work?
  • And Is there any way to run what's inside of macro without defining it first?
2
  • 1
    @Arktik you should write your comment up as an answer
    – nega
    Commented Mar 10, 2023 at 16:46
  • @Arktik thank you, your comment is the answer I was looking for. Please write your comment as answer, So I can accept it & close this question.
    – Garid
    Commented Mar 11, 2023 at 1:53

2 Answers 2

1

In order to run a macro without binding it to a name, use execute-kbd-macro. In your example, this would be:

(execute-kbd-macro [C-left ?\\ ?f ?r ?a ?c ?\{ C-right ?\} ?\{ ?\} left return return up tab])

Your second snippet creates a function, it needs to be evaluated for it to take effect. The "lambda-form" part refers to an anonymous function as the keyword lambda is used to define anonymous functions. See https://www.gnu.org/software/emacs/manual/html_node/elisp/Anonymous-Functions.html

2

The second snippet doesn't work because all you're doing is defining the keyboard macro lambda form; you're not doing anything with it. Use execute-kbd-macro as @Arktik suggests (see C-h f execute-kbd-macro). If you want to be more "lispy" you could do a snippet like the following:

# type: command
# key: /
# name: test2
# --
(backward-word)
(insert "\\frac{")
(forward-word)
(insert "}{}")
(left-char)

Note that yasnippet will wrap that in a progn on save.

But personally I wouldn't do that. I'd type my numerator, select backwards over it, then execute my snippet. Something like:

# name: test
# binding: C-c C-y
# --
\frac{`yas-selected-text`}{$1} $0

Note that you can't trigger the snippet via key:, you need to set a keybinding C-c C-y in this example.

But I wouldn't even do that. I'd just use the default latex-mode "frac" snippet which is:

# -*- mode: snippet -*-
# name: frac
# key: frac
# --
\frac{${1:numerator}}{${2:denominator}}$0

If you find yourself doing a lot of "repetitive but slightly different" text, you might want to check out Auto-YASnippet which describes itself as a hybrid of keyboard macros and YASnippet.

5
  • 1
    Just to add to the above, also have a look at cdlatex package for math stuff in LaTeX, github.com/cdominik/cdlatex.
    – Arktik
    Commented Mar 10, 2023 at 17:02
  • @nega, Thank you for your comment. As you mentioned about @Arktik 's comment about execute-kbd-macro is the answer I was looking for.
    – Garid
    Commented Mar 11, 2023 at 1:56
  • My reasoning about not to use frac snippet in latex-mode is that I write numerator first and then realize I need to write up as \frac{}{}. (It's just how my thinking process works I guess). To be honest I still use this snippet mainly until now.`
    – Garid
    Commented Mar 11, 2023 at 2:00
  • My reasoning about not to use # binding: C-c C-y or any other binding is that it has more keystrokes than simple / + tab. (also not to mention about thinking / remembering about this as clearly)
    – Garid
    Commented Mar 11, 2023 at 2:03
  • Thank you very much about the elsip code. When I learn elsip properly I will rewrite every kbd-macros in elisp. Until then I will use ecute-kbd-macro from @Arktik 's comment.
    – Garid
    Commented Mar 11, 2023 at 2:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.