0
|
1 ;;!emacs
|
|
2 ;;
|
|
3 ;; FILE: hbut.el
|
|
4 ;; SUMMARY: Hyperbole button constructs.
|
|
5 ;; USAGE: GNU Emacs Lisp Library
|
|
6 ;; KEYWORDS: extensions, hypermedia
|
|
7 ;;
|
|
8 ;; AUTHOR: Bob Weiner
|
24
|
9 ;; ORG: InfoDock Associates
|
0
|
10 ;;
|
|
11 ;; ORIG-DATE: 18-Sep-91 at 02:57:09
|
24
|
12 ;; LAST-MOD: 17-Feb-97 at 15:31:03 by Bob Weiner
|
0
|
13 ;;
|
|
14 ;; This file is part of Hyperbole.
|
|
15 ;; Available for use and distribution under the same terms as GNU Emacs.
|
|
16 ;;
|
24
|
17 ;; Copyright (C) 1991-1996, Free Software Foundation, Inc.
|
0
|
18 ;; Developed with support from Motorola Inc.
|
|
19 ;;
|
|
20 ;; DESCRIPTION:
|
|
21 ;; DESCRIP-END.
|
|
22
|
|
23 ;;; ************************************************************************
|
|
24 ;;; Other required Elisp libraries
|
|
25 ;;; ************************************************************************
|
|
26
|
|
27 (require 'hmoccur)
|
|
28 (require 'hbmap)
|
|
29 (require 'htz)
|
|
30 (require 'hbdata)
|
|
31 (require 'hact)
|
|
32
|
|
33 ;;; ************************************************************************
|
|
34 ;;; Public definitions
|
|
35 ;;; ************************************************************************
|
|
36
|
|
37 ;;; ========================================================================
|
|
38 ;;; ebut class - Explicit Hyperbole buttons
|
|
39 ;;; ========================================================================
|
|
40
|
|
41 (defvar ebut:hattr-save t
|
|
42 "*Non-nil value saves button data when button source is saved.
|
|
43 Nil disables saving.")
|
|
44
|
|
45 (defconst ebut:max-len 100
|
|
46 "Maximum length of a hyper-button label.")
|
|
47
|
|
48
|
24
|
49 (defun ebut:act (label)
|
|
50 "Activates Hyperbole explicit button with LABEL from the current buffer."
|
|
51 (interactive (list (hargs:read-match "Activate explicit button labeled: "
|
|
52 (ebut:alist)
|
|
53 nil t nil 'ebut)))
|
|
54 (let* ((lbl-key (hbut:label-to-key label))
|
|
55 (but (ebut:get lbl-key)))
|
|
56 (if but
|
|
57 (hbut:act but)
|
|
58 (error "(ebut:act): No explicit button labeled: %s" label))))
|
|
59
|
0
|
60 (defun ebut:alist (&optional file)
|
|
61 "Returns alist with each element a list containing a button label.
|
|
62 For use as a completion table. Gets labels from optional FILE or current
|
|
63 buffer."
|
|
64 (mapcar 'list (ebut:list file)))
|
|
65
|
|
66 (defun ebut:at-p (&optional start-delim end-delim)
|
|
67 "Returns explicit Hyperbole button at point or nil.
|
|
68 Assumes point is within first line of button label, if at all.
|
|
69 Optional START-DELIM and END-DELIM are strings that override default
|
|
70 button delimiters."
|
|
71 (let ((key (ebut:label-p nil start-delim end-delim)))
|
|
72 (and key (ebut:get key))))
|
|
73
|
|
74 (defun ebut:create (&optional but-sym)
|
|
75 "Creates Hyperbole explicit button based on optional BUT-SYM.
|
|
76 Default is 'hbut:current'.
|
|
77 Button should hold the following attributes (see 'hattr:set'):
|
|
78 lbl-key (normalized button label string),
|
|
79 loc (filename or buffer where button is located),
|
|
80 dir (directory name where button is located),
|
|
81 actype (action type that provides a default action for the button),
|
|
82 action (optional action that overrides the default),
|
|
83 args (list of arguments for action, if action takes a single
|
|
84 argument of the button lbl-key, args may be nil).
|
|
85
|
|
86 If successful returns any instance number to append to button label
|
|
87 except when instance number would be 1, then returns t. On failure,
|
|
88 returns nil.
|
|
89
|
|
90 If successful, leaves point in button data buffer, so caller should use
|
|
91 'save-excursion'. Does not save button data buffer."
|
|
92 (let ((lbl-instance (hbdata:write nil but-sym)))
|
|
93 (run-hooks 'ebut:create-hook)
|
|
94 lbl-instance))
|
|
95
|
|
96 (defun ebut:delete (&optional but-sym)
|
|
97 "Deletes Hyperbole explicit button based on optional BUT-SYM.
|
|
98 Default is 'hbut:current'.
|
|
99 Returns entry deleted (a list of attribute values) or nil."
|
|
100 (if (null but-sym) (setq but-sym 'hbut:current))
|
|
101 (if (ebut:is-p but-sym)
|
|
102 (let* ((but-key (hattr:get but-sym 'lbl-key))
|
|
103 (loc (hattr:get but-sym 'loc))
|
|
104 (entry (hbdata:delete-entry but-key loc)))
|
|
105 (run-hooks 'ebut:delete-hook)
|
|
106 entry)))
|
|
107
|
|
108 (defun ebut:get (&optional lbl-key buffer key-src)
|
|
109 "Returns explicit Hyperbole button symbol given by LBL-KEY and BUFFER.
|
|
110 KEY-SRC is given when retrieving global buttons and is full source pathname.
|
|
111 Retrieves button data, converts into a button object and returns a symbol
|
|
112 which references the button.
|
|
113
|
|
114 All arguments are optional. When none are given, returns symbol for
|
|
115 button that point is within or nil. BUFFER defaults to the current
|
|
116 buffer."
|
|
117 (hattr:clear 'hbut:current)
|
|
118 (save-excursion
|
|
119 (let ((key-file) (key-dir) (but-data) (actype))
|
|
120 (or lbl-key (setq lbl-key (ebut:label-p)))
|
|
121 (if buffer
|
|
122 (if (bufferp buffer) (set-buffer buffer)
|
|
123 (error "(ebut:get): Invalid buffer argument: %s" buffer)))
|
|
124 (if key-src
|
|
125 nil
|
|
126 (if (equal lbl-key (ebut:label-p))
|
|
127 nil
|
|
128 (goto-char (point-min))
|
|
129 (ebut:next-occurrence lbl-key))
|
|
130 (if (setq key-src (ebut:key-src 'full))
|
|
131 ;; 'ebut:key-src' sets current buffer to key-src buffer.
|
|
132 (setq buffer (current-buffer)))
|
|
133 )
|
|
134 (if (and (stringp lbl-key) key-src)
|
|
135 (progn
|
|
136 (if (stringp key-src)
|
|
137 (setq key-dir (file-name-directory key-src)
|
|
138 key-file (file-name-nondirectory key-src)))
|
|
139 (setq but-data (and key-src
|
|
140 (hbdata:get-entry lbl-key (or key-file key-src)
|
|
141 key-dir)))
|
|
142 (if (null but-data)
|
|
143 nil
|
|
144 (hattr:set 'hbut:current 'lbl-key lbl-key)
|
|
145 (hattr:set 'hbut:current 'loc key-src)
|
|
146 (hattr:set 'hbut:current 'categ 'explicit)
|
|
147 (hattr:set 'hbut:current 'action nil)
|
|
148 (hattr:set 'hbut:current 'actype
|
|
149 (intern (setq actype (hbdata:actype but-data))))
|
|
150 ;; Hyperbole V1 referent compatibility
|
|
151 (if (= (length actype) 2)
|
|
152 (hattr:set 'hbut:current 'referent
|
|
153 (hbdata:referent but-data)))
|
|
154 (hattr:set 'hbut:current 'args (hbdata:args but-data))
|
|
155 (hattr:set 'hbut:current 'creator (hbdata:creator but-data))
|
|
156 (hattr:set 'hbut:current
|
|
157 'create-time (hbdata:create-time but-data))
|
|
158 (hattr:set 'hbut:current
|
|
159 'modifier (hbdata:modifier but-data))
|
|
160 (hattr:set 'hbut:current
|
|
161 'mod-time (hbdata:mod-time but-data))
|
|
162 'hbut:current)
|
|
163 )))))
|
|
164
|
|
165 (defun ebut:is-p (object)
|
|
166 "Returns non-nil if OBJECT denotes an explicit Hyperbole button."
|
|
167 (and (symbolp object)
|
|
168 (eq (hattr:get object 'categ) 'explicit)))
|
|
169
|
|
170 (defun ebut:key-of-label-p (key label)
|
|
171 "Returns t iff KEY matches to LABEL in a case insensitive manner."
|
|
172 (and (stringp key) (stringp label)
|
|
173 (equal key (downcase (ebut:label-to-key label)))))
|
|
174
|
|
175 (defun ebut:key-src (&optional full)
|
|
176 "Return key source (usually unqualified) for current Hyperbole button.
|
|
177 Also sets current buffer to key source.
|
|
178 With optional FULL when source is a pathname, the full pathname is returned."
|
|
179 (let ((src (cond ((hmail:mode-is-p) (current-buffer))
|
|
180 ((ebut:key-src-fmt))
|
|
181 ((save-excursion
|
|
182 (save-restriction
|
|
183 (widen)
|
|
184 (if (and (search-backward hbut:source-prefix nil t)
|
|
185 (or (memq (preceding-char) '(?\n ?\^M))
|
|
186 (= (point) (point-min))))
|
|
187 (hbut:source full)))))
|
|
188 (buffer-file-name
|
|
189 (if full buffer-file-name
|
|
190 (file-name-nondirectory buffer-file-name)))
|
|
191 (t (current-buffer))
|
|
192 )))
|
|
193 (cond ((null src) nil)
|
|
194 ((bufferp src)
|
|
195 (set-buffer src)
|
|
196 src)
|
|
197 ((file-readable-p src)
|
|
198 (set-buffer (find-file-noselect src))
|
|
199 src)
|
|
200 ((file-readable-p (setq src (hpath:symlink-referent src)))
|
|
201 (set-buffer (find-file-noselect src))
|
|
202 src))))
|
|
203
|
|
204 (defun ebut:key-src-fmt ()
|
|
205 "Returns unformatted filename associated with formatted current buffer.
|
|
206 This is used to obtain the source of explicit buttons for buffers that
|
|
207 represent the output of particular document formatters."
|
|
208 (cond ((or (eq major-mode 'Info-mode)
|
|
209 (string-match "\\.info\\(-[0-9]+\\)?$" (buffer-name)))
|
|
210 (let ((src (and buffer-file-name
|
|
211 (substring
|
|
212 buffer-file-name
|
|
213 0 (string-match "\\.[^.]+$" buffer-file-name)))))
|
|
214 (cond ((file-exists-p (concat src ".texi"))
|
|
215 (concat src ".texi"))
|
|
216 ((file-exists-p (concat src ".texinfo"))
|
|
217 (concat src ".texinfo"))
|
|
218 ((current-buffer)))))
|
|
219 ))
|
|
220
|
|
221 (defun ebut:key-to-label (lbl-key)
|
|
222 "Unnormalizes LBL-KEY and returns a label string approximating actual label."
|
|
223 (if lbl-key
|
|
224 (let* ((pos 0) (len (length lbl-key)) (lbl) c)
|
|
225 (while (< pos len)
|
|
226 (setq c (aref lbl-key pos)
|
|
227 lbl (concat lbl
|
|
228 (if (= c ?_)
|
|
229 (if (or (= (1+ pos) len)
|
|
230 (/= (aref lbl-key (1+ pos)) ?_))
|
|
231 " "
|
|
232 (setq pos (1+ pos))
|
|
233 "_")
|
|
234 (char-to-string c)))
|
|
235 pos (1+ pos)))
|
|
236 lbl)))
|
|
237
|
|
238 (defun ebut:label-p (&optional as-label start-delim end-delim pos-flag)
|
|
239 "Returns key for Hyperbole button label that point is within.
|
|
240 Returns nil if not within a label.
|
|
241 Assumes point is within first line of button label, if at all.
|
|
242 If optional AS-LABEL is non-nil, label is returned rather than the key
|
|
243 derived from the label. Optional START-DELIM and END-DELIM are strings
|
|
244 that override default button delimiters. With optional POS-FLAG non-nil,
|
|
245 returns list of label-or-key, but-start-position, but-end-position.
|
|
246 Positions include delimiters."
|
|
247 (let ((opoint (point))
|
|
248 (npoint (1+ (point)))
|
|
249 (quoted "\\(^\\|[^\\{]\\)")
|
|
250 (start)
|
|
251 lbl-key end but-start but-end)
|
|
252 (or start-delim (setq start-delim ebut:start))
|
|
253 (or end-delim (setq end-delim ebut:end))
|
|
254 (save-excursion
|
|
255 (beginning-of-line)
|
|
256 (while (and (progn
|
|
257 (while (re-search-forward
|
|
258 (concat quoted (regexp-quote start-delim))
|
|
259 npoint t)
|
|
260 (setq start t))
|
|
261 start)
|
|
262 (re-search-forward (concat "[^\\{]" (regexp-quote end-delim))
|
|
263 npoint t))
|
|
264 (setq start nil))
|
|
265 (if start
|
|
266 (progn
|
|
267 (setq start (point)
|
|
268 but-start (match-end 1))
|
|
269 (if (= ?\( (char-syntax (preceding-char)))
|
|
270 (condition-case ()
|
|
271 (progn
|
|
272 (forward-char -1)
|
|
273 (forward-list)
|
|
274 (forward-char -2))
|
|
275 (error (goto-char (1- opoint))))
|
|
276 (goto-char (1- opoint)))
|
|
277 (and (< (point) (+ start ebut:max-len))
|
|
278 (re-search-forward (concat quoted (regexp-quote end-delim))
|
|
279 (+ start ebut:max-len) t)
|
|
280 (setq but-end (point)
|
|
281 end (- (point) (length end-delim))
|
|
282 lbl-key (ebut:label-to-key (buffer-substring start end)))
|
|
283 (cond (pos-flag
|
|
284 (if as-label
|
|
285 (list (ebut:key-to-label lbl-key) but-start but-end)
|
|
286 (list lbl-key but-start but-end)))
|
|
287 (t (if as-label (ebut:key-to-label lbl-key) lbl-key)))))))))
|
|
288
|
|
289 (defun ebut:label-regexp (lbl-key &optional no-delim)
|
|
290 "Unnormalizes LBL-KEY. Returns regular expr matching delimited but label.
|
|
291 Optional NO-DELIM leaves off delimiters and leading and trailing space."
|
|
292 (if lbl-key
|
|
293 (let* ((pos 0)
|
|
294 (len (length lbl-key))
|
|
295 (c)
|
|
296 (sep0 "[ \t\n\^M]*")
|
|
297 (sep "[ \t\n\^M]+")
|
|
298 (regexp (if no-delim "" (concat (regexp-quote ebut:start) sep0)))
|
|
299 (case-fold-search))
|
|
300 (while (< pos len)
|
|
301 (setq c (aref lbl-key pos)
|
|
302 regexp (concat regexp
|
|
303 (if (= c ?_)
|
|
304 (if (or (= (1+ pos) len)
|
|
305 (/= (aref lbl-key (1+ pos)) ?_))
|
|
306 sep
|
|
307 (setq pos (1+ pos))
|
|
308 "_")
|
|
309 (regexp-quote (char-to-string c))))
|
|
310 pos (1+ pos)))
|
|
311 (if no-delim regexp
|
|
312 (setq regexp (concat regexp sep0 (regexp-quote ebut:end)))))))
|
|
313
|
|
314 (defun ebut:label-to-key (label)
|
|
315 "Normalizes LABEL for use as a Hyperbole button key and returns key.
|
|
316 Eliminates any fill prefix in the middle of the label, replaces '_' with
|
|
317 '__', removes leading and trailing whitespace and replaces each other
|
|
318 whitespace sequence with '_'."
|
|
319 (if (null label)
|
|
320 nil
|
|
321 (setq label (hbut:fill-prefix-remove label)
|
|
322 ;; Remove leading and trailing space.
|
|
323 label (hypb:replace-match-string "\\`[ \t\n\^M]+\\|[ \t\n\^M]+\\'"
|
|
324 label "" t)
|
|
325 label (hypb:replace-match-string "_" label "__" t))
|
|
326 (hypb:replace-match-string "[ \t\n\^M]+" label "_" t)))
|
|
327
|
|
328 (defun ebut:list (&optional file loc-p)
|
|
329 "Returns list of button labels from given FILE or current buffer.
|
|
330 Removes duplicate labels if optional LOC-P is omitted. With LOC-P, returns
|
|
331 list of elements (label start end) where start and end are the buffer
|
|
332 positions at which the starting button delimiter begins and ends."
|
|
333 (interactive)
|
|
334 (setq file (if file (and (file-exists-p file) (find-file-noselect file))
|
|
335 (current-buffer)))
|
|
336 (if file
|
|
337 (progn
|
|
338 (set-buffer file)
|
|
339 (let ((buts (ebut:map (if loc-p
|
|
340 (function
|
|
341 (lambda (lbl start end)
|
|
342 ;; Normalize label spacing
|
|
343 (list (ebut:key-to-label
|
|
344 (ebut:label-to-key lbl))
|
|
345 start end)))
|
|
346 (function
|
|
347 (lambda (lbl start end)
|
|
348 ;; Normalize label spacing
|
|
349 (ebut:key-to-label
|
|
350 (ebut:label-to-key lbl))))))))
|
24
|
351 (if loc-p buts (if buts (nreverse (set:create buts))))))))
|
0
|
352
|
|
353 (fset 'map-ebut 'ebut:map)
|
|
354 (defun ebut:map (but-func &optional start-delim end-delim
|
|
355 regexp-match include-delims)
|
|
356 "Applies BUT-FUNC to buttons delimited by optional START-DELIM and END-DELIM.
|
|
357 If REGEXP-MATCH is non-nil, only buttons which match this argument are
|
|
358 considered.
|
|
359 Maps over portion of buffer visible under any current restriction.
|
|
360 BUT-FUNC must take precisely three arguments: the button label, the
|
|
361 start position of the delimited button label and its end position (positions
|
|
362 include delimiters when INCLUDE-DELIMS is non-nil).
|
|
363 If END-DELIM is a symbol, e.g. t, then START-DELIM is taken as a regular
|
|
364 expression which matches an entire button string."
|
|
365 (or start-delim (setq start-delim ebut:start))
|
|
366 (or end-delim (setq end-delim ebut:end))
|
|
367 (let* ((regexp (symbolp end-delim))
|
|
368 (end-sym (or regexp (substring end-delim -1)))
|
|
369 (rtn)
|
|
370 (quoted)
|
|
371 start end but lbl)
|
|
372 (save-excursion
|
|
373 (goto-char (point-min))
|
|
374 (setq include-delims (if include-delims 0 1))
|
|
375 (while (re-search-forward
|
|
376 (if regexp start-delim
|
|
377 (concat (regexp-quote start-delim)
|
|
378 "\\([^" end-sym "\"][^" end-sym "]*\\)"
|
|
379 (regexp-quote end-delim)))
|
|
380 nil t)
|
|
381 (setq start (match-beginning include-delims)
|
|
382 end (match-end include-delims)
|
|
383 but (buffer-substring (match-beginning 0) (match-end 0))
|
|
384 lbl (buffer-substring (match-beginning 1) (match-end 1)))
|
|
385 (save-excursion
|
|
386 (goto-char start)
|
|
387 (if (or (= (preceding-char) ?\\) (= (preceding-char) ?\{))
|
|
388 ;; Ignore matches with quoted delimiters.
|
|
389 (setq quoted t)))
|
|
390 (cond (quoted (setq quoted nil))
|
|
391 ((or (not regexp-match)
|
|
392 (string-match regexp-match but))
|
|
393 (setq rtn (cons (funcall but-func lbl start end) rtn))))))
|
|
394 (nreverse rtn)))
|
|
395
|
|
396 (defun ebut:modify (&optional lbl-key but-sym)
|
|
397 "Modifies existing Hyperbole button from optional LBL-KEY and BUT-SYM.
|
|
398 Defaults are the key for any button label at point and 'hbut:current'.
|
|
399 If successful, returns button's instance number except when instance
|
|
400 number is 1, then returns t. On failure, as when button does not exist,
|
|
401 returns nil.
|
|
402
|
|
403 If successful, leaves point in button data buffer, so caller should use
|
|
404 'save-excursion'. Does not save button data buffer."
|
|
405 (save-excursion
|
|
406 (let ((lbl-instance (hbdata:write lbl-key but-sym)))
|
|
407 (run-hooks 'ebut:modify-hook)
|
|
408 lbl-instance)))
|
|
409
|
|
410 (defun ebut:next-occurrence (lbl-key &optional buffer)
|
|
411 "Moves point to next occurrence of button with LBL-KEY in optional BUFFER.
|
|
412 BUFFER defaults to current buffer. It may be a buffer name.
|
|
413 Returns non-nil iff occurrence is found.
|
|
414
|
|
415 Remember to use (goto-char (point-min)) before calling this in order to
|
|
416 move to the first occurrence of the button."
|
|
417 (if buffer
|
|
418 (if (not (or (bufferp buffer)
|
|
419 (and (stringp buffer) (get-buffer buffer))))
|
|
420 (error "(ebut:next-occurrence): Invalid buffer arg: %s" buffer)
|
|
421 (switch-to-buffer buffer)))
|
|
422 (if (re-search-forward (ebut:label-regexp lbl-key) nil t)
|
|
423 (goto-char (+ (match-beginning 0) (length ebut:start)))))
|
|
424
|
|
425 (defun ebut:operate (curr-label new-label)
|
|
426 "Operates on a new or existing Hyperbole button given by CURR-LABEL.
|
|
427 When NEW-LABEL is non-nil, this is substituted for CURR-LABEL and the
|
|
428 associated button is modified. Otherwise, a new button is created.
|
|
429 Returns instance string appended to label to form unique label, nil if
|
|
430 label is already unique. Signals an error when no such button is found
|
|
431 in the current buffer."
|
|
432 (let* ((lbl-key (ebut:label-to-key curr-label))
|
|
433 (lbl-regexp (ebut:label-regexp lbl-key))
|
|
434 (modify new-label)
|
|
435 (instance-flag))
|
|
436 (or new-label (setq new-label curr-label))
|
|
437 (hattr:set 'hbut:current 'lbl-key (ebut:label-to-key new-label))
|
|
438 (save-excursion
|
|
439 (if (setq instance-flag
|
|
440 (if modify (ebut:modify lbl-key) (ebut:create)))
|
|
441 (if (hmail:editor-p) (hmail:msg-narrow))))
|
|
442 (if instance-flag
|
|
443 (progn
|
|
444 ;; Rename all occurrences of button - those with same label.
|
|
445 (if modify
|
|
446 (let* ((but-key-and-pos (ebut:label-p nil nil nil 'pos))
|
|
447 (at-but (equal (car but-key-and-pos)
|
|
448 (ebut:label-to-key new-label))))
|
|
449 (if at-but
|
|
450 (ebut:delimit (nth 1 but-key-and-pos)
|
|
451 (nth 2 but-key-and-pos)
|
|
452 instance-flag))
|
|
453 (cond ((ebut:map
|
|
454 (function
|
|
455 (lambda (lbl start end)
|
|
456 (delete-region start end)
|
|
457 (ebut:delimit
|
|
458 (point)
|
|
459 (progn (insert new-label) (point))
|
|
460 instance-flag)))
|
|
461 nil nil lbl-regexp 'include-delims))
|
|
462 (at-but)
|
|
463 ((hypb:error "(ebut:operate): No button matching: %s" curr-label))))
|
|
464 ;; Add a new button.
|
|
465 (let (start end buf-lbl)
|
|
466 (cond ((and (marker-position (hypb:mark-marker t))
|
|
467 (setq start (region-beginning)
|
|
468 end (region-end)
|
|
469 buf-lbl (buffer-substring start end))
|
|
470 (equal buf-lbl curr-label))
|
|
471 nil)
|
|
472 ((looking-at (regexp-quote curr-label))
|
|
473 (setq start (point)
|
|
474 end (match-end 0)))
|
|
475 (t (setq start (point))
|
|
476 (insert curr-label)
|
|
477 (setq end (point))))
|
|
478 (ebut:delimit start end instance-flag))
|
|
479 )
|
|
480 ;; Position point
|
|
481 (let ((new-key (ebut:label-to-key new-label)))
|
|
482 (cond ((equal (ebut:label-p) new-key)
|
|
483 (forward-char 1) (search-backward ebut:start nil t)
|
|
484 (goto-char (match-end 0)))
|
|
485 ((let ((regexp (ebut:label-regexp new-key)))
|
|
486 (or (re-search-forward regexp nil t)
|
|
487 (re-search-backward regexp nil t)))
|
|
488 (goto-char (+ (match-beginning 0) (length ebut:start))))))
|
|
489 ;; instance-flag might be 't which we don't want to return.
|
|
490 (if (stringp instance-flag) instance-flag))
|
|
491 (hypb:error
|
|
492 "(ebut:operate): Operation failed. Check button attribute permissions: %s"
|
|
493 hattr:filename))))
|
|
494
|
|
495 (defun ebut:search (string out-buf &optional match-part)
|
|
496 "Writes explicit button lines matching STRING to OUT-BUF.
|
|
497 Uses Hyperbole space into which user has written buttons for the search.
|
|
498 By default, only matches for whole button labels are found, optional MATCH-PART
|
|
499 enables partial matches."
|
|
500 (let* ((buffers (mapcar (function
|
|
501 (lambda (dir)
|
|
502 (expand-file-name hattr:filename dir)))
|
|
503 (hbmap:dir-list)))
|
|
504 (total 0)
|
|
505 (firstmatch))
|
|
506 (save-excursion
|
|
507 (set-buffer out-buf)
|
|
508 (setq buffer-read-only nil)
|
|
509 (widen)
|
|
510 (erase-buffer)
|
|
511 (let (currbuf currfile kill-buf src-matches dir)
|
|
512 (while buffers
|
|
513 (setq currbuf (car buffers)
|
|
514 currfile (if (stringp currbuf) currbuf)
|
|
515 kill-buf (and currfile (not (get-file-buffer currfile)))
|
|
516 buffers (cdr buffers))
|
|
517 (if currfile
|
|
518 (setq currbuf (and (file-readable-p currfile)
|
|
519 (find-file-noselect currfile))
|
|
520 dir (file-name-directory currfile))
|
|
521 (setq currfile (buffer-file-name currbuf)))
|
|
522 (and currfile currbuf
|
|
523 (unwind-protect
|
|
524 (setq src-matches
|
|
525 (hbdata:search currbuf string match-part))
|
|
526 (if kill-buf (kill-buffer currbuf))))
|
|
527 (if src-matches
|
|
528 (let (elt matches)
|
|
529 (while src-matches
|
|
530 (setq elt (car src-matches))
|
|
531 (if (null elt) nil
|
|
532 (setq src-matches (cdr src-matches)
|
|
533 currfile (expand-file-name (car elt) dir)
|
|
534 matches (cdr elt)
|
|
535 currbuf (get-file-buffer currfile)
|
|
536 kill-buf (not currbuf)
|
|
537 currbuf (or currbuf
|
|
538 (and (file-readable-p currfile)
|
|
539 (find-file-noselect currfile))))
|
|
540 (if (null currbuf)
|
|
541 (progn (set-buffer out-buf)
|
|
542 (insert "ERROR: (ebut:search): \"" currfile
|
|
543 "\" is not readable.\n\n"))
|
|
544 (set-buffer currbuf)
|
|
545 (unwind-protect
|
|
546 (save-excursion
|
|
547 (widen) (goto-char 1)
|
|
548 (let ((case-fold-search t)
|
|
549 (regexp
|
|
550 (ebut:match-regexp matches match-part)))
|
|
551 (setq firstmatch t)
|
|
552 (while (re-search-forward regexp nil t)
|
|
553 (setq total (1+ total))
|
|
554 (let* ((linenum (count-lines (point-min)
|
|
555 (point)))
|
|
556 (tag (format "\n%4d:" linenum))
|
|
557 lns start end)
|
|
558 (setq end (progn (end-of-line) (point))
|
|
559 start (progn
|
|
560 (goto-char (match-beginning 0))
|
|
561 (beginning-of-line) (point))
|
|
562 lns (buffer-substring start end))
|
|
563 (goto-char end)
|
|
564 (save-excursion
|
|
565 (set-buffer out-buf)
|
|
566 (if firstmatch
|
|
567 (progn
|
|
568 (insert hbut:source-prefix "\""
|
|
569 currfile "\"\n")
|
|
570 (setq firstmatch nil)))
|
|
571 (insert tag lns))))
|
|
572 (set-buffer out-buf)
|
|
573 (if (not firstmatch) (insert "\n\n"))))
|
|
574 (if kill-buf (kill-buffer currbuf)))))))))))
|
|
575 total))
|
|
576
|
|
577 ;;; ------------------------------------------------------------------------
|
|
578 (defun ebut:delimit (start end instance-str)
|
|
579 "Delimits button label spanning region START to END in current buffer.
|
|
580 If button is already delimited or delimit fails, returns nil, else t.
|
|
581 Inserts INSTANCE-STR after END, before ending delimiter."
|
|
582 (goto-char start)
|
|
583 (if (looking-at (regexp-quote ebut:start))
|
|
584 (forward-char (length ebut:start)))
|
|
585 (if (ebut:label-p)
|
|
586 nil
|
|
587 (if (not (stringp instance-str)) (setq instance-str ""))
|
|
588 (insert ebut:start)
|
|
589 (goto-char (setq end (+ end (length ebut:start))))
|
|
590 (insert instance-str ebut:end)
|
|
591 (setq end (+ end (length instance-str) (length ebut:end)))
|
|
592 (and (fboundp 'hproperty:but-add) (hproperty:but-add start end hproperty:but))
|
|
593 (hbut:comment start end)
|
|
594 (goto-char end)
|
|
595 t))
|
|
596
|
|
597 (defun ebut:match-regexp (match-keys match-part)
|
|
598 "Returns regexp to match to all explicit button keys from MATCH-KEYS."
|
|
599 (setq match-part (if match-part
|
|
600 (concat "[^" (substring ebut:end -1) "]*")
|
|
601 "[ \t\n]*"))
|
|
602 (concat
|
|
603 (regexp-quote ebut:start) match-part
|
|
604 "\\(" (mapconcat (function
|
|
605 (lambda (key) (ebut:label-regexp key 'no-delim)))
|
|
606 match-keys "\\|")
|
|
607 "\\)" match-part (regexp-quote ebut:end)))
|
|
608
|
|
609 (defconst ebut:start "<("
|
|
610 "String matching the start of a hyper-button.")
|
|
611 (defconst ebut:end ")>"
|
|
612 "String matching the end of a hyper-button.")
|
|
613 (defconst ebut:instance-sep ":"
|
|
614 "String of one character, separates an ebut label from its instance num.")
|
|
615
|
|
616 ;;; ========================================================================
|
|
617 ;;; gbut class - Global Hyperbole buttons - activated by typing label name
|
|
618 ;;; ========================================================================
|
|
619
|
|
620 (defvar gbut:file (expand-file-name hbmap:filename hbmap:dir-user)
|
|
621 "File that stores Hyperbole buttons accessible by name, global buttons.")
|
|
622
|
|
623 (defun gbut:act (label)
|
|
624 "Activates Hyperbole global button with LABEL."
|
|
625 (interactive (list (hargs:read-match "Activate global button labeled: "
|
24
|
626 (mapcar 'list (gbut:label-list))
|
0
|
627 nil t nil 'ebut)))
|
|
628 (let* ((lbl-key (hbut:label-to-key label))
|
|
629 (but (ebut:get lbl-key nil gbut:file)))
|
|
630 (if but
|
|
631 (hbut:act but)
|
|
632 (error "(gbut:act): No global button labeled: %s" label))))
|
|
633
|
|
634 (defun gbut:help (label)
|
|
635 "Displays help for Hyperbole global button with LABEL."
|
|
636 (interactive (list (hargs:read-match "Report on global button labeled: "
|
24
|
637 (mapcar 'list (gbut:label-list))
|
0
|
638 nil t nil 'ebut)))
|
|
639 (let* ((lbl-key (hbut:label-to-key label))
|
|
640 (but (ebut:get lbl-key nil gbut:file)))
|
|
641 (if but
|
|
642 (hbut:report but)
|
|
643 (error "(gbut:help): No global button labeled: %s" label))))
|
|
644
|
24
|
645 (defun gbut:label-list ()
|
|
646 "Returns list of global button labels."
|
|
647 (mapcar 'hbut:key-to-label (gbut:key-list)))
|
|
648
|
0
|
649 ;;; ------------------------------------------------------------------------
|
|
650 (defun gbut:key-list ()
|
|
651 "Returns list of global button label keys."
|
|
652 (save-excursion
|
|
653 (if (hbdata:to-entry-buf gbut:file)
|
|
654 (let ((gbuts))
|
|
655 (save-restriction
|
|
656 (narrow-to-region (point) (if (search-forward "\^L" nil t)
|
|
657 (point) (point-max)))
|
|
658 (goto-char (point-min))
|
|
659 (condition-case ()
|
|
660 (while (setq gbuts (cons (car (read (current-buffer))) gbuts)))
|
|
661 (error nil))
|
|
662 gbuts)))))
|
|
663
|
|
664 ;;; ========================================================================
|
|
665 ;;; hattr class
|
|
666 ;;; ========================================================================
|
|
667
|
|
668 (defun hattr:attributes (obj-symbol)
|
|
669 "Returns a list of OBJ-SYMBOL's attributes as symbols."
|
|
670 (if (symbolp obj-symbol)
|
|
671 (let* ((attr-val-list (symbol-plist obj-symbol))
|
|
672 (i -1))
|
|
673 (delq nil (mapcar (function
|
|
674 (lambda (elt)
|
|
675 (setq i (1+ i))
|
|
676 (and (= (% i 2) 0) elt)))
|
|
677 attr-val-list)))))
|
|
678
|
|
679 (defun hattr:clear (hbut)
|
|
680 "Removes all of HBUT's attributes except `variable-documentation'."
|
|
681 (let (sublist)
|
|
682 (or (symbolp hbut)
|
|
683 (error "(hattr:clear): Argument not a Hyperbole button: %s" hbut))
|
|
684 (if (setq sublist (memq 'variable-documentation (symbol-plist hbut)))
|
|
685 (progn
|
|
686 (setcdr (cdr sublist) nil)
|
|
687 (setplist hbut sublist))
|
|
688 (setplist hbut nil)
|
|
689 )))
|
|
690
|
|
691 (defun hattr:copy (from-hbut to-hbut)
|
|
692 "Copies attributes FROM-HBUT TO-HBUT, eliminating attributes TO-HBUT had.
|
|
693 Returns TO-HBUT."
|
|
694 (mapcar
|
|
695 (function
|
|
696 (lambda (hbut)
|
|
697 (or (and hbut (symbolp hbut))
|
|
698 (error "(hattr:clear): Argument not a Hyperbole button: %s" hbut))))
|
|
699 (list from-hbut to-hbut))
|
|
700 (unwind-protect
|
|
701 nil
|
|
702 (hattr:clear to-hbut)
|
|
703 (setplist to-hbut (copy-sequence (symbol-plist from-hbut))))
|
|
704 to-hbut)
|
|
705
|
|
706 (defun hattr:get (obj-symbol attr-symbol)
|
|
707 "Returns value of OBJ-SYMBOL's attribute ATTR-SYMBOL."
|
|
708 (get obj-symbol attr-symbol))
|
|
709
|
|
710 (defun hattr:list (obj-symbol)
|
|
711 "Returns a property list of OBJ-SYMBOL's attributes.
|
|
712 Each pair of elements is: <attrib-name> <attrib-value>."
|
|
713 (if (symbolp obj-symbol)
|
|
714 (symbol-plist obj-symbol)
|
|
715 (error "(hattr:list): Argument not a symbol: %s" obj-symbol)))
|
|
716
|
|
717 (defun hattr:memq (attr-symbol obj-symbol)
|
|
718 "Returns t if ATTR-SYMBOL is in OBJ-SYMBOL's attribute list, else nil."
|
|
719 (and (symbolp obj-symbol) (symbolp attr-symbol)
|
|
720 (let* ((attr-val-list (symbol-plist obj-symbol))
|
|
721 (attr-list (let ((i -1))
|
|
722 (delq nil (mapcar
|
|
723 (function
|
|
724 (lambda (elt)
|
|
725 (setq i (1+ i))
|
|
726 (and (= (% i 2) 0) elt)))
|
|
727 attr-val-list)))))
|
|
728 (if (memq attr-symbol attr-list) t))))
|
|
729
|
|
730 (defun hattr:report (attrib-list)
|
|
731 "Pretty prints to standard-output attribute-value pairs from ATTRIB-LIST.
|
|
732 Ignores nil valued attributes. Returns t unless no attributes are printed."
|
|
733 (let ((has-attr) attr val len)
|
|
734 (if (or (null attrib-list) (not (listp attrib-list))
|
|
735 ;; odd number of elements?
|
|
736 (= (% (length attrib-list) 2) 1))
|
|
737 nil
|
|
738 (while (setq attr (car attrib-list))
|
|
739 (setq val (car (setq attrib-list (cdr attrib-list)))
|
|
740 attrib-list (cdr attrib-list))
|
|
741 (if val
|
|
742 (progn
|
|
743 (setq has-attr t
|
|
744 attr (symbol-name attr)
|
|
745 len (max (- 16 (length attr)) 1))
|
|
746 (princ " ") (princ attr) (princ ":")
|
|
747 (princ (make-string len ? ))
|
|
748 (let (str)
|
|
749 (prin1 (cond ((string-match "time" attr)
|
|
750 (htz:date-unix val
|
|
751 (and (>= (aref val 0) ?0)
|
|
752 (<= (aref val 0) ?9)
|
|
753 "GMT") htz:local))
|
|
754 ((and (setq str (if (stringp val) val
|
|
755 (prin1-to-string val)))
|
|
756 (string-match "\\`actypes::" str))
|
|
757 (intern (substring str (match-end 0))))
|
|
758 (t val))))
|
|
759 (terpri))))
|
|
760 has-attr)))
|
|
761
|
|
762 (defun hattr:save ()
|
|
763 "Saves button attribute file for current directory, if modified.
|
|
764 Suitable for use as part of 'write-file-hooks'."
|
|
765 (let* ((bd-file (expand-file-name hattr:filename default-directory))
|
|
766 (buf (and (stringp default-directory)
|
|
767 (get-file-buffer bd-file))))
|
|
768 (if (and ebut:hattr-save buf (not (eq buf (current-buffer))))
|
|
769 (let ((ebut:hattr-save));; Prevents 'write-file-hooks' looping.
|
|
770 (and (buffer-modified-p buf)
|
|
771 (save-excursion
|
|
772 (set-buffer buf) (save-buffer)
|
|
773 ;; Unlock button attribute file; kill buffer so user is
|
|
774 ;; never holding a buffer which is out of sync with file,
|
|
775 ;; due to some other user's edits.
|
|
776 ;; Maybe this should be user or site configurable.
|
|
777 (or (buffer-modified-p buf) (kill-buffer buf))
|
|
778 )))))
|
|
779 ;; Must return nil, so can be used as part of write-file-hooks.
|
|
780 nil)
|
|
781
|
|
782 (defun hattr:set (obj-symbol attr-symbol attr-value)
|
|
783 "Sets OBJ-SYMBOL's attribute ATTR-SYMBOL to ATTR-VALUE."
|
|
784 (put obj-symbol attr-symbol attr-value))
|
|
785
|
|
786 (fset 'hattr:summarize 'hattr:report)
|
|
787
|
|
788 (defvar hattr:filename
|
24
|
789 (if hyperb:microcruft-os-p "_hypb" ".hypb")
|
0
|
790 "Per directory file name in which explicit button attributes are stored.
|
|
791 If you change its value, you will be unable to use buttons created by
|
|
792 others who use a different value!")
|
|
793
|
|
794 ;;; ========================================================================
|
|
795 ;;; hbut class - abstract
|
|
796 ;;; ========================================================================
|
|
797
|
|
798 (defun hbut:act (hbut)
|
|
799 "Performs action for explicit or implicit Hyperbole button symbol HBUT."
|
24
|
800 (if hbut (apply 'actype:act (hattr:get hbut 'actype)
|
|
801 (hattr:get hbut 'args))))
|
0
|
802
|
|
803 (defun hbut:action (hbut)
|
|
804 "Returns appropriate action for Hyperbole button symbol HBUT."
|
|
805 (let ((categ (hattr:get hbut 'categ)) (atype) (action))
|
|
806 (if (eq categ 'explicit)
|
|
807 (progn (setq action (hattr:get hbut 'action)
|
|
808 atype (hattr:get hbut 'actype))
|
|
809 (if (= (length (symbol-name atype)) 2)
|
|
810 atype
|
|
811 (or action (actype:action atype))))
|
|
812 ;; Must be an implicit button.
|
|
813 (if (fboundp atype) atype))))
|
|
814
|
|
815 (defun hbut:at-p ()
|
|
816 "Returns symbol for explicit or implicit Hyperbole button at point or nil."
|
|
817 (or (ebut:at-p) (ibut:at-p)))
|
|
818
|
|
819
|
|
820 (defun hbut:comment (start end)
|
|
821 "Comment button label spanning region START to END in current buffer.
|
|
822 Use buffer commenting grammar, if any, otherwise don't comment."
|
|
823 (save-excursion
|
|
824 (if comment-start
|
|
825 (if (or (equal comment-end "")
|
|
826 (null comment-end))
|
|
827 (progn
|
|
828 (beginning-of-line)
|
|
829 (if (search-forward comment-start start t)
|
|
830 nil
|
|
831 (goto-char start)
|
|
832 (insert comment-start)
|
|
833 (if (/= (preceding-char) ? )
|
|
834 (insert ? ))))
|
|
835 ;; Comments have both start and end delimiters
|
|
836 (if (and (re-search-backward
|
|
837 (concat (regexp-quote comment-start) "\\|"
|
|
838 (regexp-quote comment-end))
|
|
839 nil t)
|
|
840 (looking-at (regexp-quote comment-start)))
|
|
841 nil
|
|
842 (goto-char start)
|
|
843 (insert comment-start)
|
|
844 (if (/= (preceding-char) ? )
|
|
845 (insert ? ))
|
|
846 (goto-char (+ (point) (- end start)))
|
|
847 (if (/= (following-char) ? )
|
|
848 (insert ? ))
|
|
849 (insert comment-end)
|
|
850 )))))
|
|
851
|
|
852 ;;; Regexps derived in part from "filladapt.el" under the GPL, Copyright
|
|
853 ;;; 1989 Kyle E. Jones.
|
|
854 (defvar hbut:fill-prefix-regexps
|
|
855 '(
|
|
856 ;; Included text in news or mail messages
|
|
857 "\n[ \t]*\\([:|<>]+ *\\)+"
|
|
858 ;; Included text generated by SUPERCITE. We can't hope to match all
|
|
859 ;; the possible variations.
|
|
860 "\n[ \t]*[^'`\"< \t]*> *"
|
|
861 ;; Lisp comments
|
|
862 "\n[ \t]*\\(;+[ \t]*\\)+"
|
|
863 ;; UNIX shell comments
|
|
864 "\n[ \t]*\\(#+[ \t]*\\)+"
|
|
865 ;; C++ comments
|
|
866 "\n[ \t]*//[/ \t]+"
|
|
867 ;; C or Pascal comments, one open and close per line, so match close
|
|
868 ;; then open.
|
|
869 "\\*+[/\)][ \t]*\n+[ \t]*[/\(]\\*+"
|
|
870 "}[ \t]*\n+[ \t]*{"
|
|
871 ;; Eiffel or Sather comments
|
|
872 "\n[ \t]*--[ \t]+"
|
|
873 ;; Fortran comments
|
|
874 "\n[Cc][ \t]+"
|
|
875 ;; Postscript comments
|
|
876 "\n[ \t]*\\(%+[ \t]*\\)+"
|
|
877 )
|
|
878 "List of regexps of fill prefixes to remove from the middle of buttons.")
|
|
879
|
|
880 (defun hbut:fill-prefix-remove (label)
|
|
881 "Removes any recognized fill prefix from within LABEL.
|
|
882 'hbut:fill-prefix-regexps' is a list of fill prefixes to recognize."
|
|
883 (if (string-match "\n" label)
|
|
884 (mapcar
|
|
885 (function
|
|
886 (lambda (fill-prefix)
|
|
887 (and (string-match "\n" label)
|
|
888 (setq label
|
|
889 (hypb:replace-match-string fill-prefix label " " t)))))
|
|
890 hbut:fill-prefix-regexps))
|
|
891 label)
|
|
892
|
|
893 (defun hbut:is-p (object)
|
|
894 "Returns non-nil if object denotes a Hyperbole button."
|
|
895 (and (symbolp object) (hattr:get object 'categ)))
|
|
896
|
|
897 (fset 'hbut:key-src 'ebut:key-src)
|
|
898 (fset 'hbut:key-to-label 'ebut:key-to-label)
|
|
899
|
|
900 (defun hbut:label (hbut)
|
|
901 "Returns the label for Hyperbole button symbol HBUT."
|
|
902 (if (hbut:is-p hbut)
|
|
903 (hbut:key-to-label (hattr:get hbut 'lbl-key))
|
|
904 (error "(hbut:label): Argument is not a Hyperbole button symbol, '%s'"
|
|
905 hbut)))
|
|
906
|
|
907 (fset 'hbut:label-p 'ebut:label-p)
|
|
908 (fset 'hbut:label-to-key 'ebut:label-to-key)
|
|
909
|
|
910 (defun hbut:report (&optional arg)
|
|
911 "Pretty prints the attributes of a button or buttons.
|
|
912
|
|
913 Takes an optional ARG interpreted as follows:
|
|
914 a button symbol - report on that button;
|
|
915 nil - report on button at point, if any;
|
|
916 integer > 0 - report on all explicit buttons in buffer, alphabetize;
|
|
917 integer < 1 - report on all explicit buttons in occurrence order;
|
|
918
|
|
919 Returns number of buttons reported on or nil if none."
|
|
920 (setq arg (cond ((or (integerp arg) (symbolp arg)) arg)
|
|
921 ((listp arg)
|
|
922 (if (integerp (setq arg (car arg))) arg 1))
|
|
923 (t 1)))
|
|
924 (let* ((but (if (and arg (symbolp arg)) arg (hbut:at-p)))
|
|
925 (curr-key (and but (hattr:get but 'lbl-key)))
|
|
926 (key-src (or (and but (hattr:get but 'loc)) (hbut:key-src)))
|
|
927 (lbl-lst (cond ((not arg)
|
|
928 (if curr-key (list (ebut:key-to-label curr-key))))
|
|
929 ((symbolp arg) (if curr-key
|
|
930 (list (hbut:key-to-label
|
|
931 (hattr:get arg 'lbl-key)))))
|
|
932 ((< arg 1) (ebut:list))
|
|
933 (t (sort (ebut:list)
|
|
934 (function
|
|
935 (lambda (s1 s2)
|
|
936 (string< (downcase s1) (downcase s2))))))))
|
|
937 (key-buf (current-buffer))
|
|
938 (buf-name (hypb:help-buf-name))
|
|
939 (attribs))
|
|
940 (if lbl-lst
|
|
941 (progn
|
|
942 (with-output-to-temp-buffer buf-name
|
|
943 (princ hbut:source-prefix)
|
|
944 (prin1 key-src)
|
|
945 (terpri)
|
|
946 (terpri)
|
|
947 (mapcar
|
|
948 (function
|
|
949 (lambda (lbl)
|
|
950 (if (setq but
|
|
951 (cond ((or (null arg) (symbolp arg)) but)
|
|
952 (t (ebut:get (ebut:label-to-key lbl) key-buf)))
|
|
953 attribs (hattr:list but))
|
|
954 (progn
|
|
955 (princ (if (ibut:is-p but)
|
|
956 lbl
|
|
957 (concat ebut:start lbl ebut:end)))
|
|
958 (terpri)
|
|
959 (let ((doc (actype:doc but (= 1 (length lbl-lst)))))
|
|
960 (if doc
|
|
961 (progn
|
|
962 (princ " ")
|
|
963 (princ doc)
|
|
964 (terpri))))
|
|
965 (hattr:report
|
|
966 ;; (if (eq (car (cdr (memq 'categ attribs))) 'explicit)
|
|
967 ;; (memq 'action attribs)
|
|
968 ;; (memq 'categ attribs))
|
|
969 attribs)
|
|
970 (terpri))
|
|
971 )))
|
|
972 lbl-lst))
|
|
973 (length lbl-lst)))))
|
|
974
|
|
975 (defun hbut:source (&optional full)
|
|
976 "Returns Hyperbole source buffer or file given at point.
|
|
977 If a file, always returns a full path if optional FULL is non-nil."
|
|
978 (goto-char (match-end 0))
|
24
|
979 (cond ((looking-at "#<buffer \"?\\([^ \n\"]+\\)\"?>")
|
0
|
980 (get-buffer (buffer-substring (match-beginning 1)
|
|
981 (match-end 1))))
|
|
982 ((looking-at "\".+\"")
|
|
983 (let* ((file (buffer-substring (1+ (match-beginning 0))
|
|
984 (1- (match-end 0))))
|
|
985 (absolute (file-name-absolute-p file)))
|
|
986 (if (and full (not absolute))
|
|
987 (expand-file-name file default-directory)
|
|
988 file)))))
|
|
989
|
|
990 (fset 'hbut:summarize 'hbut:report)
|
|
991
|
|
992 (defvar hbut:current nil
|
|
993 "Currently selected Hyperbole button.
|
|
994 Available to action routines.")
|
|
995
|
|
996 (defconst hbut:source-prefix moccur-source-prefix
|
|
997 "String found at start of a buffer containing only a hyper-button menu.
|
|
998 This expression should be followed immediately by a file-name indicating the
|
|
999 source file for the buttons in the menu, if any.")
|
|
1000
|
|
1001 ;;; ========================================================================
|
|
1002 ;;; htype class - Hyperbole Types, e.g. action and implicit button types
|
|
1003 ;;; ========================================================================
|
|
1004
|
|
1005 (require 'set)
|
|
1006
|
|
1007 (defun htype:body (htype-sym)
|
|
1008 "Return body for HTYPE-SYM. If HTYPE-SYM is nil, return nil."
|
|
1009 (and htype-sym (hypb:indirect-function htype-sym)))
|
|
1010
|
|
1011 (defun htype:category (type-category)
|
|
1012 "Return list of symbols in Hyperbole TYPE-CATEGORY in priority order.
|
|
1013 Symbols contain category component.
|
|
1014 TYPE-CATEGORY should be 'actypes, 'ibtypes or nil for all."
|
|
1015 (let ((types (symset:get type-category 'symbols))
|
|
1016 (categ-name (symbol-name type-category)))
|
|
1017 (mapcar (function
|
|
1018 (lambda (type)
|
|
1019 (intern (concat categ-name "::" (symbol-name type)))))
|
|
1020 types)))
|
|
1021
|
|
1022 ;; Thanks to JWZ for help on this.
|
|
1023 (defmacro htype:create (type type-category doc params body property-list)
|
|
1024 "Create a new Hyperbole TYPE within TYPE-CATEGORY (both unquoted symbols).
|
|
1025 Third arg DOC is a string describing the type.
|
|
1026 Fourth arg PARAMS is a list of parameters to send to the fifth arg BODY,
|
|
1027 which is a list of forms executed when the type is evaluated.
|
|
1028 Sixth arg PROPERTY-LIST is attached to the new type's symbol.
|
|
1029
|
|
1030 This symbol is returned."
|
|
1031 (let* ((sym (htype:symbol type type-category))
|
|
1032 (action (nconc (list 'defun sym params doc) body)))
|
|
1033 (` (progn
|
|
1034 (, action)
|
|
1035 (setplist '(, sym) (, property-list))
|
|
1036 (symset:add '(, type) '(, type-category) 'symbols)
|
|
1037 (run-hooks 'htype:create-hook)
|
|
1038 '(, sym)))))
|
|
1039
|
|
1040 (defun htype:delete (type type-category)
|
|
1041 "Delete a Hyperbole TYPE derived from TYPE-CATEGORY (both symbols).
|
|
1042 Return the Hyperbole symbol for the TYPE if it existed, else nil."
|
|
1043 (let* ((sym (htype:symbol type type-category))
|
|
1044 (exists (fboundp 'sym)))
|
|
1045 (setplist sym nil)
|
|
1046 (symset:delete type type-category 'symbols)
|
|
1047 (fmakunbound sym)
|
|
1048 (run-hooks 'htype:delete-hook)
|
|
1049 (and exists sym)))
|
|
1050
|
|
1051 (defun htype:doc (type)
|
|
1052 "Return documentation for Hyperbole TYPE, a symbol."
|
|
1053 (documentation type))
|
|
1054
|
|
1055 (defun htype:names (type-category &optional sym)
|
|
1056 "Return list of current names for Hyperbole TYPE-CATEGORY in priority order.
|
|
1057 Names do not contain category component.
|
|
1058 TYPE-CATEGORY should be 'actypes, 'ibtypes or nil for all.
|
|
1059 When optional SYM is given, return the name for that symbol only, if any."
|
|
1060 (let ((types (symset:get type-category 'symbols))
|
|
1061 (sym-name (and sym (symbol-name sym))))
|
|
1062 (if sym-name
|
|
1063 ;; Strip category from sym-name before looking for a match.
|
|
1064 (progn (if (string-match "::" sym-name)
|
|
1065 (setq sym (intern (substring sym-name (match-end 0)))))
|
|
1066 (if (memq sym types) (symbol-name sym)))
|
|
1067 (mapcar 'symbol-name types))))
|
|
1068
|
|
1069 ;;; ------------------------------------------------------------------------
|
|
1070
|
|
1071 (defun htype:symbol (type type-category)
|
|
1072 "Return Hyperbole type symbol composed from TYPE and TYPE-CATEGORY (both symbols)."
|
|
1073 (intern (concat (symbol-name type-category) "::"
|
|
1074 (symbol-name type))))
|
|
1075
|
|
1076 ;;; ========================================================================
|
|
1077 ;;; ibut class - Implicit Hyperbole Buttons
|
|
1078 ;;; ========================================================================
|
|
1079
|
|
1080 (defun ibut:at-p (&optional key-only)
|
|
1081 "Returns symbol for implicit button at point, else nil.
|
|
1082 With optional KEY-ONLY, returns only the label key for button."
|
|
1083 (let ((types (htype:category 'ibtypes))
|
|
1084 ;; Global var used in (hact) function, don't delete.
|
|
1085 (hrule:action 'actype:identity)
|
|
1086 (itype)
|
|
1087 (args)
|
|
1088 (is-type))
|
|
1089 (or key-only (hattr:clear 'hbut:current))
|
|
1090 (while (and (not is-type) types)
|
|
1091 (setq itype (car types))
|
|
1092 (if (setq args (funcall itype))
|
|
1093 (setq is-type itype)
|
|
1094 (setq types (cdr types))))
|
|
1095 (if is-type
|
|
1096 (if key-only
|
|
1097 (hattr:get 'hbut:current 'lbl-key)
|
|
1098 (hattr:set 'hbut:current 'loc (save-excursion
|
|
1099 (hbut:key-src 'full)))
|
|
1100 (hattr:set 'hbut:current 'categ is-type)
|
|
1101 (or (hattr:get 'hbut:current 'args)
|
|
1102 (not (listp args))
|
|
1103 (progn
|
|
1104 (hattr:set 'hbut:current 'actype
|
|
1105 (or
|
|
1106 ;; Hyperbole action type
|
|
1107 (intern-soft (concat "actypes::"
|
|
1108 (symbol-name (car args))))
|
|
1109 ;; Regular Emacs Lisp function symbol
|
|
1110 (car args)
|
|
1111 ))
|
|
1112 (hattr:set 'hbut:current 'args (cdr args))))
|
|
1113 'hbut:current))))
|
|
1114
|
|
1115 (defun ibut:is-p (object)
|
|
1116 "Returns non-nil if object denotes an implicit Hyperbole button."
|
|
1117 (if (symbolp object)
|
|
1118 (let ((categ (hattr:get object 'categ)))
|
|
1119 (and categ (string-match "^ibtypes::" (symbol-name categ))))))
|
|
1120
|
|
1121 (defun ibut:label-p ()
|
|
1122 "Returns key for Hyperbole implicit button label that point is on or nil."
|
|
1123 (ibut:at-p 'key-only))
|
|
1124
|
|
1125 (defun ibut:label-set (label &optional start end)
|
|
1126 "Sets current implicit button attributes from LABEL and START, END position.
|
24
|
1127 Returns label. START and END are optional. When given, they specify the
|
|
1128 region in the buffer to flash when this implicit button is activated or
|
|
1129 queried for its attributes. If LABEL is a list, it is assumed to contain all
|
|
1130 arguments."
|
0
|
1131 (cond ((stringp label)
|
|
1132 (hattr:set 'hbut:current 'lbl-key (hbut:label-to-key label))
|
|
1133 (and start (hattr:set 'hbut:current 'lbl-start start))
|
|
1134 (and end (hattr:set 'hbut:current 'lbl-end end)))
|
|
1135 ((and label (listp label))
|
|
1136 (hattr:set 'hbut:current 'lbl-key (hbut:label-to-key (car label)))
|
|
1137 (hattr:set 'hbut:current 'lbl-start (nth 1 label))
|
|
1138 (hattr:set 'hbut:current 'lbl-end (nth 2 label)))
|
|
1139 (t (error "(ibut:label-set): Invalid label arg: '%s'" label)))
|
24
|
1140 label)
|
0
|
1141
|
|
1142 ;;; ========================================================================
|
|
1143 ;;; ibtype class - Implicit button types
|
|
1144 ;;; ========================================================================
|
|
1145
|
|
1146 (fset 'defib 'ibtype:create)
|
|
1147 (put 'ibtype:create 'lisp-indent-function 'defun)
|
|
1148 (defmacro ibtype:create (type params doc at-p &optional to-p style)
|
|
1149 "Creates implicit button TYPE (unquoted sym) with PARAMS, described by DOC.
|
|
1150 PARAMS are presently ignored.
|
|
1151
|
|
1152 AT-P is a boolean form of no arguments which determines whether or not point
|
|
1153 is within a button of this type.
|
|
1154 Optional TO-P is a boolean form which moves point immediately after the next
|
|
1155 button of this type within the current buffer and returns a list of (button-
|
|
1156 label start-pos end-pos), or nil when none is found.
|
|
1157 Optional STYLE is a display style specification to use when highlighting
|
|
1158 buttons of this type; most useful when TO-P is also given.
|
|
1159
|
|
1160 Returns symbol created when successful, else nil. Nil indicates that action
|
|
1161 type for ibtype is presently undefined."
|
|
1162 (if type
|
|
1163 (let ((to-func (if to-p (action:create nil (list to-p))))
|
|
1164 (at-func (list at-p)))
|
|
1165 (` (htype:create (, type) ibtypes (, doc) nil (, at-func)
|
|
1166 (list 'to-p (, to-func) 'style (, style)))))))
|
|
1167
|
|
1168 (defun ibtype:delete (type)
|
|
1169 "Deletes an implicit button TYPE (a symbol).
|
|
1170 Returns TYPE's symbol if it existed, else nil."
|
|
1171 (htype:delete type 'ibtypes))
|
|
1172
|
|
1173 ;;; ========================================================================
|
|
1174 ;;; symset class - Hyperbole internal symbol set maintenance
|
|
1175 ;;; ========================================================================
|
|
1176
|
|
1177 (require 'set)
|
|
1178
|
|
1179 (defun symset:add (elt symbol prop)
|
|
1180 "Adds ELT to SYMBOL's PROP set.
|
|
1181 Returns nil iff ELT is already in SET. Uses 'eq' for comparison."
|
|
1182 (let* ((set (get symbol prop))
|
|
1183 (set:equal-op 'eq)
|
|
1184 (new-set (set:add elt set)))
|
|
1185 (and new-set (put symbol prop new-set))))
|
|
1186
|
|
1187 (fset 'symset:delete 'symset:remove)
|
|
1188
|
|
1189 (defun symset:get (symbol prop)
|
|
1190 "Returns SYMBOL's PROP set."
|
|
1191 (get symbol prop))
|
|
1192
|
|
1193 (defun symset:remove (elt symbol prop)
|
|
1194 "Removes ELT from SYMBOL's PROP set and returns the new set.
|
|
1195 Assumes PROP is a valid set. Uses 'eq' for comparison."
|
|
1196 (let ((set (get symbol prop))
|
|
1197 (set:equal-op 'eq))
|
|
1198 (put symbol prop (set:remove elt set))))
|
|
1199
|
|
1200
|
|
1201 (provide 'hbut)
|