help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to automatically increment an index array


From: David
Subject: Re: How to automatically increment an index array
Date: Fri, 27 Jun 2008 10:47:39 +0200
User-agent: Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux)

"Francis Moreau" <address@hidden> writes:
> I have this problem: in a buffer, *scratch* for example I have:
>
>   [8735b450] = xxx,
>   [0x15] = xxx,
>   [0x16] = xxx,
>   [0x17] = xxx,
>   [0x18] = xxx,
>   [0x19] = xxx,
>   [0x1a] = xxx,
>   [0x1b] = xxx,
>
> After running a 'magic' command I'd like to calculate the new array indexes
> as follow:
>
>   [8735b450] = xxx,
>   [8735b454] = xxx,
>   [8735b458] = xxx,
>   [8735b45c] = xxx,
>   [8735b460] = xxx,
>   [8735b464] = xxx,
>   [8735b468] = xxx,
>   [8735b46c] = xxx,
>   ...
>
> Can anybody give me a hint ?

There are already some very nice suggestions on this thread, but one
problem here is that the hex numbers will at least on 32bit systems be
too large for Emacs to handle (just do C-h v most-positive-fixnum RET).

Since these replacement problems one encounters while programming are
often slightly different, I usually use keyboard macros instead of fixed
functions to tackle them. For the above example, I'd do:

C-x C-k C-f %x RET  (set macro counter *f*ormat to hex)
C-x C-k C-c #xb454 RET  (set initial macro *c*ounter value)

now go to the line [0x15] and set the cursor on the zero. Then

C-x ( 
DEL DEL DEL DEL  (delete existing number)
8 7 3 5 
C-x C-k C-i  (*i*nsert macro counter, incrementing it by 1)
C-x C-k C-a 3 RET  (*a*dd another 3)
(... move cursor the next line and again on the zero ...)
C-x )  (end macro)

Now you can just do `C-x e' and repeatedly press `e' to replace the next
lines. Maybe this is a bit confusing when doing this the first time, but
you get quickly used to it.

Note that you need Emacs 22 for using the macro counter. For older
Emacsen, use registers instead.

-David





reply via email to

[Prev in Thread] Current Thread [Next in Thread]