Merge from trunk
[bpt/emacs.git] / lisp / button.el
CommitLineData
8e2b057d 1;;; button.el --- clickable buttons
0c26f463 2;;
73b0cd50 3;; Copyright (C) 2001-2011 Free Software Foundation, Inc.
0c26f463
MB
4;;
5;; Author: Miles Bader <miles@gnu.org>
6;; Keywords: extensions
bd78fa1d 7;; Package: emacs
0c26f463
MB
8;;
9;; This file is part of GNU Emacs.
10;;
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
0c26f463 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
0c26f463
MB
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
eb3fa2cf 20
0c26f463 21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
0c26f463
MB
23
24;;; Commentary:
25;;
26;; This package defines functions for inserting and manipulating
27;; clickable buttons in Emacs buffers, such as might be used for help
28;; hyperlinks, etc.
29;;
30;; In some ways it duplicates functionality also offered by the
31;; `widget' package, but the button package has the advantage that it
32;; is (1) much faster, (2) much smaller, and (3) much, much, simpler
33;; (the code, that is, not the interface).
34;;
35;; Buttons can either use overlays, in which case the button is
36;; represented by the overlay itself, or text-properties, in which case
37;; the button is represented by a marker or buffer-position pointing
38;; somewhere in the button. In the latter case, no markers into the
39;; buffer are retained, which is important for speed if there are are
4d265b4d
GM
40;; extremely large numbers of buttons. Note however that if there is
41;; an existing face text-property at the site of the button, the
42;; button face may not be visible. Using overlays avoids this.
0c26f463
MB
43;;
44;; Using `define-button-type' to define default properties for buttons
45;; is not necessary, but it is is encouraged, since doing so makes the
46;; resulting code clearer and more efficient.
47;;
48
49;;; Code:
50
51\f
52;; Globals
53
0e0d5683 54;; Use color for the MS-DOS port because it doesn't support underline.
f8ac059a
GM
55;; FIXME if MS-DOS correctly answers the (supports) question, it need
56;; no longer be a special case.
35136493 57(defface button '((((type pc) (class color))
f2e24189 58 (:foreground "lightblue"))
f8ac059a
GM
59 (((supports :underline t)) :underline t)
60 (t (:foreground "lightblue")))
d28242e1 61 "Default face used for buttons."
21b9448f 62 :group 'basic-faces)
0c26f463 63
0c26f463
MB
64(defvar button-map
65 (let ((map (make-sparse-keymap)))
2b786e07
JR
66 ;; The following definition needs to avoid using escape sequences that
67 ;; might get converted to ^M when building loaddefs.el
68 (define-key map [(control ?m)] 'push-button)
0c26f463
MB
69 (define-key map [mouse-2] 'push-button)
70 map)
71 "Keymap used by buttons.")
72
0c26f463
MB
73(defvar button-buffer-map
74 (let ((map (make-sparse-keymap)))
75 (define-key map [?\t] 'forward-button)
272199ae 76 (define-key map "\e\t" 'backward-button)
0c26f463
MB
77 (define-key map [backtab] 'backward-button)
78 map)
79 "Keymap useful for buffers containing buttons.
80Mode-specific keymaps may want to use this as their parent keymap.")
81
82;; Default properties for buttons
83(put 'default-button 'face 'button)
84(put 'default-button 'mouse-face 'highlight)
85(put 'default-button 'keymap button-map)
86(put 'default-button 'type 'button)
bea55b5b 87;; action may be either a function to call, or a marker to go to
62dc75c4 88(put 'default-button 'action 'ignore)
1e8780b1 89(put 'default-button 'help-echo (purecopy "mouse-2, RET: Push this button"))
0c26f463
MB
90;; Make overlay buttons go away if their underlying text is deleted.
91(put 'default-button 'evaporate t)
92;; Prevent insertions adjacent to the text-property buttons from
93;; inheriting its properties.
94(put 'default-button 'rear-nonsticky t)
0c26f463 95
530893b2
MB
96;; A `category-symbol' property for the default button type
97(put 'button 'button-category-symbol 'default-button)
98
0c26f463
MB
99\f
100;; Button types (which can be used to hold default properties for buttons)
101
ded42dd3
MB
102;; Because button-type properties are inherited by buttons using the
103;; special `category' property (implemented by both overlays and
104;; text-properties), we need to store them on a symbol to which the
105;; `category' properties can point. Instead of using the symbol that's
106;; the name of each button-type, however, we use a separate symbol (with
107;; `-button' appended, and uninterned) to store the properties. This is
108;; to avoid name clashes.
109
110;; [this is an internal function]
111(defsubst button-category-symbol (type)
112 "Return the symbol used by button-type TYPE to store properties.
113Buttons inherit them by setting their `category' property to that symbol."
114 (or (get type 'button-category-symbol)
115 (error "Unknown button type `%s'" type)))
116
0c26f463 117(defun define-button-type (name &rest properties)
b9712b1c 118 "Define a `button type' called NAME (a symbol).
0c26f463
MB
119The remaining arguments form a sequence of PROPERTY VALUE pairs,
120specifying properties to use as defaults for buttons with this type
121\(a button's type may be set by giving it a `type' property when
ecdbe16a 122creating the button, using the :type keyword argument).
ded42dd3 123
ecdbe16a
MB
124In addition, the keyword argument :supertype may be used to specify a
125button-type from which NAME inherits its default property values
126\(however, the inheritance happens only when NAME is defined; subsequent
127changes to a supertype are not reflected in its subtypes)."
588c722f
MB
128 (let ((catsym (make-symbol (concat (symbol-name name) "-button")))
129 (super-catsym
130 (button-category-symbol
530893b2 131 (or (plist-get properties 'supertype)
588c722f
MB
132 (plist-get properties :supertype)
133 'button))))
0c26f463
MB
134 ;; Provide a link so that it's easy to find the real symbol.
135 (put name 'button-category-symbol catsym)
136 ;; Initialize NAME's properties using the global defaults.
ded42dd3 137 (let ((default-props (symbol-plist super-catsym)))
0c26f463
MB
138 (while default-props
139 (put catsym (pop default-props) (pop default-props))))
140 ;; Add NAME as the `type' property, which will then be returned as
141 ;; the type property of individual buttons.
142 (put catsym 'type name)
143 ;; Add the properties in PROPERTIES to the real symbol.
144 (while properties
530893b2
MB
145 (let ((prop (pop properties)))
146 (when (eq prop :supertype)
147 (setq prop 'supertype))
148 (put catsym prop (pop properties))))
588c722f
MB
149 ;; Make sure there's a `supertype' property
150 (unless (get catsym 'supertype)
151 (put catsym 'supertype 'button))
0c26f463
MB
152 name))
153
0c26f463
MB
154(defun button-type-put (type prop val)
155 "Set the button-type TYPE's PROP property to VAL."
156 (put (button-category-symbol type) prop val))
157
158(defun button-type-get (type prop)
159 "Get the property of button-type TYPE named PROP."
160 (get (button-category-symbol type) prop))
161
ded42dd3
MB
162(defun button-type-subtype-p (type supertype)
163 "Return t if button-type TYPE is a subtype of SUPERTYPE."
164 (or (eq type supertype)
165 (and type
166 (button-type-subtype-p (button-type-get type 'supertype)
167 supertype))))
168
0c26f463
MB
169\f
170;; Button properties and other attributes
171
172(defun button-start (button)
173 "Return the position at which BUTTON starts."
174 (if (overlayp button)
175 (overlay-start button)
176 ;; Must be a text-property button.
177 (or (previous-single-property-change (1+ button) 'button)
178 (point-min))))
179
180(defun button-end (button)
181 "Return the position at which BUTTON ends."
182 (if (overlayp button)
183 (overlay-end button)
184 ;; Must be a text-property button.
185 (or (next-single-property-change button 'button)
186 (point-max))))
187
188(defun button-get (button prop)
189 "Get the property of button BUTTON named PROP."
190 (if (overlayp button)
191 (overlay-get button prop)
192 ;; Must be a text-property button.
193 (get-text-property button prop)))
194
195(defun button-put (button prop val)
196 "Set BUTTON's PROP property to VAL."
197 ;; Treat some properties specially.
530893b2 198 (cond ((memq prop '(type :type))
0c26f463
MB
199 ;; We translate a `type' property a `category' property, since
200 ;; that's what's actually used by overlays/text-properties for
201 ;; inheriting properties.
202 (setq prop 'category)
203 (setq val (button-category-symbol val)))
204 ((eq prop 'category)
205 ;; Disallow updating the `category' property directly.
206 (error "Button `category' property may not be set directly")))
207 ;; Add the property.
208 (if (overlayp button)
209 (overlay-put button prop val)
210 ;; Must be a text-property button.
211 (put-text-property
212 (or (previous-single-property-change (1+ button) 'button)
213 (point-min))
214 (or (next-single-property-change button 'button)
215 (point-max))
216 prop val)))
217
1e00f720 218(defsubst button-activate (button &optional use-mouse-action)
d6bc0bdc
MB
219 "Call BUTTON's action property.
220If USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
221instead of its normal action; if the button has no mouse-action,
222the normal action is used instead."
bea55b5b
DP
223 (let ((action (or (and use-mouse-action (button-get button 'mouse-action))
224 (button-get button 'action))))
225 (if (markerp action)
226 (save-selected-window
227 (select-window (display-buffer (marker-buffer action)))
228 (goto-char action)
229 (recenter 0))
230 (funcall action button))))
0c26f463
MB
231
232(defun button-label (button)
233 "Return BUTTON's text label."
234 (buffer-substring-no-properties (button-start button) (button-end button)))
235
530893b2 236(defsubst button-type (button)
ecdbe16a 237 "Return BUTTON's button-type."
530893b2
MB
238 (button-get button 'type))
239
ded42dd3
MB
240(defun button-has-type-p (button type)
241 "Return t if BUTTON has button-type TYPE, or one of TYPE's subtypes."
242 (button-type-subtype-p (button-get button 'type) type))
243
0c26f463
MB
244\f
245;; Creating overlay buttons
246
0c26f463
MB
247(defun make-button (beg end &rest properties)
248 "Make a button from BEG to END in the current buffer.
249The remaining arguments form a sequence of PROPERTY VALUE pairs,
ecdbe16a
MB
250specifying properties to add to the button.
251In addition, the keyword argument :type may be used to specify a
252button-type from which to inherit other properties; see
253`define-button-type'.
0c26f463
MB
254
255Also see `make-text-button', `insert-button'."
256 (let ((overlay (make-overlay beg end nil t nil)))
257 (while properties
258 (button-put overlay (pop properties) (pop properties)))
259 ;; Put a pointer to the button in the overlay, so it's easy to get
260 ;; when we don't actually have a reference to the overlay.
261 (overlay-put overlay 'button overlay)
262 ;; If the user didn't specify a type, use the default.
263 (unless (overlay-get overlay 'category)
264 (overlay-put overlay 'category 'default-button))
265 ;; OVERLAY is the button, so return it
266 overlay))
267
0c26f463
MB
268(defun insert-button (label &rest properties)
269 "Insert a button with the label LABEL.
270The remaining arguments form a sequence of PROPERTY VALUE pairs,
ecdbe16a
MB
271specifying properties to add to the button.
272In addition, the keyword argument :type may be used to specify a
273button-type from which to inherit other properties; see
274`define-button-type'.
0c26f463
MB
275
276Also see `insert-text-button', `make-button'."
277 (apply #'make-button
278 (prog1 (point) (insert label))
279 (point)
280 properties))
281
282\f
283;; Creating text-property buttons
284
0c26f463
MB
285(defun make-text-button (beg end &rest properties)
286 "Make a button from BEG to END in the current buffer.
287The remaining arguments form a sequence of PROPERTY VALUE pairs,
ecdbe16a
MB
288specifying properties to add to the button.
289In addition, the keyword argument :type may be used to specify a
290button-type from which to inherit other properties; see
291`define-button-type'.
0c26f463
MB
292
293This function is like `make-button', except that the button is actually
4d265b4d
GM
294part of the text instead of being a property of the buffer. That is,
295this function uses text properties, the other uses overlays.
296Creating large numbers of buttons can also be somewhat faster
297using `make-text-button'. Note, however, that if there is an existing
298face property at the site of the button, the button face may not be visible.
299You may want to use `make-button' in that case.
0c26f463 300
12b139e9
SM
301BEG can also be a string, in which case it is made into a button.
302
0c26f463 303Also see `insert-text-button'."
12b139e9
SM
304 (let ((object nil)
305 (type-entry
aad1926a
MB
306 (or (plist-member properties 'type)
307 (plist-member properties :type))))
12b139e9
SM
308 (when (stringp beg)
309 (setq object beg beg 0 end (length object)))
aad1926a
MB
310 ;; Disallow setting the `category' property directly.
311 (when (plist-get properties 'category)
312 (error "Button `category' property may not be set directly"))
313 (if (null type-entry)
314 ;; The user didn't specify a `type' property, use the default.
315 (setq properties (cons 'category (cons 'default-button properties)))
316 ;; The user did specify a `type' property. Translate it into a
317 ;; `category' property, which is what's actually used by
318 ;; text-properties for inheritance.
319 (setcar type-entry 'category)
320 (setcar (cdr type-entry)
12b139e9
SM
321 (button-category-symbol (car (cdr type-entry)))))
322 ;; Now add all the text properties at once
323 (add-text-properties beg end
324 ;; Each button should have a non-eq `button'
325 ;; property so that next-single-property-change can
326 ;; detect boundaries reliably.
327 (cons 'button (cons (list t) properties))
328 object)
329 ;; Return something that can be used to get at the button.
330 beg))
0c26f463 331
0c26f463
MB
332(defun insert-text-button (label &rest properties)
333 "Insert a button with the label LABEL.
334The remaining arguments form a sequence of PROPERTY VALUE pairs,
ecdbe16a
MB
335specifying properties to add to the button.
336In addition, the keyword argument :type may be used to specify a
337button-type from which to inherit other properties; see
338`define-button-type'.
0c26f463
MB
339
340This function is like `insert-button', except that the button is
341actually part of the text instead of being a property of the buffer.
342Creating large numbers of buttons can also be somewhat faster using
343`insert-text-button'.
344
345Also see `make-text-button'."
346 (apply #'make-text-button
347 (prog1 (point) (insert label))
348 (point)
349 properties))
350
351\f
352;; Finding buttons in a buffer
353
354(defun button-at (pos)
355 "Return the button at position POS in the current buffer, or nil."
356 (let ((button (get-char-property pos 'button)))
357 (if (or (overlayp button) (null button))
358 button
359 ;; Must be a text-property button; return a marker pointing to it.
360 (copy-marker pos t))))
361
fee34a28
MB
362(defun next-button (pos &optional count-current)
363 "Return the next button after position POS in the current buffer.
0c26f463 364If COUNT-CURRENT is non-nil, count any button at POS in the search,
fee34a28 365instead of starting at the next button."
0c26f463
MB
366 (unless count-current
367 ;; Search for the next button boundary.
368 (setq pos (next-single-char-property-change pos 'button)))
fee34a28
MB
369 (and (< pos (point-max))
370 (or (button-at pos)
0c26f463
MB
371 ;; We must have originally been on a button, and are now in
372 ;; the inter-button space. Recurse to find a button.
fee34a28 373 (next-button pos))))
0c26f463 374
fee34a28 375(defun previous-button (pos &optional count-current)
1106c41b 376 "Return the previous button before position POS in the current buffer.
0c26f463 377If COUNT-CURRENT is non-nil, count any button at POS in the search,
fee34a28 378instead of starting at the next button."
1106c41b
CY
379 (let ((button (button-at pos)))
380 (if button
381 (if count-current
382 button
383 ;; We started out on a button, so move to its start and look
384 ;; for the previous button boundary.
385 (setq pos (previous-single-char-property-change
386 (button-start button) 'button))
387 (let ((new-button (button-at pos)))
388 (if new-button
389 ;; We are in a button again; this can happen if there
390 ;; are adjacent buttons (or at bob).
6d0a6f30 391 (unless (= pos (button-start button)) new-button)
1106c41b
CY
392 ;; We are now in the space between buttons.
393 (previous-button pos))))
394 ;; We started out in the space between buttons.
395 (setq pos (previous-single-char-property-change pos 'button))
396 (or (button-at pos)
397 (and (> pos (point-min))
398 (button-at (1- pos)))))))
0c26f463
MB
399
400\f
401;; User commands
402
d6bc0bdc 403(defun push-button (&optional pos use-mouse-action)
0c26f463 404 "Perform the action specified by a button at location POS.
bea55b5b
DP
405POS may be either a buffer position or a mouse-event. If
406USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
d6bc0bdc 407instead of its normal action; if the button has no mouse-action,
bea55b5b
DP
408the normal action is used instead. The action may be either a
409function to call or a marker to display.
0c26f463
MB
410POS defaults to point, except when `push-button' is invoked
411interactively as the result of a mouse-event, in which case, the
412mouse event is used.
413If there's no button at POS, do nothing and return nil, otherwise
414return t."
415 (interactive
416 (list (if (integerp last-command-event) (point) last-command-event)))
417 (if (and (not (integerp pos)) (eventp pos))
418 ;; POS is a mouse event; switch to the proper window/buffer
419 (let ((posn (event-start pos)))
420 (with-current-buffer (window-buffer (posn-window posn))
d6bc0bdc 421 (push-button (posn-point posn) t)))
0c26f463
MB
422 ;; POS is just normal position
423 (let ((button (button-at (or pos (point)))))
424 (if (not button)
425 nil
d6bc0bdc 426 (button-activate button use-mouse-action)
0c26f463
MB
427 t))))
428
429(defun forward-button (n &optional wrap display-message)
430 "Move to the Nth next button, or Nth previous button if N is negative.
fee34a28 431If N is 0, move to the start of any button at point.
0c26f463
MB
432If WRAP is non-nil, moving past either end of the buffer continues from the
433other end.
434If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
fee34a28 435Any button with a non-nil `skip' property is skipped over.
0c26f463
MB
436Returns the button found."
437 (interactive "p\nd\nd")
fee34a28
MB
438 (let (button)
439 (if (zerop n)
440 ;; Move to start of current button
441 (if (setq button (button-at (point)))
442 (goto-char (button-start button)))
443 ;; Move to Nth next button
444 (let ((iterator (if (> n 0) #'next-button #'previous-button))
f46a3642
CY
445 (wrap-start (if (> n 0) (point-min) (point-max)))
446 opoint fail)
fee34a28
MB
447 (setq n (abs n))
448 (setq button t) ; just to start the loop
f46a3642 449 (while (and (null fail) (> n 0) button)
fee34a28
MB
450 (setq button (funcall iterator (point)))
451 (when (and (not button) wrap)
452 (setq button (funcall iterator wrap-start t)))
453 (when button
454 (goto-char (button-start button))
f46a3642
CY
455 ;; Avoid looping forever (e.g., if all the buttons have
456 ;; the `skip' property).
457 (cond ((null opoint)
458 (setq opoint (point)))
459 ((= opoint (point))
460 (setq fail t)))
fee34a28
MB
461 (unless (button-get button 'skip)
462 (setq n (1- n)))))))
0c26f463
MB
463 (if (null button)
464 (error (if wrap "No buttons!" "No more buttons"))
0c26f463
MB
465 (let ((msg (and display-message (button-get button 'help-echo))))
466 (when msg
467 (message "%s" msg)))
468 button)))
469
470(defun backward-button (n &optional wrap display-message)
471 "Move to the Nth previous button, or Nth next button if N is negative.
fee34a28 472If N is 0, move to the start of any button at point.
0c26f463
MB
473If WRAP is non-nil, moving past either end of the buffer continues from the
474other end.
475If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
fee34a28 476Any button with a non-nil `skip' property is skipped over.
0c26f463
MB
477Returns the button found."
478 (interactive "p\nd\nd")
479 (forward-button (- n) wrap display-message))
480
481
482(provide 'button)
483
484;;; button.el ends here