22 KiB
- Chronometrist
- How to…
- Explanation
- Reference
- Legend of currently-used time formats
- chronometrist-common.el
- chronometrist-custom.el
- chronometrist-diary-view.el
- chronometrist.el
- chronometrist-events.el
- chronometrist-migrate.el
- chronometrist-plist-pp.el
- chronometrist-queries.el
- chronometrist-report-custom.el
- chronometrist-report.el
- chronometrist-key-values.el
- chronometrist-statistics-custom.el
- chronometrist-statistics.el
- chronometrist-time.el
- chronometrist-timer.el
- chronometrist-goal
- chronometrist-sexp
Chronometrist
The structure of this manual was inspired by https://documentation.divio.com/
How to…
Setup Emacs to browse the project and contribute
I recommend using
- nameless-mode for easier reading of Emacs Lisp code, and
- visual-fill-column-mode to soft-wrap lines in Org/Markdown files.
org-indent-mode
(for Org files) and adaptive-prefix-mode (for Markdown and other files) will further enhance the experience.
Explanation
Design goals
-
Don't make assumptions about the user's profession
- e.g. timeclock seems to assume you're using it for a 9-to-5/contractor job
-
Incentivize use
- Hooks allow the time tracker to automate tasks and become a useful part of your workflow
-
Make it easy to edit data using existing, familiar tools
- We don't use an SQL database, where changing a single field is tricky [1]
- We use a text file containing s-expressions (easy for humans to read and write)
- We use ISO-8601 for timestamps (easy for humans to read and write) rather than UNIX epoch time
- Reduce human errors in tracking
- Have a useful, informative, interactive interface
- Support mouse and keyboard use equally
[1] I still have doubts about this. Having SQL as a query language would be very useful in perusing the stored data. Maybe we should have tried to create a companion mode to edit SQL databases interactively?
Terminology
For lack of a better term, events are how we refer to time intervals. They are stored as plists; each contains at least a :name "<name>"
, a :start "<iso-timestamp>"
, and (except in case of an ongoing task) a :stop "<iso-timestamp>"
.
Project overview
Chronometrist has three components, and each has a file containing major mode definitions and user-facing commands.
All three of these use (info "(elisp)Tabulated List Mode")
. Each of them also contains a "-print-non-tabular" function, which prints the non-tabular parts of the buffer.
Each of them has a corresponding -custom
file, which contain the Customize group and custom variable definitions for user-facing variables -
chronometrist-common.el contains definitions common to all components.
All three components use timers to keep their buffers updated. chronometrist-timer.el contains all timer-related code.
Note - sometimes, when hacking or dealing with errors, timers may result in subtle bugs which are very hard to debug. Using chronometrist-force-restart-timer
or restarting Emacs can fix them, so try that as a first sanity check.
Midnight-spanning events
A unique problem in working with Chronometrist, one I had never foreseen, was tasks which start on one day and end on another. These mess up data consumption (especially interval calculations and acquiring data for a specific date) in all sorts of unforeseen ways.
There are a few different approaches of dealing with them. (Currently, Chronometrist uses #3.)
1. (timeclock format) When the code of the first event in the day is "o", it's a midnight-spanning event.
- Advantage - very simple to detect
- Disadvantage - "in" and "out" events must be represented separately
2. Split them at the file level
- Advantage - operation is performed only once for each such event + simpler data-consuming code + reduced post-parsing load.
-
What happens when the user changes their day-start-time? The split-up events are now split wrongly, and the second event may get split again. Possible solutions -
- Add function to check if, for two events A and B, the :stop of A is the same as the :start of B, and that all their other tags are identical. Then we can re-split them according to the new day-start-time.
- Add a :split tag to split events. It can denote that the next event was originally a part of this one.
-
Re-check and update the file when the day-start-time changes.
- Possible with
add-variable-watcher
or:custom-set
in Customize (thanks bpalmer)
- Possible with
3. Split them at the hash-table-level
Handled by chronometrist-sexp-events-populate
- Advantage - simpler data-consuming code.
4. Split them at the data-consumer level (e.g. when calculating time for one day/getting events for one day)
- Advantage - reduced repetitive post-parsing load.
Point restore behaviour
After hacking, always test for and ensure the following -
- Toggling the buffer via
chronometrist=/=chronometrist-report=/=chronometrist-statistics
should preserve point - The timer function should preserve point when the buffer is current
- The timer function should preserve point when the buffer is not current, but is visible in another window
- The next/previous week keys and buttons should preserve point.
chronometrist-report date range logic
A quick description, starting from the first time chronometrist-report
is run in an Emacs session -
- We get the current date as a ts struct
(chronometrist-date)
. - The variable
chronometrist-report-week-start-day
stores the day we consider the week to start with. The default is "Sunday". We check if the date from #2 is on the week start day, else decrement it till we are, using(chronometrist-report-previous-week-start)
. - We store the date from #3 in the global variable
chronometrist-report--ui-date
. - By counting up from
chronometrist-report--ui-date
, we get dates for the days in the next 7 days using(chronometrist-report-date->dates-in-week)
. We store them inchronometrist-report--ui-week-dates
. The dates inchronometrist-report--ui-week-dates
are what is finally used to query the data displayed in the buffer. - To get data for the previous/next weeks, we decrement/increment the date in
chronometrist-report--ui-date
by 7 days and repeat the above process (via(chronometrist-report-previous-week)=/=(chronometrist-report-next-week)
).
Tags and Key-Values
chronometrist-key-values.el deals with adding additional information to events, in the form of key-values and tags.
Key-values are stored as plist keywords and values. The user can add any keywords except :name
, :tags
, :start
, and :stop
. 1 Values can be any readable Lisp values.
Similarly, tags are stored using a :tags (<tag>*)
keyword-value pair. The tags themselves (the elements of the list) can be any readable Lisp value.
The entry points are chronometrist-kv-add
and chronometrist-tags-add
. The user adds these to the desired hooks, and they prompt the user for tags/key-values.
Both have corresponding functions to create a prompt - chronometrist-key-prompt
, chronometrist-value-prompt
, and chronometrist-tags-prompt
.
The key-value prompt is somewhat different from most Emacs prompts - it creates a new buffer, and uses the minibuffer to alternatingly ask for keys and values in a loop. Key-values are inserted into the buffer as the user enters/selects them. The user can break out of this loop with an empty input (the keys to accept an empty input differ between completion systems, so we try to let the user know about them using chronometrist-kv-completion-quit-key
). After exiting the loop, they can use chronometrist-kv-accept
to accept the key-values (using chronometrist-append-to-last
to add them to the last plist in chronometrist-file
) or chronometrist-kv-reject
to discard them.
All prompts suggest past user inputs. These are queried from three history hash tables -, chronometrist-key-history
, chronometrist-value-history
, and chronometrist-tags-history
.
Reference
Legend of currently-used time formats
ts
ts.el struct
- Used by nearly all internal functions
iso-timestamp
"YYYY-MM-DDTHH:MM:SSZ"
- Used in the s-expression file format
- Read by chronometrist-sexp-events-populate
- Used in the plists in the chronometrist-events hash table values
iso-date
"YYYY-MM-DD"
- Used as hash table keys in chronometrist-events - can't use ts structs for keys, you'd have to make a hash table predicate which uses ts=
seconds
integer seconds as duration
- Used for most durations
- May be changed to floating point to allow larger durations. The minimum range of `most-positive-fixnum` is 536870911, which seems to be enough to represent durations of 17 years.
- Used for update intervals (chronometrist-update-interval, chronometrist-change-update-interval)
minutes
integer minutes as duration
- Used for goals (chronometrist-goals-list, chronometrist-get-goal) - minutes seems like the ideal unit for users to enter
list-duration
(hours minute seconds)
- Only returned by chronometrist-seconds-to-hms, called by chronometrist-format-time
chronometrist-common.el
- Variable - chronometrist-empty-time-string
- Variable - chronometrist-date-re
- Variable - chronometrist-time-re-ui
- Variable - chronometrist-task-list
- Function - chronometrist-task-list-add (task)
- Internal Variable - chronometrist–fs-watch
- Function - chronometrist-current-task ()
-
Function - chronometrist-format-time (seconds &optional (blank " "))
- seconds -> "hⓂ️s"
- Function - chronometrist-common-file-empty-p (file)
- Function - chronometrist-common-clear-buffer (buffer)
- Function - chronometrist-format-keybinds (command map &optional firstonly)
-
Function - chronometrist-events->ts-pairs (events)
- (plist …) -> ((ts . ts) …)
-
Function - chronometrist-ts-pairs->durations (ts-pairs)
- ((ts . ts) …) -> seconds
-
Function - chronometrist-previous-week-start (ts)
- ts -> ts
chronometrist-custom.el
- Custom variable - chronometrist-file
- Custom variable - chronometrist-buffer-name
- Custom variable - chronometrist-hide-cursor
- Custom variable - chronometrist-update-interval
- Custom variable - chronometrist-activity-indicator
- Custom variable - chronometrist-day-start-time
chronometrist-diary-view.el
- Variable - chronometrist-diary-buffer-name
- Internal Variable - chronometrist-diary–current-date
- Function - chronometrist-intervals-on (date)
- Function - chronometrist-diary-tasks-reasons-on (date)
- Function - chronometrist-diary-refresh (&optional ignore-auto noconfirm date)
- Major Mode - chronometrist-diary-view-mode
- Command - chronometrist-diary-view (&optional date)
chronometrist.el
- Internal Variable - chronometrist–task-history
- Internal Variable - chronometrist–point
- Internal Variable - chronometrist–inhibit-read-p
- Keymap - chronometrist-mode-map
- Command - chronometrist-open-log (&optional button)
- Function - chronometrist-common-create-file ()
-
Function - chronometrist-task-active? (task)
- String -> Boolean
- Function - chronometrist-use-goals? ()
- Function - chronometrist-activity-indicator ()
- Function - chronometrist-entries ()
- Function - chronometrist-task-at-point ()
- Function - chronometrist-goto-last-task ()
- Function - chronometrist-print-keybind (command &optional description firstonly)
- Function - chronometrist-print-non-tabular ()
- Function - chronometrist-goto-nth-task (n)
- Function - chronometrist-refresh (&optional ignore-auto noconfirm)
- Function - chronometrist-refresh-file (fs-event)
- Command - chronometrist-query-stop ()
- Command - chronometrist-in (task &optional _prefix)
- Command - chronometrist-out (&optional _prefix)
- Variable - chronometrist-before-in-functions
- Variable - chronometrist-after-in-functions
- Variable - chronometrist-before-out-functions
- Variable - chronometrist-after-out-functions
- Function - chronometrist-run-functions-and-clock-in (task)
- Function - chronometrist-run-functions-and-clock-out (task)
- Keymap - chronometrist-mode-map
- Major Mode - chronometrist-mode
- Function - chronometrist-toggle-task-button (button)
- Function - chronometrist-add-new-task-button (button)
- Command - chronometrist-toggle-task (&optional prefix inhibit-hooks)
- Command - chronometrist-toggle-task-no-hooks (&optional prefix)
- Command - chronometrist-add-new-task ()
- Command - chronometrist (&optional arg)
chronometrist-events.el
-
Variable - chronometrist-events
- keys - iso-date
-
Function - chronometrist-day-start (timestamp)
- iso-timestamp -> encode-time
-
Function - chronometrist-file-clean ()
- commented out, unused
- Function - chronometrist-events-maybe-split (event)
- Function - chronometrist-events-populate ()
- Function - chronometrist-tasks-from-table ()
- Function - chronometrist-events-add (plist)
- Function - chronometrist-events-replace-last (plist)
-
Function - chronometrist-events-subset (start end)
- ts ts -> hash-table
- Function - chronometrist-events-query-spec-match-p (plist specifiers)
- Function - chronometrist-events-query (table &key get specifiers except)
chronometrist-migrate.el
- Variable - chronometrist-migrate-table
- Function - chronometrist-migrate-populate (in-file)
- Function - chronometrist-migrate-timelog-file->sexp-file (&optional in-file out-file)
- Function - chronometrist-migrate-check ()
chronometrist-plist-pp.el
- Variable - chronometrist-plist-pp-keyword-re
- Variable - chronometrist-plist-pp-whitespace-re
- Function - chronometrist-plist-pp-longest-keyword-length ()
- Function - chronometrist-plist-pp-buffer-keyword-helper ()
- Function - chronometrist-plist-pp-buffer ()
- Function - chronometrist-plist-pp-to-string (object)
- Function - chronometrist-plist-pp (object &optional stream)
chronometrist-queries.el
-
Function - chronometrist-last ()
- -> plist
-
Function - chronometrist-task-time-one-day (task &optional (ts (ts-now)))
- String &optional ts -> seconds
-
Function - chronometrist-active-time-one-day (&optional ts)
- &optional ts -> seconds
- Function - chronometrist-statistics-count-active-days (task &optional (table chronometrist-events))
- Function - chronometrist-task-events-in-day (task ts)
chronometrist-report-custom.el
- Custom variable - chronometrist-report-buffer-name
- Custom variable - chronometrist-report-week-start-day
- Custom variable - chronometrist-report-weekday-number-alist
chronometrist-report.el
- Internal Variable - chronometrist-report–ui-date
- Internal Variable - chronometrist-report–ui-week-dates
- Internal Variable - chronometrist-report–point
- Function - chronometrist-report-date ()
-
Function - chronometrist-report-date->dates-in-week (first-date-in-week)
- ts-1 -> (ts-1 … ts-7)
- Function - chronometrist-report-date->week-dates ()
- Function - chronometrist-report-entries ()
- Function - chronometrist-report-print-keybind (command &optional description firstonly)
- Function - chronometrist-report-print-non-tabular ()
- Function - chronometrist-report-refresh (&optional _ignore-auto _noconfirm)
- Function - chronometrist-report-refresh-file (_fs-event)
- Keymap - chronometrist-report-mode-map
- Major Mode - chronometrist-report-mode
- Function - chronometrist-report (&optional keep-date)
- Function - chronometrist-report-previous-week (arg)
- Function - chronometrist-report-next-week (arg)
chronometrist-key-values.el
- Internal Variable - chronometrist–tag-suggestions
- Internal Variable - chronometrist–value-suggestions
- Function - chronometrist-plist-remove (plist &rest keys)
- Function - chronometrist-maybe-string-to-symbol (list)
- Function - chronometrist-maybe-symbol-to-string (list)
- Function - chronometrist-append-to-last (tags plist)
- Variable - chronometrist-tags-history
- Function - chronometrist-tags-history-populate ()
- Function - chronometrist-tags-history-add (plist)
- Function - chronometrist-tags-history-replace-last (plist)
- Function - chronometrist-tags-history-combination-strings (task)
- Function - chronometrist-tags-history-individual-strings (task)
- Function - chronometrist-tags-prompt (task &optional initial-input)
- Function - chronometrist-tags-add (&rest args)
- Custom Variable - chronometrist-kv-buffer-name
- Variable - chronometrist-key-history
- Variable - chronometrist-value-history
- Function - chronometrist-ht-history-prep (table)
- Function - chronometrist-key-history-populate ()
- Function - chronometrist-value-history-populate ()
- Keymap - chronometrist-kv-read-mode-map
- Major Mode - chronometrist-kv-read-mode
- Function - chronometrist-kv-completion-quit-key ()
- Function - chronometrist-string-has-whitespace-p (string)
- Function - chronometrist-key-prompt (used-keys)
- Function - chronometrist-value-prompt (key)
- Function - chronometrist-value-insert (value)
- Function - chronometrist-kv-add (&rest args)
- Command - chronometrist-kv-accept ()
- Command - chronometrist-kv-reject ()
chronometrist-statistics-custom.el
- Custom variable - chronometrist-statistics-buffer-name
chronometrist-statistics.el
- Internal Variable - chronometrist-statistics–ui-state
- Internal Variable - chronometrist-statistics–point
-
Function - chronometrist-statistics-count-average-time-spent (task &optional (table chronometrist-events))
- string &optional hash-table -> seconds
- Function - chronometrist-statistics-entries-internal (table)
- Function - chronometrist-statistics-entries ()
- Function - chronometrist-statistics-print-keybind (command &optional description firstonly)
- Function - chronometrist-statistics-print-non-tabular ()
- Function - chronometrist-statistics-refresh (&optional ignore-auto noconfirm)
- Keymap - chronometrist-statistics-mode-map
- Major Mode - chronometrist-statistics-mode
- Command - chronometrist-statistics (&optional preserve-state)
- Command - chronometrist-statistics-previous-range (arg)
- Command - chronometrist-statistics-next-range (arg)
chronometrist-time.el
-
Function - chronometrist-iso-timestamp->ts (timestamp)
- iso-timestamp -> ts
-
Function - chronometrist-iso-date->ts (date)
- iso-date -> ts
-
Function - chronometrist-date (&optional (ts (ts-now)))
- &optional ts -> ts (with time 00:00:00)
- Function - chronometrist-format-time-iso8601 (&optional unix-time)
- Function - chronometrist-midnight-spanning-p (start-time stop-time)
-
Function - chronometrist-seconds-to-hms (seconds)
- seconds -> list-duration
-
Function - chronometrist-interval (event)
- event -> duration
chronometrist-timer.el
- Internal Variable - chronometrist–timer-object
- Function - chronometrist-timer ()
- Command - chronometrist-stop-timer ()
- Command - chronometrist-maybe-start-timer (&optional interactive-test)
- Command - chronometrist-force-restart-timer ()
- Command - chronometrist-change-update-interval (arg)
chronometrist-goal
- Internal Variable - chronometrist-goal–timers-list
- Custom Variable - chronometrist-goal-list nil
- Function - chronometrist-goal-run-at-time (time repeat function &rest args)
-
Function - chronometrist-goal-seconds->alert-string (seconds)
- seconds -> string
-
Function - chronometrist-goal-approach-alert (task goal spent)
- string minutes minutes
-
Function - chronometrist-goal-complete-alert (task goal spent)
- string minutes minutes
-
Function - chronometrist-goal-exceed-alert (task goal spent)
- string minutes minutes
-
Function - chronometrist-goal-no-goal-alert (task goal spent)
- string minutes minutes
-
Custom Variable - chronometrist-goal-alert-functions
- each function is passed - string minutes minutes
-
Function - chronometrist-goal-get (task &optional (goal-list chronometrist-goal-list))
- String &optional List -> minutes
- Function - chronometrist-goal-run-alert-timers (task)
- Function - chronometrist-goal-stop-alert-timers (&optional _task)
- Function - chronometrist-goal-on-file-change ()
chronometrist-sexp
- Macro - chronometrist-sexp-in-file (file &rest body)
- Function - chronometrist-sexp-open-log ()
- Function - chronometrist-sexp-between (&optional (ts-beg (chronometrist-date)) (ts-end (ts-adjust 'day +1 (chronometrist-date))))
- Function - chronometrist-sexp-query-till (&optional (date (chronometrist-date)))
-
Function - chronometrist-sexp-last ()
- -> plist
- Function - chronometrist-sexp-current-task ()
- Function - chronometrist-sexp-events-populate ()
- Function - chronometrist-sexp-create-file ()
- Function - chronometrist-sexp-new (plist &optional (buffer (find-file-noselect chronometrist-file)))
- Function - chronometrist-sexp-delete-list (&optional arg)
- Function - chronometrist-sexp-replace-last (plist)
- Command - chronometrist-sexp-reindent-buffer ()
To remove this restriction, I had briefly considered making a keyword called :user
, whose value would be another plist containing all user-defined keyword-values. But in practice, this hasn't been a big enough issue yet to justify the work.