There have been various times in my programming career that I've wanted an emacs macro with arguments, so that it would do things like concatenate a number in each line. Things like this in C:
#define a 0
#define b 1
...
#define l 10
#define m 11
Usually I take that as a sign that I need to write some other program to generate the lines of the C program, but for some tasks that's overkill and I want an automated way to create a list of sequential things so I don't have to type 1 down 1 down 1 down 1 down 2 down 2 down 2 down...
If you want to produce this list:
test1
test2
test3
...
Do like this:
C-x r n q (store the number 1 in register q)
C-x ( (start macro recording)
test C-x r i q (insert contents of register q)
C-x r + q (increment register q by 1)
RET (new line)
C-x ) (stop macro recording)
The easy way I do it is to make however many copies of "test0" I want, then make a trivial throwaway macro where I move my cursor to the number I want to increment, and hit M-x rolling-increment-number-at-point. (Of course with autocomplete...)
I don't think it's quite what you're talking about, but I have an Emacs function that runs the current region through Python and replaces it with the stdout from running it.
For your example, I write this:
vars = list('abcdefghijklm')
for v,i in zip(vars, range(len(vars))):
print("#define {} {}".format(v, i))
Select it and press Ctrl+Alt+p and have it replaced by:
#define a 0
#define b 1
#define c 2
#define d 3
#define e 4
#define f 5
#define g 6
#define h 7
#define i 8
#define j 9
#define k 10
#define l 11
#define m 12
Yeah, I considered that, but the situation comes up so infrequently I'd never remember the syntax. I use Python everyday anyway, so it almost comes naturally.
I know exactly what you mean -- but perhaps it doesn't even have to be a macro, but a more powerful search-and-replace, with changing replacements.
My lisp-fu isn't good enough to implement this today, but I would find it really easy to:
* copy `#define a 0` with C-k, M-w, C-y, RET, C-y, RET, C-y, RET, ... (so copy the line and paste N times)
* begin interactive search-and-replace, with usual keybindings (Y for replace, N for skip, . for replace and stop)
* but with additional keybinding, e.g. + for change replacement and apply
Alternatively, the replace argument in M-% would be a [somehow-delimited] list of replacements, and + or capital Y would pop off the next replacement and begin using that.
There is a keyboard macro counter, which I've been able to use in situations like this. It's described in the manual[1] and on this Stackoverflow question[2].
The newest emacs keybindings should help sort this out. Use <F3> to make it easy. Might as well just go C-h k <F3> to see what it does, but the function name `kmacro-start-macro-or-insert-counter' might give you a clue.
Here's what I'd do for your example, starting in an empty buffer:
Then press F4 until you reach `m'. (You may find that the sequence ends with `m' being 12.)
(NOTE you should totally use `enum' for this; your debugger may on occasion permit you to use the names when you are entering an expression, and you'll get sensible error messages in the event of a conflict. Unlikely in this case I suppose since nobody uses variables called `i' or `j'.)
In the past, I've used 2 buffers to solve this problem.
I'll fill one buffer with the text I'm editing, and the other buffer with any 'arguments'. Then I'll just write the macro to switch between buffers and grab arguments as necessary.
I don’t know Emacs, but I know a way to do that in Vim, and Emacs might support a similar method. In Vim, Ctrl-A and Ctrl-X increment and decrement the number the cursor is over. You can generate a list by recording a macro that duplicates the current line and then increments its number (qqVyp ^a q), and running that macro repeatedly (10@q). Emacs might also have a function for incrementing characters, not just numbers.
I find that copy rectangle works wonders for lots of problems like this. It's simple to type
1
2
3
4
5
6
7
8
9
0
and then using copy rectangle I can quickly assemble long lists of numbers.
I do use keyboard macros quite frequently too, but mostly for more complex operations (e.g. start of line, search for char, set point, search for next char, kill-ring-save, move to end, set point, yank, uppercase region)
Surely this is a solved problem.