unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Interative batch query-replace question
@ 2007-11-29  3:08 Ke Lu
  2007-11-29  5:56 ` Ke Lu
  2007-11-29  6:54 ` Interative batch query-replace question Andreas Röhler
  0 siblings, 2 replies; 22+ messages in thread
From: Ke Lu @ 2007-11-29  3:08 UTC (permalink / raw)
  To: help-gnu-emacs

I want to do a Interative batch query-replace which looks like:
(defun a-batch-query-relace()
(interactive)
(query-replace "jp.co" "jp.co2")
(query-replace "java" "java2")
.....
)
But only first query-replace be excuted,I would like  to know why
and how to solve it.

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

* Re: Interative batch query-replace question
  2007-11-29  3:08 Interative batch query-replace question Ke Lu
@ 2007-11-29  5:56 ` Ke Lu
  2007-11-29  7:37   ` Andreas Röhler
       [not found]   ` <mailman.4291.1196321879.18990.help-gnu-emacs@gnu.org>
  2007-11-29  6:54 ` Interative batch query-replace question Andreas Röhler
  1 sibling, 2 replies; 22+ messages in thread
From: Ke Lu @ 2007-11-29  5:56 UTC (permalink / raw)
  To: help-gnu-emacs

Sorry, I forgot to move point to the begining of buffer,
It is ok now.

But dired-do-query-replace-regexp still have some peoblem.
My function looks like:
(defadvice query-replace-regexp (before
move-start-after-query-replace-regexp activate)
  (goto-char (point-min)))
  
  (defun batch-query-replace-regexp()
    (interactive)
      (switch-to-buffer "*Find*")
      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
      (switch-to-buffer "*Find*")
       (dired-do-query-replace-regexp "java" "java2")
  )

But the second one can't be executed and there is not any
error message.
What is the reason?

Ke Lu <lu@luxdo.jp> writes:

> I want to do a Interative batch query-replace which looks like:
> (defun a-batch-query-relace()
> (interactive)
> (query-replace "jp.co" "jp.co2")
> (query-replace "java" "java2")
> .....
> )
> But only first query-replace be excuted,I would like  to know why
> and how to solve it.

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

* Re: Interative batch query-replace question
  2007-11-29  3:08 Interative batch query-replace question Ke Lu
  2007-11-29  5:56 ` Ke Lu
@ 2007-11-29  6:54 ` Andreas Röhler
  1 sibling, 0 replies; 22+ messages in thread
From: Andreas Röhler @ 2007-11-29  6:54 UTC (permalink / raw)
  To: help-gnu-emacs

Am Donnerstag, 29. November 2007 04:08 schrieb Ke Lu:
> I want to do a Interative batch query-replace which looks like:
> (defun a-batch-query-relace()
> (interactive)
> (query-replace "jp.co" "jp.co2")
> (query-replace "java" "java2")
> .....
> )
> But only first query-replace be excuted,I would like  to know why
> and how to solve it.
> _______________________________________________

If it's enough to replace one by one, then should help
always starting from the beginning.

Introduce

(goto-char (point-min)) 

before each loop

Andreas Röhler

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

* Re: Interative batch query-replace question
  2007-11-29  5:56 ` Ke Lu
@ 2007-11-29  7:37   ` Andreas Röhler
       [not found]   ` <mailman.4291.1196321879.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 22+ messages in thread
From: Andreas Röhler @ 2007-11-29  7:37 UTC (permalink / raw)
  To: help-gnu-emacs

Am Donnerstag, 29. November 2007 06:56 schrieb Ke Lu:
> Sorry, I forgot to move point to the begining of buffer,
> It is ok now.
>
> But dired-do-query-replace-regexp still have some peoblem.
> My function looks like:
> (defadvice query-replace-regexp (before
> move-start-after-query-replace-regexp activate)
>   (goto-char (point-min)))
>
>   (defun batch-query-replace-regexp()
>     (interactive)
>       (switch-to-buffer "*Find*")
>       (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>       (switch-to-buffer "*Find*")
>        (dired-do-query-replace-regexp "java" "java2")
>   )
>
> But the second one can't be executed and there is not any
> error message.
> What is the reason?
>
> Ke Lu <lu@luxdo.jp> writes:
> > I want to do a Interative batch query-replace which looks like:
> > (defun a-batch-query-relace()
> > (interactive)
> > (query-replace "jp.co" "jp.co2")
> > (query-replace "java" "java2")
> > .....
> > )
> > But only first query-replace be excuted,I would like  to know why
> > and how to solve it.
>
> _______________________________________________

Same problem?

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

* Re: Interative batch query-replace question
       [not found]   ` <mailman.4291.1196321879.18990.help-gnu-emacs@gnu.org>
@ 2007-11-29  9:15     ` Ke Lu
  2007-11-29 10:18       ` Ke Lu
  0 siblings, 1 reply; 22+ messages in thread
From: Ke Lu @ 2007-11-29  9:15 UTC (permalink / raw)
  To: help-gnu-emacs

Yes,Only the first one is executed.

Andreas Röhler <andreas.roehler@online.de> writes:

> Am Donnerstag, 29. November 2007 06:56 schrieb Ke Lu:
>> Sorry, I forgot to move point to the begining of buffer,
>> It is ok now.
>>
>> But dired-do-query-replace-regexp still have some peoblem.
>> My function looks like:
>> (defadvice query-replace-regexp (before
>> move-start-after-query-replace-regexp activate)
>>   (goto-char (point-min)))
>>
>>   (defun batch-query-replace-regexp()
>>     (interactive)
>>       (switch-to-buffer "*Find*")
>>       (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>>       (switch-to-buffer "*Find*")
>>        (dired-do-query-replace-regexp "java" "java2")
>>   )
>>
>> But the second one can't be executed and there is not any
>> error message.
>> What is the reason?
>>
>> Ke Lu <lu@luxdo.jp> writes:
>> > I want to do a Interative batch query-replace which looks like:
>> > (defun a-batch-query-relace()
>> > (interactive)
>> > (query-replace "jp.co" "jp.co2")
>> > (query-replace "java" "java2")
>> > .....
>> > )
>> > But only first query-replace be excuted,I would like  to know why
>> > and how to solve it.
>>
>> _______________________________________________
>
> Same problem?

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

* Re: Interative batch query-replace question
  2007-11-29  9:15     ` Ke Lu
@ 2007-11-29 10:18       ` Ke Lu
  2007-11-29 20:55         ` Andreas Röhler
       [not found]         ` <mailman.4324.1196369791.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 22+ messages in thread
From: Ke Lu @ 2007-11-29 10:18 UTC (permalink / raw)
  To: help-gnu-emacs

The problem is:
 (defun batch-query-replace-regexp()
   (interactive)
     (switch-to-buffer "*Find*")
     (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
->can't reach here     (switch-to-buffer "*Find*")
      (dired-do-query-replace-regexp "java" "java2")
 )

Ke Lu <lu@luxdo.jp> writes:

> Yes,Only the first one is executed.
>
> Andreas Röhler <andreas.roehler@online.de> writes:
>
>> Am Donnerstag, 29. November 2007 06:56 schrieb Ke Lu:
>>> Sorry, I forgot to move point to the begining of buffer,
>>> It is ok now.
>>>
>>> But dired-do-query-replace-regexp still have some peoblem.
>>> My function looks like:
>>> (defadvice query-replace-regexp (before
>>> move-start-after-query-replace-regexp activate)
>>>   (goto-char (point-min)))
>>>
>>>   (defun batch-query-replace-regexp()
>>>     (interactive)
>>>       (switch-to-buffer "*Find*")
>>>       (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>>>       (switch-to-buffer "*Find*")
>>>        (dired-do-query-replace-regexp "java" "java2")
>>>   )
>>>
>>> But the second one can't be executed and there is not any
>>> error message.
>>> What is the reason?
>>>
>>> Ke Lu <lu@luxdo.jp> writes:
>>> > I want to do a Interative batch query-replace which looks like:
>>> > (defun a-batch-query-relace()
>>> > (interactive)
>>> > (query-replace "jp.co" "jp.co2")
>>> > (query-replace "java" "java2")
>>> > .....
>>> > )
>>> > But only first query-replace be excuted,I would like  to know why
>>> > and how to solve it.
>>>
>>> _______________________________________________
>>
>> Same problem?

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

* Re: Interative batch query-replace question
  2007-11-29 10:18       ` Ke Lu
@ 2007-11-29 20:55         ` Andreas Röhler
       [not found]         ` <mailman.4324.1196369791.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 22+ messages in thread
From: Andreas Röhler @ 2007-11-29 20:55 UTC (permalink / raw)
  To: help-gnu-emacs


If you could give a short description of your task,
that may help to find the code.

AFAIK `dired-' is designed to be called from the
dired-buffer. If you do query-replace from a program,
you probably should use other functions.


Andreas Röhler

> The problem is:
>  (defun batch-query-replace-regexp()
>    (interactive)
>      (switch-to-buffer "*Find*")
>      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
> ->can't reach here     (switch-to-buffer "*Find*")
>       (dired-do-query-replace-regexp "java" "java2")
>  )
>
> Ke Lu <lu@luxdo.jp> writes:
> > Yes,Only the first one is executed.
> >
> > Andreas Röhler <andreas.roehler@online.de> writes:
> >> Am Donnerstag, 29. November 2007 06:56 schrieb Ke Lu:
> >>> Sorry, I forgot to move point to the begining of buffer,
> >>> It is ok now.
> >>>
> >>> But dired-do-query-replace-regexp still have some peoblem.
> >>> My function looks like:
> >>> (defadvice query-replace-regexp (before
> >>> move-start-after-query-replace-regexp activate)
> >>>   (goto-char (point-min)))
> >>>
> >>>   (defun batch-query-replace-regexp()
> >>>     (interactive)
> >>>       (switch-to-buffer "*Find*")
> >>>       (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
> >>>       (switch-to-buffer "*Find*")
> >>>        (dired-do-query-replace-regexp "java" "java2")
> >>>   )
> >>>
> >>> But the second one can't be executed and there is not any
> >>> error message.
> >>> What is the reason?
> >>>
> >>> Ke Lu <lu@luxdo.jp> writes:
> >>> > I want to do a Interative batch query-replace which looks like:
> >>> > (defun a-batch-query-relace()
> >>> > (interactive)
> >>> > (query-replace "jp.co" "jp.co2")
> >>> > (query-replace "java" "java2")
> >>> > .....
> >>> > )
> >>> > But only first query-replace be excuted,I would like  to know why
> >>> > and how to solve it.
> >>>
> >>> _______________________________________________
> >>
> >> Same problem?
>
> _______________________________________________
> help-gnu-emacs mailing list
> help-gnu-emacs@gnu.org
> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

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

* Re: Interative batch query-replace question
       [not found]         ` <mailman.4324.1196369791.18990.help-gnu-emacs@gnu.org>
@ 2007-11-29 21:16           ` Joel J. Adamson
  2007-11-30  1:58           ` Ke Lu
  1 sibling, 0 replies; 22+ messages in thread
From: Joel J. Adamson @ 2007-11-29 21:16 UTC (permalink / raw)
  To: help-gnu-emacs

Andreas Röhler <andreas.roehler@online.de> writes:

> If you could give a short description of your task,
> that may help to find the code.
>
> AFAIK `dired-' is designed to be called from the
> dired-buffer. If you do query-replace from a program,
> you probably should use other functions.
>
>
> Andreas Röhler
>
>> The problem is:
>>  (defun batch-query-replace-regexp()
>>    (interactive)
>>      (switch-to-buffer "*Find*")
>>      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>> ->can't reach here     (switch-to-buffer "*Find*")
>>       (dired-do-query-replace-regexp "java" "java2")
>>  )
>>
>> Ke Lu <lu@luxdo.jp> writes:

I'm coming in late on this convo, but AFAIK dired and ibuffer do their
own "batch" query-replace-regexp functions.

Joel
-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

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

* Re: Interative batch query-replace question
       [not found]         ` <mailman.4324.1196369791.18990.help-gnu-emacs@gnu.org>
  2007-11-29 21:16           ` Joel J. Adamson
@ 2007-11-30  1:58           ` Ke Lu
  2007-11-30  8:08             ` Andreas Röhler
       [not found]             ` <mailman.4336.1196410171.18990.help-gnu-emacs@gnu.org>
  1 sibling, 2 replies; 22+ messages in thread
From: Ke Lu @ 2007-11-30  1:58 UTC (permalink / raw)
  To: help-gnu-emacs

My task is:
1. Use find-dired to Find all java file in a fold.
2. Mark all file in "*Find*" buffer
3. call batch-query-replace-regexp.
(I must do it, because I have about 50 pattern to query-replace-regexp
and I must do it in interative way to confirm if it is right)

>>  (defun batch-query-replace-regexp()
>>    (interactive)
>>      (switch-to-buffer "*Find*")
>>      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>> ->can't reach here     (switch-to-buffer "*Find*")
>>       (dired-do-query-replace-regexp "java" "java2")
...a lot of pattern to query-replace-regexp
>>  )
>>
Andreas Röhler <andreas.roehler@online.de> writes:

> If you could give a short description of your task,
> that may help to find the code.
>
> AFAIK `dired-' is designed to be called from the
> dired-buffer. If you do query-replace from a program,
> you probably should use other functions.
>
>
> Andreas Röhler
>
>> The problem is:
>>  (defun batch-query-replace-regexp()
>>    (interactive)
>>      (switch-to-buffer "*Find*")
>>      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>> ->can't reach here     (switch-to-buffer "*Find*")
>>       (dired-do-query-replace-regexp "java" "java2")
>>  )
>>
>> Ke Lu <lu@luxdo.jp> writes:
>> > Yes,Only the first one is executed.
>> >
>> > Andreas Röhler <andreas.roehler@online.de> writes:
>> >> Am Donnerstag, 29. November 2007 06:56 schrieb Ke Lu:
>> >>> Sorry, I forgot to move point to the begining of buffer,
>> >>> It is ok now.
>> >>>
>> >>> But dired-do-query-replace-regexp still have some peoblem.
>> >>> My function looks like:
>> >>> (defadvice query-replace-regexp (before
>> >>> move-start-after-query-replace-regexp activate)
>> >>>   (goto-char (point-min)))
>> >>>
>> >>>   (defun batch-query-replace-regexp()
>> >>>     (interactive)
>> >>>       (switch-to-buffer "*Find*")
>> >>>       (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>> >>>       (switch-to-buffer "*Find*")
>> >>>        (dired-do-query-replace-regexp "java" "java2")
>> >>>   )
>> >>>
>> >>> But the second one can't be executed and there is not any
>> >>> error message.
>> >>> What is the reason?
>> >>>
>> >>> Ke Lu <lu@luxdo.jp> writes:
>> >>> > I want to do a Interative batch query-replace which looks like:
>> >>> > (defun a-batch-query-relace()
>> >>> > (interactive)
>> >>> > (query-replace "jp.co" "jp.co2")
>> >>> > (query-replace "java" "java2")
>> >>> > .....
>> >>> > )
>> >>> > But only first query-replace be excuted,I would like  to know why
>> >>> > and how to solve it.
>> >>>
>> >>> _______________________________________________
>> >>
>> >> Same problem?
>>
>> _______________________________________________
>> help-gnu-emacs mailing list
>> help-gnu-emacs@gnu.org
>> http://lists.gnu.org/mailman/listinfo/help-gnu-emacs

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

* Re: Interative batch query-replace question
  2007-11-30  1:58           ` Ke Lu
@ 2007-11-30  8:08             ` Andreas Röhler
       [not found]             ` <mailman.4336.1196410171.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 22+ messages in thread
From: Andreas Röhler @ 2007-11-30  8:08 UTC (permalink / raw)
  To: help-gnu-emacs

Am Freitag, 30. November 2007 02:58 schrieb Ke Lu:
> jp\\.co

To question is: Must you do that just once and only
renaming is at stake? If yes, I'd choose `wdired'.

Should you have the same renamings to perform
repeatedly and/or other tasks than renaming with this
files--backups, contents changes or whatever--I would
write a skript aside from dired.

BTW the *Find*" buffer usually is
write-protected. Editing them looks special for me.

To get the appropriate files in directory I'd choose
something like

(let ((files (directory-files (expand-file-name (substitute-in-file-name dir)) 
t "jp\\.co$")))
 (while files
 (let ((file (car files)))
 (when (file-exists-p file)

 DO SOMETHING
 (setq files (cdr files))))))

;;;;;;;

For DO SOMETHING

you could proceed

(if      (string-match ...

     ;; your interactive form
    (y-or-n-p
		   (rename-file ...

				DO MORE THINGS

;;;;;;;;;;;

HTH

Andreas Röhler

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

* Re: Interative batch query-replace question
       [not found]             ` <mailman.4336.1196410171.18990.help-gnu-emacs@gnu.org>
@ 2007-11-30  9:25               ` Ke Lu
  2007-11-30 14:25                 ` Joel J. Adamson
                                   ` (2 more replies)
  0 siblings, 3 replies; 22+ messages in thread
From: Ke Lu @ 2007-11-30  9:25 UTC (permalink / raw)
  To: help-gnu-emacs

I want do it only once.
I just want to call dired-do-query-replace-regexp
many times in a function.
The thing is the only the first call is excuted,
Don't you feel Is it strange?
Or it is the bug of dired?

Andreas Röhler <andreas.roehler@online.de> writes:

> Am Freitag, 30. November 2007 02:58 schrieb Ke Lu:
>> jp\\.co
>
> To question is: Must you do that just once and only
> renaming is at stake? If yes, I'd choose `wdired'.
>
> Should you have the same renamings to perform
> repeatedly and/or other tasks than renaming with this
> files--backups, contents changes or whatever--I would
> write a skript aside from dired.
>
> BTW the *Find*" buffer usually is
> write-protected. Editing them looks special for me.
>
> To get the appropriate files in directory I'd choose
> something like
>
> (let ((files (directory-files (expand-file-name (substitute-in-file-name dir)) 
> t "jp\\.co$")))
>  (while files
>  (let ((file (car files)))
>  (when (file-exists-p file)
>
>  DO SOMETHING
>  (setq files (cdr files))))))
>
> ;;;;;;;
>
> For DO SOMETHING
>
> you could proceed
>
> (if      (string-match ...
>
>      ;; your interactive form
>     (y-or-n-p
> 		   (rename-file ...
>
> 				DO MORE THINGS
>
> ;;;;;;;;;;;
>
> HTH
>
> Andreas Röhler

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

* Re: Interative batch query-replace question
  2007-11-30  9:25               ` Ke Lu
@ 2007-11-30 14:25                 ` Joel J. Adamson
  2007-11-30 19:33                 ` Andreas Röhler
       [not found]                 ` <mailman.4365.1196451256.18990.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 22+ messages in thread
From: Joel J. Adamson @ 2007-11-30 14:25 UTC (permalink / raw)
  To: help-gnu-emacs

Ke Lu <lu@luxdo.jp> writes:

> I want do it only once.
> I just want to call dired-do-query-replace-regexp
> many times in a function.

That sounds like it calls for an iteration, e.g. `dolist'.

Joel
-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

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

* Re: Interative batch query-replace question
  2007-11-30  9:25               ` Ke Lu
  2007-11-30 14:25                 ` Joel J. Adamson
@ 2007-11-30 19:33                 ` Andreas Röhler
       [not found]                 ` <mailman.4365.1196451256.18990.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 22+ messages in thread
From: Andreas Röhler @ 2007-11-30 19:33 UTC (permalink / raw)
  To: help-gnu-emacs

Am Freitag, 30. November 2007 10:25 schrieb Ke Lu:
> I want do it only once.

Then probably you are right with dired.

Do want to change filenames in a directory or the contents of files in that 
directory?

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

* Re: Interative batch query-replace question
       [not found]                 ` <mailman.4365.1196451256.18990.help-gnu-emacs@gnu.org>
@ 2007-12-01  1:00                   ` Ke Lu
  2007-12-01  8:19                     ` Andreas Röhler
       [not found]                     ` <mailman.4382.1196497198.18990.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 22+ messages in thread
From: Ke Lu @ 2007-12-01  1:00 UTC (permalink / raw)
  To: help-gnu-emacs

> Do want to change filenames in a directory or the contents of files in that 
> directory?
I don't want to change filenames. 
I want to change contents of files in a directory.

Andreas Röhler <andreas.roehler@online.de> writes:

> Am Freitag, 30. November 2007 10:25 schrieb Ke Lu:
>> I want do it only once.
>
> Then probably you are right with dired.
>
> Do want to change filenames in a directory or the contents of files in that 
> directory?

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

* Re: Interative batch query-replace question
  2007-12-01  1:00                   ` Ke Lu
@ 2007-12-01  8:19                     ` Andreas Röhler
       [not found]                     ` <mailman.4382.1196497198.18990.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 22+ messages in thread
From: Andreas Röhler @ 2007-12-01  8:19 UTC (permalink / raw)
  To: help-gnu-emacs

Am Samstag, 1. Dezember 2007 02:00 schrieb Ke Lu:
> > Do want to change filenames in a directory or the contents of files in
> > that directory?
>
> I don't want to change filenames.
> I want to change contents of files in a directory.
>

So let's start with the first task:

If you want to add a "2" at all endings in that directory,

as  "jp.co" => "jp.co2" let's assume,

you can perform this

with

M-x wdired-change-to-wdired-mode

M-x query-replace-regexp $ RET 2 !

If everything looks fine, do C-c C-c to write down your
changing or quit (with C-c ESC or C-d).

With quit nothing is changed.

Andreas Röhler

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

* Re: Interative batch query-replace question
       [not found]                     ` <mailman.4382.1196497198.18990.help-gnu-emacs@gnu.org>
@ 2007-12-01  9:11                       ` Ke Lu
  2007-12-01 15:19                         ` Xah Lee
                                           ` (2 more replies)
  0 siblings, 3 replies; 22+ messages in thread
From: Ke Lu @ 2007-12-01  9:11 UTC (permalink / raw)
  To: help-gnu-emacs

Perhaps my explain is not quite enough.

I want to query-replace some files.(not filename)

1. Use find-dired to Find some files.(M-x find-dired)
In "*File" buffer,it looks like:
  /home/lu/workspace/:
  find . \( -name "*.java" \) -exec ls -ld \{\} \;
  -rw-r--r--  1 lu lu 215  1月 21  2003 ejbdemo/DemoEntity.java
  -rw-r--r--  1 lu lu 178  1月 21  2003 ejbdemo/DemoSession.java
  -rw-r--r--  1 lu lu 146  1月 21  2003 ejbdemo/DemoSessionLocal.java
  -rw-r--r--  1 lu lu 573  1月 21  2003 ejbdemo/DemoSessionEJB.java
  -rw-r--r--  1 lu lu 194  1月 21  2003 ejbdemo/DemoSessionHome.java
  
2. Mark some file in "*Find*" buffer
In "*File" buffer,it looks like:
  /home/lu/workspace/:
  find . \( -name "*.java" \) -exec ls -ld \{\} \;
  -rw-r--r--  1 lu lu 215  1月 21  2003 ejbdemo/DemoEntity.java
* -rw-r--r--  1 lu lu 178  1月 21  2003 ejbdemo/DemoSession.java
* -rw-r--r--  1 lu lu 146  1月 21  2003 ejbdemo/DemoSessionLocal.java
* -rw-r--r--  1 lu lu 573  1月 21  2003 ejbdemo/DemoSessionEJB.java
  -rw-r--r--  1 lu lu 194  1月 21  2003 ejbdemo/DemoSessionHome.java
* -rw-r--r--  1 lu lu 159  1月 21  2003 ejbdemo/DemoSessionLocalHome.java


3. call batch-query-replace-regexp.

>>  (defun batch-query-replace-regexp()
>>    (interactive)
>>      (switch-to-buffer "*Find*")
>>      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>> ->can't reach here     (switch-to-buffer "*Find*")
>>       (dired-do-query-replace-regexp "java" "java2")
...a lot of pattern to query-replace-regexp
>>  )
>>
You can press "Q" key to dired-do-query-replace-regexp once.
But I have about 50 patterns to replace, do it one by one is
quite troublesome.  so I copy patterns from excel file
and write them to a function
(which named batch-query-replace-regexp above).
The problem is the function finished when first call
of dired-do-query-replace-regexp finish.

Doesn't you feel it is strange?

Andreas Röhler <andreas.roehler@online.de> writes:

> Am Samstag, 1. Dezember 2007 02:00 schrieb Ke Lu:
>> > Do want to change filenames in a directory or the contents of files in
>> > that directory?
>>
>> I don't want to change filenames.
>> I want to change contents of files in a directory.
>>
>
> So let's start with the first task:
>
> If you want to add a "2" at all endings in that directory,
>
> as  "jp.co" => "jp.co2" let's assume,
>
> you can perform this
>
> with
>
> M-x wdired-change-to-wdired-mode
>
> M-x query-replace-regexp $ RET 2 !
>
> If everything looks fine, do C-c C-c to write down your
> changing or quit (with C-c ESC or C-d).
>
> With quit nothing is changed.
>
> Andreas Röhler

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

* Re: Interative batch query-replace question
  2007-12-01  9:11                       ` Ke Lu
@ 2007-12-01 15:19                         ` Xah Lee
  2007-12-02  2:16                           ` Ke Lu
  2007-12-01 17:39                         ` Andreas Röhler
       [not found]                         ` <mailman.4401.1196530793.18990.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 22+ messages in thread
From: Xah Lee @ 2007-12-01 15:19 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Ke Lu,

what you want to do is exactly what i wanted to do ever since i knew
about dired-do-query-replace-regexp. In summary, you want to use it to
do multiple find-replace pairs.

(Note: i have (defalias 'ddqrr 'dired-do-query-replace-regexp)
)

I don't think you can just slap in several ddqrr in your own function
to achieve multiple find-replace pairs one-shot. Rather, i think you
need to find the function that's behind the interface used by ddqrr.

  Xah
  xah@xahlee.org
\xAD\xF4 http://xahlee.org/

On Dec 1, 1:11 am, Ke Lu <l...@luxdo.jp> wrote:
Perhaps my explain is not quite enough.

I want to query-replace some files.(not filename)

1. Use find-dired to Find some files.(M-x find-dired)
In "*File" buffer,it looks like:
  /home/lu/workspace/:
  find . \( -name "*.java" \) -exec ls -ld \{\} \;
  -rw-r--r--  1 lu lu 215  1月 21  2003 ejbdemo/DemoEntity.java
  -rw-r--r--  1 lu lu 178  1月 21  2003 ejbdemo/DemoSession.java
  -rw-r--r--  1 lu lu 146  1月 21  2003 ejbdemo/DemoSessionLocal.java
  -rw-r--r--  1 lu lu 573  1月 21  2003 ejbdemo/DemoSessionEJB.java
  -rw-r--r--  1 lu lu 194  1月 21  2003 ejbdemo/DemoSessionHome.java

2. Mark some file in "*Find*" buffer
In "*File" buffer,it looks like:
  /home/lu/workspace/:
  find . \( -name "*.java" \) -exec ls -ld \{\} \;
  -rw-r--r--  1 lu lu 215  1月 21  2003 ejbdemo/DemoEntity.java
* -rw-r--r--  1 lu lu 178  1月 21  2003 ejbdemo/DemoSession.java
* -rw-r--r--  1 lu lu 146  1月 21  2003 ejbdemo/DemoSessionLocal.java
* -rw-r--r--  1 lu lu 573  1月 21  2003 ejbdemo/DemoSessionEJB.java
  -rw-r--r--  1 lu lu 194  1月 21  2003 ejbdemo/DemoSessionHome.java
* -rw-r--r--  1 lu lu 159  1月 21  2003 ejbdemo/
DemoSessionLocalHome.java

3. call batch-query-replace-regexp.

>>  (defun batch-query-replace-regexp()
>>    (interactive)
>>      (switch-to-buffer "*Find*")
>>      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>> ->can't reach here     (switch-to-buffer "*Find*")
>>       (dired-do-query-replace-regexp "java" "java2")

...a lot of pattern to query-replace-regexp>>  )

You can press "Q" key to dired-do-query-replace-regexp once.
But I have about 50 patterns to replace, do it one by one is
quite troublesome.  so I copy patterns from excel file
and write them to a function
(which named batch-query-replace-regexp above).
The problem is the function finished when first call
of dired-do-query-replace-regexp finish.

Doesn't you feel it is strange?

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

* Re: Interative batch query-replace question
  2007-12-01  9:11                       ` Ke Lu
  2007-12-01 15:19                         ` Xah Lee
@ 2007-12-01 17:39                         ` Andreas Röhler
       [not found]                         ` <mailman.4401.1196530793.18990.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 22+ messages in thread
From: Andreas Röhler @ 2007-12-01 17:39 UTC (permalink / raw)
  To: help-gnu-emacs

Am Samstag, 1. Dezember 2007 10:11 schrieb Ke Lu:
> Perhaps my explain is not quite enough.
>
> I want to query-replace some files.(not filename)
...


So we have basically two tasks

1 - find the list of files
2 - operate on them

Dired probably isn't TRT here, because it's for
manually operation.

Maybe run this at your dir to have some impression how
it may start:

(defun my-list-of-files ()
  " "
  (interactive)
  (let ((my-endings-list '(".co$" ".java$")))
    (dolist (el my-endings-list)
      (let ((files (directory-files default-directory t el)))
	(while files
	  (let ((file (car files)))
	    (when (file-exists-p file)
	      (when (y-or-n-p (concat "You agree to change \""file"\" later")))
	      (message "Later we will change name and contents of %s " file))
	    (setq files (cdr files))))))))


Andreas Röhler

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

* Re: Interative batch query-replace question
  2007-12-01 15:19                         ` Xah Lee
@ 2007-12-02  2:16                           ` Ke Lu
  2007-12-03 15:27                             ` Joel J. Adamson
  0 siblings, 1 reply; 22+ messages in thread
From: Ke Lu @ 2007-12-02  2:16 UTC (permalink / raw)
  To: help-gnu-emacs

Hi Xah Lee,

Finally I give up call dired-do-query-replace-regexp directly.
I did it by the following:

(defun my-proc-one-query-replace-regexp (files from to)
  (dolist (cur-file files)
  (find-file cur-file)
  (goto-char 0)
  (query-replace-regexp from to))
  )

(defun my-batch-query-replace-regexp()
  "Muti-pattern query-replace-regrex on a dozen of files.
It must be called in dired buffer.
Mark some files then you can call this function."
  (interactive)
  (let ((files (dired-get-marked-files nil nil (lambda (file) (not (file-directory-p file)))))
        (patterns '(("ejb" . "ejb2")
                    ("java" . "java2")
                    ("sun2" . "sun3")
                    ;; Add any other patterns
                    )))
    (when files
      (while patterns
        (let ((pattern (car patterns)))
          (my-proc-one-query-replace-regexp files (car pattern) (cdr pattern))
          (setq patterns (cdr patterns))))
      )))


Thanks all.      
Xah Lee <xah@xahlee.org> writes:

> Hi Ke Lu,
>
> what you want to do is exactly what i wanted to do ever since i knew
> about dired-do-query-replace-regexp. In summary, you want to use it to
> do multiple find-replace pairs.
>
> (Note: i have (defalias 'ddqrr 'dired-do-query-replace-regexp)
> )
>
> I don't think you can just slap in several ddqrr in your own function
> to achieve multiple find-replace pairs one-shot. Rather, i think you
> need to find the function that's behind the interface used by ddqrr.
>
>   Xah
>   xah@xahlee.org
> \xAD\xF4 http://xahlee.org/
>
> On Dec 1, 1:11 am, Ke Lu <l...@luxdo.jp> wrote:
> Perhaps my explain is not quite enough.
>
> I want to query-replace some files.(not filename)
>
> 1. Use find-dired to Find some files.(M-x find-dired)
> In "*File" buffer,it looks like:
>   /home/lu/workspace/:
>   find . \( -name "*.java" \) -exec ls -ld \{\} \;
>   -rw-r--r--  1 lu lu 215  1月 21  2003 ejbdemo/DemoEntity.java
>   -rw-r--r--  1 lu lu 178  1月 21  2003 ejbdemo/DemoSession.java
>   -rw-r--r--  1 lu lu 146  1月 21  2003 ejbdemo/DemoSessionLocal.java
>   -rw-r--r--  1 lu lu 573  1月 21  2003 ejbdemo/DemoSessionEJB.java
>   -rw-r--r--  1 lu lu 194  1月 21  2003 ejbdemo/DemoSessionHome.java
>
> 2. Mark some file in "*Find*" buffer
> In "*File" buffer,it looks like:
>   /home/lu/workspace/:
>   find . \( -name "*.java" \) -exec ls -ld \{\} \;
>   -rw-r--r--  1 lu lu 215  1月 21  2003 ejbdemo/DemoEntity.java
> * -rw-r--r--  1 lu lu 178  1月 21  2003 ejbdemo/DemoSession.java
> * -rw-r--r--  1 lu lu 146  1月 21  2003 ejbdemo/DemoSessionLocal.java
> * -rw-r--r--  1 lu lu 573  1月 21  2003 ejbdemo/DemoSessionEJB.java
>   -rw-r--r--  1 lu lu 194  1月 21  2003 ejbdemo/DemoSessionHome.java
> * -rw-r--r--  1 lu lu 159  1月 21  2003 ejbdemo/
> DemoSessionLocalHome.java
>
> 3. call batch-query-replace-regexp.
>
>>>  (defun batch-query-replace-regexp()
>>>    (interactive)
>>>      (switch-to-buffer "*Find*")
>>>      (dired-do-query-replace-regexp "jp\\.co" "jp.co2")
>>> ->can't reach here     (switch-to-buffer "*Find*")
>>>       (dired-do-query-replace-regexp "java" "java2")
>
> ...a lot of pattern to query-replace-regexp>>  )
>
> You can press "Q" key to dired-do-query-replace-regexp once.
> But I have about 50 patterns to replace, do it one by one is
> quite troublesome.  so I copy patterns from excel file
> and write them to a function
> (which named batch-query-replace-regexp above).
> The problem is the function finished when first call
> of dired-do-query-replace-regexp finish.
>
> Doesn't you feel it is strange?

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

* Re: Interative batch query-replace question
       [not found]                         ` <mailman.4401.1196530793.18990.help-gnu-emacs@gnu.org>
@ 2007-12-02  2:26                           ` Ke Lu
  2007-12-03 15:32                             ` jedit.el (was: Interative batch query-replace question) Joel J. Adamson
  0 siblings, 1 reply; 22+ messages in thread
From: Ke Lu @ 2007-12-02  2:26 UTC (permalink / raw)
  To: help-gnu-emacs

Thank you very much.
It is true,we needs two task.
> 1 - find the list of files
I do it by find-dired and mark it because
find-dired using os command "find" to find file and is powerful.

> 2 - operate on them
I do it by my-proc-one-query-replace-regexp.

(defun my-proc-one-query-replace-regexp (files from to)
  (dolist (cur-file files)
  (find-file cur-file)
  (goto-char 0)
  (query-replace-regexp from to))
  )

(defun my-batch-query-replace-regexp()
  "Muti-pattern query-replace-regrex on a dozen of files.
It must be called in dired buffer.
Mark some files then you can call this function."
  (interactive)
  (let ((files (dired-get-marked-files nil nil (lambda (file) (not (file-directory-p file)))))
        (patterns '(("ejb" . "ejb2")
                    ("java" . "java2")
                    ("sun2" . "sun3")
                    ;; Add any other patterns
                    )))
    (when files
      (while patterns
        (let ((pattern (car patterns)))
          (my-proc-one-query-replace-regexp files (car pattern) (cdr pattern))
          (setq patterns (cdr patterns))))
      )))

Andreas Röhler <andreas.roehler@online.de> writes:

> Am Samstag, 1. Dezember 2007 10:11 schrieb Ke Lu:
>> Perhaps my explain is not quite enough.
>>
>> I want to query-replace some files.(not filename)
> ...
>
>
> So we have basically two tasks
>
> 1 - find the list of files
> 2 - operate on them
>
> Dired probably isn't TRT here, because it's for
> manually operation.
>
> Maybe run this at your dir to have some impression how
> it may start:
>
> (defun my-list-of-files ()
>   " "
>   (interactive)
>   (let ((my-endings-list '(".co$" ".java$")))
>     (dolist (el my-endings-list)
>       (let ((files (directory-files default-directory t el)))
> 	(while files
> 	  (let ((file (car files)))
> 	    (when (file-exists-p file)
> 	      (when (y-or-n-p (concat "You agree to change \""file"\" later")))
> 	      (message "Later we will change name and contents of %s " file))
> 	    (setq files (cdr files))))))))
>
>
> Andreas Röhler

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

* Re: Interative batch query-replace question
  2007-12-02  2:16                           ` Ke Lu
@ 2007-12-03 15:27                             ` Joel J. Adamson
  0 siblings, 0 replies; 22+ messages in thread
From: Joel J. Adamson @ 2007-12-03 15:27 UTC (permalink / raw)
  To: help-gnu-emacs

Ke Lu <lu@luxdo.jp> writes:

> (defun my-proc-one-query-replace-regexp (files from to)
>   (dolist (cur-file files)
>   (find-file cur-file)
>   (goto-char 0)
>   (query-replace-regexp from to))
>   )

FYI, you can do this from a shell-script using #!/usr/bin/emacs
--script.  You can also have interactive and non-interactive versions of
the same function.

> (defun my-batch-query-replace-regexp()
>   "Muti-pattern query-replace-regrex on a dozen of files.
> It must be called in dired buffer.
> Mark some files then you can call this function."
>   (interactive)
>   (let ((files (dired-get-marked-files nil nil (lambda (file) (not (file-directory-p file)))))
>         (patterns '(("ejb" . "ejb2")
>                     ("java" . "java2")
>                     ("sun2" . "sun3")
>                     ;; Add any other patterns
>                     )))
>     (when files
>       (while patterns
>         (let ((pattern (car patterns)))
>           (my-proc-one-query-replace-regexp files (car pattern) (cdr pattern))
>           (setq patterns (cdr patterns))))
>       )))

One suggestion (and I'd be happy to show you my version of the same
function):  make this a function that processes the alist, instead of
directly including the alist, then you can re-use it.

For example:
(setq replace-list '(("\\$\\(.*?\n*.*?\\)\\$" "\\1")
		     ("sidewaystable" "table")
		     ("\\(\\(\\^\\|_\\){?\\([a-z0-9*.,]+ *\\)\\)}?" "\\3")
		     ("^.*listof.*$" "")
		     ("^.*bibliography.*$" "")))
(jedit-strip-regex replace-list)

"jedit-strip-regex" is my function that does the same thing as yours.

Joel

-- 
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA  02114
(617) 643-1432
(303) 880-3109

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

* jedit.el (was: Interative batch query-replace question)
  2007-12-02  2:26                           ` Ke Lu
@ 2007-12-03 15:32                             ` Joel J. Adamson
  0 siblings, 0 replies; 22+ messages in thread
From: Joel J. Adamson @ 2007-12-03 15:32 UTC (permalink / raw)
  To: help-gnu-emacs

Some custom editing functions: suggestions welcome.

;;; jedit.el --- Joel's Custom Editing Functions
;; Author: Joel J. Adamson
;; Maintainer: Joel J. Adamson
;; version 0.1
;; keywords:  tex matching
;; Copyright (C) 2007 Joel J. Adamson

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 3 of
;; the License, or (at your option) any later version.

;; This program is distributed in the hope that it will be
;; useful, but WITHOUT ANY WARRANTY; without even the implied
;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
;; PURPOSE.  See the GNU General Public License for more details.

;; You should have received a copy of the GNU General Public
;; License along with this program; if not, write to the Free
;; Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301 USA
;;; Code:

;; strip unrecognized regexes from latex files for latex2rtf
(defun jedit-strip-regex (alist)
  "Takes a list of regex-replacement string pairs;
processes entire buffer."
  (interactive "sList: ")
  ;; for each cell in alist, define regex and replacement text
  (dolist (regex-cell alist)
    (let ((regex (car regex-cell))
	  (replacement (cadr regex-cell)))
      ;; go to beginning of buffer
      (goto-char (point-min))
      ;; when you find the search string, replace it with replacement
      ;; text
      (while (re-search-forward regex nil t)
	(replace-match replacement nil nil)))))

;; tex-insert-file
;; takes an input command and inserts the file
;; directly into the current buffer
(defun jedit-tex-insert-file (&optional buffer)
  "Find a .tex file and insert it into a buffer
at \input{`filename'}; `filename' is found automatically from
\input command by searching for a `filename' as a blank text
file and then for `filename'.tex"
  ;; enter a buffer name; defaults to (current-buffer)
  (interactive "bBuffer: ")
  ;; return to the beginning of buffer
  (goto-char (point-min))
  ;; set the symbol filename to be nothing, incase it already exists
  (setq filename "")
  ;; every time you find an \input{`filename'} command, 
  (let ((count 0))
    (while (and (< (point) (point-max))
		(re-search-forward "\\\\input{\\(.*?\\)}" nil t))
      (setq count (1+ count))
      ;; search for the file first as `filename' (a plain text file)
      (let ((txt-filename (match-string-no-properties 1))
	    ;; and then as a TeX file with a .tex extension
	    (tex-filename (concat
			   (match-string-no-properties 1)
			   ".tex")))
	(when (cond ((file-readable-p txt-filename)
		     (setq filename txt-filename))
		    ((file-readable-p tex-filename)
		     (setq filename tex-filename))
		    ;; if the file can't be found within these
		    ;; parameters, exit with an error
		    (t (error "tex-insert-file Error: no %s or %s found"
			      txt-filename
			      tex-filename)))
	  ;; remove the entire matched string
	  (replace-match "")
	  ;; insert the file contents into the current-buffer
	  (insert-file-contents filename)))
      ;; output messages for user
      (cond ((zerop count)
	     (message "No replacements"))
	    ((= 1 count)
	     (message "Made 1 replacement"))
	    (t (message "Made %d replacements" count))))))

(provide 'jedit)
(require 'jedit)
;;; jedit.el ends here

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

end of thread, other threads:[~2007-12-03 15:32 UTC | newest]

Thread overview: 22+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-29  3:08 Interative batch query-replace question Ke Lu
2007-11-29  5:56 ` Ke Lu
2007-11-29  7:37   ` Andreas Röhler
     [not found]   ` <mailman.4291.1196321879.18990.help-gnu-emacs@gnu.org>
2007-11-29  9:15     ` Ke Lu
2007-11-29 10:18       ` Ke Lu
2007-11-29 20:55         ` Andreas Röhler
     [not found]         ` <mailman.4324.1196369791.18990.help-gnu-emacs@gnu.org>
2007-11-29 21:16           ` Joel J. Adamson
2007-11-30  1:58           ` Ke Lu
2007-11-30  8:08             ` Andreas Röhler
     [not found]             ` <mailman.4336.1196410171.18990.help-gnu-emacs@gnu.org>
2007-11-30  9:25               ` Ke Lu
2007-11-30 14:25                 ` Joel J. Adamson
2007-11-30 19:33                 ` Andreas Röhler
     [not found]                 ` <mailman.4365.1196451256.18990.help-gnu-emacs@gnu.org>
2007-12-01  1:00                   ` Ke Lu
2007-12-01  8:19                     ` Andreas Röhler
     [not found]                     ` <mailman.4382.1196497198.18990.help-gnu-emacs@gnu.org>
2007-12-01  9:11                       ` Ke Lu
2007-12-01 15:19                         ` Xah Lee
2007-12-02  2:16                           ` Ke Lu
2007-12-03 15:27                             ` Joel J. Adamson
2007-12-01 17:39                         ` Andreas Röhler
     [not found]                         ` <mailman.4401.1196530793.18990.help-gnu-emacs@gnu.org>
2007-12-02  2:26                           ` Ke Lu
2007-12-03 15:32                             ` jedit.el (was: Interative batch query-replace question) Joel J. Adamson
2007-11-29  6:54 ` Interative batch query-replace question Andreas Röhler

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).