4
|
1 ;;; man-xref.el --- cross reference selection functions for man mode
|
|
2
|
|
3 ;; Author: Mark Hood <hood@eng.sun.com>
|
|
4 ;; @(#)man-xref.el 1.15
|
|
5
|
|
6 ;; This file is part of XEmacs.
|
|
7
|
|
8 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
9 ;; under the terms of the GNU General Public License as published by
|
|
10 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
11 ;; any later version.
|
|
12
|
|
13 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
16 ;; General Public License for more details.
|
|
17
|
|
18 ;; You should have received a copy of the GNU General Public License
|
|
19 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
20 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
21 ;; 02111-1307, USA.
|
|
22
|
|
23 ;;; Synched up with: FSF 19.35.
|
|
24
|
|
25 ;;; Commentary:
|
|
26
|
|
27 ;; This package is an add-on to the man.el that comes with Emacs
|
|
28 ;; 19.34. It renders manpage cross references in bold, sets them up
|
|
29 ;; for mouse highlighting, and allows selection via keystrokes or
|
|
30 ;; mouse. All strings matching Man-reference-regexp in the text of
|
|
31 ;; the man page are set up, in addition to the ones in the See Also
|
|
32 ;; section.
|
|
33
|
|
34 ;; To use this package, put something like the following in your Emacs
|
|
35 ;; initialization file. This example causes tab and M-tab to go to
|
|
36 ;; the next and previous manual cross references, causes carriage
|
|
37 ;; return to display a man page for the reference under point, and
|
|
38 ;; allows mouse button 2 to invoke a man page display.
|
|
39
|
|
40 ;; (add-hook 'Man-mode-hook
|
|
41 ;; '(lambda ()
|
|
42 ;; (Man-mouseify-xrefs)
|
|
43 ;; (define-key Man-mode-map "\r" 'Man-do-manual-reference)
|
|
44 ;; (define-key Man-mode-map "\t" 'Man-next-manual-reference)
|
|
45 ;; (define-key Man-mode-map "\e\t" 'Man-prev-manual-reference)
|
|
46 ;; (define-key Man-mode-map [mouse-2] 'Man-mouse-manual-reference)
|
|
47 ;; ))
|
|
48 ;;
|
|
49 ;; (autoload 'Man-mouseify-xrefs "~/emacs/man-xref")
|
|
50
|
|
51 (eval-when-compile (require 'cl))
|
|
52
|
|
53 (defvar Man-word-syntax "w_()" "Syntax for words in a man buffer.")
|
|
54
|
|
55 (defun Man-current-word ()
|
|
56 "Return word under point, using `Man-word-syntax' for word syntax."
|
|
57 (save-excursion
|
|
58 (let ((s (+ (point) (skip-syntax-backward Man-word-syntax))))
|
|
59 (skip-syntax-forward Man-word-syntax)
|
|
60 (buffer-substring s (point)))))
|
|
61
|
|
62 (defun Man-prev-word-hyphen-p ()
|
|
63 "Return nil if previous word is not hyphenated.
|
|
64 Non-nil value is the buffer position of the beginning of the hyphenated word."
|
|
65 (save-excursion
|
|
66 (skip-syntax-backward Man-word-syntax)
|
|
67 (skip-chars-backward " \t")
|
|
68 (cond ((and (> (point) (1+ (point-min)))
|
|
69 (string-equal "-\n" (buffer-substring (- (point) 2) (point))))
|
|
70 (backward-char)
|
|
71 (skip-syntax-backward Man-word-syntax)
|
|
72 (point)))))
|
|
73
|
|
74 (defun Man-next-manual-reference ()
|
|
75 "Move point to the beginning of the next manual reference."
|
|
76 (interactive)
|
|
77 (let ((current (point))
|
|
78 (end (re-search-forward (concat "[ \t]" Man-reference-regexp) nil t))
|
|
79 (start (or (Man-prev-word-hyphen-p) (1+ (match-beginning 0)))))
|
|
80 (cond ((eq end nil))
|
|
81 ((> start current)
|
|
82 (goto-char start))
|
|
83 ;; current is in the pre-hyphen portion of a hyphenated reference
|
|
84 ((re-search-forward Man-reference-regexp nil t)
|
|
85 (goto-char (or (Man-prev-word-hyphen-p) (match-beginning 0))))
|
|
86 ((goto-char current)))))
|
|
87
|
|
88 (defun Man-prev-manual-reference ()
|
|
89 "Move point to the beginning of the previous manual reference."
|
|
90 (interactive)
|
|
91 (if (re-search-backward (concat "[ \t]" Man-reference-regexp) nil t)
|
|
92 (goto-char (or (Man-prev-word-hyphen-p) (1+ (match-beginning 0))))))
|
|
93
|
|
94 (defun Man-mouseify-xrefs ()
|
|
95 "Render man cross references in bold font and set up mouse highlighting.
|
|
96 Add these cross references to `Man-refpages-alist'."
|
|
97 (let (start end xref hyphen alist)
|
|
98 (goto-char (point-min))
|
|
99 (forward-line 1)
|
|
100 (while (re-search-forward Man-reference-regexp nil t)
|
|
101 (setq start (match-beginning 0))
|
|
102 (setq end (match-end 0))
|
|
103 (setq xref (buffer-substring start end))
|
|
104 (cond ((setq hyphen (Man-prev-word-hyphen-p))
|
|
105 (setq start hyphen)
|
|
106 (goto-char hyphen)
|
|
107 (setq xref (concat (substring (Man-current-word) 0 -1) xref))
|
|
108 (goto-char end)))
|
|
109 (setq Man-refpages-alist (cons (list xref) Man-refpages-alist))
|
|
110 (Man-boldify-mouse-region start end))
|
|
111 (setq Man-refpages-alist
|
|
112 (sort Man-refpages-alist
|
|
113 (function (lambda (a b) (string< (car a) (car b))))))
|
|
114 ;; delete duplicate entries in the alist
|
|
115 (setq alist Man-refpages-alist)
|
|
116 (while alist
|
|
117 (cond ((string= (car (car alist)) (car (car (cdr alist))))
|
|
118 (setcdr alist (cdr (cdr alist))))
|
|
119 ((setq alist (cdr alist)))))
|
|
120 (goto-char (point-min))
|
|
121 (forward-line 1)))
|
|
122
|
|
123 (defun Man-mouse-manual-reference (mouse)
|
|
124 "Move point to mouse position and run `Man-getpage-in-background' there."
|
|
125 (interactive "e")
|
|
126 (select-window (car (car (cdr mouse))))
|
|
127 (goto-char (car (cdr (car (cdr mouse)))))
|
|
128 (Man-do-manual-reference))
|
|
129
|
|
130 (defun Man-do-manual-reference ()
|
|
131 "Run `Man-getpage-in-background' on cross reference under point.
|
|
132 Word under point is checked for a match with `Man-reference-regexp'.
|
|
133 If point is not over a word, try to use previous word for a match."
|
|
134 (interactive)
|
|
135 (save-excursion
|
|
136 (let ((xref (Man-current-word)) (hyphen (Man-prev-word-hyphen-p)))
|
|
137 (if (and (zerop (length xref))
|
|
138 (setq xref " ")
|
|
139 (skip-syntax-backward " ")
|
|
140 (not (eq (point) (point-min))))
|
|
141 (Man-do-manual-reference)
|
|
142 (cond ((string-equal "-" (substring xref -1))
|
|
143 (skip-syntax-forward Man-word-syntax)
|
|
144 (skip-syntax-forward " ")
|
|
145 (setq xref (concat (substring xref 0 -1) (Man-current-word))))
|
|
146 (hyphen
|
|
147 (goto-char hyphen)
|
|
148 (setq xref (concat (substring (Man-current-word) 0 -1) xref))))
|
|
149 (if (string-match Man-reference-regexp xref)
|
|
150 (Man-getpage-in-background
|
|
151 (Man-translate-references
|
|
152 (substring xref (match-beginning 0) (match-end 0))))
|
|
153 (message "No cross reference found under point."))))))
|
|
154
|
|
155 (eval-and-compile
|
|
156 (when (string-match "XEmacs\\|Lucid" emacs-version)
|
|
157 (fset 'make-overlay 'make-extent)
|
|
158 (fset 'overlay-put 'set-extent-property)))
|
|
159
|
|
160 (defun Man-boldify-mouse-region (beg end)
|
|
161 "Render region text in bold with mouse highlighting."
|
|
162 (let ((overlay (make-overlay beg end)))
|
|
163 (overlay-put overlay 'face 'bold)
|
|
164 (overlay-put overlay 'mouse-face 'highlight)
|
|
165 (overlay-put overlay 'hilit t)))
|
|
166
|