;;; type-break.el --- encourage rests from typing at appropriate intervals
-;; Copyright (C) 1994, 95, 97, 2000, 2004 Free Software Foundation, Inc.
+;; Copyright (C) 1994, 1995, 1997, 2000, 2001, 2002, 2003,
+;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
;; Author: Noah Friedman
;; Maintainer: Noah Friedman <friedman@splode.com>
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs 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.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; 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., 59 Temple Place - Suite 330,
-;; Boston, MA 02111-1307, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
:group 'type-break)
(defcustom type-break-file-name (convert-standard-filename "~/.type-break")
- "*Name of file used to save state across sessions."
+ "*Name of file used to save state across sessions.
+If this is nil, no data will be saved across sessions."
:type 'file
:group 'type-break)
(and (interactive-p)
(message "Type Break mode is already enabled")))
(type-break-mode
- (with-current-buffer (find-file-noselect type-break-file-name 'nowarn)
- (setq buffer-save-without-query t))
+ (when type-break-file-name
+ (with-current-buffer (find-file-noselect type-break-file-name 'nowarn)
+ (setq buffer-save-without-query t)))
(or global-mode-string
(setq global-mode-string '("")))
(type-break-keystroke-reset)
(type-break-mode-line-countdown-or-break nil)
- (setq type-break-time-last-break (type-break-get-previous-time))
+ (setq type-break-time-last-break
+ (or (type-break-get-previous-time)
+ (current-time)))
;; schedule according to break time from session file
(type-break-schedule
(type-break-mode-line-countdown-or-break nil)
(type-break-cancel-schedule)
(do-auto-save)
- (with-current-buffer (find-file-noselect type-break-file-name
- 'nowarn)
- (set-buffer-modified-p nil)
- (unlock-buffer)
- (kill-this-buffer))
+ (when type-break-file-name
+ (with-current-buffer (find-file-noselect type-break-file-name
+ 'nowarn)
+ (set-buffer-modified-p nil)
+ (unlock-buffer)
+ (kill-this-buffer)))
(and (interactive-p)
(message "Type Break mode is disabled")))))
type-break-mode)
(defun type-break-file-time (&optional time)
"File break time in `type-break-file-name', unless the file is locked."
- (if (not (stringp (file-locked-p type-break-file-name)))
+ (if (and type-break-file-name
+ (not (stringp (file-locked-p type-break-file-name))))
(with-current-buffer (find-file-noselect type-break-file-name
'nowarn)
(let ((inhibit-read-only t))
(defun type-break-file-keystroke-count ()
"File keystroke count in `type-break-file-name', unless the file is locked."
- (if (not (stringp (file-locked-p type-break-file-name)))
+ (if (and type-break-file-name
+ (not (stringp (file-locked-p type-break-file-name))))
;; Prevent deactivation of the mark in some other buffer.
(let (deactivate-mark)
(with-current-buffer (find-file-noselect type-break-file-name
(defun type-break-choose-file ()
"Return file to read from."
(cond
+ ((not type-break-file-name)
+ nil)
((and (file-exists-p type-break-auto-save-file-name)
(file-readable-p type-break-auto-save-file-name))
type-break-auto-save-file-name)
(if file
(timep ;; returns expected format, else nil
(with-current-buffer (find-file-noselect file 'nowarn)
- (save-excursion
- (goto-char (point-min))
- (read (current-buffer))))))))
+ (condition-case nil
+ (save-excursion
+ (goto-char (point-min))
+ (read (current-buffer)))
+ (end-of-file
+ (error "End of file in `%s'" file))))))))
(defun type-break-get-previous-count ()
"Get previous keystroke count from `type-break-file-name'.
(setq file
(with-current-buffer
(find-file-noselect file 'nowarn)
- (save-excursion
- (goto-char (point-min))
- (forward-line 1)
- (read (current-buffer)))))))
+ (condition-case nil
+ (save-excursion
+ (goto-char (point-min))
+ (forward-line 1)
+ (read (current-buffer)))
+ (end-of-file
+ (error "End of file in `%s'" file)))))))
file
0)))
\f
;;; Demo wrappers
+(defun type-break-catch-up-event ()
+ ;; If the last input event is a down-event, read and discard the
+ ;; corresponding up-event too, to avoid triggering another prompt.
+ (and (eventp last-input-event)
+ (memq 'down (event-modifiers last-input-event))
+ (read-event)))
+
;; This is a wrapper around hanoi that calls it with an arg large enough to
;; make the largest discs possible that will fit in the window.
;; Also, clean up the *Hanoi* buffer after we're done.
(progn
(hanoi (/ (window-width) 8))
;; Wait for user to come back.
- (read-char)
+ (read-event)
+ (type-break-catch-up-event)
(kill-buffer "*Hanoi*"))
(quit
- ;; eat char
- (read-char)
+ (read-event)
+ (type-break-catch-up-event)
(and (get-buffer "*Hanoi*")
(kill-buffer "*Hanoi*")))))
(progn
(life 3)
;; wait for user to return
- (read-char)
+ (read-event)
+ (type-break-catch-up-event)
(kill-buffer "*Life*"))
(life-extinct
(message "%s" (get 'life-extinct 'error-message))
- (sit-for 3)
;; restart demo
(setq continue t))
(quit
+ (type-break-catch-up-event)
(and (get-buffer "*Life*")
(kill-buffer "*Life*")))))))
message))))
(goto-char (point-min))
(sit-for 60))
- (read-char)
+ (read-event)
+ (type-break-catch-up-event)
(kill-buffer buffer-name))
(quit
(and (get-buffer buffer-name)
(if type-break-mode
(type-break-mode 1))
-;;; arch-tag: 943a2eb3-07e6-420b-993f-96e4796f5fd0
+;; arch-tag: 943a2eb3-07e6-420b-993f-96e4796f5fd0
;;; type-break.el ends here