Reparse timeline when buffer-local variable is nil

This removes a lot of duplicate code.
This commit is contained in:
Daniel - 2020-01-26 19:50:29 +01:00
parent ea4d49e99b
commit 3f5ae63ea4
No known key found for this signature in database
GPG Key ID: 1C7071A75BB72D64
1 changed files with 65 additions and 68 deletions

View File

@ -416,6 +416,9 @@ Filtering is done by applying all functions from
timeline timeline
timeline-tools-filter-functions)) timeline-tools-filter-functions))
;; Interactive functions
;;;###autoload ;;;###autoload
(defun timeline-tools-format-timeline (tstart tend &optional files) (defun timeline-tools-format-timeline (tstart tend &optional files)
"Display timeline of tasks between TSTART and TEND from FILES. "Display timeline of tasks between TSTART and TEND from FILES.
@ -426,21 +429,19 @@ value of `timeline-tools-filter-functions. When called
interactively, START and END are queried with `org-read-date." interactively, START and END are queried with `org-read-date."
(interactive (list (org-read-date nil nil nil "Start time: ") (interactive (list (org-read-date nil nil nil "Start time: ")
(org-read-date nil nil nil "End time: "))) (org-read-date nil nil nil "End time: ")))
(let* ((timeline (timeline-tools-transform-timeline (let ((target-buffer (get-buffer-create " *Org Timeline*")))
(timeline-tools-timeline tstart tend files)))) (with-current-buffer target-buffer
(let ((target-buffer (get-buffer-create " *Org Timeline*"))) (timeline-tools-mode)
(with-current-buffer target-buffer (setq-local timeline-tools--current-time-start (org-time-string-to-seconds tstart))
(timeline-tools-mode) (setq-local timeline-tools--current-time-end (org-time-string-to-seconds tend))
(setq-local timeline-tools--current-time-start (org-time-string-to-seconds tstart)) (setq-local timeline-tools--current-files files)
(setq-local timeline-tools--current-time-end (org-time-string-to-seconds tend)) (setq-local timeline-tools--current-timeline nil)
(setq-local timeline-tools--current-files files) (setq-local timeline-tools-time-format timeline-tools-time-format)
(setq-local timeline-tools--current-timeline timeline) (setq-local timeline-tools-headline-time-format timeline-tools-headline-time-format)
(setq-local timeline-tools-time-format timeline-tools-time-format) (hl-line-mode)
(setq-local timeline-tools-headline-time-format timeline-tools-headline-time-format) (timeline-tools-redraw-timeline))
(hl-line-mode) (pop-to-buffer target-buffer)
(timeline-tools-redraw-timeline)) t))
(pop-to-buffer target-buffer)
t)))
;;;###autoload ;;;###autoload
(defun timeline-tools-format-timeline-of-day (date &optional files) (defun timeline-tools-format-timeline-of-day (date &optional files)
@ -458,45 +459,54 @@ archives."
(org-time-string-to-time date)) (org-time-string-to-time date))
files))) files)))
;; Interactive functions
(defun timeline-tools-redraw-timeline () (defun timeline-tools-redraw-timeline ()
"Redraw timeline of current buffer." "Redraw timeline of current buffer.
If the current timeline is `nil' (via the buffer local variable
`timeline-tools--current-timeline', reparse the timeline using
`timeline-tools-timeline' within the time span given by the
current values of the relevant buffer local variables."
(interactive) (interactive)
(if (not (eq major-mode 'timeline-tools-mode)) (if (not (eq major-mode 'timeline-tools-mode))
(user-error "Not in Timeline buffer") (user-error "Not in Timeline buffer")
(let ((timeline timeline-tools--current-timeline)) (unless timeline-tools--current-timeline
(erase-buffer) (setq-local timeline-tools--current-timeline
(insert (format "* Timeline from [%s] to [%s]\n\n" (timeline-tools-transform-timeline
(format-time-string timeline-tools-headline-time-format (timeline-tools-timeline
timeline-tools--current-time-start) timeline-tools--current-time-start
(format-time-string timeline-tools-headline-time-format timeline-tools--current-time-end
timeline-tools--current-time-end))) timeline-tools--current-files))))
(insert "|--|\n") (erase-buffer)
(insert "| Category | Start | End | Duration | Task |\n") (insert (format "* Timeline from [%s] to [%s]\n\n"
(let ((last-category nil) (format-time-string timeline-tools-headline-time-format
(current-category nil)) timeline-tools--current-time-start)
(dolist (line timeline) (format-time-string timeline-tools-headline-time-format
(setq current-category (funcall timeline-tools-category-function timeline-tools--current-time-end)))
line (insert "|--|\n")
timeline-tools--current-time-start (insert "| Category | Start | End | Duration | Task |\n")
timeline-tools--current-time-end)) (let ((last-category nil)
(when (not (equal last-category current-category)) (current-category nil))
(insert "|--|\n") (dolist (line timeline-tools--current-timeline)
(setq last-category current-category)) (setq current-category (funcall timeline-tools-category-function
(insert line
(propertize (format "| %s | %s | %s | %s min | %s | \n" timeline-tools--current-time-start
current-category timeline-tools--current-time-end))
(timeline-tools-format-entry-time line 'start) (when (not (equal last-category current-category))
(timeline-tools-format-entry-time line 'end) (insert "|--|\n")
(timeline-tools-entry-duration line) (timeline-tools-entry-headline line)) (setq last-category current-category))
'marker (timeline-tools-entry-marker line) (insert
'entry line)))) (propertize (format "| %s | %s | %s | %s min | %s | \n"
(insert "|--|\n") current-category
(org-table-align) (timeline-tools-format-entry-time line 'start)
(goto-char (point-min)) (timeline-tools-format-entry-time line 'end)
(timeline-tools-next-line)))) (timeline-tools-entry-duration line)
(timeline-tools-entry-headline line))
'marker (timeline-tools-entry-marker line)
'entry line))))
(insert "|--|\n")
(org-table-align)
(goto-char (point-min))
(timeline-tools-next-line)
(buffer-enable-undo)))
(defun timeline-tools-reparse-timeline () (defun timeline-tools-reparse-timeline ()
"Parse timeline from files again and redraw current display. "Parse timeline from files again and redraw current display.
@ -505,12 +515,7 @@ Updates category properties before constructing the new timeline."
(dolist (file timeline-tools--current-files) (dolist (file timeline-tools--current-files)
(with-current-buffer (get-file-buffer file) (with-current-buffer (get-file-buffer file)
(org-refresh-category-properties))) (org-refresh-category-properties)))
(setq-local timeline-tools--current-timeline (setq-local timeline-tools--current-timeline nil)
(timeline-tools-transform-timeline
(timeline-tools-timeline
timeline-tools--current-time-start
timeline-tools--current-time-end
timeline-tools--current-files)))
(timeline-tools-redraw-timeline)) (timeline-tools-redraw-timeline))
(defun timeline-tools-forward-day () (defun timeline-tools-forward-day ()
@ -520,12 +525,8 @@ Updates category properties before constructing the new timeline."
(user-error "Not in Timeline buffer") (user-error "Not in Timeline buffer")
(setq-local timeline-tools--current-time-start (+ 86400 timeline-tools--current-time-start)) (setq-local timeline-tools--current-time-start (+ 86400 timeline-tools--current-time-start))
(setq-local timeline-tools--current-time-end (+ 86400 timeline-tools--current-time-end)) (setq-local timeline-tools--current-time-end (+ 86400 timeline-tools--current-time-end))
(setq-local timeline-tools--current-timeline ;; Set current timeline to nil to trigger reparsing
(timeline-tools-transform-timeline (setq-local timeline-tools--current-timeline nil)
(timeline-tools-timeline
timeline-tools--current-time-start
timeline-tools--current-time-end
timeline-tools--current-files)))
(timeline-tools-redraw-timeline))) (timeline-tools-redraw-timeline)))
(defun timeline-tools-backward-day () (defun timeline-tools-backward-day ()
@ -537,12 +538,8 @@ Updates category properties before constructing the new timeline."
(- timeline-tools--current-time-start 86400)) (- timeline-tools--current-time-start 86400))
(setq-local timeline-tools--current-time-end (setq-local timeline-tools--current-time-end
(- timeline-tools--current-time-end 86400)) (- timeline-tools--current-time-end 86400))
(setq-local timeline-tools--current-timeline ;; Set current timeline to nil to trigger reparsing
(timeline-tools-transform-timeline (setq-local timeline-tools--current-timeline nil)
(timeline-tools-timeline
timeline-tools--current-time-start
timeline-tools--current-time-end
timeline-tools--current-files)))
(timeline-tools-redraw-timeline))) (timeline-tools-redraw-timeline)))
(defun timeline-tools-skip-short-entries () (defun timeline-tools-skip-short-entries ()