Move more functions to -sexp

New
chronometrist-last
chronometrist-sexp-last
chronometrist-sexp-replace-last

Renamed
chronometrist-append-to-last-expr -> chronometrist-append-to-last

Moved/renamed
chronometrist-reindent-buffer -> chronometrist-sexp-reindent-buffer
This commit is contained in:
contrapunctus 2020-05-14 15:21:27 +05:30
parent 395656d03a
commit c63815ea72
7 changed files with 78 additions and 64 deletions

View File

@ -21,8 +21,6 @@
(require 'chronometrist-time)
(require 'chronometrist-sexp)
(declare-function chronometrist-sexp-last "chronometrist-sexp")
;; ## VARIABLES ##
;;; Code:

View File

@ -60,7 +60,6 @@ This is not guaranteed to be accurate - see (info \"(elisp)Timers\")."
:type 'integer)
(declare-function chronometrist-format-time "chronometrist-common")
(declare-function chronometrist-sexp-last "chronometrist-sexp")
(eval-when-compile (require 'subr-x))
(defcustom chronometrist-activity-indicator "*"

View File

@ -208,7 +208,7 @@ To use, add this to `chronometrist-after-out-functions', and
(defun chronometrist-goals-on-file-change ()
"Manage timed alerts when `chronometrist-file' changes."
(let ((last (chronometrist-sexp-last)))
(let ((last (chronometrist-last)))
(chronometrist-goals-stop-alert-timers)
;; if there's a task running, start timed alerts for it
(unless (plist-get last :stop)

View File

@ -60,61 +60,40 @@ Used as history by `chronometrist--value-suggestions'.")
it)
list))
(defun chronometrist-reindent-buffer ()
"Reindent the current buffer.
This is meant to be run in `chronometrist-file'."
(interactive)
(let (expr)
(goto-char (point-min))
(while (setq expr (ignore-errors (read (current-buffer))))
(backward-list)
(chronometrist-delete-list)
(when (looking-at "\n*")
(delete-region (match-beginning 0)
(match-end 0)))
(chronometrist-plist-pp expr (current-buffer))
(insert "\n")
(unless (eobp)
(insert "\n")))))
(defun chronometrist-append-to-last-expr (tags plist)
"Add TAGS and PLIST to last s-expression in `chronometrist-file'.
(defun chronometrist-append-to-last (tags plist)
"Add TAGS and PLIST to last entry in `chronometrist-file'.
TAGS should be a list of symbols and/or strings.
PLIST should be a property list. Properties reserved by
Chronometrist - currently :name, :tags, :start, and :stop - will
be removed."
(let* ((old-expr (chronometrist-sexp-last))
(old-name (plist-get old-expr :name))
(old-start (plist-get old-expr :start))
(old-stop (plist-get old-expr :stop))
(old-tags (plist-get old-expr :tags))
(old-kvs (chronometrist-plist-remove old-expr :name :tags :start :stop))
(plist (chronometrist-plist-remove plist :name :tags :start :stop))
(new-tags (if old-tags
(-> (append old-tags tags)
(cl-remove-duplicates :test #'equal))
tags))
(new-kvs (cl-copy-list old-expr))
(new-kvs (if plist
(-> (cl-loop for (key val) on plist by #'cddr
do (plist-put new-kvs key val)
finally return new-kvs)
(chronometrist-plist-remove :name :tags :start :stop))
old-kvs))
(buffer (find-file-noselect chronometrist-file)))
(with-current-buffer buffer
(goto-char (point-max))
(backward-list)
(chronometrist-delete-list)
(-> (append `(:name ,old-name)
(when new-tags `(:tags ,new-tags))
new-kvs
`(:start ,old-start)
(when old-stop `(:stop ,old-stop)))
(chronometrist-plist-pp buffer))
(save-buffer))))
be removed.
Return a new plist which should be appended to the last entry."
(let* ((old-expr (chronometrist-last))
(old-name (plist-get old-expr :name))
(old-start (plist-get old-expr :start))
(old-stop (plist-get old-expr :stop))
(old-tags (plist-get old-expr :tags))
(old-kvs (chronometrist-plist-remove old-expr :name :tags :start :stop))
(plist-clean (chronometrist-plist-remove plist :name :tags :start :stop))
(new-tags (if old-tags
(-> (append old-tags tags)
(cl-remove-duplicates :test #'equal))
tags))
(new-kvs (cl-copy-list old-expr))
(new-kvs (if plist-clean
(-> (cl-loop for (key val) on plist-clean by #'cddr
do (plist-put new-kvs key val)
finally return new-kvs)
(chronometrist-plist-remove :name :tags :start :stop))
old-kvs))
(plist (append `(:name ,old-name)
(when new-tags `(:tags ,new-tags))
new-kvs
`(:start ,old-start)
(when old-stop `(:stop ,old-stop)))))
(chronometrist-sexp-replace-last plist)))
;;;; TAGS ;;;;
(defvar chronometrist-tags-history (make-hash-table :test #'equal)
@ -194,7 +173,7 @@ INITIAL-INPUT is as used in `completing-read'."
(defun chronometrist-tags-add (&rest _args)
"Read tags from the user, add them to the last s-expr in `chronometrist-file'.
_ARGS are ignored. This function always returns t."
(let* ((last-expr (chronometrist-sexp-last))
(let* ((last-expr (chronometrist-last))
(last-name (plist-get last-expr :name))
(last-tags (plist-get last-expr :tags))
(input (->> last-tags
@ -208,7 +187,7 @@ _ARGS are ignored. This function always returns t."
(reverse)
(cl-remove-duplicates :test #'equal)
(reverse)
(chronometrist-append-to-last-expr nil)))
(chronometrist-append-to-last nil)))
t))
;;;; KEY-VALUES ;;;;
@ -386,7 +365,7 @@ It currently supports ido, ido-ubiquitous, ivy, and helm."
"Prompt the user to enter keys.
USED-KEYS are keys they have already added since the invocation
of `chronometrist-kv-add'."
(let ((key-suggestions (--> (chronometrist-sexp-last)
(let ((key-suggestions (--> (chronometrist-last)
(plist-get it :name)
(gethash it chronometrist-key-history))))
(completing-read (concat "Key ("
@ -437,7 +416,7 @@ _ARGS are ignored. This function always returns t, so it can be
used in `chronometrist-before-out-functions'."
(let* ((buffer (get-buffer-create chronometrist-kv-buffer-name))
(first-key-p t)
(last-kvs (chronometrist-plist-remove (chronometrist-sexp-last)
(last-kvs (chronometrist-plist-remove (chronometrist-last)
:name :tags :start :stop))
(used-keys (->> (seq-filter #'keywordp last-kvs)
(mapcar #'symbol-name)
@ -471,7 +450,7 @@ used in `chronometrist-before-out-functions'."
(if (string-empty-p input)
(throw 'empty-input nil)
(chronometrist-value-insert value)))))
(chronometrist-reindent-buffer)))
(chronometrist-sexp-reindent-buffer)))
t)

View File

@ -20,6 +20,10 @@
(require 'chronometrist-common)
(require 'chronometrist-events)
(defun chronometrist-last ()
"Return the last entry from `chronometrist-file' as a plist."
(chronometrist-sexp-last))
(defun chronometrist-task-time-one-day (task &optional date-string)
"Return total time spent on TASK today or (if supplied) on DATE-STRING.
The data is obtained from `chronometrist-file', via `chronometrist-events'.

View File

@ -9,6 +9,7 @@
(with-current-buffer (find-file-noselect chronometrist-file)
(write-file chronometrist-file))))
;;;; Queries
(defun chronometrist-sexp-last ()
"Return last s-expression from `chronometrist-file'."
(let ((buffer (find-file-noselect chronometrist-file)))
@ -26,6 +27,34 @@
nil
(plist-get last-event :name))))
;;;; Modifications
(defun chronometrist-sexp-replace-last (plist)
"Replace the last s-expression in `chronometrist-file' with plist."
(let ((buffer (find-file-noselect chronometrist-file)))
(with-current-buffer buffer
(goto-char (point-max))
(backward-list)
(chronometrist-delete-list)
(chronometrist-plist-pp plist buffer)
(save-buffer))))
(defun chronometrist-sexp-reindent-buffer ()
"Reindent the current buffer.
This is meant to be run in `chronometrist-file' when using the s-expression backend."
(interactive)
(let (expr)
(goto-char (point-min))
(while (setq expr (ignore-errors (read (current-buffer))))
(backward-list)
(chronometrist-delete-list)
(when (looking-at "\n*")
(delete-region (match-beginning 0)
(match-end 0)))
(chronometrist-plist-pp expr (current-buffer))
(insert "\n")
(unless (eobp)
(insert "\n")))))
(provide 'chronometrist-sexp)
;;; chronometrist-sexp.el ends here

View File

@ -154,12 +154,14 @@
6. Function - chronometrist-plist-pp-to-string (object)
7. Function - chronometrist-plist-pp (object &optional stream)
*** chronometrist-queries.el
1. Function - chronometrist-task-time-one-day (task &optional date-string)
1. Function - chronometrist-last ()
* -> plist
2. Function - chronometrist-task-time-one-day (task &optional date-string)
* String &optional iso-date -> seconds
2. Function - chronometrist-active-time-one-day (&optional date-string)
3. Function - chronometrist-active-time-one-day (&optional date-string)
* &optional iso-date -> vector-duration
3. Function - chronometrist-statistics-count-active-days (task &optional table)
4. Function - chronometrist-task-events-in-day (task date-string)
4. Function - chronometrist-statistics-count-active-days (task &optional table)
5. Function - chronometrist-task-events-in-day (task date-string)
*** chronometrist-report-custom.el
1. Custom variable - chronometrist-report-buffer-name
2. Custom variable - chronometrist-report-week-start-day
@ -189,7 +191,7 @@
4. Function - chronometrist-maybe-string-to-symbol (list)
5. Function - chronometrist-maybe-symbol-to-string (list)
6. Command - chronometrist-reindent-buffer ()
7. Function - chronometrist-append-to-last-expr (tags plist)
7. Function - chronometrist-append-to-last (tags plist)
8. Variable - chronometrist-tags-history
9. Function - chronometrist-tags-history-populate ()
10. Function - chronometrist-tags-history-combination-strings (task)
@ -283,4 +285,7 @@
*** chronometrist-sexp
1. Function - chronometrist-sexp-create-file ()
2. Function - chronometrist-sexp-last ()
* -> plist
3. Function - chronometrist-sexp-current-task ()
4. Function - chronometrist-sexp-replace-last (plist)
5. Function - chronometrist-sexp-reindent-buffer ()