wid-edit.el 114 KB
Newer Older
Per Abrahamsen's avatar
Per Abrahamsen committed
1 2
;;; wid-edit.el --- Functions for creating and using widgets.
;;
3
;; Copyright (C) 1996, 1997, 1999 Free Software Foundation, Inc.
Per Abrahamsen's avatar
Per Abrahamsen committed
4 5
;;
;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6
;; Maintainer: FSF
Per Abrahamsen's avatar
Per Abrahamsen committed
7
;; Keywords: extensions
Per Abrahamsen's avatar
Per Abrahamsen committed
8
;; Version: 1.9951
9
;; X-URL: http://www.dina.kvl.dk/~abraham/custom/ (probably obsolete)
Per Abrahamsen's avatar
Per Abrahamsen committed
10

11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; 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
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

Per Abrahamsen's avatar
Per Abrahamsen committed
28 29 30 31 32 33 34
;;; Commentary:
;;
;; See `widget.el'.

;;; Code:

(require 'widget)
Per Abrahamsen's avatar
Per Abrahamsen committed
35
(eval-when-compile (require 'cl))
Per Abrahamsen's avatar
Per Abrahamsen committed
36 37

;;; Compatibility.
38 39 40
  
(defun widget-event-point (event)
  "Character position of the end of event if that exists, or nil."
41
  (posn-point (event-end event)))
42 43

(defalias 'widget-read-event 'read-event)
Per Abrahamsen's avatar
Per Abrahamsen committed
44 45 46 47

(eval-and-compile
  (autoload 'pp-to-string "pp")
  (autoload 'Info-goto-node "info")
Per Abrahamsen's avatar
Per Abrahamsen committed
48
  (autoload 'finder-commentary "finder" nil t)
Per Abrahamsen's avatar
Per Abrahamsen committed
49

Per Abrahamsen's avatar
Per Abrahamsen committed
50 51 52 53 54 55 56
  (unless (fboundp 'button-release-event-p)
    ;; XEmacs function missing from Emacs.
    (defun 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))
57
	       (memq  'drag (event-modifiers event)))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
58 59 60 61 62 63 64 65

;;; Customization.

(defgroup widgets nil
  "Customization support for the Widget Library."
  :link '(custom-manual "(widget)Top")
  :link '(url-link :tag "Development Page" 
		   "http://www.dina.kvl.dk/~abraham/custom/")
Per Abrahamsen's avatar
Per Abrahamsen committed
66
  :link '(emacs-library-link :tag "Lisp File" "widget.el")
Per Abrahamsen's avatar
Per Abrahamsen committed
67 68 69 70
  :prefix "widget-"
  :group 'extensions
  :group 'hypermedia)

Per Abrahamsen's avatar
Per Abrahamsen committed
71 72 73 74
(defgroup widget-documentation nil
  "Options controling the display of documentation strings."
  :group 'widgets)

Per Abrahamsen's avatar
Per Abrahamsen committed
75 76 77 78 79
(defgroup widget-faces nil
  "Faces used by the widget library."
  :group 'widgets
  :group 'faces)

80
(defvar widget-documentation-face 'widget-documentation-face
81
  "Face used for documentation strings in widgets.
82 83
This exists as a variable so it can be set locally in certain buffers.")

Per Abrahamsen's avatar
Per Abrahamsen committed
84 85 86 87 88 89 90 91 92 93 94
(defface widget-documentation-face '((((class color)
				       (background dark))
				      (:foreground "lime green"))
				     (((class color)
				       (background light))
				      (:foreground "dark green"))
				     (t nil))
  "Face used for documentation text."
  :group 'widget-documentation
  :group 'widget-faces)

95
(defvar widget-button-face 'widget-button-face
96
  "Face used for buttons in widgets.
97 98
This exists as a variable so it can be set locally in certain buffers.")

Per Abrahamsen's avatar
Per Abrahamsen committed
99 100
(defface widget-button-face '((t (:bold t)))
  "Face used for widget buttons."
Per Abrahamsen's avatar
Per Abrahamsen committed
101
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
102 103 104 105

(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
106
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
107 108 109

(defface widget-field-face '((((class grayscale color)
			       (background light))
Per Abrahamsen's avatar
Per Abrahamsen committed
110
			      (:background "gray85"))
Per Abrahamsen's avatar
Per Abrahamsen committed
111 112
			     (((class grayscale color)
			       (background dark))
Per Abrahamsen's avatar
Per Abrahamsen committed
113
			      (:background "dim gray"))
Per Abrahamsen's avatar
Per Abrahamsen committed
114 115 116
			     (t 
			      (:italic t)))
  "Face used for editable fields."
Per Abrahamsen's avatar
Per Abrahamsen committed
117
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
118

Per Abrahamsen's avatar
Per Abrahamsen committed
119 120 121 122 123 124 125 126 127 128 129
(defface widget-single-line-field-face '((((class grayscale color)
					   (background light))
					  (:background "gray85"))
					 (((class grayscale color)
					   (background dark))
					  (:background "dim gray"))
					 (t 
					  (:italic t)))
  "Face used for editable fields spanning only a single line."
  :group 'widget-faces)

130 131 132 133 134 135 136 137 138 139 140
;;; 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
141

Per Abrahamsen's avatar
Per Abrahamsen committed
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
;;; Utility functions.
;;
;; These are not really widget specific.

(defun widget-princ-to-string (object)
  ;; Return string representation of OBJECT, any Lisp object.
  ;; No quoting characters are used; no delimiters are printed around
  ;; the contents of strings.
  (save-excursion
    (set-buffer (get-buffer-create " *widget-tmp*"))
    (erase-buffer)
    (let ((standard-output (current-buffer)))
      (princ object))
    (buffer-string)))

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

Per Abrahamsen's avatar
Per Abrahamsen committed
162 163 164 165 166 167
(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)

168 169 170 171 172 173 174
(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
175 176 177 178 179 180
  "*Control how to ask for a choice from the keyboard.
Non-nil means use the minibuffer;
nil means read a single character."
  :group 'widgets
  :type 'boolean)

Per Abrahamsen's avatar
Per Abrahamsen committed
181 182 183 184
(defun widget-choose (title items &optional event)
  "Choose an item from a list.

First argument TITLE is the name of the list.
Per Abrahamsen's avatar
Per Abrahamsen committed
185 186 187
Second argument ITEMS is an list whose members are either
 (NAME . VALUE), to indicate selectable items, or just strings to
 indicate unselectable items.
Per Abrahamsen's avatar
Per Abrahamsen committed
188 189 190 191 192 193 194 195 196 197 198 199
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)
	      event (fboundp 'x-popup-menu) window-system)
	 ;; We are in Emacs-19, pressed by the mouse
	 (x-popup-menu event
		       (list title (cons "" items))))
200 201
	((or widget-menu-minibuffer-flag
	     (> (length items) widget-menu-max-shortcuts))
202
	 ;; Read the choice of name from the minibuffer.
203
	 (setq items (widget-remove-if 'stringp items))
Per Abrahamsen's avatar
Per Abrahamsen committed
204 205 206 207 208 209
	 (let ((val (completing-read (concat title ": ") items nil t)))
	   (if (stringp val)
	       (let ((try (try-completion val items)))
		 (when (stringp try)
		   (setq val try))
		 (cdr (assoc val items)))
210 211 212 213 214 215 216
	     nil)))
	(t
	 ;; Construct a menu of the choices
	 ;; and then use it for prompting for a single character.
	 (let* ((overriding-terminal-local-map
		 (make-sparse-keymap))
		map choice (next-digit ?0)
217
		some-choice-enabled
218 219 220 221
		value)
	   ;; Define SPC as a prefix char to get to this menu.
	   (define-key overriding-terminal-local-map " "
	     (setq map (make-sparse-keymap title)))
222 223 224 225 226 227 228 229 230 231
	   (save-excursion
	     (set-buffer (get-buffer-create " widget-choose"))
	     (erase-buffer)
	     (insert "Available choices:\n\n")
	     (while items
	       (setq choice (car items) items (cdr items))
	       (if (consp choice)
		   (let* ((name (car choice))
			 (function (cdr choice)))
		     (insert (format "%c = %s\n" next-digit name))
232 233
		     (define-key map (vector next-digit) function)
		     (setq some-choice-enabled t)))
234 235 236 237
	       ;; Allocate digits to disabled alternatives
	       ;; so that the digit of a given alternative never varies.
	       (setq next-digit (1+ next-digit)))
	     (insert "\nC-g = Quit"))
238 239
	   (or some-choice-enabled
	       (error "None of the choices is currently meaningful"))
240
	   (define-key map [?\C-g] 'keyboard-quit)
241
	   (define-key map [t] 'keyboard-quit)
242 243
	   (define-key map [?\M-\C-v] 'scroll-other-window)
	   (define-key map [?\M--] 'negative-argument)
244 245 246
	   (setcdr map (nreverse (cdr map)))
	   ;; Read a char with the menu, and return the result
	   ;; that corresponds to it.
247
	   (save-window-excursion
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
	     (let ((buf (get-buffer " widget-choose")))
	       (display-buffer buf)
	       (let ((cursor-in-echo-area t)
		     keys
		     (char 0)
		     (arg 1))
		 (while (not (or (and (>= char ?0) (< char next-digit))
				 (eq value 'keyboard-quit)))
		   ;; Unread a SPC to lead to our new menu.
		   (setq unread-command-events (cons ?\ unread-command-events))
		   (setq keys (read-key-sequence title))
		   (setq value (lookup-key overriding-terminal-local-map keys t)
			 char (string-to-char (substring keys 1)))
		   (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))
			 (t
			  (setq arg 1)))))))
271 272 273
	   (when (eq value 'keyboard-quit)
	     (error "Canceled"))
	   value))))
Per Abrahamsen's avatar
Per Abrahamsen committed
274

275 276 277 278 279 280 281 282
(defun widget-remove-if (predictate list)
  (let (result (tail list))
    (while tail
      (or (funcall predictate (car tail))
	  (setq result (cons (car tail) result)))
      (setq tail (cdr tail)))
    (nreverse result)))

Per Abrahamsen's avatar
Per Abrahamsen committed
283 284 285 286
;;; Widget text specifications.
;; 
;; These functions are for specifying text properties. 

Per Abrahamsen's avatar
Per Abrahamsen committed
287 288 289 290 291 292 293 294 295 296 297 298 299
(defcustom widget-field-add-space 
  (or (< emacs-major-version 20)
      (and (eq emacs-major-version 20)
	   (< emacs-minor-version 3))
      (not (string-match "XEmacs" emacs-version)))
  "Non-nil means add extra space at the end of editable text fields.

This is needed on all versions of Emacs, and on XEmacs before 20.3.  
If you don't add the space, it will become impossible to edit a zero
size field."
  :type 'boolean
  :group 'widgets)

Per Abrahamsen's avatar
Per Abrahamsen committed
300
(defcustom widget-field-use-before-change
Per Abrahamsen's avatar
Per Abrahamsen committed
301 302 303
  (and (or (> emacs-minor-version 34)
	   (> emacs-major-version 19))
       (not (string-match "XEmacs" emacs-version)))
Per Abrahamsen's avatar
Per Abrahamsen committed
304
  "Non-nil means use `before-change-functions' to track editable fields.
305
This enables the use of undo, but doesn't work on Emacs 19.34 and earlier. 
Per Abrahamsen's avatar
Per Abrahamsen committed
306 307 308 309 310
Using before hooks also means that the :notify function can't know the
new value."
  :type 'boolean
  :group 'widgets)

Per Abrahamsen's avatar
Per Abrahamsen committed
311
(defun widget-specify-field (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
312
  "Specify editable button for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
313 314 315 316 317
  ;; 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
318 319 320 321
    (cond ((null (widget-get widget :size))
	   (forward-char 1))
	  (widget-field-add-space
	   (insert-and-inherit " ")))
Per Abrahamsen's avatar
Per Abrahamsen committed
322
    (setq to (point)))
Per Abrahamsen's avatar
Per Abrahamsen committed
323
  (let ((map (widget-get widget :keymap))
Per Abrahamsen's avatar
Per Abrahamsen committed
324 325
	(face (or (widget-get widget :value-face) 'widget-field-face))
	(help-echo (widget-get widget :help-echo))
Per Abrahamsen's avatar
Per Abrahamsen committed
326 327 328
	(overlay (make-overlay from to nil 
			       nil (or (not widget-field-add-space)
				       (widget-get widget :size)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
329
    (unless (or (stringp help-echo) (null help-echo))
Per Abrahamsen's avatar
Per Abrahamsen committed
330 331
      (setq help-echo 'widget-mouse-help))    
    (widget-put widget :field-overlay overlay)
332
    ;;(overlay-put overlay 'detachable nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
333 334
    (overlay-put overlay 'field widget)
    (overlay-put overlay 'local-map map)
335
    ;;(overlay-put overlay 'keymap map)
Per Abrahamsen's avatar
Per Abrahamsen committed
336
    (overlay-put overlay 'face face)
337
    ;;(overlay-put overlay 'balloon-help help-echo)
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    (overlay-put overlay 'help-echo help-echo))
  (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)))
	(when size 
	  (while (and (> end begin)
		      (eq (char-after (1- end)) ?\ ))
	    (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
358 359

(defun widget-specify-button (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
360
  "Specify button for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
361
  (let ((face (widget-apply widget :button-face-get))
Per Abrahamsen's avatar
Per Abrahamsen committed
362 363 364
	(help-echo (widget-get widget :help-echo))
	(overlay (make-overlay from to nil t nil)))
    (widget-put widget :button-overlay overlay)
Per Abrahamsen's avatar
Per Abrahamsen committed
365 366
    (unless (or (null help-echo) (stringp help-echo))
      (setq help-echo 'widget-mouse-help))
Per Abrahamsen's avatar
Per Abrahamsen committed
367 368
    (overlay-put overlay 'button widget)
    (overlay-put overlay 'mouse-face widget-mouse-face)
369
    ;;(overlay-put overlay 'balloon-help help-echo)
Per Abrahamsen's avatar
Per Abrahamsen committed
370 371
    (overlay-put overlay 'help-echo help-echo)
    (overlay-put overlay 'face face)))
Per Abrahamsen's avatar
Per Abrahamsen committed
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386

(defun widget-mouse-help (extent)
  "Find mouse help string for button in extent."
  (let* ((widget (widget-at (extent-start-position extent)))
	 (help-echo (and widget (widget-get widget :help-echo))))
    (cond ((stringp help-echo)
	   help-echo)
	  ((and (symbolp help-echo) (fboundp help-echo)
		(stringp (setq help-echo (funcall help-echo widget))))
	   help-echo)
	  (t
	   (format "(widget %S :help-echo %S)" widget help-echo)))))

(defun widget-specify-sample (widget from to)
  ;; Specify sample for WIDGET between FROM and TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
387 388 389 390 391
  (let ((face (widget-apply widget :sample-face-get))
	(overlay (make-overlay from to nil t nil)))
    (overlay-put overlay 'face face)
    (widget-put widget :sample-overlay overlay)))

Per Abrahamsen's avatar
Per Abrahamsen committed
392 393
(defun widget-specify-doc (widget from to)
  ;; Specify documentation for WIDGET between FROM and TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
394 395 396 397
  (let ((overlay (make-overlay from to nil t nil)))
    (overlay-put overlay 'widget-doc widget)
    (overlay-put overlay 'face widget-documentation-face)
    (widget-put widget :doc-overlay overlay)))
Per Abrahamsen's avatar
Per Abrahamsen committed
398 399 400

(defmacro widget-specify-insert (&rest form)
  ;; Execute FORM without inheriting any text properties.
Per Abrahamsen's avatar
Per Abrahamsen committed
401 402
  (`
   (save-restriction
Per Abrahamsen's avatar
Per Abrahamsen committed
403 404
     (let ((inhibit-read-only t)
	   result
405
	   before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
406 407 408 409
	   after-change-functions)
       (insert "<>")
       (narrow-to-region (- (point) 2) (point))
       (goto-char (1+ (point-min)))
Per Abrahamsen's avatar
Per Abrahamsen committed
410
       (setq result (progn (,@ form)))
Per Abrahamsen's avatar
Per Abrahamsen committed
411 412 413
       (delete-region (point-min) (1+ (point-min)))
       (delete-region (1- (point-max)) (point-max))
       (goto-char (point-max))
Per Abrahamsen's avatar
Per Abrahamsen committed
414
       result))))
Per Abrahamsen's avatar
Per Abrahamsen committed
415 416 417 418 419 420

(defface widget-inactive-face '((((class grayscale color)
				  (background dark))
				 (:foreground "light gray"))
				(((class grayscale color)
				  (background light))
Per Abrahamsen's avatar
Per Abrahamsen committed
421
				 (:foreground "dim gray"))
Per Abrahamsen's avatar
Per Abrahamsen committed
422 423 424
				(t 
				 (:italic t)))
  "Face used for inactive widgets."
Per Abrahamsen's avatar
Per Abrahamsen committed
425
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
426 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)))
      (overlay-put overlay 'face 'widget-inactive-face)
Per Abrahamsen's avatar
Per Abrahamsen committed
432 433
      ;; This is disabled, as it makes the mouse cursor change shape.
      ;; (overlay-put overlay 'mouse-face 'widget-inactive-face)
Per Abrahamsen's avatar
Per Abrahamsen committed
434 435
      (overlay-put overlay 'evaporate t)
      (overlay-put overlay 'priority 100)
436
      (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
Per Abrahamsen's avatar
Per Abrahamsen committed
437 438 439 440 441
      (widget-put widget :inactive overlay))))

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


(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)
  "Return the type of WIDGET, a symbol."
  (car widget))

Per Abrahamsen's avatar
Per Abrahamsen committed
458 459 460 461 462 463 464 465 466
(defun widget-get-indirect (widget property)
  "In WIDGET, get the value of PROPERTY.
If the value is a symbol, return its binding.  
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
467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485
(defun widget-member (widget property)
  "Non-nil iff there is a definition in WIDGET for PROPERTY."
  (cond ((widget-plist-member (cdr widget) property)
	 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)))

486
(defun widget-default-get (widget)
487
  "Extract the default value of WIDGET."
488 489 490
  (or (widget-get widget :value)
      (widget-apply widget :default-get)))

Per Abrahamsen's avatar
Per Abrahamsen committed
491
(defun widget-match-inline (widget vals)
492
  "In WIDGET, match the start of VALS."
Per Abrahamsen's avatar
Per Abrahamsen committed
493 494 495 496 497 498 499 500 501
  (cond ((widget-get widget :inline)
	 (widget-apply widget :match-inline vals))
	((and vals
	      (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
502 503 504
  (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
505

Per Abrahamsen's avatar
Per Abrahamsen committed
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
;;; 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)
      (error "Value does not match %S type." (car widget)))
    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."
  (let* ((parent (widget-get widget :parent))
	 (children (widget-get parent :children))
	 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
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
(defun widget-map-buttons (function &optional buffer maparg)
  "Map FUNCTION over the buttons in BUFFER.
FUNCTION is called with the arguments WIDGET and MAPARG.

If FUNCTION returns non-nil, the walk is cancelled.

The arguments MAPARG, and BUFFER default to nil and (current-buffer),
respectively."
  (let ((cur (point-min))
	(widget nil)
	(parent nil)
	(overlays (if buffer
		      (save-excursion (set-buffer buffer) (overlay-lists))
		    (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)))))

Per Abrahamsen's avatar
Per Abrahamsen committed
557 558 559 560 561
;;; Glyphs.

(defcustom widget-glyph-directory (concat data-directory "custom/")
  "Where widget glyphs are located.
If this variable is nil, widget will try to locate the directory
Per Abrahamsen's avatar
Per Abrahamsen committed
562
automatically."
Per Abrahamsen's avatar
Per Abrahamsen committed
563 564 565 566 567 568 569 570
  :group 'widgets
  :type 'directory)

(defcustom widget-glyph-enable t
  "If non nil, use glyphs in images when available."
  :group 'widgets
  :type 'boolean)

Per Abrahamsen's avatar
Per Abrahamsen committed
571 572 573 574 575 576 577 578 579 580
(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")))))

Per Abrahamsen's avatar
Per Abrahamsen committed
581 582 583 584 585 586 587
(defun widget-glyph-find (image tag)
  "Create a glyph corresponding to IMAGE with string TAG as fallback.
IMAGE should either already be a glyph, or be a file name sans
extension (xpm, xbm, gif, jpg, or png) located in
`widget-glyph-directory'." 
  (cond ((not (and image 
		   (string-match "XEmacs" emacs-version)
Per Abrahamsen's avatar
Per Abrahamsen committed
588 589
		   widget-glyph-enable
		   (fboundp 'make-glyph)
Per Abrahamsen's avatar
Per Abrahamsen committed
590
		   (fboundp 'locate-file)
Per Abrahamsen's avatar
Per Abrahamsen committed
591 592
		   image))
	 ;; We don't want or can't use glyphs.
Per Abrahamsen's avatar
Per Abrahamsen committed
593
	 nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
594 595
	((and (fboundp 'glyphp)
	      (glyphp image))
Per Abrahamsen's avatar
Per Abrahamsen committed
596 597
	 ;; Already a glyph.  Use it.
	 image)
Per Abrahamsen's avatar
Per Abrahamsen committed
598 599 600 601 602 603 604 605 606
	((stringp image)
	 ;; A string.  Look it up in relevant directories.
	 (let* ((dirlist (list (or widget-glyph-directory
				   (concat data-directory
					   "custom/"))
			       data-directory))
		(formats widget-image-conversion)
		file)
	   (while (and formats (not file))
Per Abrahamsen's avatar
Per Abrahamsen committed
607 608 609 610 611 612
	     (when (valid-image-instantiator-format-p (car (car formats)))
	       (setq file (locate-file image dirlist
				       (mapconcat 'identity
						  (cdr (car formats))
						  ":"))))
	     (unless file
Per Abrahamsen's avatar
Per Abrahamsen committed
613
	       (setq formats (cdr formats))))
Per Abrahamsen's avatar
Per Abrahamsen committed
614 615 616 617 618
	   (and file
		;; We create a glyph with the file as the default image
		;; instantiator, and the TAG fallback
		(make-glyph (list (vector (car (car formats)) ':file file)
				  (vector 'string ':data tag))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
619 620
	((valid-instantiator-p image 'image)
	 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
Per Abrahamsen's avatar
Per Abrahamsen committed
621 622
	 (make-glyph (list image
			   (vector 'string ':data tag))))
Per Abrahamsen's avatar
Per Abrahamsen committed
623 624 625
	((consp image)
	 ;; This could be virtually anything.  Let `make-glyph' sort it out.
	 (make-glyph image))
Per Abrahamsen's avatar
Per Abrahamsen committed
626
	(t
Per Abrahamsen's avatar
Per Abrahamsen committed
627
	 ;; Oh well.
Per Abrahamsen's avatar
Per Abrahamsen committed
628 629 630 631 632 633 634 635 636 637 638 639 640 641
	 nil)))

(defun widget-glyph-insert (widget tag image &optional down inactive)
  "In WIDGET, insert the text TAG or, if supported, IMAGE.
IMAGE should either be a glyph, an image instantiator, or an image file
name sans extension (xpm, xbm, gif, jpg, or png) located in
`widget-glyph-directory'.

Optional arguments DOWN and INACTIVE is used instead of IMAGE when the
glyph is pressed or inactive, respectively. 

WARNING: If you call this with a glyph, and you want the user to be
able to invoke the glyph, make sure it is unique.  If you use the
same glyph for multiple widgets, invoking any of the glyphs will
Per Abrahamsen's avatar
Per Abrahamsen committed
642 643 644 645 646
cause the last created widget to be invoked.

Instead of an instantiator, you can also use a list of instantiators,
or whatever `make-glyph' will accept.  However, in that case you must
provide the fallback TAG as a part of the instantiator yourself."
Per Abrahamsen's avatar
Per Abrahamsen committed
647 648 649 650 651 652 653
  (let ((glyph (widget-glyph-find image tag)))
    (if glyph 
	(widget-glyph-insert-glyph widget 
				   glyph
				   (widget-glyph-find down tag)
				   (widget-glyph-find inactive tag))
      (insert tag))))
Per Abrahamsen's avatar
Per Abrahamsen committed
654 655

(defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
656 657 658
  "In WIDGET, insert GLYPH.
If optional arguments DOWN and INACTIVE are given, they should be
glyphs used when the widget is pushed and inactive, respectively."
Per Abrahamsen's avatar
Per Abrahamsen committed
659 660 661 662 663 664
  (when widget
    (set-glyph-property glyph 'widget widget)
    (when down
      (set-glyph-property down 'widget widget))
    (when inactive
      (set-glyph-property inactive 'widget widget)))
Per Abrahamsen's avatar
Per Abrahamsen committed
665
  (insert "*")
Per Abrahamsen's avatar
Per Abrahamsen committed
666
  (let ((ext (make-extent (point) (1- (point))))
Per Abrahamsen's avatar
Per Abrahamsen committed
667
	(help-echo (and widget (widget-get widget :help-echo))))
Per Abrahamsen's avatar
Per Abrahamsen committed
668
    (set-extent-property ext 'invisible t)
Per Abrahamsen's avatar
Per Abrahamsen committed
669 670
    (set-extent-property ext 'start-open t)
    (set-extent-property ext 'end-open t)
Per Abrahamsen's avatar
Per Abrahamsen committed
671 672 673 674
    (set-extent-end-glyph ext glyph)
    (when help-echo
      (set-extent-property ext 'balloon-help help-echo)
      (set-extent-property ext 'help-echo help-echo)))
Per Abrahamsen's avatar
Per Abrahamsen committed
675 676 677 678
  (when widget
    (widget-put widget :glyph-up glyph)
    (when down (widget-put widget :glyph-down down))
    (when inactive (widget-put widget :glyph-inactive inactive))))
Per Abrahamsen's avatar
Per Abrahamsen committed
679

Per Abrahamsen's avatar
Per Abrahamsen committed
680 681 682 683 684 685 686 687 688
;;; 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
689
  :group 'widget-button)
Per Abrahamsen's avatar
Per Abrahamsen committed
690 691 692 693

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

Per Abrahamsen's avatar
Per Abrahamsen committed
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
;;; Creating Widgets.

;;;###autoload
(defun widget-create (type &rest args)
  "Create widget of TYPE.  
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."
720
  (let ((widget (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
721 722 723 724 725 726 727 728 729 730
    (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."
731
  (let ((widget (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
    (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))

(defun widget-convert (type &rest args)
  "Convert TYPE to a widget without inserting it in the buffer. 
The optional ARGS are additional keyword arguments."
  ;; Don't touch the type.
  (let* ((widget (if (symbolp type) 
		     (list type)
752
		   (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793
	 (current widget)
	 (keys args))
    ;; First set the :args keyword.
    (while (cdr current)		;Look in the type.
      (let ((next (car (cdr current))))
	(if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
	    (setq current (cdr (cdr current)))
	  (setcdr current (list :args (cdr current)))
	  (setq current nil))))
    (while args				;Look in the args.
      (let ((next (nth 0 args)))
	(if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
	    (setq args (nthcdr 2 args))
	  (widget-put widget :args args)
	  (setq args nil))))
    ;; 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.
    (while keys 
      (let ((next (nth 0 keys)))
	(if (and (symbolp next) (eq (aref (symbol-name next) 0) ?:))
	    (progn 
	      (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)
	(let ((value (widget-get widget :value)))
	  (widget-put widget
		      :value (widget-apply widget :value-to-internal value))))
    ;; Return the newly create widget.
    widget))

(defun widget-insert (&rest args)
  "Call `insert' with ARGS and make the text read only."
  (let ((inhibit-read-only t)
794
	before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
795 796
	after-change-functions
	(from (point)))
Per Abrahamsen's avatar
Per Abrahamsen committed
797
    (apply 'insert args)))
Per Abrahamsen's avatar
Per Abrahamsen committed
798

Per Abrahamsen's avatar
Per Abrahamsen committed
799 800 801
(defun widget-convert-text (type from to
				 &optional button-from button-to
				 &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
802
  "Return a widget of type TYPE with endpoint FROM TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
803
Optional ARGS are extra keyword arguments for TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
804 805
and TO will be used as the widgets end points. If optional arguments
BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
Per Abrahamsen's avatar
Per Abrahamsen committed
806 807 808
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
809 810 811 812 813 814 815 816 817 818
	(from (copy-marker from))
	(to (copy-marker to)))
    (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
819
(defun widget-convert-button (type from to &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
820
  "Return a widget of type TYPE with endpoint FROM TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
821
Optional ARGS are extra keyword arguments for TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
822 823 824
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
825 826 827 828 829 830 831
  (apply 'widget-convert-text type from to from to args))

(defun widget-leave-text (widget)
  "Remove markers and overlays from WIDGET and its children."
  (let ((from (widget-get widget :from))
	(to (widget-get widget :to))
	(button (widget-get widget :button-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
832
	(sample (widget-get widget :sample-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
833
	(doc (widget-get widget :doc-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
834 835 836 837
	(field (widget-get widget :field-overlay))
	(children (widget-get widget :children)))
    (set-marker from nil)
    (set-marker to nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
838 839
    (when button
      (delete-overlay button))
Per Abrahamsen's avatar
Per Abrahamsen committed
840 841
    (when sample
      (delete-overlay sample))
Per Abrahamsen's avatar
Per Abrahamsen committed
842 843
    (when doc
      (delete-overlay doc))
Per Abrahamsen's avatar
Per Abrahamsen committed
844 845
    (when field
      (delete-overlay field))
Per Abrahamsen's avatar
Per Abrahamsen committed
846
    (mapcar 'widget-leave-text children)))
Per Abrahamsen's avatar
Per Abrahamsen committed
847

Per Abrahamsen's avatar
Per Abrahamsen committed
848 849 850 851 852 853 854 855 856 857 858
;;; Keymap and Commands.

(defvar widget-keymap nil
  "Keymap containing useful binding for buffers containing widgets.
Recommended as a parent keymap for modes using widgets.")

(unless widget-keymap 
  (setq widget-keymap (make-sparse-keymap))
  (define-key widget-keymap "\t" 'widget-forward)
  (define-key widget-keymap [(shift tab)] 'widget-backward)
  (define-key widget-keymap [backtab] 'widget-backward)
Per Abrahamsen's avatar
Per Abrahamsen committed
859
  (if (string-match "XEmacs" emacs-version)
Per Abrahamsen's avatar
Per Abrahamsen committed
860
      (progn 
Per Abrahamsen's avatar
Per Abrahamsen committed
861 862 863
	;;Glyph support.
	(define-key widget-keymap [button1] 'widget-button1-click) 
	(define-key widget-keymap [button2] 'widget-button-click))
Per Abrahamsen's avatar
Per Abrahamsen committed
864 865 866 867 868 869 870 871 872 873 874 875
    (define-key widget-keymap [down-mouse-2] 'widget-button-click))
  (define-key widget-keymap "\C-m" 'widget-button-press))

(defvar widget-global-map global-map
  "Keymap used for events the widget does not handle themselves.")
(make-variable-buffer-local 'widget-global-map)

(defvar widget-field-keymap nil
  "Keymap used inside an editable field.")

(unless widget-field-keymap 
  (setq widget-field-keymap (copy-keymap widget-keymap))
876
  (define-key widget-field-keymap [menu-bar] 'nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
877 878
  (define-key widget-field-keymap "\C-k" 'widget-kill-line)
  (define-key widget-field-keymap "\M-\t" 'widget-complete)
Per Abrahamsen's avatar
Per Abrahamsen committed
879 880 881 882 883 884 885 886 887 888
  (define-key widget-field-keymap "\C-m" 'widget-field-activate)
  (define-key widget-field-keymap "\C-a" 'widget-beginning-of-line)
  (define-key widget-field-keymap "\C-e" 'widget-end-of-line)
  (set-keymap-parent widget-field-keymap global-map))

(defvar widget-text-keymap nil
  "Keymap used inside a text field.")

(unless widget-text-keymap 
  (setq widget-text-keymap (copy-keymap widget-keymap))
889
  (define-key widget-text-keymap [menu-bar] 'nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
890 891 892 893 894
  (define-key widget-text-keymap "\C-a" 'widget-beginning-of-line)
  (define-key widget-text-keymap "\C-e" 'widget-end-of-line)
  (set-keymap-parent widget-text-keymap global-map))

(defun widget-field-activate (pos &optional event)
Per Abrahamsen's avatar
Per Abrahamsen committed
895
  "Invoke the ediable field at point."
Per Abrahamsen's avatar
Per Abrahamsen committed
896
  (interactive "@d")
Per Abrahamsen's avatar
Per Abrahamsen committed
897
  (let ((field (get-char-property pos 'field)))
Per Abrahamsen's avatar
Per Abrahamsen committed
898 899 900 901 902
    (if field
	(widget-apply-action field event)
      (call-interactively
       (lookup-key widget-global-map (this-command-keys))))))

903 904 905 906
(defvar widget-button-pressed-face 'widget-button-pressed-face
  "Face used for pressed buttons in widgets.
This exists as a variable so it can be set locally in certain buffers.")

Per Abrahamsen's avatar
Per Abrahamsen committed
907 908 909 910 911 912
(defface widget-button-pressed-face 
  '((((class color))
     (:foreground "red"))
    (t
     (:bold t :underline t)))
  "Face used for pressed buttons."
Per Abrahamsen's avatar
Per Abrahamsen committed
913
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
914

Per Abrahamsen's avatar
Per Abrahamsen committed
915
(defun widget-button-click (event)
916
  "Invoke the button that the mouse is pointing at, and move there."
Per Abrahamsen's avatar
Per Abrahamsen committed
917
  (interactive "@e")
918
  (mouse-set-point event)
Per Abrahamsen's avatar
Per Abrahamsen committed
919 920
  (cond ((and (fboundp 'event-glyph)
	      (event-glyph event))
Per Abrahamsen's avatar
Per Abrahamsen committed
921 922 923
	 (widget-glyph-click event))
	((widget-event-point event)
	 (let* ((pos (widget-event-point event))
Per Abrahamsen's avatar
Per Abrahamsen committed
924
		(button (get-char-property pos 'button)))
Per Abrahamsen's avatar
Per Abrahamsen committed
925
	   (if button
Per Abrahamsen's avatar
Per Abrahamsen committed
926 927
	       (let* ((overlay (widget-get button :button-overlay))
		      (face (overlay-get overlay 'face))
Per Abrahamsen's avatar
Per Abrahamsen committed
928
		      (mouse-face (overlay-get overlay 'mouse-face)))
Per Abrahamsen's avatar
Per Abrahamsen committed
929 930
		 (unwind-protect
		     (let ((track-mouse t))
Per Abrahamsen's avatar
Per Abrahamsen committed
931
		       (overlay-put overlay
932
				    'face widget-button-pressed-face)
Per Abrahamsen's avatar
Per Abrahamsen committed
933
		       (overlay-put overlay 
934
				    'mouse-face widget-button-pressed-face)
Per Abrahamsen's avatar
Per Abrahamsen committed
935 936
		       (unless (widget-apply button :mouse-down-action event)
			 (while (not (button-release-event-p event))
Per Abrahamsen's avatar
Per Abrahamsen committed
937
			   (setq event (widget-read-event)
Per Abrahamsen's avatar
Per Abrahamsen committed
938 939
				 pos (widget-event-point event))
			   (if (and pos
Per Abrahamsen's avatar
Per Abrahamsen committed
940
				    (eq (get-char-property pos 'button)
Per Abrahamsen's avatar
Per Abrahamsen committed
941 942 943 944
					button))
			       (progn 
				 (overlay-put overlay 
					      'face
945
					      widget-button-pressed-face)
Per Abrahamsen's avatar
Per Abrahamsen committed
946 947
				 (overlay-put overlay 
					      'mouse-face 
948
					      widget-button-pressed-face))
Per Abrahamsen's avatar
Per Abrahamsen committed
949 950
			     (overlay-put overlay 'face face)
			     (overlay-put overlay 'mouse-face mouse-face))))
Per Abrahamsen's avatar
Per Abrahamsen committed
951
		       (when (and pos 
Per Abrahamsen's avatar
Per Abrahamsen committed
952
				  (eq (get-char-property pos 'button) button))
Per Abrahamsen's avatar
Per Abrahamsen committed
953
			 (widget-apply-action button event)))
Per Abrahamsen's avatar
Per Abrahamsen committed
954 955
		   (overlay-put overlay 'face face)
		   (overlay-put overlay 'mouse-face mouse-face)))
Per Abrahamsen's avatar
Per Abrahamsen committed
956 957
	     (let ((up t)
		   command)
Per Abrahamsen's avatar
Per Abrahamsen committed
958 959 960
	       ;; Find the global command to run, and check whether it
	       ;; is bound to an up event.
	       (cond ((setq command	;down event
Per Abrahamsen's avatar
Per Abrahamsen committed
961 962
			    (lookup-key widget-global-map [ button2 ]))
		      (setq up nil))
Per Abrahamsen's avatar
Per Abrahamsen committed
963
		     ((setq command	;down event
Per Abrahamsen's avatar
Per Abrahamsen committed
964 965
			    (lookup-key widget-global-map [ down-mouse-2 ]))
		      (setq up nil))
Per Abrahamsen's avatar
Per Abrahamsen committed
966
		     ((setq command	;up event
Per Abrahamsen's avatar
Per Abrahamsen committed
967
			    (lookup-key widget-global-map [ button2up ])))
Per Abrahamsen's avatar
Per Abrahamsen committed
968
		     ((setq command	;up event
Per Abrahamsen's avatar
Per Abrahamsen committed
969 970
			    (lookup-key widget-global-map [ mouse-2]))))
	       (when up
Per Abrahamsen's avatar
Per Abrahamsen committed
971
		 ;; Don't execute up events twice.
Per Abrahamsen's avatar
Per Abrahamsen committed
972 973 974
		 (while (not (button-release-event-p event))
		   (setq event (widget-read-event))))
	       (when command
Per Abrahamsen's avatar
Per Abrahamsen committed
975
		 (call-interactively command))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
976 977 978 979
	(t
	 (message "You clicked somewhere weird."))))

(defun widget-button1-click (event)
Per Abrahamsen's avatar
Per Abrahamsen committed
980
  "Invoke glyph below mouse pointer."
Per Abrahamsen's avatar
Per Abrahamsen committed
981 982 983
  (interactive "@e")
  (if (and (fboundp 'event-glyph)
	   (event-glyph event))
Per Abrahamsen's avatar
Per Abrahamsen committed
984
      (widget-glyph-click event)
Per Abrahamsen's avatar
Per Abrahamsen committed
985 986
    (call-interactively (lookup-key widget-global-map (this-command-keys)))))

Per Abrahamsen's avatar
Per Abrahamsen committed
987 988 989 990 991 992 993 994 995 996 997 998 999
(defun widget-glyph-click (event)
  "Handle click on a glyph."
  (let* ((glyph (event-glyph event))
	 (widget (glyph-property glyph 'widget))
	 (extent (event-glyph-extent event))
	 (down-glyph (or (and widget (widget-get widget :glyph-down)) glyph))
	 (up-glyph (or (and widget (widget-get widget :glyph-up)) glyph))
	 (last event))
    ;; Wait for the release.
    (while (not (button-release-event-p last))
      (if (eq extent (event-glyph-extent last))
	  (set-extent-property extent 'end-glyph down-glyph)
	(set-extent-property extent 'end-glyph up-glyph))
1000
      (setq last (read-event event)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
    ;; Release glyph.
    (when down-glyph
      (set-extent-property extent 'end-glyph up-glyph))
    ;; Apply widget action.
    (when (eq extent (event-glyph-extent last))
      (let ((widget (glyph-property (event-glyph event) 'widget)))
	(cond ((null widget)
	       (message "You clicked on a glyph."))
	      ((not (widget-apply widget :active))
	       (message "This glyph is inactive."))
	      (t
	       (widget-apply-action widget event)))))))

Per Abrahamsen's avatar
Per Abrahamsen committed
1014
(defun widget-button-press (pos &optional event)
Per Abrahamsen's avatar
Per Abrahamsen committed
1015
  "Invoke button at POS."
Per Abrahamsen's avatar
Per Abrahamsen committed
1016
  (interactive "@d")
Per Abrahamsen's avatar
Per Abrahamsen committed
1017
  (let ((button (get-char-property pos 'button)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1018 1019 1020 1021 1022 1023
    (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
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039
(defun widget-tabable-at (&optional pos)
  "Return the tabable widget at POS, or nil.
POS defaults to the value of (point)."
  (unless pos
    (setq pos (point)))
  (let ((widget (or (get-char-property (point) 'button)
		    (get-char-property (point) 'field))))
    (if widget
	(let ((order (widget-get widget :tab-order)))
	  (if order
	      (if (>= order 0)
		  widget
		nil)
	    widget))
      nil)))

1040
(defvar widget-use-overlay-change t
Per Abrahamsen's avatar
Per Abrahamsen committed
1041
  "If non-nil, use overlay change functions to tab around in the buffer.
1042
This is much faster, but doesn't work reliably on Emacs 19.34.")
Per Abrahamsen's avatar
Per Abrahamsen committed
1043

Per Abrahamsen's avatar
Per Abrahamsen committed
1044 1045 1046
(defun widget-move (arg)
  "Move point to the ARG next field or button.
ARG may be negative to move backward."
Per Abrahamsen's avatar
Per Abrahamsen committed
1047
  (or (bobp) (> arg 0) (backward-char))
Per Abrahamsen's avatar
Per Abrahamsen committed
1048
  (let ((pos (point))
Per Abrahamsen's avatar
Per Abrahamsen committed
1049
	(number arg)
Per Abrahamsen's avatar
Per Abrahamsen committed
1050
	(old (widget-tabable-at))
Per Abrahamsen's avatar
Per Abrahamsen committed
1051 1052 1053
	new)
    ;; Forward.
    (while (> arg 0)
Per Abrahamsen's avatar
Per Abrahamsen committed
1054 1055 1056 1057 1058 1059
      (cond ((eobp)
	     (goto-char (point-min)))
	    (widget-use-overlay-change
	     (goto-char (next-overlay-change (point))))
	    (t
	     (forward-char 1)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1060 1061 1062
      (and (eq pos (point))
	   (eq arg number)
	   (error "No buttons or fields found"))
Per Abrahamsen's avatar
Per Abrahamsen committed
1063
      (let ((new (widget-tabable-at)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1064 1065
	(when new
	  (unless (eq new old)
Per Abrahamsen's avatar
Per Abrahamsen committed
1066
	    (setq arg (1- arg))
Per Abrahamsen's avatar
Per Abrahamsen committed
1067 1068 1069
	    (setq old new)))))
    ;; Backward.
    (while (< arg 0)
Per Abrahamsen's avatar
Per Abrahamsen committed
1070 1071 1072 1073 1074 1075
      (cond ((bobp)
	     (goto-char (point-max)))
	    (widget-use-overlay-change
	     (goto-char (previous-overlay-change (point))))
	    (t
	     (backward-char 1)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1076 1077 1078
      (and (eq pos (point))
	   (eq arg number)
	   (error "No buttons or fields found"))
Per Abrahamsen's avatar
Per Abrahamsen committed
1079
      (let ((new (widget-tabable-at)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1080 1081
	(when new
	  (unless (eq new old)
Per Abrahamsen's avatar
Per Abrahamsen committed
1082 1083 1084 1085
	    (setq arg (1+ arg))))))
    (let ((new (widget-tabable-at)))
      (while (eq (widget-tabable-at) new)
	(backward-char)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1086 1087 1088
    (forward-char))
  (widget-echo-help (point))
  (run-hooks 'widget-move-hook))
Per Abrahamsen's avatar
Per Abrahamsen committed
1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106

(defun widget-forward (arg)
  "Move point to the next field or button.
With optional ARG, move across that many fields."
  (interactive "p")
  (run-hooks 'widget-forward-hook)
  (widget-move arg))

(defun widget-backward (arg)
  "Move point to the previous field or button.
With optional ARG, move across that many fields."
  (interactive "p")
  (run-hooks 'widget-backward-hook)
  (widget-move (- arg)))

(defun widget-beginning-of-line ()
  "Go to beginning of field or beginning of line, whichever is first."
  (interactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
1107
  (let* ((field (widget-field-find (point)))
1108 1109 1110 1111 1112 1113 1114
	 (start (and field (widget-field-start field)))
         (bol (save-excursion
                (beginning-of-line)
                (point))))
    (goto-char (if start
                   (max start bol)
                 bol))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1115 1116 1117 1118

(defun widget-end-of-line ()
  "Go to end of field or end of line, whichever is first."
  (interactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
1119
  (let* ((field (widget-field-find (point)))
1120 1121 1122 1123 1124 1125 1126
	 (end (and field (widget-field-end field)))
         (eol (save-excursion
                (end-of-line)
                (point))))
    (goto-char (if end
                   (min end eol)
                 eol))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1127 1128 1129 1130

(defun widget-kill-line ()
  "Kill to end of field or end of line, whichever is first."
  (interactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
1131 1132 1133 1134 1135
  (let* ((field (widget-field-find (point)))
	 (newline (save-excursion (forward-line 1) (point)))
	 (end (and field (widget-field-end field))))
    (if (and field (> newline end))
	(kill-region (point) end)
Per Abrahamsen's avatar
Per Abrahamsen committed
1136 1137
      (call-interactively 'kill-line))))

Per Abrahamsen's avatar
Per Abrahamsen committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
(defcustom widget-complete-field (lookup-key global-map "\M-\t")
  "Default function to call for completion inside fields."
  :options '(ispell-complete-word complete-tag lisp-complete-symbol)
  :type 'function
  :group 'widgets)

(defun widget-complete ()
  "Complete content of editable field from point.
When not inside a field, move to the previous button or field."
  (interactive)
  (let ((field (widget-field-find (point))))
    (if field
	(widget-apply field :complete)
      (error "Not in an editable field"))))

Per Abrahamsen's avatar
Per Abrahamsen committed
1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166
;;; Setting up the buffer.

(defvar widget-field-new nil)
;; List of all newly created editable fields in the buffer.
(make-variable-buffer-local 'widget-field-new)

(defvar widget-field-list nil)
;; List of all editable fields in the buffer.
(make-variable-buffer-local 'widget-field-list)

(defun widget-setup ()
  "Setup current buffer so editing string widgets works."
  (let ((inhibit-read-only t)
	(after-change-functions nil)
1167
	before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
1168 1169 1170 1171 1172
	field)
    (while widget-field-new
      (setq field (car widget-field-new)
	    widget-field-new (cdr widget-field-new)
	    widget-field-list (cons field widget-field-list))
Per Abrahamsen's avatar
Per Abrahamsen committed
1173 1174
      (let ((from (car (widget-get field :field-overlay)))
	    (to (cdr (widget-get field :field-overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1175 1176
	(widget-specify-field field 
			      (marker-position from) (marker-position to))
Per Abrahamsen's avatar
Per Abrahamsen committed
1177 1178
	(set-marker from nil)
	(set-marker to nil))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1179
  (widget-clear-undo)
Per Abrahamsen's avatar
Per Abrahamsen committed
1180
  (widget-add-change))
Per Abrahamsen's avatar
Per Abrahamsen committed
1181 1182 1183 1184 1185 1186 1187 1188 1189

(defvar widget-field-last nil)
;; Last field containing point.
(make-variable-buffer-local 'widget-field-last)

(defvar widget-field-was nil)
;; The widget data before the change.
(make-variable-buffer-local 'widget-field-was)

Per Abrahamsen's avatar
Per Abrahamsen committed
1190 1191
(defun widget-field-buffer (widget)
  "Return the start of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1192 1193
  (let ((overlay (widget-get widget :field-overlay)))
    (and overlay (overlay-buffer overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1194 1195 1196

(defun widget-field-start (widget)
  "Return the start of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1197 1198
  (let ((overlay (widget-get widget :field-overlay)))
    (and overlay (overlay-start overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1199 1200 1201

(defun widget-field-end (widget)
  "Return the end of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1202 1203
  (let ((overlay (widget-get widget :field-overlay)))
    ;; Don't subtract one if local-map works at the end of the overlay.
Per Abrahamsen's avatar
Per Abrahamsen committed
1204 1205
    (and overlay (if (or widget-field-add-space
			 (null (widget-get widget :size)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1206 1207
		     (1- (overlay-end overlay))
		   (overlay-end overlay)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1208

Per Abrahamsen's avatar
Per Abrahamsen committed
1209
(defun widget-field-find (pos)
Per Abrahamsen's avatar
Per Abrahamsen committed
1210 1211
  "Return the field at POS.
Unlike (get-char-property POS 'field) this, works with empty fields too."
Per Abrahamsen's avatar
Per Abrahamsen committed
1212 1213 1214 1215 1216
  (let ((fields widget-field-list)
	field found)
    (while fields
      (setq field (car fields)
	    fields (cdr fields))
Per Abrahamsen's avatar
Per Abrahamsen committed
1217 1218 1219 1220 1221 1222
      (let ((start (widget-field-start field))
	    (end (widget-field-end field)))
	(when (and (<= start pos) (<= pos end))
	  (when found
	    (debug "Overlapping fields"))
	  (setq found field))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1223 1224
    found))

Per Abrahamsen's avatar
Per Abrahamsen committed
1225
(defun widget-before-change (from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
1226 1227
  ;; This is how, for example, a variable changes its state to `modified'.
  ;; when it is being edited.
1228 1229 1230 1231 1232
  (unless inhibit-read-only
    (let ((from-field (widget-field-find from))
	  (to-field (widget-field-find to)))
      (cond ((not (eq from-field to-field))
	     (add-hook 'post-command-hook 'widget-add-change nil t)
1233 1234
	     (signal 'text-read-only
		     '("Change should be restricted to a single field")))
1235 1236
	    ((null from-field)
	     (add-hook 'post-command-hook 'widget-add-change nil t)
1237 1238
	     (signal 'text-read-only
		     '("Attempt to change text outside editable field")))
1239 1240 1241 1242
	    (widget-field-use-before-change
	     (condition-case nil
		 (widget-apply from-field :notify from-field)
	       (error (debug "Before Change"))))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1243 1244 1245 1246 1247 1248 1249 1250

(defun widget-add-change ()
  (make-local-hook 'post-command-hook)
  (remove-hook 'post-command-hook 'widget-add-change t)
  (make-local-hook 'before-change-functions)
  (add-hook 'before-change-functions 'widget-before-change nil t)
  (make-local-hook 'after-change-functions)
  (add-hook 'after-change-functions 'widget-after-change nil t))
1251

Per Abrahamsen's avatar
Per Abrahamsen committed
1252 1253 1254 1255
(defun widget-after-change (from to old)
  ;; Adjust field size and text properties.
  (condition-case nil
      (let ((field (widget-field-find from))
Per Abrahamsen's avatar
Per Abrahamsen committed
1256 1257 1258 1259
	    (other (widget-field-find to)))
	(when field
	  (unless (eq field other)
	    (debug "Change in different fields"))
1260
	  (let ((size (widget-get field :size)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280
	    (when size 
	      (let ((begin (widget-field-start field))
		    (end (widget-field-end field)))
		(cond ((< (- end begin) size)
		       ;; Field too small.
		       (save-excursion
			 (goto-char end)
			 (insert-char ?\  (- (+ begin size) end))))
		      ((> (- end begin) size)
		       ;; Field too large and
		       (if (or (< (point) (+ begin size))
			       (> (point) end))
			   ;; Point is outside extra space.
			   (setq begin (+ begin size))
			 ;; Point is within the extra space.
			 (setq begin (point)))
		       (save-excursion
			 (goto-char end)
			 (while (and (eq (preceding-char) ?\ )
				     (> (point) begin))
Per Abrahamsen's avatar
Per Abrahamsen committed
1281
			   (delete-backward-char 1)))))))
1282
	    (widget-specify-secret field))
Per Abrahamsen's avatar
Per Abrahamsen committed
1283
	  (widget-apply field :notify field)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1284
    (error (debug "After Change"))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1285 1286 1287 1288 1289

;;; Widget Functions
;;
;; These functions are used in the definition of multiple widgets.