[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Emacs: adding 1 to every number made of 2 digits inside a marked reg
From: |
Stephen Berman |
Subject: |
Re: Emacs: adding 1 to every number made of 2 digits inside a marked region. |
Date: |
Thu, 23 Sep 2021 12:23:12 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
On Thu, 23 Sep 2021 18:13:08 +0800 Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
> On Thu, Sep 23, 2021 at 6:08 PM Stephen Berman <stephen.berman@gmx.net> wrote:
>>
>> On Thu, 23 Sep 2021 17:00:01 +0800 Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>>
>> > On Thu, Sep 23, 2021 at 4:07 PM Stephen Berman <stephen.berman@gmx.net>
>> > wrote:
>> [...]
>> >> In your first screenshot it looks like point in *scratch* is after the
>> >> number 30 when you evaluate the while-sexp. Make sure point is before
>> >> 34 and then it should work.
>> >
>> > Exactly. Thank you for pointing this out. Then I do the following
>> > testing in the scratch buffer:
>> >
>> > some 30
>> > word 31 *
>> >
>> > * This is the position of point.
>> >
>> > M-:
>> > (while (re-search-backward "[[:digit:]]\\{2\\}" nil t) (let ((x
>> > (match-string 0))) (delete-backward-char 2) (insert (format "%d" (1+
>> > (string-to-number x))))))
>> >
>> > Then I obtained the following in scratch:
>> >
>> > ;; This buffer is for
>> > tex100999897969594939291908988878685848382818079787776757473727170696867666564636261605958575655545352515049484746454443424140393837363534333231
>> >
>> > Any hints for this strange result?
>>
>> Since you're now searching backwards, you need to delete forwards to
>> retain the position of the numbers and you need to make sure point is in
>> front of the number that was just incremented before continuing the loop:
>>
>> (while (re-search-backward "[[:digit:]]\\{2\\}" nil t)
>> (let ((x (match-string 0))
>> (pt (point)))
>> (delete-char 2)
>> (insert (format "%d" (1+ (string-to-number x))))
>> (goto-char pt)))
>
> Tried but it does nothing.
Since you're now searching backwards, are you sure you had point *after*
the numbers? I.e., if this is the *scratch* buffer:
-------------------------------------------------------
;; some 35
;; word 31
;; another 39
;; thing 60
;; to 40
;; say 11
;; here 48
(while (re-search-backward "[[:digit:]]\\{2\\}" nil t)
(let ((x (match-string 0))
(pt (point)))
(delete-char 2)
(insert (format "%d" (1+ (string-to-number x))))
(goto-char pt)))
-------------------------------------------------------
then put the cursor at the end of the sexp and type `C-x C-e'.
Steve Berman
- Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/22
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/22
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Emanuel Berg, 2021/09/22
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/22
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region.,
Stephen Berman <=
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Stephen Berman, 2021/09/23
- Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Hongyi Zhao, 2021/09/23
Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Emanuel Berg, 2021/09/22
Re: Emacs: adding 1 to every number made of 2 digits inside a marked region., Emanuel Berg, 2021/09/22