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