0

I wrote a function that will update my roam notes .gitignore. I would like to call this function whenever there is a creation or renaming of roam node. What hook should I be using?

5
  • What does .gitignore' have to do with org-roam'?
    – NickD
    Mar 10, 2022 at 22:29
  • There are some org-roam notes that carry a particular file tag that I would like to be git ignored, I wrote a function to retrieve all org-roam files with that file-tag, and write that list to .gitignore, I do source control of my org-roam notes because I don't have access to internet accross my machines
    – Tian
    Mar 11, 2022 at 1:36
  • Why didn't you add all that to the question? How do you expect people to figure that out if you don't tell them? Please take your comment and add it to the question.
    – NickD
    Mar 11, 2022 at 1:42
  • @NickD I don't think it's necessary for the answer I'm seeking
    – Tian
    Mar 11, 2022 at 1:47
  • Then why did you add that detail? If it is not relevant, take it out.
    – NickD
    Mar 11, 2022 at 2:00

2 Answers 2

1

find-file-hook, advice on rename-file and delete-file.

Here is the source code of org-roam-db-autosync-mode you can refer to:

(define-minor-mode org-roam-db-autosync-mode
  "Global minor mode to keep your Org-roam session automatically synchronized.
Through the session this will continue to setup your
buffers (that are Org-roam file visiting), keep track of the
related changes, maintain cache consistency and incrementally
update the currently active database.

If you need to manually trigger resync of the currently active
database, see `org-roam-db-sync' command."
  :group 'org-roam
  :global t
  :init-value nil
  (let ((enabled org-roam-db-autosync-mode))
    (cond
     (enabled
      (add-hook 'find-file-hook  #'org-roam-db-autosync--setup-file-h)
      (add-hook 'kill-emacs-hook #'org-roam-db--close-all)
      (advice-add #'rename-file :after  #'org-roam-db-autosync--rename-file-a)
      (advice-add #'delete-file :before #'org-roam-db-autosync--delete-file-a)
      (org-roam-db-sync))
     (t
      (remove-hook 'find-file-hook  #'org-roam-db-autosync--setup-file-h)
      (remove-hook 'kill-emacs-hook #'org-roam-db--close-all)
      (advice-remove #'rename-file #'org-roam-db-autosync--rename-file-a)
      (advice-remove #'delete-file #'org-roam-db-autosync--delete-file-a)
      (org-roam-db--close-all)
      ;; Disable local hooks for all org-roam buffers
      (dolist (buf (org-roam-buffer-list))
        (with-current-buffer buf
          (remove-hook 'after-save-hook #'org-roam-db-autosync--try-update-on-save-h t)))))))
4
  • I am confused by the question, but you seem not to be. So how does this answer the question?
    – NickD
    Mar 10, 2022 at 22:31
  • @NickD Hi, I don't particularly understand why he needs to update .gitignore either, but back to his question about how to execute certain functions when creating and renaming org roam nodes. Mar 11, 2022 at 1:24
  • @TianshuWang Thanks for the quick reply, I was just wondering if org-roam should have a dedicated hook for this
    – Tian
    Mar 11, 2022 at 1:37
  • @Tian Not, as the aboving code are copied from org-roam Mar 11, 2022 at 2:00
0

According to the answer given by @TianshuWang, I came up with the following function (requires lexical binding).

;;; -*- lexical-binding:t; -*-
(defun add-to-roam-file-hooks (FUNCTION)
  "Adds function to crud event hooks for org roam files only"

  ;; for file saving
  (advice-add
   #'org-roam-db-autosync--setup-file-h :after
   (lambda ()
     (add-hook 'after-save-hook
      (lambda ()
    (when org-roam-db-update-on-save
      (funcall FUNCTION))))
     nil t))

  ;; for file renaming
  (advice-add
   #'org-roam-db-autosync--rename-file-a :after
   (lambda (old-file new-file-or-dir &rest _args)
     (let ((new-file (if (directory-name-p new-file-or-dir)
             (expand-file-name (file-name-nondirectory old-file) new-file-or-dir)
               new-file-or-dir)))
       (setq new-file (expand-file-name new-file))
       (when (org-roam-file-p new-file)
     (funcall FUNCTION)))))

  ;; for file deleting
  (advice-add
   #'org-roam-db-autosync--delete-file-a :before
   (lambda (file &optional _trash)
     (when (and (not (auto-save-file-name-p file))
        (not (backup-file-name-p file))
        (org-roam-file-p file))
       (funcall FUNCTION))))
  )

To use it, evaluate the following

(add-to-roam-file-hooks #'my-func)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.