View statistics, a calendar, and bar graphs of your habits in Emacs org-mode.
- Emacs 25.1
This package is available on MELPA as org-habit-stats
.
Run M-x package-refresh-contents
and M-x package-install
and type in the package name to install it.
Clone this repo to ~/.emacs.d/site-lisp/
.
Then add this to your config:
(load "org-habit-stats/org-habit-stats")
(package! org-habit-stats)
Use org-habit-stats-view-habit-at-point
inside org-mode files to view the stats of a habit.
Use org-habit-stats-view-habit-at-point-agenda
inside org-agenda buffers.
Bind these to keys of your choosing.
(define-key org-mode-map (kbd "C-c h") 'org-habit-stats-view-habit-at-point)
(define-key org-agenda-mode-map (kbd "H") 'org-habit-stats-view-habit-at-point-agenda)
The built-in keybindings for the org-habit-stats buffer are
Key | Command |
---|---|
q | org-habit-stats-exit |
, | org-habit-stats-view-previous-habit |
. | org-habit-stats-view-next-habit |
< | org-habit-stats-calendar-scroll-left |
> | org-habit-stats-calendar-scroll-right |
C-v | 'org-habit-stats-calendar-scroll-left-three-months |
M-v | org-habit-stats-calendar-scroll-right-three-months |
C-x ] | org-habit-stats-calendar-forward-year |
C-x [ | org-habit-stats-calendar-backward-year |
[ | org-habit-stats-scroll-graph-left |
] | org-habit-stats-scroll-graph-right |
} | org-habit-stats-scroll-graph-left-big |
{ | org-habit-stats-scroll-graph-right-big |
gm | Switch to monthly graph view |
gw | Switch to weekly graph view |
gd | Switch to weekday graph view |
gs | Switch to daily habit strength graph view |
To automatically add habit stats as properties of the habit item, use the following:
(add-hook 'org-after-todo-state-change-hook 'org-habit-stats-update-properties)
Based on the history of a habit, a string (called a "message") may be displayed on a given day. Messages can be used for rewards, encouraging messages, milestone indicators, and more.
Three message functions are provided. (See below for how to add your own message functions.)
org-habit-stats-streak-message
shows a message when the current habit streak reaches certain values. The values and messages can be customized in org-habit-stats-streak-message-alist
.
org-habit-stats-unstreak-message
shows a message when the current habit unstreak reaches certain values. The values and messages can be customized in org-habit-stats-unstreak-message-alist
.
org-habit-stats-comeback-message
shows a message when the current habit streak reaches the previous record habit streak. The message can be customized in org-habit-stats-comeback-message
.
You can add your own statistics, graph, and message functions. After writing your function, you must add it to the appropriate (a)list with the required information. (See below for more details.)
All three types of functions must have the same function signature:
(defun your-new-function (history history-rev habit-data))
history
contains a list of pairs (date . completed) where date is represented as the number of days since December 31, 1 BC (as is used by many org-mode functions) and completed is 1 if the habit was completed that day, and 0 otherwise.history-rev
is the reverse ofhistory
.habit-data
is the result of runningorg-habit-parse-todo
on a habit. From the docstring fororg-habit-parse-todo
:
Returns a list with the following elements:
0: Scheduled date for the habit (may be in the past)
1: \".+\"-style repeater for the schedule, in days
2: Optional deadline (nil if not present)
3: If deadline, the repeater for the deadline, otherwise nil
4: A list of all the past dates this todo was mark closed
5: Repeater type as a string
Statistics functions must return a number or string.
After creating your stat function, add it to org-habit-stats-stat-function-alist
along with a human readable name:
(add-to-list 'org-habit-stats-stat-function-alist
'(my-new-stat-function . "Stat Name Here"))
Message functions must return a string or nil. If it returns nil, no message will be displayed. This allows messages to be used for rewards, encouraging messages, etc.
After creating your message function, add it to org-habit-stats-message-function-list
:
(add-to-list 'org-habit-stats-message-function-list
'my-new-message-function)
Graph functions must return a pair (LABELS . VALUES)
, where LABELS is a list of strings to be used as bar labels and VALUES is a list of numbers to be used as the bar sizes.
After creating your graph function, add it to org-habit-stats-graph-function-list
.
(add-to-list 'org-habit-stats-graph-function-list
(my-new-graph-function . (:key "a"
:title "Title of bar graph here"
:x-label "x-axis label here"
:y-label "y-axis label here"
:dir horizontal
:max-bars 10)))
The dir
property can be either horizontal
or vertical
, and determines which direction the bar graphs are drawn.
The max-bars
property determines the maximum number of bars to appear in the graph at a time.
The Habit Strength score
The formula is
where
By default,
To calibrate it such that
I might try to get this package on ELPA, so any significant contributions (at least 15 LOC) requires copyright assignment to the FSF.
Powered by the wonderful built-in packages calendar.el and chart.el.
Inspired by the awesome free and open source app Loop Habit Tracker.
GPLv3