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: Xah
Subject: Re: How to automatically increment an index array
Date: Thu, 26 Jun 2008 19:44:34 -0700 (PDT)
User-agent: G2/1.0

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/

reply via email to

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