branch: elpa/helm
commit 2a940981027f35b6f9330c6454f99e3e84438628
Author: Thierry Volpiatto <thie...@posteo.net>
Commit: Thierry Volpiatto <thie...@posteo.net>

    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.
---
 helm-command.el | 24 +++++++++++++++++++++++-
 1 file changed, 23 insertions(+), 1 deletion(-)

diff --git a/helm-command.el b/helm-command.el
index e2fa11e6ab..4722dc0be0 100644
--- a/helm-command.el
+++ b/helm-command.el
@@ -59,6 +59,12 @@ This value can be toggled with
   "When nil, do not sort helm-M-x's commands history."
   :type 'boolean)
 
+(defcustom helm-M-x-exclude-unusable-commands-in-mode nil
+  "When non nil exclude commands not usable in current buffer.
+This will exclude only commands defined with `interactive' MODES argument, for
+other commands, they will be displayed even if unusable as lon as they 
satisfies
+`commandp'."
+  :type 'boolean)
 
 ;;; Faces
 ;;
@@ -401,6 +407,18 @@ Save COMMAND to `extended-command-history'."
           (helm-mode 1))
       (read-extended-command)))))
 
+(defun helm-M-x--mode-predicate (sym cur-mode)
+  "Check is symbol SYM is suitable for current mode CUR-MODE.
+This predicate honors commands defined with the `interactive' MODES argument."
+  (let ((modes (command-modes sym)))
+    (and (commandp sym)
+         (if modes
+             (or (memq cur-mode modes)
+                 (memq (car modes)
+                  (buffer-local-value 'local-minor-modes helm-current-buffer))
+                 (memq (car modes) global-minor-modes))
+           t))))
+
 ;;;###autoload
 (defun helm-M-x (_arg)
   "Preconfigured `helm' for Emacs commands.
@@ -420,7 +438,11 @@ You can get help on each command by persistent action."
      (list current-prefix-arg)))
   (if (or defining-kbd-macro executing-kbd-macro)
       (helm-M-x--vanilla-M-x)
-  (helm-M-x-read-extended-command obarray)))
+    (helm-M-x-read-extended-command
+     obarray (if (and (fboundp 'command-modes)
+                      helm-M-x-exclude-unusable-commands-in-mode)
+                 (lambda (sym) (helm-M-x--mode-predicate sym major-mode))
+               #'commandp))))
 (put 'helm-M-x 'interactive-only 'command-execute)
 
 (provide 'helm-command)

Reply via email to