Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add two more insert at point commands #339

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 47 additions & 28 deletions org-journal.el
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ This allows the use of `org-journal-tag-alist' and
(when (re-search-backward "^#\\+" nil t)
(org-ctrl-c-ctrl-c)))))

(defun org-journal--insert-entry-header (time)
(defun org-journal--insert-entry-header (time &optional insert-at-point)
"Create new journal entry if there isn't one."
(let ((entry-header
(if (functionp org-journal-date-format)
Expand All @@ -641,7 +641,8 @@ This allows the use of `org-journal-tag-alist' and
(user-error "org-journal-date-format is empty, this won't work"))
(concat org-journal-date-prefix
(format-time-string org-journal-date-format time)))))
(goto-char (point-min))
(unless insert-at-point
(goto-char (point-min)))
(unless (if (org-journal--daily-p)
(or (search-forward entry-header nil t) (and (goto-char (point-max)) nil))
(cl-loop
Expand Down Expand Up @@ -689,8 +690,13 @@ This allows the use of `org-journal-tag-alist' and
(unless (member org-crypt-tag-matcher (org-get-tags))
(org-set-tags org-crypt-tag-matcher)))
(run-hooks 'org-journal-after-header-create-hook))))
;;;###autoload
(defun org-journal-insert-header-at-point ()
"Create journal style headline at point."
(interactive)
(org-journal--insert-entry-header (current-time) t))

(defun org-journal--insert-entry (time org-extend-today-until-active-p)
(defun org-journal--insert-entry (time org-extend-today-until-active-p &optional todo)
"Insert a new entry."
(unless (eq (current-column) 0) (insert "\n"))
(let* ((day-discrepancy (- (time-to-days (current-time)) (time-to-days time)))
Expand All @@ -706,11 +712,11 @@ This allows the use of `org-journal-tag-alist' and
(format-time-string org-journal-time-format)))
;; “time” is on some other day, use blank timestamp
(t ""))))
(insert org-journal-time-prefix timestamp))
(insert (concat org-journal-time-prefix (when todo "TODO ") timestamp)))
(run-hooks 'org-journal-after-entry-create-hook))

;;;###autoload
(defun org-journal-new-entry (prefix &optional time)
(defun org-journal-new-entry (prefix &optional time todo insert-at-point)
"Open today's journal file and start a new entry.

With a PREFIX arg, open the today's file, create a heading if it doesn't exist yet,
Expand Down Expand Up @@ -742,29 +748,30 @@ hook is run."
(nth 8 now))))
(setq entry-path (org-journal--get-entry-path time))

;; Open journal file
(unless (string= entry-path (buffer-file-name))
(funcall org-journal-find-file entry-path))
(unless insert-at-point
;; Open journal file
(unless (string= entry-path (buffer-file-name))
(funcall org-journal-find-file entry-path))

;; Insure `view-mode' is not active
(view-mode -1)
;; Insure `view-mode' is not active
(view-mode -1)

(org-journal--insert-header time)
(org-journal--insert-entry-header time)
(org-journal--decrypt)
(org-journal--insert-header time)
(org-journal--insert-entry-header time)
(org-journal--decrypt)

;; Move TODOs from previous day to new entry
(when (and org-journal-carryover-items
(not (string-blank-p org-journal-carryover-items))
(string= entry-path (org-journal--get-entry-path (current-time))))
(org-journal--carryover))
;; Move TODOs from previous day to new entry
(when (and org-journal-carryover-items
(not (string-blank-p org-journal-carryover-items))
(string= entry-path (org-journal--get-entry-path (current-time))))
(org-journal--carryover))

(if (org-journal--is-date-prefix-org-heading-p)
(outline-end-of-subtree)
(goto-char (point-max)))
(if (org-journal--is-date-prefix-org-heading-p)
(outline-end-of-subtree)
(goto-char (point-max))))

(when should-add-entry-p
(org-journal--insert-entry time org-extend-today-until-active-p))
(org-journal--insert-entry time org-extend-today-until-active-p todo))

(if (and org-journal-hide-entries-p (org-journal--time-entry-level))
(outline-hide-sublevels (org-journal--time-entry-level))
Expand All @@ -773,6 +780,13 @@ hook is run."
(when should-add-entry-p
(outline-show-entry))))

;;;###autoload
(defun org-journal-new-entry-at-point (prefix &optional time todo)
"Insert a journal style entry at current cursor location."
(interactive "P")
(org-journal-new-entry prefix time todo t))


(defvar org-journal--kill-buffer nil
"Will be set to the `t' if `org-journal--open-entry' is visiting a
buffer not open already, otherwise `nil'.")
Expand Down Expand Up @@ -1053,21 +1067,26 @@ arguments (C-u C-u) are given. In that case insert just the heading."
(org-journal-new-scheduled-entry prefix time))))

;;;###autoload
(defun org-journal-new-scheduled-entry (prefix &optional scheduled-time)
(defun org-journal-new-scheduled-entry (prefix &optional scheduled-time insert-at-point)
"Create a new entry in the future with an active timestamp.

With non-nil prefix argument create a regular entry instead of a TODO entry."
(interactive "P")
(let ((time (or scheduled-time (org-time-string-to-time (org-read-date nil nil nil "Date:"))))
org-journal-carryover-items)
(when (time-less-p time (current-time))
(user-error "Scheduled time needs to be in the future"))
(org-journal-new-entry nil time)
(unless prefix
(insert "TODO "))
(org-journal-new-entry nil time (not prefix) insert-at-point)
(save-excursion
(insert "\n")
(org-insert-time-stamp time))))
(insert "SCHEDULED: ")
(org-insert-time-stamp time t)
(org-cycle))))

;;;###autoload
(defun org-journal-new-scheduled-entry-at-point (prefix &optional scheduled-time)
"Insert a journal style scheduled entry at current cursor location."
(interactive "P")
(org-journal-new-scheduled-entry prefix scheduled-time t))

;;;###autoload
(defun org-journal-reschedule-scheduled-entry (&optional time)
Expand Down