*** empty log message ***
[bpt/emacs.git] / lisp / button.el
CommitLineData
0c26f463
MB
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\f
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.
71Mode-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)
62dc75c4 78(put 'default-button 'action 'ignore)
0c26f463
MB
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
530893b2
MB
89;; A `category-symbol' property for the default button type
90(put 'button 'button-category-symbol 'default-button)
91
0c26f463
MB
92\f
93;; Button types (which can be used to hold default properties for buttons)
94
ded42dd3
MB
95;; Because button-type properties are inherited by buttons using the
96;; special `category' property (implemented by both overlays and
97;; text-properties), we need to store them on a symbol to which the
98;; `category' properties can point. Instead of using the symbol that's
99;; the name of each button-type, however, we use a separate symbol (with
100;; `-button' appended, and uninterned) to store the properties. This is
101;; to avoid name clashes.
102
103;; [this is an internal function]
104(defsubst button-category-symbol (type)
105 "Return the symbol used by button-type TYPE to store properties.
106Buttons inherit them by setting their `category' property to that symbol."
107 (or (get type 'button-category-symbol)
108 (error "Unknown button type `%s'" type)))
109
0c26f463
MB
110;;;###autoload
111(defun define-button-type (name &rest properties)
112 "Define a `button type' called NAME.
113The remaining arguments form a sequence of PROPERTY VALUE pairs,
114specifying properties to use as defaults for buttons with this type
115\(a button's type may be set by giving it a `type' property when
ded42dd3
MB
116creating the button).
117
118The property `supertype' may be used to specify a button-type from which
119NAME inherits its default property values \(however, the inheritance
120happens only when NAME is defined; subsequent changes to a supertype are
121not reflected in its subtypes)."
122 (let* ((catsym (make-symbol (concat (symbol-name name) "-button")))
530893b2
MB
123 (supertype
124 (or (plist-get properties 'supertype)
125 (plist-get properties :supertype)))
ded42dd3
MB
126 (super-catsym
127 (if supertype (button-category-symbol supertype) 'default-button)))
0c26f463
MB
128 ;; Provide a link so that it's easy to find the real symbol.
129 (put name 'button-category-symbol catsym)
130 ;; Initialize NAME's properties using the global defaults.
ded42dd3 131 (let ((default-props (symbol-plist super-catsym)))
0c26f463
MB
132 (while default-props
133 (put catsym (pop default-props) (pop default-props))))
134 ;; Add NAME as the `type' property, which will then be returned as
135 ;; the type property of individual buttons.
136 (put catsym 'type name)
137 ;; Add the properties in PROPERTIES to the real symbol.
138 (while properties
530893b2
MB
139 (let ((prop (pop properties)))
140 (when (eq prop :supertype)
141 (setq prop 'supertype))
142 (put catsym prop (pop properties))))
0c26f463
MB
143 name))
144
0c26f463
MB
145(defun button-type-put (type prop val)
146 "Set the button-type TYPE's PROP property to VAL."
147 (put (button-category-symbol type) prop val))
148
149(defun button-type-get (type prop)
150 "Get the property of button-type TYPE named PROP."
151 (get (button-category-symbol type) prop))
152
ded42dd3
MB
153(defun button-type-subtype-p (type supertype)
154 "Return t if button-type TYPE is a subtype of SUPERTYPE."
155 (or (eq type supertype)
156 (and type
157 (button-type-subtype-p (button-type-get type 'supertype)
158 supertype))))
159
0c26f463
MB
160\f
161;; Button properties and other attributes
162
163(defun button-start (button)
164 "Return the position at which BUTTON starts."
165 (if (overlayp button)
166 (overlay-start button)
167 ;; Must be a text-property button.
168 (or (previous-single-property-change (1+ button) 'button)
169 (point-min))))
170
171(defun button-end (button)
172 "Return the position at which BUTTON ends."
173 (if (overlayp button)
174 (overlay-end button)
175 ;; Must be a text-property button.
176 (or (next-single-property-change button 'button)
177 (point-max))))
178
179(defun button-get (button prop)
180 "Get the property of button BUTTON named PROP."
181 (if (overlayp button)
182 (overlay-get button prop)
183 ;; Must be a text-property button.
184 (get-text-property button prop)))
185
186(defun button-put (button prop val)
187 "Set BUTTON's PROP property to VAL."
188 ;; Treat some properties specially.
530893b2 189 (cond ((memq prop '(type :type))
0c26f463
MB
190 ;; We translate a `type' property a `category' property, since
191 ;; that's what's actually used by overlays/text-properties for
192 ;; inheriting properties.
193 (setq prop 'category)
194 (setq val (button-category-symbol val)))
195 ((eq prop 'category)
196 ;; Disallow updating the `category' property directly.
197 (error "Button `category' property may not be set directly")))
198 ;; Add the property.
199 (if (overlayp button)
200 (overlay-put button prop val)
201 ;; Must be a text-property button.
202 (put-text-property
203 (or (previous-single-property-change (1+ button) 'button)
204 (point-min))
205 (or (next-single-property-change button 'button)
206 (point-max))
207 prop val)))
208
1e00f720 209(defsubst button-activate (button &optional use-mouse-action)
d6bc0bdc
MB
210 "Call BUTTON's action property.
211If USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
212instead of its normal action; if the button has no mouse-action,
213the normal action is used instead."
214 (funcall (or (and use-mouse-action (button-get button 'mouse-action))
215 (button-get button 'action))
216 button))
0c26f463
MB
217
218(defun button-label (button)
219 "Return BUTTON's text label."
220 (buffer-substring-no-properties (button-start button) (button-end button)))
221
530893b2
MB
222(defsubst button-type (button)
223 (button-get button 'type))
224
ded42dd3
MB
225(defun button-has-type-p (button type)
226 "Return t if BUTTON has button-type TYPE, or one of TYPE's subtypes."
227 (button-type-subtype-p (button-get button 'type) type))
228
0c26f463
MB
229\f
230;; Creating overlay buttons
231
232;;;###autoload
233(defun make-button (beg end &rest properties)
234 "Make a button from BEG to END in the current buffer.
235The remaining arguments form a sequence of PROPERTY VALUE pairs,
236specifying properties to add to the button. In particular, the `type'
237property may be used to specify a button-type from which to inherit
238other properties; see `define-button-type'.
239
240Also see `make-text-button', `insert-button'."
241 (let ((overlay (make-overlay beg end nil t nil)))
242 (while properties
243 (button-put overlay (pop properties) (pop properties)))
244 ;; Put a pointer to the button in the overlay, so it's easy to get
245 ;; when we don't actually have a reference to the overlay.
246 (overlay-put overlay 'button overlay)
247 ;; If the user didn't specify a type, use the default.
248 (unless (overlay-get overlay 'category)
249 (overlay-put overlay 'category 'default-button))
250 ;; OVERLAY is the button, so return it
251 overlay))
252
253;;;###autoload
254(defun insert-button (label &rest properties)
255 "Insert a button with the label LABEL.
256The remaining arguments form a sequence of PROPERTY VALUE pairs,
257specifying properties to add to the button. In particular, the `type'
258property may be used to specify a button-type from which to inherit
259other properties; see `define-button-type'.
260
261Also see `insert-text-button', `make-button'."
262 (apply #'make-button
263 (prog1 (point) (insert label))
264 (point)
265 properties))
266
267\f
268;; Creating text-property buttons
269
270;;;###autoload
271(defun make-text-button (beg end &rest properties)
272 "Make a button from BEG to END in the current buffer.
273The remaining arguments form a sequence of PROPERTY VALUE pairs,
274specifying properties to add to the button. In particular, the `type'
275property may be used to specify a button-type from which to inherit
276other properties; see `define-button-type'.
277
278This function is like `make-button', except that the button is actually
279part of the text instead of being a property of the buffer. Creating
280large numbers of buttons can also be somewhat faster using
281`make-text-button'.
282
283Also see `insert-text-button'."
284 (let (prop val)
285 (while properties
286 (setq prop (pop properties))
287 (setq val (pop properties))
288 ;; Note that all the following code is basically equivalent to
289 ;; `button-put', but we can do it much more efficiently since we
290 ;; already have BEG and END.
530893b2 291 (cond ((memq prop '(type :type))
0c26f463
MB
292 ;; We translate a `type' property into a `category'
293 ;; property, since that's what's actually used by
294 ;; text-properties for inheritance.
295 (setq prop 'category)
296 (setq val (button-category-symbol val)))
297 ((eq prop 'category)
298 ;; Disallow setting the `category' property directly.
299 (error "Button `category' property may not be set directly")))
300 ;; Add the property.
301 (put-text-property beg end prop val)))
302 ;; Return something that can be used to get at the button.
303 beg)
304
305;;;###autoload
306(defun insert-text-button (label &rest properties)
307 "Insert a button with the label LABEL.
308The remaining arguments form a sequence of PROPERTY VALUE pairs,
309specifying properties to add to the button. In particular, the `type'
310property may be used to specify a button-type from which to inherit
311other properties; see `define-button-type'.
312
313This function is like `insert-button', except that the button is
314actually part of the text instead of being a property of the buffer.
315Creating large numbers of buttons can also be somewhat faster using
316`insert-text-button'.
317
318Also see `make-text-button'."
319 (apply #'make-text-button
320 (prog1 (point) (insert label))
321 (point)
322 properties))
323
324\f
325;; Finding buttons in a buffer
326
327(defun button-at (pos)
328 "Return the button at position POS in the current buffer, or nil."
329 (let ((button (get-char-property pos 'button)))
330 (if (or (overlayp button) (null button))
331 button
332 ;; Must be a text-property button; return a marker pointing to it.
333 (copy-marker pos t))))
334
fee34a28
MB
335(defun next-button (pos &optional count-current)
336 "Return the next button after position POS in the current buffer.
0c26f463 337If COUNT-CURRENT is non-nil, count any button at POS in the search,
fee34a28 338instead of starting at the next button."
0c26f463
MB
339 (unless count-current
340 ;; Search for the next button boundary.
341 (setq pos (next-single-char-property-change pos 'button)))
fee34a28
MB
342 (and (< pos (point-max))
343 (or (button-at pos)
0c26f463
MB
344 ;; We must have originally been on a button, and are now in
345 ;; the inter-button space. Recurse to find a button.
fee34a28 346 (next-button pos))))
0c26f463 347
fee34a28 348(defun previous-button (pos &optional count-current)
0c26f463 349 "Return the Nth button before position POS in the current buffer.
0c26f463 350If COUNT-CURRENT is non-nil, count any button at POS in the search,
fee34a28
MB
351instead of starting at the next button."
352 (unless count-current
353 (setq pos (previous-single-char-property-change pos 'button)))
354 (and (> pos (point-min))
355 (or (button-at (1- pos))
356 ;; We must have originally been on a button, and are now in
357 ;; the inter-button space. Recurse to find a button.
358 (previous-button pos))))
0c26f463
MB
359
360\f
361;; User commands
362
d6bc0bdc 363(defun push-button (&optional pos use-mouse-action)
0c26f463
MB
364 "Perform the action specified by a button at location POS.
365POS may be either a buffer position or a mouse-event.
d6bc0bdc
MB
366If USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
367instead of its normal action; if the button has no mouse-action,
368the normal action is used instead.
0c26f463
MB
369POS defaults to point, except when `push-button' is invoked
370interactively as the result of a mouse-event, in which case, the
371mouse event is used.
372If there's no button at POS, do nothing and return nil, otherwise
373return t."
374 (interactive
375 (list (if (integerp last-command-event) (point) last-command-event)))
376 (if (and (not (integerp pos)) (eventp pos))
377 ;; POS is a mouse event; switch to the proper window/buffer
378 (let ((posn (event-start pos)))
379 (with-current-buffer (window-buffer (posn-window posn))
d6bc0bdc 380 (push-button (posn-point posn) t)))
0c26f463
MB
381 ;; POS is just normal position
382 (let ((button (button-at (or pos (point)))))
383 (if (not button)
384 nil
d6bc0bdc 385 (button-activate button use-mouse-action)
0c26f463
MB
386 t))))
387
388(defun forward-button (n &optional wrap display-message)
389 "Move to the Nth next button, or Nth previous button if N is negative.
fee34a28 390If N is 0, move to the start of any button at point.
0c26f463
MB
391If WRAP is non-nil, moving past either end of the buffer continues from the
392other end.
393If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
fee34a28 394Any button with a non-nil `skip' property is skipped over.
0c26f463
MB
395Returns the button found."
396 (interactive "p\nd\nd")
fee34a28
MB
397 (let (button)
398 (if (zerop n)
399 ;; Move to start of current button
400 (if (setq button (button-at (point)))
401 (goto-char (button-start button)))
402 ;; Move to Nth next button
403 (let ((iterator (if (> n 0) #'next-button #'previous-button))
404 (wrap-start (if (> n 0) (point-min) (point-max))))
405 (setq n (abs n))
406 (setq button t) ; just to start the loop
407 (while (and (> n 0) button)
408 (setq button (funcall iterator (point)))
409 (when (and (not button) wrap)
410 (setq button (funcall iterator wrap-start t)))
411 (when button
412 (goto-char (button-start button))
413 (unless (button-get button 'skip)
414 (setq n (1- n)))))))
0c26f463
MB
415 (if (null button)
416 (error (if wrap "No buttons!" "No more buttons"))
0c26f463
MB
417 (let ((msg (and display-message (button-get button 'help-echo))))
418 (when msg
419 (message "%s" msg)))
420 button)))
421
422(defun backward-button (n &optional wrap display-message)
423 "Move to the Nth previous button, or Nth next button if N is negative.
fee34a28 424If N is 0, move to the start of any button at point.
0c26f463
MB
425If WRAP is non-nil, moving past either end of the buffer continues from the
426other end.
427If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
fee34a28 428Any button with a non-nil `skip' property is skipped over.
0c26f463
MB
429Returns the button found."
430 (interactive "p\nd\nd")
431 (forward-button (- n) wrap display-message))
432
433
434(provide 'button)
435
436;;; button.el ends here