GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Bob Dylan Quotes+       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


meal-timer.el

    














;;; d-meal-timer.el --- A meal timer count-down system that improves over appt.el

;; Copyright (C) 2006-2011 Davin Pearson

;; Author/Maintainer: Davin Pearson http://davin.50webs.com
;; Keywords: Meal time
;; Version: 1.0

;;; Commentary:

;; This file is not part of GNU Emacs.

;; Have you ever sat programming on your computer only to realise that
;; the cooking on your stove top has been burnt for want of lack of
;; care of your cooking.  For those of you who are out there, this
;; system is for you.  You simply execute M-x meal-timer and the
;; minibuffer prompts you for a time to elapse before a sound sample
;; is played.  While the timer is counting down, the number of hours,
;; minutes and seconds remaining is continuously updated on the mode
;; line.  This is a superior replacement to the built-in appt.el
;; system.

;;; Limitation of Warranty

;; 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 3 of the License, 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 detail.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs, see the file COPYING.  If not, see:
;;
;; <http://www.gnu.org/licenses/gpl-3.0.txt>.


;;; Install Instructions:
;; See the following URL for the latest info and a tarball:
;;
;; <http://davin.50webs.com/research/2010/mopa2e2.html#meal-timer>
;;
;; Then untar the tarball to a folder pointed to by the Emacs variable
;; load-path and add the following line to your ~/.emacs file.
;;
;; (require 'meal-timer)

;;; Known Bugs:

;; none!

;;; Code:

(require 'd-time)

(progn
  (setq meal-timer--on nil)
  (setq meal-timer--stopped-time nil)
  (setq meal-timer--foo nil)
  (setq meal-timer--secs nil)
  (setq meal-timer--filename "~/.meal-timer.el")
  (setq meal-timer--added-mma nil)
  (setq meal-timer--timer nil)
  (setq meal-timer--paused nil)
  (setq meal-timer--last-command-pause nil)
  (setq meal-timer--last-command-on    nil)
  )

;;(defvar meal-timer--file "c:/music/already-burnt-8GB/B52s--Love-Shack.wma")
;;(defvar meal-timer--file "c:/music/mc-hammer--keep.wma")
;;(defvar meal-timer--file "c:/music/midi-150MB/Midi-Popular/B52s/Love-Shack.mid")
;;(defvar meal-timer--file "~/c++-projects/2007/R4/music/Africa.mid")

;; (d-seconds-to-readable-string 123 nil t)
;; (d-seconds-to-readable-string 12.3 nil t)
;; (meal-timer (setq minutes 5))
(defun meal-timer (minutes)
  (interactive "nEnter Meal Timer (minutes): ")
  (let ((seconds nil))
    ;;(if meal-timer--on
    ;;    (cancel-timer 'meal-timer--timer))
    (if (< minutes 0)
        (error "You cannot enter a negative time"))
    ;;(setq meal-timer--stopped-time (current-time))
    (setq seconds (* 60.0 minutes))
    (setq meal-timer--duration (d-seconds-to-readable-string seconds nil t))
    (setq meal-timer--secs seconds)
    ;;(setq foo (current-time))
    ;;(meal-timer--minutes-to-seconds 1 'foo)
    ;;(setq meal-timer (run-with-timer seconds nil 'meal-timer-function))
    ;;(d-assert (not 'meal-timer--on)) ;; (not meal-timer--on)
    (start-meal-timer seconds)
    ))

;; (d-with-buffer (setq buf meal-timer--filename) (setq resty '(progn (insert "abc"))))
;; (d-with-buffer buf rest)

(defmacro d-with-buffer (buf &rest x)
  `(save-excursion
     (find-file (eval ,buf))
     (goto-char (point-min))
     (progn ,@x)
     (save-buffer)
     (kill-buffer)
     t
     ))

;; (setq t1 (current-time))
;; (setq t2 (current-time))
;; (d-seconds-of-time-difference t1 t2)
;; (d-seconds-of-time-difference t2 t1)
(defun stop-meal-timer ()
  (interactive)
  (cond
   (meal-timer--last-command-on
    (setq meal-timer--last-command-on nil))
   (t
    (d-error "Tried to stop timer while timer is already stopped")))  ;;(if (not meal-timer--on)
  ;;    (d-beeps "Cannot stop meal timer as meal timer is off")
  (setq meal-timer--on nil)
  (setq meal-timer--secs ;;(- meal-timer--total-time-in-seconds
        (d-seconds-of-time-difference
         meal-timer--stopped-time (current-time)))
  (if meal-timer--timer (cancel-timer meal-timer--timer))
  (setq meal-timer--stopped-time nil)
  (setq meal-timer--foo nil)
  ;;(d-assert (file-exists-p meal-timer--filename))
  (when (file-exists-p meal-timer--filename)
    (delete-file meal-timer--filename))
  )

;; (start-meal-timer 0.1)
(defun start-meal-timer (secs)
  (interactive)
  (cond
   ((not meal-timer--last-command-on)
    (setq meal-timer--last-command-on t))
   (t
    (d-error "Tried to start timer while timer is already started")))
  (setq meal-timer--on t)
  ;;(setq meal-timer--total-time-in-seconds meal-timer--secs)
  (setq meal-timer--stopped-time (current-time))
  (setq meal-timer--stopped-time (d-add-seconds secs meal-timer--stopped-time))
  ;; (d-decode-time meal-timer--stopped-time)
  ;;(d-debug "Secs")
  (setq meal-timer--secs secs)
  (setq meal-timer--duration (d-seconds-to-readable-string meal-timer--secs nil t))
  ;;(message "**** set meal-timer--duration")
  (setq meal-timer--timer (run-with-timer meal-timer--secs nil 'meal-timer-function))
  ;;(d-debug "Bruce Springsteen / Dancing in the Streets")
  (meal-timer-save)
  )

;;(setq meal-timer--total-time-in-seconds seconds)

(defun meal-timer-function ()
  ;;(d-nbeeps 5 "Time is up!")
  ;; NOTE: Better replacement for sit-for
  (stop-meal-timer)
  (setq meal-timer--on           nil)
  (setq meal-timer--stopped-time nil)
  (setq meal-timer--secs         nil)
  (setq meal-timer--foo          nil)
  (setq meal-timer--paused       nil)
  (cancel-timer meal-timer--timer)
  (if (fboundp 'd-fonts) (d-fonts))
  (sit-for 1)
  (let (f)
    (setq f "completed-meal-timer.wav")
    (if (file-exists-p f)
        (play-sound (list 'sound :file f :volume .99)))
    (setq f "c:/sound-samples/emacs/completed-meal-timer.wav")
    (if (file-exists-p f)
        (play-sound (list 'sound :file f :volume .99)))
    (setq f "/media/www/C1TB/sound-samples/emacs/completed-meal-timer.wav")
    (if (file-exists-p f)
        (play-sound (list 'sound :file f :volume 1.0)))
    )
  (d-beeps "Meal timer expired at %s elasped=%s" (d-time--get-stamp) meal-timer--duration)
  (if (fboundp 'd-fonts) (d-fonts))
  (redraw-display)
  )

;;(setq minor-mode-alist (cons '(meal-timer--on " MEAL-TIMER") minor-mode-alist))
;;(setq minor-mode-alist (cons '(meal-timer--on (:eval (meal-timer-get-string))) minor-mode-alist))
(when (not meal-timer--added-mma)
  (setq minor-mode-alist (cons '(:eval (meal-timer-get-string)) minor-mode-alist))
  (setq meal-timer--added-mma t))

(defun meal-timer-save ()
  (d-with-buffer
   meal-timer--filename
   (erase-buffer)
   (goto-char (point-min))
   ;;(setq meal-timer--secs (d-seconds-of-time-difference (current-time) meal-timer--stopped-time))
   (insert "(progn\n"
           ;;"(setq meal-timer--secs " (format "%s" (round meal-timer--secs)) ")\n")
           )
   (insert "  (setq meal-timer--stopped-time " (prin1-to-string (d-decode-time meal-timer--stopped-time)) ")\n")
   (insert "  (setq meal-timer--on nil)\n")
   (insert "  (setq meal-timer--paused " (format "%s" meal-timer--paused) "))\n")
   ))

(defun pause-meal-timer ()
  (interactive)
  (cond
   ((and (not meal-timer--last-command-pause) meal-timer--last-command-on)
    (setq meal-timer--last-command-pause t))
   ((not meal-timer--last-command-on)
    (d-error "Tried to pause while timer is off"))
   (t
    (d-error "Tried to pause while last pause is false")))
  (when meal-timer--timer
    (d-assert (timerp meal-timer--timer))
    (cancel-timer meal-timer--timer))
  ;;(setq meal-timer--stopped-time nil)
  (setq meal-timer--secs
        (d-seconds-of-time-difference (current-time) meal-timer--stopped-time))
  (setq meal-timer--on nil)
  (setq meal-timer--foo (concat " Paused:" meal-timer--foo))
  )

;; (unpause-meal-timer)
(defun unpause-meal-timer (&optional not-do-run-timer)
  (interactive)
  (cond
   ((and meal-timer--last-command-pause meal-timer--last-command-on)
    (setq meal-timer--last-command-pause t))
   ((not meal-timer--last-command-on)
    (d-error "Tried to unpause while timer is off"))
   (t
    (d-error "Tried to unpause while last pause is true")))
  (when (not not-do-run-timer)
    (setq meal-timer (run-with-timer meal-timer--secs nil 'meal-timer-function)))
  (setq meal-timer--stopped-time (d-add-seconds meal-timer--secs (current-time)))
  (setq meal-timer--on t)
  ;;(setq meal-timer--foo (meal-timer-get-string-inner))
  (when (file-exists-p meal-timer--filename)
    (delete-file meal-timer--filename))
  )

(defun meal-timer-get-string-inner ()
  (let* ((count (d-seconds-of-time-difference (current-time) meal-timer--stopped-time))
         (count (floor count))
         (str (d-seconds-to-readable-string count nil t)))
    (format " Timer=(%s/%s)" str meal-timer--duration)))

(defun meal-timer-get-string ()
  (if meal-timer--on
      (setq meal-timer--foo (meal-timer-get-string-inner))
    meal-timer--foo)
    )

(run-with-timer 1 1 'force-mode-line-update)

;;(run-with-idle-timer 10 nil 'd-foo)

(defun meal-timer-init ()
  (when (file-exists-p meal-timer--filename)
    (message "**** ran meal-timer-init")
    (setq meal-timer--stopped-time nil)
    (setq meal-timer--secs nil)
    (setq meal-timer--on nil)
    (setq meal-timer--paused nil)
    (setq ready-to-start nil)
    (load-file meal-timer--filename)
    ;;(d-debug "Joni Mitchell / In Morning Morgantown")
    ;;(d-assert meal-timer--stopped-time)
    ;;(d-assert (stringp meal-timer--stopped-time))
    ;;(d-debug "Calamansi")
    (when (and meal-timer--stopped-time (stringp meal-timer--stopped-time))
      (setq meal-timer--stopped-time
            (d-encode-time meal-timer--stopped-time))
      (setq secs (d-seconds-of-time-difference (current-time) meal-timer--stopped-time))
      (setq meal-timer--secs secs)
      (setq ready-to-start t))
    (when meal-timer--secs
      (setq secs meal-timer--secs)
      (setq meal-timer--stopped-time (d-add-seconds secs (current-time)))
      (setq ready-to-start t))
    (setq meal-timer--duration (d-seconds-to-readable-string secs nil t))
    (if (and ready-to-start
             (> secs 0)
             (time-newer-than-time meal-timer--stopped-time (current-time)))
        (start-meal-timer secs)
      (setq meal-timer--on nil))
    (if meal-timer--paused
        (pause-meal-timer)
      (unpause-meal-timer t))
    )
  )

(meal-timer-init)

;;;(d-assert (boundp 'secs))
;;;(setq meal-timer--secs secs)

(defadvice kill-emacs (before meal-timer activate)
  (let (t1)
    (when (and (boundp 'meal-timer--secs)
               meal-timer--secs)
      (d-with-buffer
       meal-timer--filename
       (progn
         (setq t1 (current-time))
         (d-assert (boundp 'meal-timer--secs))
         (setq t1 (d-add-seconds meal-timer--secs t1))
         (erase-buffer)
         (insert "(progn\n"
                 "  (setq meal-timer--stopped-time " (prin1-to-string
                                                      (d-decode-time t1)) ")\n"
                 "  (setq meal-timer--on t))\n")))
      )))

(provide 'meal-timer)

(d-quote
 (progn
   (setq mtr (run-with-timer 10 10 'd-foo))
   (cancel-timer mtr)
   (timerp mtr))
 )
Back
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Bob Dylan Quotes+ | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Mon May 6 01:56:46 NZST 2019
Best viewed at 800x600 or above resolution.
© Copyright 1999-2019 Davin Pearson.
Please report any broken links to