3

I sometimes work on legacy code, and they have mixture of tabs and spaces. I use whitespace-cleanup function in whitespace.el to clean them up. But I have many files, so i want to run the command on all of them (excluding the .git directory). What is the implementation of this recursive function call in elisp? My seasonal elisp skill does not let me do it right away.

  • One possibility: Mark the files in Dired, and act on them using a shell command or an Emacs-Lisp command. There are examples in the Dired files and library Dired+ of the latter. See the definitions of diredp-*-recursive commands in dired+.el. – Drew Sep 29 '16 at 0:09
3

Library Dired+ offers a bunch of commands that let you act on the marked files and the files marked in marked subdirectories, defined recursively. These commands have names like diredp-*-recursive.

It is also the case that you can insert any subdirs (and subsubdirs etc.) into the same Dired buffer, and then act on marked files there.

One of the Dired+ commands is diredp-do-apply-function-recursive, bound to M-+ @. It lets you apply an Emacs-Lisp function to each of the marked files, defined recursively (see above). Define a function that does what you want to do to one file, and then you can use M-+ @ to apply it to all of the marked files (defined recursively).

diredp-do-apply-function-recursive is an interactive compiled Lisp function in dired+.el.

(diredp-do-apply-function-recursive FUNCTION &optional ARG)

Apply FUNCTION to the marked files.

Like diredp-do-apply-function but act recursively on subdirs.

The files acted on are those that are marked in the current Dired buffer, or all files in the directory if none are marked. Marked subdirectories are handled recursively in the same way.

With a plain prefix ARG (C-u), visit each file and invoke FUNCTION with no arguments.

Otherwise, apply FUNCTION to each file name.

Any other prefix arg behaves according to the ARG argument of dired-get-marked-files. In particular, C-u C-u operates on all files in the Dired buffer.

As you can see from that doc string, another command, diredp-do-apply-function, does the same thing, but only in the current Dired buffer. It is bound @. Use it if all of the files you want to act on are in the same Dired buffer (e.g., because you inserted their directories there).

  • This will surely do the job. I am just a bit hesitated to use it because I use ranger package at the moment. Reading through some threads on SO, I think it is possible for me to write a standalone function to do the job. Here are some answers by Chris Barret: stackoverflow.com/a/19508559/588867 and stackoverflow.com/a/14671532/588867 . I will however accept @Drew as a solution. – biocyberman Oct 1 '16 at 8:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.