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