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