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