Function: read-extended-command

read-extended-command is a byte-compiled function defined in simple.el.gz.

Signature

(read-extended-command)

Documentation

Read command name to invoke in execute-extended-command.

This function uses the read-extended-command-predicate user option.

Probably introduced at or before Emacs version 28.1.

Source Code

;; Defined in /usr/src/emacs/lisp/simple.el.gz
(defun read-extended-command ()
  "Read command name to invoke in `execute-extended-command'.
This function uses the `read-extended-command-predicate' user option."
  (let ((buffer (current-buffer)))
    (minibuffer-with-setup-hook
        (lambda ()
          (add-hook 'post-self-insert-hook
                    (lambda ()
                      (setq execute-extended-command--last-typed
                            (minibuffer-contents)))
                    nil 'local)
          (setq-local minibuffer-default-add-function
                      (lambda ()
                        ;; Get a command name at point in the original buffer
                        ;; to propose it after M-n.
                        (let ((def
                               (with-current-buffer
                                   (window-buffer (minibuffer-selected-window))
                                 (and (commandp (function-called-at-point))
				      (format
                                       "%S" (function-called-at-point)))))
                              (all (sort (minibuffer-default-add-completions)
                                         #'string<)))
                          (if def
                              (cons def (delete def all))
                            all)))))
      ;; Read a string, completing from and restricting to the set of
      ;; all defined commands.  Don't provide any initial input.
      ;; Save the command read on the extended-command history list.
      (completing-read
       (concat (cond
                ((eq current-prefix-arg '-) "- ")
                ((and (consp current-prefix-arg)
		      (eq (car current-prefix-arg) 4))
		 "C-u ")
                ((and (consp current-prefix-arg)
		      (integerp (car current-prefix-arg)))
                 (format "%d " (car current-prefix-arg)))
                ((integerp current-prefix-arg)
                 (format "%d " current-prefix-arg)))
	       ;; This isn't strictly correct if `execute-extended-command'
	       ;; is bound to anything else (e.g. [menu]).
	       ;; It could use (key-description (this-single-command-keys)),
	       ;; but actually a prompt other than "M-x" would be confusing,
	       ;; because "M-x" is a well-known prompt to read a command
	       ;; and it serves as a shorthand for "Extended command: ".
               (if (memq 'shift (event-modifiers last-command-event))
                   "M-X "
                 "M-x "))
       (lambda (string pred action)
         (if (and suggest-key-bindings (eq action 'metadata))
	     '(metadata
	       (affixation-function . read-extended-command--affixation)
	       (category . command))
           (let ((pred
                  (if (memq action '(nil t))
                      ;; Exclude from completions obsolete commands
                      ;; lacking a `current-name', or where `when' is
                      ;; not the current major version.
                      (lambda (sym)
                        (let ((obsolete (get sym 'byte-obsolete-info)))
                          (and (funcall pred sym)
                               (or (equal string (symbol-name sym))
                                   (not obsolete)
                                   (and
                                    ;; Has a current-name.
                                    (functionp (car obsolete))
                                    ;; when >= emacs-major-version
                                    (condition-case nil
                                        (>= (car (version-to-list
                                                  (caddr obsolete)))
                                            emacs-major-version)
                                      ;; If the obsoletion version isn't
                                      ;; valid, include the command.
                                      (error t)))))))
                    pred)))
             (complete-with-action action obarray string pred))))
       (lambda (sym)
         (and (commandp sym)
              (cond ((null read-extended-command-predicate))
                    ((functionp read-extended-command-predicate)
                     ;; Don't let bugs break M-x completion; interpret
                     ;; them as the absence of a predicate.
                     (condition-case-unless-debug err
                         (funcall read-extended-command-predicate sym buffer)
                       (error (message "read-extended-command-predicate: %s: %s"
                                       sym (error-message-string err))))))))
       t nil 'extended-command-history))))