39643
|
1 ;;; button.el --- Clickable buttons
|
|
2 ;;
|
|
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
|
|
4 ;;
|
|
5 ;; Author: Miles Bader <miles@gnu.org>
|
|
6 ;; Keywords: extensions
|
|
7 ;;
|
|
8 ;; This file is part of GNU Emacs.
|
|
9 ;;
|
|
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
11 ;; it under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14 ;;
|
|
15 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 ;; GNU General Public License for more details.
|
|
19 ;;
|
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
23 ;; Boston, MA 02111-1307, USA.
|
|
24
|
|
25 ;;; Commentary:
|
|
26 ;;
|
|
27 ;; This package defines functions for inserting and manipulating
|
|
28 ;; clickable buttons in Emacs buffers, such as might be used for help
|
|
29 ;; hyperlinks, etc.
|
|
30 ;;
|
|
31 ;; In some ways it duplicates functionality also offered by the
|
|
32 ;; `widget' package, but the button package has the advantage that it
|
|
33 ;; is (1) much faster, (2) much smaller, and (3) much, much, simpler
|
|
34 ;; (the code, that is, not the interface).
|
|
35 ;;
|
|
36 ;; Buttons can either use overlays, in which case the button is
|
|
37 ;; represented by the overlay itself, or text-properties, in which case
|
|
38 ;; the button is represented by a marker or buffer-position pointing
|
|
39 ;; somewhere in the button. In the latter case, no markers into the
|
|
40 ;; buffer are retained, which is important for speed if there are are
|
|
41 ;; extremely large numbers of buttons.
|
|
42 ;;
|
|
43 ;; Using `define-button-type' to define default properties for buttons
|
|
44 ;; is not necessary, but it is is encouraged, since doing so makes the
|
|
45 ;; resulting code clearer and more efficient.
|
|
46 ;;
|
|
47
|
|
48 ;;; Code:
|
|
49
|
|
50
|
|
51 ;; Globals
|
|
52
|
|
53 (defface button '((t :underline t))
|
|
54 "Default face used for buttons.")
|
|
55
|
|
56 ;;;###autoload
|
|
57 (defvar button-map
|
|
58 (let ((map (make-sparse-keymap)))
|
|
59 (define-key map "\r" 'push-button)
|
|
60 (define-key map [mouse-2] 'push-button)
|
|
61 map)
|
|
62 "Keymap used by buttons.")
|
|
63
|
|
64 ;;;###autoload
|
|
65 (defvar button-buffer-map
|
|
66 (let ((map (make-sparse-keymap)))
|
|
67 (define-key map [?\t] 'forward-button)
|
|
68 (define-key map [backtab] 'backward-button)
|
|
69 map)
|
|
70 "Keymap useful for buffers containing buttons.
|
|
71 Mode-specific keymaps may want to use this as their parent keymap.")
|
|
72
|
|
73 ;; Default properties for buttons
|
|
74 (put 'default-button 'face 'button)
|
|
75 (put 'default-button 'mouse-face 'highlight)
|
|
76 (put 'default-button 'keymap button-map)
|
|
77 (put 'default-button 'type 'button)
|
|
78 (put 'default-button 'action 'button-nop)
|
|
79 (put 'default-button 'help-echo "mouse-2, RET: Push this button")
|
|
80 ;; Make overlay buttons go away if their underlying text is deleted.
|
|
81 (put 'default-button 'evaporate t)
|
|
82 ;; Prevent insertions adjacent to the text-property buttons from
|
|
83 ;; inheriting its properties.
|
|
84 (put 'default-button 'rear-nonsticky t)
|
|
85 ;; Text property buttons don't have a `button' property of their own, so
|
|
86 ;; they inherit this.
|
|
87 (put 'default-button 'button t)
|
|
88
|
|
89 ;; This is the default button action.
|
|
90 (defun button-nop (button)
|
|
91 "Do nothing to BUTTON."
|
|
92 nil)
|
|
93
|
|
94
|
|
95 ;; Button types (which can be used to hold default properties for buttons)
|
|
96
|
|
97 ;;;###autoload
|
|
98 (defun define-button-type (name &rest properties)
|
|
99 "Define a `button type' called NAME.
|
|
100 The remaining arguments form a sequence of PROPERTY VALUE pairs,
|
|
101 specifying properties to use as defaults for buttons with this type
|
|
102 \(a button's type may be set by giving it a `type' property when
|
|
103 creating the button)."
|
|
104 ;; We use a different symbol than NAME (with `-button' appended, and
|
|
105 ;; uninterned) to store the properties. This is to avoid name
|
|
106 ;; clashes, since many very general properties may be include in
|
|
107 ;; PROPERTIES.
|
|
108 (let ((catsym (make-symbol (concat (symbol-name name) "-button"))))
|
|
109 ;; Provide a link so that it's easy to find the real symbol.
|
|
110 (put name 'button-category-symbol catsym)
|
|
111 ;; Initialize NAME's properties using the global defaults.
|
|
112 (let ((default-props (symbol-plist 'default-button)))
|
|
113 (while default-props
|
|
114 (put catsym (pop default-props) (pop default-props))))
|
|
115 ;; Add NAME as the `type' property, which will then be returned as
|
|
116 ;; the type property of individual buttons.
|
|
117 (put catsym 'type name)
|
|
118 ;; Add the properties in PROPERTIES to the real symbol.
|
|
119 (while properties
|
|
120 (put catsym (pop properties) (pop properties)))
|
|
121 name))
|
|
122
|
|
123 ;; [this is an internal function]
|
|
124 (defsubst button-category-symbol (type)
|
|
125 "Return the symbol used by button-type TYPE to store properties.
|
|
126 Buttons inherit them by setting their `category' property to that symbol."
|
|
127 (or (get type 'button-category-symbol)
|
|
128 (error "Unknown button type `%s'" type)))
|
|
129
|
|
130 (defun button-type-put (type prop val)
|
|
131 "Set the button-type TYPE's PROP property to VAL."
|
|
132 (put (button-category-symbol type) prop val))
|
|
133
|
|
134 (defun button-type-get (type prop)
|
|
135 "Get the property of button-type TYPE named PROP."
|
|
136 (get (button-category-symbol type) prop))
|
|
137
|
|
138
|
|
139 ;; Button properties and other attributes
|
|
140
|
|
141 (defun button-start (button)
|
|
142 "Return the position at which BUTTON starts."
|
|
143 (if (overlayp button)
|
|
144 (overlay-start button)
|
|
145 ;; Must be a text-property button.
|
|
146 (or (previous-single-property-change (1+ button) 'button)
|
|
147 (point-min))))
|
|
148
|
|
149 (defun button-end (button)
|
|
150 "Return the position at which BUTTON ends."
|
|
151 (if (overlayp button)
|
|
152 (overlay-end button)
|
|
153 ;; Must be a text-property button.
|
|
154 (or (next-single-property-change button 'button)
|
|
155 (point-max))))
|
|
156
|
|
157 (defun button-get (button prop)
|
|
158 "Get the property of button BUTTON named PROP."
|
|
159 (if (overlayp button)
|
|
160 (overlay-get button prop)
|
|
161 ;; Must be a text-property button.
|
|
162 (get-text-property button prop)))
|
|
163
|
|
164 (defun button-put (button prop val)
|
|
165 "Set BUTTON's PROP property to VAL."
|
|
166 ;; Treat some properties specially.
|
|
167 (cond ((eq prop 'type)
|
|
168 ;; We translate a `type' property a `category' property, since
|
|
169 ;; that's what's actually used by overlays/text-properties for
|
|
170 ;; inheriting properties.
|
|
171 (setq prop 'category)
|
|
172 (setq val (button-category-symbol val)))
|
|
173 ((eq prop 'category)
|
|
174 ;; Disallow updating the `category' property directly.
|
|
175 (error "Button `category' property may not be set directly")))
|
|
176 ;; Add the property.
|
|
177 (if (overlayp button)
|
|
178 (overlay-put button prop val)
|
|
179 ;; Must be a text-property button.
|
|
180 (put-text-property
|
|
181 (or (previous-single-property-change (1+ button) 'button)
|
|
182 (point-min))
|
|
183 (or (next-single-property-change button 'button)
|
|
184 (point-max))
|
|
185 prop val)))
|
|
186
|
|
187 (defsubst button-activate (button)
|
|
188 "Call BUTTON's action property."
|
|
189 (funcall (button-get button 'action) button))
|
|
190
|
|
191 (defun button-label (button)
|
|
192 "Return BUTTON's text label."
|
|
193 (buffer-substring-no-properties (button-start button) (button-end button)))
|
|
194
|
|
195
|
|
196 ;; Creating overlay buttons
|
|
197
|
|
198 ;;;###autoload
|
|
199 (defun make-button (beg end &rest properties)
|
|
200 "Make a button from BEG to END in the current buffer.
|
|
201 The remaining arguments form a sequence of PROPERTY VALUE pairs,
|
|
202 specifying properties to add to the button. In particular, the `type'
|
|
203 property may be used to specify a button-type from which to inherit
|
|
204 other properties; see `define-button-type'.
|
|
205
|
|
206 Also see `make-text-button', `insert-button'."
|
|
207 (let ((overlay (make-overlay beg end nil t nil)))
|
|
208 (while properties
|
|
209 (button-put overlay (pop properties) (pop properties)))
|
|
210 ;; Put a pointer to the button in the overlay, so it's easy to get
|
|
211 ;; when we don't actually have a reference to the overlay.
|
|
212 (overlay-put overlay 'button overlay)
|
|
213 ;; If the user didn't specify a type, use the default.
|
|
214 (unless (overlay-get overlay 'category)
|
|
215 (overlay-put overlay 'category 'default-button))
|
|
216 ;; OVERLAY is the button, so return it
|
|
217 overlay))
|
|
218
|
|
219 ;;;###autoload
|
|
220 (defun insert-button (label &rest properties)
|
|
221 "Insert a button with the label LABEL.
|
|
222 The remaining arguments form a sequence of PROPERTY VALUE pairs,
|
|
223 specifying properties to add to the button. In particular, the `type'
|
|
224 property may be used to specify a button-type from which to inherit
|
|
225 other properties; see `define-button-type'.
|
|
226
|
|
227 Also see `insert-text-button', `make-button'."
|
|
228 (apply #'make-button
|
|
229 (prog1 (point) (insert label))
|
|
230 (point)
|
|
231 properties))
|
|
232
|
|
233
|
|
234 ;; Creating text-property buttons
|
|
235
|
|
236 ;;;###autoload
|
|
237 (defun make-text-button (beg end &rest properties)
|
|
238 "Make a button from BEG to END in the current buffer.
|
|
239 The remaining arguments form a sequence of PROPERTY VALUE pairs,
|
|
240 specifying properties to add to the button. In particular, the `type'
|
|
241 property may be used to specify a button-type from which to inherit
|
|
242 other properties; see `define-button-type'.
|
|
243
|
|
244 This function is like `make-button', except that the button is actually
|
|
245 part of the text instead of being a property of the buffer. Creating
|
|
246 large numbers of buttons can also be somewhat faster using
|
|
247 `make-text-button'.
|
|
248
|
|
249 Also see `insert-text-button'."
|
|
250 (let (prop val)
|
|
251 (while properties
|
|
252 (setq prop (pop properties))
|
|
253 (setq val (pop properties))
|
|
254 ;; Note that all the following code is basically equivalent to
|
|
255 ;; `button-put', but we can do it much more efficiently since we
|
|
256 ;; already have BEG and END.
|
|
257 (cond ((eq prop 'type)
|
|
258 ;; We translate a `type' property into a `category'
|
|
259 ;; property, since that's what's actually used by
|
|
260 ;; text-properties for inheritance.
|
|
261 (setq prop 'category)
|
|
262 (setq val (button-category-symbol val)))
|
|
263 ((eq prop 'category)
|
|
264 ;; Disallow setting the `category' property directly.
|
|
265 (error "Button `category' property may not be set directly")))
|
|
266 ;; Add the property.
|
|
267 (put-text-property beg end prop val)))
|
|
268 ;; Return something that can be used to get at the button.
|
|
269 beg)
|
|
270
|
|
271 ;;;###autoload
|
|
272 (defun insert-text-button (label &rest properties)
|
|
273 "Insert a button with the label LABEL.
|
|
274 The remaining arguments form a sequence of PROPERTY VALUE pairs,
|
|
275 specifying properties to add to the button. In particular, the `type'
|
|
276 property may be used to specify a button-type from which to inherit
|
|
277 other properties; see `define-button-type'.
|
|
278
|
|
279 This function is like `insert-button', except that the button is
|
|
280 actually part of the text instead of being a property of the buffer.
|
|
281 Creating large numbers of buttons can also be somewhat faster using
|
|
282 `insert-text-button'.
|
|
283
|
|
284 Also see `make-text-button'."
|
|
285 (apply #'make-text-button
|
|
286 (prog1 (point) (insert label))
|
|
287 (point)
|
|
288 properties))
|
|
289
|
|
290
|
|
291 ;; Finding buttons in a buffer
|
|
292
|
|
293 (defun button-at (pos)
|
|
294 "Return the button at position POS in the current buffer, or nil."
|
|
295 (let ((button (get-char-property pos 'button)))
|
|
296 (if (or (overlayp button) (null button))
|
|
297 button
|
|
298 ;; Must be a text-property button; return a marker pointing to it.
|
|
299 (copy-marker pos t))))
|
|
300
|
|
301 (defun next-button (pos &optional n wrap count-current)
|
|
302 "Return the Nth button after position POS in the current buffer.
|
|
303 If N is negative, return the Nth button before POS.
|
|
304 If no Nth button is found, return nil.
|
|
305 If WRAP is non-nil, the search wraps around at the end of the buffer.
|
|
306 If COUNT-CURRENT is non-nil, count any button at POS in the search,
|
|
307 instead of starting at the next button."
|
|
308 (when (null n)
|
|
309 (setq n 1))
|
|
310 (if (< n 0)
|
|
311 ;; reverse direction
|
|
312 (previous-button pos (- n) wrap)
|
|
313 (unless count-current
|
|
314 ;; Search for the next button boundary.
|
|
315 (setq pos (next-single-char-property-change pos 'button)))
|
|
316 (let ((button (button-at pos)))
|
|
317 (cond ((and button (>= n 2))
|
|
318 ;; Found a button, but we want a different one; recurse.
|
|
319 (next-button (button-start button) (1- n) wrap))
|
|
320 (button
|
|
321 ;; This is the button we want.
|
|
322 button)
|
|
323 ((= pos (point-max))
|
|
324 ;; Failed to find a button going forwards, either wrap or
|
|
325 ;; return failure.
|
|
326 (and wrap (next-button (point-min) n nil t)))
|
|
327 (t
|
|
328 ;; We must have originally been on a button, and are now in
|
|
329 ;; the inter-button space. Recurse to find a button.
|
|
330 (next-button pos n wrap))))))
|
|
331
|
|
332 (defun previous-button (pos &optional n wrap count-current)
|
|
333 "Return the Nth button before position POS in the current buffer.
|
|
334 If N is negative, return the Nth button after POS.
|
|
335 If no Nth button is found, return nil.
|
|
336 If WRAP is non-nil, the search wraps around at the beginning of the buffer.
|
|
337 If COUNT-CURRENT is non-nil, count any button at POS in the search,
|
|
338 instead of starting at the next button."
|
|
339 (when (null n)
|
|
340 (setq n 1))
|
|
341 (if (< n 0)
|
|
342 ;; reverse direction
|
|
343 (next-button pos (- n) wrap)
|
|
344 (unless count-current
|
|
345 (setq pos (previous-single-char-property-change pos 'button)))
|
|
346 (let ((button (and (> pos (point-min)) (button-at (1- pos)))))
|
|
347 (cond ((and button (>= n 2))
|
|
348 ;; Found a button, but we want a different one; recurse.
|
|
349 (previous-button (button-start button) (1- n) wrap))
|
|
350 (button
|
|
351 ;; This is the button we want.
|
|
352 button)
|
|
353 ((= pos (point-min))
|
|
354 ;; Failed to find a button going backwards, either wrap
|
|
355 ;; or return failure.
|
|
356 (and wrap (previous-button (point-max) n nil t)))
|
|
357 (t
|
|
358 ;; We must have originally been on a button, and are now in
|
|
359 ;; the inter-button space. Recurse to find a button.
|
|
360 (previous-button pos (max n 1) wrap))))))
|
|
361
|
|
362
|
|
363 ;; User commands
|
|
364
|
|
365 (defun push-button (&optional pos)
|
|
366 "Perform the action specified by a button at location POS.
|
|
367 POS may be either a buffer position or a mouse-event.
|
|
368 POS defaults to point, except when `push-button' is invoked
|
|
369 interactively as the result of a mouse-event, in which case, the
|
|
370 mouse event is used.
|
|
371 If there's no button at POS, do nothing and return nil, otherwise
|
|
372 return t."
|
|
373 (interactive
|
|
374 (list (if (integerp last-command-event) (point) last-command-event)))
|
|
375 (if (and (not (integerp pos)) (eventp pos))
|
|
376 ;; POS is a mouse event; switch to the proper window/buffer
|
|
377 (let ((posn (event-start pos)))
|
|
378 (with-current-buffer (window-buffer (posn-window posn))
|
|
379 (push-button (posn-point posn))))
|
|
380 ;; POS is just normal position
|
|
381 (let ((button (button-at (or pos (point)))))
|
|
382 (if (not button)
|
|
383 nil
|
|
384 (button-activate button)
|
|
385 t))))
|
|
386
|
|
387 (defun forward-button (n &optional wrap display-message)
|
|
388 "Move to the Nth next button, or Nth previous button if N is negative.
|
|
389 If WRAP is non-nil, moving past either end of the buffer continues from the
|
|
390 other end.
|
|
391 If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
|
|
392 Returns the button found."
|
|
393 (interactive "p\nd\nd")
|
|
394 (let ((button (next-button (point) n wrap)))
|
|
395 (if (null button)
|
|
396 (error (if wrap "No buttons!" "No more buttons"))
|
|
397 (goto-char (button-start button))
|
|
398 (let ((msg (and display-message (button-get button 'help-echo))))
|
|
399 (when msg
|
|
400 (message "%s" msg)))
|
|
401 button)))
|
|
402
|
|
403 (defun backward-button (n &optional wrap display-message)
|
|
404 "Move to the Nth previous button, or Nth next button if N is negative.
|
|
405 If WRAP is non-nil, moving past either end of the buffer continues from the
|
|
406 other end.
|
|
407 If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
|
|
408 Returns the button found."
|
|
409 (interactive "p\nd\nd")
|
|
410 (forward-button (- n) wrap display-message))
|
|
411
|
|
412
|
|
413 (provide 'button)
|
|
414
|
|
415 ;;; button.el ends here
|