diff options
author | Daniel Jasper <djasper@google.com> | 2013-05-14 08:48:24 +0000 |
---|---|---|
committer | Daniel Jasper <djasper@google.com> | 2013-05-14 08:48:24 +0000 |
commit | 748d38fcf762727e143ceb2352dc0dae56b34ffa (patch) | |
tree | d72fe61febc5d530958721a0bfcb9efcd674701f | |
parent | a9d7b46c2fb3eb9020b60375e0cb722bd26b108d (diff) | |
download | clang-748d38fcf762727e143ceb2352dc0dae56b34ffa.tar.gz clang-748d38fcf762727e143ceb2352dc0dae56b34ffa.tar.bz2 clang-748d38fcf762727e143ceb2352dc0dae56b34ffa.tar.xz |
Update clang-format emacs integration.
- Remove free variables
- Add function clang-format-buffer, e.g. for before-save-hooks
- Wrap restoring windows in an unwind-protect
Patch by Stephen Gildea!
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181766 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | tools/clang-format/clang-format.el | 44 |
1 files changed, 29 insertions, 15 deletions
diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el index b76eb41d94..57475ac722 100644 --- a/tools/clang-format/clang-format.el +++ b/tools/clang-format/clang-format.el @@ -7,25 +7,39 @@ ;; (global-set-key [C-M-tab] 'clang-format-region) ;; ;; Depending on your configuration and coding style, you might need to modify -;; 'style' and 'binary' below. +;; 'style' in clang-format, below. + +;; *Location of the clang-format binary. If it is on your PATH, a full path name +;; need not be specified. +(defvar clang-format-binary "clang-format") + (defun clang-format-region () + "Use clang-format to format the currently active region." (interactive) + (let ((beg (if mark-active + (region-beginning) + (min (line-beginning-position) (1- (point-max))))) + (end (if mark-active + (region-end) + (line-end-position)))) + (clang-format beg end))) + +(defun clang-format-buffer () + "Use clang-format to format the current buffer." + (clang-format (point-min) (point-max))) +(defun clang-format (begin end) + "Use clang-format to format the code between BEGIN and END." (let* ((orig-windows (get-buffer-window-list (current-buffer))) (orig-window-starts (mapcar #'window-start orig-windows)) (orig-point (point)) - (binary "clang-format") (style "LLVM")) - (if mark-active - (setq beg (region-beginning) - end (region-end)) - (setq beg (min (line-beginning-position) (1- (point-max))) - end (line-end-position))) - (call-process-region (point-min) (point-max) binary t t nil - "-offset" (number-to-string (1- beg)) - "-length" (number-to-string (- end beg)) - "-style" style) - (goto-char orig-point) - (dotimes (index (length orig-windows)) - (set-window-start (nth index orig-windows) - (nth index orig-window-starts))))) + (unwind-protect + (call-process-region (point-min) (point-max) clang-format-binary t t nil + "-offset" (number-to-string (1- begin)) + "-length" (number-to-string (- end begin)) + "-style" style) + (goto-char orig-point) + (dotimes (index (length orig-windows)) + (set-window-start (nth index orig-windows) + (nth index orig-window-starts)))))) |