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