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