help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Interative batch query-replace question


From: Joel J. Adamson
Subject: Re: Interative batch query-replace question
Date: Mon, 03 Dec 2007 10:27:23 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.0.60 (gnu/linux)

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


reply via email to

[Prev in Thread] Current Thread [Next in Thread]