]> code.delx.au - gnu-emacs/blobdiff - lisp/org/org-habit.el
Merge from origin/emacs-25
[gnu-emacs] / lisp / org / org-habit.el
index dd1bacdea71783079642f5293d9477a8963fc0a1..8848ac48b73534cc96ef38c518c2a04b6ad9f212 100644 (file)
@@ -1,11 +1,10 @@
 ;;; org-habit.el --- The habit tracking code for Org-mode
 
-;; Copyright (C) 2009, 2010, 2011 Free Software Foundation, Inc.
+;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
 
 ;; Author: John Wiegley <johnw at gnu dot org>
 ;; Keywords: outlines, hypermedia, calendar, wp
 ;; Homepage: http://orgmode.org
-;; Version: 6.33x
 ;;
 ;; This file is part of GNU Emacs.
 ;;
 
 ;; This file contains the habit tracking code for Org-mode
 
+;;; Code:
+
 (require 'org)
 (require 'org-agenda)
+
 (eval-when-compile
-  (require 'cl)
-  (require 'calendar))
+  (require 'cl))
 
 (defgroup org-habit nil
   "Options concerning habit tracking in Org-mode."
@@ -66,53 +67,77 @@ relative to the current effective date."
   :group 'org-habit
   :type 'boolean)
 
+(defcustom org-habit-show-all-today nil
+  "If non-nil, will show the consistency graph of all habits on
+today's agenda, even if they are not scheduled."
+  :group 'org-habit
+  :type 'boolean)
+
+(defcustom org-habit-today-glyph ?!
+  "Glyph character used to identify today."
+  :group 'org-habit
+  :version "24.1"
+  :type 'character)
+
+(defcustom org-habit-completed-glyph ?*
+  "Glyph character used to show completed days on which a task was done."
+  :group 'org-habit
+  :version "24.1"
+  :type 'character)
+
+(defcustom org-habit-show-done-always-green nil
+  "Non-nil means DONE days will always be green in the consistency graph.
+It will be green even if it was done after the deadline."
+  :group 'org-habit
+  :type 'boolean)
+
 (defface org-habit-clear-face
-  '((((background light)) (:background "slateblue"))
+  '((((background light)) (:background "#8270f9"))
     (((background dark)) (:background "blue")))
   "Face for days on which a task shouldn't be done yet."
   :group 'org-habit
   :group 'org-faces)
 (defface org-habit-clear-future-face
-  '((((background light)) (:background "powderblue"))
+  '((((background light)) (:background "#d6e4fc"))
     (((background dark)) (:background "midnightblue")))
   "Face for future days on which a task shouldn't be done yet."
   :group 'org-habit
   :group 'org-faces)
 
 (defface org-habit-ready-face
-  '((((background light)) (:background "green"))
+  '((((background light)) (:background "#4df946"))
     (((background dark)) (:background "forestgreen")))
   "Face for days on which a task should start to be done."
   :group 'org-habit
   :group 'org-faces)
 (defface org-habit-ready-future-face
-  '((((background light)) (:background "palegreen"))
+  '((((background light)) (:background "#acfca9"))
     (((background dark)) (:background "darkgreen")))
   "Face for days on which a task should start to be done."
   :group 'org-habit
   :group 'org-faces)
 
 (defface org-habit-alert-face
-  '((((background light)) (:background "yellow"))
+  '((((background light)) (:background "#f5f946"))
     (((background dark)) (:background "gold")))
   "Face for days on which a task is due."
   :group 'org-habit
   :group 'org-faces)
 (defface org-habit-alert-future-face
-  '((((background light)) (:background "palegoldenrod"))
+  '((((background light)) (:background "#fafca9"))
     (((background dark)) (:background "darkgoldenrod")))
   "Face for days on which a task is due."
   :group 'org-habit
   :group 'org-faces)
 
 (defface org-habit-overdue-face
-  '((((background light)) (:background "red"))
+  '((((background light)) (:background "#f9372d"))
     (((background dark)) (:background "firebrick")))
   "Face for days on which a task is overdue."
   :group 'org-habit
   :group 'org-faces)
 (defface org-habit-overdue-future-face
-  '((((background light)) (:background "mistyrose"))
+  '((((background light)) (:background "#fc9590"))
     (((background dark)) (:background "darkred")))
   "Face for days on which a task is overdue."
   :group 'org-habit
@@ -147,15 +172,17 @@ This list represents a \"habit\" for the rest of this module."
     (assert (org-is-habit-p (point)))
     (let* ((scheduled (org-get-scheduled-time (point)))
           (scheduled-repeat (org-get-repeat org-scheduled-string))
-          (sr-days (org-habit-duration-to-days scheduled-repeat))
           (end (org-entry-end-position))
-          (habit-entry (org-no-properties (nth 5 (org-heading-components))))
-          closed-dates deadline dr-days)
+          (habit-entry (org-no-properties (nth 4 (org-heading-components))))
+          closed-dates deadline dr-days sr-days)
       (if scheduled
          (setq scheduled (time-to-days scheduled))
        (error "Habit %s has no scheduled date" habit-entry))
       (unless scheduled-repeat
-       (error "Habit %s has no scheduled repeat period" habit-entry))
+       (error
+        "Habit `%s' has no scheduled repeat period or has an incorrect one"
+        habit-entry))
+      (setq sr-days (org-habit-duration-to-days scheduled-repeat))
       (unless (> sr-days 0)
        (error "Habit %s scheduled repeat period is less than 1d" habit-entry))
       (when (string-match "/\\([0-9]+[dwmy]\\)" scheduled-repeat)
@@ -166,10 +193,20 @@ This list represents a \"habit\" for the rest of this module."
                   habit-entry scheduled-repeat))
        (setq deadline (+ scheduled (- dr-days sr-days))))
       (org-back-to-heading t)
-      (while (re-search-forward "- State \"DONE\".*\\[\\([^]]+\\)\\]" end t)
-       (push (time-to-days
-              (org-time-string-to-time (match-string-no-properties 1)))
-             closed-dates))
+      (let* ((maxdays (+ org-habit-preceding-days org-habit-following-days))
+            (reversed org-log-states-order-reversed)
+            (search (if reversed 're-search-forward 're-search-backward))
+            (limit (if reversed end (point)))
+            (count 0))
+       (unless reversed (goto-char end))
+       (while (and (< count maxdays)
+                   (funcall search (format "- State \"%s\".*\\[\\([^]]+\\)\\]"
+                                           (regexp-opt org-done-keywords))
+                            limit t))
+         (push (time-to-days
+                (org-time-string-to-time (match-string-no-properties 1)))
+               closed-dates)
+         (setq count (1+ count))))
       (list scheduled sr-days deadline dr-days closed-dates))))
 
 (defsubst org-habit-scheduled (habit)
@@ -179,8 +216,10 @@ This list represents a \"habit\" for the rest of this module."
 (defsubst org-habit-deadline (habit)
   (let ((deadline (nth 2 habit)))
     (or deadline
-       (+ (org-habit-scheduled habit)
-          (1- (org-habit-scheduled-repeat habit))))))
+       (if (nth 3 habit)
+           (+ (org-habit-scheduled habit)
+              (1- (org-habit-scheduled-repeat habit)))
+         (org-habit-scheduled habit)))))
 (defsubst org-habit-deadline-repeat (habit)
   (or (nth 3 habit)
       (org-habit-scheduled-repeat habit)))
@@ -191,10 +230,7 @@ This list represents a \"habit\" for the rest of this module."
   "Determine the relative priority of a habit.
 This must take into account not just urgency, but consistency as well."
   (let ((pri 1000)
-       (now (time-to-days
-             (or moment
-                 (time-subtract (current-time)
-                                (list 0 (* 3600 org-extend-today-until) 0)))))
+       (now (if moment (time-to-days moment) (org-today)))
        (scheduled (org-habit-scheduled habit))
        (deadline (org-habit-deadline habit)))
     ;; add 10 for every day past the scheduled date, and subtract for every
@@ -244,8 +280,9 @@ Habits are assigned colors on the following basis:
       (if donep
          '(org-habit-ready-face . org-habit-ready-future-face)
        '(org-habit-alert-face . org-habit-alert-future-face)))
-     (t
-      '(org-habit-overdue-face . org-habit-overdue-future-face)))))
+     ((and org-habit-show-done-always-green donep)
+      '(org-habit-ready-face . org-habit-ready-future-face))
+     (t '(org-habit-overdue-face . org-habit-overdue-future-face)))))
 
 (defun org-habit-build-graph (habit starting current ending)
   "Build a graph for the given HABIT, from STARTING to ENDING.
@@ -281,15 +318,22 @@ current time."
                       donep)))
             markedp face)
        (if donep
-           (progn
-             (aset graph index ?*)
+           (let ((done-time (time-add
+                             starting
+                             (days-to-time
+                              (- start (time-to-days starting))))))
+
+             (aset graph index org-habit-completed-glyph)
              (setq markedp t)
+             (put-text-property
+              index (1+ index) 'help-echo
+              (format-time-string (org-time-stamp-format) done-time) graph)
              (while (and done-dates
                          (= start (car done-dates)))
                (setq last-done-date (car done-dates)
                      done-dates (cdr done-dates))))
          (if todayp
-             (aset graph index ?!)))
+             (aset graph index org-habit-today-glyph)))
        (setq face (if (or in-the-past-p todayp)
                       (car faces)
                     (cdr faces)))
@@ -305,6 +349,7 @@ current time."
 (defun org-habit-insert-consistency-graphs (&optional line)
   "Insert consistency graph for any habitual tasks."
   (let ((inhibit-read-only t) l c
+       (buffer-invisibility-spec '(org-link))
        (moment (time-subtract (current-time)
                               (list 0 (* 3600 org-extend-today-until) 0))))
     (save-excursion
@@ -316,13 +361,12 @@ current time."
            (delete-char (min (+ 1 org-habit-preceding-days
                                 org-habit-following-days)
                              (- (line-end-position) (point))))
-           (insert (org-habit-build-graph
-                    habit
-                    (time-subtract moment
-                                   (days-to-time org-habit-preceding-days))
-                    moment
-                    (time-add moment
-                              (days-to-time org-habit-following-days))))))
+           (insert-before-markers
+            (org-habit-build-graph
+             habit
+             (time-subtract moment (days-to-time org-habit-preceding-days))
+             moment
+             (time-add moment (days-to-time org-habit-following-days))))))
        (forward-line)))))
 
 (defun org-habit-toggle-habits ()
@@ -339,6 +383,4 @@ current time."
 
 (provide 'org-habit)
 
-;; arch-tag: 64e070d9-bd09-4917-bd44-44465f5ed348
-
 ;;; org-habit.el ends here