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