+\f
+;;; Grokking cross-reference information in doc strings and
+;;; hyperlinking it.
+
+;; This may have some scope for extension and the same or something
+;; similar should be done for widget doc strings, which currently use
+;; another mechanism.
+
+(defcustom help-highlight-p t
+ "*If non-nil, `help-make-xrefs' highlight cross-references.
+Under a window system it highlights them with face defined by
+`help-highlight-face'. On a character terminal highlighted
+references look like cross-references in info mode."
+ :group 'help
+ :version "20.3"
+ :type 'boolean)
+
+(defcustom help-highlight-face 'underline
+ "Face used by `help-make-xrefs' to highlight cross-references.
+Must be previously-defined."
+ :group 'help
+ :version "20.3"
+ :type 'face)
+
+(defvar help-back-label "[back]"
+ "Label to use by `help-make-xrefs' for the go-back reference.")
+
+(defvar help-xref-symbol-regexp
+ (concat "\\(\\<\\(\\(variable\\|option\\)\\|"
+ "\\(function\\|command\\)\\|"
+ "\\(symbol\\)\\)\\s-+\\)?"
+ ;; Note starting with word-syntax character:
+ "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")
+ "Regexp matching doc string references to symbols.
+
+The words preceding the quoted symbol can be used in doc strings to
+distinguish references to variables, functions and symbols.")
+
+(defvar help-xref-info-regexp
+ "\\<info\\s-+node\\s-`\\([^']+\\)'"
+ "Regexp matching doc string references to an Info node.")
+
+(defun help-setup-xref (item interactive-p)
+ "Invoked from commands using the \"*Help*\" buffer to install some xref info.
+
+ITEM is a (FUNCTION . ARGS) pair appropriate for recreating the help
+buffer after following a reference. INTERACTIVE-P is non-nil if the
+calling command was invoked interactively. In this case the stack of
+items for help buffer \"back\" buttons is cleared."
+ (if interactive-p
+ (setq help-xref-stack nil))
+ (setq help-xref-stack-item item))
+
+(defun help-make-xrefs (&optional buffer)
+ "Parse and hyperlink documentation cross-references in the given BUFFER.
+
+Find cross-reference information in a buffer and, if
+`help-highlight-p' is non-nil, highlight it with face defined by
+`help-highlight-face'; activate such cross references for selection
+with `help-follow'. Cross-references have the canonical form `...'
+and the type of reference may be disambiguated by the preceding
+word(s) used in `help-xref-symbol-regexp'.
+
+A special reference `back' is made to return back through a stack of
+help buffers. Variable `help-back-label' specifies the text for
+that."
+ (interactive "b")
+ (save-excursion
+ (set-buffer (or buffer (current-buffer)))
+ (goto-char (point-min))
+ ;; Skip the header-type info, though it might be useful to parse
+ ;; it at some stage (e.g. "function in `library'").
+ (forward-paragraph)
+ (let ((old-modified (buffer-modified-p)))
+ (let ((stab (syntax-table))
+ (case-fold-search t)
+ (inhibit-read-only t))
+ (set-syntax-table emacs-lisp-mode-syntax-table)
+ ;; The following should probably be abstracted out.
+ (unwind-protect
+ (progn
+ ;; Quoted symbols
+ (save-excursion
+ (while (re-search-forward help-xref-symbol-regexp nil t)
+ (let* ((data (match-string 6))
+ (sym (intern-soft data)))
+ (if sym
+ (cond
+ ((match-string 3) ; `variable' &c
+ (and (boundp sym) ; `variable' doesn't ensure
+ ; it's actually bound
+ (help-xref-button 6 #'describe-variable sym)))
+ ((match-string 4) ; `function' &c
+ (and (fboundp sym) ; similarly
+ (help-xref-button 6 #'describe-function sym)))
+ ((match-string 5)) ; nothing for symbol
+ ((and (boundp sym) (fboundp sym))
+ ;; We can't intuit whether to use the
+ ;; variable or function doc -- supply both.
+ (help-xref-button 6 #'help-xref-interned sym))
+ ((boundp sym)
+ (help-xref-button 6 #'describe-variable sym))
+ ((fboundp sym)
+ (help-xref-button 6 #'describe-function sym)))))))
+ ;; Info references
+ (save-excursion
+ (while (re-search-forward help-xref-info-regexp nil t)
+ (help-xref-button 1 #'Info-goto-node (list (match-data 1)))))
+ ;; An obvious case of a key substitution:
+ (save-excursion
+ (while (re-search-forward
+ "\\<M-x\\s-+\\(\\sw\\(\\sw\\|\\s_\\)+\\)" nil t)
+ (let ((sym (intern-soft (match-string 1))))
+ (if (fboundp sym)
+ (help-xref-button 1 #'describe-function sym)))))
+ ;; Look for commands in whole keymap substitutions:
+ (save-excursion
+ ;; Make sure to find the first keymap.
+ (goto-char (point-min))
+ ;; Find a header and the column at which the command
+ ;; name will be found.
+ (while (re-search-forward "^key +binding\n\\(-+ +\\)-+\n\n"
+ nil t)
+ (let ((col (- (match-end 1) (match-beginning 1))))
+ (while
+ ;; Ignore single blank lines in table, but not
+ ;; double ones, which should terminate it.
+ (and (looking-at "^\n?[^\n]")
+ (progn
+ (if (and (> (move-to-column col) 0)
+ (looking-at "\\(\\sw\\|\\s_\\)+$"))
+ ;;
+ (let ((sym (intern-soft (match-string 0))))
+ (if (fboundp sym)
+ (help-xref-button
+ 0 #'describe-function sym))))
+ t)
+ (zerop (forward-line))
+ (move-to-column 0)))))))
+ (set-syntax-table stab))
+ ;; Make a back-reference in this buffer if appropriate.
+ (when help-xref-stack
+ (goto-char (point-max))
+ (save-excursion
+ (insert "\n\n" help-back-label))
+ ;; Just to provide the match data:
+ (looking-at (concat "\n\n\\(" (regexp-quote help-back-label) "\\)"))
+ (help-xref-button 1 #'help-xref-go-back (current-buffer))))
+ ;; View mode steals RET from us.
+ (set (make-local-variable 'minor-mode-overriding-map-alist)
+ (list (cons 'view-mode
+ (let ((map (make-sparse-keymap)))
+ (set-keymap-parent map view-mode-map)
+ (define-key map "\r" 'help-follow)
+ map))))
+ (set-buffer-modified-p old-modified))))
+
+(defun help-xref-button (match-number function data)
+ "Make a hyperlink for cross-reference text previously matched.
+
+MATCH-NUMBER is the subexpression of interest in the last matched
+regexp. FUNCTION is a function to invoke when the button is
+activated, applied to DATA. DATA may be a single value or a list.
+See `help-make-xrefs'."
+ (add-text-properties (match-beginning match-number)
+ (match-end match-number)
+ (list 'mouse-face 'highlight
+ 'help-xref (cons function
+ (if (listp data)
+ data
+ (list data)))))
+ (if help-highlight-p
+ (put-text-property (match-beginning match-number)
+ (match-end match-number)
+ 'face help-highlight-face)))
+
+\f
+;; Additional functions for (re-)creating types of help buffers.
+(defun help-xref-interned (symbol)
+ "Follow a hyperlink which appeared to be an arbitrary interned SYMBOL.
+
+Both variable and function documentation are extracted into a single
+help buffer."
+ (let ((fdoc (describe-function symbol)))
+ (describe-variable symbol)
+ ;; We now have a help buffer on the variable. Insert the function
+ ;; text after it.
+ (goto-char (point-max))
+ (insert "\n\n" fdoc))
+ (goto-char (point-min))
+ (help-setup-xref (list #'help-xref-interned symbol) nil))
+
+(defun help-xref-mode (buffer)
+ "Do a `describe-mode' for the specified BUFFER."
+ (save-excursion
+ (set-buffer buffer)
+ (describe-mode)))
+\f
+;;; Navigation/hyperlinking with xrefs
+
+(defun help-follow-mouse (click)
+ "Follow the cross-reference that you click on."
+ (interactive "e")
+ (let* ((start (event-start click))
+ (window (car start))
+ (pos (car (cdr start))))
+ (with-current-buffer (window-buffer window)
+ (help-follow pos))))
+
+(defun help-xref-go-back (buffer)
+ "Go back to the previous help buffer text using info on `help-xref-stack'."
+ (interactive)
+ (let (item position method args)
+ (with-current-buffer buffer
+ (when help-xref-stack
+ (setq help-xref-stack (cdr help-xref-stack)) ; due to help-follow
+ (setq item (car help-xref-stack)
+ position (car item)
+ method (cadr item)
+ args (cddr item))
+ (setq help-xref-stack (cdr help-xref-stack))))
+ (apply method args)
+ (goto-char position)))
+
+(defun help-go-back ()
+ (interactive)
+ (help-follow (1- (point-max))))
+
+(defun help-follow (&optional pos)
+ "Follow cross-reference at POS, defaulting to point.
+
+For the cross-reference format, see `help-make-xrefs'."
+ (interactive "d")
+ (let* ((help-data (or (and (not (= pos (point-max)))
+ (get-text-property pos 'help-xref))
+ (and (not (= pos (point-min)))
+ (get-text-property (1- pos) 'help-xref))))
+ (method (car help-data))
+ (args (cdr help-data)))
+ (setq help-xref-stack (cons (cons (point) help-xref-stack-item)
+ help-xref-stack))
+ (setq help-xref-stack-item nil)
+ (when help-data
+ ;; There is a reference at point. Follow it.
+ (apply method args))))
+
+;; For tabbing through buffer.
+(defun help-next-ref ()
+ "Find the next help cross-reference in the buffer."
+ (interactive)
+ (let (pos)
+ (while (not pos)
+ (if (get-text-property (point) 'help-xref) ; move off reference
+ (goto-char (or (next-single-property-change (point) 'help-xref)
+ (point))))
+ (cond ((setq pos (next-single-property-change (point) 'help-xref))
+ (if pos (goto-char pos)))
+ ((bobp)
+ (message "No cross references in the buffer.")
+ (setq pos t))
+ (t ; be circular
+ (goto-char (point-min)))))))
+
+(defun help-previous-ref ()
+ "Find the previous help cross-reference in the buffer."
+ (interactive)
+ (let (pos)
+ (while (not pos)
+ (if (get-text-property (point) 'help-xref) ; move off reference
+ (goto-char (or (previous-single-property-change (point) 'help-xref)
+ (point))))
+ (cond ((setq pos (previous-single-property-change (point) 'help-xref))
+ (if pos (goto-char pos)))
+ ((bobp)
+ (message "No cross references in the buffer.")
+ (setq pos t))
+ (t ; be circular
+ (goto-char (point-max)))))))
+