Update copyright notices for 2013.
[bpt/emacs.git] / lisp / button.el
1 ;;; button.el --- clickable buttons
2 ;;
3 ;; Copyright (C) 2001-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Miles Bader <miles@gnu.org>
6 ;; Keywords: extensions
7 ;; Package: emacs
8 ;;
9 ;; This file is part of GNU Emacs.
10 ;;
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
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.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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
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.
43 ;;
44 ;; Using `define-button-type' to define default properties for buttons
45 ;; is not necessary, but it is encouraged, since doing so makes the
46 ;; resulting code clearer and more efficient.
47 ;;
48
49 ;;; Code:
50
51 \f
52 ;; Globals
53
54 ;; Use color for the MS-DOS port because it doesn't support underline.
55 ;; FIXME if MS-DOS correctly answers the (supports) question, it need
56 ;; no longer be a special case.
57 (defface button '((t :inherit link))
58 "Default face used for buttons."
59 :group 'basic-faces)
60
61 (defvar button-map
62 (let ((map (make-sparse-keymap)))
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)
66 (define-key map [mouse-2] 'push-button)
67 map)
68 "Keymap used by buttons.")
69
70 (defvar button-buffer-map
71 (let ((map (make-sparse-keymap)))
72 (define-key map [?\t] 'forward-button)
73 (define-key map "\e\t" 'backward-button)
74 (define-key map [backtab] 'backward-button)
75 map)
76 "Keymap useful for buffers containing buttons.
77 Mode-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)
84 ;; action may be either a function to call, or a marker to go to
85 (put 'default-button 'action 'ignore)
86 (put 'default-button 'help-echo (purecopy "mouse-2, RET: Push this button"))
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)
92
93 ;; A `category-symbol' property for the default button type
94 (put 'button 'button-category-symbol 'default-button)
95
96 \f
97 ;; Button types (which can be used to hold default properties for buttons)
98
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.
110 Buttons 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
114 (defun define-button-type (name &rest properties)
115 "Define a `button type' called NAME (a symbol).
116 The remaining arguments form a sequence of PROPERTY VALUE pairs,
117 specifying 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
119 creating the button, using the :type keyword argument).
120
121 In addition, the keyword argument :supertype may be used to specify a
122 button-type from which NAME inherits its default property values
123 \(however, the inheritance happens only when NAME is defined; subsequent
124 changes to a supertype are not reflected in its subtypes)."
125 (let ((catsym (make-symbol (concat (symbol-name name) "-button")))
126 (super-catsym
127 (button-category-symbol
128 (or (plist-get properties 'supertype)
129 (plist-get properties :supertype)
130 'button))))
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.
134 (let ((default-props (symbol-plist super-catsym)))
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
142 (let ((prop (pop properties)))
143 (when (eq prop :supertype)
144 (setq prop 'supertype))
145 (put catsym prop (pop properties))))
146 ;; Make sure there's a `supertype' property
147 (unless (get catsym 'supertype)
148 (put catsym 'supertype 'button))
149 name))
150
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
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
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.
195 (cond ((memq prop '(type :type))
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
215 (defsubst button-activate (button &optional use-mouse-action)
216 "Call BUTTON's action property.
217 If USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
218 instead of its normal action; if the button has no mouse-action,
219 the normal action is used instead."
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))))
228
229 (defun button-label (button)
230 "Return BUTTON's text label."
231 (buffer-substring-no-properties (button-start button) (button-end button)))
232
233 (defsubst button-type (button)
234 "Return BUTTON's button-type."
235 (button-get button 'type))
236
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
241 \f
242 ;; Creating overlay buttons
243
244 (defun make-button (beg end &rest properties)
245 "Make a button from BEG to END in the current buffer.
246 The remaining arguments form a sequence of PROPERTY VALUE pairs,
247 specifying properties to add to the button.
248 In addition, the keyword argument :type may be used to specify a
249 button-type from which to inherit other properties; see
250 `define-button-type'.
251
252 Also 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
265 (defun insert-button (label &rest properties)
266 "Insert a button with the label LABEL.
267 The remaining arguments form a sequence of PROPERTY VALUE pairs,
268 specifying properties to add to the button.
269 In addition, the keyword argument :type may be used to specify a
270 button-type from which to inherit other properties; see
271 `define-button-type'.
272
273 Also 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
282 (defun make-text-button (beg end &rest properties)
283 "Make a button from BEG to END in the current buffer.
284 The remaining arguments form a sequence of PROPERTY VALUE pairs,
285 specifying properties to add to the button.
286 In addition, the keyword argument :type may be used to specify a
287 button-type from which to inherit other properties; see
288 `define-button-type'.
289
290 This function is like `make-button', except that the button is actually
291 part of the text instead of being a property of the buffer. That is,
292 this function uses text properties, the other uses overlays.
293 Creating large numbers of buttons can also be somewhat faster
294 using `make-text-button'. Note, however, that if there is an existing
295 face property at the site of the button, the button face may not be visible.
296 You may want to use `make-button' in that case.
297
298 BEG can also be a string, in which case it is made into a button.
299
300 Also see `insert-text-button'."
301 (let ((object nil)
302 (type-entry
303 (or (plist-member properties 'type)
304 (plist-member properties :type))))
305 (when (stringp beg)
306 (setq object beg beg 0 end (length object)))
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)
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))
328
329 (defun insert-text-button (label &rest properties)
330 "Insert a button with the label LABEL.
331 The remaining arguments form a sequence of PROPERTY VALUE pairs,
332 specifying properties to add to the button.
333 In addition, the keyword argument :type may be used to specify a
334 button-type from which to inherit other properties; see
335 `define-button-type'.
336
337 This function is like `insert-button', except that the button is
338 actually part of the text instead of being a property of the buffer.
339 Creating large numbers of buttons can also be somewhat faster using
340 `insert-text-button'.
341
342 Also 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 If the button at POS is a text property button, the return value
354 is a marker pointing to POS."
355 (let ((button (get-char-property pos 'button)))
356 (if (or (overlayp button) (null button))
357 button
358 ;; Must be a text-property button; return a marker pointing to it.
359 (copy-marker pos t))))
360
361 (defun next-button (pos &optional count-current)
362 "Return the next button after position POS in the current buffer.
363 If COUNT-CURRENT is non-nil, count any button at POS in the search,
364 instead of starting at the next button."
365 (unless count-current
366 ;; Search for the next button boundary.
367 (setq pos (next-single-char-property-change pos 'button)))
368 (and (< pos (point-max))
369 (or (button-at pos)
370 ;; We must have originally been on a button, and are now in
371 ;; the inter-button space. Recurse to find a button.
372 (next-button pos))))
373
374 (defun previous-button (pos &optional count-current)
375 "Return the previous button before position POS in the current buffer.
376 If COUNT-CURRENT is non-nil, count any button at POS in the search,
377 instead of starting at the next button."
378 (let ((button (button-at pos)))
379 (if button
380 (if count-current
381 button
382 ;; We started out on a button, so move to its start and look
383 ;; for the previous button boundary.
384 (setq pos (previous-single-char-property-change
385 (button-start button) 'button))
386 (let ((new-button (button-at pos)))
387 (if new-button
388 ;; We are in a button again; this can happen if there
389 ;; are adjacent buttons (or at bob).
390 (unless (= pos (button-start button)) new-button)
391 ;; We are now in the space between buttons.
392 (previous-button pos))))
393 ;; We started out in the space between buttons.
394 (setq pos (previous-single-char-property-change pos 'button))
395 (or (button-at pos)
396 (and (> pos (point-min))
397 (button-at (1- pos)))))))
398
399 \f
400 ;; User commands
401
402 (defun push-button (&optional pos use-mouse-action)
403 "Perform the action specified by a button at location POS.
404 POS may be either a buffer position or a mouse-event. If
405 USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
406 instead of its normal action; if the button has no mouse-action,
407 the normal action is used instead. The action may be either a
408 function to call or a marker to display.
409 POS defaults to point, except when `push-button' is invoked
410 interactively as the result of a mouse-event, in which case, the
411 mouse event is used.
412 If there's no button at POS, do nothing and return nil, otherwise
413 return t."
414 (interactive
415 (list (if (integerp last-command-event) (point) last-command-event)))
416 (if (and (not (integerp pos)) (eventp pos))
417 ;; POS is a mouse event; switch to the proper window/buffer
418 (let ((posn (event-start pos)))
419 (with-current-buffer (window-buffer (posn-window posn))
420 (push-button (posn-point posn) t)))
421 ;; POS is just normal position
422 (let ((button (button-at (or pos (point)))))
423 (if (not button)
424 nil
425 (button-activate button use-mouse-action)
426 t))))
427
428 (defun forward-button (n &optional wrap display-message)
429 "Move to the Nth next button, or Nth previous button if N is negative.
430 If N is 0, move to the start of any button at point.
431 If WRAP is non-nil, moving past either end of the buffer continues from the
432 other end.
433 If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
434 Any button with a non-nil `skip' property is skipped over.
435 Returns the button found."
436 (interactive "p\nd\nd")
437 (let (button)
438 (if (zerop n)
439 ;; Move to start of current button
440 (if (setq button (button-at (point)))
441 (goto-char (button-start button)))
442 ;; Move to Nth next button
443 (let ((iterator (if (> n 0) #'next-button #'previous-button))
444 (wrap-start (if (> n 0) (point-min) (point-max)))
445 opoint fail)
446 (setq n (abs n))
447 (setq button t) ; just to start the loop
448 (while (and (null fail) (> n 0) button)
449 (setq button (funcall iterator (point)))
450 (when (and (not button) wrap)
451 (setq button (funcall iterator wrap-start t)))
452 (when button
453 (goto-char (button-start button))
454 ;; Avoid looping forever (e.g., if all the buttons have
455 ;; the `skip' property).
456 (cond ((null opoint)
457 (setq opoint (point)))
458 ((= opoint (point))
459 (setq fail t)))
460 (unless (button-get button 'skip)
461 (setq n (1- n)))))))
462 (if (null button)
463 (error (if wrap "No buttons!" "No more buttons"))
464 (let ((msg (and display-message (button-get button 'help-echo))))
465 (when msg
466 (message "%s" msg)))
467 button)))
468
469 (defun backward-button (n &optional wrap display-message)
470 "Move to the Nth previous button, or Nth next button if N is negative.
471 If N is 0, move to the start of any button at point.
472 If WRAP is non-nil, moving past either end of the buffer continues from the
473 other end.
474 If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
475 Any button with a non-nil `skip' property is skipped over.
476 Returns the button found."
477 (interactive "p\nd\nd")
478 (forward-button (- n) wrap display-message))
479
480
481 (provide 'button)
482
483 ;;; button.el ends here