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