2016-10-04 17:53:04 +08:00
|
|
|
|
;;; clang-format.el --- Format code using clang-format -*- lexical-binding: t; -*-
|
2013-04-09 23:23:04 +08:00
|
|
|
|
|
2015-01-08 23:29:03 +08:00
|
|
|
|
;; Keywords: tools, c
|
2015-01-13 16:35:34 +08:00
|
|
|
|
;; Package-Requires: ((cl-lib "0.3"))
|
2015-01-08 23:29:03 +08:00
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
|
|
;; This package allows to filter code through clang-format to fix its formatting.
|
|
|
|
|
;; clang-format is a tool that formats C/C++/Obj-C code according to a set of
|
|
|
|
|
;; style options, see <http://clang.llvm.org/docs/ClangFormatStyleOptions.html>.
|
|
|
|
|
;; Note that clang-format 3.4 or newer is required.
|
|
|
|
|
|
|
|
|
|
;; clang-format.el is available via MELPA and can be installed via
|
2013-04-09 23:23:04 +08:00
|
|
|
|
;;
|
2015-01-08 23:29:03 +08:00
|
|
|
|
;; M-x package-install clang-format
|
2013-04-09 23:23:04 +08:00
|
|
|
|
;;
|
2015-01-08 23:29:03 +08:00
|
|
|
|
;; when ("melpa" . "http://melpa.org/packages/") is included in
|
2016-10-04 17:53:04 +08:00
|
|
|
|
;; `package-archives'. Alternatively, ensure the directory of this
|
2015-01-08 23:29:03 +08:00
|
|
|
|
;; file is in your `load-path' and add
|
|
|
|
|
;;
|
|
|
|
|
;; (require 'clang-format)
|
|
|
|
|
;;
|
|
|
|
|
;; to your .emacs configuration.
|
|
|
|
|
|
|
|
|
|
;; You may also want to bind `clang-format-region' to a key:
|
|
|
|
|
;;
|
|
|
|
|
;; (global-set-key [C-M-tab] 'clang-format-region)
|
|
|
|
|
|
|
|
|
|
;;; Code:
|
2013-05-14 16:48:24 +08:00
|
|
|
|
|
2015-01-13 16:35:34 +08:00
|
|
|
|
(require 'cl-lib)
|
|
|
|
|
(require 'xml)
|
2013-05-21 20:21:39 +08:00
|
|
|
|
|
2015-01-08 23:29:03 +08:00
|
|
|
|
(defgroup clang-format nil
|
|
|
|
|
"Format code using clang-format."
|
|
|
|
|
:group 'tools)
|
|
|
|
|
|
|
|
|
|
(defcustom clang-format-executable
|
|
|
|
|
(or (executable-find "clang-format")
|
|
|
|
|
"clang-format")
|
|
|
|
|
"Location of the clang-format executable.
|
|
|
|
|
|
|
|
|
|
A string containing the name or the full path of the executable."
|
|
|
|
|
:group 'clang-format
|
2016-10-04 17:53:04 +08:00
|
|
|
|
:type '(file :must-match t)
|
2015-01-08 23:29:03 +08:00
|
|
|
|
:risky t)
|
|
|
|
|
|
|
|
|
|
(defcustom clang-format-style "file"
|
|
|
|
|
"Style argument to pass to clang-format.
|
|
|
|
|
|
|
|
|
|
By default clang-format will load the style configuration from
|
|
|
|
|
a file named .clang-format located in one of the parent directories
|
|
|
|
|
of the buffer."
|
|
|
|
|
:group 'clang-format
|
|
|
|
|
:type 'string
|
|
|
|
|
:safe #'stringp)
|
|
|
|
|
(make-variable-buffer-local 'clang-format-style)
|
|
|
|
|
|
2015-01-13 16:35:34 +08:00
|
|
|
|
(defun clang-format--extract (xml-node)
|
|
|
|
|
"Extract replacements and cursor information from XML-NODE."
|
|
|
|
|
(unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements))
|
|
|
|
|
(error "Expected <replacements> node"))
|
|
|
|
|
(let ((nodes (xml-node-children xml-node))
|
2015-05-10 15:47:19 +08:00
|
|
|
|
(incomplete-format (xml-get-attribute xml-node 'incomplete_format))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
replacements
|
|
|
|
|
cursor)
|
|
|
|
|
(dolist (node nodes)
|
|
|
|
|
(when (listp node)
|
|
|
|
|
(let* ((children (xml-node-children node))
|
|
|
|
|
(text (car children)))
|
|
|
|
|
(cl-case (xml-node-name node)
|
|
|
|
|
('replacement
|
|
|
|
|
(let* ((offset (xml-get-attribute-or-nil node 'offset))
|
|
|
|
|
(length (xml-get-attribute-or-nil node 'length)))
|
|
|
|
|
(when (or (null offset) (null length))
|
|
|
|
|
(error "<replacement> node does not have offset and length attributes"))
|
|
|
|
|
(when (cdr children)
|
|
|
|
|
(error "More than one child node in <replacement> node"))
|
|
|
|
|
|
2015-01-19 18:48:04 +08:00
|
|
|
|
(setq offset (string-to-number offset))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
(setq length (string-to-number length))
|
|
|
|
|
(push (list offset length text) replacements)))
|
|
|
|
|
('cursor
|
2015-01-19 18:48:04 +08:00
|
|
|
|
(setq cursor (string-to-number text)))))))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
|
|
|
|
|
;; Sort by decreasing offset, length.
|
|
|
|
|
(setq replacements (sort (delq nil replacements)
|
|
|
|
|
(lambda (a b)
|
|
|
|
|
(or (> (car a) (car b))
|
|
|
|
|
(and (= (car a) (car b))
|
|
|
|
|
(> (cadr a) (cadr b)))))))
|
|
|
|
|
|
2015-05-10 15:47:19 +08:00
|
|
|
|
(list replacements cursor (string= incomplete-format "true"))))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
|
|
|
|
|
(defun clang-format--replace (offset length &optional text)
|
2016-10-04 17:53:04 +08:00
|
|
|
|
"Replace the region defined by OFFSET and LENGTH with TEXT.
|
|
|
|
|
OFFSET and LENGTH are measured in bytes, not characters. OFFSET
|
2017-01-20 17:37:50 +08:00
|
|
|
|
is a zero-based file offset, assuming ‘utf-8-unix’ coding."
|
|
|
|
|
(let ((start (clang-format--filepos-to-bufferpos offset 'exact 'utf-8-unix))
|
|
|
|
|
(end (clang-format--filepos-to-bufferpos (+ offset length) 'exact
|
|
|
|
|
'utf-8-unix)))
|
2015-01-19 18:48:04 +08:00
|
|
|
|
(goto-char start)
|
|
|
|
|
(delete-region start end)
|
|
|
|
|
(when text
|
|
|
|
|
(insert text))))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
|
2016-10-04 17:53:04 +08:00
|
|
|
|
;; ‘bufferpos-to-filepos’ and ‘filepos-to-bufferpos’ are new in Emacs 25.1.
|
|
|
|
|
;; Provide fallbacks for older versions.
|
|
|
|
|
(defalias 'clang-format--bufferpos-to-filepos
|
|
|
|
|
(if (fboundp 'bufferpos-to-filepos)
|
|
|
|
|
'bufferpos-to-filepos
|
|
|
|
|
(lambda (position &optional _quality _coding-system)
|
|
|
|
|
(1- (position-bytes position)))))
|
|
|
|
|
|
|
|
|
|
(defalias 'clang-format--filepos-to-bufferpos
|
|
|
|
|
(if (fboundp 'filepos-to-bufferpos)
|
|
|
|
|
'filepos-to-bufferpos
|
|
|
|
|
(lambda (byte &optional _quality _coding-system)
|
|
|
|
|
(byte-to-position (1+ byte)))))
|
|
|
|
|
|
2015-01-08 23:29:03 +08:00
|
|
|
|
;;;###autoload
|
Fix assume-filename handling in clang-format.el
Summary:
When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error.
This was a problem when using clang-format-buffer on an orgmode source code editing buffer.
I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil.
To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'.
Reviewers: klimek, djasper, phst, phi
Reviewed By: phst, phi
Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits
Differential Revision: https://reviews.llvm.org/D37903
llvm-svn: 319621
2017-12-03 05:18:14 +08:00
|
|
|
|
(defun clang-format-region (start end &optional style assume-file-name)
|
2015-01-08 23:29:03 +08:00
|
|
|
|
"Use clang-format to format the code between START and END according to STYLE.
|
Fix assume-filename handling in clang-format.el
Summary:
When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error.
This was a problem when using clang-format-buffer on an orgmode source code editing buffer.
I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil.
To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'.
Reviewers: klimek, djasper, phst, phi
Reviewed By: phst, phi
Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits
Differential Revision: https://reviews.llvm.org/D37903
llvm-svn: 319621
2017-12-03 05:18:14 +08:00
|
|
|
|
If called interactively uses the region or the current statement if there is no
|
|
|
|
|
no active region. If no STYLE is given uses `clang-format-style'. Use
|
|
|
|
|
ASSUME-FILE-NAME to locate a style config file, if no ASSUME-FILE-NAME is given
|
|
|
|
|
uses the function `buffer-file-name'."
|
2015-01-08 23:29:03 +08:00
|
|
|
|
(interactive
|
|
|
|
|
(if (use-region-p)
|
|
|
|
|
(list (region-beginning) (region-end))
|
|
|
|
|
(list (point) (point))))
|
|
|
|
|
|
|
|
|
|
(unless style
|
|
|
|
|
(setq style clang-format-style))
|
|
|
|
|
|
Fix assume-filename handling in clang-format.el
Summary:
When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error.
This was a problem when using clang-format-buffer on an orgmode source code editing buffer.
I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil.
To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'.
Reviewers: klimek, djasper, phst, phi
Reviewed By: phst, phi
Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits
Differential Revision: https://reviews.llvm.org/D37903
llvm-svn: 319621
2017-12-03 05:18:14 +08:00
|
|
|
|
(unless assume-file-name
|
|
|
|
|
(setq assume-file-name buffer-file-name))
|
|
|
|
|
|
2017-01-20 17:37:50 +08:00
|
|
|
|
(let ((file-start (clang-format--bufferpos-to-filepos start 'approximate
|
|
|
|
|
'utf-8-unix))
|
|
|
|
|
(file-end (clang-format--bufferpos-to-filepos end 'approximate
|
|
|
|
|
'utf-8-unix))
|
|
|
|
|
(cursor (clang-format--bufferpos-to-filepos (point) 'exact 'utf-8-unix))
|
2015-01-19 18:48:04 +08:00
|
|
|
|
(temp-buffer (generate-new-buffer " *clang-format-temp*"))
|
2017-01-18 01:30:55 +08:00
|
|
|
|
(temp-file (make-temp-file "clang-format"))
|
2017-01-20 17:37:50 +08:00
|
|
|
|
;; Output is XML, which is always UTF-8. Input encoding should match
|
|
|
|
|
;; the encoding used to convert between buffer and file positions,
|
|
|
|
|
;; otherwise the offsets calculated above are off. For simplicity, we
|
|
|
|
|
;; always use ‘utf-8-unix’ and ignore the buffer coding system.
|
|
|
|
|
(default-process-coding-system '(utf-8-unix . utf-8-unix)))
|
2013-05-14 16:48:24 +08:00
|
|
|
|
(unwind-protect
|
Fix assume-filename handling in clang-format.el
Summary:
When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error.
This was a problem when using clang-format-buffer on an orgmode source code editing buffer.
I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil.
To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'.
Reviewers: klimek, djasper, phst, phi
Reviewed By: phst, phi
Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits
Differential Revision: https://reviews.llvm.org/D37903
llvm-svn: 319621
2017-12-03 05:18:14 +08:00
|
|
|
|
(let ((status (apply #'call-process-region
|
|
|
|
|
nil nil clang-format-executable
|
|
|
|
|
nil `(,temp-buffer ,temp-file) nil
|
|
|
|
|
`("-output-replacements-xml"
|
2018-04-06 23:14:32 +08:00
|
|
|
|
;; Guard against a nil assume-file-name.
|
Fix assume-filename handling in clang-format.el
Summary:
When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error.
This was a problem when using clang-format-buffer on an orgmode source code editing buffer.
I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil.
To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'.
Reviewers: klimek, djasper, phst, phi
Reviewed By: phst, phi
Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits
Differential Revision: https://reviews.llvm.org/D37903
llvm-svn: 319621
2017-12-03 05:18:14 +08:00
|
|
|
|
;; If the clang-format option -assume-filename
|
|
|
|
|
;; is given a blank string it will crash as per
|
|
|
|
|
;; the following bug report
|
|
|
|
|
;; https://bugs.llvm.org/show_bug.cgi?id=34667
|
|
|
|
|
,@(and assume-file-name
|
|
|
|
|
(list "-assume-filename" assume-file-name))
|
|
|
|
|
"-style" ,style
|
|
|
|
|
"-offset" ,(number-to-string file-start)
|
|
|
|
|
"-length" ,(number-to-string (- file-end file-start))
|
|
|
|
|
"-cursor" ,(number-to-string cursor))))
|
2016-10-04 17:53:04 +08:00
|
|
|
|
(stderr (with-temp-buffer
|
|
|
|
|
(unless (zerop (cadr (insert-file-contents temp-file)))
|
|
|
|
|
(insert ": "))
|
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
(point-min) (line-end-position)))))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
(cond
|
|
|
|
|
((stringp status)
|
|
|
|
|
(error "(clang-format killed by signal %s%s)" status stderr))
|
2016-10-04 17:53:04 +08:00
|
|
|
|
((not (zerop status))
|
2015-05-10 15:47:19 +08:00
|
|
|
|
(error "(clang-format failed with code %d%s)" status stderr)))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
|
2016-10-04 17:53:04 +08:00
|
|
|
|
(cl-destructuring-bind (replacements cursor incomplete-format)
|
|
|
|
|
(with-current-buffer temp-buffer
|
|
|
|
|
(clang-format--extract (car (xml-parse-region))))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
(save-excursion
|
2016-10-04 17:53:04 +08:00
|
|
|
|
(dolist (rpl replacements)
|
|
|
|
|
(apply #'clang-format--replace rpl)))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
(when cursor
|
2017-01-20 17:37:50 +08:00
|
|
|
|
(goto-char (clang-format--filepos-to-bufferpos cursor 'exact
|
|
|
|
|
'utf-8-unix)))
|
2015-05-10 15:47:19 +08:00
|
|
|
|
(if incomplete-format
|
|
|
|
|
(message "(clang-format: incomplete (syntax errors)%s)" stderr)
|
|
|
|
|
(message "(clang-format: success%s)" stderr))))
|
2015-01-13 16:35:34 +08:00
|
|
|
|
(delete-file temp-file)
|
|
|
|
|
(when (buffer-name temp-buffer) (kill-buffer temp-buffer)))))
|
2015-01-08 23:29:03 +08:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
Fix assume-filename handling in clang-format.el
Summary:
When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error.
This was a problem when using clang-format-buffer on an orgmode source code editing buffer.
I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil.
To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'.
Reviewers: klimek, djasper, phst, phi
Reviewed By: phst, phi
Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits
Differential Revision: https://reviews.llvm.org/D37903
llvm-svn: 319621
2017-12-03 05:18:14 +08:00
|
|
|
|
(defun clang-format-buffer (&optional style assume-file-name)
|
|
|
|
|
"Use clang-format to format the current buffer according to STYLE.
|
|
|
|
|
If no STYLE is given uses `clang-format-style'. Use ASSUME-FILE-NAME
|
|
|
|
|
to locate a style config file. If no ASSUME-FILE-NAME is given uses
|
|
|
|
|
the function `buffer-file-name'."
|
2015-01-08 23:29:03 +08:00
|
|
|
|
(interactive)
|
Fix assume-filename handling in clang-format.el
Summary:
When 'buffer-file-name' is nil 'call-process-region' returned a segmentation fault error.
This was a problem when using clang-format-buffer on an orgmode source code editing buffer.
I fixed this problem by excluding the '-assume-filename' argument when 'buffer-file-name' is nil.
To make it a bit more flexible I also added an optional argument, 'assume-file-name', to specify an assume-filename that overrides 'buffer-file-name'.
Reviewers: klimek, djasper, phst, phi
Reviewed By: phst, phi
Subscribers: phi, jholewinski, mgorny, javed.absar, eraman, cfe-commits
Differential Revision: https://reviews.llvm.org/D37903
llvm-svn: 319621
2017-12-03 05:18:14 +08:00
|
|
|
|
(clang-format-region (point-min) (point-max) style assume-file-name))
|
2015-01-08 23:29:03 +08:00
|
|
|
|
|
|
|
|
|
;;;###autoload
|
|
|
|
|
(defalias 'clang-format 'clang-format-region)
|
2014-11-05 12:32:54 +08:00
|
|
|
|
|
|
|
|
|
(provide 'clang-format)
|
2015-01-08 23:29:03 +08:00
|
|
|
|
;;; clang-format.el ends here
|