Skip navigation

Traveling with my kid I had some medication to give him the other night. I didn’t have an alarm clock with me, only my laptop computer. Into the Windows system I found nothing suited to help me gain some rest while waking up again at 3:00 am. I wished Emacs could help, and it certainly didn’t disappoint me once more. I found a simple alarm clock coded by Mathias Dahl here . Great!, I only had to tweak it a little by adding a sound load enough to ring me at the desired time. I picked a rooster sing, which I had collected a while ago when my child was getting to know farm animals. I put a loop around to repeat it a few times so to guarantee the awakening.
Anyways, for what is worth, here goes the cool little snippet:

(defvar alarm-clock-timer nil
  "Keep timer so that the user can cancel the alarm")

(defun alarm-clock-message (text)
  "The actual alarm action"
    (let((i 0))
      (while (< i 3)
        (play-sound-file "c:/web/sounds/rooster.wav")
        (setq i (1+ i)))
      (message-box text))))

(defun alarm-clock ()
  "Set an alarm.
The time format is the same accepted by `run-at-time'.  For
example \"11:30am\"."
  (let ((time (read-string "Time: "))
        (text (read-string "Alarm message: ")))
      (setq alarm-clock-timer 
          (run-at-time time nil 'alarm-clock-message text)))))

(defun alarm-clock-cancel ()
  "Cancel the alarm clock"
  (cancel-timer alarm-clock-timer))

UPDATE: see the small variation presented inside a comment for changing times on different days.



  1. Is there any way this could be extended to set different times for different days?

    • Sure that could be possible. If I get some time will try to find a way to do it.
      Now something like this will permit you to set different times for each day of the week;

      (require 'calendar)
      (setq text "Wake up dude, time to get up!")
      (setq time-to-get-up '(("Monday" . "7:00am")
                             ("Tuesday" . "7:20am")
                             ("Wednesday" . "6:50am")
                             ("Thursday" . "6:30am")
                             ("Friday" . "7:00am")
                             ("Saturday" . "9:00am")
                             ("Sunday" . nil)))
      (defun my-alarm-clock ()
        "Set an alarm.
      The time format is the same accepted by `run-at-time'.  For
      example \"11:30am\". You can provide your own defined in the list above."
            (setq currDay (calendar-day-name (calendar-current-date)))
            (setq time (cdr (assoc currDay time-to-get-up)))
            (setq alarm-clock-timer (run-at-time time nil 'alarm-clock-message text))))

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: