Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to increase a number under cursor?

23 views
Skip to first unread message

Wang Yin

unread,
May 20, 2003, 4:37:08 AM5/20/03
to
Hi,

I want to record a macro to type flexible numbered lists.
for example:

continuous:
1.
2.
3.
4.
...

odd:
1.
3.
5.
7.
...


hex:

0x9
0xA
0xB

octal:
007
010
011

The only problem I meet is how to increase the number under
the cursor?

Brendan Halpin

unread,
May 20, 2003, 8:58:46 AM5/20/03
to
Wang Yin <wang...@mails.tsinghua.edu.cn> writes:

> The only problem I meet is how to increase the number under
> the cursor?


How about this?

(defun wy-incr-num-at-cursor ()
(interactive)
(let ((number (number-at-point)))
(and number
(let ((bounds (bounds-of-thing-at-point 'sexp)))
(delete-region (car bounds) (cdr bounds))
(insert (format "%d" (1+ number)))))))

(local-set-key " a" 'wy-incr-num-at-cursor)

For hex and octal, you'll need to look into where number-at-point
is defined, i.e. thingatpt.el, which comes with Emacs.

Brendan

PS This answers some of what you asked for, but there are probably
cleverer ways of achieving what you actually want to do.

--
Brendan Halpin, Department of Sociology, University of Limerick, Ireland
Tel: w +353-61-213147 f +353-61-202569 h +353-61-390476; Room F2-025 x 3147
mailto:brendan...@ul.ie http://www.ul.ie/sociology/brendan.halpin.html

Oliver Scholz

unread,
May 20, 2003, 9:34:32 AM5/20/03
to
Wang Yin <wang...@mails.tsinghua.edu.cn> writes:

> Hi,
>
> I want to record a macro to type flexible numbered lists.
> for example:

Are you talking about a keyboard macro?

[...]


> The only problem I meet is how to increase the number under
> the cursor?

It is not clear to me what you want here. Do you simply want a
function that deletes the number at point and inserts number + X into
the buffer again? What do you want to achieve?

If you simply want a skeleton for lists, you could use something like
this:

(defun wy-list-skel ()
(interactive)
(let ((num 0))
(skeleton-insert
'(nil
("Type something: "
(number-to-string (setq num (1+ num)))
". " str "\n")))))

Hexadezimal:

(defun wy-hex-list-skel ()
(interactive)
(let ((num 0))
(skeleton-insert
'(nil
("Type something: "
(format "0x%02x" (setq num (1+ num)))
". " str "\n")))))

Oliver
--
1 Prairial an 211 de la Révolution
Liberté, Egalité, Fraternité!

David Kastrup

unread,
May 20, 2003, 10:17:40 AM5/20/03
to
Oliver Scholz <alkib...@gmx.de> writes:

> Wang Yin <wang...@mails.tsinghua.edu.cn> writes:
>
> > I want to record a macro to type flexible numbered lists.
> > for example:
>
> Are you talking about a keyboard macro?
>
> [...]
> > The only problem I meet is how to increase the number under
> > the cursor?
>
> It is not clear to me what you want here. Do you simply want a
> function that deletes the number at point and inserts number + X
> into the buffer again? What do you want to achieve?

Well, with a development Emacs we have the silly answer: type
M-# e 1 + q

Which will incidentally also increment -3/8 to 5:8, and simplify
polynomial expressions and stuff.

--
David Kastrup, Kriemhildstr. 15, 44793 Bochum

Wang Yin

unread,
May 21, 2003, 4:10:26 AM5/21/03
to
Yes! This is what I wanted!
Thank you.

But thingatpt.el seems can't solve the problem of increase a
hexadecimal number and octal number.

Eric Ludlam

unread,
May 21, 2003, 8:27:45 AM5/21/03
to
>>> Wang Yin <wang...@mails.tsinghua.edu.cn> seems to think that:

>Hi,
>
>I want to record a macro to type flexible numbered lists.

This is one of my favorite commands I pulled off of one of these
newsgroups many years ago, and have been tweaking ever since. I bound
it to M-o, and use it quite often.

;;; Original author: t...@netcom.com, 28-Jan-1996
;;; Modified for multiple lines: Eric
(defun another-line (num-lines)
"Copies line, preserving cursor column, and increments any numbers found.
Copies a block of optional NUM-LINES lines. If no optional argument is given,
then only one line is copied."
(interactive "p")
(if (not num-lines) (setq num-lines 0) (setq num-lines (1- num-lines)))
(let* ((col (current-column))
(bol (save-excursion (forward-line (- num-lines)) (beginning-of-line) (point)))
(eol (progn (end-of-line) (point)))
(line (buffer-substring bol eol)))
(goto-char bol)
(while (re-search-forward "[0-9]+" eol 1)
(let ((num (string-to-int (buffer-substring
(match-beginning 0) (match-end 0)))))
(replace-match (int-to-string (1+ num))))
(setq eol (save-excursion (goto-char eol) (end-of-line) (point))))
(goto-char bol)
(insert line "\n")
(move-to-column col)))


--
Eric Ludlam "Photonic Doodler" The MathWorks x 7556
elu...@mathworks.com (work) http://www.mathworks.com
er...@siege-engine.com (home) http://www.siege-engine.com

Sandip Chitale

unread,
May 21, 2003, 12:13:20 PM5/21/03
to
Well, I am not sure if this is what you want but here is a defun
that inserts a template into a file:

(defun insert-template (template &optional count)
(interactive "sTemplate: \np")
(let ((i 1))
(while (<= i count)
(insert (format template i))
(setq i (+ i 1)))))

Type:

C-u 5 M-x insert-template Ret

Prompt in minibuffer:
---------------------------------
Template:
---------------------------------

Type:

% d . C-q C-j Ret

The following is inserted in the buffer:

1.
2.
3.
4.
5.

Wang Yin <wang...@mails.tsinghua.edu.cn> wrote in message news:<phwugm3...@wangyin.com>...

0 new messages