[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to automatically increment an index array
From: |
Rupert Swarbrick |
Subject: |
Re: How to automatically increment an index array |
Date: |
Thu, 26 Jun 2008 12:47:01 +0100 |
User-agent: |
Gnus/5.110011 (No Gnus v0.11) Emacs/22.2 (gnu/linux) |
"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)))))))