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