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