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