-;;; clang-format.el --- Format code using clang-format
+;;; clang-format.el --- Format code using clang-format  -*- lexical-binding: t; -*-
 
 ;; Keywords: tools, c
 ;; Package-Requires: ((cl-lib "0.3"))

  (unless style
    (setq style clang-format-style))

  (let ((start (1- (position-bytes char-start)))
        (end (1- (position-bytes char-end)))
        (cursor (1- (position-bytes (point))))
        (temp-buffer (generate-new-buffer " *clang-format-temp*"))
        (temp-file (make-temp-file "clang-format")))
    (unwind-protect
        (let (status stderr operations)
          (setq status
                (call-process-region
                 (point-min) (point-max) clang-format-executable
                 nil `(,temp-buffer ,temp-file) nil

                 "-output-replacements-xml"
                 "-assume-filename" (or (buffer-file-name) "")
                 "-style" style
                 "-offset" (number-to-string start)
                 "-length" (number-to-string (- end start))
                 "-cursor" (number-to-string cursor)))
          (setq stderr
                (with-temp-buffer
                  (insert-file-contents temp-file)
                  (when (> (point-max) (point-min))
                    (insert ": "))
                  (buffer-substring-no-properties
                   (point-min) (line-end-position))))

          (cond
           ((stringp status)
            (error "(clang-format killed by signal %s%s)" status stderr))
           ((not (equal 0 status))
            (error "(clang-format failed with code %d%s)" status stderr)))

          (with-current-buffer temp-buffer
            (setq operations (clang-format--extract (car (xml-parse-region)))))

          (let ((replacements (nth 0 operations))
                (cursor (nth 1 operations))
                (incomplete-format (nth 2 operations)))
            (save-excursion
              (mapc (lambda (rpl)
                      (apply #'clang-format--replace rpl))
                    replacements))
            (when cursor
              (goto-char (byte-to-position (1+ cursor))))
            (message "%s" incomplete-format)
            (if incomplete-format
                (message "(clang-format: incomplete (syntax errors)%s)" stderr)
              (message "(clang-format: success%s)" stderr))))


A string containing the name or the full path of the executable."
  :group 'clang-format
  :type '(file :must-match t)
  :risky t)

(defcustom clang-format-style "file"
