* Where to start with recursive file operations
@ 2004-10-31 0:20 Andrei Stebkov
2004-10-31 2:55 ` Pascal Bourguignon
2004-11-01 17:22 ` Kevin Rodgers
0 siblings, 2 replies; 5+ messages in thread
From: Andrei Stebkov @ 2004-10-31 0:20 UTC (permalink / raw)
Hi!
I would like to write a function that finds files recursively starting from
the current directory, opens files based on given extension and does some
operations on them. For instance, checks out the file (from CVS or
Perforce), selects the whole buffer, indents the contents and save it.
Also I'd like to have same kind of function that would search and replace in
multiple files.
As all the problems that I listed here stem from the same root (recursive
search) I need a few pointers about the basic built-in emacs functions of
this domain. The function of this nature must be already written, I just
couldn't find them.
Thanks in advance,
Andrei
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Where to start with recursive file operations
2004-10-31 0:20 Where to start with recursive file operations Andrei Stebkov
@ 2004-10-31 2:55 ` Pascal Bourguignon
2004-11-01 17:22 ` Kevin Rodgers
1 sibling, 0 replies; 5+ messages in thread
From: Pascal Bourguignon @ 2004-10-31 2:55 UTC (permalink / raw)
Andrei Stebkov <stebakov@tht.net> writes:
> Hi!
>
> I would like to write a function that finds files recursively starting from
> the current directory, opens files based on given extension and does some
> operations on them. For instance, checks out the file (from CVS or
> Perforce), selects the whole buffer, indents the contents and save it.
> Also I'd like to have same kind of function that would search and replace in
> multiple files.
> As all the problems that I listed here stem from the same root (recursive
> search) I need a few pointers about the basic built-in emacs functions of
> this domain. The function of this nature must be already written, I just
> couldn't find them.
There's a couple of little know commands in emacs that you should learn:
M-x apropos RET <pattern> RET
and:
C-h f TAB
For example, searching for "file" with:
M-x apropos RET file RET
or for "directory" with:
M-x apropos RET directory RET
you'd get a list of interesting functions, like directory-files or
file-expand-wildcards.
(defun map-files (function directory &optional full)
(dolist (item (file-expand-wildcards (concat directory "/*") full))
(if (file-directory-p item)
(map-files function item full)
(funcall function item))))
(map-files (function print) "/tmp" t)
--
__Pascal Bourguignon__ http://www.informatimago.com/
Voting Democrat or Republican is like choosing a cabin in the Titanic.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Where to start with recursive file operations
2004-10-31 0:20 Where to start with recursive file operations Andrei Stebkov
2004-10-31 2:55 ` Pascal Bourguignon
@ 2004-11-01 17:22 ` Kevin Rodgers
2004-11-02 7:32 ` Friedrich Dominicus
1 sibling, 1 reply; 5+ messages in thread
From: Kevin Rodgers @ 2004-11-01 17:22 UTC (permalink / raw)
Andrei Stebkov wrote:
> I would like to write a function that finds files recursively
starting from
> the current directory, opens files based on given extension and does some
> operations on them. For instance, checks out the file (from CVS or
> Perforce), selects the whole buffer, indents the contents and save it.
> Also I'd like to have same kind of function that would search and
replace in
> multiple files.
> As all the problems that I listed here stem from the same root (recursive
> search) I need a few pointers about the basic built-in emacs functions of
> this domain. The function of this nature must be already written, I just
> couldn't find them.
I would write a small shell script that uses the find command to
generate the file names, and use emacs' command line options to do the
processing:
#!/bin/sh
find . -type f -name "*.ext" -print |
while IFS= read -r file; do
emacs --batch "$file" \
--eval "(vc-checkout buffer-file-name)" \
--eval "(indent-region (point-min) (point-max) nil)" \
--funcall save-buffer
done
--
Kevin Rodgers
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Where to start with recursive file operations
2004-11-01 17:22 ` Kevin Rodgers
@ 2004-11-02 7:32 ` Friedrich Dominicus
2004-11-06 6:47 ` Oliver Scholz
0 siblings, 1 reply; 5+ messages in thread
From: Friedrich Dominicus @ 2004-11-02 7:32 UTC (permalink / raw)
Kevin Rodgers <ihs_4664@yahoo.com> writes:
> Andrei Stebkov wrote:
> > I would like to write a function that finds files recursively
> starting from
> > the current directory, opens files based on given extension and does some
> > operations on them. For instance, checks out the file (from CVS or
> > Perforce), selects the whole buffer, indents the contents and save it.
> > Also I'd like to have same kind of function that would search and
> replace in
> > multiple files.
> > As all the problems that I listed here stem from the same root (recursive
> > search) I need a few pointers about the basic built-in emacs functions of
> > this domain. The function of this nature must be already written, I just
> > couldn't find them.
>
> I would write a small shell script that uses the find command to
> generate the file names, and use emacs' command line options to do the
> processing:
>
> #!/bin/sh
>
> find . -type f -name "*.ext" -print |
> while IFS= read -r file; do
> emacs --batch "$file" \
> --eval "(vc-checkout buffer-file-name)" \
> --eval "(indent-region (point-min) (point-max) nil)" \
> --funcall save-buffer
> done
>
Well that would be one way. I would prefer doing it all in Emacs Lisp
unfortunatly nothing like the wished seems to be implemented, or
(probably more likely) I did not have found it. Some months ago I
wrote this:
(defun* replace-in-files (from to &optional (in-files ".*\.html")
(directory ".")
(recurse-p t))
(let ((files (nnheader-directory-files directory)))
(dolist (act-file files)
(cond
((and recurse-p (file-directory-p act-file))
(replace-in-files from to in-files act-file recurse-p))
((string-match in-files act-file)
(find-file act-file)
(replace-string from to)
(save-buffer)
(kill-buffer nil))
(t ))))) ; fall through
Well this obviously does not what was asked for, but it shows how to
start the recursion. It's dept-first, but I assume that does not
matter really.
The changes are not really big, drop the from to, add a function to do
the job and replace the stuff below (string-match with loading the
file and running the wished function on it...
Regards
Friedrich
--
Please remove just-for-news- to reply via e-mail.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Where to start with recursive file operations
2004-11-02 7:32 ` Friedrich Dominicus
@ 2004-11-06 6:47 ` Oliver Scholz
0 siblings, 0 replies; 5+ messages in thread
From: Oliver Scholz @ 2004-11-06 6:47 UTC (permalink / raw)
Friedrich Dominicus <just-for-news-frido@q-software-solutions.de> writes:
> Kevin Rodgers <ihs_4664@yahoo.com> writes:
>
>> Andrei Stebkov wrote:
>>> I would like to write a function that finds files recursively
>>> starting from the current directory, opens files based on given
>>> extension and does some operations on them.
[...]
>> I would write a small shell script that uses the find command to
>> generate the file names, and use emacs' command line options to do the
>> processing:
[shell script]
> Well that would be one way. I would prefer doing it all in Emacs Lisp
> unfortunatly nothing like the wished seems to be implemented, or
> (probably more likely) I did not have found it. Some months ago I
> wrote this:
> (defun* replace-in-files (from to &optional (in-files ".*\.html")
[...]
> Well this obviously does not what was asked for, but it shows how to
> start the recursion. It's dept-first, but I assume that does not
> matter really.
[...]
FWIW, I've been using a utility macro for operating on directory
trees:
(defun addinfo-subdirs (dir)
"Return a list of direct child directories of directory DIR."
(if (not (file-directory-p dir))
(error "Not a directory: %s" dir)
(cddr ; Get rid of "." and ".."
(delq nil (mapcar (lambda (f)
(and (file-directory-p f)
f))
(directory-files dir t))))))
(defmacro do-addinfo-dir-tree (spec &rest body)
"Run BODY once for each directory below DIRECTORY.
This traverses the directory tree below DIRECTORY (including)
depth first, binding VARIABLE to each directory name in turn.
\(fn (VARIABLE DIRECTORY) BODY)"
(let ((var (car spec))
(start-dir (cadr spec))
(dir (make-symbol "--dir"))
(subdirs (make-symbol "--subdirs"))
(dir-queue (make-symbol "--dirqueue")))
`(let ((,dir ,start-dir)
,subdirs ,dir-queue ,var)
(unless (file-directory-p ,dir)
(error "Not a directory: %s" ,dir))
(while ,dir
(setq ,subdirs (addinfo-subdirs ,dir)
,var ,dir)
,@body
(cond (,subdirs
(setq ,dir-queue (nconc (cdr ,subdirs)
,dir-queue)
,dir (car ,subdirs)))
(,dir-queue
(setq ,dir (car ,dir-queue)
,dir-queue (cdr ,dir-queue)))
(t (setq ,dir nil)))))))
Example: to find all files with the extension ".info" below
$HOME, you could use it like this:
(let ((info-files nil))
(do-addinfo-dir-tree (dir "~")
(let ((files (delq nil
(mapcar (lambda (el)
(and (not (file-directory-p el))
el))
(directory-files dir)))))
(dolist (f files)
(when (string-match "\\.info\\'" f)
(push f info-files)))))
(nreverse info-files))
Oliver
--
16 Brumaire an 213 de la Révolution
Liberté, Egalité, Fraternité!
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2004-11-06 6:47 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-10-31 0:20 Where to start with recursive file operations Andrei Stebkov
2004-10-31 2:55 ` Pascal Bourguignon
2004-11-01 17:22 ` Kevin Rodgers
2004-11-02 7:32 ` Friedrich Dominicus
2004-11-06 6:47 ` Oliver Scholz
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).