Home | History | Annotate | Download | only in clang

Lines Matching defs:format

1 ;;; clang-format.el --- Format code using clang-format  -*- lexical-binding: t; -*-
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
11 ;; Note that clang-format 3.4 or newer is required.
13 ;; clang-format.el is available via MELPA and can be installed via
15 ;; M-x package-install clang-format
21 ;; (require 'clang-format)
25 ;; You may also want to bind `clang-format-region' to a key:
27 ;; (global-set-key [C-M-tab] 'clang-format-region)
34 (defgroup clang-format nil
35 "Format code using clang-format."
38 (defcustom clang-format-executable
39 (or (executable-find "clang-format")
40 "clang-format")
41 "Location of the clang-format executable.
44 :group 'clang-format
48 (defcustom clang-format-style "file"
49 "Style argument to pass to clang-format.
51 By default clang-format will load the style configuration from
52 a file named .clang-format located in one of the parent directories
54 :group 'clang-format
57 (make-variable-buffer-local 'clang-format-style)
59 (defun clang-format--extract (xml-node)
64 (incomplete-format (xml-get-attribute xml-node 'incomplete_format))
93 (list replacements cursor (string= incomplete-format "true"))))
95 (defun clang-format--replace (offset length &optional text)
99 (let ((start (clang-format--filepos-to-bufferpos offset 'exact 'utf-8-unix))
100 (end (clang-format--filepos-to-bufferpos (+ offset length) 'exact
109 (defalias 'clang-format--bufferpos-to-filepos
115 (defalias 'clang-format--filepos-to-bufferpos
122 (defun clang-format-region (start end &optional style)
123 "Use clang-format to format the code between START and END according to STYLE.
125 is no active region. If no style is given uses `clang-format-style'."
132 (setq style clang-format-style))
134 (let ((file-start (clang-format--bufferpos-to-filepos start 'approximate
136 (file-end (clang-format--bufferpos-to-filepos end 'approximate
138 (cursor (clang-format--bufferpos-to-filepos (point) 'exact 'utf-8-unix))
139 (temp-buffer (generate-new-buffer " *clang-format-temp*"))
140 (temp-file (make-temp-file "clang-format"))
148 nil nil clang-format-executable
164 (error "(clang-format killed by signal %s%s)" status stderr))
166 (error "(clang-format failed with code %d%s)" status stderr)))
168 (cl-destructuring-bind (replacements cursor incomplete-format)
170 (clang-format--extract (car (xml-parse-region))))
173 (apply #'clang-format--replace rpl)))
175 (goto-char (clang-format--filepos-to-bufferpos cursor 'exact
177 (if incomplete-format
178 (message "(clang-format: incomplete (syntax errors)%s)" stderr)
179 (message "(clang-format: success%s)" stderr))))
184 (defun clang-format-buffer (&optional style)
185 "Use clang-format to format the current buffer according to STYLE."
187 (clang-format-region (point-min) (point-max) style))
190 (defalias 'clang-format 'clang-format-region)
192 (provide 'clang-format)
193 ;;; clang-format.el ends here