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