diff options
author | Thanos Apollo <[email protected]> | 2024-12-18 19:22:10 +0200 |
---|---|---|
committer | Thanos Apollo <[email protected]> | 2024-12-18 19:22:10 +0200 |
commit | e8de093180d6f8f86262683f6e4ffc27f638dd22 (patch) | |
tree | e1255ba522139ef7e41ef01af57b920dbe71a724 | |
parent | f68e2350cba6578829fec4a2864a86417cd67cb3 (diff) |
Add custom goto-id func & async sync function.
-rw-r--r-- | org-gnosis.el | 63 |
1 files changed, 57 insertions, 6 deletions
diff --git a/org-gnosis.el b/org-gnosis.el index 4b69c82..f7e10c2 100644 --- a/org-gnosis.el +++ b/org-gnosis.el @@ -70,6 +70,8 @@ (defvar org-gnosis-db (emacsql-sqlite-open (locate-user-emacs-file "org-gnosis.db"))) +(defvar org-gnosis-auto-sync--timer nil) + (cl-defun org-gnosis-select (value table &optional (restrictions '1=1) (flatten nil)) "Select VALUE from TABLE, optionally with RESTRICTIONS. @@ -187,9 +189,7 @@ Return the ID if found, else nil." (defun org-gnosis-parse-topic (parsed-data) "Parse topic information from the PARSED-DATA." - (let* ((topic-info (if org-gnosis-denote-p - (org-gnosis--denote-topic) - (org-gnosis-get-data--topic parsed-data))) + (let* ((topic-info (org-gnosis-get-data--topic parsed-data)) (topic-title (nth 0 topic-info)) (topic-tags (nth 1 topic-info)) (topic-id (nth 2 topic-info))) @@ -335,8 +335,9 @@ instead." (interactive) (let* ((title (or title (if org-gnosis-show-tags (org-gnosis-find--with-tags) - (completing-read "Select gnosis node: " - (org-gnosis-select 'title 'nodes))))) + (funcall org-gnosis-completing-read-func + "Select gnosis node: " + (org-gnosis-select 'title 'nodes))))) (file (or file (caar (org-gnosis-select 'file 'nodes `(= title ,title))))) (id (or id (caar (or id (org-gnosis-select 'id 'nodes `(= title ,title)))))) (directory (or directory org-gnosis-dir))) @@ -416,16 +417,66 @@ TEMPLATE: Journaling template, refer to `org-gnosis-journal-templates'." (and (not (file-exists-p file)) (or template (org-gnosis-journal-select-template)))))) +(defun org-gnosis--get-id-at-point () + "Return the Org ID link at point, if any." + (let* ((element (org-element-context)) + (id-link (when (and (eq (org-element-type element) 'link) + (string= (org-element-property :type element) "id")) + (org-element-property :path element)))) + (and id-link id-link))) + + +(defun org-gnosis-goto-id (&optional id) + "Visit file for ID." + (interactive) + (let* ((id (or id (org-gnosis--get-id-at-point))) + (file (caar (org-gnosis-select 'file 'nodes `(= id ,id))))) + (find-file (expand-file-name file org-gnosis-dir)) + (org-gnosis-mode))) + +(defvar-keymap org-gnosis-mode-map + :doc "org-gnosis keymap" + "C-c C-o" #'org-gnosis-goto-id) + (define-minor-mode org-gnosis-mode "Org gnosis mode." :lighter " org-gnosis" - :keymap nil + :keymap org-gnosis-mode-map :global nil :group 'org-gnosis (if org-gnosis-mode (add-hook 'after-save-hook #'org-gnosis-update-file nil t) ;; buffer local hook (remove-hook 'after-save-hook #'org-gnosis-update-file))) +(defvar org-gnosis-db-sync-timer nil + "Timer for org-gnosis-db-sync minor mode.") + +(defvar org-gnosis-db-sync-interval 60 + "Second interval to sync database.") + +(define-minor-mode org-gnosis-db-sync-mode + "Minor mode to automatically run `org-gnosis-db-sync` every 30 seconds." + :lighter nil + :global t + (if org-gnosis-db-sync-mode + (setq org-gnosis-db-sync-timer + (run-with-timer 0 org-gnosis-db-sync-interval #'org-gnosis-db-sync-async)) + (when org-gnosis-db-sync-timer + (cancel-timer org-gnosis-db-sync-timer) + (setq org-gnosis-db-sync-timer nil)))) + +(defun org-gnosis-db-sync-async () + "Run `org-gnosis-db-sync` asynchronously using a subprocess." + (let ((script (concat + "(progn " + "(require 'org-gnosis) " ;; Ensure the necessary package is loaded + "(org-gnosis-db-sync))"))) + (start-process "org-gnosis-db-sync-process" + nil + "emacs" + "--batch" + "--eval" script))) + ;; Org-Gnosis Database (defconst org-gnosis-db-version 1) |