 ;;; clang-format.el --- Format code using clang-format
 
 ;; Keywords: tools, c
-;; Package-Requires: ((json "1.3"))
+;; Package-Requires: ((cl-lib "0.3"))
 
 ;;; Commentary:
 

  :safe #'stringp)
(make-variable-buffer-local 'clang-format-style)

;;;###autoload
(defun clang-format-region (start end &optional style)
  "Use clang-format to format the code between START and END according to STYLE.

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

  (let ((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 (1- start))
                 "-length" (number-to-string (- end start))
                 "-cursor" (number-to-string (1- (point)))))
          (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))
           (t (message "(clang-format succeeded%s)" stderr)))

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

          (let ((replacements (car operations))
                (cursor (cdr operations)))
            (save-excursion
              (mapc (lambda (rpl)
                      (apply #'clang-format--replace rpl))
                    replacements))
            (when cursor
              (goto-char cursor))))
      (delete-file temp-file)
      (when (buffer-name temp-buffer) (kill-buffer temp-buffer)))))

;;;###autoload
(defun clang-format-buffer (&optional style)
