* Re: need help with editing multiple files in a directory
2007-07-19 15:52 need help with editing multiple files in a directory bittna
@ 2007-07-19 16:24 ` Pascal Bourguignon
2007-07-19 20:55 ` Joel J. Adamson
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Pascal Bourguignon @ 2007-07-19 16:24 UTC (permalink / raw)
To: help-gnu-emacs
bittna@gmail.com writes:
> Hello,
> I have multiple files in a directory that need a line added at a
> certain point in the file. I wrote a lisp expression to do it, but I
> have to load the file, then run the command on the buffer, the save
> the file and I lose my place.
What do you mean by you lost your place?
> I used dired to do a find and replace
> on all of the files I needed, but how do I run my lisp expression on
> all of the files?
Doing it.
(defmacro run-lisp-expressions-on-files (files &body expressions)
`(dolist (file ,files)
(with-current-buffer (find-file file)
(unwind-protect (save-excursion ,@expressions)
(save-buffer)
(kill-buffer (current-buffer))))))
(run-lisp-expressions-on-files (list "file1" "file2" ...)
(goto-char (point-min))
(while (re-search-forward ...)
...))
> Also how do I loop through all the buffers using lisp and switch into
> each buffer and do something, then move to the next buffer?
Doing it! Well, you should define better what you want and what you
mean, of course, but assuming "using lisp" means having as major-mode
lisp-mode or emacs-lisp-mode:
(require 'cl)
(dolist (buffer (remove-if-not
(lambda (buffer)
(member (with-current-buffer buffer major-mode)
'(lisp-mode emacs-lisp-mode))) ; ...
buffer-list))
(with-current-buffer buffer
(do-something)))
--
__Pascal Bourguignon__ http://www.informatimago.com/
"Logiciels libres : nourris au code source sans farine animale."
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: need help with editing multiple files in a directory
2007-07-19 15:52 need help with editing multiple files in a directory bittna
2007-07-19 16:24 ` Pascal Bourguignon
@ 2007-07-19 20:55 ` Joel J. Adamson
2007-07-19 22:49 ` Colin S. Miller
2007-07-20 5:33 ` Mathias Dahl
3 siblings, 0 replies; 5+ messages in thread
From: Joel J. Adamson @ 2007-07-19 20:55 UTC (permalink / raw)
To: help-gnu-emacs
I know you asked this as a lisp question, but sed may be better at
accomplishing this, as long as you're on a Unix-like system. On
Windows, sed would be terrible at this, since it doesn't exist ;)
Joel
bittna@gmail.com writes:
> Hello,
> I have multiple files in a directory that need a line added at a
> certain point in the file. I wrote a lisp expression to do it, but I
> have to load the file, then run the command on the buffer, the save
> the file and I lose my place. I used dired to do a find and replace
> on all of the files I needed, but how do I run my lisp expression on
> all of the files?
>
> Also how do I loop through all the buffers using lisp and switch into
> each buffer and do something, then move to the next buffer?
>
> Thanks!!!
>
--
Joel J. Adamson
Biostatistician
Pediatric Psychopharmacology Research Unit
Massachusetts General Hospital
Boston, MA 02114
(617) 643-1432
(303) 880-3109
http://www.gnu.org/philosophy/why-free.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: need help with editing multiple files in a directory
2007-07-19 15:52 need help with editing multiple files in a directory bittna
2007-07-19 16:24 ` Pascal Bourguignon
2007-07-19 20:55 ` Joel J. Adamson
@ 2007-07-19 22:49 ` Colin S. Miller
2007-07-20 5:33 ` Mathias Dahl
3 siblings, 0 replies; 5+ messages in thread
From: Colin S. Miller @ 2007-07-19 22:49 UTC (permalink / raw)
To: help-gnu-emacs
bittna@gmail.com wrote:
> Hello,
> I have multiple files in a directory that need a line added at a
> certain point in the file. I wrote a lisp expression to do it, but I
> have to load the file, then run the command on the buffer, the save
> the file and I lose my place. I used dired to do a find and replace
> on all of the files I needed, but how do I run my lisp expression on
> all of the files?
>
> Also how do I loop through all the buffers using lisp and switch into
> each buffer and do something, then move to the next buffer?
>
> Thanks!!!
>
Bittna,
The function (dired-get-marked-files) looks interesting.
I don't speak lisp well, so this mightn't be the best way of doing things.
(loop for fileName in (dired-get-marked-files) do
(progn
(find-file fileName)
(bittna-lisp-expr)))
or
(loop for fileName in (dired-get-marked-files) do
(let ((buff))
(progn
(setq buff (find-file fileName))
(bittna-lisp-expr))
(kill-buffer buff)))
I use XEmacs, rather than GNU Emacs, so YMMV.
HTH,
Colin S. Miller
--
Replace the obvious in my email address with the first three letters of the hostname to reply.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: need help with editing multiple files in a directory
2007-07-19 15:52 need help with editing multiple files in a directory bittna
` (2 preceding siblings ...)
2007-07-19 22:49 ` Colin S. Miller
@ 2007-07-20 5:33 ` Mathias Dahl
3 siblings, 0 replies; 5+ messages in thread
From: Mathias Dahl @ 2007-07-20 5:33 UTC (permalink / raw)
To: help-gnu-emacs
bittna@gmail.com writes:
> Hello, I have multiple files in a directory that need a line added
> at a certain point in the file. I wrote a lisp expression to do it,
> but I have to load the file, then run the command on the buffer, the
> save the file and I lose my place. I used dired to do a find and
> replace on all of the files I needed, but how do I run my lisp
> expression on all of the files?
Pascal posted a good example of how to use lisp to do what you
want. Depending on what exactly it is that you want to do, I would
also suggest using keyboard macros, they are really useful for adhoc
manipulations. Find the files to work with in Dired, kill all other
lines, then record the macro having point on the first file, do all
manipulations you want to do, close the file, go to the next file and
stop macro recording. Then execute the macro for the rest of the
files. Works in many cases.
/Mathias
^ permalink raw reply [flat|nested] 5+ messages in thread