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