Merge from emacs-24; up to 2012-12-25T15:07:59Z!dmantipov@yandex.ru
[bpt/emacs.git] / lisp / wid-edit.el
1 ;;; wid-edit.el --- Functions for creating and using widgets -*-byte-compile-dynamic: t; lexical-binding:t -*-
2 ;;
3 ;; Copyright (C) 1996-1997, 1999-2013 Free Software Foundation, Inc.
4 ;;
5 ;; Author: Per Abrahamsen <abraham@dina.kvl.dk>
6 ;; Maintainer: FSF
7 ;; Keywords: extensions
8 ;; Package: emacs
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Wishlist items (from widget.texi):
26
27 ;; * The `menu-choice' tag should be prettier, something like the
28 ;; abbreviated menus in Open Look.
29
30 ;; * Finish `:tab-order'.
31
32 ;; * Make indentation work with glyphs and proportional fonts.
33
34 ;; * Add commands to show overview of object and class hierarchies to
35 ;; the browser.
36
37 ;; * Find a way to disable mouse highlight for inactive widgets.
38
39 ;; * Find a way to make glyphs look inactive.
40
41 ;; * Add `key-binding' widget.
42
43 ;; * Add `widget' widget for editing widget specifications.
44
45 ;; * Find clean way to implement variable length list. See
46 ;; `TeX-printer-list' for an explanation.
47
48 ;; * `C-h' in `widget-prompt-value' should give type specific help.
49
50 ;; * A mailto widget. [This should work OK as a url-link if with
51 ;; browse-url-browser-function' set up appropriately.]
52
53 ;;; Commentary:
54 ;;
55 ;; See `widget.el'.
56
57 ;;; Code:
58
59 ;;; Compatibility.
60
61 (defun widget-event-point (event)
62 "Character position of the end of event if that exists, or nil."
63 (posn-point (event-end event)))
64
65 (defun widget-button-release-event-p (event)
66 "Non-nil if EVENT is a mouse-button-release event object."
67 (and (eventp event)
68 (memq (event-basic-type event) '(mouse-1 mouse-2 mouse-3))
69 (or (memq 'click (event-modifiers event))
70 (memq 'drag (event-modifiers event)))))
71
72 ;;; Customization.
73
74 (defgroup widgets nil
75 "Customization support for the Widget Library."
76 :link '(custom-manual "(widget)Top")
77 :link '(emacs-library-link :tag "Lisp File" "widget.el")
78 :prefix "widget-"
79 :group 'extensions)
80
81 (defgroup widget-documentation nil
82 "Options controlling the display of documentation strings."
83 :group 'widgets)
84
85 (defgroup widget-faces nil
86 "Faces used by the widget library."
87 :group 'widgets
88 :group 'faces)
89
90 (defvar widget-documentation-face 'widget-documentation
91 "Face used for documentation strings in widgets.
92 This exists as a variable so it can be set locally in certain buffers.")
93
94 (defface widget-documentation '((((class color)
95 (background dark))
96 (:foreground "lime green"))
97 (((class color)
98 (background light))
99 (:foreground "dark green"))
100 (t nil))
101 "Face used for documentation text."
102 :group 'widget-documentation
103 :group 'widget-faces)
104 (define-obsolete-face-alias 'widget-documentation-face
105 'widget-documentation "22.1")
106
107 (defvar widget-button-face 'widget-button
108 "Face used for buttons in widgets.
109 This exists as a variable so it can be set locally in certain buffers.")
110
111 (defface widget-button '((t (:weight bold)))
112 "Face used for widget buttons."
113 :group 'widget-faces)
114 (define-obsolete-face-alias 'widget-button-face 'widget-button "22.1")
115
116 (defcustom widget-mouse-face 'highlight
117 "Face used for widget buttons when the mouse is above them."
118 :type 'face
119 :group 'widget-faces)
120
121 ;; TTY gets special definitions here and in the next defface, because
122 ;; the gray colors defined for other displays cause black text on a black
123 ;; background, at least on light-background TTYs.
124 (defface widget-field '((((type tty))
125 :background "yellow3"
126 :foreground "black")
127 (((class grayscale color)
128 (background light))
129 :background "gray85")
130 (((class grayscale color)
131 (background dark))
132 :background "dim gray")
133 (t
134 :slant italic))
135 "Face used for editable fields."
136 :group 'widget-faces)
137 (define-obsolete-face-alias 'widget-field-face 'widget-field "22.1")
138
139 (defface widget-single-line-field '((((type tty))
140 :background "green3"
141 :foreground "black")
142 (((class grayscale color)
143 (background light))
144 :background "gray85")
145 (((class grayscale color)
146 (background dark))
147 :background "dim gray")
148 (t
149 :slant italic))
150 "Face used for editable fields spanning only a single line."
151 :group 'widget-faces)
152 (define-obsolete-face-alias 'widget-single-line-field-face
153 'widget-single-line-field "22.1")
154
155 ;;; This causes display-table to be loaded, and not usefully.
156 ;;;(defvar widget-single-line-display-table
157 ;;; (let ((table (make-display-table)))
158 ;;; (aset table 9 "^I")
159 ;;; (aset table 10 "^J")
160 ;;; table)
161 ;;; "Display table used for single-line editable fields.")
162
163 ;;;(when (fboundp 'set-face-display-table)
164 ;;; (set-face-display-table 'widget-single-line-field-face
165 ;;; widget-single-line-display-table))
166
167 ;;; Utility functions.
168 ;;
169 ;; These are not really widget specific.
170
171 (defun widget-princ-to-string (object)
172 "Return string representation of OBJECT, any Lisp object.
173 No quoting characters are used; no delimiters are printed around
174 the contents of strings."
175 (with-output-to-string
176 (princ object)))
177
178 (defun widget-clear-undo ()
179 "Clear all undo information."
180 (buffer-disable-undo (current-buffer))
181 (buffer-enable-undo))
182
183 (defcustom widget-menu-max-size 40
184 "Largest number of items allowed in a popup-menu.
185 Larger menus are read through the minibuffer."
186 :group 'widgets
187 :type 'integer)
188
189 (defcustom widget-menu-max-shortcuts 40
190 "Largest number of items for which it works to choose one with a character.
191 For a larger number of items, the minibuffer is used."
192 :group 'widgets
193 :type 'integer)
194
195 (defcustom widget-menu-minibuffer-flag nil
196 "Control how to ask for a choice from the keyboard.
197 Non-nil means use the minibuffer;
198 nil means read a single character."
199 :group 'widgets
200 :type 'boolean)
201
202 (defun widget-choose (title items &optional event)
203 "Choose an item from a list.
204
205 First argument TITLE is the name of the list.
206 Second argument ITEMS is a list whose members are either
207 (NAME . VALUE), to indicate selectable items, or just strings to
208 indicate unselectable items.
209 Optional third argument EVENT is an input event.
210
211 The user is asked to choose between each NAME from the items alist,
212 and the VALUE of the chosen element will be returned. If EVENT is a
213 mouse event, and the number of elements in items is less than
214 `widget-menu-max-size', a popup menu will be used, otherwise the
215 minibuffer."
216 (cond ((and (< (length items) widget-menu-max-size)
217 event (display-popup-menus-p))
218 ;; Mouse click.
219 (x-popup-menu event
220 (list title (cons "" items))))
221 ((or widget-menu-minibuffer-flag
222 (> (length items) widget-menu-max-shortcuts))
223 ;; Read the choice of name from the minibuffer.
224 (setq items (widget-remove-if 'stringp items))
225 (let ((val (completing-read (concat title ": ") items nil t)))
226 (if (stringp val)
227 (let ((try (try-completion val items)))
228 (when (stringp try)
229 (setq val try))
230 (cdr (assoc val items))))))
231 (t
232 ;; Construct a menu of the choices
233 ;; and then use it for prompting for a single character.
234 (let* ((overriding-terminal-local-map (make-sparse-keymap))
235 (next-digit ?0)
236 map choice some-choice-enabled value)
237 ;; Define SPC as a prefix char to get to this menu.
238 (define-key overriding-terminal-local-map " "
239 (setq map (make-sparse-keymap title)))
240 (with-current-buffer (get-buffer-create " widget-choose")
241 (erase-buffer)
242 (insert "Available choices:\n\n")
243 (while items
244 (setq choice (car items) items (cdr items))
245 (if (consp choice)
246 (let* ((name (car choice))
247 (function (cdr choice)))
248 (insert (format "%c = %s\n" next-digit name))
249 (define-key map (vector next-digit) function)
250 (setq some-choice-enabled t)))
251 ;; Allocate digits to disabled alternatives
252 ;; so that the digit of a given alternative never varies.
253 (setq next-digit (1+ next-digit)))
254 (insert "\nC-g = Quit")
255 (goto-char (point-min))
256 (forward-line))
257 (or some-choice-enabled
258 (error "None of the choices is currently meaningful"))
259 (define-key map [?\C-g] 'keyboard-quit)
260 (define-key map [t] 'keyboard-quit)
261 (define-key map [?\M-\C-v] 'scroll-other-window)
262 (define-key map [?\M--] 'negative-argument)
263 (setcdr map (nreverse (cdr map)))
264 ;; Read a char with the menu, and return the result
265 ;; that corresponds to it.
266 (save-window-excursion
267 (let ((buf (get-buffer " widget-choose")))
268 (fit-window-to-buffer (display-buffer buf))
269 (let ((cursor-in-echo-area t)
270 keys
271 (char 0)
272 (arg 1))
273 (while (not (or (and (integerp char)
274 (>= char ?0) (< char next-digit))
275 (eq value 'keyboard-quit)))
276 ;; Unread a SPC to lead to our new menu.
277 (setq unread-command-events (cons ?\s unread-command-events))
278 (setq keys (read-key-sequence title))
279 (setq value
280 (lookup-key overriding-terminal-local-map keys t)
281 char (aref keys 1))
282 (cond ((eq value 'scroll-other-window)
283 (let ((minibuffer-scroll-window
284 (get-buffer-window buf)))
285 (if (> 0 arg)
286 (scroll-other-window-down
287 (window-height minibuffer-scroll-window))
288 (scroll-other-window))
289 (setq arg 1)))
290 ((eq value 'negative-argument)
291 (setq arg -1))
292 (t
293 (setq arg 1)))))))
294 (when (eq value 'keyboard-quit)
295 (error "Canceled"))
296 value))))
297
298 (defun widget-remove-if (predicate list)
299 (let (result (tail list))
300 (while tail
301 (or (funcall predicate (car tail))
302 (setq result (cons (car tail) result)))
303 (setq tail (cdr tail)))
304 (nreverse result)))
305
306 ;;; Widget text specifications.
307 ;;
308 ;; These functions are for specifying text properties.
309
310 ;; We can set it to nil now that get_local_map uses get_pos_property.
311 (defconst widget-field-add-space nil
312 "Non-nil means add extra space at the end of editable text fields.
313 If you don't add the space, it will become impossible to edit a zero
314 size field.")
315
316 (defvar widget-field-use-before-change t
317 "Non-nil means use `before-change-functions' to track editable fields.
318 This enables the use of undo. Using before hooks also means that
319 the :notify function can't know the new value.")
320
321 (defun widget-specify-field (widget from to)
322 "Specify editable button for WIDGET between FROM and TO."
323 ;; Terminating space is not part of the field, but necessary in
324 ;; order for local-map to work. Remove next sexp if local-map works
325 ;; at the end of the overlay.
326 (save-excursion
327 (goto-char to)
328 (cond ((null (widget-get widget :size))
329 (forward-char 1))
330 (widget-field-add-space
331 (insert-and-inherit " ")))
332 (setq to (point)))
333 (let ((keymap (widget-get widget :keymap))
334 (face (or (widget-get widget :value-face) 'widget-field))
335 (help-echo (widget-get widget :help-echo))
336 (follow-link (widget-get widget :follow-link))
337 (rear-sticky
338 (or (not widget-field-add-space) (widget-get widget :size))))
339 (if (functionp help-echo)
340 (setq help-echo 'widget-mouse-help))
341 (when (= (char-before to) ?\n)
342 ;; When the last character in the field is a newline, we want to
343 ;; give it a `field' char-property of `boundary', which helps the
344 ;; C-n/C-p act more naturally when entering/leaving the field. We
345 ;; do this by making a small secondary overlay to contain just that
346 ;; one character.
347 (let ((overlay (make-overlay (1- to) to nil t nil)))
348 (overlay-put overlay 'field 'boundary)
349 ;; We need the real field for tabbing.
350 (overlay-put overlay 'real-field widget)
351 ;; Use `local-map' here, not `keymap', so that normal editing
352 ;; works in the field when, say, Custom uses `suppress-keymap'.
353 (overlay-put overlay 'local-map keymap)
354 (overlay-put overlay 'face face)
355 (overlay-put overlay 'follow-link follow-link)
356 (overlay-put overlay 'help-echo help-echo))
357 (setq to (1- to))
358 (setq rear-sticky t))
359 (let ((overlay (make-overlay from to nil nil rear-sticky)))
360 (widget-put widget :field-overlay overlay)
361 ;;(overlay-put overlay 'detachable nil)
362 (overlay-put overlay 'field widget)
363 (overlay-put overlay 'local-map keymap)
364 (overlay-put overlay 'face face)
365 (overlay-put overlay 'follow-link follow-link)
366 (overlay-put overlay 'help-echo help-echo)))
367 (widget-specify-secret widget))
368
369 (defun widget-specify-secret (field)
370 "Replace text in FIELD with value of `:secret', if non-nil."
371 (let ((secret (widget-get field :secret))
372 (size (widget-get field :size)))
373 (when secret
374 (let ((begin (widget-field-start field))
375 (end (widget-field-end field)))
376 (when size
377 (while (and (> end begin)
378 (eq (char-after (1- end)) ?\s))
379 (setq end (1- end))))
380 (while (< begin end)
381 (let ((old (char-after begin)))
382 (unless (eq old secret)
383 (subst-char-in-region begin (1+ begin) old secret)
384 (put-text-property begin (1+ begin) 'secret old))
385 (setq begin (1+ begin))))))))
386
387 (defun widget-specify-button (widget from to)
388 "Specify button for WIDGET between FROM and TO."
389 (let ((overlay (make-overlay from to nil t nil))
390 (follow-link (widget-get widget :follow-link))
391 (help-echo (widget-get widget :help-echo)))
392 (widget-put widget :button-overlay overlay)
393 (if (functionp help-echo)
394 (setq help-echo 'widget-mouse-help))
395 (overlay-put overlay 'button widget)
396 (overlay-put overlay 'keymap (widget-get widget :keymap))
397 (overlay-put overlay 'evaporate t)
398 ;; We want to avoid the face with image buttons.
399 (unless (widget-get widget :suppress-face)
400 (overlay-put overlay 'face (widget-apply widget :button-face-get))
401 (overlay-put overlay 'mouse-face
402 ;; Make new list structure for the mouse-face value
403 ;; so that different widgets will have
404 ;; different `mouse-face' property values
405 ;; and will highlight separately.
406 (let ((mouse-face-value
407 (widget-apply widget :mouse-face-get)))
408 ;; If it's a list, copy it.
409 (if (listp mouse-face-value)
410 (copy-sequence mouse-face-value)
411 ;; If it's a symbol, put it in a list.
412 (list mouse-face-value)))))
413 (overlay-put overlay 'pointer 'hand)
414 (overlay-put overlay 'follow-link follow-link)
415 (overlay-put overlay 'help-echo help-echo)))
416
417 (defun widget-mouse-help (_window overlay _point)
418 "Help-echo callback for widgets whose :help-echo is a function."
419 (with-current-buffer (overlay-buffer overlay)
420 (let* ((widget (widget-at (overlay-start overlay)))
421 (help-echo (if widget (widget-get widget :help-echo))))
422 (if (functionp help-echo)
423 (funcall help-echo widget)
424 help-echo))))
425
426 (defun widget-specify-sample (widget from to)
427 "Specify sample for WIDGET between FROM and TO."
428 (let ((overlay (make-overlay from to nil t nil)))
429 (overlay-put overlay 'face (widget-apply widget :sample-face-get))
430 (overlay-put overlay 'evaporate t)
431 (widget-put widget :sample-overlay overlay)))
432
433 (defun widget-specify-doc (widget from to)
434 "Specify documentation for WIDGET between FROM and TO."
435 (let ((overlay (make-overlay from to nil t nil)))
436 (overlay-put overlay 'widget-doc widget)
437 (overlay-put overlay 'face widget-documentation-face)
438 (overlay-put overlay 'evaporate t)
439 (widget-put widget :doc-overlay overlay)))
440
441 (defmacro widget-specify-insert (&rest form)
442 "Execute FORM without inheriting any text properties."
443 `(save-restriction
444 (let ((inhibit-read-only t)
445 (inhibit-modification-hooks t))
446 (narrow-to-region (point) (point))
447 (prog1 (progn ,@form)
448 (goto-char (point-max))))))
449
450 (defface widget-inactive
451 '((t :inherit shadow))
452 "Face used for inactive widgets."
453 :group 'widget-faces)
454 (define-obsolete-face-alias 'widget-inactive-face
455 'widget-inactive "22.1")
456
457 (defun widget-specify-inactive (widget from to)
458 "Make WIDGET inactive for user modifications."
459 (unless (widget-get widget :inactive)
460 (let ((overlay (make-overlay from to nil t nil)))
461 (overlay-put overlay 'face 'widget-inactive)
462 ;; This is disabled, as it makes the mouse cursor change shape.
463 ;; (overlay-put overlay 'mouse-face 'widget-inactive)
464 (overlay-put overlay 'evaporate t)
465 (overlay-put overlay 'priority 100)
466 (overlay-put overlay 'modification-hooks '(widget-overlay-inactive))
467 (widget-put widget :inactive overlay))))
468
469 (defun widget-overlay-inactive (&rest _junk)
470 "Ignoring the arguments, signal an error."
471 (unless inhibit-read-only
472 (error "The widget here is not active")))
473
474
475 (defun widget-specify-active (widget)
476 "Make WIDGET active for user modifications."
477 (let ((inactive (widget-get widget :inactive)))
478 (when inactive
479 (delete-overlay inactive)
480 (widget-put widget :inactive nil))))
481
482 ;;; Widget Properties.
483
484 (defsubst widget-type (widget)
485 "Return the type of WIDGET. The type is a symbol."
486 (car widget))
487
488 ;;;###autoload
489 (defun widgetp (widget)
490 "Return non-nil if WIDGET is a widget."
491 (if (symbolp widget)
492 (get widget 'widget-type)
493 (and (consp widget)
494 (symbolp (car widget))
495 (get (car widget) 'widget-type))))
496
497 (defun widget-get-indirect (widget property)
498 "In WIDGET, get the value of PROPERTY.
499 If the value is a symbol, return its binding.
500 Otherwise, just return the value."
501 (let ((value (widget-get widget property)))
502 (if (symbolp value)
503 (symbol-value value)
504 value)))
505
506 (defun widget-member (widget property)
507 "Non-nil if there is a definition in WIDGET for PROPERTY."
508 (cond ((plist-member (cdr widget) property)
509 t)
510 ((car widget)
511 (widget-member (get (car widget) 'widget-type) property))
512 (t nil)))
513
514 (defun widget-value (widget)
515 "Extract the current value of WIDGET."
516 (widget-apply widget
517 :value-to-external (widget-apply widget :value-get)))
518
519 (defun widget-value-set (widget value)
520 "Set the current value of WIDGET to VALUE."
521 (widget-apply widget
522 :value-set (widget-apply widget
523 :value-to-internal value)))
524
525 (defun widget-default-get (widget)
526 "Extract the default external value of WIDGET."
527 (widget-apply widget :value-to-external
528 (or (widget-get widget :value)
529 (progn
530 (when (widget-get widget :args)
531 (let (args)
532 (dolist (arg (widget-get widget :args))
533 (setq args (append args
534 (if (widget-get arg :inline)
535 (widget-get arg :args)
536 (list arg)))))
537 (widget-put widget :args args)))
538 (widget-apply widget :default-get)))))
539
540 (defun widget-match-inline (widget vals)
541 "In WIDGET, match the start of VALS."
542 (cond ((widget-get widget :inline)
543 (widget-apply widget :match-inline vals))
544 ((and (listp vals)
545 (widget-apply widget :match (car vals)))
546 (cons (list (car vals)) (cdr vals)))
547 (t nil)))
548
549 (defun widget-apply-action (widget &optional event)
550 "Apply :action in WIDGET in response to EVENT."
551 (if (widget-apply widget :active)
552 (widget-apply widget :action event)
553 (error "Attempt to perform action on inactive widget")))
554
555 ;;; Helper functions.
556 ;;
557 ;; These are widget specific.
558
559 ;;;###autoload
560 (defun widget-prompt-value (widget prompt &optional value unbound)
561 "Prompt for a value matching WIDGET, using PROMPT.
562 The current value is assumed to be VALUE, unless UNBOUND is non-nil."
563 (unless (listp widget)
564 (setq widget (list widget)))
565 (setq prompt (format "[%s] %s" (widget-type widget) prompt))
566 (setq widget (widget-convert widget))
567 (let ((answer (widget-apply widget :prompt-value prompt value unbound)))
568 (unless (widget-apply widget :match answer)
569 (error "Value does not match %S type" (car widget)))
570 answer))
571
572 (defun widget-get-sibling (widget)
573 "Get the item WIDGET is assumed to toggle.
574 This is only meaningful for radio buttons or checkboxes in a list."
575 (let* ((children (widget-get (widget-get widget :parent) :children))
576 child)
577 (catch 'child
578 (while children
579 (setq child (car children)
580 children (cdr children))
581 (when (eq (widget-get child :button) widget)
582 (throw 'child child)))
583 nil)))
584
585 (defun widget-map-buttons (function &optional buffer maparg)
586 "Map FUNCTION over the buttons in BUFFER.
587 FUNCTION is called with the arguments WIDGET and MAPARG.
588
589 If FUNCTION returns non-nil, the walk is canceled.
590
591 The arguments MAPARG, and BUFFER default to nil and (current-buffer),
592 respectively."
593 (let ((cur (point-min))
594 (widget nil)
595 (overlays (if buffer
596 (with-current-buffer buffer (overlay-lists))
597 (overlay-lists))))
598 (setq overlays (append (car overlays) (cdr overlays)))
599 (while (setq cur (pop overlays))
600 (setq widget (overlay-get cur 'button))
601 (if (and widget (funcall function widget maparg))
602 (setq overlays nil)))))
603
604 ;;; Images.
605
606 (defcustom widget-image-directory (file-name-as-directory
607 (expand-file-name "images/custom" data-directory))
608 "Where widget button images are located.
609 If this variable is nil, widget will try to locate the directory
610 automatically."
611 :group 'widgets
612 :type 'directory)
613
614 (defcustom widget-image-enable t
615 "If non-nil, use image buttons in widgets when available."
616 :version "21.1"
617 :group 'widgets
618 :type 'boolean)
619
620 (defcustom widget-image-conversion
621 '((xpm ".xpm") (gif ".gif") (png ".png") (jpeg ".jpg" ".jpeg")
622 (xbm ".xbm"))
623 "Conversion alist from image formats to file name suffixes."
624 :group 'widgets
625 :type '(repeat (cons :format "%v"
626 (symbol :tag "Image Format" unknown)
627 (repeat :tag "Suffixes"
628 (string :format "%v")))))
629
630 (defun widget-image-find (image)
631 "Create a graphical button from IMAGE.
632 IMAGE should either already be an image, or be a file name sans
633 extension (xpm, xbm, gif, jpg, or png) located in
634 `widget-image-directory' or otherwise where `find-image' will find it."
635 (cond ((not (and image widget-image-enable (display-graphic-p)))
636 ;; We don't want or can't use images.
637 nil)
638 ((and (consp image)
639 (eq 'image (car image)))
640 ;; Already an image spec. Use it.
641 image)
642 ((stringp image)
643 ;; A string. Look it up in relevant directories.
644 (let* ((load-path (cons widget-image-directory load-path))
645 specs)
646 (dolist (elt widget-image-conversion)
647 (dolist (ext (cdr elt))
648 (push (list :type (car elt) :file (concat image ext))
649 specs)))
650 (find-image (nreverse specs))))
651 (t
652 ;; Oh well.
653 nil)))
654
655 (defvar widget-button-pressed-face 'widget-button-pressed
656 "Face used for pressed buttons in widgets.
657 This exists as a variable so it can be set locally in certain
658 buffers.")
659
660 (defun widget-image-insert (widget tag image &optional _down _inactive)
661 "In WIDGET, insert the text TAG or, if supported, IMAGE.
662 IMAGE should either be an image or an image file name sans extension
663 \(xpm, xbm, gif, jpg, or png) located in `widget-image-directory'.
664
665 Optional arguments DOWN and INACTIVE are used instead of IMAGE when the
666 button is pressed or inactive, respectively. These are currently ignored."
667 (if (and (featurep 'image)
668 (setq image (widget-image-find image)))
669 (progn (widget-put widget :suppress-face t)
670 (insert-image image tag))
671 (insert tag)))
672
673 (defun widget-move-and-invoke (event)
674 "Move to where you click, and if it is an active field, invoke it."
675 (interactive "e")
676 (mouse-set-point event)
677 (let ((pos (widget-event-point event)))
678 (if (and pos (get-char-property pos 'button))
679 (widget-button-click event))))
680
681 ;;; Buttons.
682
683 (defgroup widget-button nil
684 "The look of various kinds of buttons."
685 :group 'widgets)
686
687 (defcustom widget-button-prefix ""
688 "String used as prefix for buttons."
689 :type 'string
690 :group 'widget-button)
691
692 (defcustom widget-button-suffix ""
693 "String used as suffix for buttons."
694 :type 'string
695 :group 'widget-button)
696
697 ;;; Creating Widgets.
698
699 ;;;###autoload
700 (defun widget-create (type &rest args)
701 "Create widget of TYPE.
702 The optional ARGS are additional keyword arguments."
703 (let ((widget (apply 'widget-convert type args)))
704 (widget-apply widget :create)
705 widget))
706
707 (defun widget-create-child-and-convert (parent type &rest args)
708 "As part of the widget PARENT, create a child widget TYPE.
709 The child is converted, using the keyword arguments ARGS."
710 (let ((widget (apply 'widget-convert type args)))
711 (widget-put widget :parent parent)
712 (unless (widget-get widget :indent)
713 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
714 (or (widget-get widget :extra-offset) 0)
715 (widget-get parent :offset))))
716 (widget-apply widget :create)
717 widget))
718
719 (defun widget-create-child (parent type)
720 "Create widget of TYPE."
721 (let ((widget (widget-copy type)))
722 (widget-put widget :parent parent)
723 (unless (widget-get widget :indent)
724 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
725 (or (widget-get widget :extra-offset) 0)
726 (widget-get parent :offset))))
727 (widget-apply widget :create)
728 widget))
729
730 (defun widget-create-child-value (parent type value)
731 "Create widget of TYPE with value VALUE."
732 (let ((widget (widget-copy type)))
733 (widget-put widget :value (widget-apply widget :value-to-internal value))
734 (widget-put widget :parent parent)
735 (unless (widget-get widget :indent)
736 (widget-put widget :indent (+ (or (widget-get parent :indent) 0)
737 (or (widget-get widget :extra-offset) 0)
738 (widget-get parent :offset))))
739 (widget-apply widget :create)
740 widget))
741
742 ;;;###autoload
743 (defun widget-delete (widget)
744 "Delete WIDGET."
745 (widget-apply widget :delete))
746
747 (defun widget-copy (widget)
748 "Make a deep copy of WIDGET."
749 (widget-apply (copy-sequence widget) :copy))
750
751 (defun widget-convert (type &rest args)
752 "Convert TYPE to a widget without inserting it in the buffer.
753 The optional ARGS are additional keyword arguments."
754 ;; Don't touch the type.
755 (let* ((widget (if (symbolp type)
756 (list type)
757 (copy-sequence type)))
758 (current widget)
759 done
760 (keys args))
761 ;; First set the :args keyword.
762 (while (cdr current) ;Look in the type.
763 (if (and (keywordp (cadr current))
764 ;; If the last element is a keyword,
765 ;; it is still the :args element,
766 ;; even though it is a keyword.
767 (cddr current))
768 (if (eq (cadr current) :args)
769 ;; If :args is explicitly specified, obey it.
770 (setq current nil)
771 ;; Some other irrelevant keyword.
772 (setq current (cdr (cdr current))))
773 (setcdr current (list :args (cdr current)))
774 (setq current nil)))
775 (while (and args (not done)) ;Look in ARGS.
776 (cond ((eq (car args) :args)
777 ;; Handle explicit specification of :args.
778 (setq args (cadr args)
779 done t))
780 ((keywordp (car args))
781 (setq args (cddr args)))
782 (t (setq done t))))
783 (when done
784 (widget-put widget :args args))
785 ;; Then Convert the widget.
786 (setq type widget)
787 (while type
788 (let ((convert-widget (plist-get (cdr type) :convert-widget)))
789 (if convert-widget
790 (setq widget (funcall convert-widget widget))))
791 (setq type (get (car type) 'widget-type)))
792 ;; Finally set the keyword args.
793 (while keys
794 (let ((next (nth 0 keys)))
795 (if (keywordp next)
796 (progn
797 (widget-put widget next (nth 1 keys))
798 (setq keys (nthcdr 2 keys)))
799 (setq keys nil))))
800 ;; Convert the :value to internal format.
801 (if (widget-member widget :value)
802 (widget-put widget
803 :value (widget-apply widget
804 :value-to-internal
805 (widget-get widget :value))))
806 ;; Return the newly create widget.
807 widget))
808
809 ;;;###autoload
810 (defun widget-insert (&rest args)
811 "Call `insert' with ARGS even if surrounding text is read only."
812 (let ((inhibit-read-only t)
813 (inhibit-modification-hooks t))
814 (apply 'insert args)))
815
816 (defun widget-convert-text (type from to
817 &optional button-from button-to
818 &rest args)
819 "Return a widget of type TYPE with endpoint FROM TO.
820 No text will be inserted to the buffer, instead the text between FROM
821 and TO will be used as the widgets end points. If optional arguments
822 BUTTON-FROM and BUTTON-TO are given, these will be used as the widgets
823 button end points.
824 Optional ARGS are extra keyword arguments for TYPE."
825 (let ((widget (apply 'widget-convert type :delete 'widget-leave-text args))
826 (from (copy-marker from))
827 (to (copy-marker to)))
828 (set-marker-insertion-type from t)
829 (set-marker-insertion-type to nil)
830 (widget-put widget :from from)
831 (widget-put widget :to to)
832 (when button-from
833 (widget-specify-button widget button-from button-to))
834 widget))
835
836 (defun widget-convert-button (type from to &rest args)
837 "Return a widget of type TYPE with endpoint FROM TO.
838 Optional ARGS are extra keyword arguments for TYPE.
839 No text will be inserted to the buffer, instead the text between FROM
840 and TO will be used as the widgets end points, as well as the widgets
841 button end points."
842 (apply 'widget-convert-text type from to from to args))
843
844 (defun widget-leave-text (widget)
845 "Remove markers and overlays from WIDGET and its children."
846 (let ((button (widget-get widget :button-overlay))
847 (sample (widget-get widget :sample-overlay))
848 (doc (widget-get widget :doc-overlay))
849 (field (widget-get widget :field-overlay)))
850 (set-marker (widget-get widget :from) nil)
851 (set-marker (widget-get widget :to) nil)
852 (when button
853 (delete-overlay button))
854 (when sample
855 (delete-overlay sample))
856 (when doc
857 (delete-overlay doc))
858 (when field
859 (delete-overlay field))
860 (mapc 'widget-leave-text (widget-get widget :children))))
861
862 ;;; Keymap and Commands.
863
864 ;; This alias exists only so that one can choose in doc-strings (e.g.
865 ;; Custom-mode) which key-binding of widget-keymap one wants to refer to.
866 ;; http://lists.gnu.org/archive/html/emacs-devel/2008-11/msg00480.html
867 (define-obsolete-function-alias 'advertised-widget-backward
868 'widget-backward "23.2")
869
870 ;;;###autoload
871 (defvar widget-keymap
872 (let ((map (make-sparse-keymap)))
873 (define-key map "\t" 'widget-forward)
874 (define-key map "\e\t" 'widget-backward)
875 (define-key map [(shift tab)] 'widget-backward)
876 (put 'widget-backward :advertised-binding [(shift tab)])
877 (define-key map [backtab] 'widget-backward)
878 (define-key map [down-mouse-2] 'widget-button-click)
879 (define-key map [down-mouse-1] 'widget-button-click)
880 ;; The following definition needs to avoid using escape sequences that
881 ;; might get converted to ^M when building loaddefs.el
882 (define-key map [(control ?m)] 'widget-button-press)
883 map)
884 "Keymap containing useful binding for buffers containing widgets.
885 Recommended as a parent keymap for modes using widgets.
886 Note that such modes will need to require wid-edit.")
887
888 (defvar widget-global-map global-map
889 "Keymap used for events a widget does not handle itself.")
890 (make-variable-buffer-local 'widget-global-map)
891
892 (defvar widget-field-keymap
893 (let ((map (copy-keymap widget-keymap)))
894 (define-key map "\C-k" 'widget-kill-line)
895 (define-key map "\M-\t" 'widget-complete)
896 (define-key map "\C-m" 'widget-field-activate)
897 ;; Since the widget code uses a `field' property to identify fields,
898 ;; ordinary beginning-of-line does the right thing.
899 ;; (define-key map "\C-a" 'widget-beginning-of-line)
900 (define-key map "\C-e" 'widget-end-of-line)
901 map)
902 "Keymap used inside an editable field.")
903
904 (defvar widget-text-keymap
905 (let ((map (copy-keymap widget-keymap)))
906 ;; Since the widget code uses a `field' property to identify fields,
907 ;; ordinary beginning-of-line does the right thing.
908 ;; (define-key map "\C-a" 'widget-beginning-of-line)
909 (define-key map "\C-e" 'widget-end-of-line)
910 map)
911 "Keymap used inside a text field.")
912
913 (defun widget-field-activate (pos &optional event)
914 "Invoke the editable field at point."
915 (interactive "@d")
916 (let ((field (widget-field-at pos)))
917 (if field
918 (widget-apply-action field event)
919 (call-interactively
920 (lookup-key widget-global-map (this-command-keys))))))
921
922 (defface widget-button-pressed
923 '((((min-colors 88) (class color))
924 (:foreground "red1"))
925 (((class color))
926 (:foreground "red"))
927 (t
928 (:weight bold :underline t)))
929 "Face used for pressed buttons."
930 :group 'widget-faces)
931 (define-obsolete-face-alias 'widget-button-pressed-face
932 'widget-button-pressed "22.1")
933
934 (defvar widget-button-click-moves-point nil
935 "If non-nil, `widget-button-click' moves point to a button after invoking it.
936 If nil, point returns to its original position after invoking a button.")
937
938 (defun widget-button-click (event)
939 "Invoke the button that the mouse is pointing at."
940 (interactive "e")
941 (if (widget-event-point event)
942 (let* ((oevent event)
943 (mouse-1 (memq (event-basic-type event) '(mouse-1 down-mouse-1)))
944 (pos (widget-event-point event))
945 (start (event-start event))
946 (button (get-char-property
947 pos 'button (and (windowp (posn-window start))
948 (window-buffer (posn-window start)))))
949 newpoint)
950 (when (or (null button)
951 (catch 'button-press-cancelled
952 ;; Mouse click on a widget button. Do the following
953 ;; in a save-excursion so that the click on the button
954 ;; doesn't change point.
955 (save-selected-window
956 (select-window (posn-window (event-start event)))
957 (save-excursion
958 (goto-char (posn-point (event-start event)))
959 (let* ((overlay (widget-get button :button-overlay))
960 (pressed-face (or (widget-get button :pressed-face)
961 widget-button-pressed-face))
962 (face (overlay-get overlay 'face))
963 (mouse-face (overlay-get overlay 'mouse-face)))
964 (unwind-protect
965 ;; Read events, including mouse-movement
966 ;; events, waiting for a release event. If we
967 ;; began with a mouse-1 event and receive a
968 ;; movement event, that means the user wants
969 ;; to perform drag-selection, so cancel the
970 ;; button press and do the default mouse-1
971 ;; action. For mouse-2, just highlight/
972 ;; unhighlight the button the mouse was
973 ;; initially on when we move over it.
974 (save-excursion
975 (when face ; avoid changing around image
976 (overlay-put overlay 'face pressed-face)
977 (overlay-put overlay 'mouse-face pressed-face))
978 (unless (widget-apply button :mouse-down-action event)
979 (let ((track-mouse t))
980 (while (not (widget-button-release-event-p event))
981 (setq event (read-event))
982 (when (and mouse-1 (mouse-movement-p event))
983 (push event unread-command-events)
984 (setq event oevent)
985 (throw 'button-press-cancelled t))
986 (unless (or (integerp event)
987 (memq (car event) '(switch-frame select-window))
988 (eq (car event) 'scroll-bar-movement))
989 (setq pos (widget-event-point event))
990 (if (and pos
991 (eq (get-char-property pos 'button)
992 button))
993 (when face
994 (overlay-put overlay 'face pressed-face)
995 (overlay-put overlay 'mouse-face pressed-face))
996 (overlay-put overlay 'face face)
997 (overlay-put overlay 'mouse-face mouse-face))))))
998
999 ;; When mouse is released over the button, run
1000 ;; its action function.
1001 (when (and pos (eq (get-char-property pos 'button) button))
1002 (goto-char pos)
1003 (widget-apply-action button event)
1004 (if widget-button-click-moves-point
1005 (setq newpoint (point)))))
1006 (overlay-put overlay 'face face)
1007 (overlay-put overlay 'mouse-face mouse-face))))
1008
1009 (if newpoint (goto-char newpoint))
1010 ;; This loses if the widget action switches windows. -- cyd
1011 ;; (unless (pos-visible-in-window-p (widget-event-point event))
1012 ;; (mouse-set-point event)
1013 ;; (beginning-of-line)
1014 ;; (recenter))
1015 )
1016 nil))
1017 (let ((up t) command)
1018 ;; Mouse click not on a widget button. Find the global
1019 ;; command to run, and check whether it is bound to an
1020 ;; up event.
1021 (if mouse-1
1022 (cond ((setq command ;down event
1023 (lookup-key widget-global-map [down-mouse-1]))
1024 (setq up nil))
1025 ((setq command ;up event
1026 (lookup-key widget-global-map [mouse-1]))))
1027 (cond ((setq command ;down event
1028 (lookup-key widget-global-map [down-mouse-2]))
1029 (setq up nil))
1030 ((setq command ;up event
1031 (lookup-key widget-global-map [mouse-2])))))
1032 (when up
1033 ;; Don't execute up events twice.
1034 (while (not (widget-button-release-event-p event))
1035 (setq event (read-event))))
1036 (when command
1037 (call-interactively command)))))
1038 (message "You clicked somewhere weird.")))
1039
1040 (defun widget-button-press (pos &optional event)
1041 "Invoke button at POS."
1042 (interactive "@d")
1043 (let ((button (get-char-property pos 'button)))
1044 (if button
1045 (widget-apply-action button event)
1046 (let ((command (lookup-key widget-global-map (this-command-keys))))
1047 (when (commandp command)
1048 (call-interactively command))))))
1049
1050 (defun widget-tabable-at (&optional pos)
1051 "Return the tabable widget at POS, or nil.
1052 POS defaults to the value of (point)."
1053 (let ((widget (widget-at pos)))
1054 (if widget
1055 (let ((order (widget-get widget :tab-order)))
1056 (if order
1057 (if (>= order 0)
1058 widget)
1059 widget)))))
1060
1061 (defvar widget-use-overlay-change t
1062 "If non-nil, use overlay change functions to tab around in the buffer.
1063 This is much faster.")
1064
1065 (defun widget-move (arg)
1066 "Move point to the ARG next field or button.
1067 ARG may be negative to move backward."
1068 (or (bobp) (> arg 0) (backward-char))
1069 (let ((wrapped 0)
1070 (number arg)
1071 (old (widget-tabable-at)))
1072 ;; Forward.
1073 (while (> arg 0)
1074 (cond ((eobp)
1075 (goto-char (point-min))
1076 (setq wrapped (1+ wrapped)))
1077 (widget-use-overlay-change
1078 (goto-char (next-overlay-change (point))))
1079 (t
1080 (forward-char 1)))
1081 (and (= wrapped 2)
1082 (eq arg number)
1083 (error "No buttons or fields found"))
1084 (let ((new (widget-tabable-at)))
1085 (when new
1086 (unless (eq new old)
1087 (setq arg (1- arg))
1088 (setq old new)))))
1089 ;; Backward.
1090 (while (< arg 0)
1091 (cond ((bobp)
1092 (goto-char (point-max))
1093 (setq wrapped (1+ wrapped)))
1094 (widget-use-overlay-change
1095 (goto-char (previous-overlay-change (point))))
1096 (t
1097 (backward-char 1)))
1098 (and (= wrapped 2)
1099 (eq arg number)
1100 (error "No buttons or fields found"))
1101 (let ((new (widget-tabable-at)))
1102 (when new
1103 (unless (eq new old)
1104 (setq arg (1+ arg))))))
1105 (let ((new (widget-tabable-at)))
1106 (while (eq (widget-tabable-at) new)
1107 (backward-char)))
1108 (forward-char))
1109 (widget-echo-help (point))
1110 (run-hooks 'widget-move-hook))
1111
1112 (defun widget-forward (arg)
1113 "Move point to the next field or button.
1114 With optional ARG, move across that many fields."
1115 (interactive "p")
1116 (run-hooks 'widget-forward-hook)
1117 (widget-move arg))
1118
1119 (defun widget-backward (arg)
1120 "Move point to the previous field or button.
1121 With optional ARG, move across that many fields."
1122 (interactive "p")
1123 (run-hooks 'widget-backward-hook)
1124 (widget-move (- arg)))
1125
1126 ;; Since the widget code uses a `field' property to identify fields,
1127 ;; ordinary beginning-of-line does the right thing.
1128 (defalias 'widget-beginning-of-line 'beginning-of-line)
1129
1130 (defun widget-end-of-line ()
1131 "Go to end of field or end of line, whichever is first.
1132 Trailing spaces at the end of padded fields are not considered part of
1133 the field."
1134 (interactive)
1135 ;; Ordinary end-of-line does the right thing, because we're inside
1136 ;; text with a `field' property.
1137 (end-of-line)
1138 (unless (eolp)
1139 ;; ... except that we want to ignore trailing spaces in fields that
1140 ;; aren't terminated by a newline, because they are used as padding,
1141 ;; and ignored when extracting the entered value of the field.
1142 (skip-chars-backward " " (field-beginning (1- (point))))))
1143
1144 (defun widget-kill-line ()
1145 "Kill to end of field or end of line, whichever is first."
1146 (interactive)
1147 (let* ((field (widget-field-find (point)))
1148 (end (and field (widget-field-end field))))
1149 (if (and field (> (line-beginning-position 2) end))
1150 (kill-region (point) end)
1151 (call-interactively 'kill-line))))
1152
1153 (defun widget-narrow-to-field ()
1154 "Narrow to field."
1155 (interactive)
1156 (let ((field (widget-field-find (point))))
1157 (if field
1158 (narrow-to-region (line-beginning-position) (line-end-position)))))
1159
1160 ;; This used to say:
1161 ;; "When not inside a field, move to the previous button or field."
1162 ;; but AFAICS, it has always just thrown an error.
1163 (defun widget-complete ()
1164 "Complete content of editable field from point.
1165 When not inside a field, signal an error."
1166 (interactive)
1167 (let ((data (widget-completions-at-point)))
1168 (cond
1169 ((functionp data) (funcall data))
1170 ((consp data)
1171 (let ((completion-extra-properties (nth 3 data)))
1172 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
1173 (plist-get completion-extra-properties
1174 :predicate))))
1175 (t
1176 (error "Not in an editable field")))))
1177 ;; We may want to use widget completion in buffers where the major mode
1178 ;; hasn't added widget-completions-at-point to completion-at-point-functions,
1179 ;; so it's not really obsolete (yet).
1180 ;; (make-obsolete 'widget-complete 'completion-at-point "24.1")
1181
1182 (defun widget-completions-at-point ()
1183 (let ((field (widget-field-find (point))))
1184 (when field
1185 (widget-apply field :completions-function))))
1186
1187 ;;; Setting up the buffer.
1188
1189 (defvar widget-field-new nil
1190 "List of all newly created editable fields in the buffer.")
1191 (make-variable-buffer-local 'widget-field-new)
1192
1193 (defvar widget-field-list nil
1194 "List of all editable fields in the buffer.")
1195 (make-variable-buffer-local 'widget-field-list)
1196
1197 (defun widget-at (&optional pos)
1198 "The button or field at POS (default, point)."
1199 (or (get-char-property (or pos (point)) 'button)
1200 (widget-field-at pos)))
1201
1202 ;;;###autoload
1203 (defun widget-setup ()
1204 "Setup current buffer so editing string widgets works."
1205 (let ((inhibit-read-only t)
1206 (inhibit-modification-hooks t)
1207 field)
1208 (while widget-field-new
1209 (setq field (car widget-field-new)
1210 widget-field-new (cdr widget-field-new)
1211 widget-field-list (cons field widget-field-list))
1212 (let ((from (car (widget-get field :field-overlay)))
1213 (to (cdr (widget-get field :field-overlay))))
1214 (widget-specify-field field
1215 (marker-position from) (marker-position to))
1216 (set-marker from nil)
1217 (set-marker to nil))))
1218 (widget-clear-undo)
1219 (widget-add-change))
1220
1221 (defvar widget-field-last nil)
1222 ;; Last field containing point.
1223 (make-variable-buffer-local 'widget-field-last)
1224
1225 (defvar widget-field-was nil)
1226 ;; The widget data before the change.
1227 (make-variable-buffer-local 'widget-field-was)
1228
1229 (defun widget-field-at (pos)
1230 "Return the widget field at POS, or nil if none."
1231 (let ((field (get-char-property (or pos (point)) 'field)))
1232 (if (eq field 'boundary)
1233 (get-char-property (or pos (point)) 'real-field)
1234 field)))
1235
1236 (defun widget-field-buffer (widget)
1237 "Return the buffer of WIDGET's editing field."
1238 (let ((overlay (widget-get widget :field-overlay)))
1239 (cond ((overlayp overlay)
1240 (overlay-buffer overlay))
1241 ((consp overlay)
1242 (marker-buffer (car overlay))))))
1243
1244 (defun widget-field-start (widget)
1245 "Return the start of WIDGET's editing field."
1246 (let ((overlay (widget-get widget :field-overlay)))
1247 (if (overlayp overlay)
1248 (overlay-start overlay)
1249 (car overlay))))
1250
1251 (defun widget-field-end (widget)
1252 "Return the end of WIDGET's editing field."
1253 (let ((overlay (widget-get widget :field-overlay)))
1254 ;; Don't subtract one if local-map works at the end of the overlay,
1255 ;; or if a special `boundary' field has been added after the widget
1256 ;; field.
1257 (if (overlayp overlay)
1258 ;; Don't proceed if overlay has been removed from buffer.
1259 (when (overlay-buffer overlay)
1260 (if (and (not (eq (with-current-buffer
1261 (widget-field-buffer widget)
1262 (save-restriction
1263 ;; `widget-narrow-to-field' can be
1264 ;; active when this function is called
1265 ;; from an change-functions hook. So
1266 ;; temporarily remove field narrowing
1267 ;; before to call `get-char-property'.
1268 (widen)
1269 (get-char-property (overlay-end overlay)
1270 'field)))
1271 'boundary))
1272 (or widget-field-add-space
1273 (null (widget-get widget :size))))
1274 (1- (overlay-end overlay))
1275 (overlay-end overlay)))
1276 (cdr overlay))))
1277
1278 (defun widget-field-text-end (widget)
1279 (let ((to (widget-field-end widget))
1280 (size (widget-get widget :size)))
1281 (if (or (null size) (zerop size))
1282 to
1283 (let ((from (widget-field-start widget)))
1284 (if (and from to)
1285 (with-current-buffer (widget-field-buffer widget)
1286 (while (and (> to from)
1287 (eq (char-after (1- to)) ?\s))
1288 (setq to (1- to)))
1289 to))))))
1290
1291 (defun widget-field-find (pos)
1292 "Return the field at POS.
1293 Unlike (get-char-property POS 'field), this works with empty fields too."
1294 (let ((fields widget-field-list)
1295 field found)
1296 (while fields
1297 (setq field (car fields)
1298 fields (cdr fields))
1299 (when (and (<= (widget-field-start field) pos)
1300 (<= pos (widget-field-end field)))
1301 (when found
1302 (error "Overlapping fields"))
1303 (setq found field)))
1304 found))
1305
1306 (defun widget-before-change (from to)
1307 ;; This is how, for example, a variable changes its state to `modified'.
1308 ;; when it is being edited.
1309 (unless inhibit-read-only
1310 (let ((from-field (widget-field-find from))
1311 (to-field (widget-field-find to)))
1312 (cond ((not (eq from-field to-field))
1313 (add-hook 'post-command-hook 'widget-add-change nil t)
1314 (signal 'text-read-only
1315 '("Change should be restricted to a single field")))
1316 ((null from-field)
1317 (add-hook 'post-command-hook 'widget-add-change nil t)
1318 (signal 'text-read-only
1319 '("Attempt to change text outside editable field")))
1320 (widget-field-use-before-change
1321 (widget-apply from-field :notify from-field))))))
1322
1323 (defun widget-add-change ()
1324 (remove-hook 'post-command-hook 'widget-add-change t)
1325 (add-hook 'before-change-functions 'widget-before-change nil t)
1326 (add-hook 'after-change-functions 'widget-after-change nil t))
1327
1328 (defun widget-after-change (from to _old)
1329 "Adjust field size and text properties."
1330 (let ((field (widget-field-find from))
1331 (other (widget-field-find to)))
1332 (when field
1333 (unless (eq field other)
1334 (error "Change in different fields"))
1335 (let ((size (widget-get field :size)))
1336 (when size
1337 (let ((begin (widget-field-start field))
1338 (end (widget-field-end field)))
1339 (cond ((< (- end begin) size)
1340 ;; Field too small.
1341 (save-excursion
1342 (goto-char end)
1343 (insert-char ?\s (- (+ begin size) end))))
1344 ((> (- end begin) size)
1345 ;; Field too large and
1346 (if (or (< (point) (+ begin size))
1347 (> (point) end))
1348 ;; Point is outside extra space.
1349 (setq begin (+ begin size))
1350 ;; Point is within the extra space.
1351 (setq begin (point)))
1352 (save-excursion
1353 (goto-char end)
1354 (while (and (eq (preceding-char) ?\s)
1355 (> (point) begin))
1356 (delete-char -1)))))))
1357 (widget-specify-secret field))
1358 (widget-apply field :notify field))))
1359
1360 ;;; Widget Functions
1361 ;;
1362 ;; These functions are used in the definition of multiple widgets.
1363
1364 (defun widget-parent-action (widget &optional event)
1365 "Tell :parent of WIDGET to handle the :action.
1366 Optional EVENT is the event that triggered the action."
1367 (widget-apply (widget-get widget :parent) :action event))
1368
1369 (defun widget-children-value-delete (widget)
1370 "Delete all :children and :buttons in WIDGET."
1371 (mapc 'widget-delete (widget-get widget :children))
1372 (widget-put widget :children nil)
1373 (mapc 'widget-delete (widget-get widget :buttons))
1374 (widget-put widget :buttons nil))
1375
1376 (defun widget-children-validate (widget)
1377 "All the :children must be valid."
1378 (let ((children (widget-get widget :children))
1379 child found)
1380 (while (and children (not found))
1381 (setq child (car children)
1382 children (cdr children)
1383 found (widget-apply child :validate)))
1384 found))
1385
1386 (defun widget-child-value-get (widget)
1387 "Get the value of the first member of :children in WIDGET."
1388 (widget-value (car (widget-get widget :children))))
1389
1390 (defun widget-child-value-inline (widget)
1391 "Get the inline value of the first member of :children in WIDGET."
1392 (widget-apply (car (widget-get widget :children)) :value-inline))
1393
1394 (defun widget-child-validate (widget)
1395 "The result of validating the first member of :children in WIDGET."
1396 (widget-apply (car (widget-get widget :children)) :validate))
1397
1398 (defun widget-type-value-create (widget)
1399 "Convert and instantiate the value of the :type attribute of WIDGET.
1400 Store the newly created widget in the :children attribute.
1401
1402 The value of the :type attribute should be an unconverted widget type."
1403 (let ((value (widget-get widget :value))
1404 (type (widget-get widget :type)))
1405 (widget-put widget :children
1406 (list (widget-create-child-value widget
1407 (widget-convert type)
1408 value)))))
1409
1410 (defun widget-type-default-get (widget)
1411 "Get default value from the :type attribute of WIDGET.
1412
1413 The value of the :type attribute should be an unconverted widget type."
1414 (widget-default-get (widget-convert (widget-get widget :type))))
1415
1416 (defun widget-type-match (widget value)
1417 "Non-nil if the :type value of WIDGET matches VALUE.
1418
1419 The value of the :type attribute should be an unconverted widget type."
1420 (widget-apply (widget-convert (widget-get widget :type)) :match value))
1421
1422 (defun widget-types-copy (widget)
1423 "Copy :args as widget types in WIDGET."
1424 (widget-put widget :args (mapcar 'widget-copy (widget-get widget :args)))
1425 widget)
1426
1427 ;; Made defsubst to speed up face editor creation.
1428 (defsubst widget-types-convert-widget (widget)
1429 "Convert :args as widget types in WIDGET."
1430 (widget-put widget :args (mapcar 'widget-convert (widget-get widget :args)))
1431 widget)
1432
1433 (defun widget-value-convert-widget (widget)
1434 "Initialize :value from :args in WIDGET."
1435 (let ((args (widget-get widget :args)))
1436 (when args
1437 (widget-put widget :value (car args))
1438 ;; Don't convert :value here, as this is done in `widget-convert'.
1439 ;; (widget-put widget :value (widget-apply widget
1440 ;; :value-to-internal (car args)))
1441 (widget-put widget :args nil)))
1442 widget)
1443
1444 (defun widget-value-value-get (widget)
1445 "Return the :value property of WIDGET."
1446 (widget-get widget :value))
1447
1448 ;;; The `default' Widget.
1449
1450 (define-widget 'default nil
1451 "Basic widget other widgets are derived from."
1452 :value-to-internal (lambda (_widget value) value)
1453 :value-to-external (lambda (_widget value) value)
1454 :button-prefix 'widget-button-prefix
1455 :button-suffix 'widget-button-suffix
1456 :completions-function #'widget-default-completions
1457 :create 'widget-default-create
1458 :indent nil
1459 :offset 0
1460 :format-handler 'widget-default-format-handler
1461 :button-face-get 'widget-default-button-face-get
1462 :mouse-face-get 'widget-default-mouse-face-get
1463 :sample-face-get 'widget-default-sample-face-get
1464 :delete 'widget-default-delete
1465 :copy 'identity
1466 :value-set 'widget-default-value-set
1467 :value-inline 'widget-default-value-inline
1468 :value-delete 'ignore
1469 :default-get 'widget-default-default-get
1470 :menu-tag-get 'widget-default-menu-tag-get
1471 :validate #'ignore
1472 :active 'widget-default-active
1473 :activate 'widget-specify-active
1474 :deactivate 'widget-default-deactivate
1475 :mouse-down-action #'ignore
1476 :action 'widget-default-action
1477 :notify 'widget-default-notify
1478 :prompt-value 'widget-default-prompt-value)
1479
1480 (defvar widget--completing-widget)
1481
1482 (defun widget-default-completions (widget)
1483 "Return completion data, like `completion-at-point-functions' would."
1484 (let ((completions (widget-get widget :completions)))
1485 (if completions
1486 (list (widget-field-start widget)
1487 (max (point) (widget-field-text-end widget))
1488 completions)
1489 (if (widget-get widget :complete)
1490 (lambda () (widget-apply widget :complete))
1491 (if (widget-get widget :complete-function)
1492 (lambda ()
1493 (let ((widget--completing-widget widget))
1494 (call-interactively
1495 (widget-get widget :complete-function)))))))))
1496
1497 (defun widget-default-create (widget)
1498 "Create WIDGET at point in the current buffer."
1499 (widget-specify-insert
1500 (let ((from (point))
1501 button-begin button-end
1502 sample-begin sample-end
1503 doc-begin doc-end
1504 value-pos)
1505 (insert (widget-get widget :format))
1506 (goto-char from)
1507 ;; Parse escapes in format.
1508 (while (re-search-forward "%\\(.\\)" nil t)
1509 (let ((escape (char-after (match-beginning 1))))
1510 (delete-char -2)
1511 (cond ((eq escape ?%)
1512 (insert ?%))
1513 ((eq escape ?\[)
1514 (setq button-begin (point))
1515 (insert (widget-get-indirect widget :button-prefix)))
1516 ((eq escape ?\])
1517 (insert (widget-get-indirect widget :button-suffix))
1518 (setq button-end (point)))
1519 ((eq escape ?\{)
1520 (setq sample-begin (point)))
1521 ((eq escape ?\})
1522 (setq sample-end (point)))
1523 ((eq escape ?n)
1524 (when (widget-get widget :indent)
1525 (insert ?\n)
1526 (insert-char ?\s (widget-get widget :indent))))
1527 ((eq escape ?t)
1528 (let ((image (widget-get widget :tag-glyph))
1529 (tag (widget-get widget :tag)))
1530 (cond (image
1531 (widget-image-insert widget (or tag "image") image))
1532 (tag
1533 (insert tag))
1534 (t
1535 (princ (widget-get widget :value)
1536 (current-buffer))))))
1537 ((eq escape ?d)
1538 (let ((doc (widget-get widget :doc)))
1539 (when doc
1540 (setq doc-begin (point))
1541 (insert doc)
1542 (while (eq (preceding-char) ?\n)
1543 (delete-char -1))
1544 (insert ?\n)
1545 (setq doc-end (point)))))
1546 ((eq escape ?h)
1547 (widget-add-documentation-string-button widget))
1548 ((eq escape ?v)
1549 (if (and button-begin (not button-end))
1550 (widget-apply widget :value-create)
1551 (setq value-pos (point))))
1552 (t
1553 (widget-apply widget :format-handler escape)))))
1554 ;; Specify button, sample, and doc, and insert value.
1555 (and button-begin button-end
1556 (widget-specify-button widget button-begin button-end))
1557 (and sample-begin sample-end
1558 (widget-specify-sample widget sample-begin sample-end))
1559 (and doc-begin doc-end
1560 (widget-specify-doc widget doc-begin doc-end))
1561 (when value-pos
1562 (goto-char value-pos)
1563 (widget-apply widget :value-create)))
1564 (let ((from (point-min-marker))
1565 (to (point-max-marker)))
1566 (set-marker-insertion-type from t)
1567 (set-marker-insertion-type to nil)
1568 (widget-put widget :from from)
1569 (widget-put widget :to to)))
1570 (widget-clear-undo))
1571
1572 (defun widget-default-format-handler (_widget escape)
1573 (error "Unknown escape `%c'" escape))
1574
1575 (defun widget-default-button-face-get (widget)
1576 ;; Use :button-face or widget-button-face
1577 (or (widget-get widget :button-face)
1578 (let ((parent (widget-get widget :parent)))
1579 (if parent
1580 (widget-apply parent :button-face-get)
1581 widget-button-face))))
1582
1583 (defun widget-default-mouse-face-get (widget)
1584 ;; Use :mouse-face or widget-mouse-face
1585 (or (widget-get widget :mouse-face)
1586 (let ((parent (widget-get widget :parent)))
1587 (if parent
1588 (widget-apply parent :mouse-face-get)
1589 widget-mouse-face))))
1590
1591 (defun widget-default-sample-face-get (widget)
1592 ;; Use :sample-face.
1593 (widget-get widget :sample-face))
1594
1595 (defun widget-default-delete (widget)
1596 "Remove widget from the buffer."
1597 (let ((from (widget-get widget :from))
1598 (to (widget-get widget :to))
1599 (inactive-overlay (widget-get widget :inactive))
1600 (button-overlay (widget-get widget :button-overlay))
1601 (sample-overlay (widget-get widget :sample-overlay))
1602 (doc-overlay (widget-get widget :doc-overlay))
1603 (inhibit-modification-hooks t)
1604 (inhibit-read-only t))
1605 (widget-apply widget :value-delete)
1606 (widget-children-value-delete widget)
1607 (when inactive-overlay
1608 (delete-overlay inactive-overlay))
1609 (when button-overlay
1610 (delete-overlay button-overlay))
1611 (when sample-overlay
1612 (delete-overlay sample-overlay))
1613 (when doc-overlay
1614 (delete-overlay doc-overlay))
1615 (when (< from to)
1616 ;; Kludge: this doesn't need to be true for empty formats.
1617 (delete-region from to))
1618 (set-marker from nil)
1619 (set-marker to nil))
1620 (widget-clear-undo))
1621
1622 (defun widget-default-value-set (widget value)
1623 "Recreate widget with new value."
1624 (let* ((old-pos (point))
1625 (from (copy-marker (widget-get widget :from)))
1626 (to (copy-marker (widget-get widget :to)))
1627 (offset (if (and (<= from old-pos) (<= old-pos to))
1628 (if (>= old-pos (1- to))
1629 (- old-pos to 1)
1630 (- old-pos from)))))
1631 ;;??? Bug: this ought to insert the new value before deleting the old one,
1632 ;; so that markers on either side of the value automatically
1633 ;; stay on the same side. -- rms.
1634 (save-excursion
1635 (goto-char (widget-get widget :from))
1636 (widget-apply widget :delete)
1637 (widget-put widget :value value)
1638 (widget-apply widget :create))
1639 (if offset
1640 (if (< offset 0)
1641 (goto-char (+ (widget-get widget :to) offset 1))
1642 (goto-char (min (+ from offset) (1- (widget-get widget :to))))))))
1643
1644 (defun widget-default-value-inline (widget)
1645 "Wrap value in a list unless it is inline."
1646 (if (widget-get widget :inline)
1647 (widget-value widget)
1648 (list (widget-value widget))))
1649
1650 (defun widget-default-default-get (widget)
1651 "Get `:value'."
1652 (widget-get widget :value))
1653
1654 (defun widget-default-menu-tag-get (widget)
1655 "Use tag or value for menus."
1656 (or (widget-get widget :menu-tag)
1657 (widget-get widget :tag)
1658 (widget-princ-to-string (widget-get widget :value))))
1659
1660 (defun widget-default-active (widget)
1661 "Return t if this widget is active (user modifiable)."
1662 (or (widget-get widget :always-active)
1663 (and (not (widget-get widget :inactive))
1664 (let ((parent (widget-get widget :parent)))
1665 (or (null parent)
1666 (widget-apply parent :active))))))
1667
1668 (defun widget-default-deactivate (widget)
1669 "Make WIDGET inactive for user modifications."
1670 (widget-specify-inactive widget
1671 (widget-get widget :from)
1672 (widget-get widget :to)))
1673
1674 (defun widget-default-action (widget &optional event)
1675 "Notify the parent when a widget changes."
1676 (let ((parent (widget-get widget :parent)))
1677 (when parent
1678 (widget-apply parent :notify widget event))))
1679
1680 (defun widget-default-notify (widget _child &optional event)
1681 "Pass notification to parent."
1682 (widget-default-action widget event))
1683
1684 (defun widget-default-prompt-value (_widget prompt _value _unbound)
1685 "Read an arbitrary value."
1686 (eval-minibuffer prompt))
1687
1688 (defun widget-docstring (widget)
1689 "Return the documentation string specified by WIDGET, or nil if none.
1690 If WIDGET has a `:doc' property, that specifies the documentation string.
1691 Otherwise, try the `:documentation-property' property. If this
1692 is a function, call it with the widget's value as an argument; if
1693 it is a symbol, use this symbol together with the widget's value
1694 as the argument to `documentation-property'."
1695 (let ((doc (or (widget-get widget :doc)
1696 (let ((doc-prop (widget-get widget :documentation-property))
1697 (value (widget-get widget :value)))
1698 (cond ((functionp doc-prop)
1699 (funcall doc-prop value))
1700 ((symbolp doc-prop)
1701 (documentation-property value doc-prop)))))))
1702 (when (and (stringp doc) (> (length doc) 0))
1703 ;; Remove any redundant `*' in the beginning.
1704 (when (eq (aref doc 0) ?*)
1705 (setq doc (substring doc 1)))
1706 ;; Remove trailing newlines.
1707 (when (string-match "\n+\\'" doc)
1708 (setq doc (substring doc 0 (match-beginning 0))))
1709 doc)))
1710
1711 ;;; The `item' Widget.
1712
1713 (define-widget 'item 'default
1714 "Constant items for inclusion in other widgets."
1715 :convert-widget 'widget-value-convert-widget
1716 :value-create 'widget-item-value-create
1717 :value-delete 'ignore
1718 :value-get 'widget-value-value-get
1719 :match 'widget-item-match
1720 :match-inline 'widget-item-match-inline
1721 :action 'widget-item-action
1722 :format "%t\n")
1723
1724 (defun widget-item-value-create (widget)
1725 "Insert the printed representation of the value."
1726 (princ (widget-get widget :value) (current-buffer)))
1727
1728 (defun widget-item-match (widget value)
1729 ;; Match if the value is the same.
1730 (equal (widget-get widget :value) value))
1731
1732 (defun widget-item-match-inline (widget vals)
1733 ;; Match if the value is the same.
1734 (let ((value (widget-get widget :value)))
1735 (and (listp value)
1736 (<= (length value) (length vals))
1737 (let ((head (widget-sublist vals 0 (length value))))
1738 (and (equal head value)
1739 (cons head (widget-sublist vals (length value))))))))
1740
1741 (defun widget-sublist (list start &optional end)
1742 "Return the sublist of LIST from START to END.
1743 If END is omitted, it defaults to the length of LIST."
1744 (if (> start 0) (setq list (nthcdr start list)))
1745 (if end
1746 (unless (<= end start)
1747 (setq list (copy-sequence list))
1748 (setcdr (nthcdr (- end start 1) list) nil)
1749 list)
1750 (copy-sequence list)))
1751
1752 (defun widget-item-action (widget &optional event)
1753 ;; Just notify itself.
1754 (widget-apply widget :notify widget event))
1755
1756 ;;; The `push-button' Widget.
1757
1758 ;; (defcustom widget-push-button-gui t
1759 ;; "If non-nil, use GUI push buttons when available."
1760 ;; :group 'widgets
1761 ;; :type 'boolean)
1762
1763 ;; Cache already created GUI objects.
1764 ;; (defvar widget-push-button-cache nil)
1765
1766 (defcustom widget-push-button-prefix "["
1767 "String used as prefix for buttons."
1768 :type 'string
1769 :group 'widget-button)
1770
1771 (defcustom widget-push-button-suffix "]"
1772 "String used as suffix for buttons."
1773 :type 'string
1774 :group 'widget-button)
1775
1776 (define-widget 'push-button 'item
1777 "A pushable button."
1778 :button-prefix ""
1779 :button-suffix ""
1780 :value-create 'widget-push-button-value-create
1781 :format "%[%v%]")
1782
1783 (defun widget-push-button-value-create (widget)
1784 "Insert text representing the `on' and `off' states."
1785 (let* ((tag (or (widget-get widget :tag)
1786 (widget-get widget :value)))
1787 (tag-glyph (widget-get widget :tag-glyph))
1788 (text (concat widget-push-button-prefix
1789 tag widget-push-button-suffix)))
1790 (if tag-glyph
1791 (widget-image-insert widget text tag-glyph)
1792 (insert text))))
1793
1794 ;; (defun widget-gui-action (widget)
1795 ;; "Apply :action for WIDGET."
1796 ;; (widget-apply-action widget (this-command-keys)))
1797
1798 ;;; The `link' Widget.
1799
1800 (defcustom widget-link-prefix "["
1801 "String used as prefix for links."
1802 :type 'string
1803 :group 'widget-button)
1804
1805 (defcustom widget-link-suffix "]"
1806 "String used as suffix for links."
1807 :type 'string
1808 :group 'widget-button)
1809
1810 (define-widget 'link 'item
1811 "An embedded link."
1812 :button-prefix 'widget-link-prefix
1813 :button-suffix 'widget-link-suffix
1814 :follow-link 'mouse-face
1815 :help-echo "Follow the link."
1816 :format "%[%t%]")
1817
1818 ;;; The `info-link' Widget.
1819
1820 (define-widget 'info-link 'link
1821 "A link to an info file."
1822 :action 'widget-info-link-action)
1823
1824 (defun widget-info-link-action (widget &optional _event)
1825 "Open the info node specified by WIDGET."
1826 (info (widget-value widget)))
1827
1828 ;;; The `url-link' Widget.
1829
1830 (define-widget 'url-link 'link
1831 "A link to an www page."
1832 :action 'widget-url-link-action)
1833
1834 (defun widget-url-link-action (widget &optional _event)
1835 "Open the URL specified by WIDGET."
1836 (browse-url (widget-value widget)))
1837
1838 ;;; The `function-link' Widget.
1839
1840 (define-widget 'function-link 'link
1841 "A link to an Emacs function."
1842 :action 'widget-function-link-action)
1843
1844 (defun widget-function-link-action (widget &optional _event)
1845 "Show the function specified by WIDGET."
1846 (describe-function (widget-value widget)))
1847
1848 ;;; The `variable-link' Widget.
1849
1850 (define-widget 'variable-link 'link
1851 "A link to an Emacs variable."
1852 :action 'widget-variable-link-action)
1853
1854 (defun widget-variable-link-action (widget &optional _event)
1855 "Show the variable specified by WIDGET."
1856 (describe-variable (widget-value widget)))
1857
1858 ;;; The `file-link' Widget.
1859
1860 (define-widget 'file-link 'link
1861 "A link to a file."
1862 :action 'widget-file-link-action)
1863
1864 (defun widget-file-link-action (widget &optional _event)
1865 "Find the file specified by WIDGET."
1866 (find-file (widget-value widget)))
1867
1868 ;;; The `emacs-library-link' Widget.
1869
1870 (define-widget 'emacs-library-link 'link
1871 "A link to an Emacs Lisp library file."
1872 :action 'widget-emacs-library-link-action)
1873
1874 (defun widget-emacs-library-link-action (widget &optional _event)
1875 "Find the Emacs library file specified by WIDGET."
1876 (find-file (locate-library (widget-value widget))))
1877
1878 ;;; The `emacs-commentary-link' Widget.
1879
1880 (define-widget 'emacs-commentary-link 'link
1881 "A link to Commentary in an Emacs Lisp library file."
1882 :action 'widget-emacs-commentary-link-action)
1883
1884 (defun widget-emacs-commentary-link-action (widget &optional _event)
1885 "Find the Commentary section of the Emacs file specified by WIDGET."
1886 (finder-commentary (widget-value widget)))
1887
1888 ;;; The `editable-field' Widget.
1889
1890 (define-widget 'editable-field 'default
1891 "An editable text field.
1892 Note: In an `editable-field' widget, the `%v' escape must be preceded
1893 by some other text in the `:format' string (if specified)."
1894 :convert-widget 'widget-value-convert-widget
1895 :keymap widget-field-keymap
1896 :format "%v"
1897 :help-echo "M-TAB: complete field; RET: enter value"
1898 :value ""
1899 :prompt-internal 'widget-field-prompt-internal
1900 :prompt-history 'widget-field-history
1901 :prompt-value 'widget-field-prompt-value
1902 :action 'widget-field-action
1903 :validate 'widget-field-validate
1904 :valid-regexp ""
1905 :error "Field's value doesn't match allowed forms"
1906 :value-create 'widget-field-value-create
1907 :value-set 'widget-field-value-set
1908 :value-delete 'widget-field-value-delete
1909 :value-get 'widget-field-value-get
1910 :match 'widget-field-match)
1911
1912 (defvar widget-field-history nil
1913 "History of field minibuffer edits.")
1914
1915 (defun widget-field-prompt-internal (_widget prompt initial history)
1916 "Read string for WIDGET prompting with PROMPT.
1917 INITIAL is the initial input and HISTORY is a symbol containing
1918 the earlier input."
1919 (read-string prompt initial history))
1920
1921 (defun widget-field-prompt-value (widget prompt value unbound)
1922 "Prompt for a string."
1923 (widget-apply widget
1924 :value-to-external
1925 (widget-apply widget
1926 :prompt-internal prompt
1927 (unless unbound
1928 (cons (widget-apply widget
1929 :value-to-internal value)
1930 0))
1931 (widget-get widget :prompt-history))))
1932
1933 (defvar widget-edit-functions nil)
1934
1935 (defun widget-field-action (widget &optional _event)
1936 "Move to next field."
1937 (widget-forward 1)
1938 (run-hook-with-args 'widget-edit-functions widget))
1939
1940 (defun widget-field-validate (widget)
1941 "Valid if the content matches `:valid-regexp'."
1942 (unless (string-match (widget-get widget :valid-regexp)
1943 (widget-apply widget :value-get))
1944 widget))
1945
1946 (defun widget-field-value-set (widget value)
1947 "Set an editable text field WIDGET to VALUE"
1948 (let ((from (widget-field-start widget))
1949 (to (widget-field-text-end widget))
1950 (buffer (widget-field-buffer widget)))
1951 (when (and from to (buffer-live-p buffer))
1952 (with-current-buffer buffer
1953 (goto-char from)
1954 (delete-char (- to from))
1955 (insert value)))))
1956
1957 (defun widget-field-value-create (widget)
1958 "Create an editable text field."
1959 (let ((size (widget-get widget :size))
1960 (value (widget-get widget :value))
1961 (from (point))
1962 ;; This is changed to a real overlay in `widget-setup'. We
1963 ;; need the end points to behave differently until
1964 ;; `widget-setup' is called.
1965 (overlay (cons (make-marker) (make-marker))))
1966 (widget-put widget :field-overlay overlay)
1967 (insert value)
1968 (and size
1969 (< (length value) size)
1970 (insert-char ?\s (- size (length value))))
1971 (unless (memq widget widget-field-list)
1972 (setq widget-field-new (cons widget widget-field-new)))
1973 (move-marker (cdr overlay) (point))
1974 (set-marker-insertion-type (cdr overlay) nil)
1975 (when (null size)
1976 (insert ?\n))
1977 (move-marker (car overlay) from)
1978 (set-marker-insertion-type (car overlay) t)))
1979
1980 (defun widget-field-value-delete (widget)
1981 "Remove the widget from the list of active editing fields."
1982 (setq widget-field-list (delq widget widget-field-list))
1983 (setq widget-field-new (delq widget widget-field-new))
1984 ;; These are nil if the :format string doesn't contain `%v'.
1985 (let ((overlay (widget-get widget :field-overlay)))
1986 (when (overlayp overlay)
1987 (delete-overlay overlay))))
1988
1989 (defun widget-field-value-get (widget &optional no-truncate)
1990 "Return current text in editing field.
1991 Normally, trailing spaces within the editing field are truncated.
1992 But if NO-TRUNCATE is non-nil, include them."
1993 (let ((from (widget-field-start widget))
1994 (to (if no-truncate
1995 (widget-field-end widget)
1996 (widget-field-text-end widget)))
1997 (buffer (widget-field-buffer widget))
1998 (secret (widget-get widget :secret))
1999 (old (current-buffer)))
2000 (if (and from to)
2001 (progn
2002 (set-buffer buffer)
2003 (let ((result (buffer-substring-no-properties from to)))
2004 (when secret
2005 (let ((index 0))
2006 (while (< (+ from index) to)
2007 (aset result index
2008 (get-char-property (+ from index) 'secret))
2009 (setq index (1+ index)))))
2010 (set-buffer old)
2011 result))
2012 (widget-get widget :value))))
2013
2014 (defun widget-field-match (_widget value)
2015 ;; Match any string.
2016 (stringp value))
2017
2018 ;;; The `text' Widget.
2019
2020 (define-widget 'text 'editable-field
2021 "A multiline text area."
2022 :keymap widget-text-keymap)
2023
2024 ;;; The `menu-choice' Widget.
2025
2026 (define-widget 'menu-choice 'default
2027 "A menu of options."
2028 :convert-widget 'widget-types-convert-widget
2029 :copy 'widget-types-copy
2030 :format "%[%t%]: %v"
2031 :case-fold t
2032 :tag "choice"
2033 :void '(item :format "invalid (%t)\n")
2034 :value-create 'widget-choice-value-create
2035 :value-get 'widget-child-value-get
2036 :value-inline 'widget-child-value-inline
2037 :default-get 'widget-choice-default-get
2038 :mouse-down-action 'widget-choice-mouse-down-action
2039 :action 'widget-choice-action
2040 :error "Make a choice"
2041 :validate 'widget-choice-validate
2042 :match 'widget-choice-match
2043 :match-inline 'widget-choice-match-inline)
2044
2045 (defun widget-choice-value-create (widget)
2046 "Insert the first choice that matches the value."
2047 (let ((value (widget-get widget :value))
2048 (args (widget-get widget :args))
2049 (explicit (widget-get widget :explicit-choice))
2050 current)
2051 (if explicit
2052 (progn
2053 ;; If the user specified the choice for this value,
2054 ;; respect that choice.
2055 (widget-put widget :children (list (widget-create-child-value
2056 widget explicit value)))
2057 (widget-put widget :choice explicit)
2058 (widget-put widget :explicit-choice nil))
2059 (while args
2060 (setq current (car args)
2061 args (cdr args))
2062 (when (widget-apply current :match value)
2063 (widget-put widget :children (list (widget-create-child-value
2064 widget current value)))
2065 (widget-put widget :choice current)
2066 (setq args nil
2067 current nil)))
2068 (when current
2069 (let ((void (widget-get widget :void)))
2070 (widget-put widget :children (list (widget-create-child-and-convert
2071 widget void :value value)))
2072 (widget-put widget :choice void))))))
2073
2074 (defun widget-choice-default-get (widget)
2075 ;; Get default for the first choice.
2076 (widget-default-get (car (widget-get widget :args))))
2077
2078 (defcustom widget-choice-toggle nil
2079 "If non-nil, a binary choice will just toggle between the values.
2080 Otherwise, the user will explicitly have to choose between the values
2081 when he invoked the menu."
2082 :type 'boolean
2083 :group 'widgets)
2084
2085 (defun widget-choice-mouse-down-action (widget &optional _event)
2086 ;; Return non-nil if we need a menu.
2087 (let ((args (widget-get widget :args))
2088 (old (widget-get widget :choice)))
2089 (cond ((not (display-popup-menus-p))
2090 ;; No place to pop up a menu.
2091 nil)
2092 ((< (length args) 2)
2093 ;; Empty or singleton list, just return the value.
2094 nil)
2095 ((> (length args) widget-menu-max-size)
2096 ;; Too long, prompt.
2097 nil)
2098 ((> (length args) 2)
2099 ;; Reasonable sized list, use menu.
2100 t)
2101 ((and widget-choice-toggle (memq old args))
2102 ;; We toggle.
2103 nil)
2104 (t
2105 ;; Ask which of the two.
2106 t))))
2107
2108 (defun widget-choice-action (widget &optional event)
2109 ;; Make a choice.
2110 (let ((args (widget-get widget :args))
2111 (old (widget-get widget :choice))
2112 (tag (widget-apply widget :menu-tag-get))
2113 (completion-ignore-case (widget-get widget :case-fold))
2114 this-explicit
2115 current choices)
2116 ;; Remember old value.
2117 (if (and old (not (widget-apply widget :validate)))
2118 (let* ((external (widget-value widget))
2119 (internal (widget-apply old :value-to-internal external)))
2120 (widget-put old :value internal)))
2121 ;; Find new choice.
2122 (setq current
2123 (cond ((= (length args) 0)
2124 nil)
2125 ((= (length args) 1)
2126 (nth 0 args))
2127 ((and widget-choice-toggle
2128 (= (length args) 2)
2129 (memq old args))
2130 (if (eq old (nth 0 args))
2131 (nth 1 args)
2132 (nth 0 args)))
2133 (t
2134 (while args
2135 (setq current (car args)
2136 args (cdr args))
2137 (setq choices
2138 (cons (cons (widget-apply current :menu-tag-get)
2139 current)
2140 choices)))
2141 (setq this-explicit t)
2142 (widget-choose tag (reverse choices) event))))
2143 (when current
2144 ;; If this was an explicit user choice, record the choice,
2145 ;; so that widget-choice-value-create will respect it.
2146 (when this-explicit
2147 (widget-put widget :explicit-choice current))
2148 (widget-value-set widget (widget-default-get current))
2149 (widget-setup)
2150 (widget-apply widget :notify widget event)))
2151 (run-hook-with-args 'widget-edit-functions widget))
2152
2153 (defun widget-choice-validate (widget)
2154 ;; Valid if we have made a valid choice.
2155 (if (eq (widget-get widget :void) (widget-get widget :choice))
2156 widget
2157 (widget-apply (car (widget-get widget :children)) :validate)))
2158
2159 (defun widget-choice-match (widget value)
2160 ;; Matches if one of the choices matches.
2161 (let ((args (widget-get widget :args))
2162 current found)
2163 (while (and args (not found))
2164 (setq current (car args)
2165 args (cdr args)
2166 found (widget-apply current :match value)))
2167 found))
2168
2169 (defun widget-choice-match-inline (widget vals)
2170 ;; Matches if one of the choices matches.
2171 (let ((args (widget-get widget :args))
2172 current found)
2173 (while (and args (null found))
2174 (setq current (car args)
2175 args (cdr args)
2176 found (widget-match-inline current vals)))
2177 found))
2178
2179 ;;; The `toggle' Widget.
2180
2181 (define-widget 'toggle 'item
2182 "Toggle between two states."
2183 :format "%[%v%]\n"
2184 :value-create 'widget-toggle-value-create
2185 :action 'widget-toggle-action
2186 :match (lambda (_widget _value) t)
2187 :on "on"
2188 :off "off")
2189
2190 (defun widget-toggle-value-create (widget)
2191 "Insert text representing the `on' and `off' states."
2192 (let* ((val (widget-value widget))
2193 (text (widget-get widget (if val :on :off)))
2194 (img (widget-image-find
2195 (widget-get widget (if val :on-glyph :off-glyph)))))
2196 (widget-image-insert widget (or text "")
2197 (if img
2198 (append img '(:ascent center))))))
2199
2200 (defun widget-toggle-action (widget &optional event)
2201 ;; Toggle value.
2202 (widget-value-set widget (not (widget-value widget)))
2203 (widget-apply widget :notify widget event)
2204 (run-hook-with-args 'widget-edit-functions widget))
2205
2206 ;;; The `checkbox' Widget.
2207
2208 (define-widget 'checkbox 'toggle
2209 "A checkbox toggle."
2210 :button-suffix ""
2211 :button-prefix ""
2212 :format "%[%v%]"
2213 :on "[X]"
2214 ;; We could probably do the same job as the images using single
2215 ;; space characters in a boxed face with a stretch specification to
2216 ;; make them square.
2217 :on-glyph "checked"
2218 :off "[ ]"
2219 :off-glyph "unchecked"
2220 :help-echo "Toggle this item."
2221 :action 'widget-checkbox-action)
2222
2223 (defun widget-checkbox-action (widget &optional event)
2224 "Toggle checkbox, notify parent, and set active state of sibling."
2225 (widget-toggle-action widget event)
2226 (let ((sibling (widget-get-sibling widget)))
2227 (when sibling
2228 (if (widget-value widget)
2229 (widget-apply sibling :activate)
2230 (widget-apply sibling :deactivate))
2231 (widget-clear-undo))))
2232
2233 ;;; The `checklist' Widget.
2234
2235 (define-widget 'checklist 'default
2236 "A multiple choice widget."
2237 :convert-widget 'widget-types-convert-widget
2238 :copy 'widget-types-copy
2239 :format "%v"
2240 :offset 4
2241 :entry-format "%b %v"
2242 :greedy nil
2243 :value-create 'widget-checklist-value-create
2244 :value-get 'widget-checklist-value-get
2245 :validate 'widget-checklist-validate
2246 :match 'widget-checklist-match
2247 :match-inline 'widget-checklist-match-inline)
2248
2249 (defun widget-checklist-value-create (widget)
2250 ;; Insert all values
2251 (let ((alist (widget-checklist-match-find widget))
2252 (args (widget-get widget :args)))
2253 (dolist (item args)
2254 (widget-checklist-add-item widget item (assq item alist)))
2255 (widget-put widget :children (nreverse (widget-get widget :children)))))
2256
2257 (defun widget-checklist-add-item (widget type chosen)
2258 "Create checklist item in WIDGET of type TYPE.
2259 If the item is checked, CHOSEN is a cons whose cdr is the value."
2260 (and (eq (preceding-char) ?\n)
2261 (widget-get widget :indent)
2262 (insert-char ?\s (widget-get widget :indent)))
2263 (widget-specify-insert
2264 (let* ((children (widget-get widget :children))
2265 (buttons (widget-get widget :buttons))
2266 (button-args (or (widget-get type :sibling-args)
2267 (widget-get widget :button-args)))
2268 (from (point))
2269 child button)
2270 (insert (widget-get widget :entry-format))
2271 (goto-char from)
2272 ;; Parse % escapes in format.
2273 (while (re-search-forward "%\\([bv%]\\)" nil t)
2274 (let ((escape (char-after (match-beginning 1))))
2275 (delete-char -2)
2276 (cond ((eq escape ?%)
2277 (insert ?%))
2278 ((eq escape ?b)
2279 (setq button (apply 'widget-create-child-and-convert
2280 widget 'checkbox
2281 :value (not (null chosen))
2282 button-args)))
2283 ((eq escape ?v)
2284 (setq child
2285 (cond ((not chosen)
2286 (let ((child (widget-create-child widget type)))
2287 (widget-apply child :deactivate)
2288 child))
2289 ((widget-get type :inline)
2290 (widget-create-child-value
2291 widget type (cdr chosen)))
2292 (t
2293 (widget-create-child-value
2294 widget type (car (cdr chosen)))))))
2295 (t
2296 (error "Unknown escape `%c'" escape)))))
2297 ;; Update properties.
2298 (and button child (widget-put child :button button))
2299 (and button (widget-put widget :buttons (cons button buttons)))
2300 (and child (widget-put widget :children (cons child children))))))
2301
2302 (defun widget-checklist-match (widget vals)
2303 ;; All values must match a type in the checklist.
2304 (and (listp vals)
2305 (null (cdr (widget-checklist-match-inline widget vals)))))
2306
2307 (defun widget-checklist-match-inline (widget vals)
2308 ;; Find the values which match a type in the checklist.
2309 (let ((greedy (widget-get widget :greedy))
2310 (args (copy-sequence (widget-get widget :args)))
2311 found rest)
2312 (while vals
2313 (let ((answer (widget-checklist-match-up args vals)))
2314 (cond (answer
2315 (let ((vals2 (widget-match-inline answer vals)))
2316 (setq found (append found (car vals2))
2317 vals (cdr vals2)
2318 args (delq answer args))))
2319 (greedy
2320 (setq rest (append rest (list (car vals)))
2321 vals (cdr vals)))
2322 (t
2323 (setq rest (append rest vals)
2324 vals nil)))))
2325 (cons found rest)))
2326
2327 (defun widget-checklist-match-find (widget &optional vals)
2328 "Find the vals which match a type in the checklist.
2329 Return an alist of (TYPE MATCH)."
2330 (or vals (setq vals (widget-get widget :value)))
2331 (let ((greedy (widget-get widget :greedy))
2332 (args (copy-sequence (widget-get widget :args)))
2333 found)
2334 (while vals
2335 (let ((answer (widget-checklist-match-up args vals)))
2336 (cond (answer
2337 (let ((match (widget-match-inline answer vals)))
2338 (setq found (cons (cons answer (car match)) found)
2339 vals (cdr match)
2340 args (delq answer args))))
2341 (greedy
2342 (setq vals (cdr vals)))
2343 (t
2344 (setq vals nil)))))
2345 found))
2346
2347 (defun widget-checklist-match-up (args vals)
2348 "Return the first type from ARGS that matches VALS."
2349 (let (current found)
2350 (while (and args (null found))
2351 (setq current (car args)
2352 args (cdr args)
2353 found (widget-match-inline current vals)))
2354 (if found
2355 current)))
2356
2357 (defun widget-checklist-value-get (widget)
2358 ;; The values of all selected items.
2359 (let ((children (widget-get widget :children))
2360 child result)
2361 (while children
2362 (setq child (car children)
2363 children (cdr children))
2364 (if (widget-value (widget-get child :button))
2365 (setq result (append result (widget-apply child :value-inline)))))
2366 result))
2367
2368 (defun widget-checklist-validate (widget)
2369 ;; Ticked children must be valid.
2370 (let ((children (widget-get widget :children))
2371 child button found)
2372 (while (and children (not found))
2373 (setq child (car children)
2374 children (cdr children)
2375 button (widget-get child :button)
2376 found (and (widget-value button)
2377 (widget-apply child :validate))))
2378 found))
2379
2380 ;;; The `option' Widget
2381
2382 (define-widget 'option 'checklist
2383 "An widget with an optional item."
2384 :inline t)
2385
2386 ;;; The `choice-item' Widget.
2387
2388 (define-widget 'choice-item 'item
2389 "Button items that delegate action events to their parents."
2390 :action 'widget-parent-action
2391 :format "%[%t%] \n")
2392
2393 ;;; The `radio-button' Widget.
2394
2395 (define-widget 'radio-button 'toggle
2396 "A radio button for use in the `radio' widget."
2397 :notify 'widget-radio-button-notify
2398 :format "%[%v%]"
2399 :button-suffix ""
2400 :button-prefix ""
2401 :on "(*)"
2402 :on-glyph "radio1"
2403 :off "( )"
2404 :off-glyph "radio0")
2405
2406 (defun widget-radio-button-notify (widget _child &optional event)
2407 ;; Tell daddy.
2408 (widget-apply (widget-get widget :parent) :action widget event))
2409
2410 ;;; The `radio-button-choice' Widget.
2411
2412 (define-widget 'radio-button-choice 'default
2413 "Select one of multiple options."
2414 :convert-widget 'widget-types-convert-widget
2415 :copy 'widget-types-copy
2416 :offset 4
2417 :format "%v"
2418 :entry-format "%b %v"
2419 :value-create 'widget-radio-value-create
2420 :value-get 'widget-radio-value-get
2421 :value-inline 'widget-radio-value-inline
2422 :value-set 'widget-radio-value-set
2423 :error "You must push one of the buttons"
2424 :validate 'widget-radio-validate
2425 :match 'widget-choice-match
2426 :match-inline 'widget-choice-match-inline
2427 :action 'widget-radio-action)
2428
2429 (defun widget-radio-value-create (widget)
2430 ;; Insert all values
2431 (let ((args (widget-get widget :args))
2432 arg)
2433 (while args
2434 (setq arg (car args)
2435 args (cdr args))
2436 (widget-radio-add-item widget arg))))
2437
2438 (defun widget-radio-add-item (widget type)
2439 "Add to radio widget WIDGET a new radio button item of type TYPE."
2440 ;; (setq type (widget-convert type))
2441 (and (eq (preceding-char) ?\n)
2442 (widget-get widget :indent)
2443 (insert-char ?\s (widget-get widget :indent)))
2444 (widget-specify-insert
2445 (let* ((value (widget-get widget :value))
2446 (children (widget-get widget :children))
2447 (buttons (widget-get widget :buttons))
2448 (button-args (or (widget-get type :sibling-args)
2449 (widget-get widget :button-args)))
2450 (from (point))
2451 (chosen (and (null (widget-get widget :choice))
2452 (widget-apply type :match value)))
2453 child button)
2454 (insert (widget-get widget :entry-format))
2455 (goto-char from)
2456 ;; Parse % escapes in format.
2457 (while (re-search-forward "%\\([bv%]\\)" nil t)
2458 (let ((escape (char-after (match-beginning 1))))
2459 (delete-char -2)
2460 (cond ((eq escape ?%)
2461 (insert ?%))
2462 ((eq escape ?b)
2463 (setq button (apply 'widget-create-child-and-convert
2464 widget 'radio-button
2465 :value (not (null chosen))
2466 button-args)))
2467 ((eq escape ?v)
2468 (setq child (if chosen
2469 (widget-create-child-value
2470 widget type value)
2471 (widget-create-child widget type)))
2472 (unless chosen
2473 (widget-apply child :deactivate)))
2474 (t
2475 (error "Unknown escape `%c'" escape)))))
2476 ;; Update properties.
2477 (when chosen
2478 (widget-put widget :choice type))
2479 (when button
2480 (widget-put child :button button)
2481 (widget-put widget :buttons (nconc buttons (list button))))
2482 (when child
2483 (widget-put widget :children (nconc children (list child))))
2484 child)))
2485
2486 (defun widget-radio-value-get (widget)
2487 ;; Get value of the child widget.
2488 (let ((chosen (widget-radio-chosen widget)))
2489 (and chosen (widget-value chosen))))
2490
2491 (defun widget-radio-chosen (widget)
2492 "Return the widget representing the chosen radio button."
2493 (let ((children (widget-get widget :children))
2494 current found)
2495 (while children
2496 (setq current (car children)
2497 children (cdr children))
2498 (when (widget-apply (widget-get current :button) :value-get)
2499 (setq found current
2500 children nil)))
2501 found))
2502
2503 (defun widget-radio-value-inline (widget)
2504 ;; Get value of the child widget.
2505 (let ((children (widget-get widget :children))
2506 current found)
2507 (while children
2508 (setq current (car children)
2509 children (cdr children))
2510 (when (widget-apply (widget-get current :button) :value-get)
2511 (setq found (widget-apply current :value-inline)
2512 children nil)))
2513 found))
2514
2515 (defun widget-radio-value-set (widget value)
2516 ;; We can't just delete and recreate a radio widget, since children
2517 ;; can be added after the original creation and won't be recreated
2518 ;; by `:create'.
2519 (let ((children (widget-get widget :children))
2520 current found)
2521 (while children
2522 (setq current (car children)
2523 children (cdr children))
2524 (let* ((button (widget-get current :button))
2525 (match (and (not found)
2526 (widget-apply current :match value))))
2527 (widget-value-set button match)
2528 (if match
2529 (progn
2530 (widget-value-set current value)
2531 (widget-apply current :activate))
2532 (widget-apply current :deactivate))
2533 (setq found (or found match))))))
2534
2535 (defun widget-radio-validate (widget)
2536 ;; Valid if we have made a valid choice.
2537 (let ((children (widget-get widget :children))
2538 current found button)
2539 (while (and children (not found))
2540 (setq current (car children)
2541 children (cdr children)
2542 button (widget-get current :button)
2543 found (widget-apply button :value-get)))
2544 (if found
2545 (widget-apply current :validate)
2546 widget)))
2547
2548 (defun widget-radio-action (widget child event)
2549 ;; Check if a radio button was pressed.
2550 (let ((children (widget-get widget :children))
2551 (buttons (widget-get widget :buttons))
2552 current)
2553 (when (memq child buttons)
2554 (while children
2555 (setq current (car children)
2556 children (cdr children))
2557 (let* ((button (widget-get current :button)))
2558 (cond ((eq child button)
2559 (widget-value-set button t)
2560 (widget-apply current :activate))
2561 ((widget-value button)
2562 (widget-value-set button nil)
2563 (widget-apply current :deactivate)))))))
2564 ;; Pass notification to parent.
2565 (widget-apply widget :notify child event))
2566
2567 ;;; The `insert-button' Widget.
2568
2569 (define-widget 'insert-button 'push-button
2570 "An insert button for the `editable-list' widget."
2571 :tag "INS"
2572 :help-echo "Insert a new item into the list at this position."
2573 :action 'widget-insert-button-action)
2574
2575 (defun widget-insert-button-action (widget &optional _event)
2576 ;; Ask the parent to insert a new item.
2577 (widget-apply (widget-get widget :parent)
2578 :insert-before (widget-get widget :widget)))
2579
2580 ;;; The `delete-button' Widget.
2581
2582 (define-widget 'delete-button 'push-button
2583 "A delete button for the `editable-list' widget."
2584 :tag "DEL"
2585 :help-echo "Delete this item from the list."
2586 :action 'widget-delete-button-action)
2587
2588 (defun widget-delete-button-action (widget &optional _event)
2589 ;; Ask the parent to insert a new item.
2590 (widget-apply (widget-get widget :parent)
2591 :delete-at (widget-get widget :widget)))
2592
2593 ;;; The `editable-list' Widget.
2594
2595 ;; (defcustom widget-editable-list-gui nil
2596 ;; "If non-nil, use GUI push-buttons in editable list when available."
2597 ;; :type 'boolean
2598 ;; :group 'widgets)
2599
2600 (define-widget 'editable-list 'default
2601 "A variable list of widgets of the same type."
2602 :convert-widget 'widget-types-convert-widget
2603 :copy 'widget-types-copy
2604 :offset 12
2605 :format "%v%i\n"
2606 :format-handler 'widget-editable-list-format-handler
2607 :entry-format "%i %d %v"
2608 :value-create 'widget-editable-list-value-create
2609 :value-get 'widget-editable-list-value-get
2610 :validate 'widget-children-validate
2611 :match 'widget-editable-list-match
2612 :match-inline 'widget-editable-list-match-inline
2613 :insert-before 'widget-editable-list-insert-before
2614 :delete-at 'widget-editable-list-delete-at)
2615
2616 (defun widget-editable-list-format-handler (widget escape)
2617 ;; We recognize the insert button.
2618 ;; (let ((widget-push-button-gui widget-editable-list-gui))
2619 (cond ((eq escape ?i)
2620 (and (widget-get widget :indent)
2621 (insert-char ?\s (widget-get widget :indent)))
2622 (apply 'widget-create-child-and-convert
2623 widget 'insert-button
2624 (widget-get widget :append-button-args)))
2625 (t
2626 (widget-default-format-handler widget escape)))
2627 ;; )
2628 )
2629
2630 (defun widget-editable-list-value-create (widget)
2631 ;; Insert all values
2632 (let* ((value (widget-get widget :value))
2633 (type (nth 0 (widget-get widget :args)))
2634 children)
2635 (widget-put widget :value-pos (copy-marker (point)))
2636 (set-marker-insertion-type (widget-get widget :value-pos) t)
2637 (while value
2638 (let ((answer (widget-match-inline type value)))
2639 (if answer
2640 (setq children (cons (widget-editable-list-entry-create
2641 widget
2642 (if (widget-get type :inline)
2643 (car answer)
2644 (car (car answer)))
2645 t)
2646 children)
2647 value (cdr answer))
2648 (setq value nil))))
2649 (widget-put widget :children (nreverse children))))
2650
2651 (defun widget-editable-list-value-get (widget)
2652 ;; Get value of the child widget.
2653 (apply 'append (mapcar (lambda (child) (widget-apply child :value-inline))
2654 (widget-get widget :children))))
2655
2656 (defun widget-editable-list-match (widget value)
2657 ;; Value must be a list and all the members must match the type.
2658 (and (listp value)
2659 (null (cdr (widget-editable-list-match-inline widget value)))))
2660
2661 (defun widget-editable-list-match-inline (widget value)
2662 (let ((type (nth 0 (widget-get widget :args)))
2663 (ok t)
2664 found)
2665 (while (and value ok)
2666 (let ((answer (widget-match-inline type value)))
2667 (if answer
2668 (setq found (append found (car answer))
2669 value (cdr answer))
2670 (setq ok nil))))
2671 (cons found value)))
2672
2673 (defun widget-editable-list-insert-before (widget before)
2674 ;; Insert a new child in the list of children.
2675 (save-excursion
2676 (let ((children (widget-get widget :children))
2677 (inhibit-read-only t)
2678 before-change-functions
2679 after-change-functions)
2680 (cond (before
2681 (goto-char (widget-get before :entry-from)))
2682 (t
2683 (goto-char (widget-get widget :value-pos))))
2684 (let ((child (widget-editable-list-entry-create
2685 widget nil nil)))
2686 (when (< (widget-get child :entry-from) (widget-get widget :from))
2687 (set-marker (widget-get widget :from)
2688 (widget-get child :entry-from)))
2689 (if (eq (car children) before)
2690 (widget-put widget :children (cons child children))
2691 (while (not (eq (car (cdr children)) before))
2692 (setq children (cdr children)))
2693 (setcdr children (cons child (cdr children)))))))
2694 (widget-setup)
2695 (widget-apply widget :notify widget))
2696
2697 (defun widget-editable-list-delete-at (widget child)
2698 ;; Delete child from list of children.
2699 (save-excursion
2700 (let ((buttons (copy-sequence (widget-get widget :buttons)))
2701 button
2702 (inhibit-read-only t)
2703 before-change-functions
2704 after-change-functions)
2705 (while buttons
2706 (setq button (car buttons)
2707 buttons (cdr buttons))
2708 (when (eq (widget-get button :widget) child)
2709 (widget-put widget
2710 :buttons (delq button (widget-get widget :buttons)))
2711 (widget-delete button))))
2712 (let ((entry-from (widget-get child :entry-from))
2713 (entry-to (widget-get child :entry-to))
2714 (inhibit-read-only t)
2715 before-change-functions
2716 after-change-functions)
2717 (widget-delete child)
2718 (delete-region entry-from entry-to)
2719 (set-marker entry-from nil)
2720 (set-marker entry-to nil))
2721 (widget-put widget :children (delq child (widget-get widget :children))))
2722 (widget-setup)
2723 (widget-apply widget :notify widget))
2724
2725 (defun widget-editable-list-entry-create (widget value conv)
2726 ;; Create a new entry to the list.
2727 (let ((type (nth 0 (widget-get widget :args)))
2728 ;; (widget-push-button-gui widget-editable-list-gui)
2729 child delete insert)
2730 (widget-specify-insert
2731 (save-excursion
2732 (and (widget-get widget :indent)
2733 (insert-char ?\s (widget-get widget :indent)))
2734 (insert (widget-get widget :entry-format)))
2735 ;; Parse % escapes in format.
2736 (while (re-search-forward "%\\(.\\)" nil t)
2737 (let ((escape (char-after (match-beginning 1))))
2738 (delete-char -2)
2739 (cond ((eq escape ?%)
2740 (insert ?%))
2741 ((eq escape ?i)
2742 (setq insert (apply 'widget-create-child-and-convert
2743 widget 'insert-button
2744 (widget-get widget :insert-button-args))))
2745 ((eq escape ?d)
2746 (setq delete (apply 'widget-create-child-and-convert
2747 widget 'delete-button
2748 (widget-get widget :delete-button-args))))
2749 ((eq escape ?v)
2750 (if conv
2751 (setq child (widget-create-child-value
2752 widget type value))
2753 (setq child (widget-create-child-value
2754 widget type (widget-default-get type)))))
2755 (t
2756 (error "Unknown escape `%c'" escape)))))
2757 (let ((buttons (widget-get widget :buttons)))
2758 (if insert (push insert buttons))
2759 (if delete (push delete buttons))
2760 (widget-put widget :buttons buttons))
2761 (let ((entry-from (point-min-marker))
2762 (entry-to (point-max-marker)))
2763 (set-marker-insertion-type entry-from t)
2764 (set-marker-insertion-type entry-to nil)
2765 (widget-put child :entry-from entry-from)
2766 (widget-put child :entry-to entry-to)))
2767 (if insert (widget-put insert :widget child))
2768 (if delete (widget-put delete :widget child))
2769 child))
2770
2771 ;;; The `group' Widget.
2772
2773 (define-widget 'group 'default
2774 "A widget which groups other widgets inside."
2775 :convert-widget 'widget-types-convert-widget
2776 :copy 'widget-types-copy
2777 :format "%v"
2778 :value-create 'widget-group-value-create
2779 :value-get 'widget-editable-list-value-get
2780 :default-get 'widget-group-default-get
2781 :validate 'widget-children-validate
2782 :match 'widget-group-match
2783 :match-inline 'widget-group-match-inline)
2784
2785 (defun widget-group-value-create (widget)
2786 ;; Create each component.
2787 (let ((args (widget-get widget :args))
2788 (value (widget-get widget :value))
2789 arg answer children)
2790 (while args
2791 (setq arg (car args)
2792 args (cdr args)
2793 answer (widget-match-inline arg value)
2794 value (cdr answer))
2795 (and (eq (preceding-char) ?\n)
2796 (widget-get widget :indent)
2797 (insert-char ?\s (widget-get widget :indent)))
2798 (push (cond ((null answer)
2799 (widget-create-child widget arg))
2800 ((widget-get arg :inline)
2801 (widget-create-child-value widget arg (car answer)))
2802 (t
2803 (widget-create-child-value widget arg (car (car answer)))))
2804 children))
2805 (widget-put widget :children (nreverse children))))
2806
2807 (defun widget-group-default-get (widget)
2808 ;; Get the default of the components.
2809 (mapcar 'widget-default-get (widget-get widget :args)))
2810
2811 (defun widget-group-match (widget vals)
2812 ;; Match if the components match.
2813 (and (listp vals)
2814 (let ((match (widget-group-match-inline widget vals)))
2815 (and match (null (cdr match))))))
2816
2817 (defun widget-group-match-inline (widget vals)
2818 ;; Match if the components match.
2819 (let ((args (widget-get widget :args))
2820 argument answer found)
2821 (while args
2822 (setq argument (car args)
2823 args (cdr args))
2824 (if (setq answer (widget-match-inline argument vals))
2825 (setq found (append found (car answer))
2826 vals (cdr answer))
2827 (setq vals nil
2828 args nil)))
2829 (if answer
2830 (cons found vals))))
2831
2832 ;;; The `visibility' Widget.
2833
2834 (define-widget 'visibility 'item
2835 "An indicator and manipulator for hidden items.
2836
2837 The following properties have special meanings for this widget:
2838 :on-glyph Image filename or spec to display when the item is visible.
2839 :on Text shown if the \"on\" image is nil or cannot be displayed.
2840 :off-glyph Image filename or spec to display when the item is hidden.
2841 :off Text shown if the \"off\" image is nil cannot be displayed."
2842 :format "%[%v%]"
2843 :button-prefix ""
2844 :button-suffix ""
2845 :on-glyph "down"
2846 :on "Hide"
2847 :off-glyph "right"
2848 :off "Show"
2849 :value-create 'widget-visibility-value-create
2850 :action 'widget-toggle-action
2851 :match (lambda (_widget _value) t))
2852
2853 (defalias 'widget-visibility-value-create 'widget-toggle-value-create)
2854
2855 ;;; The `documentation-link' Widget.
2856 ;;
2857 ;; This is a helper widget for `documentation-string'.
2858
2859 (define-widget 'documentation-link 'link
2860 "Link type used in documentation strings."
2861 :tab-order -1
2862 :help-echo "Describe this symbol"
2863 :action 'widget-documentation-link-action)
2864
2865 (defun widget-documentation-link-action (widget &optional _event)
2866 "Display documentation for WIDGET's value. Ignore optional argument EVENT."
2867 (let* ((string (widget-get widget :value))
2868 (symbol (intern string)))
2869 (if (and (fboundp symbol) (boundp symbol))
2870 ;; If there are two doc strings, give the user a way to pick one.
2871 (apropos (concat "\\`" (regexp-quote string) "\\'"))
2872 (if (fboundp symbol)
2873 (describe-function symbol)
2874 (describe-variable symbol)))))
2875
2876 (defcustom widget-documentation-links t
2877 "Add hyperlinks to documentation strings when non-nil."
2878 :type 'boolean
2879 :group 'widget-documentation)
2880
2881 (defcustom widget-documentation-link-regexp "`\\([^\n`' ]+\\)'"
2882 "Regexp for matching potential links in documentation strings.
2883 The first group should be the link itself."
2884 :type 'regexp
2885 :group 'widget-documentation)
2886
2887 (defcustom widget-documentation-link-p 'intern-soft
2888 "Predicate used to test if a string is useful as a link.
2889 The value should be a function. The function will be called with one
2890 argument, a string, and should return non-nil if there should be a
2891 link for that string."
2892 :type 'function
2893 :options '(widget-documentation-link-p)
2894 :group 'widget-documentation)
2895
2896 (defcustom widget-documentation-link-type 'documentation-link
2897 "Widget type used for links in documentation strings."
2898 :type 'symbol
2899 :group 'widget-documentation)
2900
2901 (defun widget-documentation-link-add (widget from to)
2902 (widget-specify-doc widget from to)
2903 (when widget-documentation-links
2904 (let ((regexp widget-documentation-link-regexp)
2905 (buttons (widget-get widget :buttons))
2906 (widget-mouse-face (default-value 'widget-mouse-face))
2907 (widget-button-face widget-documentation-face)
2908 (widget-button-pressed-face widget-documentation-face))
2909 (save-excursion
2910 (goto-char from)
2911 (while (re-search-forward regexp to t)
2912 (let ((name (match-string 1))
2913 (begin (match-beginning 1))
2914 (end (match-end 1)))
2915 (when (funcall widget-documentation-link-p name)
2916 (push (widget-convert-button widget-documentation-link-type
2917 begin end :value name)
2918 buttons)))))
2919 (widget-put widget :buttons buttons))))
2920
2921 ;;; The `documentation-string' Widget.
2922
2923 (define-widget 'documentation-string 'item
2924 "A documentation string."
2925 :format "%v"
2926 :action 'widget-documentation-string-action
2927 :value-create 'widget-documentation-string-value-create
2928 :visibility-widget 'visibility)
2929
2930 (defun widget-documentation-string-value-create (widget)
2931 ;; Insert documentation string.
2932 (let ((doc (widget-value widget))
2933 (indent (widget-get widget :indent))
2934 (shown (widget-get (widget-get widget :parent) :documentation-shown))
2935 (start (point)))
2936 (if (string-match "\n" doc)
2937 (let ((before (substring doc 0 (match-beginning 0)))
2938 (after (substring doc (match-end 0)))
2939 button end)
2940 (widget-documentation-string-indent-to indent)
2941 (insert before ?\s)
2942 (widget-documentation-link-add widget start (point))
2943 (setq button
2944 (widget-create-child-and-convert
2945 widget (widget-get widget :visibility-widget)
2946 :help-echo "Show or hide rest of the documentation."
2947 :on "Hide"
2948 :off "More"
2949 :always-active t
2950 :action 'widget-parent-action
2951 shown))
2952 (when shown
2953 (insert ?\n)
2954 (setq start (point))
2955 (when (and indent (not (zerop indent)))
2956 (insert-char ?\s indent))
2957 (insert after)
2958 (setq end (point))
2959 (widget-documentation-link-add widget start end)
2960 ;; Indent the subsequent lines.
2961 (when (and indent (> indent 0))
2962 (save-excursion
2963 (save-restriction
2964 (narrow-to-region start end)
2965 (goto-char (point-min))
2966 (while (search-forward "\n" nil t)
2967 (widget-documentation-string-indent-to indent))))))
2968 (widget-put widget :buttons (list button)))
2969 (widget-documentation-string-indent-to indent)
2970 (insert doc)
2971 (widget-documentation-link-add widget start (point))))
2972 (insert ?\n))
2973
2974 (defun widget-documentation-string-indent-to (col)
2975 (when (and (numberp col)
2976 (> col 0))
2977 (let ((opoint (point)))
2978 (indent-to col)
2979 (put-text-property opoint (point)
2980 'display `(space :align-to ,col)))))
2981
2982 (defun widget-documentation-string-action (widget &rest _ignore)
2983 ;; Toggle documentation.
2984 (let ((parent (widget-get widget :parent)))
2985 (widget-put parent :documentation-shown
2986 (not (widget-get parent :documentation-shown))))
2987 ;; Redraw.
2988 (widget-value-set widget (widget-value widget)))
2989
2990 (defun widget-add-documentation-string-button (widget &rest args)
2991 "Insert a new `documentation-string' widget based on WIDGET.
2992 The new widget becomes a child of WIDGET, and is also added to
2993 its `:buttons' list. The documentation string is found from
2994 WIDGET using the function `widget-docstring'.
2995 Optional ARGS specifies additional keyword arguments for the
2996 `documentation-string' widget."
2997 (let ((doc (widget-docstring widget))
2998 (indent (widget-get widget :indent))
2999 (doc-indent (widget-get widget :documentation-indent)))
3000 (when doc
3001 (and (eq (preceding-char) ?\n)
3002 indent
3003 (insert-char ?\s indent))
3004 (unless (or (numberp doc-indent) (null doc-indent))
3005 (setq doc-indent 0))
3006 (widget-put widget :buttons
3007 (cons (apply 'widget-create-child-and-convert
3008 widget 'documentation-string
3009 :indent doc-indent
3010 (nconc args (list doc)))
3011 (widget-get widget :buttons))))))
3012 \f
3013 ;;; The Sexp Widgets.
3014
3015 (define-widget 'const 'item
3016 "An immutable sexp."
3017 :prompt-value 'widget-const-prompt-value
3018 :format "%t\n%d")
3019
3020 (defun widget-const-prompt-value (widget _prompt _value _unbound)
3021 ;; Return the value of the const.
3022 (widget-value widget))
3023
3024 (define-widget 'function-item 'const
3025 "An immutable function name."
3026 :format "%v\n%h"
3027 :documentation-property (lambda (symbol)
3028 (condition-case nil
3029 (documentation symbol t)
3030 (error nil))))
3031
3032 (define-widget 'variable-item 'const
3033 "An immutable variable name."
3034 :format "%v\n%h"
3035 :documentation-property 'variable-documentation)
3036
3037 (define-widget 'other 'sexp
3038 "Matches any value, but doesn't let the user edit the value.
3039 This is useful as last item in a `choice' widget.
3040 You should use this widget type with a default value,
3041 as in (other DEFAULT) or (other :tag \"NAME\" DEFAULT).
3042 If the user selects this alternative, that specifies DEFAULT
3043 as the value."
3044 :tag "Other"
3045 :format "%t%n"
3046 :value 'other)
3047
3048 (defvar widget-string-prompt-value-history nil
3049 "History of input to `widget-string-prompt-value'.")
3050
3051 (define-widget 'string 'editable-field
3052 "A string"
3053 :tag "String"
3054 :format "%{%t%}: %v"
3055 :complete-function 'ispell-complete-word
3056 :prompt-history 'widget-string-prompt-value-history)
3057
3058 (define-widget 'regexp 'string
3059 "A regular expression."
3060 :match 'widget-regexp-match
3061 :validate 'widget-regexp-validate
3062 ;; Doesn't work well with terminating newline.
3063 ;; :value-face 'widget-single-line-field
3064 :tag "Regexp")
3065
3066 (defun widget-regexp-match (_widget value)
3067 ;; Match valid regexps.
3068 (and (stringp value)
3069 (condition-case nil
3070 (prog1 t
3071 (string-match value ""))
3072 (error nil))))
3073
3074 (defun widget-regexp-validate (widget)
3075 "Check that the value of WIDGET is a valid regexp."
3076 (condition-case data
3077 (prog1 nil
3078 (string-match (widget-value widget) ""))
3079 (error (widget-put widget :error (error-message-string data))
3080 widget)))
3081
3082 (define-widget 'file 'string
3083 "A file widget.
3084 It reads a file name from an editable text field."
3085 :completions #'completion-file-name-table
3086 :prompt-value 'widget-file-prompt-value
3087 :format "%{%t%}: %v"
3088 ;; Doesn't work well with terminating newline.
3089 ;; :value-face 'widget-single-line-field
3090 :tag "File")
3091
3092 (defun widget-file-prompt-value (widget prompt value unbound)
3093 ;; Read file from minibuffer.
3094 (abbreviate-file-name
3095 (if unbound
3096 (read-file-name prompt)
3097 (let ((prompt2 (format "%s (default %s): " prompt value))
3098 (dir (file-name-directory value))
3099 (file (file-name-nondirectory value))
3100 (must-match (widget-get widget :must-match)))
3101 (read-file-name prompt2 dir nil must-match file)))))
3102
3103 ;;;(defun widget-file-action (widget &optional event)
3104 ;;; ;; Read a file name from the minibuffer.
3105 ;;; (let* ((value (widget-value widget))
3106 ;;; (dir (file-name-directory value))
3107 ;;; (file (file-name-nondirectory value))
3108 ;;; (menu-tag (widget-apply widget :menu-tag-get))
3109 ;;; (must-match (widget-get widget :must-match))
3110 ;;; (answer (read-file-name (concat menu-tag " (default " value "): ")
3111 ;;; dir nil must-match file)))
3112 ;;; (widget-value-set widget (abbreviate-file-name answer))
3113 ;;; (widget-setup)
3114 ;;; (widget-apply widget :notify widget event)))
3115
3116 ;; Fixme: use file-name-as-directory.
3117 (define-widget 'directory 'file
3118 "A directory widget.
3119 It reads a directory name from an editable text field."
3120 :tag "Directory")
3121
3122 (defvar widget-symbol-prompt-value-history nil
3123 "History of input to `widget-symbol-prompt-value'.")
3124
3125 (define-widget 'symbol 'editable-field
3126 "A Lisp symbol."
3127 :value nil
3128 :tag "Symbol"
3129 :format "%{%t%}: %v"
3130 :match (lambda (_widget value) (symbolp value))
3131 :completions obarray
3132 :prompt-internal 'widget-symbol-prompt-internal
3133 :prompt-match 'symbolp
3134 :prompt-history 'widget-symbol-prompt-value-history
3135 :value-to-internal (lambda (_widget value)
3136 (if (symbolp value)
3137 (symbol-name value)
3138 value))
3139 :value-to-external (lambda (_widget value)
3140 (if (stringp value)
3141 (intern value)
3142 value)))
3143
3144 (defun widget-symbol-prompt-internal (widget prompt initial history)
3145 ;; Read file from minibuffer.
3146 (let ((answer (completing-read prompt obarray
3147 (widget-get widget :prompt-match)
3148 nil initial history)))
3149 (if (and (stringp answer)
3150 (not (zerop (length answer))))
3151 answer
3152 (error "No value"))))
3153
3154 (defvar widget-function-prompt-value-history nil
3155 "History of input to `widget-function-prompt-value'.")
3156
3157 (define-widget 'function 'restricted-sexp
3158 "A Lisp function."
3159 :completions (apply-partially #'completion-table-with-predicate
3160 obarray #'fboundp 'strict)
3161 :prompt-value 'widget-field-prompt-value
3162 :prompt-internal 'widget-symbol-prompt-internal
3163 :prompt-match 'fboundp
3164 :prompt-history 'widget-function-prompt-value-history
3165 :action 'widget-field-action
3166 :match-alternatives '(functionp)
3167 :validate (lambda (widget)
3168 (unless (functionp (widget-value widget))
3169 (widget-put widget :error (format "Invalid function: %S"
3170 (widget-value widget)))
3171 widget))
3172 :value 'ignore
3173 :tag "Function")
3174
3175 (defvar widget-variable-prompt-value-history nil
3176 "History of input to `widget-variable-prompt-value'.")
3177
3178 (define-widget 'variable 'symbol
3179 "A Lisp variable."
3180 :prompt-match 'boundp
3181 :prompt-history 'widget-variable-prompt-value-history
3182 :completions (apply-partially #'completion-table-with-predicate
3183 obarray #'boundp 'strict)
3184 :tag "Variable")
3185
3186 (define-widget 'coding-system 'symbol
3187 "A MULE coding-system."
3188 :format "%{%t%}: %v"
3189 :tag "Coding system"
3190 :base-only nil
3191 :prompt-history 'coding-system-value-history
3192 :prompt-value 'widget-coding-system-prompt-value
3193 :action 'widget-coding-system-action
3194 :completions (apply-partially #'completion-table-with-predicate
3195 obarray #'coding-system-p 'strict)
3196 :validate (lambda (widget)
3197 (unless (coding-system-p (widget-value widget))
3198 (widget-put widget :error (format "Invalid coding system: %S"
3199 (widget-value widget)))
3200 widget))
3201 :value 'undecided
3202 :prompt-match 'coding-system-p)
3203
3204 (defun widget-coding-system-prompt-value (widget prompt value _unbound)
3205 "Read coding-system from minibuffer."
3206 (if (widget-get widget :base-only)
3207 (intern
3208 (completing-read (format "%s (default %s): " prompt value)
3209 (mapcar #'list (coding-system-list t)) nil nil nil
3210 coding-system-history))
3211 (read-coding-system (format "%s (default %s): " prompt value) value)))
3212
3213 (defun widget-coding-system-action (widget &optional event)
3214 (let ((answer
3215 (widget-coding-system-prompt-value
3216 widget
3217 (widget-apply widget :menu-tag-get)
3218 (widget-value widget)
3219 t)))
3220 (widget-value-set widget answer)
3221 (widget-apply widget :notify widget event)
3222 (widget-setup)))
3223 \f
3224 ;;; I'm not sure about what this is good for? KFS.
3225 (defvar widget-key-sequence-prompt-value-history nil
3226 "History of input to `widget-key-sequence-prompt-value'.")
3227
3228 (defvar widget-key-sequence-default-value [ignore]
3229 "Default value for an empty key sequence.")
3230
3231 (defvar widget-key-sequence-map
3232 (let ((map (make-sparse-keymap)))
3233 (set-keymap-parent map widget-field-keymap)
3234 (define-key map [(control ?q)] 'widget-key-sequence-read-event)
3235 map))
3236
3237 (define-widget 'key-sequence 'restricted-sexp
3238 "A key sequence."
3239 :prompt-value 'widget-field-prompt-value
3240 :prompt-internal 'widget-symbol-prompt-internal
3241 ; :prompt-match 'fboundp ;; What was this good for? KFS
3242 :prompt-history 'widget-key-sequence-prompt-value-history
3243 :action 'widget-field-action
3244 :match-alternatives '(stringp vectorp)
3245 :format "%{%t%}: %v"
3246 :validate 'widget-key-sequence-validate
3247 :value-to-internal 'widget-key-sequence-value-to-internal
3248 :value-to-external 'widget-key-sequence-value-to-external
3249 :value widget-key-sequence-default-value
3250 :keymap widget-key-sequence-map
3251 :help-echo "C-q: insert KEY, EVENT, or CODE; RET: enter value"
3252 :tag "Key sequence")
3253
3254 (defun widget-key-sequence-read-event (ev)
3255 (interactive (list
3256 (let ((inhibit-quit t) quit-flag)
3257 (read-event "Insert KEY, EVENT, or CODE: "))))
3258 (let ((ev2 (and (memq 'down (event-modifiers ev))
3259 (read-event)))
3260 (tr (and (keymapp function-key-map)
3261 (lookup-key function-key-map (vector ev)))))
3262 (when (and (integerp ev)
3263 (or (and (<= ?0 ev) (< ev (+ ?0 (min 10 read-quoted-char-radix))))
3264 (and (<= ?a (downcase ev))
3265 (< (downcase ev) (+ ?a -10 (min 36 read-quoted-char-radix))))))
3266 (setq unread-command-events (cons ev unread-command-events)
3267 ev (read-quoted-char (format "Enter code (radix %d)" read-quoted-char-radix))
3268 tr nil)
3269 (if (and (integerp ev) (not (characterp ev)))
3270 (insert (char-to-string ev)))) ;; throw invalid char error
3271 (setq ev (key-description (list ev)))
3272 (when (arrayp tr)
3273 (setq tr (key-description (list (aref tr 0))))
3274 (if (y-or-n-p (format "Key %s is translated to %s -- use %s? " ev tr tr))
3275 (setq ev tr ev2 nil)))
3276 (insert (if (= (char-before) ?\s) "" " ") ev " ")
3277 (if ev2
3278 (insert (key-description (list ev2)) " "))))
3279
3280 (defun widget-key-sequence-validate (widget)
3281 (unless (or (stringp (widget-value widget))
3282 (vectorp (widget-value widget)))
3283 (widget-put widget :error (format "Invalid key sequence: %S"
3284 (widget-value widget)))
3285 widget))
3286
3287 (defun widget-key-sequence-value-to-internal (widget value)
3288 (if (widget-apply widget :match value)
3289 (if (equal value widget-key-sequence-default-value)
3290 ""
3291 (key-description value))
3292 value))
3293
3294 (defun widget-key-sequence-value-to-external (_widget value)
3295 (if (stringp value)
3296 (if (string-match "\\`[[:space:]]*\\'" value)
3297 widget-key-sequence-default-value
3298 (read-kbd-macro value))
3299 value))
3300
3301 \f
3302 (define-widget 'sexp 'editable-field
3303 "An arbitrary Lisp expression."
3304 :tag "Lisp expression"
3305 :format "%{%t%}: %v"
3306 :value nil
3307 :validate 'widget-sexp-validate
3308 :match (lambda (_widget _value) t)
3309 :value-to-internal 'widget-sexp-value-to-internal
3310 :value-to-external (lambda (_widget value) (read value))
3311 :prompt-history 'widget-sexp-prompt-value-history
3312 :prompt-value 'widget-sexp-prompt-value)
3313
3314 (defun widget-sexp-value-to-internal (_widget value)
3315 ;; Use pp for printer representation.
3316 (let ((pp (if (symbolp value)
3317 (prin1-to-string value)
3318 (pp-to-string value))))
3319 (while (string-match "\n\\'" pp)
3320 (setq pp (substring pp 0 -1)))
3321 (if (or (string-match "\n\\'" pp)
3322 (> (length pp) 40))
3323 (concat "\n" pp)
3324 pp)))
3325
3326 (defun widget-sexp-validate (widget)
3327 ;; Valid if we can read the string and there is no junk left after it.
3328 (with-temp-buffer
3329 (insert (widget-apply widget :value-get))
3330 (goto-char (point-min))
3331 (let (err)
3332 (condition-case data ;Note: We get a spurious byte-compile warning here.
3333 (progn
3334 ;; Avoid a confusing end-of-file error.
3335 (skip-syntax-forward "\\s-")
3336 (if (eobp)
3337 (setq err "Empty sexp -- use `nil'?")
3338 (unless (widget-apply widget :match (read (current-buffer)))
3339 (setq err (widget-get widget :type-error))))
3340 ;; Allow whitespace after expression.
3341 (skip-syntax-forward "\\s-")
3342 (if (and (not (eobp))
3343 (not err))
3344 (setq err (format "Junk at end of expression: %s"
3345 (buffer-substring (point)
3346 (point-max))))))
3347 (end-of-file ; Avoid confusing error message.
3348 (setq err "Unbalanced sexp"))
3349 (error (setq err (error-message-string data))))
3350 (if (not err)
3351 nil
3352 (widget-put widget :error err)
3353 widget))))
3354
3355 (defvar widget-sexp-prompt-value-history nil
3356 "History of input to `widget-sexp-prompt-value'.")
3357
3358 (defun widget-sexp-prompt-value (widget prompt value unbound)
3359 ;; Read an arbitrary sexp.
3360 (let ((found (read-string prompt
3361 (if unbound nil (cons (prin1-to-string value) 0))
3362 (widget-get widget :prompt-history))))
3363 (let ((answer (read-from-string found)))
3364 (unless (= (cdr answer) (length found))
3365 (error "Junk at end of expression: %s"
3366 (substring found (cdr answer))))
3367 (car answer))))
3368
3369 (define-widget 'restricted-sexp 'sexp
3370 "A Lisp expression restricted to values that match.
3371 To use this type, you must define :match or :match-alternatives."
3372 :type-error "The specified value is not valid"
3373 :match 'widget-restricted-sexp-match
3374 :value-to-internal (lambda (widget value)
3375 (if (widget-apply widget :match value)
3376 (prin1-to-string value)
3377 value)))
3378
3379 (defun widget-restricted-sexp-match (widget value)
3380 (let ((alternatives (widget-get widget :match-alternatives))
3381 matched)
3382 (while (and alternatives (not matched))
3383 (if (cond ((functionp (car alternatives))
3384 (funcall (car alternatives) value))
3385 ((and (consp (car alternatives))
3386 (eq (car (car alternatives)) 'quote))
3387 (eq value (nth 1 (car alternatives)))))
3388 (setq matched t))
3389 (setq alternatives (cdr alternatives)))
3390 matched))
3391 \f
3392 (define-widget 'integer 'restricted-sexp
3393 "An integer."
3394 :tag "Integer"
3395 :value 0
3396 :type-error "This field should contain an integer"
3397 :match-alternatives '(integerp))
3398
3399 (define-widget 'number 'restricted-sexp
3400 "A number (floating point or integer)."
3401 :tag "Number"
3402 :value 0.0
3403 :type-error "This field should contain a number (floating point or integer)"
3404 :match-alternatives '(numberp))
3405
3406 (define-widget 'float 'restricted-sexp
3407 "A floating point number."
3408 :tag "Floating point number"
3409 :value 0.0
3410 :type-error "This field should contain a floating point number"
3411 :match-alternatives '(floatp))
3412
3413 (define-widget 'character 'editable-field
3414 "A character."
3415 :tag "Character"
3416 :value 0
3417 :size 1
3418 :format "%{%t%}: %v\n"
3419 :valid-regexp "\\`.\\'"
3420 :error "This field should contain a single character"
3421 :value-get (lambda (w) (widget-field-value-get w t))
3422 :value-to-internal (lambda (_widget value)
3423 (if (stringp value)
3424 value
3425 (char-to-string value)))
3426 :value-to-external (lambda (_widget value)
3427 (if (stringp value)
3428 (aref value 0)
3429 value))
3430 :match (lambda (_widget value)
3431 (characterp value)))
3432
3433 (define-widget 'list 'group
3434 "A Lisp list."
3435 :tag "List"
3436 :format "%{%t%}:\n%v")
3437
3438 (define-widget 'vector 'group
3439 "A Lisp vector."
3440 :tag "Vector"
3441 :format "%{%t%}:\n%v"
3442 :match 'widget-vector-match
3443 :value-to-internal (lambda (_widget value) (append value nil))
3444 :value-to-external (lambda (_widget value) (apply 'vector value)))
3445
3446 (defun widget-vector-match (widget value)
3447 (and (vectorp value)
3448 (widget-group-match widget
3449 (widget-apply widget :value-to-internal value))))
3450
3451 (define-widget 'cons 'group
3452 "A cons-cell."
3453 :tag "Cons-cell"
3454 :format "%{%t%}:\n%v"
3455 :match 'widget-cons-match
3456 :value-to-internal (lambda (_widget value)
3457 (list (car value) (cdr value)))
3458 :value-to-external (lambda (_widget value)
3459 (apply 'cons value)))
3460
3461 (defun widget-cons-match (widget value)
3462 (and (consp value)
3463 (widget-group-match widget
3464 (widget-apply widget :value-to-internal value))))
3465 \f
3466 ;;; The `lazy' Widget.
3467 ;;
3468 ;; Recursive datatypes.
3469
3470 (define-widget 'lazy 'default
3471 "Base widget for recursive datastructures.
3472
3473 The `lazy' widget will, when instantiated, contain a single inferior
3474 widget, of the widget type specified by the :type parameter. The
3475 value of the `lazy' widget is the same as the value of the inferior
3476 widget. When deriving a new widget from the 'lazy' widget, the :type
3477 parameter is allowed to refer to the widget currently being defined,
3478 thus allowing recursive datastructures to be described.
3479
3480 The :type parameter takes the same arguments as the defcustom
3481 parameter with the same name.
3482
3483 Most composite widgets, i.e. widgets containing other widgets, does
3484 not allow recursion. That is, when you define a new widget type, none
3485 of the inferior widgets may be of the same type you are currently
3486 defining.
3487
3488 In Lisp, however, it is custom to define datastructures in terms of
3489 themselves. A list, for example, is defined as either nil, or a cons
3490 cell whose cdr itself is a list. The obvious way to translate this
3491 into a widget type would be
3492
3493 (define-widget 'my-list 'choice
3494 \"A list of sexps.\"
3495 :tag \"Sexp list\"
3496 :args '((const nil) (cons :value (nil) sexp my-list)))
3497
3498 Here we attempt to define my-list as a choice of either the constant
3499 nil, or a cons-cell containing a sexp and my-lisp. This will not work
3500 because the `choice' widget does not allow recursion.
3501
3502 Using the `lazy' widget you can overcome this problem, as in this
3503 example:
3504
3505 (define-widget 'sexp-list 'lazy
3506 \"A list of sexps.\"
3507 :tag \"Sexp list\"
3508 :type '(choice (const nil) (cons :value (nil) sexp sexp-list)))"
3509 :format "%{%t%}: %v"
3510 ;; We don't convert :type because we want to allow recursive
3511 ;; datastructures. This is slow, so we should not create speed
3512 ;; critical widgets by deriving from this.
3513 :convert-widget 'widget-value-convert-widget
3514 :value-create 'widget-type-value-create
3515 :value-get 'widget-child-value-get
3516 :value-inline 'widget-child-value-inline
3517 :default-get 'widget-type-default-get
3518 :match 'widget-type-match
3519 :validate 'widget-child-validate)
3520
3521 \f
3522 ;;; The `plist' Widget.
3523 ;;
3524 ;; Property lists.
3525
3526 (define-widget 'plist 'list
3527 "A property list."
3528 :key-type '(symbol :tag "Key")
3529 :value-type '(sexp :tag "Value")
3530 :convert-widget 'widget-plist-convert-widget
3531 :tag "Plist")
3532
3533 (defvar widget-plist-value-type) ;Dynamic variable
3534
3535 (defun widget-plist-convert-widget (widget)
3536 ;; Handle `:options'.
3537 (let* ((options (widget-get widget :options))
3538 (widget-plist-value-type (widget-get widget :value-type))
3539 (other `(editable-list :inline t
3540 (group :inline t
3541 ,(widget-get widget :key-type)
3542 ,widget-plist-value-type)))
3543 (args (if options
3544 (list `(checklist :inline t
3545 :greedy t
3546 ,@(mapcar 'widget-plist-convert-option
3547 options))
3548 other)
3549 (list other))))
3550 (widget-put widget :args args)
3551 widget))
3552
3553 (defun widget-plist-convert-option (option)
3554 ;; Convert a single plist option.
3555 (let (key-type value-type)
3556 (if (listp option)
3557 (let ((key (nth 0 option)))
3558 (setq value-type (nth 1 option))
3559 (if (listp key)
3560 (setq key-type key)
3561 (setq key-type `(const ,key))))
3562 (setq key-type `(const ,option)
3563 value-type widget-plist-value-type))
3564 `(group :format "Key: %v" :inline t ,key-type ,value-type)))
3565
3566
3567 ;;; The `alist' Widget.
3568 ;;
3569 ;; Association lists.
3570
3571 (define-widget 'alist 'list
3572 "An association list."
3573 :key-type '(sexp :tag "Key")
3574 :value-type '(sexp :tag "Value")
3575 :convert-widget 'widget-alist-convert-widget
3576 :tag "Alist")
3577
3578 (defvar widget-alist-value-type) ;Dynamic variable
3579
3580 (defun widget-alist-convert-widget (widget)
3581 ;; Handle `:options'.
3582 (let* ((options (widget-get widget :options))
3583 (widget-alist-value-type (widget-get widget :value-type))
3584 (other `(editable-list :inline t
3585 (cons :format "%v"
3586 ,(widget-get widget :key-type)
3587 ,widget-alist-value-type)))
3588 (args (if options
3589 (list `(checklist :inline t
3590 :greedy t
3591 ,@(mapcar 'widget-alist-convert-option
3592 options))
3593 other)
3594 (list other))))
3595 (widget-put widget :args args)
3596 widget))
3597
3598 (defun widget-alist-convert-option (option)
3599 ;; Convert a single alist option.
3600 (let (key-type value-type)
3601 (if (listp option)
3602 (let ((key (nth 0 option)))
3603 (setq value-type (nth 1 option))
3604 (if (listp key)
3605 (setq key-type key)
3606 (setq key-type `(const ,key))))
3607 (setq key-type `(const ,option)
3608 value-type widget-alist-value-type))
3609 `(cons :format "Key: %v" ,key-type ,value-type)))
3610 \f
3611 (define-widget 'choice 'menu-choice
3612 "A union of several sexp types."
3613 :tag "Choice"
3614 :format "%{%t%}: %[Value Menu%] %v"
3615 :button-prefix 'widget-push-button-prefix
3616 :button-suffix 'widget-push-button-suffix
3617 :prompt-value 'widget-choice-prompt-value)
3618
3619 (defun widget-choice-prompt-value (widget prompt value _unbound)
3620 "Make a choice."
3621 (let ((args (widget-get widget :args))
3622 (completion-ignore-case (widget-get widget :case-fold))
3623 current choices old)
3624 ;; Find the first arg that matches VALUE.
3625 (let ((look args))
3626 (while look
3627 (if (widget-apply (car look) :match value)
3628 (setq old (car look)
3629 look nil)
3630 (setq look (cdr look)))))
3631 ;; Find new choice.
3632 (setq current
3633 (cond ((= (length args) 0)
3634 nil)
3635 ((= (length args) 1)
3636 (nth 0 args))
3637 ((and (= (length args) 2)
3638 (memq old args))
3639 (if (eq old (nth 0 args))
3640 (nth 1 args)
3641 (nth 0 args)))
3642 (t
3643 (while args
3644 (setq current (car args)
3645 args (cdr args))
3646 (setq choices
3647 (cons (cons (widget-apply current :menu-tag-get)
3648 current)
3649 choices)))
3650 (let ((val (completing-read prompt choices nil t)))
3651 (if (stringp val)
3652 (let ((try (try-completion val choices)))
3653 (when (stringp try)
3654 (setq val try))
3655 (cdr (assoc val choices)))
3656 nil)))))
3657 (if current
3658 (widget-prompt-value current prompt nil t)
3659 value)))
3660 \f
3661 (define-widget 'radio 'radio-button-choice
3662 "A union of several sexp types."
3663 :tag "Choice"
3664 :format "%{%t%}:\n%v"
3665 :prompt-value 'widget-choice-prompt-value)
3666
3667 (define-widget 'repeat 'editable-list
3668 "A variable length homogeneous list."
3669 :tag "Repeat"
3670 :format "%{%t%}:\n%v%i\n")
3671
3672 (define-widget 'set 'checklist
3673 "A list of members from a fixed set."
3674 :tag "Set"
3675 :format "%{%t%}:\n%v")
3676
3677 (define-widget 'boolean 'toggle
3678 "To be nil or non-nil, that is the question."
3679 :tag "Boolean"
3680 :prompt-value 'widget-boolean-prompt-value
3681 :button-prefix 'widget-push-button-prefix
3682 :button-suffix 'widget-push-button-suffix
3683 :format "%{%t%}: %[Toggle%] %v\n"
3684 :on "on (non-nil)"
3685 :off "off (nil)")
3686
3687 (defun widget-boolean-prompt-value (_widget prompt _value _unbound)
3688 ;; Toggle a boolean.
3689 (y-or-n-p prompt))
3690 \f
3691 ;;; The `color' Widget.
3692
3693 ;; Fixme: match
3694 (define-widget 'color 'editable-field
3695 "Choose a color name (with sample)."
3696 :format "%{%t%}: %v (%{sample%})\n"
3697 :value-create 'widget-color-value-create
3698 :size 10
3699 :tag "Color"
3700 :value "black"
3701 :completions (or facemenu-color-alist (defined-colors))
3702 :sample-face-get 'widget-color-sample-face-get
3703 :notify 'widget-color-notify
3704 :action 'widget-color-action)
3705
3706 (defun widget-color-value-create (widget)
3707 (widget-field-value-create widget)
3708 (widget-insert " ")
3709 (widget-create-child-and-convert
3710 widget 'push-button
3711 :tag " Choose " :action 'widget-color--choose-action)
3712 (widget-insert " "))
3713
3714 (defun widget-color--choose-action (widget &optional _event)
3715 (list-colors-display
3716 nil nil
3717 `(lambda (color)
3718 (when (buffer-live-p ,(current-buffer))
3719 (widget-value-set ',(widget-get widget :parent) color)
3720 (let* ((buf (get-buffer "*Colors*"))
3721 (win (get-buffer-window buf 0)))
3722 (bury-buffer buf)
3723 (and win (> (length (window-list)) 1)
3724 (delete-window win)))
3725 (pop-to-buffer ,(current-buffer))))))
3726
3727 (defun widget-color-sample-face-get (widget)
3728 (let* ((value (condition-case nil
3729 (widget-value widget)
3730 (error (widget-get widget :value)))))
3731 (if (color-defined-p value)
3732 (list (cons 'foreground-color value))
3733 'default)))
3734
3735 (defun widget-color-action (widget &optional event)
3736 "Prompt for a color."
3737 (let* ((tag (widget-apply widget :menu-tag-get))
3738 (prompt (concat tag ": "))
3739 (answer (facemenu-read-color prompt)))
3740 (unless (zerop (length answer))
3741 (widget-value-set widget answer)
3742 (widget-setup)
3743 (widget-apply widget :notify widget event))))
3744
3745 (defun widget-color-notify (widget child &optional event)
3746 "Update the sample, and notify the parent."
3747 (overlay-put (widget-get widget :sample-overlay)
3748 'face (widget-apply widget :sample-face-get))
3749 (widget-default-notify widget child event))
3750 \f
3751 ;;; The Help Echo
3752
3753 (defun widget-echo-help (pos)
3754 "Display help-echo text for widget at POS."
3755 (let* ((widget (widget-at pos))
3756 (help-echo (and widget (widget-get widget :help-echo))))
3757 (if (functionp help-echo)
3758 (setq help-echo (funcall help-echo widget)))
3759 (if help-echo (message "%s" (eval help-echo)))))
3760
3761 ;;; The End:
3762
3763 (provide 'wid-edit)
3764
3765 ;;; wid-edit.el ends here