Skip to content

Commit

Permalink
Fix bug w/ creating TODOs
Browse files Browse the repository at this point in the history
  • Loading branch information
rlvoyer committed Aug 21, 2021
1 parent 2e213a7 commit 3d4a953
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 32 deletions.
12 changes: 4 additions & 8 deletions bin/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,12 @@ while [[ $# -gt 0 ]]; do
exit 1;;
-v|--version)
VERSION="$2"
shift # past argument
shift # past value
shift
shift
;;
--default)
DEFAULT=YES
shift # past argument
;;
*) # unknown option
*) # unknown option
POSITIONAL+=("$1") # save it in an array for later
shift # past argument
shift
;;
esac
done
Expand Down
52 changes: 28 additions & 24 deletions mxtodo.el
Original file line number Diff line number Diff line change
Expand Up @@ -345,18 +345,19 @@ with incomplete todo items first, followed by completed todo items."
(mxtodo-make-todo-buffer buffer-name))))
nil)

(defun mxtodo--daily-note-filename ()
(defun mxtodo--daily-note-filename (&optional folder-path)
"Get the path to today's daily note file."
(unless folder-path (setq folder-path mxtodo-folder-path))
(let* ((todays-date-str (ts-format "%Y-%-m-%-d" (ts-now)))
(buffer-name (concat todays-date-str ".md"))
(file-name (concat (expand-file-name (file-name-as-directory mxtodo-folder-path)) buffer-name)))
(file-name (concat (expand-file-name (file-name-as-directory folder-path)) buffer-name)))
file-name))

;;;###autoload
(defun mxtodo-create-daily-note ()
(defun mxtodo-create-daily-note (&optional folder-path)
"Create a new Markdown notes file for today."
(interactive)
(let* ((file-name (mxtodo--daily-note-filename))
(let* ((file-name (mxtodo--daily-note-filename folder-path))
(todays-date-str (ts-format "%Y-%-m-%-d" (ts-now))))
(progn
(find-file file-name)
Expand All @@ -382,11 +383,13 @@ with incomplete todo items first, followed by completed todo items."
(cl-values nil (format "Unable to parse specified date string %s; date must be ISO-8601-formatted." date-str)))))

;;;###autoload
(defun mxtodo-create-todo (&optional buffer-name todo-text due-date-ts)
(defun mxtodo-create-todo (&optional folder-path file-name buffer-name todo-text due-date-ts)
"Add a todo to today's daily note, updating todo buffer with name BUFFER-NAME."
(interactive)
(save-excursion
(progn
(unless file-name
(setq file-name (mxtodo-create-daily-note folder-path)))
(unless buffer-name
(setq buffer-name mxtodo-buffer-name))
(unless todo-text
Expand All @@ -398,26 +401,27 @@ with incomplete todo items first, followed by completed todo items."
(if (not (equal err nil))
(error err)
(setq due-date-ts due-date-parsed))))))
(let ((file-name (mxtodo-create-daily-note)))
(progn
(goto-char (point-max))
(if (not (mxtodo--current-line-empty-p))
(progn
(end-of-line)
(open-line 1)
(goto-char (point-max))))
(let ((todo (make-mxtodo-item
:file-path file-name
:file-line-number (1+ (string-to-number (format-mode-line "%l")))
:file-display-date-ts (ts-now)
:file-last-update (current-time)
:date-due-ts due-date-ts
:text todo-text
:is-completed nil)))
(progn
(goto-char (point-max))
(if (not (mxtodo--current-line-empty-p))
(progn
(newline)
(mxtodo--write-todo-to-file todo)))))
(mxtodo-make-todo-buffer buffer-name))))
(end-of-line)
(open-line 1)
(goto-char (point-max))))
(let ((todo (make-mxtodo-item
:file-path file-name
:file-line-number (1+ (string-to-number (format-mode-line "%l")))
:file-display-date-ts (ts-now)
:file-last-update (mxtodo--file-last-modified file-name)
:date-due-ts due-date-ts
:text todo-text
:is-completed nil)))
(progn
(newline)
(mxtodo--write-todo-to-file todo)
(mxtodo-make-todo-buffer buffer-name folder-path)
todo))))))


;;;###autoload
(defun mxtodo-today ()
Expand Down
16 changes: 16 additions & 0 deletions test/test.el
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,19 @@
(progn
(set-file-times notes-file)
(should (equal expected actual)))))

(ert-deftest test-adding-a-todo-works-for-a-new-notefile ()
"Test that adding a todo works on a fresh daily notefile."
(let* ((notes-dir (make-test-notes-dir))
(todo-text "Water the garden")
(due-date (ts-adjust 'day +7 (ts-now))))
(should (not (equal (mxtodo-create-todo notes-dir nil nil todo-text due-date) nil)))))

(ert-deftest test-adding-a-todo-works-for-an-existing-notefile ()
"Test that adding a todo works on a fresh daily notefile."
(let* ((notes-dir (make-test-notes-dir))
(notes-file (make-test-notes-file notes-dir 1))
(todo-text "Take out the garbage")
(due-date (ts-adjust 'day +7 (ts-now))))
(should (not (equal (mxtodo-create-todo notes-dir nil notes-file todo-text due-date) nil)))))

0 comments on commit 3d4a953

Please sign in to comment.