Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recording a macro that preserves timing

26 views
Skip to first unread message

Juha Nieminen

unread,
Oct 4, 2012, 6:21:32 AM10/4/12
to
I can record (and then replay) keyboard macros in emacs. However, I would
like a variant that also records the timing between the keypresses so
that when it's played back, it does the exact same thing as when recording,
with the same timing.

Then I would like to be able to play said macro with a speed factor. In
other words, I would like to be able to multiply all the recorded timings
by a value (so that if I multiply them eg. by 0.5 the macro will play at
twice the original speed).

If possible, it would be nice if this macro could be saved to a file
where it could be edited by hand (to eg. add or modify timings). In fact,
it would be really great if it was saved in a format that would allow
defining different factors for different groups of recorded keypresses
(so that by editing the file I could define "this part should be played
back with a factor of 0.5, this part with a factor of 0.2, etc.")

How difficult would such elisp code be to make?

Doug Lewan

unread,
Oct 4, 2012, 7:50:43 AM10/4/12
to Juha Nieminen, help-gn...@gnu.org
Juha,

On occasion I've wanted such a thing too, but never looked into it.

>From the emacs lisp Info on "Keyboard Macros", their definition is controlled by the variable defining-kbd-macro which comes from the C code. So, that would be the place to look.

I find it (defining_kbd_macro in C) in keyboard.c and macros.c. It only appears 18 times, so it shouldn't be *too* hard to copy or mimic or modify to meet your needs. You'd also have to consider doing corresponding work with kmacro.el (and maybe other places too).

With respect to editing: "C-x C-k e runs the command edit-kbd-macro,...."

You can save that definition and do whatever you need to with it. Indeed, the annotations in the keyboard macro editor generally give enough good hints that you could convert it into LISP, which would let you do very general programming with it.

I hope this helps.

,Doug

Afterthought: By "*too* hard" I mean that I'm guessing it would take under a week of full time development. It might be a few hours, but I've never touched emacs' C code.
I didn't see anything like this in the emacs TODO list. Perhaps you could make a feature request to bug-gn...@gnu.org.

Stefan Monnier

unread,
Oct 4, 2012, 9:33:27 AM10/4/12
to
> How difficult would such elisp code be to make?

Not sure. I'd recommend you look at kmacro.el.
I'd suggest you change kmacro-start-macro to add a pre-command-hook that
records the time of every command, and then in kmacro-end-macro record
those timings alongside the actual macro.

Maybe you could combine the two into a normal macro, but this requires
some trick because macros contain sequences of *events* (like
key-presses) rather than commands, so you can't directly add code
into them.
But you could use a special `macro-wait' event, and add a binding for it
in the global-map.

Otherwise, you'll need to write a new function to run your macros.


Stefan
0 new messages