blob: 70504c9f3c0842ee5ef0ef16df4604edc2410e42 [file] [log] [blame]
Daniel Jasper63911832013-04-09 15:23:04 +00001;;; 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 Jaspera50b5782013-04-17 07:55:02 +00006;; (load "<path-to-clang>/tools/clang-format/clang-format.el")
Daniel Jasper63911832013-04-09 15:23:04 +00007;; (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 Jasperf1ed9fe2013-04-25 07:06:48 +000013
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 Jasperc6d82ca2013-04-12 10:12:01 +000019 (if mark-active
Daniel Jaspera50b5782013-04-17 07:55:02 +000020 (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 Jasperc6d82ca2013-04-12 10:12:01 +000026 "-length" (number-to-string (- end beg))
27 "-style" style)
28 (goto-char orig-point)
Daniel Jasperf1ed9fe2013-04-25 07:06:48 +000029 (dotimes (index (length orig-windows))
30 (set-window-start (nth index orig-windows)
31 (nth index orig-window-starts)))))