Skip to content

Commit 2a94098

Browse files
Allow excluding commands not usable commands in helm-M-x
This is controlled by new user var helm-M-x-exclude-unusable-commands-in-mode.
1 parent 5b24aad commit 2a94098

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

helm-command.el

+23-1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ This value can be toggled with
5959
"When nil, do not sort helm-M-x's commands history."
6060
:type 'boolean)
6161

62+
(defcustom helm-M-x-exclude-unusable-commands-in-mode nil
63+
"When non nil exclude commands not usable in current buffer.
64+
This will exclude only commands defined with `interactive' MODES argument, for
65+
other commands, they will be displayed even if unusable as lon as they satisfies
66+
`commandp'."
67+
:type 'boolean)
6268

6369
;;; Faces
6470
;;
@@ -401,6 +407,18 @@ Save COMMAND to `extended-command-history'."
401407
(helm-mode 1))
402408
(read-extended-command)))))
403409

410+
(defun helm-M-x--mode-predicate (sym cur-mode)
411+
"Check is symbol SYM is suitable for current mode CUR-MODE.
412+
This predicate honors commands defined with the `interactive' MODES argument."
413+
(let ((modes (command-modes sym)))
414+
(and (commandp sym)
415+
(if modes
416+
(or (memq cur-mode modes)
417+
(memq (car modes)
418+
(buffer-local-value 'local-minor-modes helm-current-buffer))
419+
(memq (car modes) global-minor-modes))
420+
t))))
421+
404422
;;;###autoload
405423
(defun helm-M-x (_arg)
406424
"Preconfigured `helm' for Emacs commands.
@@ -420,7 +438,11 @@ You can get help on each command by persistent action."
420438
(list current-prefix-arg)))
421439
(if (or defining-kbd-macro executing-kbd-macro)
422440
(helm-M-x--vanilla-M-x)
423-
(helm-M-x-read-extended-command obarray)))
441+
(helm-M-x-read-extended-command
442+
obarray (if (and (fboundp 'command-modes)
443+
helm-M-x-exclude-unusable-commands-in-mode)
444+
(lambda (sym) (helm-M-x--mode-predicate sym major-mode))
445+
#'commandp))))
424446
(put 'helm-M-x 'interactive-only 'command-execute)
425447

426448
(provide 'helm-command)

0 commit comments

Comments
 (0)