1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
|
;;; working --- Display a "working" message in the minibuffer.
;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003,
;; 2004, 2007 Eric M. Ludlam
;; Author: Eric M. Ludlam <zappo@gnu.org>
;; Version: 1.5
;; Keywords: status
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; Working lets Emacs Lisp programmers easily display working messages.
;; These messages typically come in the form of a percentile, or generic
;; doodles if a maximum is unknown.
;;
;; The working entry points are quite simple. If you have a loop that needs
;; to display a status as it goes along, it would look like this:
;;
;; (working-status-forms "Doing stuff" "done"
;; (while condition
;; (working-status (calc-percentile))
;; (my-work))
;; (working-status t))
;;
;; If you cannot calculate a percentile, use the function
;; `working-dynamic-status' instead, and pass in what you know. For
;; both status printing functions, the first argument is optional,
;; and you may pass in additional arguments as `format' elements
;; to the first argument of `working-status-forms'.
;;
;; See the examples at the end of the buffer.
;;; Backwards Compatibility:
;;
;; If you want to use working in your program, but don't want to force people
;; to install working, use could add this at the beginning of your program for
;; compatibility.
;;
;; (eval-and-compile
;; (condition-case nil
;; (require 'working)
;; (error
;; (progn
;; (defmacro working-status-forms (message donestr &rest forms)
;; "Contain a block of code during which a working status is shown."
;; (list 'let (list (list 'msg message) (list 'dstr donestr)
;; '(ref1 0))
;; (cons 'progn forms)))
;;
;; (defun working-status (&optional percent &rest args)
;; "Called within the macro `working-status-forms', show the status."
;; (message "%s%s" (apply 'format msg args)
;; (if (eq percent t) (concat "... " dstr)
;; (format "... %3d%%"
;; (or percent
;; (floor (* 100.0 (/ (float (point))
;; (point-max)))))))))
;;
;; (defun working-dynamic-status (&optional number &rest args)
;; "Called within the macro `working-status-forms', show the status."
;; (message "%s%s" (apply 'format msg args)
;; (format "... %c" (aref [ ?- ?/ ?| ?\\ ] (% ref1 4))))
;; (setq ref1 (1+ ref1)))
;;
;; (put 'working-status-forms 'lisp-indent-function 2)))))
;;
;; Depending on what features you use, it is, of course, easy to
;; reduce the total size of the above by omitting those features you
;; do not use.
;;; History:
;;
;; 1.0 First Version
;;
;; 1.1 Working messages are no longer logged.
;; Added a generic animation display funciton:
;; Convert celeron to animator
;; Added a bounce display
;; Made working robust under a multi-frame environment (speedbar)
;;
;; 1.2 Fix up documentation.
;; Updated dotgrowth function for exceptionally large numbers of dots.
;; Added the percentage bubble displays.
;;
;; 1.3 Added `working-status-timeout' and `working-status-call-process'.
;; Added test fns `working-wait-for-keypress' and `working-verify-sleep'.
;;
;; 1.4 ???
;;
;; 1.5 Use features from the fame library.
;;
(require 'custom)
(require 'fame)
;;; Code:
(defgroup working nil
"Working messages display."
:prefix "working"
:group 'lisp
)
;;; User configurable variables
;;
(defcustom working-status-percentage-type 'working-bar-percent-display
"*Function used to display the percent status.
Functions provided in `working' are:
`working-percent-display'
`working-bar-display'
`working-bar-percent-display'
`working-percent-bar-display'
`working-bubble-display'
`working-bubble-precent-display'
`working-celeron-percent-display'"
:group 'working
:type '(choice (const working-percent-display)
(const working-bar-display)
(const working-bar-percent-display)
(const working-percent-bar-display)
(const working-bubble-display)
(const working-bubble-percent-display)
(const working-celeron-percent-display)
(const nil)))
(defcustom working-status-dynamic-type 'working-celeron-display
"*Function used to display an animation indicating progress being made.
Dynamic working types occur when the program does not know how long
it will take ahead of time. Functions provided in `working' are:
`working-number-display'
`working-text-display'
`working-spinner-display'
`working-dotgrowth-display'
`working-celeron-display'
`working-bounce-display'"
:group 'working
:type '(choice (const working-number-display)
(const working-text-display)
(const working-spinner-display)
(const working-dotgrowth-display)
(const working-celeron-display)
(const working-bounce-display)
(const nil)))
(defcustom working-percentage-step 2
"*Percentage display step.
A number representing how large a step must be taken when working a
percentage display. A number such as `2' means `2%'."
:group 'working'
:type 'number)
;;; Mode line hacks
;;
;; When the user doesn't want messages in the minibuffer, hack the mode
;; line of the current buffer.
(if (featurep 'xemacs)
(eval-and-compile (defalias 'working-mode-line-update 'redraw-modeline))
(eval-and-compile (defalias 'working-mode-line-update 'force-mode-line-update)))
(defvar working-mode-line-message nil
"Message used by working when showing status in the mode line.")
(if (boundp 'global-mode-string)
(progn
;; If this variable exists, use it to push the working message into
;; an interesting part of the mode line.
(if (null global-mode-string)
(setq global-mode-string (list "")))
(setq global-mode-string
(append global-mode-string '(working-mode-line-message))))
;; Else, use minor mode trickery to get a reliable way of doing the
;; same thing across many versions of Emacs.
(setq minor-mode-alist (cons
'(working-mode-line-message working-mode-line-message)
minor-mode-alist))
)
(defvar working-use-echo-area-p t
"*Non-nil use the echo area to display working messages.")
;;; Variables used in stages
;;
(defvar working-message nil
"Message stored when in a status loop.")
(defvar working-donestring nil
"Done string stored when in a status loop.")
(defvar working-ref1 nil
"A reference number used in a status loop.")
(defvar working-last-percent 0
"A reference number used in a status loop.")
;;; Programmer functions
;;
(eval-when-compile
(cond
((fboundp 'noninteractive)
;; Silence the XEmacs byte compiler
(defvar noninteractive))
((boundp 'noninteractive)
;; Silence the Emacs byte compiler
(defun noninteractive nil))
))
(defsubst working-noninteractive ()
"Return non-nil if running without interactive terminal."
(if (boundp 'noninteractive)
noninteractive
(noninteractive)))
(defun working-message-echo (&rest args)
"Print but don't log a one-line message at the bottom of the screen.
See the function `message' for details on ARGS."
(or (working-noninteractive)
(apply 'fame-message-nolog args)))
(defalias 'working-current-message 'fame-current-message)
(defalias 'working-temp-message 'fame-temp-message)
(defun working-message (&rest args)
"Display a message using `working-message-echo' or in mode line.
See the function `message' for details on ARGS."
(if working-use-echo-area-p
(apply 'working-message-echo args)
(when (not working-mode-line-message)
;; If we start out nil, put stuff in to show we are up to
(setq working-mode-line-message "Working...")
(working-mode-line-update)
(sit-for 0)
)))
;;; Compatibility
(cond ((fboundp 'run-with-timer)
(eval-and-compile (defalias 'working-run-with-timer 'run-with-timer))
(eval-and-compile (defalias 'working-cancel-timer 'cancel-timer))
)
;;Add compatibility here
(t
;; This gets the message out but has no timers.
(defun working-run-with-timer (&rest foo)
(working-message working-message))
(defun working-cancel-timer (&rest foo)
(working-message "%s%s"
working-message
working-donestring)))
)
(defmacro working-status-forms (message donestr &rest forms)
"Contain a block of code during which a working status is shown.
MESSAGE is the message string to use and DONESTR is the completed text
to use when the functions `working-status' is called from FORMS."
(let ((current-message (make-symbol "working-current-message")))
`(let ((,current-message (working-current-message))
(working-message ,message)
(working-donestring ,donestr)
(working-ref1 0)
(working-last-percent 0))
(unwind-protect
(progn ,@forms)
(setq working-mode-line-message nil)
(if working-use-echo-area-p
(message ,current-message)
(working-mode-line-update)
(sit-for 0))))
))
(put 'working-status-forms 'lisp-indent-function 2)
(defmacro working-status-timeout (timeout message donestr &rest forms)
"Contain a block of code during which working status is shown.
The code may call `sit-for' or `accept-process-output', so a timer
is needed to update the message.
TIMEOUT is the length of time to wait between message updates.
MESSAGE is the message string to use and DONESTR is the completed text
to use when the functions `working-status' is called from FORMS."
(let ((current-message (make-symbol "working-current-message")))
`(let* ((,current-message (working-current-message))
(working-message ,message)
(working-donestring ,donestr)
(working-ref1 0)
(time ,timeout)
(working-timer
(working-run-with-timer time time 'working-dynamic-status)))
(unwind-protect
(progn ,@forms)
(working-cancel-timer working-timer)
(working-dynamic-status t)
(setq working-mode-line-message nil)
(if working-use-echo-area-p
(message ,current-message)
(working-mode-line-update)
(sit-for 0))))
))
(put 'working-status-timeout 'lisp-indent-function 3)
(defun working-status-call-process
(timeout message donestr program &optional infile buffer display &rest args)
"Display working messages while running a process.
TIMEOUT is how fast to display the messages.
MESSAGE is the message to show, and DONESTR is the string to add when done.
CALLPROCESSARGS are the same style of args as passed to `call-process'.
The are: PROGRAM, INFILE, BUFFER, DISPLAY, and ARGS.
Since it actually calls `start-process', not all features will work."
(working-status-timeout timeout message donestr
(let ((proc (apply 'start-process "working"
(if (listp buffer) (car buffer) buffer)
program args)))
(set-process-sentinel proc 'list)
(while (eq (process-status proc) 'run)
(accept-process-output proc)
;; accept-process-output caused my solaris Emacs 20.3 to crash.
;; If this is unreliable for you, use the below which will work
;; in that situation.
;; (if (not (sit-for timeout)) (read-event))
))))
(defun working-status (&optional percent &rest args)
"Called within the macro `working-status-forms', show the status.
If PERCENT is nil, then calculate PERCENT from the value of `point' in
the current buffer. If it is a number or float, use it as the raw
percentile.
Additional ARGS are passed to fill on % elements of MESSAGE from the
macro `working-status-forms'."
(when (and working-message working-status-percentage-type)
(let ((p (or percent
(floor (* 100.0 (/ (float (point)) (point-max)))))))
(if (or (eq p t)
(> (- p working-last-percent) working-percentage-step))
(let* ((m1 (apply 'format working-message args))
(m2 (funcall working-status-percentage-type (length m1) p)))
(working-message "%s%s" m1 m2)
(setq working-last-percent p))))))
(defun working-dynamic-status (&optional number &rest args)
"Called within the macro `working-status-forms', show the status.
If NUMBER is nil, then increment a local NUMBER from 0 with each call.
If it is a number or float, use it as the raw percentile.
Additional ARGS are passed to fill on % elements of MESSAGE from the
macro `working-status-forms'."
(when (and working-message working-status-dynamic-type)
(let* ((n (or number working-ref1))
(m1 (apply 'format working-message args))
(m2 (funcall working-status-dynamic-type (length m1) n)))
(working-message "%s%s" m1 m2)
(setq working-ref1 (1+ working-ref1)))))
;;; Utilities
;;
(defun working-message-frame-width ()
"Return the width of the frame the working message will be in."
(let* ((mbw (cond ((fboundp 'frame-parameter)
(frame-parameter (selected-frame) 'minibuffer))
((fboundp 'frame-property)
(frame-property (selected-frame) 'minibuffer))))
(fr (if (windowp mbw)
(window-frame mbw)
default-minibuffer-frame)))
(frame-width fr)))
;;; Percentage display types.
;;
(defun working-percent-display (length percent)
"Return the percentage of the buffer that is done in a string.
LENGTH is the amount of display that has been used. PERCENT
is t to display the done string, or the percentage to display."
(cond ((eq percent t) (concat "... " working-donestring))
;; All the % signs because it then gets passed to message.
(t (format "... %3d%%" percent))))
(defun working-bar-display (length percent)
"Return a string with a bar-graph showing percent.
LENGTH is the amount of display that has been used. PERCENT
is t to display the done string, or the percentage to display."
(let ((bs (- (working-message-frame-width) length 5)))
(cond ((eq percent t)
(concat ": [" (make-string bs ?#) "] " working-donestring))
((< bs 0) "")
(t (let ((bsl (floor (* (/ percent 100.0) bs))))
(concat ": ["
(make-string bsl ?#)
(make-string (- bs bsl) ?.)
"]"))))))
(defun working-bar-percent-display (length percent)
"Return a string with a bar-graph and percentile showing percentage.
LENGTH is the amount of display that has been used. PERCENT
is t to display the done string, or the percentage to display."
(let* ((ps (if (eq percent t)
(concat "... " working-donestring)
(working-percent-display length percent)))
(psl (+ 2 length (if (eq percent t) working-ref1 (length ps)))))
(cond ((eq percent t)
(concat (working-bar-display psl 100) " " ps))
(t
(setq working-ref1 (length ps))
(concat (working-bar-display psl percent) " " ps)))))
(defun working-percent-bar-display (length percent)
"Return a string with a percentile and bar-graph showing percentage.
LENGTH is the amount of display that has been used. PERCENT
is t to display the done string, or the percentage to display."
(let* ((ps (if (eq percent t)
(concat "... " working-donestring)
(working-percent-display length percent)))
(psl (+ 1 length (if (eq percent t) working-ref1 (length ps)))))
(cond ((eq percent t)
(concat ps " " (working-bar-display psl 100)))
(t
(setq working-ref1 (length ps))
(concat ps " " (working-bar-display psl percent))))))
(defun working-bubble-display (length percent)
"Return a string with a bubble graph indicating the precent completed.
LENGTH is the amount of the display that has been used. PERCENT
is t to display the done string, or the percentage to display."
(if (eq percent t)
(concat " [@@@@@@@@@@@@@@@@@@@@] " working-donestring)
(let ((bs " [")
(bubbles [ ?. ?- ?o ?O ?@ ]))
(if (> percent 5)
(setq bs (concat bs (make-string (/ (floor percent) 5) ?@))))
(setq bs (concat bs
(char-to-string (aref bubbles (% (floor percent) 5)))))
(if (< (/ (floor percent) 5) 20)
(setq bs (concat bs (make-string (- 19 (/ (floor percent) 5)) ? ))))
(concat bs "]"))))
(defun working-bubble-percent-display (length percent)
"Return a string with a percentile and bubble graph showing percentage.
LENGTH is the amount of display that has been used. PERCENT
is t to display the done string, or the percentage to display."
(let* ((ps (if (eq percent t)
(concat " ... " working-donestring)
(working-percent-display length percent)))
(psl (+ 1 length (if (eq percent t) working-ref1 (length ps)))))
(cond ((eq percent t)
(concat (working-bubble-display psl t)))
(t
(setq working-ref1 (length ps))
(concat (working-bubble-display psl percent) ps)))))
(defun working-celeron-percent-display (length percent)
"Return a string with a celeron and string showing percent.
LENGTH is the amount of display that has been used. PERCENT
is t to display the done string, or the percentage to display."
(prog1
(cond ((eq percent t) (working-celeron-display length t))
;; All the % signs because it then gets passed to message.
(t (format "%s %3d%%"
(working-celeron-display length 0)
percent)))
(setq working-ref1 (1+ working-ref1))))
;;; Dynamic display types.
;;
(defun working-number-display (length number)
"Return a string displaying the number of things that happened.
LENGTH is the amount of display that has been used. NUMBER
is t to display the done string, or the number to display."
(cond ((eq number t) (concat "... " working-donestring))
;; All the % signs because it then gets passed to message.
(t (format "... %d" number))))
(defun working-text-display (length text)
"Return a string displaying the name of things that happened.
LENGTH is the amount of display that has been used. TEXT
is t to display the done string, or the text to display."
(if (eq text t)
(concat "... " working-donestring)
(format "... %s" text)))
(defun working-spinner-display (length number)
"Return a string displaying a spinner based on a number.
LENGTH is the amount of display that has been used. NUMBER
is t to display the done string, or the number to display."
(cond ((eq number t) (concat "... " working-donestring))
;; All the % signs because it then gets passed to message.
(t (format "... %c" (aref [ ?- ?/ ?| ?\\ ] (% working-ref1 4))))))
(defun working-dotgrowth-display (length number)
"Return a string displaying growing dots due to activity.
LENGTH is the amount of display that has been used. NUMBER
is t to display the done string, or the number to display.
This display happens to ignore NUMBER."
(let* ((width (- (working-message-frame-width) 4 length))
(num-wrap (/ working-ref1 width))
(num-. (% working-ref1 width))
(dots [ ?. ?, ?o ?* ?O ?@ ?# ]))
(concat " (" (make-string num-. (aref dots (% num-wrap (length dots)))) ")"
(if (eq number t) (concat " " working-donestring) ""))))
(defun working-frame-animation-display (length number frames)
"Manage a simple frame-based animation for working functions.
LENGTH is the number of characters left. NUMBER is a passed in
number (which happens to be ignored.). While coders pass t into
NUMBER, functions using this should convert NUMBER into a vector
describing how to render the done message.
Argument FRAMES are the frames used in the animation."
(cond ((vectorp number)
(let ((zone (- (length (aref frames 0)) (length (aref number 0))
(length (aref number 1)))))
(if (< (length working-donestring) zone)
(concat " " (aref number 0)
(make-string
(ceiling (/ (- (float zone)
(length working-donestring)) 2)) ? )
working-donestring
(make-string
(floor (/ (- (float zone)
(length working-donestring)) 2)) ? )
(aref number 1))
(concat " " (aref frames (% working-ref1 (length frames)))
" " working-donestring))))
(t (concat " " (aref frames (% working-ref1 (length frames)))))))
(defvar working-celeron-strings
[ "[O ]" "[oO ]" "[-oO ]" "[ -oO ]" "[ -oO ]" "[ -oO]"
"[ -O]" "[ O]" "[ Oo]" "[ Oo-]" "[ Oo- ]" "[ Oo- ]"
"[Oo- ]" "[O- ]"]
"Strings representing a silly celeron.")
(defun working-celeron-display (length number)
"Return a string displaying a celeron as things happen.
LENGTH is the amount of display that has been used. NUMBER
is t to display the done string, or the number to display."
(cond ((eq number t)
(working-frame-animation-display length [ "[" "]" ]
working-celeron-strings))
;; All the % signs because it then gets passed to message.
(t (working-frame-animation-display length number
working-celeron-strings))))
(defvar working-bounce-strings
[
"[_ ]"
"[ - ]"
"[ ~ ]"
"[ - ]"
"[ _ ]"
"[ - ]"
"[ ~ ]"
"[ - ]"
"[ _ ]"
"[ -]"
]
"Strings for the bounce animation.")
(defun working-bounce-display (length number)
"Return a string displaying a celeron as things happen.
LENGTH is the amount of display that has been used. NUMBER
is t to display the done string, or the number to display."
(cond ((eq number t)
(working-frame-animation-display length [ "[" "]" ]
working-bounce-strings))
;; All the % signs because it then gets passed to message.
(t (working-frame-animation-display length number
working-bounce-strings))))
;;; Some edebug hooks
;;
(add-hook
'edebug-setup-hook
(lambda ()
(def-edebug-spec working-status-forms (form form def-body))
(def-edebug-spec working-status-timeout (form form form def-body))))
;;; Example function using `working'
;;
(defun working-verify-parenthesis-a ()
"Verify all the parenthesis in an elisp program buffer."
(interactive)
(save-excursion
(goto-char (point-min))
(working-status-forms "Scanning" "done"
(while (not (eobp))
;; Use default buffer position.
(working-status)
(forward-sexp 1)
(sleep-for 0.05)
)
(working-status t))))
(defun working-verify-parenthesis-b ()
"Verify all the parenthesis in an elisp program buffer."
(interactive)
(save-excursion
(goto-char (point-min))
(working-status-forms "Scanning" "done"
(while (not (eobp))
;; Use default buffer position.
(working-dynamic-status nil)
(forward-sexp 1)
(sleep-for 0.05)
)
(working-dynamic-status t))))
(defun working-wait-for-keypress ()
"Display funny graphics while waiting for a keypress."
(interactive)
(working-status-timeout .1 "Press a key" "done"
(while (sit-for 10))))
(defun working-verify-sleep ()
"Display funny graphics while waiting for sleep to sleep."
(interactive)
(working-status-call-process .1 "Zzzzz" "Snort" "sleep" nil nil nil "5"))
(defun working-verify-mode-line ()
"Display graphics in the mode-line for timeout."
(interactive)
(let ((working-use-echo-area-p nil))
(message "Pres a Key")
(working-status-timeout .1 "" ""
(while (sit-for 10)))
))
(provide 'working)
;;; working.el ends here
|