I am quite bad at counting and remember how many times I have invoked a special command, so I wonder is there any way to count and show in the minibuffer how many time a keyboard macro was invoked? Say that I want to use the macro to navigate the buffer in a specific way, and increment a counter as I move.
Asked
Active
Viewed 228 times
6
-
To understand why you need a macro for that, is the application something that is different each time and you need to record a macro for that? Or would it be fine to instead write a little elisp function with a `message` from at the end? – Kaushal Modi Jun 06 '15 at 01:01
-
Hi, a function that increments a counter and saves the state of the counter each time a specific macro is invoked and of course message me about how many times it was invoked would suffice, elisp is not my strong side however. – JKRT Jun 06 '15 at 11:06
1 Answers
4
- Initialize some counter variable, say
counter
, to 0.(setq counter 0)
- Define the macro
- Give the macro a name, say "some-macro" by
C-x C-k n
(kmacro-name-last-macro
) M-x insert-kbd-macro
. This will dump the macro out as Lisp code.Add
(setq counter (1+ counter))
to the Lisp code as follows:(fset 'some-macro (lambda (&optional arg) "Keyboard macro." (interactive "p") (setq counter (1+ counter)) (kmacro-exec-ring-item ...... arg)))
- Evaluate the s-exp.
- Execute your macro by
M-x some-macro
. If you find this cumbersome, just define another keyboard to do this... - To check how many times you've replayed the macro, just evaluate the variable
counter
.

Peking Duck
- 56
- 3
-
Thank you, I wrote: (fset 'test (lambda (&optional arg) "Keyboard macro." (interactive "p") (setq counter( + 1 counter)) (kmacro-exec-ring-item (quote ([return 32 49 50 51] 0 "%d")) arg) (print counter))) Seems to be working and output the number of invocations in the minibuffer! – JKRT Jun 06 '15 at 12:40