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