fix(org-noter): auto-insert NOTER_DOCUMENT property, never prompt for document path

This commit is contained in:
2026-02-23 21:08:54 +01:00
parent 55861e9381
commit 527693773c

View File

@@ -1249,28 +1249,48 @@ 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 launcher: always start org-noter from the PDF window, ;; Smart org-noter launcher: works seamlessly from a PDF buffer.
;; regardless of which window is currently selected. ;; Ensures the notes file exists with NOTER_DOCUMENT property before starting,
;; Fixes: calling SPC o n from an org buffer while PDF is visible in split. ;; so org-noter never prompts "No document property found".
(defun my/org-noter-smart () (defun my/org-noter-start ()
"Start org-noter, auto-selecting the PDF window in the current frame. "Start org-noter for the PDF visible in the current frame.
If current buffer is pdf-view-mode: start normally. Auto-creates or updates the notes file with the NOTER_DOCUMENT property."
If another visible window shows a PDF: switch to it first, then start.
Otherwise: run standard org-noter (interactive, prompts for document)."
(interactive) (interactive)
(let ((pdf-win (cl-find-if (let* ((pdf-win (if (derived-mode-p 'pdf-view-mode)
(selected-window)
(cl-find-if
(lambda (w) (lambda (w)
(with-current-buffer (window-buffer w) (with-current-buffer (window-buffer w)
(derived-mode-p 'pdf-view-mode))) (derived-mode-p 'pdf-view-mode)))
(window-list (selected-frame))))) (window-list (selected-frame)))))
(if pdf-win (pdf-path (when pdf-win
(with-current-buffer (window-buffer pdf-win)
(buffer-file-name)))))
(if (not pdf-path)
;; No PDF open — fall back to standard org-noter
(org-noter)
;; Build notes file path from PDF basename + notes search path
(let* ((base (file-name-base pdf-path))
(notes-dir (car org-noter-notes-search-path))
(notes-file (expand-file-name (concat base ".org") notes-dir)))
(make-directory notes-dir t)
;; Ensure notes file has NOTER_DOCUMENT property
(with-current-buffer (find-file-noselect notes-file)
(unless (save-excursion
(goto-char (point-min))
(re-search-forward ":NOTER_DOCUMENT:" nil t))
;; No NOTER_DOCUMENT found — insert a top-level heading with property
(goto-char (point-min))
(insert (format "* Notes: %s\n:PROPERTIES:\n:NOTER_DOCUMENT: %s\n:END:\n\n"
base pdf-path))
(save-buffer)))
;; Start org-noter from the PDF window
(with-selected-window pdf-win (with-selected-window pdf-win
(org-noter)) (org-noter))))))
(org-noter))))
(map! :leader (map! :leader
(:prefix ("o" . "open") (:prefix ("o" . "open")
:desc "org-noter (smart)" "n" #'my/org-noter-smart :desc "org-noter" "n" #'my/org-noter-start
:desc "org-noter insert note" "N" #'org-noter-insert-note)) :desc "org-noter insert note" "N" #'org-noter-insert-note))