Skip to content

Instantly share code, notes, and snippets.

@pelagisk
Created December 7, 2011 18:01
Show Gist options
  • Select an option

  • Save pelagisk/1443850 to your computer and use it in GitHub Desktop.

Select an option

Save pelagisk/1443850 to your computer and use it in GitHub Desktop.
;;;; Skiss på tillägg till koll!
;; http://www.gnu.org/software/emacs/elisp/html_node/Change-Hooks.html#Change-Hooks
;; Author : Kristoffer Ström
;; License: public domain
;; very simple program to create a logfile of files worked on
;; can be used to keep track of work related projects for billing
;; just put this in elisp path and (require 'koll) in your config to have a go.
;; alist containing latest file writeout
(setf *koll-latest* nil
;; koll interval is the number of seconds that should pass between
;; log entries of the same file. default 10 minutes.
*koll-interval* (* 60 10)
;; this must exist
*koll-treshold* (* 60 2)
*koll-start-time* nil
*koll-last-changed* nil
*koll-logdir* "~/.koll"
*koll-filename* "%Y-%m"
*koll-timestamp* "\[%Y-%m-%d %H:%M\] ")
(unless (file-directory-p *koll-logdir*)
(if (file-exists-p *koll-logdir*)
(error "*koll-logdir* is a file!")
(make-directory *koll-logdir*)))
;(defun koll-hook ()
; (let* ((file (buffer-file-name))
; (time (string-to-number (format-time-string "%s")))
; (latest (aget *koll-latest* file)))
; (when (and file
; (not (and (fboundp 'tramp-tramp-file-p)
; (tramp-tramp-file-p file)))
; (or (not latest) (> (- time latest) *koll-interval*)))
; ;; write logfile
; (shell-command (concat "echo " (format-time-string *koll-timestamp*) file " >> " *koll-logdir* "/" (format-time-string *koll-filename*)))
; (aput '*koll-latest* file time)
; ;; from write-file-hooks doc
; ;; "If one of them returns non-nil, the file is considered already written
; ;; and the rest are not called."
; nil)))
(defun koll-start-hook ()
;; set current time as last changed time.
(setq *koll-last-changed* (string-to-number (format-time-string "%s")))
(if (> *koll-start-time* 0)
(nil)
;; if koll-start-time is nil or 0, set now as koll-start-time.
(setq *koll-start-time* *koll-last-changed*)))
(defun koll-stop-hook ()
;; sleep for treshold time
(sleep-for *koll-treshold*)
;; if time since last-changed larger than treshold, write log.
(if (<= (- (format-time-string "%s") *koll-last-changed*) *koll-treshold*)
(let* ((file (buffer-file-name))
(time (- (string-to-number (format-time-string "%s")) *koll-start-time*))
(latest (aget *koll-latest* file)))
(when (and file
(not (and (fboundp 'tramp-tramp-file-p)
(tramp-tramp-file-p file)))
(or (not latest) (> (- time latest) *koll-interval*)))
;; write logfile
(shell-command (concat "echo " () (format-time-string *koll-timestamp*) file " >> " *koll-logdir* "/" (format-time-string *koll-filename*)))
(aput '*koll-latest* file time)
;; from write-file-hooks doc
;; "If one of them returns non-nil, the file is considered already written
;; and the rest are not called."
nil)) nil))
;(add-hook 'write-file-hooks 'koll-hook)
(add-hook 'before-changes-functions 'koll-start-hook)
(add-hook 'after-changes-functions 'koll-stop-hook)
(provide 'koll)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment