wid-edit.el 124 KB
Newer Older
1
;;; wid-edit.el --- Functions for creating and using widgets -*- lexical-binding:t -*-
Per Abrahamsen's avatar
Per Abrahamsen committed
2
;;
Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1996-1997, 1999-2019 Free Software Foundation, Inc.
Per Abrahamsen's avatar
Per Abrahamsen committed
4 5
;;
;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6
;; Maintainer: emacs-devel@gnu.org
Per Abrahamsen's avatar
Per Abrahamsen committed
7
;; Keywords: extensions
8
;; Package: emacs
Per Abrahamsen's avatar
Per Abrahamsen committed
9

10 11
;; This file is part of GNU Emacs.

12
;; GNU Emacs is free software: you can redistribute it and/or modify
13
;; it under the terms of the GNU General Public License as published by
14 15
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
16 17 18 19 20 21 22

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
23
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
24

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
;;; Wishlist items (from widget.texi):

;; * The `menu-choice' tag should be prettier, something like the
;;   abbreviated menus in Open Look.

;; * Finish `:tab-order'.

;; * Make indentation work with glyphs and proportional fonts.

;; * Add commands to show overview of object and class hierarchies to
;;   the browser.

;; * Find a way to disable mouse highlight for inactive widgets.

;; * Find a way to make glyphs look inactive.

;; * Add `key-binding' widget.

;; * Add `widget' widget for editing widget specifications.

;; * Find clean way to implement variable length list.  See
;;   `TeX-printer-list' for an explanation.

;; * `C-h' in `widget-prompt-value' should give type specific help.

;; * A mailto widget. [This should work OK as a url-link if with
;;   browse-url-browser-function' set up appropriately.]

Per Abrahamsen's avatar
Per Abrahamsen committed
53 54 55 56 57
;;; Commentary:
;;
;; See `widget.el'.

;;; Code:
58
(require 'cl-lib)
Per Abrahamsen's avatar
Per Abrahamsen committed
59 60

;;; Compatibility.
61

62 63
(defun widget-event-point (event)
  "Character position of the end of event if that exists, or nil."
64
  (posn-point (event-end event)))
65

66 67 68 69 70 71
(defun widget-button-release-event-p (event)
  "Non-nil if EVENT is a mouse-button-release event object."
  (and (eventp event)
       (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
       (or (memq 'click (event-modifiers event))
	   (memq  'drag (event-modifiers event)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
72 73 74 75 76 77

;;; Customization.

(defgroup widgets nil
  "Customization support for the Widget Library."
  :link '(custom-manual "(widget)Top")
Per Abrahamsen's avatar
Per Abrahamsen committed
78
  :link '(emacs-library-link :tag "Lisp File" "widget.el")
Per Abrahamsen's avatar
Per Abrahamsen committed
79
  :prefix "widget-"
80
  :group 'extensions)
Per Abrahamsen's avatar
Per Abrahamsen committed
81

Per Abrahamsen's avatar
Per Abrahamsen committed
82
(defgroup widget-documentation nil
83
  "Options controlling the display of documentation strings."
Per Abrahamsen's avatar
Per Abrahamsen committed
84 85
  :group 'widgets)

Per Abrahamsen's avatar
Per Abrahamsen committed
86 87 88 89 90
(defgroup widget-faces nil
  "Faces used by the widget library."
  :group 'widgets
  :group 'faces)

91
(defvar widget-documentation-face 'widget-documentation
92
  "Face used for documentation strings in widgets.
93 94
This exists as a variable so it can be set locally in certain buffers.")

95 96 97 98 99 100 101
(defface widget-documentation '((((class color)
				  (background dark))
				 (:foreground "lime green"))
				(((class color)
				  (background light))
				 (:foreground "dark green"))
				(t nil))
Per Abrahamsen's avatar
Per Abrahamsen committed
102 103 104 105
  "Face used for documentation text."
  :group 'widget-documentation
  :group 'widget-faces)

106
(defvar widget-button-face 'widget-button
107
  "Face used for buttons in widgets.
108 109
This exists as a variable so it can be set locally in certain buffers.")

110
(defface widget-button '((t (:weight bold)))
Per Abrahamsen's avatar
Per Abrahamsen committed
111
  "Face used for widget buttons."
Per Abrahamsen's avatar
Per Abrahamsen committed
112
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
113 114 115 116

(defcustom widget-mouse-face 'highlight
  "Face used for widget buttons when the mouse is above them."
  :type 'face
Per Abrahamsen's avatar
Per Abrahamsen committed
117
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
118

119 120 121
;; TTY gets special definitions here and in the next defface, because
;; the gray colors defined for other displays cause black text on a black
;; background, at least on light-background TTYs.
122 123 124 125 126 127 128 129 130 131 132
(defface widget-field '((((type tty))
			 :background "yellow3"
			 :foreground "black")
			(((class grayscale color)
			  (background light))
			 :background "gray85")
			(((class grayscale color)
			  (background dark))
			 :background "dim gray")
			(t
			 :slant italic))
Per Abrahamsen's avatar
Per Abrahamsen committed
133
  "Face used for editable fields."
Per Abrahamsen's avatar
Per Abrahamsen committed
134
  :group 'widget-faces)
135 136 137 138 139 140 141 142 143 144 145 146

(defface widget-single-line-field '((((type tty))
				     :background "green3"
				     :foreground "black")
				    (((class grayscale color)
				      (background light))
				     :background "gray85")
				    (((class grayscale color)
				      (background dark))
				     :background "dim gray")
				    (t
				     :slant italic))
Per Abrahamsen's avatar
Per Abrahamsen committed
147 148 149
  "Face used for editable fields spanning only a single line."
  :group 'widget-faces)

150 151 152 153 154 155 156 157 158 159 160
;;; This causes display-table to be loaded, and not usefully.
;;;(defvar widget-single-line-display-table
;;;  (let ((table (make-display-table)))
;;;    (aset table 9  "^I")
;;;    (aset table 10 "^J")
;;;    table)
;;;  "Display table used for single-line editable fields.")

;;;(when (fboundp 'set-face-display-table)
;;;  (set-face-display-table 'widget-single-line-field-face
;;;			  widget-single-line-display-table))
Per Abrahamsen's avatar
Per Abrahamsen committed
161

Per Abrahamsen's avatar
Per Abrahamsen committed
162 163 164 165 166
;;; Utility functions.
;;
;; These are not really widget specific.

(defun widget-princ-to-string (object)
167 168 169 170 171
  "Return string representation of OBJECT, any Lisp object.
No quoting characters are used; no delimiters are printed around
the contents of strings."
  (with-output-to-string
      (princ object)))
Per Abrahamsen's avatar
Per Abrahamsen committed
172 173 174 175 176 177

(defun widget-clear-undo ()
  "Clear all undo information."
  (buffer-disable-undo (current-buffer))
  (buffer-enable-undo))

Per Abrahamsen's avatar
Per Abrahamsen committed
178 179 180 181 182 183
(defcustom widget-menu-max-size 40
  "Largest number of items allowed in a popup-menu.
Larger menus are read through the minibuffer."
  :group 'widgets
  :type 'integer)

184 185 186 187 188 189 190
(defcustom widget-menu-max-shortcuts 40
  "Largest number of items for which it works to choose one with a character.
For a larger number of items, the minibuffer is used."
  :group 'widgets
  :type 'integer)

(defcustom widget-menu-minibuffer-flag nil
Lute Kamstra's avatar
Lute Kamstra committed
191
  "Control how to ask for a choice from the keyboard.
192 193 194 195 196
Non-nil means use the minibuffer;
nil means read a single character."
  :group 'widgets
  :type 'boolean)

Per Abrahamsen's avatar
Per Abrahamsen committed
197 198 199 200
(defun widget-choose (title items &optional event)
  "Choose an item from a list.

First argument TITLE is the name of the list.
201
Second argument ITEMS is a list whose members are either
Per Abrahamsen's avatar
Per Abrahamsen committed
202 203
 (NAME . VALUE), to indicate selectable items, or just strings to
 indicate unselectable items.
Per Abrahamsen's avatar
Per Abrahamsen committed
204 205 206 207 208 209 210 211
Optional third argument EVENT is an input event.

The user is asked to choose between each NAME from the items alist,
and the VALUE of the chosen element will be returned.  If EVENT is a
mouse event, and the number of elements in items is less than
`widget-menu-max-size', a popup menu will be used, otherwise the
minibuffer."
  (cond ((and (< (length items) widget-menu-max-size)
212
	      event (display-popup-menus-p))
213
	 ;; Mouse click.
Per Abrahamsen's avatar
Per Abrahamsen committed
214 215
	 (x-popup-menu event
		       (list title (cons "" items))))
216 217
	((or widget-menu-minibuffer-flag
	     (> (length items) widget-menu-max-shortcuts))
218
	 ;; Read the choice of name from the minibuffer.
219
	 (setq items (cl-remove-if 'stringp items))
Per Abrahamsen's avatar
Per Abrahamsen committed
220 221 222 223 224
	 (let ((val (completing-read (concat title ": ") items nil t)))
	   (if (stringp val)
	       (let ((try (try-completion val items)))
		 (when (stringp try)
		   (setq val try))
225
		 (cdr (assoc val items))))))
226 227 228
	(t
	 ;; Construct a menu of the choices
	 ;; and then use it for prompting for a single character.
229 230 231
	 (let* ((next-digit ?0)
		(map (make-sparse-keymap))
                choice some-choice-enabled value)
232
	   (with-current-buffer (get-buffer-create " widget-choose")
233 234 235
	     (erase-buffer)
	     (insert "Available choices:\n\n")
	     (while items
236 237
	       (setq choice (pop items))
	       (when (consp choice)
238
                 (let* ((name (substitute-command-keys (car choice)))
239 240 241 242
                        (function (cdr choice)))
                   (insert (format "%c = %s\n" next-digit name))
                   (define-key map (vector next-digit) function)
                   (setq some-choice-enabled t)))
243 244 245
	       ;; Allocate digits to disabled alternatives
	       ;; so that the digit of a given alternative never varies.
	       (setq next-digit (1+ next-digit)))
246 247 248
	     (insert "\nC-g = Quit")
	     (goto-char (point-min))
	     (forward-line))
249 250
	   (or some-choice-enabled
	       (error "None of the choices is currently meaningful"))
251 252
	   (define-key map [?\M-\C-v] 'scroll-other-window)
	   (define-key map [?\M--] 'negative-argument)
253
	   (save-window-excursion
254
	     (let ((buf (get-buffer " widget-choose")))
255 256 257 258
	       (display-buffer buf
			       '(display-buffer-in-direction
				 (direction . bottom)
				 (window-height . fit-window-to-buffer)))
259 260
	       (let ((cursor-in-echo-area t)
		     (arg 1))
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
                 (while (not value)
                   (setq value (lookup-key map (read-key-sequence (format "%s: " title))))
                   (unless value
                     (user-error "Canceled"))
                   (when
                       (cond ((eq value 'scroll-other-window)
                               (let ((minibuffer-scroll-window
                                      (get-buffer-window buf)))
                                 (if (> 0 arg)
                                     (scroll-other-window-down
                                      (window-height minibuffer-scroll-window))
                                   (scroll-other-window))
                                 (setq arg 1)))
                              ((eq value 'negative-argument)
                               (setq arg -1)))
                     (setq value nil))))))
277
	   value))))
Per Abrahamsen's avatar
Per Abrahamsen committed
278 279

;;; Widget text specifications.
280
;;
281
;; These functions are for specifying text properties.
Per Abrahamsen's avatar
Per Abrahamsen committed
282

283 284
;; We can set it to nil now that get_local_map uses get_pos_property.
(defconst widget-field-add-space nil
Per Abrahamsen's avatar
Per Abrahamsen committed
285 286
  "Non-nil means add extra space at the end of editable text fields.
If you don't add the space, it will become impossible to edit a zero
287
size field.")
Per Abrahamsen's avatar
Per Abrahamsen committed
288

289
(defvar widget-field-use-before-change t
Per Abrahamsen's avatar
Per Abrahamsen committed
290
  "Non-nil means use `before-change-functions' to track editable fields.
Glenn Morris's avatar
Glenn Morris committed
291 292
This enables the use of undo.  Using before hooks also means that
the :notify function can't know the new value.")
Per Abrahamsen's avatar
Per Abrahamsen committed
293

Per Abrahamsen's avatar
Per Abrahamsen committed
294
(defun widget-specify-field (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
295
  "Specify editable button for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
296 297 298 299 300
  ;; Terminating space is not part of the field, but necessary in
  ;; order for local-map to work.  Remove next sexp if local-map works
  ;; at the end of the overlay.
  (save-excursion
    (goto-char to)
Per Abrahamsen's avatar
Per Abrahamsen committed
301 302 303 304
    (cond ((null (widget-get widget :size))
	   (forward-char 1))
	  (widget-field-add-space
	   (insert-and-inherit " ")))
Per Abrahamsen's avatar
Per Abrahamsen committed
305
    (setq to (point)))
Miles Bader's avatar
Miles Bader committed
306
  (let ((keymap (widget-get widget :keymap))
307
	(face (or (widget-get widget :value-face) 'widget-field))
Miles Bader's avatar
Miles Bader committed
308
	(help-echo (widget-get widget :help-echo))
309
	(follow-link (widget-get widget :follow-link))
Miles Bader's avatar
Miles Bader committed
310 311
	(rear-sticky
	 (or (not widget-field-add-space) (widget-get widget :size))))
312
    (if (functionp help-echo)
313
      (setq help-echo 'widget-mouse-help))
314
    (when (= (char-before to) ?\n)
Miles Bader's avatar
Miles Bader committed
315 316 317
      ;; When the last character in the field is a newline, we want to
      ;; give it a `field' char-property of `boundary', which helps the
      ;; C-n/C-p act more naturally when entering/leaving the field.  We
318
     ;; do this by making a small secondary overlay to contain just that
Miles Bader's avatar
Miles Bader committed
319 320 321
      ;; one character.
      (let ((overlay (make-overlay (1- to) to nil t nil)))
	(overlay-put overlay 'field 'boundary)
322 323
        ;; We need the real field for tabbing.
	(overlay-put overlay 'real-field widget)
324 325 326
	;; Use `local-map' here, not `keymap', so that normal editing
	;; works in the field when, say, Custom uses `suppress-keymap'.
	(overlay-put overlay 'local-map keymap)
Miles Bader's avatar
Miles Bader committed
327
	(overlay-put overlay 'face face)
328
	(overlay-put overlay 'follow-link follow-link)
Miles Bader's avatar
Miles Bader committed
329 330 331 332 333 334 335
	(overlay-put overlay 'help-echo help-echo))
      (setq to (1- to))
      (setq rear-sticky t))
    (let ((overlay (make-overlay from to nil nil rear-sticky)))
      (widget-put widget :field-overlay overlay)
      ;;(overlay-put overlay 'detachable nil)
      (overlay-put overlay 'field widget)
336
      (overlay-put overlay 'local-map keymap)
Miles Bader's avatar
Miles Bader committed
337
      (overlay-put overlay 'face face)
338
      (overlay-put overlay 'follow-link follow-link)
Miles Bader's avatar
Miles Bader committed
339
      (overlay-put overlay 'help-echo help-echo)))
340 341 342 343 344 345 346 347 348
  (widget-specify-secret widget))

(defun widget-specify-secret (field)
  "Replace text in FIELD with value of `:secret', if non-nil."
  (let ((secret (widget-get field :secret))
	(size (widget-get field :size)))
    (when secret
      (let ((begin (widget-field-start field))
	    (end (widget-field-end field)))
349
	(when size
350
	  (while (and (> end begin)
351
		      (eq (char-after (1- end)) ?\s))
352 353 354 355 356 357 358
	    (setq end (1- end))))
	(while (< begin end)
	  (let ((old (char-after begin)))
	    (unless (eq old secret)
	      (subst-char-in-region begin (1+ begin) old secret)
	      (put-text-property begin (1+ begin) 'secret old))
	    (setq begin (1+ begin))))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
359 360

(defun widget-specify-button (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
361
  "Specify button for WIDGET between FROM and TO."
362
  (let ((overlay (make-overlay from to nil t nil))
363
	(follow-link (widget-get widget :follow-link))
364
	(help-echo (widget-get widget :help-echo)))
Per Abrahamsen's avatar
Per Abrahamsen committed
365
    (widget-put widget :button-overlay overlay)
366 367
    (if (functionp help-echo)
      (setq help-echo 'widget-mouse-help))
Per Abrahamsen's avatar
Per Abrahamsen committed
368
    (overlay-put overlay 'button widget)
369
    (overlay-put overlay 'keymap (widget-get widget :keymap))
370
    (overlay-put overlay 'evaporate t)
371 372
    ;; We want to avoid the face with image buttons.
    (unless (widget-get widget :suppress-face)
373
      (overlay-put overlay 'face (widget-apply widget :button-face-get))
374
      (overlay-put overlay 'mouse-face
375 376 377 378 379 380 381 382 383 384 385
		   ;; Make new list structure for the mouse-face value
		   ;; so that different widgets will have
		   ;; different `mouse-face' property values
		   ;; and will highlight separately.
		   (let ((mouse-face-value
			  (widget-apply widget :mouse-face-get)))
		     ;; If it's a list, copy it.
		     (if (listp mouse-face-value)
			 (copy-sequence mouse-face-value)
		       ;; If it's a symbol, put it in a list.
		       (list mouse-face-value)))))
386
    (overlay-put overlay 'pointer 'hand)
387
    (overlay-put overlay 'follow-link follow-link)
388 389
    (overlay-put overlay 'help-echo help-echo)))

390
(defun widget-mouse-help (_window overlay _point)
391 392 393 394 395 396 397
  "Help-echo callback for widgets whose :help-echo is a function."
  (with-current-buffer (overlay-buffer overlay)
    (let* ((widget (widget-at (overlay-start overlay)))
	   (help-echo (if widget (widget-get widget :help-echo))))
      (if (functionp help-echo)
	  (funcall help-echo widget)
	help-echo))))
Per Abrahamsen's avatar
Per Abrahamsen committed
398 399

(defun widget-specify-sample (widget from to)
400
  "Specify sample for WIDGET between FROM and TO."
401 402
  (let ((overlay (make-overlay from to nil t nil)))
    (overlay-put overlay 'face (widget-apply widget :sample-face-get))
403
    (overlay-put overlay 'evaporate t)
Per Abrahamsen's avatar
Per Abrahamsen committed
404 405
    (widget-put widget :sample-overlay overlay)))

Per Abrahamsen's avatar
Per Abrahamsen committed
406
(defun widget-specify-doc (widget from to)
407
  "Specify documentation for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
408 409 410
  (let ((overlay (make-overlay from to nil t nil)))
    (overlay-put overlay 'widget-doc widget)
    (overlay-put overlay 'face widget-documentation-face)
411
    (overlay-put overlay 'evaporate t)
Per Abrahamsen's avatar
Per Abrahamsen committed
412
    (widget-put widget :doc-overlay overlay)))
Per Abrahamsen's avatar
Per Abrahamsen committed
413 414

(defmacro widget-specify-insert (&rest form)
415 416 417
  "Execute FORM without inheriting any text properties."
  `(save-restriction
    (let ((inhibit-read-only t)
418 419 420 421
	  (inhibit-modification-hooks t))
      (narrow-to-region (point) (point))
      (prog1 (progn ,@form)
	(goto-char (point-max))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
422

423 424
(defface widget-inactive
  '((t :inherit shadow))
Per Abrahamsen's avatar
Per Abrahamsen committed
425
  "Face used for inactive widgets."
Per Abrahamsen's avatar
Per Abrahamsen committed
426
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
427 428 429 430 431

(defun widget-specify-inactive (widget from to)
  "Make WIDGET inactive for user modifications."
  (unless (widget-get widget :inactive)
    (let ((overlay (make-overlay from to nil t nil)))
432
      (overlay-put overlay 'face 'widget-inactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
433
      ;; This is disabled, as it makes the mouse cursor change shape.
434
      ;; (overlay-put overlay 'mouse-face 'widget-inactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
435 436
      (overlay-put overlay 'evaporate t)
      (overlay-put overlay 'priority 100)
437
      (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
Per Abrahamsen's avatar
Per Abrahamsen committed
438 439
      (widget-put widget :inactive overlay))))

440
(defun widget-overlay-inactive (&rest _junk)
Per Abrahamsen's avatar
Per Abrahamsen committed
441 442
  "Ignoring the arguments, signal an error."
  (unless inhibit-read-only
443
    (error "The widget here is not active")))
Per Abrahamsen's avatar
Per Abrahamsen committed
444 445 446 447 448 449 450 451 452 453 454 455


(defun widget-specify-active (widget)
  "Make WIDGET active for user modifications."
  (let ((inactive (widget-get widget :inactive)))
    (when inactive
      (delete-overlay inactive)
      (widget-put widget :inactive nil))))

;;; Widget Properties.

(defsubst widget-type (widget)
Richard M. Stallman's avatar
Richard M. Stallman committed
456
  "Return the type of WIDGET.  The type is a symbol."
Per Abrahamsen's avatar
Per Abrahamsen committed
457 458
  (car widget))

459 460
;;;###autoload
(defun widgetp (widget)
461
  "Return non-nil if WIDGET is a widget."
462 463 464
  (if (symbolp widget)
      (get widget 'widget-type)
    (and (consp widget)
465 466
	 (symbolp (car widget))
	 (get (car widget) 'widget-type))))
467

Per Abrahamsen's avatar
Per Abrahamsen committed
468 469
(defun widget-get-indirect (widget property)
  "In WIDGET, get the value of PROPERTY.
470
If the value is a symbol, return its binding.
Per Abrahamsen's avatar
Per Abrahamsen committed
471 472 473 474 475 476
Otherwise, just return the value."
  (let ((value (widget-get widget property)))
    (if (symbolp value)
	(symbol-value value)
      value)))

Per Abrahamsen's avatar
Per Abrahamsen committed
477
(defun widget-member (widget property)
478
  "Non-nil if there is a definition in WIDGET for PROPERTY."
479
  (cond ((plist-member (cdr widget) property)
Per Abrahamsen's avatar
Per Abrahamsen committed
480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
	 t)
	((car widget)
	 (widget-member (get (car widget) 'widget-type) property))
	(t nil)))

(defun widget-value (widget)
  "Extract the current value of WIDGET."
  (widget-apply widget
		:value-to-external (widget-apply widget :value-get)))

(defun widget-value-set (widget value)
  "Set the current value of WIDGET to VALUE."
  (widget-apply widget
		:value-set (widget-apply widget
					 :value-to-internal value)))

496
(defun widget-default-get (widget)
Per Abrahamsen's avatar
Per Abrahamsen committed
497
  "Extract the default external value of WIDGET."
498
  (widget-apply widget :value-to-external
Per Abrahamsen's avatar
Per Abrahamsen committed
499
		(or (widget-get widget :value)
500 501
		    (progn
		      (when (widget-get widget :args)
502
			(setq widget (widget-copy widget))
503 504 505 506 507 508 509 510
			(let (args)
			  (dolist (arg (widget-get widget :args))
			    (setq args (append args
					       (if (widget-get arg :inline)
						   (widget-get arg :args)
						 (list arg)))))
			  (widget-put widget :args args)))
		      (widget-apply widget :default-get)))))
511

Per Abrahamsen's avatar
Per Abrahamsen committed
512
(defun widget-match-inline (widget vals)
513
  "In WIDGET, match the start of VALS."
Per Abrahamsen's avatar
Per Abrahamsen committed
514 515
  (cond ((widget-get widget :inline)
	 (widget-apply widget :match-inline vals))
516
	((and (listp vals)
Per Abrahamsen's avatar
Per Abrahamsen committed
517 518 519 520 521 522
	      (widget-apply widget :match (car vals)))
	 (cons (list (car vals)) (cdr vals)))
	(t nil)))

(defun widget-apply-action (widget &optional event)
  "Apply :action in WIDGET in response to EVENT."
Per Abrahamsen's avatar
Per Abrahamsen committed
523 524 525
  (if (widget-apply widget :active)
      (widget-apply widget :action event)
    (error "Attempt to perform action on inactive widget")))
Per Abrahamsen's avatar
Per Abrahamsen committed
526

Per Abrahamsen's avatar
Per Abrahamsen committed
527 528 529 530 531 532 533 534 535 536 537 538 539 540
;;; Helper functions.
;;
;; These are widget specific.

;;;###autoload
(defun widget-prompt-value (widget prompt &optional value unbound)
  "Prompt for a value matching WIDGET, using PROMPT.
The current value is assumed to be VALUE, unless UNBOUND is non-nil."
  (unless (listp widget)
    (setq widget (list widget)))
  (setq prompt (format "[%s] %s" (widget-type widget) prompt))
  (setq widget (widget-convert widget))
  (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
    (unless (widget-apply widget :match answer)
541
      (error "Value does not match %S type" (car widget)))
Per Abrahamsen's avatar
Per Abrahamsen committed
542 543 544 545 546
    answer))

(defun widget-get-sibling (widget)
  "Get the item WIDGET is assumed to toggle.
This is only meaningful for radio buttons or checkboxes in a list."
547
  (let* ((children (widget-get (widget-get widget :parent) :children))
Per Abrahamsen's avatar
Per Abrahamsen committed
548 549 550 551 552 553 554 555 556
	 child)
    (catch 'child
      (while children
	(setq child (car children)
	      children (cdr children))
	(when (eq (widget-get child :button) widget)
	  (throw 'child child)))
      nil)))

Per Abrahamsen's avatar
Per Abrahamsen committed
557 558 559 560
(defun widget-map-buttons (function &optional buffer maparg)
  "Map FUNCTION over the buttons in BUFFER.
FUNCTION is called with the arguments WIDGET and MAPARG.

Paul Eggert's avatar
Paul Eggert committed
561
If FUNCTION returns non-nil, the walk is canceled.
Per Abrahamsen's avatar
Per Abrahamsen committed
562 563 564 565 566 567

The arguments MAPARG, and BUFFER default to nil and (current-buffer),
respectively."
  (let ((cur (point-min))
	(widget nil)
	(overlays (if buffer
568
		      (with-current-buffer buffer (overlay-lists))
Per Abrahamsen's avatar
Per Abrahamsen committed
569 570 571 572 573 574 575
		    (overlay-lists))))
    (setq overlays (append (car overlays) (cdr overlays)))
    (while (setq cur (pop overlays))
      (setq widget (overlay-get cur 'button))
      (if (and widget (funcall function widget maparg))
	  (setq overlays nil)))))

576
;;; Images.
Per Abrahamsen's avatar
Per Abrahamsen committed
577

578
(defcustom widget-image-directory (file-name-as-directory
579
				   (expand-file-name "images/custom" data-directory))
580
  "Where widget button images are located.
Per Abrahamsen's avatar
Per Abrahamsen committed
581
If this variable is nil, widget will try to locate the directory
Per Abrahamsen's avatar
Per Abrahamsen committed
582
automatically."
Per Abrahamsen's avatar
Per Abrahamsen committed
583 584 585
  :group 'widgets
  :type 'directory)

586
(defcustom widget-image-enable t
587
  "If non-nil, use image buttons in widgets when available."
588
  :version "21.1"
Per Abrahamsen's avatar
Per Abrahamsen committed
589 590 591
  :group 'widgets
  :type 'boolean)

Per Abrahamsen's avatar
Per Abrahamsen committed
592 593 594 595 596 597 598 599 600 601
(defcustom widget-image-conversion
  '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
    (xbm ".xbm"))
  "Conversion alist from image formats to file name suffixes."
  :group 'widgets
  :type '(repeat (cons :format "%v"
		       (symbol :tag "Image Format" unknown)
		       (repeat :tag "Suffixes"
			       (string :format "%v")))))

602 603 604
(defun widget-image-find (image)
  "Create a graphical button from IMAGE.
IMAGE should either already be an image, or be a file name sans
Per Abrahamsen's avatar
Per Abrahamsen committed
605
extension (xpm, xbm, gif, jpg, or png) located in
606 607 608
`widget-image-directory' or otherwise where `find-image' will find it."
  (cond ((not (and image widget-image-enable (display-graphic-p)))
	 ;; We don't want or can't use images.
Per Abrahamsen's avatar
Per Abrahamsen committed
609
	 nil)
610 611 612
	((and (consp image)
	      (eq 'image (car image)))
	 ;; Already an image spec.  Use it.
Per Abrahamsen's avatar
Per Abrahamsen committed
613
	 image)
Per Abrahamsen's avatar
Per Abrahamsen committed
614 615
	((stringp image)
	 ;; A string.  Look it up in relevant directories.
616 617 618 619
	 (let* ((load-path (cons widget-image-directory load-path))
		specs)
	   (dolist (elt widget-image-conversion)
	     (dolist (ext (cdr elt))
620 621
	       (push (list :type (car elt) :file (concat image ext))
		     specs)))
622
 	   (find-image (nreverse specs))))
Per Abrahamsen's avatar
Per Abrahamsen committed
623
	(t
Per Abrahamsen's avatar
Per Abrahamsen committed
624
	 ;; Oh well.
Per Abrahamsen's avatar
Per Abrahamsen committed
625 626
	 nil)))

627
(defvar widget-button-pressed-face 'widget-button-pressed
628 629 630 631
  "Face used for pressed buttons in widgets.
This exists as a variable so it can be set locally in certain
buffers.")

632
(defun widget-image-insert (widget tag image &optional _down _inactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
633
  "In WIDGET, insert the text TAG or, if supported, IMAGE.
634 635 636 637 638
IMAGE should either be an image or an image file name sans extension
\(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.

Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
button is pressed or inactive, respectively.  These are currently ignored."
639
  (if (and (featurep 'image)
640 641
	   (setq image (widget-image-find image)))
      (progn (widget-put widget :suppress-face t)
642
	     (insert-image image tag))
643
    (insert tag)))
Per Abrahamsen's avatar
Per Abrahamsen committed
644

645 646 647 648
(defun widget-move-and-invoke (event)
  "Move to where you click, and if it is an active field, invoke it."
  (interactive "e")
  (mouse-set-point event)
649 650 651
  (let ((pos (widget-event-point event)))
    (if (and pos (get-char-property pos 'button))
	(widget-button-click event))))
652

Per Abrahamsen's avatar
Per Abrahamsen committed
653 654 655 656 657 658 659 660 661
;;; Buttons.

(defgroup widget-button nil
  "The look of various kinds of buttons."
  :group 'widgets)

(defcustom widget-button-prefix ""
  "String used as prefix for buttons."
  :type 'string
Per Abrahamsen's avatar
Per Abrahamsen committed
662
  :group 'widget-button)
Per Abrahamsen's avatar
Per Abrahamsen committed
663 664 665 666

(defcustom widget-button-suffix ""
  "String used as suffix for buttons."
  :type 'string
Per Abrahamsen's avatar
Per Abrahamsen committed
667
  :group 'widget-button)
Per Abrahamsen's avatar
Per Abrahamsen committed
668

Per Abrahamsen's avatar
Per Abrahamsen committed
669 670 671 672
;;; Creating Widgets.

;;;###autoload
(defun widget-create (type &rest args)
673
  "Create widget of TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
The optional ARGS are additional keyword arguments."
  (let ((widget (apply 'widget-convert type args)))
    (widget-apply widget :create)
    widget))

(defun widget-create-child-and-convert (parent type &rest args)
  "As part of the widget PARENT, create a child widget TYPE.
The child is converted, using the keyword arguments ARGS."
  (let ((widget (apply 'widget-convert type args)))
    (widget-put widget :parent parent)
    (unless (widget-get widget :indent)
      (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
				    (or (widget-get widget :extra-offset) 0)
				    (widget-get parent :offset))))
    (widget-apply widget :create)
    widget))

(defun widget-create-child (parent type)
  "Create widget of TYPE."
Per Abrahamsen's avatar
Per Abrahamsen committed
693
  (let ((widget (widget-copy type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
694 695 696 697 698 699 700 701 702 703
    (widget-put widget :parent parent)
    (unless (widget-get widget :indent)
      (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
				    (or (widget-get widget :extra-offset) 0)
				    (widget-get parent :offset))))
    (widget-apply widget :create)
    widget))

(defun widget-create-child-value (parent type value)
  "Create widget of TYPE with value VALUE."
Per Abrahamsen's avatar
Per Abrahamsen committed
704
  (let ((widget (widget-copy type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
705 706 707 708 709 710 711 712 713 714 715 716 717 718
    (widget-put widget :value (widget-apply widget :value-to-internal value))
    (widget-put widget :parent parent)
    (unless (widget-get widget :indent)
      (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
				    (or (widget-get widget :extra-offset) 0)
				    (widget-get parent :offset))))
    (widget-apply widget :create)
    widget))

;;;###autoload
(defun widget-delete (widget)
  "Delete WIDGET."
  (widget-apply widget :delete))

Per Abrahamsen's avatar
Per Abrahamsen committed
719 720 721 722
(defun widget-copy (widget)
  "Make a deep copy of WIDGET."
  (widget-apply (copy-sequence widget) :copy))

Per Abrahamsen's avatar
Per Abrahamsen committed
723
(defun widget-convert (type &rest args)
724
  "Convert TYPE to a widget without inserting it in the buffer.
Per Abrahamsen's avatar
Per Abrahamsen committed
725 726
The optional ARGS are additional keyword arguments."
  ;; Don't touch the type.
727
  (let* ((widget (if (symbolp type)
Per Abrahamsen's avatar
Per Abrahamsen committed
728
		     (list type)
729
		   (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
730
	 (current widget)
731
	 done
Per Abrahamsen's avatar
Per Abrahamsen committed
732 733 734
	 (keys args))
    ;; First set the :args keyword.
    (while (cdr current)		;Look in the type.
735 736 737 738 739 740 741 742 743 744
      (if (and (keywordp (cadr current))
	       ;; If the last element is a keyword,
	       ;; it is still the :args element,
	       ;; even though it is a keyword.
	       (cddr current))
	  (if (eq (cadr current) :args)
	      ;; If :args is explicitly specified, obey it.
	      (setq current nil)
	    ;; Some other irrelevant keyword.
	    (setq current (cdr (cdr current))))
745 746
	(setcdr current (list :args (cdr current)))
	(setq current nil)))
747 748 749 750 751 752 753 754 755 756
    (while (and args (not done))	;Look in ARGS.
      (cond ((eq (car args) :args)
	     ;; Handle explicit specification of :args.
	     (setq args (cadr args)
		   done t))
	    ((keywordp (car args))
	     (setq args (cddr args)))
	    (t (setq done t))))
    (when done
      (widget-put widget :args args))
Per Abrahamsen's avatar
Per Abrahamsen committed
757 758 759 760 761 762 763 764
    ;; Then Convert the widget.
    (setq type widget)
    (while type
      (let ((convert-widget (plist-get (cdr type) :convert-widget)))
	(if convert-widget
	    (setq widget (funcall convert-widget widget))))
      (setq type (get (car type) 'widget-type)))
    ;; Finally set the keyword args.
765
    (while keys
Per Abrahamsen's avatar
Per Abrahamsen committed
766
      (let ((next (nth 0 keys)))
767 768
	(if (keywordp next)
	    (progn
Per Abrahamsen's avatar
Per Abrahamsen committed
769 770 771 772 773
	      (widget-put widget next (nth 1 keys))
	      (setq keys (nthcdr 2 keys)))
	  (setq keys nil))))
    ;; Convert the :value to internal format.
    (if (widget-member widget :value)
774 775 776 777
	(widget-put widget
		    :value (widget-apply widget
					 :value-to-internal
					 (widget-get widget :value))))
Per Abrahamsen's avatar
Per Abrahamsen committed
778 779 780
    ;; Return the newly create widget.
    widget))

781
;;;###autoload
Per Abrahamsen's avatar
Per Abrahamsen committed
782
(defun widget-insert (&rest args)
783
  "Call `insert' with ARGS even if surrounding text is read only."
Per Abrahamsen's avatar
Per Abrahamsen committed
784
  (let ((inhibit-read-only t)
785
	(inhibit-modification-hooks t))
Per Abrahamsen's avatar
Per Abrahamsen committed
786
    (apply 'insert args)))
Per Abrahamsen's avatar
Per Abrahamsen committed
787

Per Abrahamsen's avatar
Per Abrahamsen committed
788 789 790
(defun widget-convert-text (type from to
				 &optional button-from button-to
				 &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
791
  "Return a widget of type TYPE with endpoint FROM TO.
792 793
No text will be inserted to the buffer, instead the text between FROM
and TO will be used as the widgets end points.  If optional arguments
Per Abrahamsen's avatar
Per Abrahamsen committed
794
BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
Per Abrahamsen's avatar
Per Abrahamsen committed
795 796 797
button end points.
Optional ARGS are extra keyword arguments for TYPE."
  (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
Per Abrahamsen's avatar
Per Abrahamsen committed
798
	(from (copy-marker from))
799
	(to (copy-marker to)))
Per Abrahamsen's avatar
Per Abrahamsen committed
800 801 802 803 804 805 806 807
    (set-marker-insertion-type from t)
    (set-marker-insertion-type to nil)
    (widget-put widget :from from)
    (widget-put widget :to to)
    (when button-from
      (widget-specify-button widget button-from button-to))
    widget))

Per Abrahamsen's avatar
Per Abrahamsen committed
808
(defun widget-convert-button (type from to &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
809
  "Return a widget of type TYPE with endpoint FROM TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
810
Optional ARGS are extra keyword arguments for TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
811 812 813
No text will be inserted to the buffer, instead the text between FROM
and TO will be used as the widgets end points, as well as the widgets
button end points."
Per Abrahamsen's avatar
Per Abrahamsen committed
814 815 816 817
  (apply 'widget-convert-text type from to from to args))

(defun widget-leave-text (widget)
  "Remove markers and overlays from WIDGET and its children."
818
  (let ((button (widget-get widget :button-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
819
	(sample (widget-get widget :sample-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
820
	(doc (widget-get widget :doc-overlay))
821 822 823
	(field (widget-get widget :field-overlay)))
    (set-marker (widget-get widget :from) nil)
    (set-marker (widget-get widget :to) nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
824 825
    (when button
      (delete-overlay button))
Per Abrahamsen's avatar
Per Abrahamsen committed
826 827
    (when sample
      (delete-overlay sample))
Per Abrahamsen's avatar
Per Abrahamsen committed
828 829
    (when doc
      (delete-overlay doc))
Per Abrahamsen's avatar
Per Abrahamsen committed
830 831
    (when field
      (delete-overlay field))
832
    (mapc 'widget-leave-text (widget-get widget :children))))
Per Abrahamsen's avatar
Per Abrahamsen committed
833

Per Abrahamsen's avatar
Per Abrahamsen committed
834 835
;;; Keymap and Commands.

836 837
;; This alias exists only so that one can choose in doc-strings (e.g.
;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
838
;; https://lists.gnu.org/r/emacs-devel/2008-11/msg00480.html
839 840
(define-obsolete-function-alias 'advertised-widget-backward
  'widget-backward "23.2")
841

842
;;;###autoload
843 844 845
(defvar widget-keymap
  (let ((map (make-sparse-keymap)))
    (define-key map "\t" 'widget-forward)
846
    (define-key map "\e\t" 'widget-backward)
847 848
    (define-key map [(shift tab)] 'widget-backward)
    (put 'widget-backward :advertised-binding [(shift tab)])
849
    (define-key map [backtab] 'widget-backward)
850 851
    (define-key map [down-mouse-2] 'widget-button-click)
    (define-key map [down-mouse-1] 'widget-button-click)
Jason Rumney's avatar
Jason Rumney committed
852 853 854
    ;; The following definition needs to avoid using escape sequences that
    ;; might get converted to ^M when building loaddefs.el
    (define-key map [(control ?m)] 'widget-button-press)
855
    map)
Per Abrahamsen's avatar
Per Abrahamsen committed
856
  "Keymap containing useful binding for buffers containing widgets.
Glenn Morris's avatar
Glenn Morris committed
857 858
Recommended as a parent keymap for modes using widgets.
Note that such modes will need to require wid-edit.")
Per Abrahamsen's avatar
Per Abrahamsen committed
859 860

(defvar widget-global-map global-map
Dave Love's avatar
Don't  
Dave Love committed
861
  "Keymap used for events a widget does not handle itself.")
Per Abrahamsen's avatar
Per Abrahamsen committed
862 863
(make-variable-buffer-local 'widget-global-map)

864 865 866 867 868
(defvar widget-field-keymap
  (let ((map (copy-keymap widget-keymap)))
    (define-key map "\C-k" 'widget-kill-line)
    (define-key map "\M-\t" 'widget-complete)
    (define-key map "\C-m" 'widget-field-activate)
869
    ;; Since the widget code uses a `field' property to identify fields,
870
    ;; ordinary beginning-of-line does the right thing.
871
    ;;  (define-key map "\C-a" 'widget-beginning-of-line)
872
    (define-key map "\C-e" 'widget-end-of-line)
873
    map)
Per Abrahamsen's avatar
Per Abrahamsen committed
874 875
  "Keymap used inside an editable field.")

876 877
(defvar widget-text-keymap
  (let ((map (copy-keymap widget-keymap)))
878
    ;; Since the widget code uses a `field' property to identify fields,
879
    ;; ordinary beginning-of-line does the right thing.
880
    ;;  (define-key map "\C-a" 'widget-beginning-of-line)
881
    (define-key map "\C-e" 'widget-end-of-line)
882
    map)
Per Abrahamsen's avatar
Per Abrahamsen committed
883 884 885
  "Keymap used inside a text field.")

(defun widget-field-activate (pos &optional event)
Dave Love's avatar
Dave Love committed
886
  "Invoke the editable field at point."
Per Abrahamsen's avatar
Per Abrahamsen committed
887
  (interactive "@d")
Miles Bader's avatar
Miles Bader committed
888
  (let ((field (widget-field-at pos)))
Per Abrahamsen's avatar
Per Abrahamsen committed
889 890 891 892 893
    (if field
	(widget-apply-action field event)
      (call-interactively
       (lookup-key widget-global-map (this-command-keys))))))

894
(defface widget-button-pressed
895 896 897
  '((((min-colors 88) (class color))
     (:foreground "red1"))
    (((class color))
Per Abrahamsen's avatar
Per Abrahamsen committed
898 899
     (:foreground "red"))
    (t
900
     (:weight bold :underline t)))
Per Abrahamsen's avatar
Per Abrahamsen committed
901
  "Face used for pressed buttons."
Per Abrahamsen's avatar
Per Abrahamsen committed
902
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
903

904 905 906 907
(defvar widget-button-click-moves-point nil
  "If non-nil, `widget-button-click' moves point to a button after invoking it.
If nil, point returns to its original position after invoking a button.")

Per Abrahamsen's avatar
Per Abrahamsen committed
908
(defun widget-button-click (event)
909
  "Invoke the button that the mouse is pointing at."
910
  (interactive "e")
911
  (if (widget-event-point event)
912 913 914
      (let* ((oevent event)
	     (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
	     (pos (widget-event-point event))
915
	     (start (event-start event))
916
	     (button (get-char-property
917
		      pos 'button (and (windowp (posn-window start))
918 919
				       (window-buffer (posn-window start)))))
	     newpoint)
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955
	(when (or (null button)
		  (catch 'button-press-cancelled
	      ;; Mouse click on a widget button.  Do the following
	      ;; in a save-excursion so that the click on the button
	      ;; doesn't change point.
	      (save-selected-window
		(select-window (posn-window (event-start event)))
		(save-excursion
		  (goto-char (posn-point (event-start event)))
		  (let* ((overlay (widget-get button :button-overlay))
			 (pressed-face (or (widget-get button :pressed-face)
					   widget-button-pressed-face))
			 (face (overlay-get overlay 'face))
			 (mouse-face (overlay-get overlay 'mouse-face)))
		    (unwind-protect
			;; Read events, including mouse-movement
			;; events, waiting for a release event.  If we
			;; began with a mouse-1 event and receive a
			;; movement event, that means the user wants
			;; to perform drag-selection, so cancel the
			;; button press and do the default mouse-1
			;; action.  For mouse-2, just highlight/
			;; unhighlight the button the mouse was
			;; initially on when we move over it.
			(save-excursion
			  (when face	; avoid changing around image
			    (overlay-put overlay 'face pressed-face)
			    (overlay-put overlay 'mouse-face pressed-face))
			  (unless (widget-apply button :mouse-down-action event)
			    (let ((track-mouse t))
			      (while (not (widget-button-release-event-p event))
				(setq event (read-event))
				(when (and mouse-1 (mouse-movement-p event))
				  (push event unread-command-events)
				  (setq event oevent)
				  (throw 'button-press-cancelled t))
956 957 958 959 960 961 962 963 964 965 966 967
				(unless (or (integerp event)
					    (memq (car event) '(switch-frame select-window))
					    (eq (car event) 'scroll-bar-movement))
				  (setq pos (widget-event-point event))
				  (if (and pos
					   (eq (get-char-property pos 'button)
					       button))
				      (when face
					(overlay-put overlay 'face pressed-face)
					(overlay-put overlay 'mouse-face pressed-face))
				    (overlay-put overlay 'face face)
				    (overlay-put overlay 'mouse-face mouse-face))))))
968 969 970

			  ;; When mouse is released over the button, run
			  ;; its action function.
971 972 973 974 975
			  (when (and pos (eq (get-char-property pos 'button) button))
			    (goto-char pos)
			    (widget-apply-action button event)
			    (if widget-button-click-moves-point
				(setq newpoint (point)))))
976 977 978
		      (overlay-put overlay 'face face)
		      (overlay-put overlay 'mouse-face mouse-face))))

979
		(if newpoint (goto-char newpoint))
980 981 982 983 984 985 986
		;; This loses if the widget action switches windows. -- cyd
		;; (unless (pos-visible-in-window-p (widget-event-point event))
		;;   (mouse-set-point event)
		;;   (beginning-of-line)
		;;   (recenter))
		)
	      nil))
Richard M. Stallman's avatar
Richard M. Stallman committed
987 988 989 990
	  (let ((up t) command)
	    ;; Mouse click not on a widget button.  Find the global
	    ;; command to run, and check whether it is bound to an
	    ;; up event.
991
	    (if mouse-1
992
		(cond ((setq command	;down event
Richard M. Stallman's avatar
Richard M. Stallman committed
993
			     (lookup-key widget-global-map [down-mouse-1]))
994 995
		       (setq up nil))
		      ((setq command	;up event
Richard M. Stallman's avatar
Richard M. Stallman committed
996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
			     (lookup-key widget-global-map [mouse-1]))))
	      (cond ((setq command	;down event
			   (lookup-key widget-global-map [down-mouse-2]))
		     (setq up nil))
		    ((setq command	;up event
			   (lookup-key widget-global-map [mouse-2])))))
	    (when up
	      ;; Don't execute up events twice.
	      (while (not (widget-button-release-event-p event))
		(setq event (read-event))))
	    (when command
	      (call-interactively command)))))
1008
    (message "You clicked somewhere weird.")))
Per Abrahamsen's avatar
Per Abrahamsen committed
1009

Per Abrahamsen's avatar
Per Abrahamsen committed
1010
(defun widget-button-press (pos &optional event)
Per Abrahamsen's avatar
Per Abrahamsen committed
1011
  "Invoke button at POS."
Per Abrahamsen's avatar
Per Abrahamsen committed
1012
  (interactive "@d")
Per Abrahamsen's avatar
Per Abrahamsen committed
1013
  (let ((button (get-char-property pos 'button)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1014 1015 1016 1017 1018 1019
    (if button
	(widget-apply-action button event)
      (let ((command (lookup-key widget-global-map (this-command-keys))))
	(when (commandp command)
	  (call-interactively command))))))

Per Abrahamsen's avatar
Per Abrahamsen committed
1020 1021 1022
(defun widget-tabable-at (&optional pos)
  "Return the tabable widget at POS, or nil.
POS defaults to the value of (point)."
Miles Bader's avatar
Miles Bader committed
1023
  (let ((widget (widget-at pos)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1024 1025 1026 1027
    (if widget
	(let ((order (widget-get widget :tab-order)))
	  (if order
	      (if (>= order 0)
1028 1029
		  widget)
	    widget)))))