summaryrefslogtreecommitdiff
path: root/tools/clang-format/clang-format.el
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-04-09 15:23:04 +0000
committerDaniel Jasper <djasper@google.com>2013-04-09 15:23:04 +0000
commit63911838bf7891445ff39fdc7f81d1469d54f5c1 (patch)
treee8d0591f84016c3a6c2ce852a6edaed7f0e2c90e /tools/clang-format/clang-format.el
parent40db5155fbd78faa792aefec5ceddcbf4fc3bb41 (diff)
downloadclang-63911838bf7891445ff39fdc7f81d1469d54f5c1.tar.gz
clang-63911838bf7891445ff39fdc7f81d1469d54f5c1.tar.bz2
clang-63911838bf7891445ff39fdc7f81d1469d54f5c1.tar.xz
Improvements to clang-format integrations.
This adds an emacs editor integration (thanks to Ami Fischman). Also pulls out the style into a variable for the vi integration and just uses clang-formats defaults style in clang-format-diff.py. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@179098 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/clang-format/clang-format.el')
-rw-r--r--tools/clang-format/clang-format.el47
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/clang-format/clang-format.el b/tools/clang-format/clang-format.el
new file mode 100644
index 0000000000..c63c62ee55
--- /dev/null
+++ b/tools/clang-format/clang-format.el
@@ -0,0 +1,47 @@
+;;; 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:
+;;
+;; (load "<path-to-clang>/tools/clang/clang-format/clang-format.el")
+;; (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.
+(defun clang-format-region ()
+ (interactive)
+ (let ((orig-file buffer-file-name)
+ (orig-point (point))
+ (orig-mark (mark t))
+ (orig-mark-active mark-active)
+ (binary "clang-format")
+ (style "LLVM")
+ replacement-text replaced beg end)
+ (basic-save-buffer)
+ (save-restriction
+ (widen)
+ (if mark-active
+ (setq beg (1- (region-beginning))
+ end (1- (region-end)))
+ (setq beg (1- (line-beginning-position))
+ end (1- (line-end-position))))
+ (with-temp-buffer
+ (call-process
+ binary orig-file '(t nil) t
+ "-offset" (number-to-string beg)
+ "-length" (number-to-string (- end beg))
+ "-style" style)
+ (setq replacement-text
+ (buffer-substring-no-properties (point-min) (point-max))))
+ (unless (string= replacement-text
+ (buffer-substring-no-properties (point-min) (point-max)))
+ (delete-region (point-min) (point-max))
+ (insert replacement-text)
+ (setq replaced t)))
+ (ignore-errors
+ (when orig-mark
+ (push-mark orig-mark)
+ (when orig-mark-active
+ (activate-mark)
+ (setq deactivate-mark nil)))
+ (goto-char orig-point))))