6

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.

Drew
  • 75,699
  • 9
  • 109
  • 225
JKRT
  • 201
  • 2
  • 4
  • 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 Answers1

4
  1. Initialize some counter variable, say counter, to 0. (setq counter 0)
  2. Define the macro
  3. Give the macro a name, say "some-macro" by C-x C-k n (kmacro-name-last-macro)
  4. M-x insert-kbd-macro. This will dump the macro out as Lisp code.
  5. 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)))

  6. Evaluate the s-exp.
  7. Execute your macro by M-x some-macro. If you find this cumbersome, just define another keyboard to do this...
  8. To check how many times you've replayed the macro, just evaluate the variable counter.
  • 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