handy emacs functions for code cleanup

29 Nov 2008

Here are my "secret" functions for cleaning up a source code file (biased for C or C++).

;; Code Cleanup for Emacs V1.0
;; http://blog.modp.com/2008/11/handy-emacs-functions-for-code-cleanup.html

;; zap tabs
(defun buffer-untabify ()
  "Untabify an entire buffer"
  (untabify (point-min) (point-max)))

;; re-indent buffer
(defun buffer-indent()
  "Reindent an entire buffer"
  (indent-region (point-min) (point-max) nil))

;; Untabify, re-indent, make EOL be '\n' not '\r\n'
;;   and delete trailing whitespace
(defun buffer-cleanup()
  "Untabify and re-indent an entire buffer"
  (if (equal buffer-file-coding-system 'undecided-unix )
    (set-buffer-file-coding-system 'undecided-unix))
  (setq c-basic-offset 4
        tab-width 4
        indent-tabs-mode nil)