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