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