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