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