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