Skip to content

Instantly share code, notes, and snippets.

@psanford
Last active August 2, 2019 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save psanford/636cbe569648b8bef85ce22ccedd565b to your computer and use it in GitHub Desktop.
Save psanford/636cbe569648b8bef85ce22ccedd565b to your computer and use it in GitHub Desktop.
emacs batch reformat directory
(defun reformat-directory (dir)
"Reformat all files of extention in directory"
(interactive "D")
(let* ((ext ".go")
(ts)
(tmp-buffer)
;; disable find-file hooks to speed up processing:
;; don't run git commands on every file
(find-file-hook '())
(files (split-string (shell-command-to-string (format "find %s -name *%s -print0" dir ext)) "\0" t)))
(dolist (f files)
(message "[%s] %s start" (format-time-string "%Y-%m-%dT%H:%M:%S.%6N") f)
(setq tmp-buffer (find-file f))
(setq ts (current-time))
(with-current-buffer tmp-buffer
(mark-whole-buffer)
(ignore-errors
(indent-region (point-min) (point-max) nil))
(message "[%s] %s stop took=%0.6f" (format-time-string "%Y-%m-%dT%H:%M:%S.%6N") f (float-time (time-since ts)))
(save-buffer))
(kill-buffer tmp-buffer))))
#!/bin/bash
GO_MODE="/home/psanford/projects/go-mode.el/go-mode.el"
if [ ! -e "$1" ]; then
echo "usage: $0 <directory|file>" >&2
exit 1
fi
echo "$1" | emacs --batch -q -l $GO_MODE -l reformat-directory.el -f reformat-directory
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment