emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: James TD Smith <ahktenzero@mohorovi.cc>
To: emacs-orgmode@gnu.org
Subject: [PATCH 3/3] Add clock persistence.
Date: Thu, 23 Oct 2008 11:28:43 +0100	[thread overview]
Message-ID: <1224757723-32352-1-git-send-email-ahktenzero@mohorovi.cc> (raw)
In-Reply-To: <1224757662-32103-3-git-send-email-ahktenzero@mohorovi.cc>

Clock-related data are saved when exiting emacs ands restored when emacs
is restarted. The data saved include the contents of `org-clock-history',
and the running clock, if there is one.

To use this, you will need to add

(require 'org-clock)
(org-clock-persistence-insinuate)

to your .emacs and either add

(setq org-clock-persist t)
(setq org-clock-in-resume t)

or set those options to t in custom.

This patch requires the clock resume patch.
---
 lisp/ChangeLog    |   16 +++++++++
 lisp/org-clock.el |   90 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 106 insertions(+), 0 deletions(-)

diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 438296d..063ae15 100644
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -10,6 +10,22 @@
 	(org-clock-in): When clocking in to an entry, if
 	`org-clock-in-resume' is set, check if the first clock line is
 	open and if so, start the clock from the time in the clock line.
+	(org-clock-persist): Add a custom option to toggle clock
+	persistence.
+	(org-clock-persist-query-save): Add a custom option to toggle
+	asking the user if they want to save the running clock when
+	exiting.
+	(org-clock-persist-query-resume): Add a custom option to toggle
+	asking the user if they want to resume the saved clock when Emacs
+	is restarted.
+	(org-clock-save): Add a function to save clock data.
+	This includes the contents of `org-clock-history' and the buffer
+	and position of the currently clocked task, if any.
+	(org-clock-load): Add a function to load clock data.
+	This populates `org-clock-history', and resumes the saved clocked
+	task if there is one.
+	(org-clock-persistence-insinuate): Add a method to set up the
+	hooks for clock persistence.
 
 2008-10-22  Carsten Dominik  <dominik@science.uva.nl>
 
diff --git a/lisp/org-clock.el b/lisp/org-clock.el
index 40272d4..90b2992 100644
--- a/lisp/org-clock.el
+++ b/lisp/org-clock.el
@@ -101,6 +101,28 @@ has not been closed, resume the clock from that point"
   :group 'org-clock
   :type 'boolean)
 
+(defcustom org-clock-persist nil
+  "When non-nil, save the running clock when emacs is closed, and
+  resume it next time emacs is started."
+  :group 'org-clock
+  :type 'boolean)
+
+(defcustom org-clock-persist-file "~/.emacs.d/org-clock-save.el"
+  "File to save clock data to"
+  :group 'org-clock
+  :type 'string)
+
+(defcustom org-clock-persist-query-save nil
+  "When non-nil, ask before saving the current clock on exit"
+  :group 'org-clock
+  :type 'boolean)
+
+(defcustom org-clock-persist-query-resume t
+  "When non-nil, ask before resuming any stored clock during
+load."
+  :group 'org-clock
+  :type 'boolean)
+
 ;;; The clock for measuring work time.
 
 (defvar org-mode-line-string "")
@@ -989,6 +1011,74 @@ the currently selected interval size."
 			   lines)
 		   "\n"))))
 
+(defun org-clock-save ()
+  "Persist various clock-related data to disk"
+  (with-current-buffer (find-file (expand-file-name org-clock-persist-file))
+    (progn (delete-region (point-min) (point-max))
+	   ;;Store clock
+	   (insert (format ";; org-persist.el - %s at %s\n" system-name (time-stamp-string)))
+	   (if (and org-clock-persist (marker-buffer org-clock-marker)
+		    (or (not org-clock-persist-query-save)
+			(y-or-n-p (concat "Save current clock ("
+					  (substring-no-properties org-clock-heading)
+					  ")"))))
+	       (insert "(setq resume-clock '(\""
+		       (buffer-file-name (marker-buffer org-clock-marker))
+		       "\" . " (int-to-string (marker-position org-clock-marker))
+		       "))\n"))
+	   ;;Store clocked task history. Tasks are stored reversed to make
+	   ;;reading simpler
+	   (if org-clock-history
+	       (insert "(setq stored-clock-history '("
+		       (mapconcat
+			(lambda (m)
+			  (when (marker-buffer m)
+			    (concat "(\"" (buffer-file-name (marker-buffer m))
+				    "\" . " (int-to-string (marker-position m))
+				")")))
+			(reverse org-clock-history) " ") "))\n"))
+	   (save-buffer)
+	   (kill-buffer (current-buffer)))))
+
+(defvar org-clock-loaded nil)
+
+(defun org-clock-load ()
+  "Load various clock-related data from disk, optionally resuming
+a stored clock"
+  (if (not org-clock-loaded)
+      (let ((filename (expand-file-name org-clock-persist-file))
+	    (org-clock-in-resume t))
+	(if (file-readable-p filename)
+	    (progn
+	      (message "%s" "Restoring clock data")
+	      (setq org-clock-loaded t)
+	      (load-file filename)
+	      ;; load history
+	      (if (boundp 'stored-clock-history)
+		  (save-window-excursion
+		    (mapc (lambda (task)
+			    (org-clock-history-push (cdr task)
+						    (find-file (car task))))
+			  stored-clock-history)))
+	      ;; resume clock
+	      (if (and (boundp 'resume-clock) org-clock-persist
+		       (or (not org-clock-persist-query-resume)
+			   (y-or-n-p "Resume clock ("
+				     (with-current-buffer (find-file (car resume-clock))
+				       (progn (goto-char (cdr resume-clock))
+					      (looking-at org-complex-heading-regexp)
+					      (match-string 4))) ")")))
+		  (with-current-buffer (find-file (car resume-clock))
+		    (progn (goto-char (cdr resume-clock))
+			   (org-clock-in)))))
+	  (message "Not restoring clock data; %s not found"
+		   org-clock-persist-file)))))
+
+(defun org-clock-persistence-insinuate ()
+  "Set up hooks for clock persistence"
+  (add-hook 'org-mode-hook 'org-clock-load)
+  (add-hook 'kill-emacs-hook 'org-clock-save))
+
 (provide 'org-clock)
 
 ;; arch-tag: 7b42c5d4-9b36-48be-97c0-66a869daed4c
-- 
1.5.6.5

  parent reply	other threads:[~2008-10-23 10:28 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-10-19 16:22 [PATCH] Bugfix in org-add-log-setup James TD Smith
2008-10-19 16:22 ` [PATCH] Allow storing clock status between emacs sessions James TD Smith
2008-10-20 15:42   ` Helge Gudmundsen
2008-10-22  8:45     ` Helge Gudmundsen
2008-10-22  9:18       ` Carsten Dominik
2008-10-22  9:25         ` Helge Gudmundsen
2008-10-22  9:42         ` James TD Smith
2008-10-22 15:23           ` Carsten Dominik
2008-10-23 10:27             ` [PATCH 0/3] Split my clock resume/clock persistence patch James TD Smith
2008-10-23 10:27               ` [PATCH 1/3] Fix some typos and duplication in the ChangeLog James TD Smith
2008-10-23 10:27                 ` [PATCH 2/3] Add clock resuming James TD Smith
2008-10-23 10:27                   ` James TD Smith
2008-10-23 10:28                   ` James TD Smith [this message]
2008-10-28  7:33                     ` [PATCH 3/3] Add clock persistence Carsten Dominik
2008-10-28 11:10                       ` Helge Gudmundsen
2008-10-24  4:55                 ` [PATCH 1/3] Fix some typos and duplication in the ChangeLog Carsten Dominik
2008-10-20  5:46 ` [PATCH] Bugfix in org-add-log-setup Carsten Dominik

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1224757723-32352-1-git-send-email-ahktenzero@mohorovi.cc \
    --to=ahktenzero@mohorovi.cc \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).