Skip to content

Instantly share code, notes, and snippets.

@pervognsen
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pervognsen/32a0ecdcfaa6fb520de6 to your computer and use it in GitHub Desktop.
Save pervognsen/32a0ecdcfaa6fb520de6 to your computer and use it in GitHub Desktop.
emacscast.el
(require 'cl)
(require 'timer)
(defvar emacscast-events nil)
(defvar emacscast-recording-p nil)
(defvar emacscast-replay-events nil)
(defvar emacscast-replay-time nil)
(defun emacscast-forget-last-command ()
(when emacscast-recording-p
(pop emacscast-events)))
(defun emacscast-pre-command-hook ()
(when emacscast-recording-p
(push (list (current-time) (this-command-keys-vector) this-command) emacscast-events)))
(defun emacscast-schedule-replay-timer ()
(let ((timer (timer-create)))
(timer-set-function timer 'emacscast-replay-timer)
(timer-set-idle-time timer 0 nil)
(timer-activate-when-idle timer nil)))
(defun emacscast-replay-timer ()
(if emacscast-replay-events
(let* ((event (first emacscast-replay-events))
(time (first event))
(keys (second event))
(command (third event)))
(sleep-for (float-time (time-subtract time emacscast-replay-time)))
(setq emacscast-replay-events (rest emacscast-replay-events))
(setq emacscast-replay-time time)
(if (memq command '(universal-argument digit-argument negative-argument))
(emacscast-replay-timer) ;; Skip prefix keys since they are also in the complete command's key sequence.
(setq unread-command-events (append unread-command-events (listify-key-sequence keys)))
(emacscast-schedule-replay-timer)))
(message "Finished replaying.")))
(defun emacscast-mode ()
(interactive)
(add-hook 'pre-command-hook 'emacscast-pre-command-hook))
(defun emacscast-start-recording ()
(interactive)
(emacscast-forget-last-command)
(if emacscast-recording-p
(message "Already recording.")
(setq emacscast-recording-p t)
(setq emacscast-events nil)
(message "Recording...")))
(defun emacscast-cancel-recording ()
(interactive)
(emacscast-forget-last-command)
(if (not emacscast-recording-p)
(message "Not recording.")
(setq emacscast-recording-p nil)
(setq emacscast-events nil)
(message "Recording canceled.")))
(defun emacscast-stop-recording ()
(interactive)
(emacscast-forget-last-command)
(if (not emacscast-recording-p)
(message "Not recording.")
(setq emacscast-recording-p nil)
(setq emacscast-events (nreverse emacscast-events))
(message "Finished recording.")))
(defun emacscast-replay (&optional events)
(interactive)
(emacscast-forget-last-command)
(when emacscast-recording-p
(emacscast-stop-recording))
(message "Replaying...")
(setq emacscast-replay-events (or events emacscast-events))
(setq emacscast-replay-time (first (first emacscast-replay-events)))
(emacscast-schedule-replay-timer))
(provide 'emacscast)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment