4.1 KiB
User:
Write an emacs function that moves an org header at point with the property :CREATED: in the format [2009-07-27 Mon 17:12] to an org-roam-dalies node with the same date using org-roam-dailies-find-date
Ellama:
Here's an Emacs Lisp code of a function that performs the described action:
(defun move-header-to-org-roam-dailies ()
"Move the org header at point with the property :CREATED: in the format [2009-07-27 Mon 17:12] to
an org-roam-dailies node with the same date."
(interactive)
(when-let ((created (org-entry-get nil "CREATED")) ; get the CREATED property of the headline
(created-time (and created (org-time-string-to-time created)))) ; convert string to
time
(let ((heading (org-get-heading t t t t)) ; get heading of current line
(content (org-get-entry))
(date (format-time-string "%Y-%m-%d" created-time)))
(org-cut-subtree) ; cut the subtree
(org-roam-dailies-find-date created-time) ; navigate to the daily note
(goto-char (point-max)) ; move point to the end of the buffer
(unless (looking-at-p "^") (insert "\n")) ; make sure we start on a new line
(insert "* " heading "\n" content) ; insert heading and content
(save-buffer)))) ; save the current buffer
This function gets the :CREATED: property of a header (returns nil if the property doesn't exist
or on error), checks for its existence, gets the heading and the content of the header, cuts the
header, finds or creates the org-roam daily note for the date specified in the :CREATED: property,
moves the point to the end of that buffer, inserts the cut header and saves the changes. Note that
"org-roam-dailies-find-date" is a function from "org-roam" package, thus you need to have this
package installed and loaded.
This function is assigned to global key-binding C-c d as follows:
(global-set-key (kbd "C-c d") 'move-header-to-org-roam-dailies)
This code assumes that a header with a :CREATED: property is in a specific date format
([2009-07-27 Mon 17:12]). If the :CREATED: property is in a different format, you will need to
adjust the org-time-string-to-time function accordingly to be able to correctly parse the dates.
User:
Write an emacs function that moves an org header at point with the property :CREATED: in the format [2009-07-27 Mon 17:12] to an org-roam-dalies node with the same date using org-roam-dailies-find-date
Ellama:
Below demostrates an Emacs function that does exactly that:
(defun my-org-move-header-to-roam-dailies()
(interactive)
(let* ((header-point (point))
(planning-line (org-get-planning-or-clock-line))
(created-date-property (when planning-line
(org-entry-get (point) "CREATED")))
(created-date-timestamp (when created-date-property
(org-read-date nil nil created-date-property)))
(created-date (when created-date-timestamp
(org-time-string-to-time created-date-timestamp)))
(headers (org-heading-components)))
(when created-date
(save-excursion
(org-cut-subtree)
(org-roam-dailies-find-date created-date)
(goto-char (point-max))
(org-paste-subtree)
(goto-char header-point)))))
Note, org-entry-get is used to retrieve the property :CREATED: from the org header at point. The org-time-string-to-time is used to convert the timestamp string to a time value operational in emacs. This time value is then passed to org-roam-dailies-find-date to open the daily note with the associated date.
This function is also wrapped in a save-excursion, which saves the current position of point, so that any movement within the function will return to the original location after the function ends. org-cut-subtree is used to cut the current subtree (the header and all of its sub-headers), and org-paste-subtree is used to paste it into the daily note.
Don't forget to add error checking to this function to handle headers that may not have a :CREATED: property, or any other edge cases that may exist in your org files.