Skip to content

Commit

Permalink
org-lint for logbook entries with duplicate timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffkowalski committed Jun 26, 2021
1 parent 6515faf commit f44b604
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 0 deletions.
59 changes: 59 additions & 0 deletions personal/personal.el
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,65 @@ be global."
(req-package org-plot
:after gnuplot-mode)

;; org lint

(req-package org-lint
:after (org)
:config
(defun jeff/org-logbook-retrieve-timestamps (beg end)
"Retrieve timestamp of all state-change entries between BEG and END."
(save-excursion
(let* ((reversed org-log-states-order-reversed)
(search (if reversed 're-search-forward 're-search-backward))
(limit (if reversed end (point)))
(re (format
"^[ \t]*-[ \t]+\\(?:State \"%s\"\s+from\s+\"%s\".*%s%s\\)"
org-todo-regexp
org-todo-regexp
org-ts-regexp-inactive
(let ((value (cdr (assq 'done org-log-note-headings))))
(if (not value) ""
(concat "\\|"
(org-replace-escapes
(regexp-quote value)
`(("%d" . ,org-ts-regexp-inactive)
("%D" . ,org-ts-regexp)
("%s" . "\"\\S-+\"")
("%S" . "\"\\S-+\"")
("%t" . ,org-ts-regexp-inactive)
("%T" . ,org-ts-regexp)
("%u" . ".*?")
("%U" . ".*?"))))))))
log-entries)
(goto-char (if reversed beg end))
(while (funcall search re limit t)
(push (match-string-no-properties 3) log-entries))
log-entries)))

(defun org-lint-duplicate-logbook-timestamps (ast)
"Report LOGBOOK entries with duplicate timestamp"
(org-element-map ast 'drawer
(lambda (d)
(when (equal (org-element-property :drawer-name d) "LOGBOOK")
(let* ((beg (org-element-property :contents-begin d))
(end (org-element-property :contents-end d))
(orig (jeff/org-logbook-retrieve-timestamps beg end))
(uniq (cl-remove-duplicates
orig
:test (lambda (x y) (or (null y) (equal x y)))
:from-end t))
(diff (- (length orig) (length uniq))))
(unless (zerop diff)
(list (org-element-property :begin d)
(format "LOGBOOK has %d entries with duplicate timestamp" diff))))))))

(add-to-list 'org-lint--checkers
(make-org-lint-checker
:name 'duplicate-logbook-timestamps
:description "Report LOGBOOK entries with duplicate timestamp"
:categories '(properties)))
)

;; org-ehtml

(req-package web-server)
Expand Down
59 changes: 59 additions & 0 deletions personal/personal.org
Original file line number Diff line number Diff line change
Expand Up @@ -1210,6 +1210,65 @@ be global."
(req-package org-plot
:after gnuplot-mode)
#+end_src
*** org lint
#+begin_src emacs-lisp
(req-package org-lint
:after (org)
:config
(defun jeff/org-logbook-retrieve-timestamps (beg end)
"Retrieve timestamp of all state-change entries between BEG and END."
(save-excursion
(let* ((reversed org-log-states-order-reversed)
(search (if reversed 're-search-forward 're-search-backward))
(limit (if reversed end (point)))
(re (format
"^[ \t]*-[ \t]+\\(?:State \"%s\"\s+from\s+\"%s\".*%s%s\\)"
org-todo-regexp
org-todo-regexp
org-ts-regexp-inactive
(let ((value (cdr (assq 'done org-log-note-headings))))
(if (not value) ""
(concat "\\|"
(org-replace-escapes
(regexp-quote value)
`(("%d" . ,org-ts-regexp-inactive)
("%D" . ,org-ts-regexp)
("%s" . "\"\\S-+\"")
("%S" . "\"\\S-+\"")
("%t" . ,org-ts-regexp-inactive)
("%T" . ,org-ts-regexp)
("%u" . ".*?")
("%U" . ".*?"))))))))
log-entries)
(goto-char (if reversed beg end))
(while (funcall search re limit t)
(push (match-string-no-properties 3) log-entries))
log-entries)))

(defun org-lint-duplicate-logbook-timestamps (ast)
"Report LOGBOOK entries with duplicate timestamp"
(org-element-map ast 'drawer
(lambda (d)
(when (equal (org-element-property :drawer-name d) "LOGBOOK")
(let* ((beg (org-element-property :contents-begin d))
(end (org-element-property :contents-end d))
(orig (jeff/org-logbook-retrieve-timestamps beg end))
(uniq (cl-remove-duplicates
orig
:test (lambda (x y) (or (null y) (equal x y)))
:from-end t))
(diff (- (length orig) (length uniq))))
(unless (zerop diff)
(list (org-element-property :begin d)
(format "LOGBOOK has %d entries with duplicate timestamp" diff))))))))

(add-to-list 'org-lint--checkers
(make-org-lint-checker
:name 'duplicate-logbook-timestamps
:description "Report LOGBOOK entries with duplicate timestamp"
:categories '(properties)))
)
#+end_src
** org-ehtml
#+BEGIN_SRC emacs-lisp
(req-package web-server)
Expand Down

0 comments on commit f44b604

Please sign in to comment.