wid-edit.el 108 KB
Newer Older
1
;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t;-*-
Per Abrahamsen's avatar
Per Abrahamsen committed
2
;;
3
;; Copyright (C) 1996, 1997, 1999, 2000 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 8
;; Keywords: extensions

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

;;; Code:

;;; Compatibility.
33

34 35
(defun widget-event-point (event)
  "Character position of the end of event if that exists, or nil."
36
  (posn-point (event-end event)))
37

38 39
(autoload 'pp-to-string "pp")
(autoload 'Info-goto-node "info")
Per Abrahamsen's avatar
Per Abrahamsen committed
40

41 42 43 44 45 46
(defun widget-button-release-event-p (event)
  "Non-nil if EVENT is a mouse-button-release event object."
  (and (eventp event)
       (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
       (or (memq 'click (event-modifiers event))
	   (memq  'drag (event-modifiers event)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
47 48 49 50 51 52

;;; Customization.

(defgroup widgets nil
  "Customization support for the Widget Library."
  :link '(custom-manual "(widget)Top")
Per Abrahamsen's avatar
Per Abrahamsen committed
53
  :link '(emacs-library-link :tag "Lisp File" "widget.el")
Per Abrahamsen's avatar
Per Abrahamsen committed
54 55 56 57
  :prefix "widget-"
  :group 'extensions
  :group 'hypermedia)

Per Abrahamsen's avatar
Per Abrahamsen committed
58 59 60 61
(defgroup widget-documentation nil
  "Options controling the display of documentation strings."
  :group 'widgets)

Per Abrahamsen's avatar
Per Abrahamsen committed
62 63 64 65 66
(defgroup widget-faces nil
  "Faces used by the widget library."
  :group 'widgets
  :group 'faces)

67
(defvar widget-documentation-face 'widget-documentation-face
68
  "Face used for documentation strings in widgets.
69 70
This exists as a variable so it can be set locally in certain buffers.")

Per Abrahamsen's avatar
Per Abrahamsen committed
71 72 73 74 75 76 77 78 79 80 81
(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)

82
(defvar widget-button-face 'widget-button-face
83
  "Face used for buttons in widgets.
84 85
This exists as a variable so it can be set locally in certain buffers.")

Per Abrahamsen's avatar
Per Abrahamsen committed
86 87
(defface widget-button-face '((t (:bold t)))
  "Face used for widget buttons."
Per Abrahamsen's avatar
Per Abrahamsen committed
88
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
89 90 91 92

(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
93
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
94 95 96

(defface widget-field-face '((((class grayscale color)
			       (background light))
Per Abrahamsen's avatar
Per Abrahamsen committed
97
			      (:background "gray85"))
Per Abrahamsen's avatar
Per Abrahamsen committed
98 99
			     (((class grayscale color)
			       (background dark))
Per Abrahamsen's avatar
Per Abrahamsen committed
100
			      (:background "dim gray"))
101
			     (t
Per Abrahamsen's avatar
Per Abrahamsen committed
102 103
			      (:italic t)))
  "Face used for editable fields."
Per Abrahamsen's avatar
Per Abrahamsen committed
104
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
105

Per Abrahamsen's avatar
Per Abrahamsen committed
106 107 108 109 110 111
(defface widget-single-line-field-face '((((class grayscale color)
					   (background light))
					  (:background "gray85"))
					 (((class grayscale color)
					   (background dark))
					  (:background "dim gray"))
112
					 (t
Per Abrahamsen's avatar
Per Abrahamsen committed
113 114 115 116
					  (:italic t)))
  "Face used for editable fields spanning only a single line."
  :group 'widget-faces)

117 118 119 120 121 122 123 124 125 126 127
;;; 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
128

Per Abrahamsen's avatar
Per Abrahamsen committed
129 130 131 132 133
;;; Utility functions.
;;
;; These are not really widget specific.

(defun widget-princ-to-string (object)
134 135 136 137 138
  "Return string representation of OBJECT, any Lisp object.
No quoting characters are used; no delimiters are printed around
the contents of strings."
  (with-output-to-string
      (princ object)))
Per Abrahamsen's avatar
Per Abrahamsen committed
139 140 141 142 143 144

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

Per Abrahamsen's avatar
Per Abrahamsen committed
145 146 147 148 149 150
(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)

151 152 153 154 155 156 157
(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
158 159 160 161 162 163
  "*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
164 165 166 167
(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
168 169 170
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
171 172 173 174 175 176 177 178
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)
179
	      event (fboundp 'x-popup-menu) (display-mouse-p))
Per Abrahamsen's avatar
Per Abrahamsen committed
180 181 182
	 ;; We are in Emacs-19, pressed by the mouse
	 (x-popup-menu event
		       (list title (cons "" items))))
183 184
	((or widget-menu-minibuffer-flag
	     (> (length items) widget-menu-max-shortcuts))
185
	 ;; Read the choice of name from the minibuffer.
186
	 (setq items (widget-remove-if 'stringp items))
Per Abrahamsen's avatar
Per Abrahamsen committed
187 188 189 190 191
	 (let ((val (completing-read (concat title ": ") items nil t)))
	   (if (stringp val)
	       (let ((try (try-completion val items)))
		 (when (stringp try)
		   (setq val try))
192
		 (cdr (assoc val items))))))
193 194 195 196 197 198
	(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)
199
		some-choice-enabled
200 201 202 203
		value)
	   ;; Define SPC as a prefix char to get to this menu.
	   (define-key overriding-terminal-local-map " "
	     (setq map (make-sparse-keymap title)))
204 205 206 207 208 209 210 211 212 213
	   (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))
214 215
		     (define-key map (vector next-digit) function)
		     (setq some-choice-enabled t)))
216 217 218 219
	       ;; 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"))
220 221
	   (or some-choice-enabled
	       (error "None of the choices is currently meaningful"))
222
	   (define-key map [?\C-g] 'keyboard-quit)
223
	   (define-key map [t] 'keyboard-quit)
224 225
	   (define-key map [?\M-\C-v] 'scroll-other-window)
	   (define-key map [?\M--] 'negative-argument)
226 227 228
	   (setcdr map (nreverse (cdr map)))
	   ;; Read a char with the menu, and return the result
	   ;; that corresponds to it.
229
	   (save-window-excursion
230 231 232 233 234 235 236 237 238 239 240
	     (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))
241 242
		   (setq value
			 (lookup-key overriding-terminal-local-map keys t)
243 244
			 char (string-to-char (substring keys 1)))
		   (cond ((eq value 'scroll-other-window)
245 246
			  (let ((minibuffer-scroll-window
				 (get-buffer-window buf)))
247
			    (if (> 0 arg)
248 249
				(scroll-other-window-down
				 (window-height minibuffer-scroll-window))
250 251 252 253 254 255
			      (scroll-other-window))
			    (setq arg 1)))
			 ((eq value 'negative-argument)
			  (setq arg -1))
			 (t
			  (setq arg 1)))))))
256 257 258
	   (when (eq value 'keyboard-quit)
	     (error "Canceled"))
	   value))))
Per Abrahamsen's avatar
Per Abrahamsen committed
259

260 261 262 263 264 265 266 267
(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
268 269
;;; Widget text specifications.
;; 
270
;; These functions are for specifying text properties.
Per Abrahamsen's avatar
Per Abrahamsen committed
271

272
(defvar widget-field-add-space t
Per Abrahamsen's avatar
Per Abrahamsen committed
273 274
  "Non-nil means add extra space at the end of editable text fields.
If you don't add the space, it will become impossible to edit a zero
275
size field.")
Per Abrahamsen's avatar
Per Abrahamsen committed
276

277
(defvar widget-field-use-before-change t
Per Abrahamsen's avatar
Per Abrahamsen committed
278
  "Non-nil means use `before-change-functions' to track editable fields.
279
This enables the use of undo, but doesn't work on Emacs 19.34 and earlier.
Per Abrahamsen's avatar
Per Abrahamsen committed
280
Using before hooks also means that the :notify function can't know the
281
new value.")
Per Abrahamsen's avatar
Per Abrahamsen committed
282

Per Abrahamsen's avatar
Per Abrahamsen committed
283
(defun widget-specify-field (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
284
  "Specify editable button for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
285 286 287 288 289
  ;; 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
290 291 292 293
    (cond ((null (widget-get widget :size))
	   (forward-char 1))
	  (widget-field-add-space
	   (insert-and-inherit " ")))
Per Abrahamsen's avatar
Per Abrahamsen committed
294
    (setq to (point)))
Per Abrahamsen's avatar
Per Abrahamsen committed
295
  (let ((map (widget-get widget :keymap))
Per Abrahamsen's avatar
Per Abrahamsen committed
296 297
	(face (or (widget-get widget :value-face) 'widget-field-face))
	(help-echo (widget-get widget :help-echo))
298
	(overlay (make-overlay from to nil
Per Abrahamsen's avatar
Per Abrahamsen committed
299
			       nil (or (not widget-field-add-space)
300
				       (widget-get widget :size)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
301
    (widget-put widget :field-overlay overlay)
302
    ;;(overlay-put overlay 'detachable nil)
Per Abrahamsen's avatar
Per Abrahamsen committed
303
    (overlay-put overlay 'field widget)
304
    (overlay-put overlay 'keymap map)
Per Abrahamsen's avatar
Per Abrahamsen committed
305
    (overlay-put overlay 'face face)
306
    ;;(overlay-put overlay 'balloon-help help-echo)
307
    (overlay-put overlay 'help-echo help-echo))
308 309 310 311 312 313 314 315 316
  (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)))
317
	(when size
318 319 320 321 322 323 324 325 326
	  (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
327 328

(defun widget-specify-button (widget from to)
Per Abrahamsen's avatar
Per Abrahamsen committed
329
  "Specify button for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
330
  (let ((face (widget-apply widget :button-face-get))
Per Abrahamsen's avatar
Per Abrahamsen committed
331 332 333 334
	(help-echo (widget-get widget :help-echo))
	(overlay (make-overlay from to nil t nil)))
    (widget-put widget :button-overlay overlay)
    (overlay-put overlay 'button widget)
335 336 337 338
    ;; We want to avoid the face with image buttons.
    (unless (widget-get widget :suppress-face)
      (overlay-put overlay 'face face)
      (overlay-put overlay 'mouse-face widget-mouse-face))
339
    ;;(overlay-put overlay 'balloon-help help-echo)
340
    (overlay-put overlay 'help-echo help-echo)))
Per Abrahamsen's avatar
Per Abrahamsen committed
341 342

(defun widget-specify-sample (widget from to)
343
  "Specify sample for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
344 345 346 347 348
  (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
349
(defun widget-specify-doc (widget from to)
350
  "Specify documentation for WIDGET between FROM and TO."
Per Abrahamsen's avatar
Per Abrahamsen committed
351 352 353 354
  (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
355 356

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

(defface widget-inactive-face '((((class grayscale color)
				  (background dark))
				 (:foreground "light gray"))
				(((class grayscale color)
				  (background light))
Per Abrahamsen's avatar
Per Abrahamsen committed
377
				 (:foreground "dim gray"))
378
				(t
Per Abrahamsen's avatar
Per Abrahamsen committed
379 380
				 (:italic t)))
  "Face used for inactive widgets."
Per Abrahamsen's avatar
Per Abrahamsen committed
381
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
382 383 384 385 386 387

(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
388 389
      ;; 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
390 391
      (overlay-put overlay 'evaporate t)
      (overlay-put overlay 'priority 100)
392
      (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
Per Abrahamsen's avatar
Per Abrahamsen committed
393 394 395 396 397
      (widget-put widget :inactive overlay))))

(defun widget-overlay-inactive (&rest junk)
  "Ignoring the arguments, signal an error."
  (unless inhibit-read-only
398
    (error "The widget here is not active")))
Per Abrahamsen's avatar
Per Abrahamsen committed
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413


(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
414 415
(defun widget-get-indirect (widget property)
  "In WIDGET, get the value of PROPERTY.
416
If the value is a symbol, return its binding.
Per Abrahamsen's avatar
Per Abrahamsen committed
417 418 419 420 421 422
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
423 424
(defun widget-member (widget property)
  "Non-nil iff there is a definition in WIDGET for PROPERTY."
425
  (cond ((plist-member (cdr widget) property)
Per Abrahamsen's avatar
Per Abrahamsen committed
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
	 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)))

442
(defun widget-default-get (widget)
443
  "Extract the default value of WIDGET."
444 445 446
  (or (widget-get widget :value)
      (widget-apply widget :default-get)))

Per Abrahamsen's avatar
Per Abrahamsen committed
447
(defun widget-match-inline (widget vals)
448
  "In WIDGET, match the start of VALS."
Per Abrahamsen's avatar
Per Abrahamsen committed
449 450
  (cond ((widget-get widget :inline)
	 (widget-apply widget :match-inline vals))
451
	((and (listp vals)
Per Abrahamsen's avatar
Per Abrahamsen committed
452 453 454 455 456 457
	      (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
458 459 460
  (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
461

Per Abrahamsen's avatar
Per Abrahamsen committed
462 463 464 465 466 467 468 469 470 471 472 473 474 475
;;; 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)
476
      (error "Value does not match %S type" (car widget)))
Per Abrahamsen's avatar
Per Abrahamsen committed
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    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
493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
(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)))))

513
;;; Images.
Per Abrahamsen's avatar
Per Abrahamsen committed
514

515 516 517
(defcustom widget-image-directory (file-name-as-directory
				   (expand-file-name "custom" data-directory))
  "Where widget button images are located.
Per Abrahamsen's avatar
Per Abrahamsen committed
518
If this variable is nil, widget will try to locate the directory
Per Abrahamsen's avatar
Per Abrahamsen committed
519
automatically."
Per Abrahamsen's avatar
Per Abrahamsen committed
520 521 522
  :group 'widgets
  :type 'directory)

523 524 525
(defcustom widget-image-enable t
  "If non nil, use image buttons in widgets when available."
  :version "21.1"
Per Abrahamsen's avatar
Per Abrahamsen committed
526 527 528
  :group 'widgets
  :type 'boolean)

Per Abrahamsen's avatar
Per Abrahamsen committed
529 530 531 532 533 534 535 536 537 538
(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")))))

539 540 541
(defun widget-image-find (image)
  "Create a graphical button from IMAGE.
IMAGE should either already be an image, or be a file name sans
Per Abrahamsen's avatar
Per Abrahamsen committed
542
extension (xpm, xbm, gif, jpg, or png) located in
543 544 545
`widget-image-directory' or otherwise where `find-image' will find it."
  (cond ((not (and image widget-image-enable (display-graphic-p)))
	 ;; We don't want or can't use images.
Per Abrahamsen's avatar
Per Abrahamsen committed
546
	 nil)
547 548 549
	((and (consp image)
	      (eq 'image (car image)))
	 ;; Already an image spec.  Use it.
Per Abrahamsen's avatar
Per Abrahamsen committed
550
	 image)
Per Abrahamsen's avatar
Per Abrahamsen committed
551 552
	((stringp image)
	 ;; A string.  Look it up in relevant directories.
553
	 (let* ((load-path (cons widget-image-directory load-path))
Per Abrahamsen's avatar
Per Abrahamsen committed
554
		(formats widget-image-conversion)
555 556 557 558 559 560
		specs)
	   (dolist (elt widget-image-conversion)
	     (dolist (ext (cdr elt))
	       (push (list :type (car elt) :file (concat image ext)) specs)))
	   (setq specs (nreverse specs))
	   (find-image specs)))
Per Abrahamsen's avatar
Per Abrahamsen committed
561
	(t
Per Abrahamsen's avatar
Per Abrahamsen committed
562
	 ;; Oh well.
Per Abrahamsen's avatar
Per Abrahamsen committed
563 564
	 nil)))

565 566 567 568 569 570
(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.")

(defun widget-image-insert (widget tag image &optional down inactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
571
  "In WIDGET, insert the text TAG or, if supported, IMAGE.
572 573 574 575 576 577 578 579 580 581 582 583
IMAGE should either be an image or an image file name sans extension
\(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.

Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
button is pressed or inactive, respectively.  These are currently ignored."
  (if (and (display-graphic-p)
	   (setq image (widget-image-find image)))
      (progn (widget-put widget :suppress-face t)
	     (insert-image image
			   (propertize
			    tag 'mouse-face widget-button-pressed-face)))
    (insert tag)))
Per Abrahamsen's avatar
Per Abrahamsen committed
584

Per Abrahamsen's avatar
Per Abrahamsen committed
585 586 587 588 589 590 591 592 593
;;; 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
594
  :group 'widget-button)
Per Abrahamsen's avatar
Per Abrahamsen committed
595 596 597 598

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

Per Abrahamsen's avatar
Per Abrahamsen committed
601 602 603 604
;;; Creating Widgets.

;;;###autoload
(defun widget-create (type &rest args)
605
  "Create widget of TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
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."
625
  (let ((widget (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
626 627 628 629 630 631 632 633 634 635
    (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."
636
  (let ((widget (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
    (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)
652
  "Convert TYPE to a widget without inserting it in the buffer.
Per Abrahamsen's avatar
Per Abrahamsen committed
653 654
The optional ARGS are additional keyword arguments."
  ;; Don't touch the type.
655
  (let* ((widget (if (symbolp type)
Per Abrahamsen's avatar
Per Abrahamsen committed
656
		     (list type)
657
		   (copy-sequence type)))
Per Abrahamsen's avatar
Per Abrahamsen committed
658 659 660 661 662
	 (current widget)
	 (keys args))
    ;; First set the :args keyword.
    (while (cdr current)		;Look in the type.
      (let ((next (car (cdr current))))
663
	(if (keywordp next)
Per Abrahamsen's avatar
Per Abrahamsen committed
664 665 666 667 668
	    (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)))
669
	(if (keywordp next)
Per Abrahamsen's avatar
Per Abrahamsen committed
670 671 672 673 674 675 676 677 678 679 680
	    (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.
681
    (while keys
Per Abrahamsen's avatar
Per Abrahamsen committed
682
      (let ((next (nth 0 keys)))
683 684
	(if (keywordp next)
	    (progn
Per Abrahamsen's avatar
Per Abrahamsen committed
685 686 687 688 689 690 691 692 693 694 695 696 697 698
	      (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)
699
	before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
700 701
	after-change-functions
	(from (point)))
Per Abrahamsen's avatar
Per Abrahamsen committed
702
    (apply 'insert args)))
Per Abrahamsen's avatar
Per Abrahamsen committed
703

Per Abrahamsen's avatar
Per Abrahamsen committed
704 705 706
(defun widget-convert-text (type from to
				 &optional button-from button-to
				 &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
707
  "Return a widget of type TYPE with endpoint FROM TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
708
Optional ARGS are extra keyword arguments for TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
709 710
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
711 712 713
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
714 715 716 717 718 719 720 721 722 723
	(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
724
(defun widget-convert-button (type from to &rest args)
Per Abrahamsen's avatar
Per Abrahamsen committed
725
  "Return a widget of type TYPE with endpoint FROM TO.
Per Abrahamsen's avatar
Per Abrahamsen committed
726
Optional ARGS are extra keyword arguments for TYPE.
Per Abrahamsen's avatar
Per Abrahamsen committed
727 728 729
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
730 731 732 733 734 735 736
  (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
737
	(sample (widget-get widget :sample-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
738
	(doc (widget-get widget :doc-overlay))
Per Abrahamsen's avatar
Per Abrahamsen committed
739 740 741 742
	(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
743 744
    (when button
      (delete-overlay button))
Per Abrahamsen's avatar
Per Abrahamsen committed
745 746
    (when sample
      (delete-overlay sample))
Per Abrahamsen's avatar
Per Abrahamsen committed
747 748
    (when doc
      (delete-overlay doc))
Per Abrahamsen's avatar
Per Abrahamsen committed
749 750
    (when field
      (delete-overlay field))
751
    (mapc 'widget-leave-text children)))
Per Abrahamsen's avatar
Per Abrahamsen committed
752

Per Abrahamsen's avatar
Per Abrahamsen committed
753 754
;;; Keymap and Commands.

755 756 757 758 759 760 761 762
(defvar widget-keymap
  (let ((map (make-sparse-keymap)))
    (define-key map "\t" 'widget-forward)
    (define-key map [(shift tab)] 'widget-backward)
    (define-key map [backtab] 'widget-backward)
    (define-key map [down-mouse-2] 'widget-button-click)
    (define-key map "\C-m" 'widget-button-press)
    map)
Per Abrahamsen's avatar
Per Abrahamsen committed
763 764 765 766 767 768 769
  "Keymap containing useful binding for buffers containing widgets.
Recommended as a parent keymap for modes using widgets.")

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

770 771 772 773 774 775 776 777 778 779
(defvar widget-field-keymap
  (let ((map (copy-keymap widget-keymap)))
    (define-key map [menu-bar] nil)
    (define-key map "\C-k" 'widget-kill-line)
    (define-key map "\M-\t" 'widget-complete)
    (define-key map "\C-m" 'widget-field-activate)
    (define-key map "\C-a" 'widget-beginning-of-line)
    (define-key map "\C-e" 'widget-end-of-line)
    (set-keymap-parent map global-map)
    map)
Per Abrahamsen's avatar
Per Abrahamsen committed
780 781
  "Keymap used inside an editable field.")

782 783 784 785 786 787 788
(defvar widget-text-keymap
  (let ((map (copy-keymap widget-keymap)))
    (define-key map [menu-bar] 'nil)
    (define-key map "\C-a" 'widget-beginning-of-line)
    (define-key map "\C-e" 'widget-end-of-line)
    (set-keymap-parent map global-map)
    map)
Per Abrahamsen's avatar
Per Abrahamsen committed
789 790 791
  "Keymap used inside a text field.")

(defun widget-field-activate (pos &optional event)
Per Abrahamsen's avatar
Per Abrahamsen committed
792
  "Invoke the ediable field at point."
Per Abrahamsen's avatar
Per Abrahamsen committed
793
  (interactive "@d")
Per Abrahamsen's avatar
Per Abrahamsen committed
794
  (let ((field (get-char-property pos 'field)))
Per Abrahamsen's avatar
Per Abrahamsen committed
795 796 797 798 799
    (if field
	(widget-apply-action field event)
      (call-interactively
       (lookup-key widget-global-map (this-command-keys))))))

800
(defface widget-button-pressed-face
Per Abrahamsen's avatar
Per Abrahamsen committed
801 802 803 804 805
  '((((class color))
     (:foreground "red"))
    (t
     (:bold t :underline t)))
  "Face used for pressed buttons."
Per Abrahamsen's avatar
Per Abrahamsen committed
806
  :group 'widget-faces)
Per Abrahamsen's avatar
Per Abrahamsen committed
807

Per Abrahamsen's avatar
Per Abrahamsen committed
808
(defun widget-button-click (event)
809
  "Invoke the button that the mouse is pointing at."
Per Abrahamsen's avatar
Per Abrahamsen committed
810
  (interactive "@e")
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874
  (if (widget-event-point event)
      (save-excursion
	(mouse-set-point event)
	(let* ((pos (widget-event-point event))
	       (button (get-char-property pos 'button)))
	  (if button
	      (let* ((overlay (widget-get button :button-overlay))
		     (face (overlay-get overlay 'face))
		     (mouse-face (overlay-get overlay 'mouse-face)))
		(unwind-protect
		    (let ((track-mouse t))
		      (save-excursion
			(when face	; avoid changing around image
			  (overlay-put overlay
				       'face widget-button-pressed-face)
			  (overlay-put overlay
				       'mouse-face widget-button-pressed-face))
			(unless (widget-apply button :mouse-down-action event)
			  (while (not (widget-button-release-event-p event))
			    (setq event (read-event)
				  pos (widget-event-point event))
			    (if (and pos
				     (eq (get-char-property pos 'button)
					 button))
				(when face
				  (overlay-put overlay
					       'face
					       widget-button-pressed-face)
				  (overlay-put overlay
					       'mouse-face
					       widget-button-pressed-face))
			      (overlay-put overlay 'face face)
			      (overlay-put overlay 'mouse-face mouse-face))))
			(when (and pos
				   (eq (get-char-property pos 'button) button))
			  (widget-apply-action button event))))
		  (overlay-put overlay 'face face)
		  (overlay-put overlay 'mouse-face mouse-face)))
	    (let ((up t)
		  command)
	      ;; Find the global command to run, and check whether it
	      ;; is bound to an up event.
	      (if (memq (event-basic-type event) '(mouse-1 down-mouse-1))
		  (cond ((setq command	;down event
			       (lookup-key widget-global-map [down-mouse-1]))
			 (setq up nil))
			((setq command	;up event
			       (lookup-key widget-global-map [mouse-1]))))
		(cond ((setq command	;down event
			     (lookup-key widget-global-map [down-mouse-2]))
		       (setq up nil))
		      ((setq command	;up event
			     (lookup-key widget-global-map [mouse-2])))))
	      (when up
		;; Don't execute up events twice.
		(while (not (widget-button-release-event-p event))
		  (setq event (read-event))))
	      (when command
		(call-interactively command)))))
	  (unless (pos-visible-in-window-p (widget-event-point event))
	    (mouse-set-point event)
	    (beginning-of-line)
	    (recenter)))
    (message "You clicked somewhere weird.")))
Per Abrahamsen's avatar
Per Abrahamsen committed
875

Per Abrahamsen's avatar
Per Abrahamsen committed
876
(defun widget-button-press (pos &optional event)
Per Abrahamsen's avatar
Per Abrahamsen committed
877
  "Invoke button at POS."
Per Abrahamsen's avatar
Per Abrahamsen committed
878
  (interactive "@d")
Per Abrahamsen's avatar
Per Abrahamsen committed
879
  (let ((button (get-char-property pos 'button)))
Per Abrahamsen's avatar
Per Abrahamsen committed
880 881 882 883 884 885
    (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
886 887 888 889 890
(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)))
891 892
  (let ((widget (or (get-char-property pos 'button)
		    (get-char-property pos 'field))))
Per Abrahamsen's avatar
Per Abrahamsen committed
893 894 895 896
    (if widget
	(let ((order (widget-get widget :tab-order)))
	  (if order
	      (if (>= order 0)
897 898
		  widget)
	    widget)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
899

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

Per Abrahamsen's avatar
Per Abrahamsen committed
904 905 906
(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
907
  (or (bobp) (> arg 0) (backward-char))
Per Abrahamsen's avatar
Per Abrahamsen committed
908
  (let ((pos (point))
Per Abrahamsen's avatar
Per Abrahamsen committed
909
	(number arg)
Per Abrahamsen's avatar
Per Abrahamsen committed
910
	(old (widget-tabable-at))
Per Abrahamsen's avatar
Per Abrahamsen committed
911 912 913
	new)
    ;; Forward.
    (while (> arg 0)
Per Abrahamsen's avatar
Per Abrahamsen committed
914 915 916 917 918 919
      (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
920 921 922
      (and (eq pos (point))
	   (eq arg number)
	   (error "No buttons or fields found"))
Per Abrahamsen's avatar
Per Abrahamsen committed
923
      (let ((new (widget-tabable-at)))
Per Abrahamsen's avatar
Per Abrahamsen committed
924 925
	(when new
	  (unless (eq new old)
Per Abrahamsen's avatar
Per Abrahamsen committed
926
	    (setq arg (1- arg))
Per Abrahamsen's avatar
Per Abrahamsen committed
927 928 929
	    (setq old new)))))
    ;; Backward.
    (while (< arg 0)
Per Abrahamsen's avatar
Per Abrahamsen committed
930 931 932 933 934 935
      (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
936 937 938
      (and (eq pos (point))
	   (eq arg number)
	   (error "No buttons or fields found"))
Per Abrahamsen's avatar
Per Abrahamsen committed
939
      (let ((new (widget-tabable-at)))
Per Abrahamsen's avatar
Per Abrahamsen committed
940 941
	(when new
	  (unless (eq new old)
Per Abrahamsen's avatar
Per Abrahamsen committed
942 943 944 945
	    (setq arg (1+ arg))))))
    (let ((new (widget-tabable-at)))
      (while (eq (widget-tabable-at) new)
	(backward-char)))
Per Abrahamsen's avatar
Per Abrahamsen committed
946 947 948
    (forward-char))
  (widget-echo-help (point))
  (run-hooks 'widget-move-hook))
Per Abrahamsen's avatar
Per Abrahamsen committed
949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966

(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
967
  (let* ((field (widget-field-find (point)))
968
	 (start (and field (widget-field-start field)))
969
         (bol (line-beginning-position)))
970 971 972
    (goto-char (if start
                   (max start bol)
                 bol))))
Per Abrahamsen's avatar
Per Abrahamsen committed
973 974 975 976

(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
977
  (let* ((field (widget-field-find (point)))
978
	 (end (and field (widget-field-end field)))
979
         (eol (line-end-position)))
980 981 982
    (goto-char (if end
                   (min end eol)
                 eol))))
Per Abrahamsen's avatar
Per Abrahamsen committed
983 984 985 986

(defun widget-kill-line ()
  "Kill to end of field or end of line, whichever is first."
  (interactive)
Per Abrahamsen's avatar
Per Abrahamsen committed
987 988 989 990 991
  (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
992 993
      (call-interactively 'kill-line))))

Per Abrahamsen's avatar
Per Abrahamsen committed
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
(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
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
;;; 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)
1023
	before-change-functions
Per Abrahamsen's avatar
Per Abrahamsen committed
1024 1025 1026 1027 1028
	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
1029 1030
      (let ((from (car (widget-get field :field-overlay)))
	    (to (cdr (widget-get field :field-overlay))))
1031
	(widget-specify-field field
Per Abrahamsen's avatar
Per Abrahamsen committed
1032
			      (marker-position from) (marker-position to))
Per Abrahamsen's avatar
Per Abrahamsen committed
1033 1034
	(set-marker from nil)
	(set-marker to nil))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1035
  (widget-clear-undo)
Per Abrahamsen's avatar
Per Abrahamsen committed
1036
  (widget-add-change))
Per Abrahamsen's avatar
Per Abrahamsen committed
1037 1038 1039 1040 1041 1042 1043 1044 1045

(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
1046 1047
(defun widget-field-buffer (widget)
  "Return the start of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1048 1049
  (let ((overlay (widget-get widget :field-overlay)))
    (and overlay (overlay-buffer overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1050 1051 1052

(defun widget-field-start (widget)
  "Return the start of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1053 1054
  (let ((overlay (widget-get widget :field-overlay)))
    (and overlay (overlay-start overlay))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1055 1056 1057

(defun widget-field-end (widget)
  "Return the end of WIDGET's editing field."
Per Abrahamsen's avatar
Per Abrahamsen committed
1058 1059
  (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
1060 1061
    (and overlay (if (or widget-field-add-space
			 (null (widget-get widget :size)))
Per Abrahamsen's avatar
Per Abrahamsen committed
1062 1063
		     (1- (overlay-end overlay))
		   (overlay-end overlay)))))
Per Abrahamsen's avatar
Per Abrahamsen committed
1064

Per Abrahamsen's avatar
Per Abrahamsen committed
1065
(defun widget-field-find (pos)
Per Abrahamsen's avatar
Per Abrahamsen committed
1066 1067
  "Return the field at POS.
Unlike (get-char-property POS 'field) this, works with empty fields too."
Per Abrahamsen's avatar
Per Abrahamsen committed
1068 1069 1070 1071 1072
  (let ((fields widget-field-list)
	field found)
    (while fields
      (setq field (car fields)
	    fields (cdr fields))
Per Abrahamsen's avatar
Per Abrahamsen committed
1073 1074 1075 1076 1077 1078
      (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
1079 1080
    found))