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