summaryrefslogtreecommitdiffstats
path: root/tools/clang-format/clang-format.el
diff options
context:
space:
mode:
Diffstat (limited to 'tools/clang-format/clang-format.el')
-rw-r--r--tools/clang-format/clang-format.el57
1 files changed, 41 insertions, 16 deletions
diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el
index 2c5546b..520a3e2 100644
--- a/tools/clang-format/clang-format.el
+++ b/tools/clang-format/clang-format.el
@@ -7,25 +7,50 @@
;; (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.
+
+(require 'json)
+
+;; *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."
(interactive)
+ (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 (min (line-end-position) (1- (point-max)))))
- (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)))))
+ (style "file"))
+ (unwind-protect
+ (call-process-region (point-min) (point-max) clang-format-binary
+ t (list t nil) nil
+ "-offset" (number-to-string (1- begin))
+ "-length" (number-to-string (- end begin))
+ "-cursor" (number-to-string (1- (point)))
+ "-assume-filename" (buffer-file-name)
+ "-style" style)
+ (goto-char (point-min))
+ (let ((json-output (json-read-from-string
+ (buffer-substring-no-properties
+ (point-min) (line-beginning-position 2)))))
+ (delete-region (point-min) (line-beginning-position 2))
+ (goto-char (1+ (cdr (assoc 'Cursor json-output))))
+ (dotimes (index (length orig-windows))
+ (set-window-start (nth index orig-windows)
+ (nth index orig-window-starts)))))))
OpenPOWER on IntegriCloud