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?
2 Answers
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)))))))
-
I am confused by the question, but you seem not to be. So how does this answer the question?– NickDMar 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– TianMar 11, 2022 at 1:37
-
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)
.gitignore' have to do with
org-roam'?.gitignore
, I do source control of my org-roam notes because I don't have access to internet accross my machines