@cindex obsolete command
When @kbd{M-x} completes on commands, it ignores the commands that
are declared @dfn{obsolete}; for these, you will have to type their
-full name. Obsolete commands are those for which newer, better
+full name. (Obsolete commands are those for which newer, better
alternatives exist, and which are slated for removal in some future
-Emacs release.
+Emacs release.)
+
+@vindex read-extended-command-predicate
+ In addition, @kbd{M-x} completion can exclude commands that are not
+relevant to, and generally cannot work with, the current buffer's
+major mode (@pxref{Major Modes}) and minor modes (@pxref{Minor
+Modes}). By default, no commands are excluded, but you can customize
+the option @var{read-extended-command-predicate} to exclude those
+irrelevant commands from completion results.
To cancel the @kbd{M-x} and not run a command, type @kbd{C-g} instead
of entering the command name. This takes you back to command level.
@kbd{M-x} works by running the command
@code{execute-extended-command}, which is responsible for reading the
name of another command and invoking it.
-
-@vindex read-extended-command-predicate
- This command heeds the @code{read-extended-command-predicate}
-variable, which will (by default) filter out commands that are not
-applicable to the current major mode (or enabled minor modes).
@vindex read-extended-command-predicate
This command heeds the @code{read-extended-command-predicate}
-variable, which will (by default) filter out commands that are not
-applicable to the current major mode (or enabled minor modes).
-@code{read-extended-command-predicate} will be called with two
-parameters: The symbol that is to be included or not, and the current
-buffer. If should return non-@code{nil} if the command is to be
-included when completing.
+variable, which can filter out commands that are not applicable to the
+current major mode (or enabled minor modes). By default, the value of
+this variable is @code{nil}, and no commands are filtered out.
+However, customizing it to invoke the function
+@code{command-completion-default-include-p} will perform
+mode-dependent filtering. @code{read-extended-command-predicate} can
+be any predicate function; it will be called with two parameters: the
+command's symbol and the current buffer. If should return
+non-@code{nil} if the command is to be included when completing in
+that buffer.
@end deffn
@node Distinguish Interactive
+++
** New user option 'read-extended-command-predicate'.
-This option controls how 'M-x TAB' performs completions. The default
-predicate excludes commands that are not applicable to the current
-major and minor modes, and also respects the command's completion
+This option controls how 'M-x' performs completion of commands when
+you type TAB. By default, any command that matches what you have
+typed is considered a completion candidate, but you can customize this
+option to exclude commands that are not applicable to the current
+buffer's major and minor modes, and respect the command's completion
predicate (if any).
---
(defvar extended-command-history nil)
(defvar execute-extended-command--last-typed nil)
-(defcustom read-extended-command-predicate
- #'command-completion-default-include-p
+(defcustom read-extended-command-predicate nil
"Predicate to use to determine which commands to include when completing.
-The predicate function is called with two parameters: The
-symbol (i.e., command) in question that should be included or
-not, and the current buffer. The predicate should return non-nil
-if the command should be present when doing `M-x TAB'."
+If it's nil, include all the commands.
+If it's a functoion, it will be called with two parameters: the
+symbol of the command and a buffer. The predicate should return
+non-nil if the command should be present when doing `M-x TAB'
+in that buffer."
:version "28.1"
- :type `(choice (const :tag "Exclude commands not relevant to the current mode"
+ :group 'completion
+ :type `(choice (const :tag "Don't exclude any commands" nil)
+ (const :tag "Exclude commands irrelevant to current buffer's mode"
command-completion-default-include-p)
- (const :tag "All commands" ,(lambda (_s _b) t))
(function :tag "Other function")))
(defun read-extended-command ()
(complete-with-action action obarray string pred)))
(lambda (sym)
(and (commandp sym)
- (funcall read-extended-command-predicate sym buffer)))
+ (or (null read-extended-command-predicate)
+ (and (functionp read-extended-command-predicate)
+ (funcall read-extended-command-predicate sym buffer)))))
t nil 'extended-command-history))))
(defun command-completion-default-include-p (symbol buffer)