]> code.delx.au - gnu-emacs/blobdiff - lisp/help.el
(tmm-menubar-mouse): Add autoload cookie.
[gnu-emacs] / lisp / help.el
index c11e2cb91bccbe9eb19c7997f40f0efa9913bded..f17484fe9f48718dd60598753c301027fafdf0e0 100644 (file)
   "Keymap for help mode.")
 
 (define-key global-map (char-to-string help-char) 'help-command)
+(define-key global-map [help] 'help-command)
+(define-key global-map [f1] 'help-command)
 (fset 'help-command help-map)
 
 (define-key help-map (char-to-string help-char) 'help-for-help)
+(define-key help-map [help] 'help-for-help)
+(define-key help-map [f1] 'help-for-help)
 (define-key help-map "?" 'help-for-help)
 
 (define-key help-map "\C-c" 'describe-copying)
@@ -58,6 +62,8 @@
 (define-key help-map "d" 'describe-function)
 (define-key help-map "f" 'describe-function)
 
+(define-key help-map "F" 'view-emacs-FAQ)
+
 (define-key help-map "i" 'info)
 (define-key help-map "\C-f" 'Info-goto-emacs-command-node)
 (define-key help-map "\C-k" 'Info-goto-emacs-key-command-node)
 
 (define-key help-map "q" 'help-quit)
 
+(defvar help-font-lock-keywords
+  (let ((name-char "[-+a-zA-Z0-9_*]") (sym-char "[-+a-zA-Z0-9_:*]"))
+    (list
+     ;;
+     ;; The symbol itself.
+     (list (concat "\\`\\(" name-char "+\\)\\(:\\)?")
+          '(1 (if (match-beginning 2)
+                  font-lock-function-name-face
+                font-lock-variable-name-face)
+              nil t))
+     ;;
+     ;; Words inside `' which tend to be symbol names.
+     (list (concat "`\\(" sym-char sym-char "+\\)'")
+          1 'font-lock-reference-face t)
+     ;;
+     ;; CLisp `:' keywords as references.
+     (list (concat "\\<:" sym-char "+\\>") 0 'font-lock-reference-face t)))
+  "Default expressions to highlight in Help mode.")
+
 (defun help-mode ()
   "Major mode for viewing help text.
 Entry to this mode runs the normal hook `help-mode-hook'.
@@ -93,6 +118,9 @@ Commands:
   (use-local-map help-mode-map)
   (setq mode-name "Help")
   (setq major-mode 'help-mode)
+  (make-local-variable 'font-lock-defaults)
+  (setq font-lock-defaults '(help-font-lock-keywords))
+  (view-mode)
   (run-hooks 'help-mode-hook))
 
 (defun help-quit ()
@@ -139,12 +167,29 @@ Commands:
     (and (symbolp type)
         (memq 'down (event-modifiers type))
         (read-event)))
-  (let ((defn (key-binding key)))
-    (if (or (null defn) (integerp defn))
-        (message "%s is undefined" (key-description key))
-      (message "%s runs the command %s"
-              (key-description key)
-              (if (symbolp defn) defn (prin1-to-string defn))))))
+  (save-excursion
+    (let ((modifiers (event-modifiers (aref key 0)))
+         window position)
+      ;; For a mouse button event, go to the button it applies to
+      ;; to get the right key bindings.  And go to the right place
+      ;; in case the keymap depends on where you clicked.
+      (if (or (memq 'click modifiers) (memq 'down modifiers)
+             (memq 'drag modifiers))
+         (setq window (posn-window (event-start (aref key 0)))
+               position (posn-point (event-start (aref key 0)))))
+      (if (windowp window)
+         (progn
+           (set-buffer (window-buffer window))
+           (goto-char position)))
+      ;; Ok, now look up the key and name the command.
+      (let ((defn (key-binding key)))
+       (if (or (null defn) (integerp defn))
+           (message "%s is undefined" (key-description key))
+         (message (if (windowp window)
+                      "%s at that spot runs the command %s"
+                    "%s runs the command %s")
+                  (key-description key)
+                  (if (symbolp defn) defn (prin1-to-string defn))))))))
 
 (defun print-help-return-message (&optional function)
   "Display or return message saying how to restore windows after help command.
@@ -188,8 +233,33 @@ If FUNCTION is nil, applies `message' to it, thus printing it."
                       (substitute-command-keys first-message)
                     "")
                   (if first-message "  " "")
-                  (substitute-command-keys
-                   "\\[scroll-other-window] to scroll the help."))))))
+                  ;; If the help buffer will go in a separate frame,
+                  ;; it's no use mentioning a command to scroll, so don't.
+                  (if (or (member (buffer-name standard-output)
+                                  special-display-buffer-names)
+                          (assoc (buffer-name standard-output)
+                                 special-display-buffer-names)
+                          (memq t (mapcar '(lambda (elt)
+                                             (if (consp elt)
+                                                 (setq elt (car elt)))
+                                             (string-match elt (buffer-name standard-output)))
+                                          special-display-regexps)))
+                      nil
+                    (if (or (member (buffer-name standard-output)
+                                    same-window-buffer-names)
+                            (assoc (buffer-name standard-output)
+                                   same-window-buffer-names)
+                            (memq t (mapcar '(lambda (elt)
+                                               (if (consp elt)
+                                                   (setq elt (car elt)))
+                                               (string-match elt (buffer-name standard-output)))
+                                            same-window-regexps)))
+                        ;; Say how to scroll this window.
+                        (substitute-command-keys
+                         "\\[scroll-up] to scroll the help.")
+                      ;; Say how to scroll some other window.
+                      (substitute-command-keys
+                       "\\[scroll-other-window] to scroll the help."))))))))
 
 (defun describe-key (key)
   "Display documentation of the function invoked by KEY.  KEY is a string."
@@ -202,16 +272,39 @@ If FUNCTION is nil, applies `message' to it, thus printing it."
     (and (symbolp type)
         (memq 'down (event-modifiers type))
         (read-event)))
-  (let ((defn (key-binding key)))
-    (if (or (null defn) (integerp defn))
-        (message "%s is undefined" (key-description key))
-      (with-output-to-temp-buffer "*Help*"
-       (prin1 defn)
-       (princ ":\n")
-       (if (documentation defn)
-           (princ (documentation defn))
-         (princ "not documented"))
-       (print-help-return-message)))))
+  (save-excursion
+    (let ((modifiers (event-modifiers (aref key 0)))
+         window position)
+      ;; For a mouse button event, go to the button it applies to
+      ;; to get the right key bindings.  And go to the right place
+      ;; in case the keymap depends on where you clicked.
+      (if (or (memq 'click modifiers) (memq 'down modifiers)
+             (memq 'drag modifiers))
+         (setq window (posn-window (event-start (aref key 0)))
+               position (posn-point (event-start (aref key 0)))))
+      (if (windowp window)
+         (progn
+           (set-buffer (window-buffer window))
+           (goto-char position)))
+      (let ((defn (key-binding key)))
+       (if (or (null defn) (integerp defn))
+           (message "%s is undefined" (key-description key))
+         (with-output-to-temp-buffer "*Help*"
+           (princ (key-description key))
+           (if (windowp window)
+               (princ " at that spot"))
+           (princ " runs the command ")
+           (prin1 defn)
+           (princ ":\n")
+           (let ((doc (documentation defn)))
+             (if doc
+                 (progn (terpri)
+                        (princ doc))
+               (princ "not documented")))
+           (save-excursion
+             (set-buffer standard-output)
+             (help-mode))
+           (print-help-return-message)))))))
 
 (defun describe-mode ()
   "Display documentation of current major mode and minor modes.
@@ -248,6 +341,9 @@ describes the minor mode."
     (princ mode-name)
     (princ " mode:\n")
     (princ (documentation major-mode))
+    (save-excursion
+      (set-buffer standard-output)
+      (help-mode))
     (print-help-return-message)))
 
 ;; So keyboard macro definitions are documented correctly
@@ -286,14 +382,16 @@ describes the minor mode."
 The prefix described consists of all but the last event
 of the key sequence that ran this command."
   (interactive)
-  (let* ((key (this-command-keys))
-        (prefix (make-vector (1- (length key)) nil))
-        i)
-    (setq i 0)
-    (while (< i (length prefix))
-      (aset prefix i (aref key i))
-      (setq i (1+ i)))
-    (describe-bindings prefix)))
+  (let* ((key (this-command-keys)))
+    (describe-bindings
+     (if (stringp key)
+        (substring key 0 (1- (length key)))
+       (let ((prefix (make-vector (1- (length key)) nil))
+            (i 0))
+        (while (< i (length prefix))
+          (aset prefix i (aref key i))
+          (setq i (1+ i)))
+        prefix)))))
 ;; Make C-h after a prefix, when not specifically bound, 
 ;; run describe-prefix-bindings.
 (setq prefix-help-command 'describe-prefix-bindings)
@@ -303,6 +401,11 @@ of the key sequence that ran this command."
   (interactive)
   (find-file-read-only (expand-file-name "NEWS" data-directory)))
 
+(defun view-emacs-FAQ ()
+  "Display the Emacs Frequently Asked Questions (FAQ) file."
+  (interactive)
+  (find-file-read-only (expand-file-name "FAQ" data-directory)))
+
 (defun view-lossage ()
   "Display last 100 input keystrokes."
   (interactive)
@@ -320,15 +423,15 @@ of the key sequence that ran this command."
       (goto-char (point-min))
       (while (progn (move-to-column 50) (not (eobp)))
        (search-forward " " nil t)
-       (insert "\n")))
+       (insert "\n"))
+      (help-mode))
     (print-help-return-message)))
 
 (defalias 'help 'help-for-help)
 (make-help-screen help-for-help
   "a b c f C-f i k C-k l m n p s t v w C-c C-d C-n C-w, or ? for more help:"
   "You have typed \\[help-command], the help character.  Type a Help option:
-\(Use \\<help-map>\\[scroll-up] or \\[scroll-down] to scroll through this text.
-Type \\<help-map>\\[help-quit] to exit the Help command.)
+\(Use SPC or DEL to scroll through this text.  Type \\<help-map>\\[help-quit] to exit the Help command.)
 
 a  command-apropos.  Give a substring, and see a list of commands
        (functions interactively callable) that contain
@@ -339,6 +442,7 @@ c  describe-key-briefly.  Type a command key sequence;
 f  describe-function.  Type a function name and get documentation of it.
 C-f Info-goto-emacs-command-node.  Type a function name;
        it takes you to the Info node for that command.
+F  view-emacs-FAQ.  Shows emacs frequently asked questions file.
 i  info. The  info  documentation reader.
 k  describe-key.  Type a command key sequence;
        it displays the full documentation.
@@ -377,11 +481,18 @@ C-w print information on absence of warranty for GNU Emacs."
                (and (symbolp obj) (fboundp obj) obj))))
        (error nil))
       (condition-case ()
-         (save-excursion
-           (forward-sexp -1)
-           (skip-chars-forward "'")
-           (let ((obj (read (current-buffer))))
-             (and (symbolp obj) (fboundp obj) obj)))
+         (let ((stab (syntax-table)))
+           (unwind-protect
+               (save-excursion
+                 (set-syntax-table emacs-lisp-mode-syntax-table)
+                 (or (not (zerop (skip-syntax-backward "_w")))
+                     (eq (char-syntax (following-char)) ?w)
+                     (eq (char-syntax (following-char)) ?_)
+                     (forward-sexp -1))
+                 (skip-chars-forward "'")
+                 (let ((obj (read (current-buffer))))
+                   (and (symbolp obj) (fboundp obj) obj)))
+             (set-syntax-table stab)))
        (error nil))))
 
 (defun describe-function-find-file (function)
@@ -409,6 +520,7 @@ C-w print information on absence of warranty for GNU Emacs."
     (prin1 function)
     (princ ": ")
     (let* ((def (symbol-function function))
+          file-name
           (beg (if (commandp def) "an interactive " "a ")))
       (princ (cond ((or (stringp def)
                        (vectorp def))
@@ -426,19 +538,21 @@ C-w print information on absence of warranty for GNU Emacs."
                   ((eq (car-safe def) 'mocklisp)
                    "a mocklisp function")
                   ((eq (car-safe def) 'autoload)
+                   (setq file-name (nth 1 def))
                    (format "%s autoloaded Lisp %s"
                            (if (commandp def) "an interactive" "an")
                            (if (nth 4 def) "macro" "function")
-;;; Including the file name made this line too long.
-;;;                        (nth 1 def)
                            ))
                   (t "")))
-      (let ((file (describe-function-find-file function)))
-       (if file
-           (progn
-             (princ " in `")
-             (princ file)
-             (princ ".el'"))))
+      (or file-name
+         (setq file-name (describe-function-find-file function)))
+      (if file-name
+         (progn
+           (princ " in `")
+           ;; We used to add .el to the file name,
+           ;; but that's completely wrong when the user used load-file.
+           (princ file-name)
+           (princ "'")))
       (princ ".")
       (terpri)
       (let ((arglist (cond ((byte-code-function-p def)
@@ -455,22 +569,32 @@ C-w print information on absence of warranty for GNU Emacs."
                                       (intern (upcase (symbol-name arg)))))
                                   arglist)))
              (terpri))))
-      (if (documentation function)
-         (progn (terpri)
-                (princ (documentation function)))
-       (princ "not documented"))
-      )
+      (let ((doc (documentation function)))
+       (if doc
+           (progn (terpri)
+                  (princ doc))
+         (princ "not documented"))))
     (print-help-return-message)
-    ;; Return the text we displayed.
-    (save-excursion (set-buffer standard-output) (buffer-string))))
+    (save-excursion
+      (set-buffer standard-output)
+      (help-mode)
+      ;; Return the text we displayed.
+      (buffer-string))))
 
 (defun variable-at-point ()
   (condition-case ()
-      (save-excursion
-       (forward-sexp -1)
-       (skip-chars-forward "'")
-       (let ((obj (read (current-buffer))))
-         (and (symbolp obj) (boundp obj) obj)))
+      (let ((stab (syntax-table)))
+       (unwind-protect
+           (save-excursion
+             (set-syntax-table emacs-lisp-mode-syntax-table)
+             (or (not (zerop (skip-syntax-backward "_w")))
+                 (eq (char-syntax (following-char)) ?w)
+                 (eq (char-syntax (following-char)) ?_)
+                 (forward-sexp -1))
+             (skip-chars-forward "'")
+             (let ((obj (read (current-buffer))))
+               (and (symbolp obj) (boundp obj) obj)))
+         (set-syntax-table stab)))
     (error nil)))
 
 (defun describe-variable (variable)
@@ -488,30 +612,30 @@ Returns the documentation as a string, also."
               v (intern val)))))
   (with-output-to-temp-buffer "*Help*"
     (prin1 variable)
-    (princ "'s value is ")
     (if (not (boundp variable))
-        (princ "void.")
+        (princ " is void")
+      (princ "'s value is ")
       (prin1 (symbol-value variable)))
     (terpri)
-    (let ((locals (buffer-local-variables))
-         is-local)
-      (while locals
-       (if (or (eq variable (car locals))
-               (eq variable (car-safe (car locals))))
-           (setq is-local t locals nil))
-       (setq locals (cdr locals)))
-      (if is-local
-         (princ (format "Local in buffer %s\n" (buffer-name)))))
+    (if (local-variable-p variable)
+       (progn
+         (princ (format "Local in buffer %s; " (buffer-name)))
+         (if (not (default-boundp variable))
+             (princ "globally void")
+           (princ "global value is ")
+           (prin1 (default-value variable)))
+         (terpri)))
     (terpri)
     (princ "Documentation:")
     (terpri)
     (let ((doc (documentation-property variable 'variable-documentation)))
-      (if doc
-         (princ (substitute-command-keys doc))
-       (princ "not documented as a variable.")))
+      (princ (or doc "not documented as a variable.")))
     (print-help-return-message)
-    ;; Return the text we displayed.
-    (save-excursion (set-buffer standard-output) (buffer-string))))
+    (save-excursion
+      (set-buffer standard-output)
+      (help-mode)
+      ;; Return the text we displayed.
+      (buffer-string))))
 
 (defun where-is (definition)
   "Print message listing key sequences that invoke specified command.
@@ -533,18 +657,6 @@ Argument is a command definition, usually a symbol with a function definition."
       (message "%s is not on any key" definition)))
   nil)
 
-(defun command-apropos (string)
-  "Like apropos but lists only symbols that are names of commands
-\(interactively callable functions).  Argument REGEXP is a regular expression
-that is matched against command symbol names.  Returns list of symbols and
-documentation found."
-  (interactive "sCommand apropos (regexp): ")
-  (let ((message
-        (let ((standard-output (get-buffer-create "*Help*")))
-          (print-help-return-message 'identity))))
-    (if (apropos string t 'commandp t)
-       (and message (message message)))))
-
 (defun locate-library (library &optional nosuffix)
   "Show the full path name of Emacs library LIBRARY.
 This command searches the directories in `load-path' like `M-x load-library'