home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / prim / help.el < prev    next >
Encoding:
Text File  |  1993-02-17  |  16.8 KB  |  463 lines

  1. ;; Help commands for Emacs
  2. ;; Copyright (C) 1985-1993 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 2, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (defvar help-map (make-sparse-keymap)
  22.   "Keymap for characters following the Help key.")
  23.  
  24. (fset 'help-command help-map)
  25.  
  26. (define-key help-map '(control h) 'help-for-help)
  27. (define-key help-map "?" 'help-for-help)
  28.  
  29. (define-key help-map "\C-c" 'describe-copying)
  30. (define-key help-map "\C-d" 'describe-distribution)
  31. (define-key help-map "\C-w" 'describe-no-warranty)
  32. (define-key help-map "a" 'command-apropos)
  33.  
  34. (define-key help-map "b" 'describe-bindings)
  35. (define-key help-map "p" 'describe-pointer)
  36.  
  37. (define-key help-map "c" 'describe-key-briefly)
  38. (define-key help-map "k" 'describe-key)
  39.  
  40. (define-key help-map "d" 'describe-function)
  41. (define-key help-map "f" 'describe-function)
  42.  
  43. (define-key help-map "i" 'info)
  44.  
  45. (define-key help-map "l" 'view-lossage)
  46.  
  47. (define-key help-map "m" 'describe-mode)
  48.  
  49. (define-key help-map "\C-n" 'view-emacs-news)
  50. (define-key help-map "n" 'view-emacs-news)
  51.  
  52. (define-key help-map "s" 'describe-syntax)
  53.  
  54. (define-key help-map "t" 'help-with-tutorial)
  55.  
  56. (define-key help-map "w" 'where-is)
  57.  
  58. (define-key help-map "v" 'describe-variable)
  59.  
  60. ;; This is a grody hack of the same genotype as `advertised-undo'; if the
  61. ;; bindings of Backspace and C-h are the same, we want the menubar to claim
  62. ;; that `info' in invoked with `C-h i', not `BS i'.
  63.  
  64. (defun deprecated-help-command ()
  65.   (interactive)
  66.   (if (eq 'help-command (key-binding "\C-h"))
  67.       (setq unread-command-event (character-to-event ?\C-h (allocate-event)))
  68.     (help-for-help)))
  69.  
  70. ;;(define-key global-map 'backspace 'deprecated-help-command)
  71.  
  72. (defun help-with-tutorial ()
  73.   "Select the Emacs learn-by-doing tutorial."
  74.   (interactive)
  75.   (let ((file (expand-file-name "~/TUTORIAL")))
  76.     (delete-other-windows)
  77.     (if (get-file-buffer file)
  78.     (switch-to-buffer (get-file-buffer file))
  79.       (switch-to-buffer (create-file-buffer file))
  80.       (setq buffer-file-name file)
  81.       (setq default-directory (expand-file-name "~/"))
  82.       (setq buffer-auto-save-file-name nil)
  83.       (insert-file-contents (expand-file-name "TUTORIAL" exec-directory))
  84.       (goto-char (point-min))
  85.       (search-forward "\n<<")
  86.       (beginning-of-line)
  87.       (delete-region (point) (progn (end-of-line) (point)))
  88.       (newline (- (window-height (selected-window))
  89.           (count-lines (point-min) (point))
  90.           6))
  91.       (goto-char (point-min))
  92.       (set-buffer-modified-p nil))))
  93.  
  94. (defun describe-key-briefly (key)
  95.   "Print the name of the function KEY invokes.  KEY is a string."
  96.   (interactive "kDescribe key briefly: ")
  97.   (let (defn)
  98.     ;; If the key typed was really a menu selection, grab the form out
  99.     ;; of the event object and intuit the function that would be called,
  100.     ;; and describe that instead.
  101.     (if (and (vectorp key) (= 1 (length key)) (menu-event-p (aref key 0)))
  102.     (let ((event (aref key 0)))
  103.       (setq defn (list (event-function event) (event-object event)))
  104.       (if (eq (car defn) 'eval)
  105.           (setq defn (car (cdr defn))))
  106.       (if (eq (car-safe defn) 'call-interactively)
  107.           (setq defn (car (cdr defn))))
  108.       (if (and (consp defn) (null (cdr defn)))
  109.           (setq defn (car defn))))
  110.       (setq defn (key-binding key)))
  111.     (if (or (null defn) (integerp defn))
  112.         (message "%s is undefined" (key-description key))
  113.       (message "%s runs the command %s"
  114.            (key-description key)
  115.            (if (symbolp defn) defn (prin1-to-string defn))))))
  116.  
  117. (defun print-help-return-message (&optional function)
  118.   "Display or return message saying how to restore windows after help command.
  119. Computes a message and applies the argument FUNCTION to it.
  120. If FUNCTION is nil, applies `message' to it, thus printing it."
  121.   (and (not (get-buffer-window standard-output))
  122.        (funcall (or function 'message)
  123.         (substitute-command-keys
  124.          (if (one-window-p t)
  125.              (if pop-up-windows
  126.              "Type \\[delete-other-windows] to remove help window."
  127.                "Type \\[switch-to-buffer] RET to remove help window.")
  128.            "Type \\[switch-to-buffer-other-window] RET to restore old contents of help window.")))))
  129.  
  130. (defun describe-key (key)
  131.   "Display documentation of the function KEY invokes.  
  132. KEY is a string, or vector of events.  When called interactvely, key may
  133. also be a menu selection."
  134.   (interactive "kDescribe key: ")
  135.   (let (defn)
  136.     ;; If the key typed was really a menu selection, grab the form out
  137.     ;; of the event object and intuit the function that would be called,
  138.     ;; and describe that instead.
  139.     (if (and (vectorp key) (= 1 (length key)) (menu-event-p (aref key 0)))
  140.     (let ((event (aref key 0)))
  141.       (setq defn (list (event-function event) (event-object event)))
  142.       (if (eq (car defn) 'eval)
  143.           (setq defn (car (cdr defn))))
  144.       (if (eq (car-safe defn) 'call-interactively)
  145.           (setq defn (car (cdr defn))))
  146.       (if (and (consp defn) (null (cdr defn)))
  147.           (setq defn (car defn))))
  148.       (setq defn (key-binding key)))
  149.     (if (or (null defn) (integerp defn))
  150.         (message "%s is undefined" (key-description key))
  151.       (with-output-to-temp-buffer "*Help*"
  152. ;    (princ (key-description key))
  153. ;    (princ " runs the command ")
  154.     (prin1 defn)
  155.     (princ ":\n")
  156.     (if (or (stringp defn) (vectorp defn))
  157.         (let ((cmd (key-binding defn)))
  158.           (princ "a keyboard macro")
  159.           (if cmd
  160.           (progn
  161.             (princ " which runs the command ")
  162.             (princ cmd)
  163.             (princ ":\n\n")
  164.             (if (documentation cmd) (princ (documentation cmd))))))
  165.       (if (documentation defn)
  166.           (princ (documentation defn))
  167.         (princ "not documented")))
  168.     (print-help-return-message)))))
  169.  
  170.  
  171. (defun where-is (definition)
  172.   "Print message listing key sequences that invoke specified command.
  173. Argument is a command definition, usually a symbol with a function definition."
  174.   (interactive "CWhere is command: ")
  175.   (let ((keys (where-is-internal definition (current-local-map) nil nil nil)))
  176.     (if keys
  177.     (message "%s is on %s" definition
  178.       (mapconcat 'key-description
  179.              (sort keys '(lambda (x y) (< (length x) (length y))))
  180.              ", "))
  181.       (message "%s is not on any keys" definition)))
  182.   nil)
  183.  
  184.  
  185. (defun describe-mode ()
  186.   "Display documentation of current major mode."
  187.   (interactive)
  188.   (with-output-to-temp-buffer "*Help*"
  189.     (princ mode-name)
  190.     (princ " Mode:\n")
  191.     (princ (documentation major-mode))
  192.     (print-help-return-message)))
  193.  
  194. (defun describe-distribution ()
  195.   "Display info on how to obtain the latest version of GNU Emacs."
  196.   (interactive)
  197.   (find-file-read-only
  198.    (expand-file-name "DISTRIB" exec-directory)))
  199.  
  200. (defun describe-copying ()
  201.   "Display info on how you may redistribute copies of GNU Emacs."
  202.   (interactive)
  203.   (find-file-read-only
  204.    (expand-file-name "COPYING" exec-directory))
  205.   (goto-char (point-min)))
  206.  
  207. (defun describe-pointer ()
  208.   "Show a list of all defined mouse buttons, and their definitions.
  209. This is the same as \\[universal-argument] \\[describe-bindings]."
  210.   (interactive)
  211.   (describe-bindings t))
  212.  
  213. (defun describe-no-warranty ()
  214.   "Display info on all the kinds of warranty Emacs does NOT have."
  215.   (interactive)
  216.   (describe-copying)
  217.   (let (case-fold-search)
  218.     (search-forward "NO WARRANTY")
  219.     (recenter 0)))
  220.  
  221. (defun view-emacs-news ()
  222.   "Display info on recent changes to Emacs."
  223.   (interactive)
  224.   (find-file-read-only (expand-file-name "NEWS" exec-directory)))
  225.  
  226. (defun view-lossage ()
  227.   "Display last 100 input keystrokes."
  228.   (interactive)
  229.   (with-output-to-temp-buffer "*Help*"
  230.     (princ (key-description (recent-keys)))
  231.     (save-excursion
  232.       (set-buffer standard-output)
  233.       (goto-char (point-min))
  234.       (while (progn (move-to-column 50) (not (eobp)))
  235.     (search-forward " " nil t)
  236.     (insert "\n")))
  237.     (print-help-return-message)))
  238.  
  239. (defun help-for-help ()
  240.   "You have typed C-h, the help character.  Type a Help option:
  241.  
  242. A  command-apropos.   Give a substring, and see a list of commands
  243.               (functions interactively callable) that contain
  244.           that substring.  See also the  apropos  command.
  245. B  describe-bindings.  Display table of all key bindings.
  246. C  describe-key-briefly.  Type a command key sequence;
  247.           it prints the function name that sequence runs.
  248. F  describe-function.  Type a function name and get documentation of it.
  249. I  info. The  info  documentation reader.
  250. K  describe-key.  Type a command key sequence;
  251.           it displays the full documentation.
  252. L  view-lossage.  Shows last 100 characters you typed.
  253. M  describe-mode.  Print documentation of current major mode,
  254.           which describes the commands peculiar to it.
  255. N  view-emacs-news.  Shows emacs news file.
  256. P  describe-pointer.  Display table of all mouse-button bindings.
  257. S  describe-syntax.  Display contents of syntax table, plus explanations
  258. T  help-with-tutorial.  Select the Emacs learn-by-doing tutorial.
  259. V  describe-variable.  Type name of a variable;
  260.           it displays the variable's documentation and value.
  261. W  where-is.  Type command name; it prints which keystrokes
  262.           invoke that command.
  263. C-c print Emacs copying permission (General Public License).
  264. C-d print Emacs ordering information.
  265. C-n print news of recent Emacs changes.
  266. C-w print information on absence of warranty for GNU Emacs."
  267.   (interactive)
  268.   (let ((help-key (copy-event last-command-event))
  269.     event char)
  270.     (message
  271.   "A B C F I K L M N P S T V W C-c C-d C-n C-w.  Type %s again for more help: "
  272.      (single-key-description help-key))
  273.     (setq event (next-command-event (allocate-event))
  274.       char (or (event-to-character event) event))
  275.     (if (or (equal char help-key) (equal char ?\C-h) (equal char ??))
  276.     (save-window-excursion
  277.       (switch-to-buffer "*Help*")
  278.       (erase-buffer)
  279.       (insert (documentation 'help-for-help))
  280.       (goto-char (point-min))
  281.       (while (or (equal char help-key)
  282.              (memq char '(?\C-h ?? ?\C-v ?\ ?\177 ?\M-v)))
  283.         (if (memq char '(?\C-v ?\ ))
  284.         (scroll-up))
  285.         (if (memq char '(?\177 ?\M-v))
  286.         (scroll-down))
  287.         (message "A B C F I K L M N P S T V W C-c C-d C-n C-w%s: "
  288.              (if (pos-visible-in-window-p (point-max))
  289.              "" " or Space to scroll"))
  290.         (let ((cursor-in-echo-area t))
  291.           (setq event (next-command-event event)
  292.             char (or (event-to-character event) event))))))
  293.     (let ((defn (lookup-key help-map (if (eventp char) (vector char)
  294.                        (char-to-string (downcase char))))))
  295.       (if defn (call-interactively defn) (ding)))))
  296.  
  297.  
  298. (defun function-called-at-point ()
  299.   (condition-case ()
  300.       (save-excursion
  301.     (save-restriction
  302.       (narrow-to-region (max (point-min) (- (point) 1000)) (point-max))
  303.       (backward-up-list 1)
  304.       (forward-char 1)
  305.       (let (obj)
  306.         (setq obj (read (current-buffer)))
  307.         (and (symbolp obj) (fboundp obj) obj))))
  308.     (error nil)))
  309.  
  310. (defvar describe-function-show-arglist t  ; default to nil for the non-hackers?
  311.   "*If true, then describe-function will show its arglist if the function is
  312. not an autoload.")
  313.  
  314.  
  315. (defun describe-function (function)
  316.   "Display the full documentation of FUNCTION (a symbol)."
  317.   (interactive
  318.     (let* ((fn (function-called-at-point))
  319.            (val (let ((enable-recursive-minibuffers t))
  320.                   (completing-read
  321.                     (if fn 
  322.                         (format "Describe function (default %s): " fn)
  323.                         "Describe function: ")
  324.                     obarray 'fboundp t))))
  325.       (list (if (equal val "") fn (intern val)))))
  326.   (with-output-to-temp-buffer "*Help*"
  327.     (describe-function-1 function standard-output)
  328.     (print-help-return-message)
  329.     (save-excursion (set-buffer standard-output) (buffer-string))))
  330.  
  331. (defun describe-function-1 (function stream)
  332.   (prin1 function stream)
  333.   (princ ": " stream)
  334.   (let* ((def function)
  335.          (doc (or (documentation function)
  336.                   "not documented"))
  337.      aliases kbd-macro-p)
  338.     (while (symbolp def)
  339.       (or (eq def function)
  340.       (if aliases
  341.           (setq aliases (concat aliases "\n     which is an alias for "
  342.                     (symbol-name def) ", "))
  343.         (setq aliases (concat "an alias for " (symbol-name def) ", "))))
  344.       (setq def (symbol-function def)))
  345.     (if describe-function-show-arglist
  346.         (if (cond ((eq 'autoload (car-safe def))
  347.                    nil)
  348.                   ((eq 'lambda (car-safe def))
  349.                    (princ (or (nth 1 def) "()") stream)
  350.                    t)
  351.                   ((compiled-function-p def)
  352.                    (princ (or (aref def 0) "()") stream)
  353.                    t)
  354.                   ((and (subrp def)
  355.                         (string-match "[\n\t ]*\narguments: ?\\((.*)\\)\n?\\'"
  356.                                       doc))
  357.                    (princ (substring doc (match-beginning 1) (match-end 1))
  358.                           stream)
  359.                    (setq doc (substring doc 0 (match-beginning 0)))
  360.                    t)
  361.                   (t
  362.                    nil))
  363.             (princ "\n  -- " stream)))
  364.     (if aliases (princ aliases stream))
  365.     (let ((int (function (lambda (string)
  366.                  (princ (if (commandp def) "an interactive " "a ") stream)
  367.                  (princ string stream)))))
  368.       (cond ((or (stringp def) (vectorp def))
  369.              (princ "a keyboard macro." stream)
  370.          (setq kbd-macro-p t))
  371.             ((subrp def)
  372.              (funcall int "built-in function."))
  373.             ((compiled-function-p def)
  374.              (funcall int "compiled Lisp function."))
  375.             ((symbolp def)
  376.              (princ (format "alias for `%s'." (prin1-to-string def)) stream))
  377.             ((eq (car-safe def) 'lambda)
  378.              (funcall int "Lisp function."))
  379.             ((eq (car-safe def) 'macro)
  380.              (princ "a Lisp macro." stream))
  381.             ((eq (car-safe def) 'mocklisp)
  382.              (princ "a mocklisp function." stream))
  383.             ((eq (car-safe def) 'autoload)
  384.              (funcall int "autoloaded Lisp ")
  385.              (princ (if (elt def 4) "macro" "function") stream)
  386.          (princ "\n  -- loads from " stream) (prin1 (elt def 1) stream))
  387.             (t
  388.              nil)))
  389.     (terpri)
  390.     (cond (kbd-macro-p
  391.        (princ "These characters are executed:\n\n\t" stream)
  392.        (princ (key-description def) stream)
  393.        (cond ((setq def (key-binding def))
  394.           (princ "\n\nwhich executes the command " stream)
  395.           (princ def stream)
  396.           (princ ".\n\n" stream)
  397.           (describe-function-1 def stream))))
  398.       (t
  399.        (princ doc stream)))))
  400.  
  401.  
  402. (defun variable-at-point ()
  403.   (condition-case ()
  404.       (save-excursion
  405.     (forward-sexp -1)
  406.     (skip-chars-forward "'")
  407.     (let ((obj (read (current-buffer))))
  408.       (and (symbolp obj) (boundp obj) obj)))
  409.     (error nil)))
  410.  
  411. (defun describe-variable (variable)
  412.   "Display the full documentation of VARIABLE (a symbol)."
  413.   (interactive 
  414.    (let ((v (variable-at-point))
  415.      (enable-recursive-minibuffers t)
  416.      val)
  417.      (setq val (completing-read (if v
  418.                     (format "Describe variable (default %s): " v)
  419.                   "Describe variable: ")
  420.                 obarray 'boundp t))
  421.      (list (if (equal val "")
  422.            v (intern val)))))
  423.   (with-output-to-temp-buffer "*Help*"
  424.     (prin1 variable)
  425.     (princ "'s value is ")
  426.     (if (not (boundp variable))
  427.         (princ "void.")
  428.       (prin1 (symbol-value variable)))
  429.     (terpri) (terpri)
  430.     (princ "Documentation:")
  431.     (terpri)
  432.     (let ((doc (documentation-property variable 'variable-documentation)))
  433.       (if doc
  434.       ;; note: documentation-property calls substitute-command-keys.
  435.       (princ doc)
  436.     (princ "not documented as a variable.")))
  437.     (print-help-return-message)))
  438.  
  439. (defun command-apropos (string)
  440.   "Like apropos but lists only symbols that are names of commands
  441. \(interactively callable functions).  Argument REGEXP is a regular expression
  442. that is matched against command symbol names.  Returns list of symbols and
  443. documentation found."
  444.   (interactive "sCommand apropos (regexp): ")
  445.   (let ((message
  446.      (let ((standard-output (get-buffer-create "*Help*")))
  447.        (print-help-return-message 'identity))))
  448.     (apropos string t 'commandp)
  449.     (and message (message message))))
  450.  
  451. (defun locate-library (library &optional nosuffix)
  452.   "Show the full path name of Emacs library LIBRARY.
  453. This command searches the directories in `load-path' like  M-x load-library
  454. to find the file that  M-x load-library RET LIBRARY RET  would load.
  455. Optional prefix arg NOSUFFIX non-nil means don't add suffixes `.elc' or `.el'
  456. to the specified name LIBRARY - a la calling (load LIBRARY nil nil t)."
  457.   (interactive "sLocate library: \nP")
  458.   (let ((file (locate-file library load-path (if nosuffix nil ".elc:.el:"))))
  459.     (if file
  460.     (message "Library is file %s" file)
  461.       (message "No library %s in search path" library))
  462.     file))
  463.