Lines Matching full:format

1 ;;; clang-format.el --- Format code using clang-format  -*- lexical-binding: t; -*-
9 ;; This package allows to filter code through clang-format to fix its formatting.
10 ;; clang-format is a tool that formats C/C++/Obj-C code according to a set of
12 ;; Note that clang-format 3.4 or newer is required.
14 ;; clang-format.el is available via MELPA and can be installed via
16 ;; M-x package-install clang-format
22 ;; (require 'clang-format)
26 ;; You may also want to bind `clang-format-region' to a key:
28 ;; (global-set-key [C-M-tab] 'clang-format-region)
35 (defgroup clang-format nil
36 "Format code using clang-format."
39 (defcustom clang-format-executable
40 (or (executable-find "clang-format")
41 "clang-format")
42 "Location of the clang-format executable.
45 :group 'clang-format
49 (defcustom clang-format-style nil
50 "Style argument to pass to clang-format.
52 By default clang-format will load the style configuration from
53 a file named .clang-format located in one of the parent directories
55 :group 'clang-format
58 (make-variable-buffer-local 'clang-format-style)
60 (defcustom clang-format-fallback-style "none"
61 "Fallback style to pass to clang-format.
63 This style will be used if clang-format-style is set to \"file\"
64 and no .clang-format is found in the directory of the buffer or
67 :group 'clang-format
70 (make-variable-buffer-local 'clang-format-fallback-style)
72 (defun clang-format--extract (xml-node)
77 (incomplete-format (xml-get-attribute xml-node 'incomplete_format))
106 (list replacements cursor (string= incomplete-format "true"))))
108 (defun clang-format--replace (offset length &optional text)
112 (let ((start (clang-format--filepos-to-bufferpos offset 'exact 'utf-8-unix))
113 (end (clang-format--filepos-to-bufferpos (+ offset length) 'exact
122 (defalias 'clang-format--bufferpos-to-filepos
128 (defalias 'clang-format--filepos-to-bufferpos
135 (defun clang-format-region (start end &optional style assume-file-name)
136 "Use clang-format to format the code between START and END according to STYLE.
138 no active region. If no STYLE is given uses `clang-format-style'. Use
147 (setq style clang-format-style))
152 (let ((file-start (clang-format--bufferpos-to-filepos start 'approximate
154 (file-end (clang-format--bufferpos-to-filepos end 'approximate
156 (cursor (clang-format--bufferpos-to-filepos (point) 'exact 'utf-8-unix))
157 (temp-buffer (generate-new-buffer " *clang-format-temp*"))
158 (temp-file (make-temp-file "clang-format"))
166 nil nil clang-format-executable
170 ;; If the clang-format option -assume-filename
177 "-fallback-style" ,clang-format-fallback-style
188 (error "(clang-format killed by signal %s%s)" status stderr))
190 (error "(clang-format failed with code %d%s)" status stderr)))
192 (cl-destructuring-bind (replacements cursor incomplete-format)
194 (clang-format--extract (car (xml-parse-region))))
197 (apply #'clang-format--replace rpl)))
199 (goto-char (clang-format--filepos-to-bufferpos cursor 'exact
201 (if incomplete-format
202 (message "(clang-format: incomplete (syntax errors)%s)" stderr)
203 (message "(clang-format: success%s)" stderr))))
208 (defun clang-format-buffer (&optional style assume-file-name)
209 "Use clang-format to format the current buffer according to STYLE.
210 If no STYLE is given uses `clang-format-style'. Use ASSUME-FILE-NAME
214 (clang-format-region (point-min) (point-max) style assume-file-name))
217 (defalias 'clang-format 'clang-format-region)
219 (provide 'clang-format)
220 ;;; clang-format.el ends here