Function: where-is

where-is is an interactive and byte-compiled function defined in help.el.gz.

Signature

(where-is DEFINITION &optional INSERT)

Documentation

Print message listing key sequences that invoke the command DEFINITION.

Argument is a command definition, usually a symbol with a function definition. If INSERT (the prefix arg) is non-nil, insert the message in the buffer.

View in manual

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/help.el.gz
(defun where-is (definition &optional insert)
  "Print message listing key sequences that invoke the command DEFINITION.
Argument is a command definition, usually a symbol with a function definition.
If INSERT (the prefix arg) is non-nil, insert the message in the buffer."
  (interactive
   (let ((fn (function-called-at-point))
	 (enable-recursive-minibuffers t)
	 val)
     (setq val (completing-read (format-prompt "Where is command" fn)
                                obarray #'commandp t nil nil
                                (and fn (symbol-name fn))))
     (list (unless (equal val "") (intern val))
	   current-prefix-arg)))
  (unless definition (error "No command"))
  (let ((func (indirect-function definition))
        (defs nil)
        (standard-output (if insert (current-buffer) standard-output)))
    ;; In DEFS, find all symbols that are aliases for DEFINITION.
    (mapatoms (lambda (symbol)
		(and (fboundp symbol)
		     (not (eq symbol definition))
		     (eq func (condition-case ()
				  (indirect-function symbol)
				(error symbol)))
		     (push symbol defs))))
    ;; Look at all the symbols--first DEFINITION,
    ;; then its aliases.
    (dolist (symbol (cons definition defs))
      (let* ((remapped (command-remapping symbol))
	     (keys (where-is-internal
		    symbol overriding-local-map nil nil remapped))
             (keys (mapconcat #'help--key-description-fontified
                              keys ", "))
	     string)
	(setq string
	      (if insert
		  (if (> (length keys) 0)
		      (if remapped
			  (format "%s, remapped to %s (%s)"
                                  symbol remapped keys)
			(format "%s (%s)" symbol keys))
		    (format "M-x %s RET" symbol))
		(if (> (length keys) 0)
		    (if remapped
			(if (eq symbol (symbol-function definition))
			    (format
                             "%s, which is remapped to %s, which is on %s"
			     symbol remapped keys)
			  (format "%s is remapped to %s, which is on %s"
				  symbol remapped keys))
		      (if (eq symbol (symbol-function definition))
			  (format "%s, which is on %s" symbol keys)
			(format "%s is on %s" symbol keys)))
		  ;; If this is the command the user asked about,
		  ;; and it is not on any key, say so.
		  ;; For other symbols, its aliases, say nothing
		  ;; about them unless they are on keys.
		  (if (eq symbol definition)
		      (format "%s is not on any key" symbol)))))
	(when string
	  (unless (eq symbol definition)
	    (if (eq definition (symbol-function symbol))
		(princ ";\n its alias ")
	      (princ ";\n it's an alias for ")))
	  (princ string)))))
  nil)