this post was submitted on 20 Nov 2023
1 points (100.0% liked)

Emacs

313 readers
2 users here now

A community for the timeless and infinitely powerful editor. Want to see what Emacs is capable of?!

Get Emacs

Rules

  1. Posts should be emacs related
  2. Be kind please
  3. Yes, we already know: Google results for "emacs" and "vi" link to each other. We good.

Emacs Resources

Emacs Tutorials

Useful Emacs configuration files and distributions

Quick pain-saver tip

founded 1 year ago
MODERATORS
 

I found myself wanting to automatically keep a log of every org-roam entry I work on. After struggling with the capture system for a while, I managed to make something work.

(setf (alist-get "u" org-roam-dailies-capture-templates nil nil #'equal)
      '("updated" plain nil
        :target (file+head+olp "%<%Y-%m-%d>.org" "#+title: %<%Y-%m-%d>\n" ("Updated"))
        :unnarrowed t :prepend t))

(defmacro +save-current-buffer (&rest body)
  "Save current buffer; execute BODY; restore original buffer.
I have found that this macro works under some circumstances in
which ‘save-current-buffer’ does not."
  (declare (indent 0))
  `(let ((original-buffer (current-buffer)))
     (unwind-protect (progn ,@body)
       (when (buffer-name original-buffer)
         (switch-to-buffer original-buffer)))))

(defmacro +edit-to-string (&rest body)
  "Eval BODY in a temporary buffer and extract contents to a string."
  (declare (indent 0))
  `(with-temp-buffer
     ,@body
     (buffer-string)))

(defun +org-element-contents (element)
  "Extract contents of ELEMENT from current buffer."
  (buffer-substring (org-element-property :contents-begin element)
                    (org-element-property :contents-end element)))

(defun +org-roam-updated-on-save ()
  "Log current Org-roam buffer as modified."
  (when (org-roam-buffer-p)
    (let* ((original-buffer (current-buffer))
           (id (org-id-get (point-min)))
           (title (org-get-title))
           (link-text (format "%s\n" (+edit-to-string
                                       (org-insert-link nil id title)))))
      (save-excursion  ;; in case we are currently editing the daily note
        (+save-current-buffer
         (org-roam-dailies-goto-today "u")
         (unless (eq original-buffer (current-buffer))
           (let ((elt (org-element-context)))
             (if (not (eq 'plain-list (org-element-type elt)))
                 (insert "\n- " link-text)
               (org-down-element)
               (cl-loop
                for elt = (org-element-context)
                while (eq 'item (org-element-type elt))
                if (equal link-text (+org-element-contents elt)) return nil
                do (org-forward-element)
                finally (insert "- " link-text))))))))))

(add-hook 'after-save-hook #'+org-roam-updated-on-save)

I originally tried to do this using just capture templates (i.e., just an interpolated Lisp expression, no wrapper code), but I'm not sure that that's possible due to some flexibility problems with the Org-roam capture system.

One thing I'm not happy about is having to use my weird homespun +save-current-buffer macro, but the original save-current-buffer macro doesn't switch back for some reason.

I am kind of? happy about having gotten some experience with the loop macro. I'm still not sure I like it, but I understand better why it exists in a language that doesn't have other, more structure looping facilities.

you are viewing a single comment's thread
view the rest of the comments
[–] reddit_ran@alien.top 1 points 1 year ago (1 children)

Well, this is pretty much a very complex idea, but it's commonly used in software development. I was wondering the usability of it. Maybe I'm just not seeing it right now, but I'm open to the discussion. Every time I have trouble with understanding those high tech, I always go back to the analogous version of the Zettelkasten system. In that case, if you have a new version of an old node, in that case you just write a new one. You can just start at the very beginning of that node to refer back to the old nodes. You don't have to say all the changes you make for that single node, as long as you're accumulating those new ideas and creating them together. As long as that process of making things together is evolving, then your system is okay. It introduce another layer of complexity without adding the value that is corresponding to that cost. If we go back to the basic philosophy of UNIX design, keep things simple as possible, then I'll put a big question mark on adapting this idea in my workflow right now.

In that case, if you have a new version of an old node, in that case you just write a new one. You can just start at the very beginning of that node to refer back to the old nodes.

If you keep your notes in version control, you can achieve a similar effect with git-auto-commit-mode.