* query-replace-regexp number number+1
@ 2006-04-11 16:04 rturrado
2006-04-11 16:14 ` David Kastrup
0 siblings, 1 reply; 9+ messages in thread
From: rturrado @ 2006-04-11 16:04 UTC (permalink / raw)
Hi,
I wonder if you can do that easily in emacs: to search for a number,
let's say \([0-9]+\), and replace it with its successor, let's say
\1+1. The query I've just written would replace 25 with 25+1. What I
want is to evaluate 25+1 and replace 25 with 26.
Thanks, Roberto.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-11 16:04 query-replace-regexp number number+1 rturrado
@ 2006-04-11 16:14 ` David Kastrup
2006-04-11 18:36 ` rturrado
0 siblings, 1 reply; 9+ messages in thread
From: David Kastrup @ 2006-04-11 16:14 UTC (permalink / raw)
"rturrado" <rturrado@gmail.com> writes:
> Hi,
>
> I wonder if you can do that easily in emacs: to search for a number,
> let's say \([0-9]+\), and replace it with its successor, let's say
> \1+1. The query I've just written would replace 25 with 25+1. What I
> want is to evaluate 25+1 and replace 25 with 26.
With a developer Emacs, you can do
C-M-% \([0-9]+\) RET \,(1+ \#1) RET
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-11 16:14 ` David Kastrup
@ 2006-04-11 18:36 ` rturrado
2006-04-11 19:05 ` David Kastrup
2006-04-12 14:52 ` Giorgos Keramidas
0 siblings, 2 replies; 9+ messages in thread
From: rturrado @ 2006-04-11 18:36 UTC (permalink / raw)
I've tried that, but it complains with an "Invalid use of '\' in
replacement text" error. I'm using GNU Emacs 21.3.1 on a SuSE bash.
What's the "developer Emacs" version?
Can I find a tutorial online explaining how to run commands/evaluate
expressions/and so on within a regexp?
Thanks, Roberto.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-11 18:36 ` rturrado
@ 2006-04-11 19:05 ` David Kastrup
2006-04-12 5:46 ` B. T. Raven
2006-04-12 16:12 ` rturrado
2006-04-12 14:52 ` Giorgos Keramidas
1 sibling, 2 replies; 9+ messages in thread
From: David Kastrup @ 2006-04-11 19:05 UTC (permalink / raw)
"rturrado" <rturrado@gmail.com> writes:
> I've tried that, but it complains with an "Invalid use of '\' in
> replacement text" error. I'm using GNU Emacs 21.3.1 on a SuSE bash.
> What's the "developer Emacs" version?
It announces itself as 22.0.something and has not yet been released.
Some people provide snapshots, though. No idea whether there is
something for SuSE (Mandriva has one). And one can, of course,
compile a copy oneself.
> Can I find a tutorial online explaining how to run commands/evaluate
> expressions/and so on within a regexp?
You could probably do
M-: (query-replace-regexp "\\([0-9]+\\)" '(replace-eval-replacement format "%d" (1+ (string-to-number (match-string 1))))) RET
But it is not exactly kosher.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-11 19:05 ` David Kastrup
@ 2006-04-12 5:46 ` B. T. Raven
2006-04-12 8:22 ` Thien-Thi Nguyen
2006-04-12 8:47 ` David Kastrup
2006-04-12 16:12 ` rturrado
1 sibling, 2 replies; 9+ messages in thread
From: B. T. Raven @ 2006-04-12 5:46 UTC (permalink / raw)
"David Kastrup" <dak@gnu.org> wrote in message
news:85lkuc6iho.fsf@lola.goethe.zz...
> "rturrado" <rturrado@gmail.com> writes:
>
> > I've tried that, but it complains with an "Invalid use of '\' in
> > replacement text" error. I'm using GNU Emacs 21.3.1 on a SuSE bash.
> > What's the "developer Emacs" version?
>
> It announces itself as 22.0.something and has not yet been released.
> Some people provide snapshots, though. No idea whether there is
> something for SuSE (Mandriva has one). And one can, of course,
> compile a copy oneself.
>
> > Can I find a tutorial online explaining how to run commands/evaluate
> > expressions/and so on within a regexp?
>
> You could probably do
>
> M-: (query-replace-regexp "\\([0-9]+\\)" '(replace-eval-replacement
format "%d" (1+ (string-to-number (match-string 1))))) RET
>
> But it is not exactly kosher.
Is it not kosher because it's not documented or because it accesses lisp
objects in a non-standard way? I don't understand it but it works. How?
Here is the function in 21.3:
(defun replace-eval-replacement (expression replace-count)
(let ((replacement (eval expression)))
(if (stringp replacement)
replacement ;; string
(prin1-to-string replacement t)))) ;; number
But why is format not a function, i.e. (format... and what is
replace-count (not used here)? I want do to something similar to what
rturrado needs: Convert a bunch of lines like:
p.150
into
p.150(125)... and then p.151 into p.151(126) etc.
Thanks,
Ed.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-12 5:46 ` B. T. Raven
@ 2006-04-12 8:22 ` Thien-Thi Nguyen
2006-04-12 8:47 ` David Kastrup
1 sibling, 0 replies; 9+ messages in thread
From: Thien-Thi Nguyen @ 2006-04-12 8:22 UTC (permalink / raw)
"B. T. Raven" <ecinmn@peoplepc.com> writes:
> Convert a bunch of lines like:
>
> p.150
>
> into
>
> p.150(125)... and then p.151 into p.151(126) etc.
see below.
not pretty, but...
munge to taste.
thi
______________________________
;;; another-line.el
;;;
;;; Rel:v-1-55
;;;
;;; Copyright (C) 1997, 1998, 2004 Thien-Thi Nguyen
;;; This file is part of ttn's personal elisp library, released under GNU
;;; GPL with ABSOLUTELY NO WARRANTY. See the file COPYING for details.
;;; Description: Copy current line, incrementing numbers. Bus-friendly. :->
;;;###autoload
(defun another-line ()
"Copy line, preserving cursor column, and increment any numbers found.
This should probably be generalized in the future."
(interactive)
(let* ((col (current-column))
(bol (progn (beginning-of-line) (point)))
(eol (progn (end-of-line) (point)))
(line (buffer-substring bol eol)))
(beginning-of-line)
(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)))))
(beginning-of-line)
(insert line "\n")
(move-to-column col)))
; 1999/03/19 01:46:00
;
; This is contributed by Jeff Tuckey. It only works if `transient-mark-mode'
; is nil, so we will mull on it a bit before deciding what to do.
;
; (defun copy-yank-and-increment (beg end)
; "Copy region to kill-ring, yanks this back to the buffer and
; increment any numbers found in the yanked text."
; (interactive "r")
; (copy-region-as-kill beg end)
; (yank '(4))
; (let ((p (point))
; (m (mark-marker)))
; (while (re-search-forward "[0-9]+" m 1)
; (let ((num (string-to-int (buffer-substring
; (match-beginning 0) (match-end 0)))))
; (replace-match (int-to-string (1+ num)))))
; (set-mark p)))
(provide 'another-line)
;;; another-line.el ends here
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-12 5:46 ` B. T. Raven
2006-04-12 8:22 ` Thien-Thi Nguyen
@ 2006-04-12 8:47 ` David Kastrup
1 sibling, 0 replies; 9+ messages in thread
From: David Kastrup @ 2006-04-12 8:47 UTC (permalink / raw)
"B. T. Raven" <ecinmn@peoplepc.com> writes:
> "David Kastrup" <dak@gnu.org> wrote in message
> news:85lkuc6iho.fsf@lola.goethe.zz...
>> "rturrado" <rturrado@gmail.com> writes:
>>
>> > I've tried that, but it complains with an "Invalid use of '\' in
>> > replacement text" error. I'm using GNU Emacs 21.3.1 on a SuSE bash.
>> > What's the "developer Emacs" version?
>>
>> It announces itself as 22.0.something and has not yet been released.
>> Some people provide snapshots, though. No idea whether there is
>> something for SuSE (Mandriva has one). And one can, of course,
>> compile a copy oneself.
>>
>> > Can I find a tutorial online explaining how to run commands/evaluate
>> > expressions/and so on within a regexp?
>>
>> You could probably do
>>
>> M-: (query-replace-regexp "\\([0-9]+\\)" '(replace-eval-replacement
> format "%d" (1+ (string-to-number (match-string 1))))) RET
>>
>> But it is not exactly kosher.
>
> Is it not kosher because it's not documented or because it accesses
> lisp objects in a non-standard way?
It's accessing internals.
> I don't understand it but it works. How? Here is the function in
> 21.3:
> But why is format not a function, i.e. (format... and what is
> replace-count (not used here)? I want do to something similar to what
> rturrado needs: Convert a bunch of lines like:
>
> p.150
>
> into
>
> p.150(125)... and then p.151 into p.151(126) etc.
Your best bet is to use a developer version of Emacs which offers the
\, syntax. Short of that, do your own Elisp loop around
search-forward-regexp and replace-string.
--
David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-11 18:36 ` rturrado
2006-04-11 19:05 ` David Kastrup
@ 2006-04-12 14:52 ` Giorgos Keramidas
1 sibling, 0 replies; 9+ messages in thread
From: Giorgos Keramidas @ 2006-04-12 14:52 UTC (permalink / raw)
"rturrado" <rturrado@gmail.com> writes:
> I've tried that, but it complains with an "Invalid use of '\' in
> replacement text" error. I'm using GNU Emacs 21.3.1 on a SuSE bash.
> What's the "developer Emacs" version?
The version that is currently the "HEAD" of the CVS tree. It is the
next major release of Emacs, so its number is 22.X instead of 21.X
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: query-replace-regexp number number+1
2006-04-11 19:05 ` David Kastrup
2006-04-12 5:46 ` B. T. Raven
@ 2006-04-12 16:12 ` rturrado
1 sibling, 0 replies; 9+ messages in thread
From: rturrado @ 2006-04-12 16:12 UTC (permalink / raw)
Thanks very much. That works nifty!
Roberto.
David Kastrup wrote:
> "rturrado" <rturrado@gmail.com> writes:
>
> > I've tried that, but it complains with an "Invalid use of '\' in
> > replacement text" error. I'm using GNU Emacs 21.3.1 on a SuSE bash.
> > What's the "developer Emacs" version?
>
> It announces itself as 22.0.something and has not yet been released.
> Some people provide snapshots, though. No idea whether there is
> something for SuSE (Mandriva has one). And one can, of course,
> compile a copy oneself.
>
> > Can I find a tutorial online explaining how to run commands/evaluate
> > expressions/and so on within a regexp?
>
> You could probably do
>
> M-: (query-replace-regexp "\\([0-9]+\\)" '(replace-eval-replacement format "%d" (1+ (string-to-number (match-string 1))))) RET
>
> But it is not exactly kosher.
>
> --
> David Kastrup, Kriemhildstr. 15, 44793 Bochum
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2006-04-12 16:12 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-04-11 16:04 query-replace-regexp number number+1 rturrado
2006-04-11 16:14 ` David Kastrup
2006-04-11 18:36 ` rturrado
2006-04-11 19:05 ` David Kastrup
2006-04-12 5:46 ` B. T. Raven
2006-04-12 8:22 ` Thien-Thi Nguyen
2006-04-12 8:47 ` David Kastrup
2006-04-12 16:12 ` rturrado
2006-04-12 14:52 ` Giorgos Keramidas
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).