2013-04-09 23:23:04 +08:00
|
|
|
;;; Clang-format emacs integration for use with C/Objective-C/C++.
|
|
|
|
|
|
|
|
;; This defines a function clang-format-region that you can bind to a key.
|
|
|
|
;; A minimal .emacs would contain:
|
|
|
|
;;
|
2013-04-17 15:55:02 +08:00
|
|
|
;; (load "<path-to-clang>/tools/clang-format/clang-format.el")
|
2013-04-09 23:23:04 +08:00
|
|
|
;; (global-set-key [C-M-tab] 'clang-format-region)
|
|
|
|
;;
|
|
|
|
;; Depending on your configuration and coding style, you might need to modify
|
2013-05-14 16:48:24 +08:00
|
|
|
;; 'style' in clang-format, below.
|
|
|
|
|
2013-05-21 20:21:39 +08:00
|
|
|
(require 'json)
|
|
|
|
|
2013-05-14 16:48:24 +08:00
|
|
|
;; *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")
|
|
|
|
|
2013-04-09 23:23:04 +08:00
|
|
|
(defun clang-format-region ()
|
2013-05-14 16:48:24 +08:00
|
|
|
"Use clang-format to format the currently active region."
|
2013-04-09 23:23:04 +08:00
|
|
|
(interactive)
|
2013-05-14 16:48:24 +08:00
|
|
|
(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."
|
2013-06-11 20:00:24 +08:00
|
|
|
(interactive)
|
2013-05-14 16:48:24 +08:00
|
|
|
(clang-format (point-min) (point-max)))
|
2013-04-25 15:06:48 +08:00
|
|
|
|
2013-05-14 16:48:24 +08:00
|
|
|
(defun clang-format (begin end)
|
|
|
|
"Use clang-format to format the code between BEGIN and END."
|
2013-04-25 15:06:48 +08:00
|
|
|
(let* ((orig-windows (get-buffer-window-list (current-buffer)))
|
|
|
|
(orig-window-starts (mapcar #'window-start orig-windows))
|
|
|
|
(orig-point (point))
|
2013-09-02 15:42:02 +08:00
|
|
|
(style "file"))
|
2013-05-14 16:48:24 +08:00
|
|
|
(unwind-protect
|
2013-09-13 21:40:24 +08:00
|
|
|
(call-process-region (point-min) (point-max) clang-format-binary
|
|
|
|
t (list t nil) nil
|
2013-05-14 16:48:24 +08:00
|
|
|
"-offset" (number-to-string (1- begin))
|
|
|
|
"-length" (number-to-string (- end begin))
|
2013-05-22 01:05:40 +08:00
|
|
|
"-cursor" (number-to-string (1- (point)))
|
2013-09-13 21:40:24 +08:00
|
|
|
"-assume-filename" (buffer-file-name)
|
2013-05-14 16:48:24 +08:00
|
|
|
"-style" style)
|
2013-05-21 20:21:39 +08:00
|
|
|
(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))
|
2013-05-22 01:05:40 +08:00
|
|
|
(goto-char (1+ (cdr (assoc 'Cursor json-output))))
|
2013-05-21 20:21:39 +08:00
|
|
|
(dotimes (index (length orig-windows))
|
|
|
|
(set-window-start (nth index orig-windows)
|
|
|
|
(nth index orig-window-starts)))))))
|