all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Advanced query-replace-regexp in code
@ 2009-05-27 14:03 Nordlöw
  2009-05-27 14:32 ` Pascal J. Bourguignon
                   ` (3 more replies)
  0 siblings, 4 replies; 14+ messages in thread
From: Nordlöw @ 2009-05-27 14:03 UTC (permalink / raw)
  To: help-gnu-emacs

I can't get the following example to work programmatically.

  M-x replace-regexp
  Replace regexp:  \(\w+\)
  Replace regexp with:  \,(capitalize \1)

Is this only possible in interactive query-replace?
If so should I use a combination of while(), looking-at(), re-search-
forward(), replace-match(), match-string() etc.

Thanks in advance,
Per Nordlöw


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-27 14:03 Advanced query-replace-regexp in code Nordlöw
@ 2009-05-27 14:32 ` Pascal J. Bourguignon
  2009-05-28  1:19 ` Barry Margolin
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Pascal J. Bourguignon @ 2009-05-27 14:32 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> I can't get the following example to work programmatically.
>
>   M-x replace-regexp
>   Replace regexp:  \(\w+\)
>   Replace regexp with:  \,(capitalize \1)
>
> Is this only possible in interactive query-replace?
> If so should I use a combination of while(), looking-at(), re-search-
> forward(), replace-match(), match-string() etc.

Yes, for non-interactive use, it's advised to do

        (while (re-search-forward ...)
           (let ((m1 (match-string ...))
                  ...)
               (delete-region ...)
               (insert (format "%s" (your-sexp m1 ...)))))

-- 
__Pascal Bourguignon__


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-27 14:03 Advanced query-replace-regexp in code Nordlöw
  2009-05-27 14:32 ` Pascal J. Bourguignon
@ 2009-05-28  1:19 ` Barry Margolin
  2009-05-28  4:57   ` Teemu Likonen
  2009-05-28 19:50 ` harven
  2009-08-02 16:19 ` David Kastrup
  3 siblings, 1 reply; 14+ messages in thread
From: Barry Margolin @ 2009-05-28  1:19 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 872 bytes --]

In article 
<f3f72811-6305-4f62-98af-3a86d6646e9f@e20g2000vbc.googlegroups.com>,
 Nordlöw <per.nordlow@gmail.com> wrote:

> I can't get the following example to work programmatically.
> 
>   M-x replace-regexp
>   Replace regexp:  \(\w+\)
>   Replace regexp with:  \,(capitalize \1)
> 
> Is this only possible in interactive query-replace?
> If so should I use a combination of while(), looking-at(), re-search-
> forward(), replace-match(), match-string() etc.

Yes.  Notice that the documentation of replace-regexp says:

In interactive calls, the replacement text may contain `\,'

It's a special extension just for interactive use.  I'm not sure why it 
was restricted this way.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-28  1:19 ` Barry Margolin
@ 2009-05-28  4:57   ` Teemu Likonen
  2009-05-28  7:04     ` Barry Margolin
  0 siblings, 1 reply; 14+ messages in thread
From: Teemu Likonen @ 2009-05-28  4:57 UTC (permalink / raw)
  To: help-gnu-emacs

On 2009-05-27 21:19 (-0400), Barry Margolin wrote:

> In interactive calls, the replacement text may contain `\,'
>
> It's a special extension just for interactive use. I'm not sure why it
> was restricted this way.

I think in Lisp programs it's much easier and more problem-free to
evaluate Lisp expressions normal Lisp-way than to write expressions
inside strings and use the "\\,expression" form there.

Interactive replace string

    \,(capitalize \1)

can be written as

    (capitalize (match-string 1))

in programs. With functions like "concat" or "format" the latter
expression can be concatenated with the surrounding replace string.


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-28  4:57   ` Teemu Likonen
@ 2009-05-28  7:04     ` Barry Margolin
  2009-05-28 19:36       ` Johan Bockgård
       [not found]       ` <mailman.8013.1243539406.31690.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 14+ messages in thread
From: Barry Margolin @ 2009-05-28  7:04 UTC (permalink / raw)
  To: help-gnu-emacs

In article <878wkhakp1.fsf@iki.fi>, Teemu Likonen <tlikonen@iki.fi> 
wrote:

> On 2009-05-27 21:19 (-0400), Barry Margolin wrote:
> 
> > In interactive calls, the replacement text may contain `\,'
> >
> > It's a special extension just for interactive use. I'm not sure why it
> > was restricted this way.
> 
> I think in Lisp programs it's much easier and more problem-free to
> evaluate Lisp expressions normal Lisp-way than to write expressions
> inside strings and use the "\\,expression" form there.
> 
> Interactive replace string
> 
>     \,(capitalize \1)
> 
> can be written as
> 
>     (capitalize (match-string 1))
> 
> in programs. With functions like "concat" or "format" the latter
> expression can be concatenated with the surrounding replace string.

In that case, why allow programs to call replace-regexp in the first 
place?  If they can call it, why shouldn't they be able to use all the 
features?

replace-regexp also does the searching and looping for you.  So instead 
of a simple, concise one-liner, you have to code everything, with the 
potential to screw up.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-28  7:04     ` Barry Margolin
@ 2009-05-28 19:36       ` Johan Bockgård
       [not found]       ` <mailman.8013.1243539406.31690.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 14+ messages in thread
From: Johan Bockgård @ 2009-05-28 19:36 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

> In that case, why allow programs to call replace-regexp in the first
> place? If they can call it, why shouldn't they be able to use all the
> features?

They shouldn't call it:

    This function is usually the wrong thing to use in a Lisp program.
    What you probably want is a loop like this:
      (while (re-search-forward REGEXP nil t)
        (replace-match TO-STRING nil nil))
    which will run faster and will not set the mark or print anything.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-27 14:03 Advanced query-replace-regexp in code Nordlöw
  2009-05-27 14:32 ` Pascal J. Bourguignon
  2009-05-28  1:19 ` Barry Margolin
@ 2009-05-28 19:50 ` harven
  2009-08-02 16:19 ` David Kastrup
  3 siblings, 0 replies; 14+ messages in thread
From: harven @ 2009-05-28 19:50 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> I can't get the following example to work programmatically.
>
>   M-x replace-regexp
>   Replace regexp:  \(\w+\)
>   Replace regexp with:  \,(capitalize \1)
>
> Is this only possible in interactive query-replace?
> If so should I use a combination of while(), looking-at(), re-search-
> forward(), replace-match(), match-string() etc.
>
> Thanks in advance,
> Per Nordlöw

(defun my-cap ()
  (interactive)
  (while (re-search-forward "\\w+" nil t)
     (replace-match (capitalize (match-string 0)) nil t)))


From the doc string of replace-regexp:

[replace-regexp] is usually the wrong thing to use in a Lisp program.
What you probably want is a loop like this:
  (while (re-search-forward REGEXP nil t)
    (replace-match TO-STRING nil nil))
which will run faster and will not set the mark or print anything.

hope that helps


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
       [not found]       ` <mailman.8013.1243539406.31690.help-gnu-emacs@gnu.org>
@ 2009-05-29  2:14         ` Barry Margolin
  2009-05-30  6:34           ` Nordlöw
                             ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Barry Margolin @ 2009-05-29  2:14 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.8013.1243539406.31690.help-gnu-emacs@gnu.org>,
 bojohan+news@dd.chalmers.se (Johan Bockgard) wrote:

> Barry Margolin <barmar@alum.mit.edu> writes:
> 
> > In that case, why allow programs to call replace-regexp in the first
> > place? If they can call it, why shouldn't they be able to use all the
> > features?
> 
> They shouldn't call it:
> 
>     This function is usually the wrong thing to use in a Lisp program.
>     What you probably want is a loop like this:
>       (while (re-search-forward REGEXP nil t)
>         (replace-match TO-STRING nil nil))
>     which will run faster and will not set the mark or print anything.

Regardless of whether they should or shouldn't use it, it still seems 
strange that the function interprets the second argument differently 
depending on whether it's use interactively or programmatically.  If it 
shouldn't be used programmatically, why is it important that this mode 
*not* recognize \,<expr>?

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-29  2:14         ` Barry Margolin
@ 2009-05-30  6:34           ` Nordlöw
  2009-05-30 18:55           ` Johan Bockgård
       [not found]           ` <mailman.8166.1243709727.31690.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 14+ messages in thread
From: Nordlöw @ 2009-05-30  6:34 UTC (permalink / raw)
  To: help-gnu-emacs

On May 29, 4:14 am, Barry Margolin <bar...@alum.mit.edu> wrote:
> In article <mailman.8013.1243539406.31690.help-gnu-em...@gnu.org>,
>  bojohan+n...@dd.chalmers.se (Johan Bockgard) wrote:
>
> > Barry Margolin <bar...@alum.mit.edu> writes:
>
> > > In that case, why allow programs to call replace-regexp in the first
> > > place? If they can call it, why shouldn't they be able to use all the
> > > features?
>
> > They shouldn't call it:
>
> >     This function is usually the wrong thing to use in a Lisp program.
> >     What you probably want is a loop like this:
> >       (while (re-search-forward REGEXP nil t)
> >         (replace-match TO-STRING nil nil))
> >     which will run faster and will not set the mark or print anything.
>
> Regardless of whether they should or shouldn't use it, it still seems
> strange that the function interprets the second argument differently
> depending on whether it's use interactively or programmatically.  If it
> shouldn't be used programmatically, why is it important that this mode
> *not* recognize \,<expr>?
>
> --
> Barry Margolin, bar...@alum.mit.edu
> Arlington, MA
> *** PLEASE post questions in newsgroups, not directly to me ***
> *** PLEASE don't copy me on replies, I'll read them in the group ***

I agree, this makes no sense as this is powerful feature, eventhough
it runs slowly.

/Nordlöw


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-29  2:14         ` Barry Margolin
  2009-05-30  6:34           ` Nordlöw
@ 2009-05-30 18:55           ` Johan Bockgård
       [not found]           ` <mailman.8166.1243709727.31690.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 14+ messages in thread
From: Johan Bockgård @ 2009-05-30 18:55 UTC (permalink / raw)
  To: help-gnu-emacs

Barry Margolin <barmar@alum.mit.edu> writes:

> Regardless of whether they should or shouldn't use it, it still seems
> strange that the function interprets the second argument differently
> depending on whether it's use interactively or programmatically.

It doesn't; the magic is in the interactive form.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
       [not found]           ` <mailman.8166.1243709727.31690.help-gnu-emacs@gnu.org>
@ 2009-05-31  1:57             ` Barry Margolin
  0 siblings, 0 replies; 14+ messages in thread
From: Barry Margolin @ 2009-05-31  1:57 UTC (permalink / raw)
  To: help-gnu-emacs

In article <mailman.8166.1243709727.31690.help-gnu-emacs@gnu.org>,
 bojohan+news@dd.chalmers.se (Johan Bockgard) wrote:

> Barry Margolin <barmar@alum.mit.edu> writes:
> 
> > Regardless of whether they should or shouldn't use it, it still seems
> > strange that the function interprets the second argument differently
> > depending on whether it's use interactively or programmatically.
> 
> It doesn't; the magic is in the interactive form.

Which seems like a silly place to put it, when it could just as easily 
be in the main body of the function.

-- 
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-05-27 14:03 Advanced query-replace-regexp in code Nordlöw
                   ` (2 preceding siblings ...)
  2009-05-28 19:50 ` harven
@ 2009-08-02 16:19 ` David Kastrup
  2009-08-02 17:14   ` Thierry Volpiatto
  3 siblings, 1 reply; 14+ messages in thread
From: David Kastrup @ 2009-08-02 16:19 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> writes:

> I can't get the following example to work programmatically.
>
>   M-x replace-regexp
>   Replace regexp:  \(\w+\)
>   Replace regexp with:  \,(capitalize \1)
>
> Is this only possible in interactive query-replace?
> If so should I use a combination of while(), looking-at(), re-search-
> forward(), replace-match(), match-string() etc.

Do it interactively, then use

C-x ESC ESC

in order to figure out how to this non-interactively.

-- 
David Kastrup


^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-08-02 16:19 ` David Kastrup
@ 2009-08-02 17:14   ` Thierry Volpiatto
  2009-08-02 20:25     ` Thierry Volpiatto
  0 siblings, 1 reply; 14+ messages in thread
From: Thierry Volpiatto @ 2009-08-02 17:14 UTC (permalink / raw)
  To: help-gnu-emacs

David Kastrup <dak@gnu.org> writes:

> Nordlöw <per.nordlow@gmail.com> writes:
>
>> I can't get the following example to work programmatically.
>>
>>   M-x replace-regexp
>>   Replace regexp:  \(\w+\)
>>   Replace regexp with:  \,(capitalize \1)
>>
>> Is this only possible in interactive query-replace?
>> If so should I use a combination of while(), looking-at(), re-search-
>> forward(), replace-match(), match-string() etc.
>
> Do it interactively, then use
>
> C-x ESC ESC
>
> in order to figure out how to this non-interactively.

An example of how to do that could be that:

,----[ Upcase all buffer ]
| (save-excursion
|   (goto-char (point-min))
|   (while (not (eobp))
|     (when (re-search-forward "\\w+" (point-max) t)
|       (replace-match (upcase (match-string 0))))))
`----

-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Advanced query-replace-regexp in code
  2009-08-02 17:14   ` Thierry Volpiatto
@ 2009-08-02 20:25     ` Thierry Volpiatto
  0 siblings, 0 replies; 14+ messages in thread
From: Thierry Volpiatto @ 2009-08-02 20:25 UTC (permalink / raw)
  To: help-gnu-emacs

Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:

> David Kastrup <dak@gnu.org> writes:
>
>> Nordlöw <per.nordlow@gmail.com> writes:
>>
>>> I can't get the following example to work programmatically.
>>>
>>>   M-x replace-regexp
>>>   Replace regexp:  \(\w+\)
>>>   Replace regexp with:  \,(capitalize \1)
>>>
>>> Is this only possible in interactive query-replace?
>>> If so should I use a combination of while(), looking-at(), re-search-
>>> forward(), replace-match(), match-string() etc.
>>
>> Do it interactively, then use
>>
>> C-x ESC ESC
>>
>> in order to figure out how to this non-interactively.
>
> An example of how to do that could be that:
>
> ,----[ Upcase all buffer ]
> | (save-excursion
> |   (goto-char (point-min))
> |   (while (not (eobp))
> |     (when (re-search-forward "\\w+" (point-max) t)
> |       (replace-match (upcase (match-string 0))))))
> `----

After testing the above code, (don't break at eob) i found that better:

,----
| (save-excursion
|   (goto-char (point-min))
|   (while (re-search-forward "\\w+" nil t)
|       (replace-match (upcase (match-string 0)))))
`----


-- 
A + Thierry Volpiatto
Location: Saint-Cyr-Sur-Mer - France





^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2009-08-02 20:25 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-05-27 14:03 Advanced query-replace-regexp in code Nordlöw
2009-05-27 14:32 ` Pascal J. Bourguignon
2009-05-28  1:19 ` Barry Margolin
2009-05-28  4:57   ` Teemu Likonen
2009-05-28  7:04     ` Barry Margolin
2009-05-28 19:36       ` Johan Bockgård
     [not found]       ` <mailman.8013.1243539406.31690.help-gnu-emacs@gnu.org>
2009-05-29  2:14         ` Barry Margolin
2009-05-30  6:34           ` Nordlöw
2009-05-30 18:55           ` Johan Bockgård
     [not found]           ` <mailman.8166.1243709727.31690.help-gnu-emacs@gnu.org>
2009-05-31  1:57             ` Barry Margolin
2009-05-28 19:50 ` harven
2009-08-02 16:19 ` David Kastrup
2009-08-02 17:14   ` Thierry Volpiatto
2009-08-02 20:25     ` Thierry Volpiatto

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.