Skip to content

Commit

Permalink
Update org utils
Browse files Browse the repository at this point in the history
  • Loading branch information
cmrfrd committed Feb 22, 2022
1 parent c02dde2 commit 03af9e1
Showing 1 changed file with 40 additions and 40 deletions.
80 changes: 40 additions & 40 deletions emacs/emacs.d.symlink/resources/org-utils.el
Original file line number Diff line number Diff line change
@@ -1,50 +1,50 @@
;;--------------------------
;; Handling file properties for ‘CREATED’ & ‘LAST_MODIFIED’
;;--------------------------
;;--------------------------
;; Handling file properties for ‘CREATED’ & ‘LAST_MODIFIED’
;;--------------------------

(defun zp/org-find-time-file-property (property &optional anywhere)
"Return the position of the time file PROPERTY if it exists.
(defun zp/org-find-time-file-property (property &optional anywhere)
"Return the position of the time file PROPERTY if it exists.
When ANYWHERE is non-nil, search beyond the preamble."
(save-excursion
(goto-char (point-min))
(let ((first-heading
(save-excursion
(re-search-forward org-outline-regexp-bol nil t))))
(when (re-search-forward (format "^#\\+%s:" property)
(if anywhere nil first-heading)
t)
(point)))))
(save-excursion
(goto-char (point-min))
(let ((first-heading
(save-excursion
(re-search-forward org-outline-regexp-bol nil t))))
(when (re-search-forward (format "^#\\+%s:" property)
(if anywhere nil first-heading)
t)
(point)))))

(defun zp/org-has-time-file-property-p (property &optional anywhere)
"Return the position of time file PROPERTY if it is defined.
(defun zp/org-has-time-file-property-p (property &optional anywhere)
"Return the position of time file PROPERTY if it is defined.
As a special case, return -1 if the time file PROPERTY exists but
is not defined."
(when-let ((pos (zp/org-find-time-file-property property anywhere)))
(save-excursion
(goto-char pos)
(if (and (looking-at-p " ")
(progn (forward-char)
(org-at-timestamp-p 'lax)))
pos
-1))))
(when-let ((pos (zp/org-find-time-file-property property anywhere)))
(save-excursion
(goto-char pos)
(if (and (looking-at-p " ")
(progn (forward-char)
(org-at-timestamp-p 'lax)))
pos
-1))))

(defun zp/org-set-time-file-property (property &optional anywhere pos)
"Set the time file PROPERTY in the preamble.
(defun zp/org-set-time-file-property (property &optional anywhere pos)
"Set the time file PROPERTY in the preamble.
When ANYWHERE is non-nil, search beyond the preamble.
If the position of the file PROPERTY has already been computed,
it can be passed in POS."
(when-let ((pos (or pos
(zp/org-find-time-file-property property))))
(save-excursion
(goto-char pos)
(if (looking-at-p " ")
(forward-char)
(insert " "))
(delete-region (point) (line-end-position))
(let* ((now (format-time-string "[%Y-%m-%d %a %H:%M]")))
(insert now)))))
(when-let ((pos (or pos
(zp/org-find-time-file-property property))))
(save-excursion
(goto-char pos)
(if (looking-at-p " ")
(forward-char)
(insert " "))
(delete-region (point) (line-end-position))
(let* ((now (format-time-string "[%Y-%m-%d %a %H:%M]")))
(insert now)))))

(defun zp/org-set-last-modified ()
"Update the LAST_MODIFIED file property in the preamble."
(when (derived-mode-p 'org-mode)
(zp/org-set-time-file-property "LAST_MODIFIED")))
(defun zp/org-set-last-modified ()
"Update the LAST_MODIFIED file property in the preamble."
(when (derived-mode-p 'org-mode)
(zp/org-set-time-file-property "LAST_MODIFIED")))

0 comments on commit 03af9e1

Please sign in to comment.