Fix filter of tabulated list entries

This commit is contained in:
Patrick Winter 2019-09-07 17:27:23 +02:00
parent a1f836972d
commit 5ad8fee1a9

View file

@ -13,6 +13,7 @@
(defvar taskwarrior-buffer-name "*taskwarrior*") (defvar taskwarrior-buffer-name "*taskwarrior*")
(defvar taskwarrior-filter nil)
(defvar taskwarrior-description 'taskwarrior-description (defvar taskwarrior-description 'taskwarrior-description
"Taskwarrior mode face used for tasks with a priority of C.") "Taskwarrior mode face used for tasks with a priority of C.")
@ -81,7 +82,7 @@
(define-key taskwarrior-mode-map (kbd "D") 'taskwarrior-delete) (define-key taskwarrior-mode-map (kbd "D") 'taskwarrior-delete)
(define-key taskwarrior-mode-map (kbd "m") 'taskwarrior-mark-task) (define-key taskwarrior-mode-map (kbd "m") 'taskwarrior-mark-task)
(define-key taskwarrior-mode-map (kbd "u") 'taskwarrior-unmark-task) (define-key taskwarrior-mode-map (kbd "u") 'taskwarrior-unmark-task)
(define-key taskwarrior-mode-map (kbd "f") 'taskwarrior-filter) (define-key taskwarrior-mode-map (kbd "f") 'taskwarrior-set-filter)
(define-key taskwarrior-mode-map (kbd "r") 'taskwarrior-reset-filter) (define-key taskwarrior-mode-map (kbd "r") 'taskwarrior-reset-filter)
(define-key taskwarrior-mode-map (kbd "t") 'taskwarrior-edit-tags) (define-key taskwarrior-mode-map (kbd "t") 'taskwarrior-edit-tags)
(define-key taskwarrior-mode-map (kbd "RET") 'taskwarrior-info) (define-key taskwarrior-mode-map (kbd "RET") 'taskwarrior-info)
@ -164,28 +165,18 @@
(string-match "^ [0-9]*" line) (string-match "^ [0-9]*" line)
(string-trim-left (match-string 0 line)))) (string-trim-left (match-string 0 line))))
(defun taskwarrior--get-filter-as-string ()
(if (local-variable-p 'taskwarrior-active-filters)
(mapconcat 'identity taskwarrior-active-filters " ")
""))
(defun taskwarrior--set-filter (filter)
(cond ((stringp filter) (setq-local taskwarrior-active-filters (split-string filter " ")))
((listp filter) (setq-local taskwarrior-active-filters filter))
(t (error "Filter did not match any supported type."))))
(defun taskwarrior-reset-filter () (defun taskwarrior-reset-filter ()
(interactive) (interactive)
(progn (progn
(taskwarrior--set-filter "") (setq-local taskwarrior-filter nil)
(taskwarrior-update-buffer ""))) (taskwarrior-update-buffer)))
(defun taskwarrior-filter () (defun taskwarrior-set-filter ()
(interactive) (interactive)
(let ((new-filter (read-from-minibuffer "Filter: " (taskwarrior--get-filter-as-string)))) (let ((new-filter (read-from-minibuffer "Filter: " taskwarrior-filter)))
(progn (progn
(taskwarrior--set-filter new-filter) (setq-local taskwarrior-filter new-filter)
(taskwarrior-update-buffer new-filter)))) (taskwarrior-update-buffer))))
(defun taskwarrior--shell-command (command &optional filter modifications miscellaneous confirm) (defun taskwarrior--shell-command (command &optional filter modifications miscellaneous confirm)
(let* ((confirmation (if confirm (concat "echo " confirm " |") "")) (let* ((confirmation (if confirm (concat "echo " confirm " |") ""))
@ -209,32 +200,31 @@
(vector-to-list tags) (vector-to-list tags)
" ")) " "))
(defun taskwarrior-export () (defun taskwarrior-export (&optional filter)
"Turn task export into the tabulated list entry form" "Turn task export into the tabulated list entry form"
(mapcar (let ((filter (concat filter " id.not:0")))
(lambda (entry) (mapcar
(let* ((id (format "%s" (alist-get 'id entry))) (lambda (entry)
(urgency (format "%0.2f" (alist-get 'urgency entry))) (let* ((id (format "%s" (alist-get 'id entry)))
(priority (format " %s " (or (alist-get 'priority entry) ""))) (urgency (format "%0.2f" (alist-get 'urgency entry)))
(annotations (format "%d" (length (or (alist-get 'annotations entry) '())))) (priority (format " %s " (or (alist-get 'priority entry) "")))
(project (or (format "%s" (alist-get 'project entry)) "")) (annotations (format "%d" (length (or (alist-get 'annotations entry) '()))))
(tags (or (taskwarrior--concat-tag-list (alist-get 'tags entry)) "")) (project (or (format "%s" (alist-get 'project entry)) ""))
(description (format "%s" (alist-get 'description entry)))) (tags (or (taskwarrior--concat-tag-list (alist-get 'tags entry)) ""))
`(,id [,id ,urgency ,priority ,annotations ,project ,tags ,description]))) (description (format "%s" (alist-get 'description entry))))
(vector-to-list `(,id [,id ,urgency ,priority ,annotations ,project ,tags ,description])))
(json-read-from-string (vector-to-list
(taskwarrior--shell-command "export" "id.not:0"))))) (json-read-from-string
(taskwarrior--shell-command "export" filter))))))
(defun taskwarrior-update-buffer (&optional filter) (defun taskwarrior-update-buffer ()
(interactive) (interactive)
(let* ((filter (taskwarrior--get-filter-as-string)))
(progn (progn
(setq tabulated-list-entries (taskwarrior-export)) (setq tabulated-list-entries
(tabulated-list-print t) (if taskwarrior-filter
(goto-char (point-min)) (taskwarrior-export taskwarrior-filter)
(while (not (equal (overlays-at (point)) nil)) (taskwarrior-export)))
(forward-char)) (tabulated-list-print t)))
(taskwarrior--set-filter filter))))
(defun taskwarrior-export-task (id) (defun taskwarrior-export-task (id)
(let ((task (vector-to-list (let ((task (vector-to-list
@ -362,7 +352,7 @@
("Description" 100 nil)]) ("Description" 100 nil)])
(setq tabulated-list-padding 2) (setq tabulated-list-padding 2)
(setq tabulated-list-sort-key (cons "Urg" nil)) (tabulated-list-init-header) (setq tabulated-list-sort-key (cons "Urg" nil)) (tabulated-list-init-header)
(taskwarrior-update-buffer "")) (taskwarrior-update-buffer))
;;; Externally visible functions ;;; Externally visible functions
;;;###autoload ;;;###autoload