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