| 1 | ;;; help.el --- help commands for Emacs |
| 2 | |
| 3 | ;; Copyright (C) 1985-1986, 1993-1994, 1998-2014 Free Software Foundation, Inc. |
| 4 | |
| 5 | ;; Maintainer: emacs-devel@gnu.org |
| 6 | ;; Keywords: help, internal |
| 7 | ;; Package: emacs |
| 8 | |
| 9 | ;; This file is part of GNU Emacs. |
| 10 | |
| 11 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 12 | ;; it under the terms of the GNU General Public License as published by |
| 13 | ;; the Free Software Foundation, either version 3 of the License, or |
| 14 | ;; (at your option) any later version. |
| 15 | |
| 16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | ;; GNU General Public License for more details. |
| 20 | |
| 21 | ;; You should have received a copy of the GNU General Public License |
| 22 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 23 | |
| 24 | ;;; Commentary: |
| 25 | |
| 26 | ;; This code implements GNU Emacs's on-line help system, the one invoked by |
| 27 | ;; `M-x help-for-help'. |
| 28 | |
| 29 | ;;; Code: |
| 30 | |
| 31 | ;; Get the macro make-help-screen when this is compiled, |
| 32 | ;; or run interpreted, but not when the compiled code is loaded. |
| 33 | (eval-when-compile (require 'help-macro)) |
| 34 | |
| 35 | ;; `help-window-point-marker' is a marker you can move to a valid |
| 36 | ;; position of the buffer shown in the help window in order to override |
| 37 | ;; the standard positioning mechanism (`point-min') chosen by |
| 38 | ;; `with-output-to-temp-buffer' and `with-temp-buffer-window'. |
| 39 | ;; `with-help-window' has this point nowhere before exiting. Currently |
| 40 | ;; used by `view-lossage' to assert that the last keystrokes are always |
| 41 | ;; visible. |
| 42 | (defvar help-window-point-marker (make-marker) |
| 43 | "Marker to override default `window-point' in help windows.") |
| 44 | |
| 45 | (defvar help-map |
| 46 | (let ((map (make-sparse-keymap))) |
| 47 | (define-key map (char-to-string help-char) 'help-for-help) |
| 48 | (define-key map [help] 'help-for-help) |
| 49 | (define-key map [f1] 'help-for-help) |
| 50 | (define-key map "." 'display-local-help) |
| 51 | (define-key map "?" 'help-for-help) |
| 52 | |
| 53 | (define-key map "\C-a" 'about-emacs) |
| 54 | (define-key map "\C-c" 'describe-copying) |
| 55 | (define-key map "\C-d" 'view-emacs-debugging) |
| 56 | (define-key map "\C-e" 'view-external-packages) |
| 57 | (define-key map "\C-f" 'view-emacs-FAQ) |
| 58 | (define-key map "\C-m" 'view-order-manuals) |
| 59 | (define-key map "\C-n" 'view-emacs-news) |
| 60 | (define-key map "\C-o" 'describe-distribution) |
| 61 | (define-key map "\C-p" 'view-emacs-problems) |
| 62 | (define-key map "\C-t" 'view-emacs-todo) |
| 63 | (define-key map "\C-w" 'describe-no-warranty) |
| 64 | |
| 65 | ;; This does not fit the pattern, but it is natural given the C-\ command. |
| 66 | (define-key map "\C-\\" 'describe-input-method) |
| 67 | |
| 68 | (define-key map "C" 'describe-coding-system) |
| 69 | (define-key map "F" 'Info-goto-emacs-command-node) |
| 70 | (define-key map "I" 'describe-input-method) |
| 71 | (define-key map "K" 'Info-goto-emacs-key-command-node) |
| 72 | (define-key map "L" 'describe-language-environment) |
| 73 | (define-key map "S" 'info-lookup-symbol) |
| 74 | |
| 75 | (define-key map "a" 'apropos-command) |
| 76 | (define-key map "b" 'describe-bindings) |
| 77 | (define-key map "c" 'describe-key-briefly) |
| 78 | (define-key map "d" 'apropos-documentation) |
| 79 | (define-key map "e" 'view-echo-area-messages) |
| 80 | (define-key map "f" 'describe-function) |
| 81 | (define-key map "g" 'describe-gnu-project) |
| 82 | (define-key map "h" 'view-hello-file) |
| 83 | |
| 84 | (define-key map "i" 'info) |
| 85 | (define-key map "4i" 'info-other-window) |
| 86 | |
| 87 | (define-key map "k" 'describe-key) |
| 88 | (define-key map "l" 'view-lossage) |
| 89 | (define-key map "m" 'describe-mode) |
| 90 | (define-key map "n" 'view-emacs-news) |
| 91 | (define-key map "p" 'finder-by-keyword) |
| 92 | (define-key map "P" 'describe-package) |
| 93 | (define-key map "r" 'info-emacs-manual) |
| 94 | (define-key map "s" 'describe-syntax) |
| 95 | (define-key map "t" 'help-with-tutorial) |
| 96 | (define-key map "w" 'where-is) |
| 97 | (define-key map "v" 'describe-variable) |
| 98 | (define-key map "q" 'help-quit) |
| 99 | map) |
| 100 | "Keymap for characters following the Help key.") |
| 101 | |
| 102 | (define-key global-map (char-to-string help-char) 'help-command) |
| 103 | (define-key global-map [help] 'help-command) |
| 104 | (define-key global-map [f1] 'help-command) |
| 105 | (fset 'help-command help-map) |
| 106 | |
| 107 | ;; insert-button makes the action nil if it is not store somewhere |
| 108 | (defvar help-button-cache nil) |
| 109 | |
| 110 | \f |
| 111 | (defun help-quit () |
| 112 | "Just exit from the Help command's command loop." |
| 113 | (interactive) |
| 114 | nil) |
| 115 | |
| 116 | (defvar help-return-method nil |
| 117 | "What to do to \"exit\" the help buffer. |
| 118 | This is a list |
| 119 | (WINDOW . t) delete the selected window (and possibly its frame, |
| 120 | see `quit-window'), go to WINDOW. |
| 121 | (WINDOW . quit-window) do quit-window, then select WINDOW. |
| 122 | (WINDOW BUF START POINT) display BUF at START, POINT, then select WINDOW.") |
| 123 | |
| 124 | (define-obsolete-function-alias 'print-help-return-message 'help-print-return-message "23.2") |
| 125 | (defun help-print-return-message (&optional function) |
| 126 | "Display or return message saying how to restore windows after help command. |
| 127 | This function assumes that `standard-output' is the help buffer. |
| 128 | It computes a message, and applies the optional argument FUNCTION to it. |
| 129 | If FUNCTION is nil, it applies `message', thus displaying the message. |
| 130 | In addition, this function sets up `help-return-method', which see, that |
| 131 | specifies what to do when the user exits the help buffer. |
| 132 | |
| 133 | Do not call this in the scope of `with-help-window'." |
| 134 | (and (not (get-buffer-window standard-output)) |
| 135 | (let ((first-message |
| 136 | (cond ((or |
| 137 | pop-up-frames |
| 138 | (special-display-p (buffer-name standard-output))) |
| 139 | (setq help-return-method (cons (selected-window) t)) |
| 140 | ;; If the help output buffer is a special display buffer, |
| 141 | ;; don't say anything about how to get rid of it. |
| 142 | ;; First of all, the user will do that with the window |
| 143 | ;; manager, not with Emacs. |
| 144 | ;; Secondly, the buffer has not been displayed yet, |
| 145 | ;; so we don't know whether its frame will be selected. |
| 146 | nil) |
| 147 | ((not (one-window-p t)) |
| 148 | (setq help-return-method |
| 149 | (cons (selected-window) 'quit-window)) |
| 150 | "Type \\[display-buffer] RET to restore the other window.") |
| 151 | (pop-up-windows |
| 152 | (setq help-return-method (cons (selected-window) t)) |
| 153 | "Type \\[delete-other-windows] to remove help window.") |
| 154 | (t |
| 155 | (setq help-return-method |
| 156 | (list (selected-window) (window-buffer) |
| 157 | (window-start) (window-point))) |
| 158 | "Type \\[switch-to-buffer] RET to remove help window.")))) |
| 159 | (funcall (or function 'message) |
| 160 | (concat |
| 161 | (if first-message |
| 162 | (substitute-command-keys first-message)) |
| 163 | (if first-message " ") |
| 164 | ;; If the help buffer will go in a separate frame, |
| 165 | ;; it's no use mentioning a command to scroll, so don't. |
| 166 | (if (or pop-up-windows |
| 167 | (special-display-p (buffer-name standard-output))) |
| 168 | nil |
| 169 | (if (same-window-p (buffer-name standard-output)) |
| 170 | ;; Say how to scroll this window. |
| 171 | (substitute-command-keys |
| 172 | "\\[scroll-up] to scroll the help.") |
| 173 | ;; Say how to scroll some other window. |
| 174 | (substitute-command-keys |
| 175 | "\\[scroll-other-window] to scroll the help.")))))))) |
| 176 | |
| 177 | ;; So keyboard macro definitions are documented correctly |
| 178 | (fset 'defining-kbd-macro (symbol-function 'start-kbd-macro)) |
| 179 | |
| 180 | (defalias 'help 'help-for-help-internal) |
| 181 | ;; find-function can find this. |
| 182 | (defalias 'help-for-help 'help-for-help-internal) |
| 183 | ;; It can't find this, but nobody will look. |
| 184 | (make-help-screen help-for-help-internal |
| 185 | (purecopy "Type a help option: [abcCdefFgiIkKlLmnprstvw.] C-[cdefmnoptw] or ?") |
| 186 | ;; Don't purecopy this one, because it's not evaluated (it's |
| 187 | ;; directly used as a docstring in a function definition, so it'll |
| 188 | ;; be moved to the DOC file anyway: no need for purecopying it). |
| 189 | "You have typed %THIS-KEY%, the help character. Type a Help option: |
| 190 | \(Use SPC or DEL to scroll through this text. Type \\<help-map>\\[help-quit] to exit the Help command.) |
| 191 | |
| 192 | a PATTERN Show commands whose name matches the PATTERN (a list of words |
| 193 | or a regexp). See also the `apropos' command. |
| 194 | b Display all key bindings. |
| 195 | c KEYS Display the command name run by the given key sequence. |
| 196 | C CODING Describe the given coding system, or RET for current ones. |
| 197 | d PATTERN Show a list of functions, variables, and other items whose |
| 198 | documentation matches the PATTERN (a list of words or a regexp). |
| 199 | e Go to the *Messages* buffer which logs echo-area messages. |
| 200 | f FUNCTION Display documentation for the given function. |
| 201 | F COMMAND Show the on-line manual's section that describes the command. |
| 202 | g Display information about the GNU project. |
| 203 | h Display the HELLO file which illustrates various scripts. |
| 204 | i Start the Info documentation reader: read on-line manuals. |
| 205 | I METHOD Describe a specific input method, or RET for current. |
| 206 | k KEYS Display the full documentation for the key sequence. |
| 207 | K KEYS Show the on-line manual's section for the command bound to KEYS. |
| 208 | l Show last 300 input keystrokes (lossage). |
| 209 | L LANG-ENV Describes a specific language environment, or RET for current. |
| 210 | m Display documentation of current minor modes and current major mode, |
| 211 | including their special commands. |
| 212 | n Display news of recent Emacs changes. |
| 213 | p TOPIC Find packages matching a given topic keyword. |
| 214 | P PACKAGE Describe the given Emacs Lisp package. |
| 215 | r Display the Emacs manual in Info mode. |
| 216 | s Display contents of current syntax table, plus explanations. |
| 217 | S SYMBOL Show the section for the given symbol in the on-line manual |
| 218 | for the programming language used in this buffer. |
| 219 | t Start the Emacs learn-by-doing tutorial. |
| 220 | v VARIABLE Display the given variable's documentation and value. |
| 221 | w COMMAND Display which keystrokes invoke the given command (where-is). |
| 222 | . Display any available local help at point in the echo area. |
| 223 | |
| 224 | C-a Information about Emacs. |
| 225 | C-c Emacs copying permission (GNU General Public License). |
| 226 | C-d Instructions for debugging GNU Emacs. |
| 227 | C-e External packages and information about Emacs. |
| 228 | C-f Emacs FAQ. |
| 229 | C-m How to order printed Emacs manuals. |
| 230 | C-n News of recent Emacs changes. |
| 231 | C-o Emacs ordering and distribution information. |
| 232 | C-p Info about known Emacs problems. |
| 233 | C-t Emacs TODO list. |
| 234 | C-w Information on absence of warranty for GNU Emacs." |
| 235 | help-map) |
| 236 | |
| 237 | \f |
| 238 | |
| 239 | (defun function-called-at-point () |
| 240 | "Return a function around point or else called by the list containing point. |
| 241 | If that doesn't give a function, return nil." |
| 242 | (with-syntax-table emacs-lisp-mode-syntax-table |
| 243 | (or (condition-case () |
| 244 | (save-excursion |
| 245 | (or (not (zerop (skip-syntax-backward "_w"))) |
| 246 | (eq (char-syntax (following-char)) ?w) |
| 247 | (eq (char-syntax (following-char)) ?_) |
| 248 | (forward-sexp -1)) |
| 249 | (skip-chars-forward "'") |
| 250 | (let ((obj (read (current-buffer)))) |
| 251 | (and (symbolp obj) (fboundp obj) obj))) |
| 252 | (error nil)) |
| 253 | (condition-case () |
| 254 | (save-excursion |
| 255 | (save-restriction |
| 256 | (narrow-to-region (max (point-min) |
| 257 | (- (point) 1000)) (point-max)) |
| 258 | ;; Move up to surrounding paren, then after the open. |
| 259 | (backward-up-list 1) |
| 260 | (forward-char 1) |
| 261 | ;; If there is space here, this is probably something |
| 262 | ;; other than a real Lisp function call, so ignore it. |
| 263 | (if (looking-at "[ \t]") |
| 264 | (error "Probably not a Lisp function call")) |
| 265 | (let ((obj (read (current-buffer)))) |
| 266 | (and (symbolp obj) (fboundp obj) obj)))) |
| 267 | (error nil)) |
| 268 | (let* ((str (find-tag-default)) |
| 269 | (sym (if str (intern-soft str)))) |
| 270 | (if (and sym (fboundp sym)) |
| 271 | sym |
| 272 | (save-match-data |
| 273 | (when (and str (string-match "\\`\\W*\\(.*?\\)\\W*\\'" str)) |
| 274 | (setq sym (intern-soft (match-string 1 str))) |
| 275 | (and (fboundp sym) sym)))))))) |
| 276 | |
| 277 | \f |
| 278 | ;;; `User' help functions |
| 279 | |
| 280 | (defun view-help-file (file &optional dir) |
| 281 | (view-file (expand-file-name file (or dir data-directory))) |
| 282 | (goto-address-mode 1) |
| 283 | (goto-char (point-min))) |
| 284 | |
| 285 | (defun describe-distribution () |
| 286 | "Display info on how to obtain the latest version of GNU Emacs." |
| 287 | (interactive) |
| 288 | (view-help-file "DISTRIB")) |
| 289 | |
| 290 | (defun describe-copying () |
| 291 | "Display info on how you may redistribute copies of GNU Emacs." |
| 292 | (interactive) |
| 293 | (view-help-file "COPYING")) |
| 294 | |
| 295 | ;; Maybe this command should just be removed. |
| 296 | (defun describe-gnu-project () |
| 297 | "Browse online information on the GNU project." |
| 298 | (interactive) |
| 299 | (browse-url "http://www.gnu.org/gnu/thegnuproject.html")) |
| 300 | |
| 301 | (define-obsolete-function-alias 'describe-project 'describe-gnu-project "22.2") |
| 302 | |
| 303 | (defun describe-no-warranty () |
| 304 | "Display info on all the kinds of warranty Emacs does NOT have." |
| 305 | (interactive) |
| 306 | (describe-copying) |
| 307 | (let (case-fold-search) |
| 308 | (search-forward "Disclaimer of Warranty") |
| 309 | (forward-line 0) |
| 310 | (recenter 0))) |
| 311 | |
| 312 | (defun describe-prefix-bindings () |
| 313 | "Describe the bindings of the prefix used to reach this command. |
| 314 | The prefix described consists of all but the last event |
| 315 | of the key sequence that ran this command." |
| 316 | (interactive) |
| 317 | (let ((key (this-command-keys))) |
| 318 | (describe-bindings |
| 319 | (if (stringp key) |
| 320 | (substring key 0 (1- (length key))) |
| 321 | (let ((prefix (make-vector (1- (length key)) nil)) |
| 322 | (i 0)) |
| 323 | (while (< i (length prefix)) |
| 324 | (aset prefix i (aref key i)) |
| 325 | (setq i (1+ i))) |
| 326 | prefix))))) |
| 327 | ;; Make C-h after a prefix, when not specifically bound, |
| 328 | ;; run describe-prefix-bindings. |
| 329 | (setq prefix-help-command 'describe-prefix-bindings) |
| 330 | |
| 331 | (defun view-emacs-news (&optional version) |
| 332 | "Display info on recent changes to Emacs. |
| 333 | With argument, display info only for the selected version." |
| 334 | (interactive "P") |
| 335 | (unless version |
| 336 | (setq version emacs-major-version)) |
| 337 | (when (consp version) |
| 338 | (let* ((all-versions |
| 339 | (let (res) |
| 340 | (mapc |
| 341 | (lambda (file) |
| 342 | (with-temp-buffer |
| 343 | (insert-file-contents |
| 344 | (expand-file-name file data-directory)) |
| 345 | (while (re-search-forward |
| 346 | (if (member file '("NEWS.18" "NEWS.1-17")) |
| 347 | "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)" |
| 348 | "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t) |
| 349 | (setq res (cons (match-string-no-properties 1) res))))) |
| 350 | (cons "NEWS" |
| 351 | (directory-files data-directory nil |
| 352 | "^NEWS\\.[0-9][-0-9]*$" nil))) |
| 353 | (sort (delete-dups res) (lambda (a b) (string< b a))))) |
| 354 | (current (car all-versions))) |
| 355 | (setq version (completing-read |
| 356 | (format "Read NEWS for the version (default %s): " current) |
| 357 | all-versions nil nil nil nil current)) |
| 358 | (if (integerp (string-to-number version)) |
| 359 | (setq version (string-to-number version)) |
| 360 | (unless (or (member version all-versions) |
| 361 | (<= (string-to-number version) (string-to-number current))) |
| 362 | (error "No news about version %s" version))))) |
| 363 | (when (integerp version) |
| 364 | (cond ((<= version 12) |
| 365 | (setq version (format "1.%d" version))) |
| 366 | ((<= version 18) |
| 367 | (setq version (format "%d" version))) |
| 368 | ((> version emacs-major-version) |
| 369 | (error "No news about Emacs %d (yet)" version)))) |
| 370 | (let* ((vn (if (stringp version) |
| 371 | (string-to-number version) |
| 372 | version)) |
| 373 | (file (cond |
| 374 | ((>= vn emacs-major-version) "NEWS") |
| 375 | ((< vn 18) "NEWS.1-17") |
| 376 | (t (format "NEWS.%d" vn)))) |
| 377 | res) |
| 378 | (view-file (expand-file-name file data-directory)) |
| 379 | (widen) |
| 380 | (goto-char (point-min)) |
| 381 | (when (stringp version) |
| 382 | (when (re-search-forward |
| 383 | (concat (if (< vn 19) |
| 384 | "Changes in Emacs[ \t]*" |
| 385 | "^\* [^0-9\n]*") version "$") |
| 386 | nil t) |
| 387 | (beginning-of-line) |
| 388 | (narrow-to-region |
| 389 | (point) |
| 390 | (save-excursion |
| 391 | (while (and (setq res |
| 392 | (re-search-forward |
| 393 | (if (< vn 19) |
| 394 | "Changes in \\(?:Emacs\\|version\\)?[ \t]*\\([0-9]+\\(?:\\.[0-9]+\\)?\\)" |
| 395 | "^\* [^0-9\n]*\\([0-9]+\\.[0-9]+\\)") nil t)) |
| 396 | (equal (match-string-no-properties 1) version))) |
| 397 | (or res (goto-char (point-max))) |
| 398 | (beginning-of-line) |
| 399 | (point))))))) |
| 400 | |
| 401 | (defun view-emacs-todo (&optional _arg) |
| 402 | "Display the Emacs TODO list." |
| 403 | (interactive "P") |
| 404 | (view-help-file "TODO")) |
| 405 | |
| 406 | (define-obsolete-function-alias 'view-todo 'view-emacs-todo "22.2") |
| 407 | |
| 408 | |
| 409 | (defun view-echo-area-messages () |
| 410 | "View the log of recent echo-area messages: the `*Messages*' buffer. |
| 411 | The number of messages retained in that buffer |
| 412 | is specified by the variable `message-log-max'." |
| 413 | (interactive) |
| 414 | (with-current-buffer (messages-buffer) |
| 415 | (goto-char (point-max)) |
| 416 | (display-buffer (current-buffer)))) |
| 417 | |
| 418 | (defun view-order-manuals () |
| 419 | "Display information on how to buy printed copies of Emacs manuals." |
| 420 | (interactive) |
| 421 | ;; (view-help-file "ORDERS") |
| 422 | (info "(emacs)Printed Books")) |
| 423 | |
| 424 | (defun view-emacs-FAQ () |
| 425 | "Display the Emacs Frequently Asked Questions (FAQ) file." |
| 426 | (interactive) |
| 427 | ;; (find-file-read-only (expand-file-name "FAQ" data-directory)) |
| 428 | (info "(efaq)")) |
| 429 | |
| 430 | (defun view-emacs-problems () |
| 431 | "Display info on known problems with Emacs and possible workarounds." |
| 432 | (interactive) |
| 433 | (view-help-file "PROBLEMS")) |
| 434 | |
| 435 | (defun view-emacs-debugging () |
| 436 | "Display info on how to debug Emacs problems." |
| 437 | (interactive) |
| 438 | (view-help-file "DEBUG")) |
| 439 | |
| 440 | ;; This used to visit MORE.STUFF; maybe it should just be removed. |
| 441 | (defun view-external-packages () |
| 442 | "Display info on where to get more Emacs packages." |
| 443 | (interactive) |
| 444 | (info "(efaq)Packages that do not come with Emacs")) |
| 445 | |
| 446 | (defun view-lossage () |
| 447 | "Display last 300 input keystrokes. |
| 448 | |
| 449 | To record all your input, use `open-dribble-file'." |
| 450 | (interactive) |
| 451 | (help-setup-xref (list #'view-lossage) |
| 452 | (called-interactively-p 'interactive)) |
| 453 | (with-help-window (help-buffer) |
| 454 | (princ (mapconcat (lambda (key) |
| 455 | (if (or (integerp key) (symbolp key) (listp key)) |
| 456 | (single-key-description key) |
| 457 | (prin1-to-string key nil))) |
| 458 | (recent-keys) |
| 459 | " ")) |
| 460 | (with-current-buffer standard-output |
| 461 | (goto-char (point-min)) |
| 462 | (while (progn (move-to-column 50) (not (eobp))) |
| 463 | (when (search-forward " " nil t) |
| 464 | (delete-char -1)) |
| 465 | (insert "\n")) |
| 466 | ;; jidanni wants to see the last keystrokes immediately. |
| 467 | (set-marker help-window-point-marker (point))))) |
| 468 | |
| 469 | \f |
| 470 | ;; Key bindings |
| 471 | |
| 472 | (defun describe-bindings (&optional prefix buffer) |
| 473 | "Display a buffer showing a list of all defined keys, and their definitions. |
| 474 | The keys are displayed in order of precedence. |
| 475 | |
| 476 | The optional argument PREFIX, if non-nil, should be a key sequence; |
| 477 | then we display only bindings that start with that prefix. |
| 478 | The optional argument BUFFER specifies which buffer's bindings |
| 479 | to display (default, the current buffer). BUFFER can be a buffer |
| 480 | or a buffer name." |
| 481 | (interactive) |
| 482 | (or buffer (setq buffer (current-buffer))) |
| 483 | (help-setup-xref (list #'describe-bindings prefix buffer) |
| 484 | (called-interactively-p 'interactive)) |
| 485 | (with-help-window (help-buffer) |
| 486 | ;; Be aware that `describe-buffer-bindings' puts its output into |
| 487 | ;; the current buffer. |
| 488 | (with-current-buffer (help-buffer) |
| 489 | (describe-buffer-bindings buffer prefix)))) |
| 490 | |
| 491 | ;; This function used to be in keymap.c. |
| 492 | (defun describe-bindings-internal (&optional menus prefix) |
| 493 | "Show a list of all defined keys, and their definitions. |
| 494 | We put that list in a buffer, and display the buffer. |
| 495 | |
| 496 | The optional argument MENUS, if non-nil, says to mention menu bindings. |
| 497 | \(Ordinarily these are omitted from the output.) |
| 498 | The optional argument PREFIX, if non-nil, should be a key sequence; |
| 499 | then we display only bindings that start with that prefix." |
| 500 | (declare (obsolete describe-buffer-bindings "24.4")) |
| 501 | (let ((buf (current-buffer))) |
| 502 | (with-help-window (help-buffer) |
| 503 | ;; Be aware that `describe-buffer-bindings' puts its output into |
| 504 | ;; the current buffer. |
| 505 | (with-current-buffer (help-buffer) |
| 506 | (describe-buffer-bindings buf prefix menus))))) |
| 507 | |
| 508 | (defun where-is (definition &optional insert) |
| 509 | "Print message listing key sequences that invoke the command DEFINITION. |
| 510 | Argument is a command definition, usually a symbol with a function definition. |
| 511 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer." |
| 512 | (interactive |
| 513 | (let ((fn (function-called-at-point)) |
| 514 | (enable-recursive-minibuffers t) |
| 515 | val) |
| 516 | (setq val (completing-read |
| 517 | (if fn |
| 518 | (format "Where is command (default %s): " fn) |
| 519 | "Where is command: ") |
| 520 | obarray 'commandp t nil nil |
| 521 | (and fn (symbol-name fn)))) |
| 522 | (list (unless (equal val "") (intern val)) |
| 523 | current-prefix-arg))) |
| 524 | (unless definition (error "No command")) |
| 525 | (let ((func (indirect-function definition)) |
| 526 | (defs nil) |
| 527 | (standard-output (if insert (current-buffer) standard-output))) |
| 528 | ;; In DEFS, find all symbols that are aliases for DEFINITION. |
| 529 | (mapatoms (lambda (symbol) |
| 530 | (and (fboundp symbol) |
| 531 | (not (eq symbol definition)) |
| 532 | (eq func (condition-case () |
| 533 | (indirect-function symbol) |
| 534 | (error symbol))) |
| 535 | (push symbol defs)))) |
| 536 | ;; Look at all the symbols--first DEFINITION, |
| 537 | ;; then its aliases. |
| 538 | (dolist (symbol (cons definition defs)) |
| 539 | (let* ((remapped (command-remapping symbol)) |
| 540 | (keys (where-is-internal |
| 541 | symbol overriding-local-map nil nil remapped)) |
| 542 | (keys (mapconcat 'key-description keys ", ")) |
| 543 | string) |
| 544 | (setq string |
| 545 | (if insert |
| 546 | (if (> (length keys) 0) |
| 547 | (if remapped |
| 548 | (format "%s (%s) (remapped from %s)" |
| 549 | keys remapped symbol) |
| 550 | (format "%s (%s)" keys symbol)) |
| 551 | (format "M-x %s RET" symbol)) |
| 552 | (if (> (length keys) 0) |
| 553 | (if remapped |
| 554 | (format "%s is remapped to %s which is on %s" |
| 555 | symbol remapped keys) |
| 556 | (format "%s is on %s" symbol keys)) |
| 557 | ;; If this is the command the user asked about, |
| 558 | ;; and it is not on any key, say so. |
| 559 | ;; For other symbols, its aliases, say nothing |
| 560 | ;; about them unless they are on keys. |
| 561 | (if (eq symbol definition) |
| 562 | (format "%s is not on any key" symbol))))) |
| 563 | (when string |
| 564 | (unless (eq symbol definition) |
| 565 | (princ ";\n its alias ")) |
| 566 | (princ string))))) |
| 567 | nil) |
| 568 | |
| 569 | (defun help-key-description (key untranslated) |
| 570 | (let ((string (key-description key))) |
| 571 | (if (or (not untranslated) |
| 572 | (and (eq (aref untranslated 0) ?\e) (not (eq (aref key 0) ?\e)))) |
| 573 | string |
| 574 | (let ((otherstring (key-description untranslated))) |
| 575 | (if (equal string otherstring) |
| 576 | string |
| 577 | (format "%s (translated from %s)" string otherstring)))))) |
| 578 | |
| 579 | (defun describe-key-briefly (&optional key insert untranslated) |
| 580 | "Print the name of the function KEY invokes. KEY is a string. |
| 581 | If INSERT (the prefix arg) is non-nil, insert the message in the buffer. |
| 582 | If non-nil, UNTRANSLATED is a vector of the untranslated events. |
| 583 | It can also be a number in which case the untranslated events from |
| 584 | the last key hit are used. |
| 585 | |
| 586 | If KEY is a menu item or a tool-bar button that is disabled, this command |
| 587 | temporarily enables it to allow getting help on disabled items and buttons." |
| 588 | (interactive |
| 589 | (let ((enable-disabled-menus-and-buttons t) |
| 590 | (cursor-in-echo-area t) |
| 591 | saved-yank-menu) |
| 592 | (unwind-protect |
| 593 | (let (key) |
| 594 | ;; If yank-menu is empty, populate it temporarily, so that |
| 595 | ;; "Select and Paste" menu can generate a complete event. |
| 596 | (when (null (cdr yank-menu)) |
| 597 | (setq saved-yank-menu (copy-sequence yank-menu)) |
| 598 | (menu-bar-update-yank-menu "(any string)" nil)) |
| 599 | (setq key (read-key-sequence "Describe key (or click or menu item): ")) |
| 600 | ;; Clear the echo area message (Bug#7014). |
| 601 | (message nil) |
| 602 | ;; If KEY is a down-event, read and discard the |
| 603 | ;; corresponding up-event. Note that there are also |
| 604 | ;; down-events on scroll bars and mode lines: the actual |
| 605 | ;; event then is in the second element of the vector. |
| 606 | (and (vectorp key) |
| 607 | (let ((last-idx (1- (length key)))) |
| 608 | (and (eventp (aref key last-idx)) |
| 609 | (memq 'down (event-modifiers (aref key last-idx))))) |
| 610 | (read-event)) |
| 611 | (list |
| 612 | key |
| 613 | (if current-prefix-arg (prefix-numeric-value current-prefix-arg)) |
| 614 | 1)) |
| 615 | ;; Put yank-menu back as it was, if we changed it. |
| 616 | (when saved-yank-menu |
| 617 | (setq yank-menu (copy-sequence saved-yank-menu)) |
| 618 | (fset 'yank-menu (cons 'keymap yank-menu)))))) |
| 619 | (if (numberp untranslated) |
| 620 | (setq untranslated (this-single-command-raw-keys))) |
| 621 | (let* ((event (if (and (symbolp (aref key 0)) |
| 622 | (> (length key) 1) |
| 623 | (consp (aref key 1))) |
| 624 | (aref key 1) |
| 625 | (aref key 0))) |
| 626 | (modifiers (event-modifiers event)) |
| 627 | (standard-output (if insert (current-buffer) standard-output)) |
| 628 | (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers) |
| 629 | (memq 'drag modifiers)) " at that spot" "")) |
| 630 | (defn (key-binding key t)) |
| 631 | key-desc) |
| 632 | ;; Handle the case where we faked an entry in "Select and Paste" menu. |
| 633 | (if (and (eq defn nil) |
| 634 | (stringp (aref key (1- (length key)))) |
| 635 | (eq (key-binding (substring key 0 -1)) 'yank-menu)) |
| 636 | (setq defn 'menu-bar-select-yank)) |
| 637 | ;; Don't bother user with strings from (e.g.) the select-paste menu. |
| 638 | (if (stringp (aref key (1- (length key)))) |
| 639 | (aset key (1- (length key)) "(any string)")) |
| 640 | (if (and (> (length untranslated) 0) |
| 641 | (stringp (aref untranslated (1- (length untranslated))))) |
| 642 | (aset untranslated (1- (length untranslated)) "(any string)")) |
| 643 | ;; Now describe the key, perhaps as changed. |
| 644 | (setq key-desc (help-key-description key untranslated)) |
| 645 | (if (or (null defn) (integerp defn) (equal defn 'undefined)) |
| 646 | (princ (format "%s%s is undefined" key-desc mouse-msg)) |
| 647 | (princ (format "%s%s runs the command %S" key-desc mouse-msg defn))))) |
| 648 | |
| 649 | (defun help--key-binding-keymap (key &optional accept-default no-remap position) |
| 650 | "Return a keymap holding a binding for KEY within current keymaps. |
| 651 | The effect of the arguments KEY, ACCEPT-DEFAULT, NO-REMAP and |
| 652 | POSITION is as documented in the function `key-binding'." |
| 653 | (let* ((active-maps (current-active-maps t position)) |
| 654 | map found) |
| 655 | ;; We loop over active maps like key-binding does. |
| 656 | (while (and |
| 657 | (not found) |
| 658 | (setq map (pop active-maps))) |
| 659 | (setq found (lookup-key map key accept-default)) |
| 660 | (when (integerp found) |
| 661 | ;; The first `found' characters of KEY were found but not the |
| 662 | ;; whole sequence. |
| 663 | (setq found nil))) |
| 664 | (when found |
| 665 | (if (and (symbolp found) |
| 666 | (not no-remap) |
| 667 | (command-remapping found)) |
| 668 | ;; The user might want to know in which map the binding is |
| 669 | ;; found, or in which map the remapping is found. The |
| 670 | ;; default is to show the latter. |
| 671 | (help--key-binding-keymap (vector 'remap found)) |
| 672 | map)))) |
| 673 | |
| 674 | (defun help--binding-locus (key position) |
| 675 | "Describe in which keymap KEY is defined. |
| 676 | Return a symbol pointing to that keymap if one exists ; otherwise |
| 677 | return nil." |
| 678 | (let ((map (help--key-binding-keymap key t nil position))) |
| 679 | (when map |
| 680 | (catch 'found |
| 681 | (let ((advertised-syms (nconc |
| 682 | (list 'overriding-terminal-local-map |
| 683 | 'overriding-local-map) |
| 684 | (delq nil |
| 685 | (mapcar |
| 686 | (lambda (mode-and-map) |
| 687 | (let ((mode (car mode-and-map))) |
| 688 | (when (symbol-value mode) |
| 689 | (intern-soft |
| 690 | (format "%s-map" mode))))) |
| 691 | minor-mode-map-alist)) |
| 692 | (list 'global-map |
| 693 | (intern-soft (format "%s-map" major-mode))))) |
| 694 | found) |
| 695 | ;; Look into these advertised symbols first. |
| 696 | (dolist (sym advertised-syms) |
| 697 | (when (and |
| 698 | (boundp sym) |
| 699 | (eq map (symbol-value sym))) |
| 700 | (throw 'found sym))) |
| 701 | ;; Only look in other symbols otherwise. |
| 702 | (mapatoms |
| 703 | (lambda (x) |
| 704 | (when (and (boundp x) |
| 705 | ;; Avoid let-bound symbols. |
| 706 | (special-variable-p x) |
| 707 | (eq (symbol-value x) map)) |
| 708 | (throw 'found x)))) |
| 709 | nil))))) |
| 710 | |
| 711 | (defun describe-key (&optional key untranslated up-event) |
| 712 | "Display documentation of the function invoked by KEY. |
| 713 | KEY can be any kind of a key sequence; it can include keyboard events, |
| 714 | mouse events, and/or menu events. When calling from a program, |
| 715 | pass KEY as a string or a vector. |
| 716 | |
| 717 | If non-nil, UNTRANSLATED is a vector of the corresponding untranslated events. |
| 718 | It can also be a number, in which case the untranslated events from |
| 719 | the last key sequence entered are used. |
| 720 | UP-EVENT is the up-event that was discarded by reading KEY, or nil. |
| 721 | |
| 722 | If KEY is a menu item or a tool-bar button that is disabled, this command |
| 723 | temporarily enables it to allow getting help on disabled items and buttons." |
| 724 | (interactive |
| 725 | (let ((enable-disabled-menus-and-buttons t) |
| 726 | (cursor-in-echo-area t) |
| 727 | saved-yank-menu) |
| 728 | (unwind-protect |
| 729 | (let (key) |
| 730 | ;; If yank-menu is empty, populate it temporarily, so that |
| 731 | ;; "Select and Paste" menu can generate a complete event. |
| 732 | (when (null (cdr yank-menu)) |
| 733 | (setq saved-yank-menu (copy-sequence yank-menu)) |
| 734 | (menu-bar-update-yank-menu "(any string)" nil)) |
| 735 | (setq key (read-key-sequence "Describe key (or click or menu item): ")) |
| 736 | (list |
| 737 | key |
| 738 | (prefix-numeric-value current-prefix-arg) |
| 739 | ;; If KEY is a down-event, read and include the |
| 740 | ;; corresponding up-event. Note that there are also |
| 741 | ;; down-events on scroll bars and mode lines: the actual |
| 742 | ;; event then is in the second element of the vector. |
| 743 | (and (vectorp key) |
| 744 | (let ((last-idx (1- (length key)))) |
| 745 | (and (eventp (aref key last-idx)) |
| 746 | (memq 'down (event-modifiers (aref key last-idx))))) |
| 747 | (or (and (eventp (aref key 0)) |
| 748 | (memq 'down (event-modifiers (aref key 0))) |
| 749 | ;; However, for the C-down-mouse-2 popup |
| 750 | ;; menu, there is no subsequent up-event. In |
| 751 | ;; this case, the up-event is the next |
| 752 | ;; element in the supplied vector. |
| 753 | (= (length key) 1)) |
| 754 | (and (> (length key) 1) |
| 755 | (eventp (aref key 1)) |
| 756 | (memq 'down (event-modifiers (aref key 1))))) |
| 757 | (read-event)))) |
| 758 | ;; Put yank-menu back as it was, if we changed it. |
| 759 | (when saved-yank-menu |
| 760 | (setq yank-menu (copy-sequence saved-yank-menu)) |
| 761 | (fset 'yank-menu (cons 'keymap yank-menu)))))) |
| 762 | (if (numberp untranslated) |
| 763 | (setq untranslated (this-single-command-raw-keys))) |
| 764 | (let* ((event (aref key (if (and (symbolp (aref key 0)) |
| 765 | (> (length key) 1) |
| 766 | (consp (aref key 1))) |
| 767 | 1 |
| 768 | 0))) |
| 769 | (modifiers (event-modifiers event)) |
| 770 | (mouse-msg (if (or (memq 'click modifiers) (memq 'down modifiers) |
| 771 | (memq 'drag modifiers)) " at that spot" "")) |
| 772 | (defn (key-binding key t)) |
| 773 | key-locus key-locus-up key-locus-up-tricky |
| 774 | defn-up defn-up-tricky ev-type |
| 775 | mouse-1-remapped mouse-1-tricky) |
| 776 | |
| 777 | ;; Handle the case where we faked an entry in "Select and Paste" menu. |
| 778 | (when (and (eq defn nil) |
| 779 | (stringp (aref key (1- (length key)))) |
| 780 | (eq (key-binding (substring key 0 -1)) 'yank-menu)) |
| 781 | (setq defn 'menu-bar-select-yank)) |
| 782 | (if (or (null defn) (integerp defn) (equal defn 'undefined)) |
| 783 | (message "%s%s is undefined" |
| 784 | (help-key-description key untranslated) mouse-msg) |
| 785 | (help-setup-xref (list #'describe-function defn) |
| 786 | (called-interactively-p 'interactive)) |
| 787 | ;; Don't bother user with strings from (e.g.) the select-paste menu. |
| 788 | (when (stringp (aref key (1- (length key)))) |
| 789 | (aset key (1- (length key)) "(any string)")) |
| 790 | (when (and untranslated |
| 791 | (stringp (aref untranslated (1- (length untranslated))))) |
| 792 | (aset untranslated (1- (length untranslated)) |
| 793 | "(any string)")) |
| 794 | ;; Need to do this before erasing *Help* buffer in case event |
| 795 | ;; is a mouse click in an existing *Help* buffer. |
| 796 | (when up-event |
| 797 | (setq ev-type (event-basic-type up-event)) |
| 798 | (let ((sequence (vector up-event))) |
| 799 | (when (and (eq ev-type 'mouse-1) |
| 800 | mouse-1-click-follows-link |
| 801 | (not (eq mouse-1-click-follows-link 'double)) |
| 802 | (setq mouse-1-remapped |
| 803 | (mouse-on-link-p (event-start up-event)))) |
| 804 | (setq mouse-1-tricky (and (integerp mouse-1-click-follows-link) |
| 805 | (> mouse-1-click-follows-link 0))) |
| 806 | (cond ((stringp mouse-1-remapped) |
| 807 | (setq sequence mouse-1-remapped)) |
| 808 | ((vectorp mouse-1-remapped) |
| 809 | (setcar up-event (elt mouse-1-remapped 0))) |
| 810 | (t (setcar up-event 'mouse-2)))) |
| 811 | (setq defn-up (key-binding sequence nil nil (event-start up-event))) |
| 812 | (setq key-locus-up (help--binding-locus sequence (event-start up-event))) |
| 813 | (when mouse-1-tricky |
| 814 | (setq sequence (vector up-event)) |
| 815 | (aset sequence 0 'mouse-1) |
| 816 | (setq defn-up-tricky (key-binding sequence nil nil (event-start up-event))) |
| 817 | (setq key-locus-up-tricky (help--binding-locus sequence (event-start up-event)))))) |
| 818 | (setq key-locus (help--binding-locus key (event-start event))) |
| 819 | (with-help-window (help-buffer) |
| 820 | (princ (help-key-description key untranslated)) |
| 821 | (princ (format "%s runs the command %S%s, which is " |
| 822 | mouse-msg defn (if key-locus |
| 823 | (format " (found in %s)" key-locus) |
| 824 | ""))) |
| 825 | (describe-function-1 defn) |
| 826 | (when up-event |
| 827 | (unless (or (null defn-up) |
| 828 | (integerp defn-up) |
| 829 | (equal defn-up 'undefined)) |
| 830 | (princ (format " |
| 831 | |
| 832 | ----------------- up-event %s---------------- |
| 833 | |
| 834 | %s%s%s runs the command %S%s, which is " |
| 835 | (if mouse-1-tricky "(short click) " "") |
| 836 | (key-description (vector up-event)) |
| 837 | mouse-msg |
| 838 | (if mouse-1-remapped |
| 839 | " is remapped to <mouse-2>, which" "") |
| 840 | defn-up (if key-locus-up |
| 841 | (format " (found in %s)" key-locus-up) |
| 842 | ""))) |
| 843 | (describe-function-1 defn-up)) |
| 844 | (unless (or (null defn-up-tricky) |
| 845 | (integerp defn-up-tricky) |
| 846 | (eq defn-up-tricky 'undefined)) |
| 847 | (princ (format " |
| 848 | |
| 849 | ----------------- up-event (long click) ---------------- |
| 850 | |
| 851 | Pressing <%S>%s for longer than %d milli-seconds |
| 852 | runs the command %S%s, which is " |
| 853 | ev-type mouse-msg |
| 854 | mouse-1-click-follows-link |
| 855 | defn-up-tricky (if key-locus-up-tricky |
| 856 | (format " (found in %s)" key-locus-up-tricky) |
| 857 | ""))) |
| 858 | (describe-function-1 defn-up-tricky))))))) |
| 859 | \f |
| 860 | (defun describe-mode (&optional buffer) |
| 861 | "Display documentation of current major mode and minor modes. |
| 862 | A brief summary of the minor modes comes first, followed by the |
| 863 | major mode description. This is followed by detailed |
| 864 | descriptions of the minor modes, each on a separate page. |
| 865 | |
| 866 | For this to work correctly for a minor mode, the mode's indicator |
| 867 | variable \(listed in `minor-mode-alist') must also be a function |
| 868 | whose documentation describes the minor mode. |
| 869 | |
| 870 | If called from Lisp with a non-nil BUFFER argument, display |
| 871 | documentation for the major and minor modes of that buffer." |
| 872 | (interactive "@") |
| 873 | (unless buffer (setq buffer (current-buffer))) |
| 874 | (help-setup-xref (list #'describe-mode buffer) |
| 875 | (called-interactively-p 'interactive)) |
| 876 | ;; For the sake of help-do-xref and help-xref-go-back, |
| 877 | ;; don't switch buffers before calling `help-buffer'. |
| 878 | (with-help-window (help-buffer) |
| 879 | (with-current-buffer buffer |
| 880 | (let (minor-modes) |
| 881 | ;; Older packages do not register in minor-mode-list but only in |
| 882 | ;; minor-mode-alist. |
| 883 | (dolist (x minor-mode-alist) |
| 884 | (setq x (car x)) |
| 885 | (unless (memq x minor-mode-list) |
| 886 | (push x minor-mode-list))) |
| 887 | ;; Find enabled minor mode we will want to mention. |
| 888 | (dolist (mode minor-mode-list) |
| 889 | ;; Document a minor mode if it is listed in minor-mode-alist, |
| 890 | ;; non-nil, and has a function definition. |
| 891 | (let ((fmode (or (get mode :minor-mode-function) mode))) |
| 892 | (and (boundp mode) (symbol-value mode) |
| 893 | (fboundp fmode) |
| 894 | (let ((pretty-minor-mode |
| 895 | (if (string-match "\\(\\(-minor\\)?-mode\\)?\\'" |
| 896 | (symbol-name fmode)) |
| 897 | (capitalize |
| 898 | (substring (symbol-name fmode) |
| 899 | 0 (match-beginning 0))) |
| 900 | fmode))) |
| 901 | (push (list fmode pretty-minor-mode |
| 902 | (format-mode-line (assq mode minor-mode-alist))) |
| 903 | minor-modes))))) |
| 904 | (setq minor-modes |
| 905 | (sort minor-modes |
| 906 | (lambda (a b) (string-lessp (cadr a) (cadr b))))) |
| 907 | (when minor-modes |
| 908 | (princ "Enabled minor modes:\n") |
| 909 | (make-local-variable 'help-button-cache) |
| 910 | (with-current-buffer standard-output |
| 911 | (dolist (mode minor-modes) |
| 912 | (let ((mode-function (nth 0 mode)) |
| 913 | (pretty-minor-mode (nth 1 mode)) |
| 914 | (indicator (nth 2 mode))) |
| 915 | (add-text-properties 0 (length pretty-minor-mode) |
| 916 | '(face bold) pretty-minor-mode) |
| 917 | (save-excursion |
| 918 | (goto-char (point-max)) |
| 919 | (princ "\n\f\n") |
| 920 | (push (point-marker) help-button-cache) |
| 921 | ;; Document the minor modes fully. |
| 922 | (insert pretty-minor-mode) |
| 923 | (princ (format " minor mode (%s):\n" |
| 924 | (if (zerop (length indicator)) |
| 925 | "no indicator" |
| 926 | (format "indicator%s" |
| 927 | indicator)))) |
| 928 | (princ (documentation mode-function))) |
| 929 | (insert-button pretty-minor-mode |
| 930 | 'action (car help-button-cache) |
| 931 | 'follow-link t |
| 932 | 'help-echo "mouse-2, RET: show full information") |
| 933 | (newline))) |
| 934 | (forward-line -1) |
| 935 | (fill-paragraph nil) |
| 936 | (forward-line 1)) |
| 937 | |
| 938 | (princ "\n(Information about these minor modes follows the major mode info.)\n\n")) |
| 939 | ;; Document the major mode. |
| 940 | (let ((mode mode-name)) |
| 941 | (with-current-buffer standard-output |
| 942 | (let ((start (point))) |
| 943 | (insert (format-mode-line mode nil nil buffer)) |
| 944 | (add-text-properties start (point) '(face bold))))) |
| 945 | (princ " mode") |
| 946 | (let* ((mode major-mode) |
| 947 | (file-name (find-lisp-object-file-name mode nil))) |
| 948 | (when file-name |
| 949 | (princ (concat " defined in `" (file-name-nondirectory file-name) "'")) |
| 950 | ;; Make a hyperlink to the library. |
| 951 | (with-current-buffer standard-output |
| 952 | (save-excursion |
| 953 | (re-search-backward "`\\([^`']+\\)'" nil t) |
| 954 | (help-xref-button 1 'help-function-def mode file-name))))) |
| 955 | (princ ":\n") |
| 956 | (princ (documentation major-mode))))) |
| 957 | ;; For the sake of IELM and maybe others |
| 958 | nil) |
| 959 | |
| 960 | |
| 961 | (defun describe-minor-mode (minor-mode) |
| 962 | "Display documentation of a minor mode given as MINOR-MODE. |
| 963 | MINOR-MODE can be a minor mode symbol or a minor mode indicator string |
| 964 | appeared on the mode-line." |
| 965 | (interactive (list (completing-read |
| 966 | "Minor mode: " |
| 967 | (nconc |
| 968 | (describe-minor-mode-completion-table-for-symbol) |
| 969 | (describe-minor-mode-completion-table-for-indicator) |
| 970 | )))) |
| 971 | (if (symbolp minor-mode) |
| 972 | (setq minor-mode (symbol-name minor-mode))) |
| 973 | (let ((symbols (describe-minor-mode-completion-table-for-symbol)) |
| 974 | (indicators (describe-minor-mode-completion-table-for-indicator))) |
| 975 | (cond |
| 976 | ((member minor-mode symbols) |
| 977 | (describe-minor-mode-from-symbol (intern minor-mode))) |
| 978 | ((member minor-mode indicators) |
| 979 | (describe-minor-mode-from-indicator minor-mode)) |
| 980 | (t |
| 981 | (error "No such minor mode: %s" minor-mode))))) |
| 982 | |
| 983 | ;; symbol |
| 984 | (defun describe-minor-mode-completion-table-for-symbol () |
| 985 | ;; In order to list up all minor modes, minor-mode-list |
| 986 | ;; is used here instead of minor-mode-alist. |
| 987 | (delq nil (mapcar 'symbol-name minor-mode-list))) |
| 988 | |
| 989 | (defun describe-minor-mode-from-symbol (symbol) |
| 990 | "Display documentation of a minor mode given as a symbol, SYMBOL" |
| 991 | (interactive (list (intern (completing-read |
| 992 | "Minor mode symbol: " |
| 993 | (describe-minor-mode-completion-table-for-symbol))))) |
| 994 | (if (fboundp symbol) |
| 995 | (describe-function symbol) |
| 996 | (describe-variable symbol))) |
| 997 | |
| 998 | ;; indicator |
| 999 | (defun describe-minor-mode-completion-table-for-indicator () |
| 1000 | (delq nil |
| 1001 | (mapcar (lambda (x) |
| 1002 | (let ((i (format-mode-line x))) |
| 1003 | ;; remove first space if existed |
| 1004 | (cond |
| 1005 | ((= 0 (length i)) |
| 1006 | nil) |
| 1007 | ((eq (aref i 0) ?\s) |
| 1008 | (substring i 1)) |
| 1009 | (t |
| 1010 | i)))) |
| 1011 | minor-mode-alist))) |
| 1012 | |
| 1013 | (defun describe-minor-mode-from-indicator (indicator) |
| 1014 | "Display documentation of a minor mode specified by INDICATOR. |
| 1015 | If you call this function interactively, you can give indicator which |
| 1016 | is currently activated with completion." |
| 1017 | (interactive (list |
| 1018 | (completing-read |
| 1019 | "Minor mode indicator: " |
| 1020 | (describe-minor-mode-completion-table-for-indicator)))) |
| 1021 | (let ((minor-mode (lookup-minor-mode-from-indicator indicator))) |
| 1022 | (if minor-mode |
| 1023 | (describe-minor-mode-from-symbol minor-mode) |
| 1024 | (error "Cannot find minor mode for `%s'" indicator)))) |
| 1025 | |
| 1026 | (defun lookup-minor-mode-from-indicator (indicator) |
| 1027 | "Return a minor mode symbol from its indicator on the mode line." |
| 1028 | ;; remove first space if existed |
| 1029 | (if (and (< 0 (length indicator)) |
| 1030 | (eq (aref indicator 0) ?\s)) |
| 1031 | (setq indicator (substring indicator 1))) |
| 1032 | (let ((minor-modes minor-mode-alist) |
| 1033 | result) |
| 1034 | (while minor-modes |
| 1035 | (let* ((minor-mode (car (car minor-modes))) |
| 1036 | (anindicator (format-mode-line |
| 1037 | (car (cdr (car minor-modes)))))) |
| 1038 | ;; remove first space if existed |
| 1039 | (if (and (stringp anindicator) |
| 1040 | (> (length anindicator) 0) |
| 1041 | (eq (aref anindicator 0) ?\s)) |
| 1042 | (setq anindicator (substring anindicator 1))) |
| 1043 | (if (equal indicator anindicator) |
| 1044 | (setq result minor-mode |
| 1045 | minor-modes nil) |
| 1046 | (setq minor-modes (cdr minor-modes))))) |
| 1047 | result)) |
| 1048 | \f |
| 1049 | ;;; Automatic resizing of temporary buffers. |
| 1050 | (defcustom temp-buffer-max-height |
| 1051 | (lambda (buffer) |
| 1052 | (if (eq (selected-window) (frame-root-window)) |
| 1053 | (/ (x-display-pixel-height) (frame-char-height) 2) |
| 1054 | (/ (- (frame-height) 2) 2))) |
| 1055 | "Maximum height of a window displaying a temporary buffer. |
| 1056 | This is effective only when Temp Buffer Resize mode is enabled. |
| 1057 | The value is the maximum height (in lines) which |
| 1058 | `resize-temp-buffer-window' will give to a window displaying a |
| 1059 | temporary buffer. It can also be a function to be called to |
| 1060 | choose the height for such a buffer. It gets one argument, the |
| 1061 | buffer, and should return a positive integer. At the time the |
| 1062 | function is called, the window to be resized is selected." |
| 1063 | :type '(choice integer function) |
| 1064 | :group 'help |
| 1065 | :version "24.3") |
| 1066 | |
| 1067 | (defcustom temp-buffer-max-width |
| 1068 | (lambda (buffer) |
| 1069 | (if (eq (selected-window) (frame-root-window)) |
| 1070 | (/ (x-display-pixel-width) (frame-char-width) 2) |
| 1071 | (/ (- (frame-width) 2) 2))) |
| 1072 | "Maximum width of a window displaying a temporary buffer. |
| 1073 | This is effective only when Temp Buffer Resize mode is enabled. |
| 1074 | The value is the maximum width (in columns) which |
| 1075 | `resize-temp-buffer-window' will give to a window displaying a |
| 1076 | temporary buffer. It can also be a function to be called to |
| 1077 | choose the width for such a buffer. It gets one argument, the |
| 1078 | buffer, and should return a positive integer. At the time the |
| 1079 | function is called, the window to be resized is selected." |
| 1080 | :type '(choice integer function) |
| 1081 | :group 'help |
| 1082 | :version "24.4") |
| 1083 | |
| 1084 | (define-minor-mode temp-buffer-resize-mode |
| 1085 | "Toggle auto-resizing temporary buffer windows (Temp Buffer Resize Mode). |
| 1086 | With a prefix argument ARG, enable Temp Buffer Resize mode if ARG |
| 1087 | is positive, and disable it otherwise. If called from Lisp, |
| 1088 | enable the mode if ARG is omitted or nil. |
| 1089 | |
| 1090 | When Temp Buffer Resize mode is enabled, the windows in which we |
| 1091 | show a temporary buffer are automatically resized in height to |
| 1092 | fit the buffer's contents, but never more than |
| 1093 | `temp-buffer-max-height' nor less than `window-min-height'. |
| 1094 | |
| 1095 | A window is resized only if it has been specially created for the |
| 1096 | buffer. Windows that have shown another buffer before are not |
| 1097 | resized. A frame is resized only if `fit-frame-to-buffer' is |
| 1098 | non-nil. |
| 1099 | |
| 1100 | This mode is used by `help', `apropos' and `completion' buffers, |
| 1101 | and some others." |
| 1102 | :global t :group 'help |
| 1103 | (if temp-buffer-resize-mode |
| 1104 | ;; `help-make-xrefs' may add a `back' button and thus increase the |
| 1105 | ;; text size, so `resize-temp-buffer-window' must be run *after* it. |
| 1106 | (add-hook 'temp-buffer-show-hook 'resize-temp-buffer-window 'append) |
| 1107 | (remove-hook 'temp-buffer-show-hook 'resize-temp-buffer-window))) |
| 1108 | |
| 1109 | (defun resize-temp-buffer-window (&optional window) |
| 1110 | "Resize WINDOW to fit its contents. |
| 1111 | WINDOW must be a live window and defaults to the selected one. |
| 1112 | Do not resize if WINDOW was not created by `display-buffer'. |
| 1113 | |
| 1114 | If WINDOW is part of a vertical combination, restrain its new |
| 1115 | size by `temp-buffer-max-height' and do not resize if its minimum |
| 1116 | accessible position is scrolled out of view. If WINDOW is part |
| 1117 | of a horizontal combination, restrain its new size by |
| 1118 | `temp-buffer-max-width'. In both cases, the value of the option |
| 1119 | `fit-window-to-buffer-horizontally' can inhibit resizing. |
| 1120 | |
| 1121 | If WINDOW is the root window of its frame, resize the frame |
| 1122 | provided `fit-frame-to-buffer' is non-nil." |
| 1123 | (setq window (window-normalize-window window t)) |
| 1124 | (let ((height (if (functionp temp-buffer-max-height) |
| 1125 | (with-selected-window window |
| 1126 | (funcall temp-buffer-max-height (window-buffer))) |
| 1127 | temp-buffer-max-height)) |
| 1128 | (width (if (functionp temp-buffer-max-width) |
| 1129 | (with-selected-window window |
| 1130 | (funcall temp-buffer-max-width (window-buffer))) |
| 1131 | temp-buffer-max-width)) |
| 1132 | (quit-cadr (cadr (window-parameter window 'quit-restore)))) |
| 1133 | ;; Resize WINDOW iff it was made by `display-buffer'. |
| 1134 | (when (or (and (eq quit-cadr 'window) |
| 1135 | (or (and (window-combined-p window) |
| 1136 | (not (eq fit-window-to-buffer-horizontally |
| 1137 | 'only)) |
| 1138 | (pos-visible-in-window-p (point-min) window)) |
| 1139 | (and (window-combined-p window t) |
| 1140 | fit-window-to-buffer-horizontally))) |
| 1141 | (and (eq quit-cadr 'frame) |
| 1142 | fit-frame-to-buffer |
| 1143 | (eq window (frame-root-window window)))) |
| 1144 | (fit-window-to-buffer window height nil width)))) |
| 1145 | |
| 1146 | ;;; Help windows. |
| 1147 | (defcustom help-window-select 'other |
| 1148 | "Non-nil means select help window for viewing. |
| 1149 | Choices are: |
| 1150 | never (nil) Select help window only if there is no other window |
| 1151 | on its frame. |
| 1152 | other Select help window unless the selected window is the |
| 1153 | only other window on the help window's frame. |
| 1154 | always (t) Always select the help window. |
| 1155 | |
| 1156 | This option has effect if and only if the help window was created |
| 1157 | by `with-help-window'" |
| 1158 | :type '(choice (const :tag "never (nil)" nil) |
| 1159 | (const :tag "other" other) |
| 1160 | (const :tag "always (t)" t)) |
| 1161 | :group 'help |
| 1162 | :version "23.1") |
| 1163 | |
| 1164 | (defcustom help-enable-auto-load t |
| 1165 | "Whether Help commands can perform autoloading. |
| 1166 | If non-nil, whenever \\[describe-function] is called for an |
| 1167 | autoloaded function whose docstring contains any key substitution |
| 1168 | construct (see `substitute-command-keys'), the library is loaded, |
| 1169 | so that the documentation can show the right key bindings." |
| 1170 | :type 'boolean |
| 1171 | :group 'help |
| 1172 | :version "24.3") |
| 1173 | |
| 1174 | (defun help-window-display-message (quit-part window &optional scroll) |
| 1175 | "Display message telling how to quit and scroll help window. |
| 1176 | QUIT-PART is a string telling how to quit the help window WINDOW. |
| 1177 | Optional argument SCROLL non-nil means tell how to scroll WINDOW. |
| 1178 | SCROLL equal `other' means tell how to scroll the \"other\" |
| 1179 | window." |
| 1180 | (let ((scroll-part |
| 1181 | (cond |
| 1182 | ;; If we don't have QUIT-PART we probably reuse a window |
| 1183 | ;; showing the same buffer so we don't show any message. |
| 1184 | ((not quit-part) nil) |
| 1185 | ((pos-visible-in-window-p |
| 1186 | (with-current-buffer (window-buffer window) |
| 1187 | (point-max)) window t) |
| 1188 | ;; Buffer end is at least partially visible, no need to talk |
| 1189 | ;; about scrolling. |
| 1190 | ".") |
| 1191 | ((eq scroll 'other) |
| 1192 | ", \\[scroll-other-window] to scroll help.") |
| 1193 | (scroll ", \\[scroll-up] to scroll help.")))) |
| 1194 | (message "%s" |
| 1195 | (substitute-command-keys (concat quit-part scroll-part))))) |
| 1196 | |
| 1197 | (defun help-window-setup (window &optional value) |
| 1198 | "Set up help window WINDOW for `with-help-window'. |
| 1199 | WINDOW is the window used for displaying the help buffer. |
| 1200 | Return VALUE." |
| 1201 | (let* ((help-buffer (when (window-live-p window) |
| 1202 | (window-buffer window))) |
| 1203 | (help-setup (when (window-live-p window) |
| 1204 | (car (window-parameter window 'quit-restore))))) |
| 1205 | (when help-buffer |
| 1206 | ;; Handle `help-window-point-marker'. |
| 1207 | (when (eq (marker-buffer help-window-point-marker) help-buffer) |
| 1208 | (set-window-point window help-window-point-marker) |
| 1209 | ;; Reset `help-window-point-marker'. |
| 1210 | (set-marker help-window-point-marker nil)) |
| 1211 | |
| 1212 | (cond |
| 1213 | ((or (eq window (selected-window)) |
| 1214 | (and (or (eq help-window-select t) |
| 1215 | (eq help-setup 'frame) |
| 1216 | (and (eq help-window-select 'other) |
| 1217 | (eq (window-frame window) (selected-frame)) |
| 1218 | (> (length (window-list nil 'no-mini)) 2))) |
| 1219 | (select-window window))) |
| 1220 | ;; The help window is or gets selected ... |
| 1221 | (help-window-display-message |
| 1222 | (cond |
| 1223 | ((eq help-setup 'window) |
| 1224 | ;; ... and is new, ... |
| 1225 | "Type \"q\" to delete help window") |
| 1226 | ((eq help-setup 'frame) |
| 1227 | "Type \"q\" to quit the help frame") |
| 1228 | ((eq help-setup 'other) |
| 1229 | ;; ... or displayed some other buffer before. |
| 1230 | "Type \"q\" to restore previous buffer")) |
| 1231 | window t)) |
| 1232 | ((and (eq (window-frame window) (selected-frame)) |
| 1233 | (= (length (window-list nil 'no-mini)) 2)) |
| 1234 | ;; There are two windows on the help window's frame and the |
| 1235 | ;; other one is the selected one. |
| 1236 | (help-window-display-message |
| 1237 | (cond |
| 1238 | ((eq help-setup 'window) |
| 1239 | "Type \\[delete-other-windows] to delete the help window") |
| 1240 | ((eq help-setup 'other) |
| 1241 | "Type \"q\" in help window to restore its previous buffer")) |
| 1242 | window 'other)) |
| 1243 | (t |
| 1244 | ;; The help window is not selected ... |
| 1245 | (help-window-display-message |
| 1246 | (cond |
| 1247 | ((eq help-setup 'window) |
| 1248 | ;; ... and is new, ... |
| 1249 | "Type \"q\" in help window to delete it") |
| 1250 | ((eq help-setup 'other) |
| 1251 | ;; ... or displayed some other buffer before. |
| 1252 | "Type \"q\" in help window to restore previous buffer")) |
| 1253 | window)))) |
| 1254 | ;; Return VALUE. |
| 1255 | value)) |
| 1256 | |
| 1257 | ;; `with-help-window' is a wrapper for `with-temp-buffer-window' |
| 1258 | ;; providing the following additional twists: |
| 1259 | |
| 1260 | ;; (1) It puts the buffer in `help-mode' (via `help-mode-setup') and |
| 1261 | ;; adds cross references (via `help-mode-finish'). |
| 1262 | |
| 1263 | ;; (2) It issues a message telling how to scroll and quit the help |
| 1264 | ;; window (via `help-window-setup'). |
| 1265 | |
| 1266 | ;; (3) An option (customizable via `help-window-select') to select the |
| 1267 | ;; help window automatically. |
| 1268 | |
| 1269 | ;; (4) A marker (`help-window-point-marker') to move point in the help |
| 1270 | ;; window to an arbitrary buffer position. |
| 1271 | (defmacro with-help-window (buffer-name &rest body) |
| 1272 | "Display buffer named BUFFER-NAME in a help window. |
| 1273 | Evaluate the forms in BODY with standard output bound to a buffer |
| 1274 | called BUFFER-NAME (creating it if it does not exist), put that |
| 1275 | buffer in `help-mode', display the buffer in a window (see |
| 1276 | `with-temp-buffer-window' for details) and issue a message how to |
| 1277 | deal with that \"help\" window when it's no more needed. Select |
| 1278 | the help window if the current value of the user option |
| 1279 | `help-window-select' says so. Return last value in BODY." |
| 1280 | (declare (indent 1) (debug t)) |
| 1281 | `(progn |
| 1282 | ;; Make `help-window-point-marker' point nowhere. The only place |
| 1283 | ;; where this should be set to a buffer position is within BODY. |
| 1284 | (set-marker help-window-point-marker nil) |
| 1285 | (let ((temp-buffer-window-setup-hook |
| 1286 | (cons 'help-mode-setup temp-buffer-window-setup-hook)) |
| 1287 | (temp-buffer-window-show-hook |
| 1288 | (cons 'help-mode-finish temp-buffer-window-show-hook))) |
| 1289 | (with-temp-buffer-window |
| 1290 | ,buffer-name nil 'help-window-setup (progn ,@body))))) |
| 1291 | |
| 1292 | ;; Called from C, on encountering `help-char' when reading a char. |
| 1293 | ;; Don't print to *Help*; that would clobber Help history. |
| 1294 | (defun help-form-show () |
| 1295 | "Display the output of a non-nil `help-form'." |
| 1296 | (let ((msg (eval help-form))) |
| 1297 | (if (stringp msg) |
| 1298 | (with-output-to-temp-buffer " *Char Help*" |
| 1299 | (princ msg))))) |
| 1300 | |
| 1301 | \f |
| 1302 | ;; The following functions used to be in help-fns.el, which is not preloaded. |
| 1303 | ;; But for various reasons, they are more widely needed, so they were |
| 1304 | ;; moved to this file, which is preloaded. http://debbugs.gnu.org/17001 |
| 1305 | |
| 1306 | (defun help-split-fundoc (docstring def) |
| 1307 | "Split a function DOCSTRING into the actual doc and the usage info. |
| 1308 | Return (USAGE . DOC) or nil if there's no usage info, where USAGE info |
| 1309 | is a string describing the argument list of DEF, such as |
| 1310 | \"(apply FUNCTION &rest ARGUMENTS)\". |
| 1311 | DEF is the function whose usage we're looking for in DOCSTRING." |
| 1312 | ;; Functions can get the calling sequence at the end of the doc string. |
| 1313 | ;; In cases where `function' has been fset to a subr we can't search for |
| 1314 | ;; function's name in the doc string so we use `fn' as the anonymous |
| 1315 | ;; function name instead. |
| 1316 | (when (and docstring (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring)) |
| 1317 | (cons (format "(%s%s" |
| 1318 | ;; Replace `fn' with the actual function name. |
| 1319 | (if (symbolp def) def "anonymous") |
| 1320 | (match-string 1 docstring)) |
| 1321 | (unless (zerop (match-beginning 0)) |
| 1322 | (substring docstring 0 (match-beginning 0)))))) |
| 1323 | |
| 1324 | (defun help-add-fundoc-usage (docstring arglist) |
| 1325 | "Add the usage info to DOCSTRING. |
| 1326 | If DOCSTRING already has a usage info, then just return it unchanged. |
| 1327 | The usage info is built from ARGLIST. DOCSTRING can be nil. |
| 1328 | ARGLIST can also be t or a string of the form \"(FUN ARG1 ARG2 ...)\"." |
| 1329 | (unless (stringp docstring) (setq docstring "")) |
| 1330 | (if (or (string-match "\n\n(fn\\(\\( .*\\)?)\\)\\'" docstring) |
| 1331 | (eq arglist t)) |
| 1332 | docstring |
| 1333 | (concat docstring |
| 1334 | (if (string-match "\n?\n\\'" docstring) |
| 1335 | (if (< (- (match-end 0) (match-beginning 0)) 2) "\n" "") |
| 1336 | "\n\n") |
| 1337 | (if (and (stringp arglist) |
| 1338 | (string-match "\\`([^ ]+\\(.*\\))\\'" arglist)) |
| 1339 | (concat "(fn" (match-string 1 arglist) ")") |
| 1340 | (format "%S" (help-make-usage 'fn arglist)))))) |
| 1341 | |
| 1342 | (defun help-function-arglist (def &optional preserve-names) |
| 1343 | "Return a formal argument list for the function DEF. |
| 1344 | IF PRESERVE-NAMES is non-nil, return a formal arglist that uses |
| 1345 | the same names as used in the original source code, when possible." |
| 1346 | ;; Handle symbols aliased to other symbols. |
| 1347 | (if (and (symbolp def) (fboundp def)) (setq def (indirect-function def))) |
| 1348 | ;; If definition is a macro, find the function inside it. |
| 1349 | (if (eq (car-safe def) 'macro) (setq def (cdr def))) |
| 1350 | (cond |
| 1351 | ((and (byte-code-function-p def) (listp (aref def 0))) (aref def 0)) |
| 1352 | ((eq (car-safe def) 'lambda) (nth 1 def)) |
| 1353 | ((eq (car-safe def) 'closure) (nth 2 def)) |
| 1354 | ((or (and (byte-code-function-p def) (integerp (aref def 0))) |
| 1355 | (subrp def)) |
| 1356 | (or (when preserve-names |
| 1357 | (let* ((doc (condition-case nil (documentation def) (error nil))) |
| 1358 | (docargs (if doc (car (help-split-fundoc doc nil)))) |
| 1359 | (arglist (if docargs |
| 1360 | (cdar (read-from-string (downcase docargs))))) |
| 1361 | (valid t)) |
| 1362 | ;; Check validity. |
| 1363 | (dolist (arg arglist) |
| 1364 | (unless (and (symbolp arg) |
| 1365 | (let ((name (symbol-name arg))) |
| 1366 | (if (eq (aref name 0) ?&) |
| 1367 | (memq arg '(&rest &optional)) |
| 1368 | (not (string-match "\\." name))))) |
| 1369 | (setq valid nil))) |
| 1370 | (when valid arglist))) |
| 1371 | (let* ((args-desc (if (not (subrp def)) |
| 1372 | (aref def 0) |
| 1373 | (let ((a (subr-arity def))) |
| 1374 | (logior (car a) |
| 1375 | (if (numberp (cdr a)) |
| 1376 | (lsh (cdr a) 8) |
| 1377 | (lsh 1 7)))))) |
| 1378 | (max (lsh args-desc -8)) |
| 1379 | (min (logand args-desc 127)) |
| 1380 | (rest (logand args-desc 128)) |
| 1381 | (arglist ())) |
| 1382 | (dotimes (i min) |
| 1383 | (push (intern (concat "arg" (number-to-string (1+ i)))) arglist)) |
| 1384 | (when (> max min) |
| 1385 | (push '&optional arglist) |
| 1386 | (dotimes (i (- max min)) |
| 1387 | (push (intern (concat "arg" (number-to-string (+ 1 i min)))) |
| 1388 | arglist))) |
| 1389 | (unless (zerop rest) (push '&rest arglist) (push 'rest arglist)) |
| 1390 | (nreverse arglist)))) |
| 1391 | ((and (autoloadp def) (not (eq (nth 4 def) 'keymap))) |
| 1392 | "[Arg list not available until function definition is loaded.]") |
| 1393 | (t t))) |
| 1394 | |
| 1395 | (defun help-make-usage (function arglist) |
| 1396 | (cons (if (symbolp function) function 'anonymous) |
| 1397 | (mapcar (lambda (arg) |
| 1398 | (if (not (symbolp arg)) arg |
| 1399 | (let ((name (symbol-name arg))) |
| 1400 | (cond |
| 1401 | ((string-match "\\`&" name) arg) |
| 1402 | ((string-match "\\`_" name) |
| 1403 | (intern (upcase (substring name 1)))) |
| 1404 | (t (intern (upcase name))))))) |
| 1405 | arglist))) |
| 1406 | |
| 1407 | \f |
| 1408 | (provide 'help) |
| 1409 | |
| 1410 | ;;; help.el ends here |