OLD | NEW |
1 ;;; Clang-format emacs integration for use with C/Objective-C/C++. | 1 ;;; Clang-format emacs integration for use with C/Objective-C/C++. |
2 | 2 |
3 ;; This defines a function clang-format-region that you can bind to a key. | 3 ;; This defines a function clang-format-region that you can bind to a key. |
4 ;; A minimal .emacs would contain: | 4 ;; A minimal .emacs would contain: |
5 ;; | 5 ;; |
6 ;; (load "<path-to-clang>/tools/clang-format/clang-format.el") | 6 ;; (load "<path-to-clang>/tools/clang-format/clang-format.el") |
7 ;; (global-set-key [C-M-tab] 'clang-format-region) | 7 ;; (global-set-key [C-M-tab] 'clang-format-region) |
8 ;; | 8 ;; |
9 ;; Depending on your configuration and coding style, you might need to modify | 9 ;; Depending on your configuration and coding style, you might need to modify |
10 ;; 'style' and 'binary' below. | 10 ;; 'style' and 'binary' below. |
11 (defun clang-format-region () | 11 (defun clang-format-region () |
12 (interactive) | 12 (interactive) |
13 | 13 |
14 (let* ((orig-windows (get-buffer-window-list (current-buffer))) | 14 (let* ((orig-windows (get-buffer-window-list (current-buffer))) |
15 (orig-window-starts (mapcar #'window-start orig-windows)) | 15 (orig-window-starts (mapcar #'window-start orig-windows)) |
16 (orig-point (point)) | 16 (orig-point (point)) |
17 (binary "clang-format") | 17 (binary "clang-format") |
18 (style "LLVM")) | 18 (style "LLVM")) |
19 (if mark-active | 19 (if mark-active |
20 (setq beg (region-beginning) | 20 (setq beg (region-beginning) |
21 end (region-end)) | 21 end (region-end)) |
22 (setq beg (min (line-beginning-position) (1- (point-max))) | 22 (setq beg (line-beginning-position) |
23 end (min (line-end-position) (1- (point-max))))) | 23 end (line-end-position))) |
24 (call-process-region (point-min) (point-max) binary t t nil | 24 (call-process-region (point-min) (point-max) binary t t nil |
25 "-offset" (number-to-string (1- beg)) | 25 "-offset" (number-to-string (1- beg)) |
26 "-length" (number-to-string (- end beg)) | 26 "-length" (number-to-string (- end beg)) |
27 "-style" style) | 27 "-style" style) |
28 (goto-char orig-point) | 28 (goto-char orig-point) |
29 (dotimes (index (length orig-windows)) | 29 (dotimes (index (length orig-windows)) |
30 (set-window-start (nth index orig-windows) | 30 (set-window-start (nth index orig-windows) |
31 (nth index orig-window-starts))))) | 31 (nth index orig-window-starts))))) |
OLD | NEW |