editor/format: refactor +format-buffer & users
This commit is contained in:
parent
1b4040229e
commit
349fb49c7b
1 changed files with 36 additions and 34 deletions
|
@ -96,7 +96,7 @@ of the buffer to apply formatting to."
|
|||
;;
|
||||
|
||||
;;;###autoload
|
||||
(defun +format-buffer ()
|
||||
(defun +format-buffer (formatter mode-result)
|
||||
"Format the source code in the current buffer.
|
||||
|
||||
Returns any of the following values:
|
||||
|
@ -111,11 +111,8 @@ position of the first change in the buffer.
|
|||
|
||||
See `+format/buffer' for the interactive version of this function, and
|
||||
`+format|buffer' to use as a `before-save-hook' hook."
|
||||
(require 'format-all)
|
||||
(cl-destructuring-bind (formatter mode-result)
|
||||
(format-all-probe)
|
||||
(unless formatter
|
||||
(error "Don't know how to format %S code" major-mode))
|
||||
(user-error "Don't know how to format '%s' code" major-mode))
|
||||
(let ((f-function (gethash formatter format-all-format-table))
|
||||
(executable (format-all-formatter-executable formatter)))
|
||||
(cl-destructuring-bind (output errput first-diff)
|
||||
|
@ -138,7 +135,7 @@ See `+format/buffer' for the interactive version of this function, and
|
|||
(kill-buffer patchbuf)
|
||||
(delete-file tmpfile))))
|
||||
(unless (= 0 (length errput))
|
||||
(message "Formatter error output:\n%s" errput)))))))
|
||||
(message "Formatter error output:\n%s" errput))))))
|
||||
|
||||
|
||||
;;
|
||||
|
@ -149,10 +146,7 @@ See `+format/buffer' for the interactive version of this function, and
|
|||
(defun +format/buffer ()
|
||||
"Format the source code in the current buffer."
|
||||
(interactive)
|
||||
(pcase (+format-buffer)
|
||||
(`error (message "Failed to format buffer due to errors"))
|
||||
(`noop (message "Buffer was already formatted"))
|
||||
(_ (message "Formatted (%s)" (car (format-all-probe))))))
|
||||
(+format|buffer))
|
||||
|
||||
;;;###autoload
|
||||
(defun +format/region (beg end)
|
||||
|
@ -186,5 +180,13 @@ is selected)."
|
|||
(add-hook 'before-save-hook #'+format|buffer nil t))
|
||||
|
||||
;;;###autoload
|
||||
(defalias '+format|buffer #'+format-buffer)
|
||||
(defun +format|buffer ()
|
||||
"Format the source code in the current buffer with minimal feedback.
|
||||
|
||||
Meant for `before-save-hook'."
|
||||
(cl-destructuring-bind (formatter mode-result) (format-all-probe)
|
||||
(pcase (+format-buffer formatter mode-result)
|
||||
(`error (message "Failed to format buffer due to errors"))
|
||||
(`noop (message "Buffer was already formatted"))
|
||||
(_ (message "Formatted (%s)" formatter)))))
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue