-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobsidian-scan.el
More file actions
74 lines (63 loc) · 2.93 KB
/
Copy pathobsidian-scan.el
File metadata and controls
74 lines (63 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
;; Performance enhancement for obsidian.el using Rust-based scanner
;; Place this code in your Emacs configuration after loading obsidian.el
(defcustom obsidian-scan-binary "obsidian-scan"
"Path to the obsidian-scan binary.
If just the binary name is given, it must be present in your PATH."
:type 'string
:group 'obsidian)
(defvar obsidian--scanner-process-output nil
"Temporary storage for scanner process output.")
(defun obsidian--call-scanner ()
"Call the Rust-based obsidian-scan binary and return its output as a parsed JSON object.
Falls back to the original implementation if the binary is not available."
(condition-case err
(with-temp-buffer
(let ((exit-code (call-process obsidian-scan-binary nil t nil
"--vault" (expand-file-name obsidian-directory))))
(if (= exit-code 0)
(let ((json-array-type 'vector)
(json-object-type 'hash-table))
(goto-char (point-min))
(json-read))
(error "obsidian-scan failed with exit code %d" exit-code))))
(error
(message "Failed to run obsidian-scan: %s. Falling back to original implementation."
(error-message-string err))
nil)))
(defun obsidian--process-scanner-output (data)
"Process the output DATA from obsidian-scan into the internal data structures."
(when data
;; Update tags list
(setq obsidian--tags-list
(gethash "tags" data))
;; Clear and repopulate aliases map
(obsidian--clear-aliases-map)
(maphash (lambda (alias path)
(obsidian--add-alias alias path))
(gethash "aliases" data))
;; Update files cache
(setq obsidian-files-cache
(append (gethash "files" data) nil))
(setq obsidian-cache-timestamp (float-time))))
(defun obsidian--update-with-scanner ()
"Update obsidian data using the Rust scanner if available."
(when-let ((scanner-data (obsidian--call-scanner)))
(obsidian--process-scanner-output scanner-data)
t))
;; Main advice for obsidian-update
(define-advice obsidian-update (:around (orig-fn) use-rust-scanner)
"Advice to use the Rust scanner for updating obsidian data.
Falls back to the original implementation if the scanner is not available."
(unless (obsidian--update-with-scanner)
(funcall orig-fn))
(message "Obsidian tags and aliases updated"))
;; Additional advice to prevent redundant work when scanner succeeds
(define-advice obsidian-reset-cache (:before-while () check-scanner)
"Skip cache reset if we're using the scanner."
(not (obsidian--update-with-scanner)))
(define-advice obsidian-update-tags-list (:before-while () check-scanner)
"Skip tags update if we're using the scanner."
(not (obsidian--update-with-scanner)))
(define-advice obsidian--update-all-from-front-matter (:before-while () check-scanner)
"Skip front matter update if we're using the scanner."
(not (obsidian--update-with-scanner)))