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