| 1 | ;;; clang-format.el --- Format code using clang-format -*- lexical-binding: t; -*- |
| 2 | |
| 3 | ;; Keywords: tools, c |
| 4 | ;; Package-Requires: ((cl-lib "0.3")) |
| 5 | |
| 6 | ;;; Commentary: |
| 7 | |
| 8 | ;; This package allows to filter code through clang-format to fix its formatting. |
| 9 | ;; clang-format is a tool that formats C/C++/Obj-C code according to a set of |
| 10 | ;; style options, see <http://clang.llvm.org/docs/ClangFormatStyleOptions.html>. |
| 11 | ;; Note that clang-format 3.4 or newer is required. |
| 12 | |
| 13 | ;; clang-format.el is available via MELPA and can be installed via |
| 14 | ;; |
| 15 | ;; M-x package-install clang-format |
| 16 | ;; |
| 17 | ;; when ("melpa" . "http://melpa.org/packages/") is included in |
| 18 | ;; `package-archives'. Alternatively, ensure the directory of this |
| 19 | ;; file is in your `load-path' and add |
| 20 | ;; |
| 21 | ;; (require 'clang-format) |
| 22 | ;; |
| 23 | ;; to your .emacs configuration. |
| 24 | |
| 25 | ;; You may also want to bind `clang-format-region' to a key: |
| 26 | ;; |
| 27 | ;; (global-set-key [C-M-tab] 'clang-format-region) |
| 28 | |
| 29 | ;;; Code: |
| 30 | |
| 31 | (require 'cl-lib) |
| 32 | (require 'xml) |
| 33 | |
| 34 | (defgroup clang-format nil |
| 35 | "Format code using clang-format." |
| 36 | :group 'tools) |
| 37 | |
| 38 | (defcustom clang-format-executable |
| 39 | (or (executable-find "clang-format") |
| 40 | "clang-format") |
| 41 | "Location of the clang-format executable. |
| 42 | |
| 43 | A string containing the name or the full path of the executable." |
| 44 | :group 'clang-format |
| 45 | :type '(file :must-match t) |
| 46 | :risky t) |
| 47 | |
| 48 | (defcustom clang-format-style "file" |
| 49 | "Style argument to pass to clang-format. |
| 50 | |
| 51 | By default clang-format will load the style configuration from |
| 52 | a file named .clang-format located in one of the parent directories |
| 53 | of the buffer." |
| 54 | :group 'clang-format |
| 55 | :type 'string |
| 56 | :safe #'stringp) |
| 57 | (make-variable-buffer-local 'clang-format-style) |
| 58 | |
| 59 | (defun clang-format--extract (xml-node) |
| 60 | "Extract replacements and cursor information from XML-NODE." |
| 61 | (unless (and (listp xml-node) (eq (xml-node-name xml-node) 'replacements)) |
| 62 | (error "Expected <replacements> node")) |
| 63 | (let ((nodes (xml-node-children xml-node)) |
| 64 | (incomplete-format (xml-get-attribute xml-node 'incomplete_format)) |
| 65 | replacements |
| 66 | cursor) |
| 67 | (dolist (node nodes) |
| 68 | (when (listp node) |
| 69 | (let* ((children (xml-node-children node)) |
| 70 | (text (car children))) |
| 71 | (cl-case (xml-node-name node) |
| 72 | ('replacement |
| 73 | (let* ((offset (xml-get-attribute-or-nil node 'offset)) |
| 74 | (length (xml-get-attribute-or-nil node 'length))) |
| 75 | (when (or (null offset) (null length)) |
| 76 | (error "<replacement> node does not have offset and length attributes")) |
| 77 | (when (cdr children) |
| 78 | (error "More than one child node in <replacement> node")) |
| 79 | |
| 80 | (setq offset (string-to-number offset)) |
| 81 | (setq length (string-to-number length)) |
| 82 | (push (list offset length text) replacements))) |
| 83 | ('cursor |
| 84 | (setq cursor (string-to-number text))))))) |
| 85 | |
| 86 | ;; Sort by decreasing offset, length. |
| 87 | (setq replacements (sort (delq nil replacements) |
| 88 | (lambda (a b) |
| 89 | (or (> (car a) (car b)) |
| 90 | (and (= (car a) (car b)) |
| 91 | (> (cadr a) (cadr b))))))) |
| 92 | |
| 93 | (list replacements cursor (string= incomplete-format "true")))) |
| 94 | |
| 95 | (defun clang-format--replace (offset length &optional text) |
| 96 | "Replace the region defined by OFFSET and LENGTH with TEXT. |
| 97 | OFFSET and LENGTH are measured in bytes, not characters. OFFSET |
| 98 | is a zero-based file offset, assuming ‘utf-8-unix’ coding." |
| 99 | (let ((start (clang-format--filepos-to-bufferpos offset 'exact 'utf-8-unix)) |
| 100 | (end (clang-format--filepos-to-bufferpos (+ offset length) 'exact |
| 101 | 'utf-8-unix))) |
| 102 | (goto-char start) |
| 103 | (delete-region start end) |
| 104 | (when text |
| 105 | (insert text)))) |
| 106 | |
| 107 | ;; ‘bufferpos-to-filepos’ and ‘filepos-to-bufferpos’ are new in Emacs 25.1. |
| 108 | ;; Provide fallbacks for older versions. |
| 109 | (defalias 'clang-format--bufferpos-to-filepos |
| 110 | (if (fboundp 'bufferpos-to-filepos) |
| 111 | 'bufferpos-to-filepos |
| 112 | (lambda (position &optional _quality _coding-system) |
| 113 | (1- (position-bytes position))))) |
| 114 | |
| 115 | (defalias 'clang-format--filepos-to-bufferpos |
| 116 | (if (fboundp 'filepos-to-bufferpos) |
| 117 | 'filepos-to-bufferpos |
| 118 | (lambda (byte &optional _quality _coding-system) |
| 119 | (byte-to-position (1+ byte))))) |
| 120 | |
| 121 | ;;;###autoload |
| 122 | (defun clang-format-region (start end &optional style assume-file-name) |
| 123 | "Use clang-format to format the code between START and END according to STYLE. |
| 124 | If called interactively uses the region or the current statement if there is no |
| 125 | no active region. If no STYLE is given uses `clang-format-style'. Use |
| 126 | ASSUME-FILE-NAME to locate a style config file, if no ASSUME-FILE-NAME is given |
| 127 | uses the function `buffer-file-name'." |
| 128 | (interactive |
| 129 | (if (use-region-p) |
| 130 | (list (region-beginning) (region-end)) |
| 131 | (list (point) (point)))) |
| 132 | |
| 133 | (unless style |
| 134 | (setq style clang-format-style)) |
| 135 | |
| 136 | (unless assume-file-name |
| 137 | (setq assume-file-name buffer-file-name)) |
| 138 | |
| 139 | (let ((file-start (clang-format--bufferpos-to-filepos start 'approximate |
| 140 | 'utf-8-unix)) |
| 141 | (file-end (clang-format--bufferpos-to-filepos end 'approximate |
| 142 | 'utf-8-unix)) |
| 143 | (cursor (clang-format--bufferpos-to-filepos (point) 'exact 'utf-8-unix)) |
| 144 | (temp-buffer (generate-new-buffer " *clang-format-temp*")) |
| 145 | (temp-file (make-temp-file "clang-format")) |
| 146 | ;; Output is XML, which is always UTF-8. Input encoding should match |
| 147 | ;; the encoding used to convert between buffer and file positions, |
| 148 | ;; otherwise the offsets calculated above are off. For simplicity, we |
| 149 | ;; always use ‘utf-8-unix’ and ignore the buffer coding system. |
| 150 | (default-process-coding-system '(utf-8-unix . utf-8-unix))) |
| 151 | (unwind-protect |
| 152 | (let ((status (apply #'call-process-region |
| 153 | nil nil clang-format-executable |
| 154 | nil `(,temp-buffer ,temp-file) nil |
| 155 | `("-output-replacements-xml" |
| 156 | ;; Guard against a nil assume-file-name. |
| 157 | ;; If the clang-format option -assume-filename |
| 158 | ;; is given a blank string it will crash as per |
| 159 | ;; the following bug report |
| 160 | ;; https://bugs.llvm.org/show_bug.cgi?id=34667 |
| 161 | ,@(and assume-file-name |
| 162 | (list "-assume-filename" assume-file-name)) |
| 163 | "-style" ,style |
| 164 | "-offset" ,(number-to-string file-start) |
| 165 | "-length" ,(number-to-string (- file-end file-start)) |
| 166 | "-cursor" ,(number-to-string cursor)))) |
| 167 | (stderr (with-temp-buffer |
| 168 | (unless (zerop (cadr (insert-file-contents temp-file))) |
| 169 | (insert ": ")) |
| 170 | (buffer-substring-no-properties |
| 171 | (point-min) (line-end-position))))) |
| 172 | (cond |
| 173 | ((stringp status) |
| 174 | (error "(clang-format killed by signal %s%s)" status stderr)) |
| 175 | ((not (zerop status)) |
| 176 | (error "(clang-format failed with code %d%s)" status stderr))) |
| 177 | |
| 178 | (cl-destructuring-bind (replacements cursor incomplete-format) |
| 179 | (with-current-buffer temp-buffer |
| 180 | (clang-format--extract (car (xml-parse-region)))) |
| 181 | (save-excursion |
| 182 | (dolist (rpl replacements) |
| 183 | (apply #'clang-format--replace rpl))) |
| 184 | (when cursor |
| 185 | (goto-char (clang-format--filepos-to-bufferpos cursor 'exact |
| 186 | 'utf-8-unix))) |
| 187 | (if incomplete-format |
| 188 | (message "(clang-format: incomplete (syntax errors)%s)" stderr) |
| 189 | (message "(clang-format: success%s)" stderr)))) |
| 190 | (delete-file temp-file) |
| 191 | (when (buffer-name temp-buffer) (kill-buffer temp-buffer))))) |
| 192 | |
| 193 | ;;;###autoload |
| 194 | (defun clang-format-buffer (&optional style assume-file-name) |
| 195 | "Use clang-format to format the current buffer according to STYLE. |
| 196 | If no STYLE is given uses `clang-format-style'. Use ASSUME-FILE-NAME |
| 197 | to locate a style config file. If no ASSUME-FILE-NAME is given uses |
| 198 | the function `buffer-file-name'." |
| 199 | (interactive) |
| 200 | (clang-format-region (point-min) (point-max) style assume-file-name)) |
| 201 | |
| 202 | ;;;###autoload |
| 203 | (defalias 'clang-format 'clang-format-region) |
| 204 | |
| 205 | (provide 'clang-format) |
| 206 | ;;; clang-format.el ends here |
| 207 | |