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

10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
;; 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
27 28 29 30 31 32 33
;;; Commentary:
;;
;; See `widget.el'.

;;; Code:

(require 'widget)
Per Abrahamsen's avatar
Per Abrahamsen committed
34
(eval-when-compile (require 'cl))
Per Abrahamsen's avatar
Per Abrahamsen committed
35 36 37 38 39 40 41 42 43 44 45 46

;;; Compatibility.

(eval-and-compile
  (autoload 'pp-to-string "pp")
  (autoload 'Info-goto-node "info")

  (when (string-match "XEmacs" emacs-version)
    (condition-case nil
	(require 'overlay)
      (error (load-library "x-overlay"))))
  
Per Abrahamsen's avatar
Per Abrahamsen committed
47 48 49 50 51 52 53 54 55 56
  (if (string-match "XEmacs" emacs-version)
      (defun widget-event-point (event)
	"Character position of the end of event if that exists, or nil."
	(if (mouse-event-p event)
	    (event-point event)
	  nil))
    (defun widget-event-point (event)
      "Character position of the end of event if that exists, or nil."
      (posn-point (event-end event))))

Per Abrahamsen's avatar
Per Abrahamsen committed
57 58 59 60 61
(defalias 'widget-read-event (if (string-match "XEmacs" emacs-version)
				   'next-event
				 'read-event))

  ;; The following should go away when bundled with Emacs.
Per Abrahamsen's avatar
Per Abrahamsen committed
62 63 64 65 66 67 68 69
  (condition-case ()
      (require 'custom)
    (error nil))

  (unless (and (featurep 'custom) (fboundp 'custom-declare-variable))
    ;; We have the old custom-library, hack around it!
    (defmacro defgroup (&rest args) nil)
    (defmacro defcustom (var value doc &rest args) 
Per Abrahamsen's avatar
Per Abrahamsen committed
70
      (` (defvar (, var) (, value) (, doc))))
Per Abrahamsen's avatar
Per Abrahamsen committed
71 72 73 74 75 76 77
    (defmacro defface (&rest args) nil)
    (define-widget-keywords :prefix :tag :load :link :options :type :group)
    (when (fboundp 'copy-face)
      (copy-face 'default 'widget-documentation-face)
      (copy-face 'bold 'widget-button-face)
      (copy-face 'italic 'widget-field-face)))

Per Abrahamsen's avatar
Per Abrahamsen committed
78 79 80 81 82 83 84 85
  (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))
	       (memq  'drag (event-modifiers event))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
86 87 88 89 90 91 92 93 94 95

  (unless (fboundp 'error-message-string)
    ;; Emacs function missing in XEmacs.
    (defun error-message-string (obj)
      "Convert an error value to an error message."
      (let ((buf (get-buffer-create " *error-message*")))
	(erase-buffer buf)
	(display-error obj buf)
	(buffer-string buf)))))

Per Abrahamsen's avatar
Per Abrahamsen committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
(when (let ((a "foo"))
	(put-text-property 1 2 'foo 1 a)
	(put-text-property 1 2 'bar 2 a)
	(set-text-properties 1 2 nil a)
	(text-properties-at 1 a))
  ;; XEmacs 20.2 and earlier had a buggy set-text-properties.
  (defun set-text-properties (start end props &optional buffer-or-string)
    "Completely replace properties of text from START to END.
The third argument PROPS is the new property list.
The optional fourth argument, BUFFER-OR-STRING,
is the string or buffer containing the text."
    (map-extents #'(lambda (extent ignored)
		     (remove-text-properties
		      start end
		      (list (extent-property extent 'text-prop)
			    nil)
		      buffer-or-string)
		     nil)
		 buffer-or-string start end nil nil 'text-prop)
    (add-text-properties start end props buffer-or-string)))

Per Abrahamsen's avatar
Per Abrahamsen committed
117 118 119 120 121 122 123 124 125 126 127
;;; 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/")
  :prefix "widget-"
  :group 'extensions
  :group 'hypermedia)

Per Abrahamsen's avatar
Per Abrahamsen committed
128 129 130 131
(defgroup widget-documentation nil
  "Options controling the display of documentation strings."
  :group 'widgets)

Per Abrahamsen's avatar
Per Abrahamsen committed
132 133 134 135 136
(defgroup widget-faces nil
  "Faces used by the widget library."
  :group 'widgets
  :group 'faces)

Per Abrahamsen's avatar
Per Abrahamsen committed
137 138 139 140 141 142 143 144 145 146 147
(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)

Per Abrahamsen's avatar
Per Abrahamsen committed
148 149
(defface widget-button-face '((t (:bold t)))
  "Face used for widget buttons."
Per Abrahamsen's avatar
Per Abrahamsen committed
150
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
151 152 153 154

(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
155
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
156 157 158

(defface widget-field-face '((((class grayscale color)
			       (background light))
Per Abrahamsen's avatar
Per Abrahamsen committed
159
			      (:background "gray85"))
Per Abrahamsen's avatar
Per Abrahamsen committed
160 161
			     (((class grayscale color)
			       (background dark))
Per Abrahamsen's avatar
Per Abrahamsen committed
162
			      (:background "dim gray"))
Per Abrahamsen's avatar
Per Abrahamsen committed
163 164 165
			     (t 
			      (:italic t)))
  "Face used for editable fields."
Per Abrahamsen's avatar
Per Abrahamsen committed
166
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198

;;; Utility functions.
;;
;; These are not really widget specific.

(defsubst widget-plist-member (plist prop)
  ;; Return non-nil if PLIST has the property PROP.
  ;; PLIST is a property list, which is a list of the form
  ;; (PROP1 VALUE1 PROP2 VALUE2 ...).  PROP is a symbol.
  ;; Unlike `plist-get', this allows you to distinguish between a missing
  ;; property and a property with the value nil.
  ;; The value is actually the tail of PLIST whose car is PROP.
  (while (and plist (not (eq (car plist) prop)))
    (setq plist (cdr (cdr plist))))
  plist)

(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
199 200 201 202 203 204
(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)

Per Abrahamsen's avatar
Per Abrahamsen committed
205 206 207 208
(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
209 210 211
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
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
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))))
	((and (< (length items) widget-menu-max-size)
	      event (fboundp 'popup-menu) window-system)
	 ;; We are in XEmacs, pressed by the mouse
	 (let ((val (get-popup-menu-response
		     (cons title
			   (mapcar
			    (function
			     (lambda (x)
Per Abrahamsen's avatar
Per Abrahamsen committed
232 233 234
			       (if (stringp x)
				   (vector x nil nil) 
				 (vector (car x) (list (car x)) t))))
Per Abrahamsen's avatar
Per Abrahamsen committed
235 236 237 238 239 240 241
			    items)))))
	   (setq val (and val
			  (listp (event-object val))
			  (stringp (car-safe (event-object val)))
			  (car (event-object val))))
	   (cdr (assoc val items))))
	(t
242
	 (setq items (widget-remove-if 'stringp items))
Per Abrahamsen's avatar
Per Abrahamsen committed
243 244 245 246 247 248 249 250
	 (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)))
	     nil)))))

251 252 253 254 255 256 257 258
(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
259 260 261 262 263 264 265 266 267 268 269 270
;;; Widget text specifications.
;; 
;; These functions are for specifying text properties. 

(defun widget-specify-none (from to)
  ;; Clear all text properties between FROM and TO.
  (set-text-properties from to nil))

(defun widget-specify-text (from to)
  ;; Default properties.
  (add-text-properties from to (list 'read-only t
				     'front-sticky t
Per Abrahamsen's avatar
Per Abrahamsen committed
271
				     'rear-nonsticky nil
Per Abrahamsen's avatar
Per Abrahamsen committed
272 273
				     'start-open nil
				     'end-open nil)))
Per Abrahamsen's avatar
Per Abrahamsen committed
274

Per Abrahamsen's avatar
Per Abrahamsen committed
275 276 277 278 279 280 281 282 283 284 285 286 287
(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
288
(defun widget-specify-field (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
289 290
  "Specify editable button for WIDGET between FROM and TO."
  (put-text-property from to 'read-only nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
291 292 293 294 295
  ;; 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
296 297
    (when widget-field-add-space
      (insert-and-inherit " "))
Per Abrahamsen's avatar
Per Abrahamsen committed
298
    (setq to (point)))
Per Abrahamsen's avatar
Per Abrahamsen committed
299 300 301 302 303
  (if widget-field-add-space
      (add-text-properties (1- to) to
			   '(front-sticky nil start-open t read-only to))
    (add-text-properties to (1+ to) 
			 '(front-sticky nil start-open t read-only to)))
Per Abrahamsen's avatar
Per Abrahamsen committed
304 305
  (add-text-properties (1- from) from 
		       '(rear-nonsticky t end-open t read-only from))
Per Abrahamsen's avatar
Per Abrahamsen committed
306
  (let ((map (widget-get widget :keymap))
Per Abrahamsen's avatar
Per Abrahamsen committed
307 308 309
	(face (or (widget-get widget :value-face) 'widget-field-face))
	(help-echo (widget-get widget :help-echo))
	(overlay (make-overlay from to nil nil t)))
Per Abrahamsen's avatar
Per Abrahamsen committed
310
    (unless (or (stringp help-echo) (null help-echo))
Per Abrahamsen's avatar
Per Abrahamsen committed
311 312 313 314 315 316 317 318 319
      (setq help-echo 'widget-mouse-help))    
    (widget-put widget :field-overlay overlay)
    (overlay-put overlay 'detachable nil)
    (overlay-put overlay 'field widget)
    (overlay-put overlay 'local-map map)
    (overlay-put overlay 'keymap map)
    (overlay-put overlay 'face face)
    (overlay-put overlay 'balloon-help help-echo)
    (overlay-put overlay 'help-echo help-echo)))
Per Abrahamsen's avatar
Per Abrahamsen committed
320 321

(defun widget-specify-button (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
322
  "Specify button for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
323
  (let ((face (widget-apply widget :button-face-get))
Per Abrahamsen's avatar
Per Abrahamsen committed
324 325 326
	(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
327 328
    (unless (or (null help-echo) (stringp help-echo))
      (setq help-echo 'widget-mouse-help))
Per Abrahamsen's avatar
Per Abrahamsen committed
329 330 331 332 333
    (overlay-put overlay 'button widget)
    (overlay-put overlay 'mouse-face widget-mouse-face)
    (overlay-put overlay 'balloon-help help-echo)
    (overlay-put overlay 'help-echo help-echo)
    (overlay-put overlay 'face face)))
Per Abrahamsen's avatar
Per Abrahamsen committed
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360

(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.
  (let ((face (widget-apply widget :sample-face-get)))
    (when face
      (add-text-properties from to (list 'start-open t
					 'end-open t
					 'face face)))))
(defun widget-specify-doc (widget from to)
  ;; Specify documentation for WIDGET between FROM and TO.
  (add-text-properties from to (list 'widget-doc widget
				     'face 'widget-documentation-face)))

(defmacro widget-specify-insert (&rest form)
  ;; Execute FORM without inheriting any text properties.
Per Abrahamsen's avatar
Per Abrahamsen committed
361 362
  (`
   (save-restriction
Per Abrahamsen's avatar
Per Abrahamsen committed
363 364
     (let ((inhibit-read-only t)
	   result
365
	   before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
366 367 368 369 370
	   after-change-functions)
       (insert "<>")
       (narrow-to-region (- (point) 2) (point))
       (widget-specify-none (point-min) (point-max))
       (goto-char (1+ (point-min)))
Per Abrahamsen's avatar
Per Abrahamsen committed
371
       (setq result (progn (,@ form)))
Per Abrahamsen's avatar
Per Abrahamsen committed
372 373 374
       (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
375
       result))))
Per Abrahamsen's avatar
Per Abrahamsen committed
376 377 378 379 380 381

(defface widget-inactive-face '((((class grayscale color)
				  (background dark))
				 (:foreground "light gray"))
				(((class grayscale color)
				  (background light))
Per Abrahamsen's avatar
Per Abrahamsen committed
382
				 (:foreground "dim gray"))
Per Abrahamsen's avatar
Per Abrahamsen committed
383 384 385
				(t 
				 (:italic t)))
  "Face used for inactive widgets."
Per Abrahamsen's avatar
Per Abrahamsen committed
386
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
387 388 389 390 391 392

(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
393 394
      ;; 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
395 396
      (overlay-put overlay 'evaporate t)
      (overlay-put overlay 'priority 100)
Per Abrahamsen's avatar
Per Abrahamsen committed
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
      (overlay-put overlay (if (string-match "XEmacs" emacs-version)
			       'read-only
			     'modification-hooks) '(widget-overlay-inactive))
      (widget-put widget :inactive overlay))))

(defun widget-overlay-inactive (&rest junk)
  "Ignoring the arguments, signal an error."
  (unless inhibit-read-only
    (error "Attempt to modify inactive widget")))


(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))

(defun widget-put (widget property value)
  "In WIDGET set PROPERTY to VALUE.
The value can later be retrived with `widget-get'."
  (setcdr widget (plist-put (cdr widget) property value)))

(defun widget-get (widget property)
  "In WIDGET, get the value of PROPERTY.
The value could either be specified when the widget was created, or
later with `widget-put'."
  (let ((missing t)
	value tmp)
    (while missing
      (cond ((setq tmp (widget-plist-member (cdr widget) property))
	     (setq value (car (cdr tmp))
		   missing nil))
	    ((setq tmp (car widget))
	     (setq widget (get tmp 'widget-type)))
	    (t 
	     (setq missing nil))))
    value))

Per Abrahamsen's avatar
Per Abrahamsen committed
442 443 444 445 446 447 448 449 450
(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
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
(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)))

;;;###autoload
(defun widget-apply (widget property &rest args)
  "Apply the value of WIDGET's PROPERTY to the widget itself.
ARGS are passed as extra arguments to the function."
  (apply (widget-get widget property) widget args))

(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)))

(defun widget-match-inline (widget vals)
  ;; In WIDGET, match the start of VALS.
  (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
487 488 489
  (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
490

Per Abrahamsen's avatar
Per Abrahamsen committed
491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521
;;; 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
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
(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
542 543 544 545 546
;;; 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
547
automatically."
Per Abrahamsen's avatar
Per Abrahamsen committed
548 549 550 551 552 553 554 555
  :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
556 557 558 559 560 561 562 563 564 565
(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
566 567 568 569 570 571 572
(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
573 574
		   widget-glyph-enable
		   (fboundp 'make-glyph)
Per Abrahamsen's avatar
Per Abrahamsen committed
575
		   (fboundp 'locate-file)
Per Abrahamsen's avatar
Per Abrahamsen committed
576 577
		   image))
	 ;; We don't want or can't use glyphs.
Per Abrahamsen's avatar
Per Abrahamsen committed
578
	 nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
579 580
	((and (fboundp 'glyphp)
	      (glyphp image))
Per Abrahamsen's avatar
Per Abrahamsen committed
581 582
	 ;; Already a glyph.  Use it.
	 image)
Per Abrahamsen's avatar
Per Abrahamsen committed
583 584 585 586 587 588 589 590 591
	((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
592 593 594 595 596 597
	     (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
598
	       (setq formats (cdr formats))))
Per Abrahamsen's avatar
Per Abrahamsen committed
599 600 601 602 603
	   (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
604 605
	((valid-instantiator-p image 'image)
	 ;; A valid image instantiator (e.g. [gif :file "somefile"] etc.)
Per Abrahamsen's avatar
Per Abrahamsen committed
606 607
	 (make-glyph (list image
			   (vector 'string ':data tag))))
Per Abrahamsen's avatar
Per Abrahamsen committed
608 609 610
	((consp image)
	 ;; This could be virtually anything.  Let `make-glyph' sort it out.
	 (make-glyph image))
Per Abrahamsen's avatar
Per Abrahamsen committed
611
	(t
Per Abrahamsen's avatar
Per Abrahamsen committed
612
	 ;; Oh well.
Per Abrahamsen's avatar
Per Abrahamsen committed
613 614 615 616 617 618 619 620 621 622 623 624 625 626
	 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
627 628 629 630 631
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
632 633 634 635 636 637 638
  (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
639 640

(defun widget-glyph-insert-glyph (widget glyph &optional down inactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
641 642 643
  "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
644
  (set-glyph-property glyph 'widget widget)
Per Abrahamsen's avatar
Per Abrahamsen committed
645 646 647 648
  (when down
    (set-glyph-property down 'widget widget))
  (when inactive
    (set-glyph-property inactive 'widget widget))
Per Abrahamsen's avatar
Per Abrahamsen committed
649
  (insert "*")
Per Abrahamsen's avatar
Per Abrahamsen committed
650 651 652
  (let ((ext (make-extent (point) (1- (point))))
	(help-echo (widget-get widget :help-echo)))
    (set-extent-property ext 'invisible t)
Per Abrahamsen's avatar
Per Abrahamsen committed
653 654
    (set-extent-property ext 'start-open t)
    (set-extent-property ext 'end-open t)
Per Abrahamsen's avatar
Per Abrahamsen committed
655 656 657 658
    (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
659 660
  (widget-put widget :glyph-up glyph)
  (when down (widget-put widget :glyph-down down))
Per Abrahamsen's avatar
Per Abrahamsen committed
661
  (when inactive (widget-put widget :glyph-inactive inactive)))
Per Abrahamsen's avatar
Per Abrahamsen committed
662

Per Abrahamsen's avatar
Per Abrahamsen committed
663 664 665 666 667 668 669 670 671
;;; 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
672
  :group 'widget-button)
Per Abrahamsen's avatar
Per Abrahamsen committed
673 674 675 676

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

Per Abrahamsen's avatar
Per Abrahamsen committed
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702
;;; 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."
703
  (let ((widget (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
704 705 706 707 708 709 710 711 712 713
    (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."
714
  (let ((widget (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
    (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)
735
		   (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776
	 (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)
777
	before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
778 779 780 781 782
	after-change-functions
	(from (point)))
    (apply 'insert args)
    (widget-specify-text from (point))))

Per Abrahamsen's avatar
Per Abrahamsen committed
783 784 785
(defun widget-convert-text (type from to
				 &optional button-from button-to
				 &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
786
  "Return a widget of type TYPE with endpoint FROM TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
787
Optional ARGS are extra keyword arguments for TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
788 789
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
790 791 792
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
793 794 795 796 797 798 799 800 801 802 803
	(from (copy-marker from))
	(to (copy-marker to)))
    (widget-specify-text from 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
804
(defun widget-convert-button (type from to &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
805
  "Return a widget of type TYPE with endpoint FROM TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
806
Optional ARGS are extra keyword arguments for TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
807 808 809
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
810 811 812 813 814 815 816 817 818 819 820
  (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))
	(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
821 822 823 824
    (when button
      (delete-overlay button))
    (when field
      (delete-overlay field))
Per Abrahamsen's avatar
Per Abrahamsen committed
825
    (mapcar 'widget-leave-text children)))
Per Abrahamsen's avatar
Per Abrahamsen committed
826

Per Abrahamsen's avatar
Per Abrahamsen committed
827 828 829 830 831 832 833 834 835 836 837
;;; 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
838
  (if (string-match "XEmacs" emacs-version)
Per Abrahamsen's avatar
Per Abrahamsen committed
839
      (progn 
Per Abrahamsen's avatar
Per Abrahamsen committed
840 841 842
	;;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
843 844 845 846 847 848 849 850 851 852 853 854 855 856
    (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))
  (unless (string-match "XEmacs" (emacs-version))
    (define-key widget-field-keymap [menu-bar] 'nil))
Per Abrahamsen's avatar
Per Abrahamsen committed
857 858
  (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
859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
  (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))
  (unless (string-match "XEmacs" (emacs-version))
    (define-key widget-text-keymap [menu-bar] 'nil))
  (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
876
  "Invoke the ediable field at point."
Per Abrahamsen's avatar
Per Abrahamsen committed
877
  (interactive "@d")
Per Abrahamsen's avatar
Per Abrahamsen committed
878
  (let ((field (get-char-property pos 'field)))
Per Abrahamsen's avatar
Per Abrahamsen committed
879 880 881 882 883
    (if field
	(widget-apply-action field event)
      (call-interactively
       (lookup-key widget-global-map (this-command-keys))))))

Per Abrahamsen's avatar
Per Abrahamsen committed
884 885 886 887 888 889
(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
890
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
891

Per Abrahamsen's avatar
Per Abrahamsen committed
892
(defun widget-button-click (event)
Per Abrahamsen's avatar
Per Abrahamsen committed
893
  "Invoke button below mouse pointer."
Per Abrahamsen's avatar
Per Abrahamsen committed
894 895 896
  (interactive "@e")
  (cond ((and (fboundp 'event-glyph)
	      (event-glyph event))
Per Abrahamsen's avatar
Per Abrahamsen committed
897 898 899
	 (widget-glyph-click event))
	((widget-event-point event)
	 (let* ((pos (widget-event-point event))
Per Abrahamsen's avatar
Per Abrahamsen committed
900
		(button (get-char-property pos 'button)))
Per Abrahamsen's avatar
Per Abrahamsen committed
901
	   (if button
Per Abrahamsen's avatar
Per Abrahamsen committed
902 903
	       (let* ((overlay (widget-get button :button-overlay))
		      (face (overlay-get overlay 'face))
Per Abrahamsen's avatar
Per Abrahamsen committed
904
		      (mouse-face (overlay-get overlay 'mouse-face)))
Per Abrahamsen's avatar
Per Abrahamsen committed
905 906
		 (unwind-protect
		     (let ((track-mouse t))
Per Abrahamsen's avatar
Per Abrahamsen committed
907 908
		       (overlay-put overlay
				    'face 'widget-button-pressed-face)
Per Abrahamsen's avatar
Per Abrahamsen committed
909 910 911 912
		       (overlay-put overlay 
				    'mouse-face 'widget-button-pressed-face)
		       (unless (widget-apply button :mouse-down-action event)
			 (while (not (button-release-event-p event))
Per Abrahamsen's avatar
Per Abrahamsen committed
913
			   (setq event (widget-read-event)
Per Abrahamsen's avatar
Per Abrahamsen committed
914 915
				 pos (widget-event-point event))
			   (if (and pos
Per Abrahamsen's avatar
Per Abrahamsen committed
916
				    (eq (get-char-property pos 'button)
Per Abrahamsen's avatar
Per Abrahamsen committed
917 918 919 920 921 922 923 924
					button))
			       (progn 
				 (overlay-put overlay 
					      'face
					      'widget-button-pressed-face)
				 (overlay-put overlay 
					      'mouse-face 
					      'widget-button-pressed-face))
Per Abrahamsen's avatar
Per Abrahamsen committed
925 926
			     (overlay-put overlay 'face face)
			     (overlay-put overlay 'mouse-face mouse-face))))
Per Abrahamsen's avatar
Per Abrahamsen committed
927
		       (when (and pos 
Per Abrahamsen's avatar
Per Abrahamsen committed
928
				  (eq (get-char-property pos 'button) button))
Per Abrahamsen's avatar
Per Abrahamsen committed
929
			 (widget-apply-action button event)))
Per Abrahamsen's avatar
Per Abrahamsen committed
930 931
		   (overlay-put overlay 'face face)
		   (overlay-put overlay 'mouse-face mouse-face)))
Per Abrahamsen's avatar
Per Abrahamsen committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
	     (let (command up)
	       ;; Find the global command to run, and check whether it
	       ;; is bound to an up event.
	       (cond ((setq command	;down event
			    (lookup-key widget-global-map [ button2 ])))
		     ((setq command	;down event
			    (lookup-key widget-global-map [ down-mouse-2 ])))
		     ((setq command	;up event
			    (lookup-key widget-global-map [ button2up ]))
		      (setq up t))
		     ((setq command	;up event
			    (lookup-key widget-global-map [ mouse-2]))
		      (setq up t)))
	       (when command
		 ;; Don't execute up events twice.
		 (when up
		   (while (not (button-release-event-p event))
		     (setq event (widget-read-event))))
		 (call-interactively command))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
951 952 953 954
	(t
	 (message "You clicked somewhere weird."))))

(defun widget-button1-click (event)
Per Abrahamsen's avatar
Per Abrahamsen committed
955
  "Invoke glyph below mouse pointer."
Per Abrahamsen's avatar
Per Abrahamsen committed
956 957 958
  (interactive "@e")
  (if (and (fboundp 'event-glyph)
	   (event-glyph event))
Per Abrahamsen's avatar
Per Abrahamsen committed
959
      (widget-glyph-click event)
Per Abrahamsen's avatar
Per Abrahamsen committed
960 961
    (call-interactively (lookup-key widget-global-map (this-command-keys)))))

Per Abrahamsen's avatar
Per Abrahamsen committed
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988
(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))
      (setq last (next-event event)))
    ;; 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
989
(defun widget-button-press (pos &optional event)
Per Abrahamsen's avatar
Per Abrahamsen committed
990
  "Invoke button at POS."
Per Abrahamsen's avatar
Per Abrahamsen committed
991
  (interactive "@d")
Per Abrahamsen's avatar
Per Abrahamsen committed
992
  (let ((button (get-char-property pos 'button)))
Per Abrahamsen's avatar
Per Abrahamsen committed
993 994 995 996 997 998
    (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
999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014
(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)))

Per Abrahamsen's avatar
Per Abrahamsen committed
1015 1016 1017
(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
1018
  (or (bobp) (> arg 0) (backward-char))
Per Abrahamsen's avatar
Per Abrahamsen committed
1019
  (let ((pos (point))
Per Abrahamsen's avatar
Per Abrahamsen committed
1020
	(number arg)
Per Abrahamsen's avatar
Per Abrahamsen committed
1021
	(old (widget-tabable-at))
Per Abrahamsen's avatar
Per Abrahamsen committed
1022 1023 1024 1025 1026
	new)
    ;; Forward.
    (while (> arg 0)
      (if (eobp)
	  (goto-char (point-min))
Per Abrahamsen's avatar
Per Abrahamsen committed
1027
	(forward-char 1))
Per Abrahamsen's avatar
Per Abrahamsen committed
1028 1029 1030
      (and (eq pos (point))
	   (eq arg number)
	   (error "No buttons or fields found"))
Per Abrahamsen's avatar
Per Abrahamsen committed
1031
      (let ((new (widget-tabable-at)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1032 1033
	(when new
	  (unless (eq new old)
Per Abrahamsen's avatar
Per Abrahamsen committed
1034
	    (setq arg (1- arg))
Per Abrahamsen's avatar
Per Abrahamsen committed
1035 1036 1037 1038 1039 1040 1041 1042 1043
	    (setq old new)))))
    ;; Backward.
    (while (< arg 0)
      (if (bobp)
	  (goto-char (point-max))
	(backward-char 1))
      (and (eq pos (point))
	   (eq arg number)
	   (error "No buttons or fields found"))
Per Abrahamsen's avatar
Per Abrahamsen committed
1044
      (let ((new (widget-tabable-at)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1045 1046
	(when new
	  (unless (eq new old)
Per Abrahamsen's avatar
Per Abrahamsen committed
1047 1048 1049 1050
	    (setq arg (1+ arg))))))
    (let ((new (widget-tabable-at)))
      (while (eq (widget-tabable-at) new)
	(backward-char)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1051 1052 1053
    (forward-char))
  (widget-echo-help (point))
  (run-hooks 'widget-move-hook))
Per Abrahamsen's avatar
Per Abrahamsen committed
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071

(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
1072 1073 1074 1075 1076
  (let* ((field (widget-field-find (point)))
	 (start (and field (widget-field-start field))))
    (if (and start (not (eq start (point))))
	(goto-char start)
      (call-interactively 'beginning-of-line))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1077 1078 1079 1080

(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
1081 1082 1083 1084 1085
  (let* ((field (widget-field-find (point)))
	 (end (and field (widget-field-end field))))
    (if (and end (not (eq end (point))))
	(goto-char end)
      (call-interactively 'end-of-line))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1086 1087 1088 1089

(defun widget-kill-line ()
  "Kill to end of field or end of line, whichever is first."
  (interactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
1090 1091 1092 1093 1094
  (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
1095 1096
      (call-interactively 'kill-line))))

Per Abrahamsen's avatar
Per Abrahamsen committed
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111
(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
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
;;; 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)
1126
	before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
1127 1128 1129 1130 1131
	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
1132 1133
      (let ((from (car (widget-get field :field-overlay)))
	    (to (cdr (widget-get field :field-overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1134 1135
	(widget-specify-field field 
			      (marker-position from) (marker-position to))
Per Abrahamsen's avatar
Per Abrahamsen committed
1136 1137
	(set-marker from nil)
	(set-marker to nil))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1138 1139 1140
  (widget-clear-undo)
  ;; We need to maintain text properties and size of the editing fields.
  (make-local-variable 'after-change-functions)
1141 1142 1143 1144 1145
  (make-local-variable 'before-change-functions)
  (setq after-change-functions
	(if widget-field-list '(widget-after-change) nil))
  (setq before-change-functions
	(if widget-field-list '(widget-before-change) nil)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1146 1147 1148 1149 1150 1151 1152 1153 1154

(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
1155 1156
(defun widget-field-buffer (widget)
  "Return the start of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1157 1158
  (let ((overlay (widget-get widget :field-overlay)))
    (and overlay (overlay-buffer overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1159 1160 1161

(defun widget-field-start (widget)
  "Return the start of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1162 1163
  (let ((overlay (widget-get widget :field-overlay)))
    (and overlay (overlay-start overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1164 1165 1166

(defun widget-field-end (widget)
  "Return the end of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1167 1168
  (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
1169 1170 1171
    (and overlay (if widget-field-add-space
		     (1- (overlay-end overlay))
		   (overlay-end overlay)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1172

Per Abrahamsen's avatar
Per Abrahamsen committed
1173
(defun widget-field-find (pos)
Per Abrahamsen's avatar
Per Abrahamsen committed
1174 1175
  "Return the field at POS.
Unlike (get-char-property POS 'field) this, works with empty fields too."
Per Abrahamsen's avatar
Per Abrahamsen committed
1176 1177 1178 1179 1180
  (let ((fields widget-field-list)
	field found)
    (while fields
      (setq field (car fields)
	    fields (cdr fields))
Per Abrahamsen's avatar
Per Abrahamsen committed
1181 1182 1183 1184 1185 1186
      (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
1187 1188
    found))

1189
(defun widget-before-change (from &rest ignore)
Per Abrahamsen's avatar
Per Abrahamsen committed
1190 1191
  ;; This is how, for example, a variable changes its state to `modified'.
  ;; when it is being edited.
1192 1193 1194
  (condition-case nil
      (let ((field (widget-field-find from)))
	(widget-apply field :notify field))
Per Abrahamsen's avatar
Per Abrahamsen committed
1195
    (error (debug "Before Change"))))
1196

Per Abrahamsen's avatar
Per Abrahamsen committed
1197 1198 1199 1200
(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
1201 1202 1203 1204
	    (other (widget-field-find to)))
	(when field
	  (unless (eq field other)
	    (debug "Change in different fields"))
Per Abrahamsen's avatar
Per Abrahamsen committed
1205 1206
	  (let ((size (widget-get field :size))
		(secret (widget-get field :secret)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
	    (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
1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239
			   (delete-backward-char 1)))))))
	    (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))
Per Abrahamsen's avatar
Per Abrahamsen committed
1240 1241
		    (setq begin (1+ begin)))))))
	  (widget-apply field :notify field)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1242
    (error (debug "After Change"))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1243 1244 1245 1246 1247

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

Per Abrahamsen's avatar
Per Abrahamsen committed
1248 1249 1250 1251 1252
(defun widget-parent-action (widget &optional event)
  "Tell :parent of WIDGET to handle the :action.
Optional EVENT is the event that triggered the action."
  (widget-apply (widget-get widget :parent) :action event))

Per Abrahamsen's avatar
Per Abrahamsen committed
1253 1254 1255 1256 1257 1258 1259
(defun widget-children-value-delete (widget)
  "Delete all :children and :buttons in WIDGET."
  (mapcar 'widget-delete (widget-get widget :children))
  (widget-put widget :children nil)
  (mapcar 'widget-delete (widget-get widget :buttons))
  (widget-put widget :buttons nil))

Per Abrahamsen's avatar
Per Abrahamsen committed
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
(defun widget-children-validate (widget)
  "All the :children must be valid."
  (let ((children (widget-get widget :children))
	child found)
    (while (and children (not found))
      (setq child (car children)
	    children (cdr children)
	    found (widget-apply child :validate)))
    found))

Per Abrahamsen's avatar
Per Abrahamsen committed
1270 1271 1272 1273 1274
(defun widget-types-convert-widget (widget)
  "Convert :args as widget types in WIDGET."
  (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
  widget)

Per Abrahamsen's avatar
Per Abrahamsen committed
1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289
(defun widget-value-convert-widget (widget)
  "Initialize :value from :args in WIDGET."
  (let ((args (widget-get widget :args)))
    (when args 
      (widget-put widget :value (car args))
      ;; Don't convert :value here, as this is done in `widget-convert'.
      ;; (widget-put widget :value (widget-apply widget
      ;;  				      :value-to-internal (car args)))
      (widget-put widget :args nil)))
  widget)

(defun widget-value-value-get (widget)
  "Return the :value property of WIDGET."
  (widget-get widget :value))

Per Abrahamsen's avatar
Per Abrahamsen committed
1290 1291 1292 1293 1294 1295
;;; The `default' Widget.

(define-widget 'default nil
  "Basic widget other widgets are derived from."
  :value-to-internal (lambda (widget value) value)
  :value-to-external (lambda (widget value) value)
Per Abrahamsen's avatar
Per Abrahamsen committed
1296 1297
  :button-prefix 'widget-button-prefix
  :button-suffix 'widget-button-suffix
Per Abrahamsen's avatar
Per Abrahamsen committed
1298
  :complete 'widget-default-complete				       
Per Abrahamsen's avatar
Per Abrahamsen committed
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312
  :create 'widget-default-create
  :indent nil
  :offset 0
  :format-handler 'widget-default-format-handler
  :button-face-get 'widget-default-button-face-get 
  :sample-face-get 'widget-default-sample-face-get 
  :delete 'widget-default-delete
  :value-set 'widget-default-value-set
  :value-inline 'widget-default-value-inline
  :menu-tag-get 'widget-default-menu-tag-get
  :validate (lambda (widget) nil)
  :active 'widget-default-active
  :activate 'widget-specify-active
  :deactivate 'widget-default-deactivate
Per Abrahamsen's avatar
Per Abrahamsen committed
1313
  :mouse-down-action (lambda (widget event) nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
1314
  :action 'widget-default-action
Per Abrahamsen's avatar
Per Abrahamsen committed
1315 1316
  :notify 'widget-default-notify
  :prompt-value 'widget-default-prompt-value)
Per Abrahamsen's avatar
Per Abrahamsen committed
1317

Per Abrahamsen's avatar
Per Abrahamsen committed
1318 1319 1320 1321 1322 1323
(defun widget-default-complete (widget)
  "Call the value of the :complete-function property of WIDGET.
If that does not exists, call the value of `widget-complete-field'."
  (let ((fun (widget-get widget :complete-function)))
    (call-interactively (or fun widget-complete-field))))

Per Abrahamsen's avatar
Per Abrahamsen committed
1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340
(defun widget-default-create (widget)
  "Create WIDGET at point in the current buffer."
  (widget-specify-insert
   (let ((from (point))
	 button-begin button-end
	 sample-begin sample-end
	 doc-begin doc-end
	 value-pos)
     (insert (widget-get widget :format))
     (goto-char from)
     ;; Parse escapes in format.
     (while (re-search-forward "%\\(.\\)" nil t)
       (let ((escape (aref (match-string 1) 0)))
	 (replace-match "" t t)
	 (cond ((eq escape ?%)
		(insert "%"))
	       ((eq escape ?\[)
Per Abrahamsen's avatar
Per Abrahamsen committed
1341
		(setq button-begin (point))
Per Abrahamsen's avatar
Per Abrahamsen committed
1342
		(insert (widget-get-indirect widget :button-prefix)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1343
	       ((eq escape ?\])
Per Abrahamsen's avatar
Per Abrahamsen committed
1344
		(insert (widget-get-indirect widget :button-suffix))
Per Abrahamsen's avatar
Per Abrahamsen committed
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
		(setq button-end (point)))
	       ((eq escape ?\{)
		(setq sample-begin (point)))
	       ((eq escape ?\})
		(setq sample-end (point)))
	       ((eq escape ?n)
		(when (widget-get widget :indent)
		  (insert "\n")
		  (insert-char ?  (widget-get widget :indent))))
	       ((eq escape ?t)
Per Abrahamsen's avatar
Per Abrahamsen committed
1355 1356 1357 1358 1359 1360 1361 1362 1363
		(let ((glyph (widget-get widget :tag-glyph))
		      (tag (widget-get widget :tag)))
		  (cond (glyph 
			 (widget-glyph-insert widget (or tag "image") glyph))
			(tag
			 (insert tag))
			(t
			 (let ((standard-output (current-buffer)))
			   (princ (widget-get widget :value)))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1364
	       ((eq escape ?d)
Per Abrahamsen's avatar
Per Abrahamsen committed
1365 1366 1367 1368 1369 1370 1371 1372
		(let ((doc (widget-get widget :doc)))
		  (when doc
		    (setq doc-begin (point))
		    (insert doc)
		    (while (eq (preceding-char) ?\n)
		      (delete-backward-char 1))
		    (insert "\n")
		    (setq doc-end (point)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394
	       ((eq escape ?v)
		(if (and button-begin (not button-end))
		    (widget-apply widget :value-create)
		  (setq value-pos (point))))
	       (t 
		(widget-apply widget :format-handler escape)))))
     ;; Specify button, sample, and doc, and insert value.
     (and button-begin button-end
	  (widget-specify-button widget button-begin button-end))
     (and sample-begin sample-end
	  (widget-specify-sample widget sample-begin sample-end))
     (and doc-begin doc-end
	  (widget-specify-doc widget doc-begin doc-end))
     (when value-pos
       (goto-char value-pos)
       (widget-apply widget :value-create)))
   (let ((from (copy-marker (point-min)))
	 (to (copy-marker (point-max))))
     (widget-specify-text from to)
     (set-marker-insertion-type from t)
     (set-marker-insertion-type to nil)
     (widget-put widget :from from)
Per Abrahamsen's avatar
Per Abrahamsen committed
1395 1396
     (widget-put widget :to to)))
  (widget-clear-undo))
Per Abrahamsen's avatar
Per Abrahamsen committed
1397 1398 1399

(defun widget-default-format-handler (widget escape)
  ;; We recognize the %h escape by default.
Per Abrahamsen's avatar
Per Abrahamsen committed
1400
  (let* ((buttons (widget-get widget :buttons)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1401
    (cond ((eq escape ?h)
Per Abrahamsen's avatar
Per Abrahamsen committed
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
	   (let* ((doc-property (widget-get widget :documentation-property))
		  (doc-try (cond ((widget-get widget :doc))
				 ((symbolp doc-property)
				  (documentation-property 
				   (widget-get widget :value)
				   doc-property))
				 (t
				  (funcall doc-property
					   (widget-get widget :value)))))
		  (doc-text (and (stringp doc-try)
				 (> (length doc-try) 1)
Per Abrahamsen's avatar
Per Abrahamsen committed
1413 1414
				 doc-try))
		  (doc-indent (widget-get widget :documentation-indent)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426
	     (when doc-text
	       (and (eq (preceding-char) ?\n)
		    (widget-get widget :indent)
		    (insert-char ?  (widget-get widget :indent)))
	       ;; The `*' in the beginning is redundant.
	       (when (eq (aref doc-text  0) ?*)
		 (setq doc-text (substring doc-text 1)))
	       ;; Get rid of trailing newlines.
	       (when (string-match "\n+\\'" doc-text)
		 (setq doc-text (substring doc-text 0 (match-beginning 0))))
	       (push (widget-create-child-and-convert
		      widget 'documentation-string
Per Abrahamsen's avatar
Per Abrahamsen committed
1427 1428 1429 1430 1431
		      :indent (cond ((numberp doc-indent )
				     doc-indent)
				    ((null doc-indent)
				     nil)
				    (t 0))
Per Abrahamsen's avatar
Per Abrahamsen committed
1432 1433
		      doc-text)
		     buttons))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449
	  (t 
	   (error "Unknown escape `%c'" escape)))
    (widget-put widget :buttons buttons)))

(defun widget-default-button-face-get (widget)
  ;; Use :button-face or widget-button-face
  (or (widget-get widget :button-face) 'widget-button-face))

(defun widget-default-sample-face-get (widget)
  ;; Use :sample-face.
  (widget-get widget :sample-face))

(defun widget-default-delete (widget)
  ;; Remove widget from the buffer.
  (let ((from (widget-get widget :from))
	(to (widget-get widget :to))
Per Abrahamsen's avatar
Per Abrahamsen committed
1450 1451
	(inactive-overlay (widget-get widget :inactive))
	(button-overlay (widget-get widget :button-overlay))
1452
	before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
1453 1454
	after-change-functions
	(inhibit-read-only t))
Per Abrahamsen's avatar
Per Abrahamsen committed
1455
    (widget-apply widget :value-delete)
Per Abrahamsen's avatar
Per Abrahamsen committed
1456 1457 1458 1459
    (when inactive-overlay
      (delete-overlay inactive-overlay))
    (when button-overlay
      (delete-overlay button-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
1460 1461 1462 1463
    (when (< from to)
      ;; Kludge: this doesn't need to be true for empty formats.
      (delete-region from to))
    (set-marker from nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
1464 1465
    (set-marker to nil))
  (widget-clear-undo))
Per Abrahamsen's avatar
Per Abrahamsen committed
1466 1467 1468

(defun widget-default-value-set (widget value)
  ;; Recreate widget with new value.
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487
  (let* ((old-pos (point))
	 (from (copy-marker (widget-get widget :from)))
	 (to (copy-marker (widget-get widget :to)))
	 (offset (if (and (<= from old-pos) (<= old-pos to))
		     (if (>= old-pos (1- to))
			 (- old-pos to 1)
		       (- old-pos from)))))
    ;;??? Bug: this ought to insert the new value before deleting the old one,
    ;; so that markers on either side of the value automatically 
    ;; stay on the same side.  -- rms.
    (save-excursion
      (goto-char (widget-get widget :from))
      (widget-apply widget :delete)
      (widget-put widget :value value)
      (widget-apply widget :create))
    (if offset
	(if (< offset 0)
	    (goto-char (+ (widget-get widget :to) offset 1))
	  (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523

(defun widget-default-value-inline (widget)
  ;; Wrap value in a list unless it is inline.
  (if (widget-get widget :inline)
      (widget-value widget)
    (list (widget-value widget))))

(defun widget-default-menu-tag-get (widget)
  ;; Use tag or value for menus.
  (or (widget-get widget :menu-tag)
      (widget-get widget :tag)
      (widget-princ-to-string (widget-get widget :value))))

(defun widget-default-active (widget)
  "Return t iff this widget active (user modifiable)."
  (and (not (widget-get widget :inactive))
       (let ((parent (widget-get widget :parent)))
	 (or (null parent) 
	     (widget-apply parent :active)))))

(defun widget-default-deactivate (widget)
  "Make WIDGET inactive for user modifications."
  (widget-specify-inactive widget
			   (widget-get widget :from)
			   (widget-get widget :to)))

(defun widget-default-action (widget &optional event)
  ;; Notify the parent when a widget change
  (let ((parent (widget-get widget :parent)))
    (when parent
      (widget-apply parent :notify widget event))))

(defun widget-default-notify (widget child &optional event)
  ;; Pass notification to parent.
  (widget-default-action widget event))

Per Abrahamsen's avatar
Per Abrahamsen committed
1524