Skip to content

Instantly share code, notes, and snippets.

@otfrom
Created March 9, 2024 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otfrom/6b4d9a54dff2d4edcf16d8a39a5469de to your computer and use it in GitHub Desktop.
Save otfrom/6b4d9a54dff2d4edcf16d8a39a5469de to your computer and use it in GitHub Desktop.
(defun refile-by-created ()
(interactive)
(let* ((capture-header "* From Orgzly")
(created-ts (org-entry-get nil "created"))
(timestamp (cadr (org-timestamp-from-string created-ts)))
(file-date (format "%04d-%02d-%02d"
(plist-get timestamp :year-start)
(plist-get timestamp :month-start)
(plist-get timestamp :day-start))))
(org-cut-subtree)
(org-roam-dailies--capture (date-to-time file-date) t nil)
;; (denote-journal-extras-new-or-existing-entry file-date)
(if (save-excursion
(goto-char (point-min))
(search-forward capture-header nil t))
(progn
(message "Has org-capture header")
(search-forward capture-header nil t)
(message "Outline level %d" (org-outline-level))
(org-end-of-subtree)
(insert "\n")
(org-paste-subtree)
(when (= 1 (org-outline-level))
(org-demote-subtree)))
(progn
(message "Need to insert org-capture header")
(goto-char (point-max))
(insert "\n")
(insert capture-header)
(org-end-of-subtree)
(insert "\n\n")
(message "New subtree %d" (org-outline-level))
(org-paste-subtree)
(org-demote-subtree)))
(save-buffer)))
(defun refile-browser-org-capture-by-date ()
(interactive)
(let
((capture-header "* org-capture")
(capture-date
(save-excursion
(org-narrow-to-subtree)
(org-previous-visible-heading 0)
(goto-char (point-min))
(search-forward-regexp "^Captured On: \\[\\([[:digit:]]\\{4\\}-[[:digit:]]\\{2\\}-[[:digit:]]\\{2\\}\\)")
(widen)
(match-string 1))))
(message "capture-date %s" capture-date)
(org-cut-subtree)
;; (denote-journal-extras-new-or-existing-entry capture-date)
(org-roam-dailies--capture (date-to-time capture-date) t nil)
(if (save-excursion
(goto-char (point-min))
(search-forward capture-header nil t))
(progn
(message "Has org-capture header")
(search-forward capture-header nil t)
(message "Outline level %d" (org-outline-level))
(org-end-of-subtree)
(insert "\n")
(org-paste-subtree)
(when (= 1 (org-outline-level))
(org-demote-subtree)))
(progn
(message "Need to insert org-capture header")
(goto-char (point-max))
(insert "\n")
(insert capture-header)
(org-end-of-subtree)
(insert "\n\n")
(message "New subtree %d" (org-outline-level))
(org-paste-subtree)
(org-demote-subtree)))
(save-buffer)))
(defun link-title (link)
"Extract the title from the ’org-mode’ LINK."
(string-match
(rx
(+ (not (any "]" "[")))
"]["
(group (+ (not (any "]" "["))))
)
;; "[^][]+]\\[\\([^][]+\\)"
link)
(match-string 1 link))
(defun capture-reader-entry ()
(interactive)
(goto-char (point-min))
(message "Looking at: %s" (link-title (org-element-property :title (org-element-at-point))))
(let ((title (link-title (org-element-property :title (org-element-at-point)))))
(copy-region-as-kill (point-min) (point-max))
(org-roam-capture- :goto t
:node (org-roam-node-create :title title))
(yank)
(goto-char (point-min))
(org-next-visible-heading 1)
(next-line)
(fill-region (point) (point-max))))
@sw1nn
Copy link

sw1nn commented Mar 9, 2024

Not an in-depth review, but you don't need the (progn ...) for the else branch of if, its...

(if condition
    then-form
    else-forms
)

although in this case there's enough going on in the else branch that maybe it should its own function? extract method refactoring and all that.

🍰

@otfrom
Copy link
Author

otfrom commented Mar 9, 2024

although in this case there's enough going on in the else branch that maybe it should its own function? extract method refactoring and all that.

thx, yeah the else branch could be less progn, though they've all been moving around a bit and I'm not really happy with them. They don't consistently give me the heading level I'm after and I haven't quite figured out why yet, so I'm expecting that I don't have them nailed down properly. I think I probably need to check where I am a little bit more and I have a feeling I'm not moving around the org doc properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment