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