批量indent 所有子目录下的 C 源程序

通过修改 wcy-c-file-pattern , 可以过滤任何文件,如

("\.py$") for python
("\.pl$" "\.pm$") for perl
("\.el$" ) for elisp

源程序如下:

(defun wcy-filter ( func list)
  (if  list
      (let ((head (car list))
            (tail (cdr list)))
        (if  (funcall func head)
            (cons head (wcy-filter func tail))
          (wcy-filter func tail)))
      nil))
(defun wcy-indent-func (file)
  (save-excursion
    (find-file file)
    (indent-region 1 (point-max) nil)
    (write-file file)
    (kill-buffer (current-buffer))))
(setq wcy-c-file-pattern '( "\.c$" "\.cc$" "\.h$" ))
(defun wcy-list-all-c-file-in-subdirectories (&optional path)
  (interactive "DInput the directory:\n")
  (let ((files (wcy-filter (lambda (f)
                       (or (file-directory-p f)
                           (and(file-regular-p f)
                               (file-writable-p f)
                               (wcy-filter  (lambda (s)
                                          (string-match s f))
                                        wcy-c-file-pattern))))
                     (directory-files path t "^[^.]"))))
  (apply 'append (mapcar (lambda (f)
                    (funcall (if (file-directory-p f)
                                 'indent-all-c-file-in-subdirectories
                               'list)
                             f))
                  files))))

(mapc 'wcy-indent-func
      (wcy-list-all-c-file-in-subdirectories "~/tmp/testident/"))