How to invoke a function on all files in the git working tree, while opening the ones which have had no opened buffers beforehand?

A potential use case: Check and fix spelling in all project files with flyspell.

  • 1
    The "all version controlled files" is not clear enough: there might be versioned files, which aren't actually present in file system (maybe they exist in a different branch, or in history), if these should be affected too, the task is a lot harder. Since magit was mentioned, this may be of interest to you: git-scm.com/docs/git-ls-files – wvxvw Sep 26 '18 at 16:56
  • 1
    I would be very inclined to not use Emacs for such cases, unless the processing in question was very hard to do without it. I think stripping trailing whitespace is not a great example, because it's so trivial to do with sed etc that one immediately wonders why you wouldn't do it that way. I do think the question is valid, though -- it just might benefit from a more appropriate example use-case. – phils Sep 27 '18 at 0:03
  • @phils Thank you, I changed the use case. – atevm Sep 27 '18 at 11:54
  • @wvxvw Thanks, you helped a lot. I changed the question to be more precise, and from your suggestion I will work this out. – atevm Sep 27 '18 at 11:56

This should work for some definition of "all files":

(defun my-mapc-tracked-files (fn)
    (mapc (lambda (file)
                (find-file-noselect file)
                (goto-char (point-min))
                (funcall fn))))

You will have to pass a function to this function, like for mapc.


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.