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