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