fix(org-noter): use relative NOTER_DOCUMENT path + start from org buffer context

This commit is contained in:
2026-02-23 21:13:34 +01:00
parent 527693773c
commit 388ae1c49d

View File

@@ -1249,13 +1249,17 @@ Otherwise: runs interactive ement-connect, then opens rooms after sync."
org-noter-insert-note-no-questions nil org-noter-insert-note-no-questions nil
org-noter-use-indirect-buffer nil)) org-noter-use-indirect-buffer nil))
;; Smart org-noter launcher: works seamlessly from a PDF buffer. ;; Smart org-noter launcher: always works from a PDF buffer.
;; Ensures the notes file exists with NOTER_DOCUMENT property before starting, ;; Pre-creates the notes file with correct NOTER_DOCUMENT (relative path),
;; so org-noter never prompts "No document property found". ;; then starts org-noter from the notes file (org-mode context) to avoid
;; org-noter's file-search which requires parent-directory relationship.
(defun my/org-noter-start () (defun my/org-noter-start ()
"Start org-noter for the PDF visible in the current frame. "Start org-noter for the PDF visible in the current frame.
Auto-creates or updates the notes file with the NOTER_DOCUMENT property." Creates the notes file with a correct NOTER_DOCUMENT property (relative path)
if it does not already exist, then starts the session from the notes buffer."
(interactive) (interactive)
;; Ensure org-noter is loaded so its variables are available
(require 'org-noter)
(let* ((pdf-win (if (derived-mode-p 'pdf-view-mode) (let* ((pdf-win (if (derived-mode-p 'pdf-view-mode)
(selected-window) (selected-window)
(cl-find-if (cl-find-if
@@ -1267,26 +1271,36 @@ Auto-creates or updates the notes file with the NOTER_DOCUMENT property."
(with-current-buffer (window-buffer pdf-win) (with-current-buffer (window-buffer pdf-win)
(buffer-file-name))))) (buffer-file-name)))))
(if (not pdf-path) (if (not pdf-path)
;; No PDF open — fall back to standard org-noter ;; No PDF buffer found — fall back to standard org-noter
(org-noter) (org-noter)
;; Build notes file path from PDF basename + notes search path
(let* ((base (file-name-base pdf-path)) (let* ((base (file-name-base pdf-path))
(notes-dir (car org-noter-notes-search-path)) (notes-dir (expand-file-name "notes/" org-directory))
(notes-file (expand-file-name (concat base ".org") notes-dir))) (notes-file (expand-file-name (concat base ".org") notes-dir))
;; Relative path from notes file dir to PDF (what org-noter stores)
(rel-path (file-relative-name pdf-path notes-dir)))
(make-directory notes-dir t) (make-directory notes-dir t)
;; Ensure notes file has NOTER_DOCUMENT property ;; Create or update notes file — insert NOTER_DOCUMENT as relative path
(with-current-buffer (find-file-noselect notes-file) (with-current-buffer (find-file-noselect notes-file)
(when (= (buffer-size) 0)
;; New file: create a proper org-noter heading
(insert (format "* Notes: %s\n:PROPERTIES:\n:NOTER_DOCUMENT: %s\n:END:\n\n"
base rel-path))
(save-buffer))
(unless (save-excursion (unless (save-excursion
(goto-char (point-min)) (goto-char (point-min))
(re-search-forward ":NOTER_DOCUMENT:" nil t)) (re-search-forward ":NOTER_DOCUMENT:" nil t))
;; No NOTER_DOCUMENT found — insert a top-level heading with property ;; Existing file without property: prepend heading
(goto-char (point-min)) (goto-char (point-min))
(insert (format "* Notes: %s\n:PROPERTIES:\n:NOTER_DOCUMENT: %s\n:END:\n\n" (insert (format "* Notes: %s\n:PROPERTIES:\n:NOTER_DOCUMENT: %s\n:END:\n\n"
base pdf-path)) base rel-path))
(save-buffer))) (save-buffer)))
;; Start org-noter from the PDF window ;; Open notes file and start org-noter from there (org-mode context)
(with-selected-window pdf-win ;; This uses the simpler code path that reads NOTER_DOCUMENT directly
(org-noter)))))) (find-file notes-file)
(goto-char (point-min))
(re-search-forward ":NOTER_DOCUMENT:" nil t)
(org-back-to-heading t)
(org-noter)))))
(map! :leader (map! :leader
(:prefix ("o" . "open") (:prefix ("o" . "open")