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