diff --git a/modules/editor/format/autoload/format.el b/modules/editor/format/autoload/format.el index 7d2972aa0..d4e3ff8a8 100644 --- a/modules/editor/format/autoload/format.el +++ b/modules/editor/format/autoload/format.el @@ -86,7 +86,19 @@ Stolen shamelessly from go-mode" ;; ;; 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 (defun +format-buffer (formatter mode-result) @@ -156,31 +168,32 @@ See `+format/buffer' for the interactive version of this function, and ;; Commands ;;;###autoload -(defun +format/buffer () +(defun +format/buffer (&optional arg) "Format the source code in the current buffer." - (interactive) - (+format|buffer)) + (interactive "P") + (let ((+format-with (if arg (+format-completing-read)))) + (+format|buffer arg))) ;;;###autoload -(defun +format/region (beg end) +(defun +format/region (beg end &optional arg) "Runs the active formatter on the lines within BEG and END. 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 snippets or single lines." - (interactive "r") + (interactive "rP") (save-restriction (narrow-to-region beg end) - (+format/buffer))) + (+format/buffer arg))) ;;;###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 is selected)." - (interactive "r") + (interactive "rP") (if (use-region-p) - (+format/region beg end) - (+format/buffer))) + (+format/region beg end arg) + (+format/buffer arg))) ;; diff --git a/modules/editor/format/config.el b/modules/editor/format/config.el index 438eb970e..1ad72e590 100644 --- a/modules/editor/format/config.el +++ b/modules/editor/format/config.el @@ -3,14 +3,17 @@ (defvar +format-on-save-enabled-modes '(not emacs-lisp-mode ; elisp's mechanisms are good enough sql-mode) ; sqlformat is currently broken - "A list of major modes in which to enable `format-all-mode'. - -This mode will auto-format buffers when you save them. + "A list of major modes in which to reformat the buffer upon saving. If this list begins with `not', then it negates the list. 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 - 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) (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)