Fix more errors

This commit is contained in:
contrapunctus 2021-09-04 19:21:53 +05:30
parent 080e75a4c4
commit da1f2ed329
4 changed files with 98 additions and 64 deletions

View File

@ -167,7 +167,7 @@ INITIAL-INPUT is as used in `completing-read'."
_ARGS are ignored. This function always returns t, so it can be
used in `chronometrist-before-out-functions'."
(interactive)
(let* ((last-expr (chronometrist-last))
(let* ((last-expr (chronometrist-latest-record (chronometrist-active-backend)))
(last-name (plist-get last-expr :name))
(_history (chronometrist-tags-history-populate last-name
chronometrist-tags-history chronometrist-file))
@ -179,12 +179,13 @@ used in `chronometrist-before-out-functions'."
(chronometrist-maybe-string-to-symbol))))
(when input
(--> (append last-tags input)
(reverse it)
(cl-remove-duplicates it :test #'equal)
(reverse it)
(list :tags it)
(chronometrist-plist-update (chronometrist-sexp-last) it)
(chronometrist-sexp-replace-last it)))
(reverse it)
(cl-remove-duplicates it :test #'equal)
(reverse it)
(list :tags it)
(chronometrist-plist-update
(chronometrist-latest-record (chronometrist-active-backend)) it)
(chronometrist-replace-last (chronometrist-active-backend) it)))
t))
(defgroup chronometrist-key-values nil
@ -288,7 +289,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-last)
(let ((key-suggestions (--> (chronometrist-latest-record (chronometrist-active-backend))
(plist-get it :name)
(gethash it chronometrist-key-history))))
(completing-read (format "Key (%s to quit): "
@ -335,18 +336,19 @@ used in `chronometrist-before-out-functions'."
(interactive)
(let* ((buffer (get-buffer-create chronometrist-kv-buffer-name))
(first-key-p t)
(last-sexp (chronometrist-last))
(last-sexp (chronometrist-latest-record (chronometrist-active-backend)))
(last-name (plist-get last-sexp :name))
(last-kvs (chronometrist-plist-key-values last-sexp))
(used-keys (--map (chronometrist-keyword-to-string it)
(seq-filter #'keywordp last-kvs))))
(chronometrist-key-history-populate last-name chronometrist-key-history chronometrist-file)
(chronometrist-value-history-populate chronometrist-value-history chronometrist-file)
(seq-filter #'keywordp last-kvs)))
(file (file (chronometrist-active-backend))))
(chronometrist-key-history-populate last-name chronometrist-key-history file)
(chronometrist-value-history-populate chronometrist-value-history file)
(switch-to-buffer buffer)
(with-current-buffer buffer
(erase-buffer)
(chronometrist-kv-read-mode)
(if (and (chronometrist-current-task) last-kvs)
(if (and (chronometrist-current-task (chronometrist-active-backend)) last-kvs)
(progn
(funcall chronometrist-sexp-pretty-print-function last-kvs buffer)
(down-list -1)
@ -377,14 +379,15 @@ used in `chronometrist-before-out-functions'."
(defun chronometrist-kv-accept ()
"Accept the plist in `chronometrist-kv-buffer-name' and add it to `chronometrist-file'."
(interactive)
(let (user-kv-expr)
(let ((latest (chronometrist-latest-record (chronometrist-active-backend)))
user-kv-expr)
(with-current-buffer (get-buffer chronometrist-kv-buffer-name)
(goto-char (point-min))
(setq user-kv-expr (ignore-errors (read (current-buffer))))
(kill-buffer chronometrist-kv-buffer-name))
(if user-kv-expr
(chronometrist-sexp-replace-last
(chronometrist-plist-update (chronometrist-sexp-last) user-kv-expr))
(chronometrist-replace-last (chronometrist-active-backend)
(chronometrist-plist-update latest user-kv-expr))
(chronometrist-refresh))))
(defun chronometrist-kv-reject ()
@ -401,25 +404,30 @@ used in `chronometrist-before-out-functions'."
["Change tags and key-values for active/last interval"
chronometrist-key-values-unified-prompt]))
(cl-defun chronometrist-key-values-unified-prompt (&optional (task (plist-get (chronometrist-sexp-last) :name)))
(cl-defun chronometrist-key-values-unified-prompt (&optional (task (plist-get (chronometrist-latest-record (chronometrist-active-backend)) :name)))
"Query user for tags and key-values to be added for TASK.
Return t, to permit use in `chronometrist-before-out-functions'."
(interactive)
(let ((key-values (chronometrist-loop-file for plist in chronometrist-file
when (equal (plist-get plist :name) task)
collect
(let ((plist (chronometrist-plist-remove plist :name :start :stop)))
(when plist (format "%S" plist)))
into key-value-plists
finally return
(--> (seq-filter #'identity key-value-plists)
(cl-remove-duplicates it :test #'equal :from-end t)))))
(let* ((backend (chronometrist-active-backend))
(key-values
(cl-loop for plist in (chronometrist-list-records backend)
when (equal (plist-get plist :name) task)
collect
(let ((plist (chronometrist-plist-remove plist :name :start :stop)))
(when plist (format "%S" plist)))
into key-value-plists
finally return
(--> (seq-filter #'identity key-value-plists)
(cl-remove-duplicates it :test #'equal :from-end t))))
(latest (chronometrist-latest-record backend)))
(if (null key-values)
(progn (chronometrist-tags-add) (chronometrist-kv-add))
(chronometrist-sexp-replace-last
(chronometrist-plist-update (chronometrist-sexp-last)
(read (completing-read (format "Key-values for %s: " task)
key-values))))))
(chronometrist-replace-last
backend
(chronometrist-plist-update
latest
(read (completing-read (format "Key-values for %s: " task)
key-values))))))
t)
(provide 'chronometrist-key-values)

View File

@ -301,7 +301,7 @@ INITIAL-INPUT is as used in `completing-read'."
_ARGS are ignored. This function always returns t, so it can be
used in `chronometrist-before-out-functions'."
(interactive)
(let* ((last-expr (chronometrist-last))
(let* ((last-expr (chronometrist-latest-record (chronometrist-active-backend)))
(last-name (plist-get last-expr :name))
(_history (chronometrist-tags-history-populate last-name
chronometrist-tags-history chronometrist-file))
@ -313,12 +313,13 @@ used in `chronometrist-before-out-functions'."
(chronometrist-maybe-string-to-symbol))))
(when input
(--> (append last-tags input)
(reverse it)
(cl-remove-duplicates it :test #'equal)
(reverse it)
(list :tags it)
(chronometrist-plist-update (chronometrist-sexp-last) it)
(chronometrist-sexp-replace-last it)))
(reverse it)
(cl-remove-duplicates it :test #'equal)
(reverse it)
(list :tags it)
(chronometrist-plist-update
(chronometrist-latest-record (chronometrist-active-backend)) it)
(chronometrist-replace-last (chronometrist-active-backend) it)))
t))
#+END_SRC
** Key-Values
@ -475,7 +476,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-last)
(let ((key-suggestions (--> (chronometrist-latest-record (chronometrist-active-backend))
(plist-get it :name)
(gethash it chronometrist-key-history))))
(completing-read (format "Key (%s to quit): "
@ -528,18 +529,19 @@ used in `chronometrist-before-out-functions'."
(interactive)
(let* ((buffer (get-buffer-create chronometrist-kv-buffer-name))
(first-key-p t)
(last-sexp (chronometrist-last))
(last-sexp (chronometrist-latest-record (chronometrist-active-backend)))
(last-name (plist-get last-sexp :name))
(last-kvs (chronometrist-plist-key-values last-sexp))
(used-keys (--map (chronometrist-keyword-to-string it)
(seq-filter #'keywordp last-kvs))))
(chronometrist-key-history-populate last-name chronometrist-key-history chronometrist-file)
(chronometrist-value-history-populate chronometrist-value-history chronometrist-file)
(seq-filter #'keywordp last-kvs)))
(file (file (chronometrist-active-backend))))
(chronometrist-key-history-populate last-name chronometrist-key-history file)
(chronometrist-value-history-populate chronometrist-value-history file)
(switch-to-buffer buffer)
(with-current-buffer buffer
(erase-buffer)
(chronometrist-kv-read-mode)
(if (and (chronometrist-current-task) last-kvs)
(if (and (chronometrist-current-task (chronometrist-active-backend)) last-kvs)
(progn
(funcall chronometrist-sexp-pretty-print-function last-kvs buffer)
(down-list -1)
@ -572,14 +574,15 @@ used in `chronometrist-before-out-functions'."
(defun chronometrist-kv-accept ()
"Accept the plist in `chronometrist-kv-buffer-name' and add it to `chronometrist-file'."
(interactive)
(let (user-kv-expr)
(let ((latest (chronometrist-latest-record (chronometrist-active-backend)))
user-kv-expr)
(with-current-buffer (get-buffer chronometrist-kv-buffer-name)
(goto-char (point-min))
(setq user-kv-expr (ignore-errors (read (current-buffer))))
(kill-buffer chronometrist-kv-buffer-name))
(if user-kv-expr
(chronometrist-sexp-replace-last
(chronometrist-plist-update (chronometrist-sexp-last) user-kv-expr))
(chronometrist-replace-last (chronometrist-active-backend)
(chronometrist-plist-update latest user-kv-expr))
(chronometrist-refresh))))
#+END_SRC
*** kv-reject :command:
@ -643,13 +646,15 @@ Types of prompts planned (#1 and #2 are meant to be mixed and matched)
LIST should be a list, with all elements being either a plists,
or lists of symbols."
(cl-loop with num = 0
with last = (chronometrist-latest-record (chronometrist-active-backend))
for elt in (-take 7 list)
do (incf num)
if (= num 10) do (setq num 0)
collect
(list (format "%s" num)
`(chronometrist-sexp-replace-last
(chronometrist-plist-update (chronometrist-sexp-last)
`(chronometrist-replace-last
(chronometrist-active-backend)
(chronometrist-plist-update last
',(cl-case type
(:tags (list :tags elt))
(:key-values elt))))
@ -697,25 +702,30 @@ Return t, to permit use in `chronometrist-before-out-functions'."
*** WIP unified-prompt :hook:writer:
1. [ ] Improve appearance - is there an easy way to syntax highlight the plists?
#+BEGIN_SRC emacs-lisp
(cl-defun chronometrist-key-values-unified-prompt (&optional (task (plist-get (chronometrist-sexp-last) :name)))
(cl-defun chronometrist-key-values-unified-prompt (&optional (task (plist-get (chronometrist-latest-record (chronometrist-active-backend)) :name)))
"Query user for tags and key-values to be added for TASK.
Return t, to permit use in `chronometrist-before-out-functions'."
(interactive)
(let ((key-values (chronometrist-loop-file for plist in chronometrist-file
when (equal (plist-get plist :name) task)
collect
(let ((plist (chronometrist-plist-remove plist :name :start :stop)))
(when plist (format "%S" plist)))
into key-value-plists
finally return
(--> (seq-filter #'identity key-value-plists)
(cl-remove-duplicates it :test #'equal :from-end t)))))
(let* ((backend (chronometrist-active-backend))
(key-values
(cl-loop for plist in (chronometrist-list-records backend)
when (equal (plist-get plist :name) task)
collect
(let ((plist (chronometrist-plist-remove plist :name :start :stop)))
(when plist (format "%S" plist)))
into key-value-plists
finally return
(--> (seq-filter #'identity key-value-plists)
(cl-remove-duplicates it :test #'equal :from-end t))))
(latest (chronometrist-latest-record backend)))
(if (null key-values)
(progn (chronometrist-tags-add) (chronometrist-kv-add))
(chronometrist-sexp-replace-last
(chronometrist-plist-update (chronometrist-sexp-last)
(read (completing-read (format "Key-values for %s: " task)
key-values))))))
(chronometrist-replace-last
backend
(chronometrist-plist-update
latest
(read (completing-read (format "Key-values for %s: " task)
key-values))))))
t)
#+END_SRC

View File

@ -416,6 +416,9 @@ Value must be a keyword corresponding to a key in
(cl-defgeneric chronometrist-from-hash-table (backend hash-table)
"Save data from HASH-TABLE to BACKEND.")
(cl-defgeneric chronometrist-list-records (backend)
"Return all records in BACKEND as a list of plists, in reverse chronological order.")
(defclass chronometrist-plist-backend (chronometrist-backend) ())
(add-to-list 'chronometrist-backends-alist
@ -462,7 +465,7 @@ EXPR is bound to each s-expression."
,@loop-clauses)))
(cl-defmethod chronometrist-edit-file ((backend chronometrist-plist-backend))
(find-file-other-window (path backend))
(find-file-other-window (file backend))
(goto-char (point-max)))
(cl-defmethod chronometrist-count-records ((backend chronometrist-plist-backend))
@ -568,6 +571,9 @@ This is meant to be run in `chronometrist-file' when using the s-expression back
(cl-remove-duplicates it :test #'equal)
(sort it #'string-lessp)))
(cl-defmethod chronometrist-list-records ((backend chronometrist-plist-backend))
(chronometrist-loop-file for plist in (file backend) collect plist))
(defvar chronometrist--file-state nil
"List containing the state of `chronometrist-file'.
`chronometrist-refresh-file' sets this to a plist in the form

View File

@ -1029,6 +1029,11 @@ Value must be a keyword corresponding to a key in
"Save data from HASH-TABLE to BACKEND.")
#+END_SRC
**** list-records :generic:function:
#+BEGIN_SRC emacs-lisp
(cl-defgeneric chronometrist-list-records (backend)
"Return all records in BACKEND as a list of plists, in reverse chronological order.")
#+END_SRC
*** plist backend
In this format, user data is stored as Elisp plists in a plain text file. A basic plist in this file looks like this -
@ -1185,7 +1190,7 @@ EXPR is bound to each s-expression."
**** edit-file :method:
#+BEGIN_SRC emacs-lisp
(cl-defmethod chronometrist-edit-file ((backend chronometrist-plist-backend))
(find-file-other-window (path backend))
(find-file-other-window (file backend))
(goto-char (point-max)))
#+END_SRC
**** count-records :reader:method:
@ -1318,6 +1323,11 @@ This is meant to be run in `chronometrist-file' when using the s-expression back
(should (listp task-list))
(should (seq-every-p #'stringp task-list))))
#+END_SRC
**** list-records :reader:method:
#+BEGIN_SRC emacs-lisp
(cl-defmethod chronometrist-list-records ((backend chronometrist-plist-backend))
(chronometrist-loop-file for plist in (file backend) collect plist))
#+END_SRC
**** file-state :internal:variable:
:PROPERTIES:
:VALUE: list