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