editor/format: add +format-with option

+ A formatter can now be specified explicitly buffer-locally by setting
  +format-with to a symbol representing the name of the
  formatter (accepts any key of format-all-format-table)
+ Passing C-u to any of +format/buffer, +format/region or
  +format/region-or-buffer will now prompt you to select a formatter.
+ Revise docstring for +format-on-save-enabled-modes
This commit is contained in:
Henrik Lissner 2018-09-08 23:36:24 -04:00
parent 46083ed398
commit 8480f52081
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 39 additions and 15 deletions

View file

@ -86,7 +86,19 @@ Stolen shamelessly from go-mode"
;; ;;
;; Public library ;; Public library
;;
(defun +format-completing-read ()
(require 'format-all)
(let* ((fmtlist (mapcar #'symbol-name (hash-table-keys format-all-format-table)))
(fmt (completing-read "Formatter: " fmtlist)))
(if fmt (cons (intern fmt) t))))
;;;###autoload
(defun +format*probe (orig-fn)
"Use `+format-with' instead, if it is set."
(if +format-with
(cons +format-with t)
(funcall orig-fn)))
;;;###autoload ;;;###autoload
(defun +format-buffer (formatter mode-result) (defun +format-buffer (formatter mode-result)
@ -156,31 +168,32 @@ See `+format/buffer' for the interactive version of this function, and
;; Commands ;; Commands
;;;###autoload ;;;###autoload
(defun +format/buffer () (defun +format/buffer (&optional arg)
"Format the source code in the current buffer." "Format the source code in the current buffer."
(interactive) (interactive "P")
(+format|buffer)) (let ((+format-with (if arg (+format-completing-read))))
(+format|buffer arg)))
;;;###autoload ;;;###autoload
(defun +format/region (beg end) (defun +format/region (beg end &optional arg)
"Runs the active formatter on the lines within BEG and END. "Runs the active formatter on the lines within BEG and END.
WARNING: this may not work everywhere. It will throw errors if the region WARNING: this may not work everywhere. It will throw errors if the region
contains a syntax error in isolation. It is mostly useful for formatting contains a syntax error in isolation. It is mostly useful for formatting
snippets or single lines." snippets or single lines."
(interactive "r") (interactive "rP")
(save-restriction (save-restriction
(narrow-to-region beg end) (narrow-to-region beg end)
(+format/buffer))) (+format/buffer arg)))
;;;###autoload ;;;###autoload
(defun +format/region-or-buffer (beg end) (defun +format/region-or-buffer (beg end &optional arg)
"Runs the active formatter on the selected region (or whole buffer, if nothing "Runs the active formatter on the selected region (or whole buffer, if nothing
is selected)." is selected)."
(interactive "r") (interactive "rP")
(if (use-region-p) (if (use-region-p)
(+format/region beg end) (+format/region beg end arg)
(+format/buffer))) (+format/buffer arg)))
;; ;;

View file

@ -3,14 +3,17 @@
(defvar +format-on-save-enabled-modes (defvar +format-on-save-enabled-modes
'(not emacs-lisp-mode ; elisp's mechanisms are good enough '(not emacs-lisp-mode ; elisp's mechanisms are good enough
sql-mode) ; sqlformat is currently broken sql-mode) ; sqlformat is currently broken
"A list of major modes in which to enable `format-all-mode'. "A list of major modes in which to reformat the buffer upon saving.
This mode will auto-format buffers when you save them.
If this list begins with `not', then it negates the list. If this list begins with `not', then it negates the list.
If it is `t', it is enabled in all modes. If it is `t', it is enabled in all modes.
If nil, it is disabled in all modes, the same as if the +onsave flag wasn't If nil, it is disabled in all modes, the same as if the +onsave flag wasn't
used at all.") used at all.
Irrelevant if you do not have the +onsave flag enabled for this module.")
(defvar-local +format-with nil "Set this to explicitly use a certain formatter
for the current buffer.")
;; ;;
@ -32,3 +35,11 @@ This is controlled by `+format-on-save-enabled-modes'."
(when (featurep! +onsave) (when (featurep! +onsave)
(add-hook 'after-change-major-mode-hook #'+format|enable-on-save-maybe)) (add-hook 'after-change-major-mode-hook #'+format|enable-on-save-maybe))
;;
;; Hacks
;; Allow a specific formatter to be used by setting `+format-with', either
;; buffer-locally or let-bound.
(advice-add #'format-all-probe :around #'+format*probe)