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