Skip to content

Commit dce9c3f

Browse files
Integrate with `read-extended-command-predicate'
Adds the option to specify a list of modes that the interactive commands are defined for. This way, people using a `read-extended-command-predicate' may have the "-region" and "-buffer" commands only available via M-x in those modes.
1 parent 8962e1d commit dce9c3f

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

reformatter-tests.el

+31
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,37 @@
8181
(reformatter-tests-shfmt-in-place-buffer)
8282
(should (equal "[ foo ] && echo yes\n" (buffer-string)))))
8383

84+
;; Formatting commands tagged for specific modes: `command-modes' checks which
85+
;; modes they're defined to be interactively usable in, but it's only available
86+
;; in Emacs 28 and newer.
87+
(when (fboundp 'command-modes)
88+
(reformatter-define reformatter-tests-shfmt-no-interactive-modes
89+
:program "shfmt")
90+
91+
(ert-deftest reformatter-tests-no-interactive-modes ()
92+
(should (not (command-modes 'reformatter-tests-shfmt-no-interactive-modes-buffer)))
93+
(should (not (command-modes 'reformatter-tests-shfmt-no-interactive-modes-region))))
94+
95+
(reformatter-define reformatter-tests-shfmt-single-interactive-mode
96+
:program "shfmt"
97+
:interactive-modes (sh-mode))
98+
99+
(ert-deftest reformatter-tests-single-interactive-mode ()
100+
(should (equal (command-modes 'reformatter-tests-shfmt-single-interactive-mode-buffer)
101+
'(sh-mode)))
102+
(should (equal (command-modes 'reformatter-tests-shfmt-single-interactive-mode-region)
103+
'(sh-mode))))
104+
105+
(reformatter-define reformatter-tests-shfmt-multiple-interactive-modes
106+
:program "shfmt"
107+
:interactive-modes (sh-mode haskell-mode))
108+
109+
(ert-deftest reformatter-tests-multiple-interactive-modes ()
110+
(should (equal (command-modes 'reformatter-tests-shfmt-multiple-interactive-modes-buffer)
111+
'(sh-mode haskell-mode)))
112+
(should (equal (command-modes 'reformatter-tests-shfmt-multiple-interactive-modes-region)
113+
'(sh-mode haskell-mode)))))
114+
84115

85116
(provide 'reformatter-tests)
86117
;;; reformatter-tests.el ends here

reformatter.el

+13-4
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ WORKING-DIRECTORY see the documentation of the `reformatter-define' macro."
143143
(delete-file stdout-file))))
144144

145145
;;;###autoload
146-
(cl-defmacro reformatter-define (name &key program args (mode t) (stdin t) (stdout t) input-file lighter keymap group (exit-code-success-p 'zerop) working-directory)
146+
(cl-defmacro reformatter-define (name &key program args (mode t) (stdin t) (stdout t) input-file lighter keymap group (exit-code-success-p 'zerop) working-directory interactive-modes)
147147
"Define a reformatter command with NAME.
148148
149149
When called, the reformatter will use PROGRAM and any ARGS to
@@ -241,7 +241,16 @@ WORKING-DIRECTORY
241241
242242
Directory where your reformatter program is started. If provided, this
243243
should be a form that evaluates to a string at runtime. Default is the
244-
value of `default-directory' in the buffer."
244+
value of `default-directory' in the buffer.
245+
246+
INTERACTIVE-MODES
247+
248+
If provided, this is a list of mode names (as unquoted
249+
symbols). The created commands for formatting regions and
250+
buffers are then tagged for interactive use in these modes,
251+
making them compatible with some built-in predicate functions
252+
for `read-extended-command-predicate', like
253+
`command-completion-default-include-p'."
245254
(declare (indent defun))
246255
(cl-assert (symbolp name))
247256
(cl-assert (functionp exit-code-success-p))
@@ -282,7 +291,7 @@ might use:
282291
"Reformats the region from BEG to END.
283292
When called interactively, or with prefix argument
284293
DISPLAY-ERRORS, shows a buffer if the formatting fails."
285-
(interactive "rp")
294+
(interactive "rp" ,@interactive-modes)
286295
(let ((input-file ,(if input-file
287296
input-file
288297
`(reformatter--make-temp-file ',name))))
@@ -300,7 +309,7 @@ DISPLAY-ERRORS, shows a buffer if the formatting fails."
300309
"Reformats the current buffer.
301310
When called interactively, or with prefix argument
302311
DISPLAY-ERRORS, shows a buffer if the formatting fails."
303-
(interactive "p")
312+
(interactive "p" ,@interactive-modes)
304313
(message "Formatting buffer")
305314
(,region-fn-name (point-min) (point-max) display-errors))
306315

0 commit comments

Comments
 (0)