* How to automatically increment an index array
@ 2008-06-26 7:45 Francis Moreau
2008-06-26 12:51 ` Peter Dyballa
2008-06-27 8:47 ` David
0 siblings, 2 replies; 10+ messages in thread
From: Francis Moreau @ 2008-06-26 7:45 UTC (permalink / raw)
To: gnu-emacs-help
Hello
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 ?
Thanks
--
Francis
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to automatically increment an index array
2008-06-26 7:45 How to automatically increment an index array Francis Moreau
@ 2008-06-26 12:51 ` Peter Dyballa
2008-06-27 8:47 ` David
1 sibling, 0 replies; 10+ messages in thread
From: Peter Dyballa @ 2008-06-26 12:51 UTC (permalink / raw)
To: Francis Moreau; +Cc: gnu-emacs-help
Am 26.06.2008 um 09:45 schrieb Francis Moreau:
> Can anybody give me a hint ?
With an up-to-date GNU Emacs 22.2 (or from development 23.0.x) you
could use the comma operator with query-replace-regexp or replace-
regexp as in:
C-M-% \([0-9]+\) RET \,(1+ \#1) RET
Each set of (decimal) digits is assumed a number and is replaced by
number plus one. I think exactly this example is given in the Emacs
info node. With the comma operator you could also use calc to perform
real calculations on any base.
--
Greetings
Pete
Globalisation – communism from above.
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to automatically increment an index array
2008-06-26 7:45 How to automatically increment an index array Francis Moreau
2008-06-26 12:51 ` Peter Dyballa
@ 2008-06-27 8:47 ` David
1 sibling, 0 replies; 10+ messages in thread
From: David @ 2008-06-27 8:47 UTC (permalink / raw)
To: help-gnu-emacs
"Francis Moreau" <francis.moro@gmail.com> 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
^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <mailman.13859.1214468323.18990.help-gnu-emacs@gnu.org>]
* Re: How to automatically increment an index array
[not found] <mailman.13859.1214468323.18990.help-gnu-emacs@gnu.org>
@ 2008-06-26 11:47 ` Rupert Swarbrick
2008-06-26 11:52 ` Rupert Swarbrick
2008-06-27 2:44 ` Xah
2008-06-27 7:23 ` Klaus Zeitler
2 siblings, 1 reply; 10+ messages in thread
From: Rupert Swarbrick @ 2008-06-26 11:47 UTC (permalink / raw)
To: help-gnu-emacs
"Francis Moreau" <francis.moro@gmail.com> writes:
> Hello
>
> 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 ?
>
No doubt others will come up with neater versions, but here's a quick
hack. Problems with it: it doesn't do any large-scale inspection of the
text, so if you had two different sets of these with different base
addresses, you'd need to use narrow-to-region. Also there's no error
checking, so you should probably look carefully at what happened to the
buffer before saving...
Rupert
(defun do-hex-incrementing ()
(interactive)
(save-excursion
(re-search-forward "\\[[0-9a-z]+\\]")
(let ((pt) (base))
(setf pt (1- (point)))
(search-backward "\[")
(setf base
(string-to-number
(buffer-substring (1+ (point)) pt) 16))
(while
(re-search-forward "\\[0x[0-9a-z]+\]" nil t)
(search-backward "[0x")
(forward-char)
(insert
(format "%x"
(+ (string-to-number
(substring (thing-at-point 'word) 2) 16)
base)))
(setf pt (point))
(search-forward "]")
(delete-region pt (1- (point)))))))
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to automatically increment an index array
2008-06-26 11:47 ` Rupert Swarbrick
@ 2008-06-26 11:52 ` Rupert Swarbrick
0 siblings, 0 replies; 10+ messages in thread
From: Rupert Swarbrick @ 2008-06-26 11:52 UTC (permalink / raw)
To: help-gnu-emacs
Rupert Swarbrick <rswarbrick@gmail.com> writes:
> (defun do-hex-incrementing ()
> (interactive)
> (save-excursion
> (re-search-forward "\\[[0-9a-z]+\\]")
> (let ((pt) (base))
> (setf pt (1- (point)))
> (search-backward "\[")
> (setf base
> (string-to-number
> (buffer-substring (1+ (point)) pt) 16))
> (while
> (re-search-forward "\\[0x[0-9a-z]+\]" nil t)
> (search-backward "[0x")
> (forward-char)
> (insert
> (format "%x"
> (+ (string-to-number
> (substring (thing-at-point 'word) 2) 16)
> base)))
> (setf pt (point))
> (search-forward "]")
> (delete-region pt (1- (point)))))))
I've just realised that I've used setf throughout. Too much common lisp
recently! In this case, I could have just used setq, so either replace
all setf's with setq's in the above or (require 'cl) beforehand.
Rupert
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to automatically increment an index array
[not found] <mailman.13859.1214468323.18990.help-gnu-emacs@gnu.org>
2008-06-26 11:47 ` Rupert Swarbrick
@ 2008-06-27 2:44 ` Xah
2008-06-27 6:50 ` David Kastrup
2008-06-27 7:23 ` Klaus Zeitler
2 siblings, 1 reply; 10+ messages in thread
From: Xah @ 2008-06-27 2:44 UTC (permalink / raw)
To: help-gnu-emacs
On Jun 26, 12:45 am, "Francis Moreau" <francis.m...@gmail.com> wrote:
> Hello
>
> 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,
If i understand you correctly, you want to insert increment numbers
into a column. I needed this every few weeks. Not often, but when
needed is rather tedious to do manually, so i've written a elisp to do
it once for all.
(defun insert-counter-column (n)
"Insert a sequence of integers vertically.
Example:
do this 1 times
do this 2 times
do this 3 times
...
If there are not enough existing lines after the cursor
when this function is called, it aborts at the last line.
See also: `kill-rectangle' and `string-rectangle'."
(interactive "nEnter the max integer: ")
(let ((i 1) colpos)
(setq colpos (- (point) (point-at-bol)))
(while (<= i n)
(insert (number-to-string i))
(next-line) (beginning-of-line) (forward-char colpos)
(setq i (1+ i))
)))
Use it together with buildin
kill-rectangle (C-x r k )
and string-rectangle (C-x r t)
or
query-replace (M-%).
Since from your example your increment is 4, and it's hex, you might
need to modify the code a bit.
Xah
∑ http://xahlee.org/
☄
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to automatically increment an index array
2008-06-27 2:44 ` Xah
@ 2008-06-27 6:50 ` David Kastrup
2008-06-27 7:52 ` Thien-Thi Nguyen
[not found] ` <mailman.13886.1214553354.18990.help-gnu-emacs@gnu.org>
0 siblings, 2 replies; 10+ messages in thread
From: David Kastrup @ 2008-06-27 6:50 UTC (permalink / raw)
To: help-gnu-emacs
Xah <xahlee@gmail.com> writes:
> On Jun 26, 12:45 am, "Francis Moreau" <francis.m...@gmail.com> wrote:
>> Hello
>>
>> 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,
>
> If i understand you correctly, you want to insert increment numbers
> into a column. I needed this every few weeks. Not often, but when
> needed is rather tedious to do manually, so i've written a elisp to do
> it once for all.
Well, one could write something like
C-M-% \[0x\([0-9a-f]+\)\] RET \,(format "[%08x]" (+ (* 4 (string-to-number
\1 16)) #X8745b400)) RET
It is not all too pretty, but not all too bad either. If the pattern of
the numbers is regular, you might use \# in the replacement rather than
an explicit conversion.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to automatically increment an index array
2008-06-27 6:50 ` David Kastrup
@ 2008-06-27 7:52 ` Thien-Thi Nguyen
[not found] ` <mailman.13886.1214553354.18990.help-gnu-emacs@gnu.org>
1 sibling, 0 replies; 10+ messages in thread
From: Thien-Thi Nguyen @ 2008-06-27 7:52 UTC (permalink / raw)
To: help-gnu-emacs
() David Kastrup <dak@gnu.org>
() Fri, 27 Jun 2008 08:50:35 +0200
Well, one could write something like
C-M-% \[0x\([0-9a-f]+\)\] RET \,(format "[%08x]" (+ (* 4
(string-to-number \1 16)) #X8745b400)) RET
It is not all too pretty, but not all too bad either. If the
pattern of the numbers is regular, you might use \# in the
replacement rather than an explicit conversion.
Although the general approach is sound, in this case
the #x8745b400 will be silently truncated on a 32-bit host.
To workaround, one can hardformat the high hex digits:
(format "[8745%04x]" (... #xb400))
Munge to taste...
thi
^ permalink raw reply [flat|nested] 10+ messages in thread
[parent not found: <mailman.13886.1214553354.18990.help-gnu-emacs@gnu.org>]
* Re: How to automatically increment an index array
[not found] ` <mailman.13886.1214553354.18990.help-gnu-emacs@gnu.org>
@ 2008-06-27 8:19 ` David Kastrup
0 siblings, 0 replies; 10+ messages in thread
From: David Kastrup @ 2008-06-27 8:19 UTC (permalink / raw)
To: help-gnu-emacs
Thien-Thi Nguyen <ttn@gnuvola.org> writes:
> () David Kastrup <dak@gnu.org>
> () Fri, 27 Jun 2008 08:50:35 +0200
>
> Well, one could write something like
>
> C-M-% \[0x\([0-9a-f]+\)\] RET \,(format "[%08x]" (+ (* 4
> (string-to-number \1 16)) #X8745b400)) RET
>
> It is not all too pretty, but not all too bad either. If the
> pattern of the numbers is regular, you might use \# in the
> replacement rather than an explicit conversion.
>
> Although the general approach is sound, in this case
> the #x8745b400 will be silently truncated on a 32-bit host.
Oops.
> To workaround, one can hardformat the high hex digits:
>
> (format "[8745%04x]" (... #xb400))
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: How to automatically increment an index array
[not found] <mailman.13859.1214468323.18990.help-gnu-emacs@gnu.org>
2008-06-26 11:47 ` Rupert Swarbrick
2008-06-27 2:44 ` Xah
@ 2008-06-27 7:23 ` Klaus Zeitler
2 siblings, 0 replies; 10+ messages in thread
From: Klaus Zeitler @ 2008-06-27 7:23 UTC (permalink / raw)
To: help-gnu-emacs
>>>>> "Francis" == Francis Moreau <francis.moro@gmail.com> writes:
Francis>
Francis> I have this problem: in a buffer, *scratch* for example I have:
Francis>
Francis> [8735b450] = xxx,
Francis> [0x15] = xxx,
Francis> [0x16] = xxx,
Francis>
Francis> After running a 'magic' command I'd like to calculate the new
Francis> array indexes as follow:
Francis>
Francis> [8735b450] = xxx,
Francis> [8735b454] = xxx,
Francis> [8735b458] = xxx,
Francis> ...
I would recommend a keyboard macro. You start by inserting a number into
a register (with number-to-register). Then go to your first line, delete
the chars between '[' and ']', insert register, increment register, move to
next line and so on.
Just look in info for "Keeping Numbers in Registers" (A nice feature IMHO :-).
HTH
Klaus
--
-------------------------------------
| Klaus Zeitler Alcatel-Lucent |
-------------------------------------
---
The secret to creativity is knowing how to
hide your sources. -- Albert Einstein
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2008-06-27 8:47 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-06-26 7:45 How to automatically increment an index array Francis Moreau
2008-06-26 12:51 ` Peter Dyballa
2008-06-27 8:47 ` David
[not found] <mailman.13859.1214468323.18990.help-gnu-emacs@gnu.org>
2008-06-26 11:47 ` Rupert Swarbrick
2008-06-26 11:52 ` Rupert Swarbrick
2008-06-27 2:44 ` Xah
2008-06-27 6:50 ` David Kastrup
2008-06-27 7:52 ` Thien-Thi Nguyen
[not found] ` <mailman.13886.1214553354.18990.help-gnu-emacs@gnu.org>
2008-06-27 8:19 ` David Kastrup
2008-06-27 7:23 ` Klaus Zeitler
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).