Skip to content

Instantly share code, notes, and snippets.

@cdkamat
Created October 18, 2012 07:25
Show Gist options
  • Save cdkamat/3910275 to your computer and use it in GitHub Desktop.
Save cdkamat/3910275 to your computer and use it in GitHub Desktop.
Emacs function to batch untabify and reindent
;;; File: emacs-format-file
;; http://www.cslab.pepperdine.edu/warford/BatchIndentationEmacs.html
;; to be run as --
;; emacs -batch *.[ch] -l ~/src/scripts/emacs-format-file -f emacs-format-function
(defun emacs-format-function ()
"Format the whole buffer."
(if (< 1 (count-windows))
(delete-other-windows (selected-window)))
(catch 'tag
(while t
(c-mode)
(c-set-style "linux")
(setq indent-tabs-mode 'nil)
(setq c-basic-offset 4)
;; can add a save hook to delete trailing whitespaces
(untabify (point-min) (point-max))
(indent-region (point-min) (point-max) nil)
(if buffer-file-name ; nil for *scratch* buffer
(progn
(write-file buffer-file-name)
(kill-buffer (current-buffer)))
(throw 'tag t)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment