* nsgui.h: Fix compiler warning about gnulib redefining verify.
[emacs.git] / lisp / button.el
blob6ef79532ae76238b1167f463cb5174b3931baca5
1 ;;; button.el --- clickable buttons
2 ;;
3 ;; Copyright (C) 2001-2011 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.
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.
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.
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/>.
24 ;;; Commentary:
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.
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).
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.
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.
49 ;;; Code:
52 ;; Globals
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)
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.")
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.")
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)
93 ;; A `category-symbol' property for the default button type
94 (put 'button 'button-category-symbol 'default-button)
97 ;; Button types (which can be used to hold default properties for buttons)
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.
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)))
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).
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))
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))
155 (defun button-type-get (type prop)
156 "Get the property of button-type TYPE named PROP."
157 (get (button-category-symbol type) prop))
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))))
167 ;; Button properties and other attributes
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))))
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))))
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)))
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)))
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))))
229 (defun button-label (button)
230 "Return BUTTON's text label."
231 (buffer-substring-no-properties (button-start button) (button-end button)))
233 (defsubst button-type (button)
234 "Return BUTTON's button-type."
235 (button-get button 'type))
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))
242 ;; Creating overlay buttons
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'.
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))
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'.
273 Also see `insert-text-button', `make-button'."
274 (apply #'make-button
275 (prog1 (point) (insert label))
276 (point)
277 properties))
280 ;; Creating text-property buttons
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'.
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.
298 BEG can also be a string, in which case it is made into a button.
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))
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'.
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'.
342 Also see `make-text-button'."
343 (apply #'make-text-button
344 (prog1 (point) (insert label))
345 (point)
346 properties))
349 ;; Finding buttons in a buffer
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))))
359 (defun next-button (pos &optional count-current)
360 "Return the next button after position POS in the current buffer.
361 If COUNT-CURRENT is non-nil, count any button at POS in the search,
362 instead of starting at the next button."
363 (unless count-current
364 ;; Search for the next button boundary.
365 (setq pos (next-single-char-property-change pos 'button)))
366 (and (< pos (point-max))
367 (or (button-at pos)
368 ;; We must have originally been on a button, and are now in
369 ;; the inter-button space. Recurse to find a button.
370 (next-button pos))))
372 (defun previous-button (pos &optional count-current)
373 "Return the previous button before position POS in the current buffer.
374 If COUNT-CURRENT is non-nil, count any button at POS in the search,
375 instead of starting at the next button."
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).
388 (unless (= pos (button-start button)) new-button)
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)))))))
398 ;; User commands
400 (defun push-button (&optional pos use-mouse-action)
401 "Perform the action specified by a button at location POS.
402 POS may be either a buffer position or a mouse-event. If
403 USE-MOUSE-ACTION is non-nil, invoke the button's mouse-action
404 instead of its normal action; if the button has no mouse-action,
405 the normal action is used instead. The action may be either a
406 function to call or a marker to display.
407 POS defaults to point, except when `push-button' is invoked
408 interactively as the result of a mouse-event, in which case, the
409 mouse event is used.
410 If there's no button at POS, do nothing and return nil, otherwise
411 return 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))
418 (push-button (posn-point posn) t)))
419 ;; POS is just normal position
420 (let ((button (button-at (or pos (point)))))
421 (if (not button)
423 (button-activate button use-mouse-action)
424 t))))
426 (defun forward-button (n &optional wrap display-message)
427 "Move to the Nth next button, or Nth previous button if N is negative.
428 If N is 0, move to the start of any button at point.
429 If WRAP is non-nil, moving past either end of the buffer continues from the
430 other end.
431 If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
432 Any button with a non-nil `skip' property is skipped over.
433 Returns the button found."
434 (interactive "p\nd\nd")
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))
442 (wrap-start (if (> n 0) (point-min) (point-max)))
443 opoint fail)
444 (setq n (abs n))
445 (setq button t) ; just to start the loop
446 (while (and (null fail) (> n 0) button)
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))
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)))
458 (unless (button-get button 'skip)
459 (setq n (1- n)))))))
460 (if (null button)
461 (error (if wrap "No buttons!" "No more buttons"))
462 (let ((msg (and display-message (button-get button 'help-echo))))
463 (when msg
464 (message "%s" msg)))
465 button)))
467 (defun backward-button (n &optional wrap display-message)
468 "Move to the Nth previous button, or Nth next button if N is negative.
469 If N is 0, move to the start of any button at point.
470 If WRAP is non-nil, moving past either end of the buffer continues from the
471 other end.
472 If DISPLAY-MESSAGE is non-nil, the button's help-echo string is displayed.
473 Any button with a non-nil `skip' property is skipped over.
474 Returns the button found."
475 (interactive "p\nd\nd")
476 (forward-button (- n) wrap display-message))
479 (provide 'button)
481 ;;; button.el ends here