fix(org-noter): always-create-frame nil + start from PDF window (current frame split)

This commit is contained in:
2026-02-23 22:22:19 +01:00
parent f0b967bc7d
commit 3eca3349f1

View File

@@ -1232,7 +1232,6 @@ Otherwise: runs interactive ement-connect, then opens rooms after sync."
;; Emacs 31 may not autoload dired-read-dir-and-switches early enough, ;; Emacs 31 may not autoload dired-read-dir-and-switches early enough,
;; causing "Symbol's function definition is void" when org-noter starts. ;; causing "Symbol's function definition is void" when org-noter starts.
;; Ensure dired is loaded so all its internal functions are available.
(require 'dired) (require 'dired)
(use-package! org-noter (use-package! org-noter
@@ -1241,21 +1240,25 @@ Otherwise: runs interactive ement-connect, then opens rooms after sync."
(setq org-noter-notes-window-location 'horizontal-split (setq org-noter-notes-window-location 'horizontal-split
;; Directories to search for and create notes files ;; Directories to search for and create notes files
org-noter-notes-search-path (list (expand-file-name "notes/" org-directory)) org-noter-notes-search-path (list (expand-file-name "notes/" org-directory))
;; Default note file name when creating a new session ;; Default note file name candidates
org-noter-default-notes-file-names '("notes.org") org-noter-default-notes-file-names '("notes.org")
;; Remember last position in PDF across sessions ;; Remember last position in PDF across sessions
org-noter-auto-save-last-location t org-noter-auto-save-last-location t
;; Insert note at precise location, not just page level ;; Insert note at precise location, not just page level
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
;; Split in the current frame (PDF + notes side by side), no new frame
org-noter-always-create-frame nil))
;; Smart org-noter launcher: finds the PDF window, repairs broken notes files, ;; Smart org-noter launcher: repairs broken notes files and starts from the PDF window.
;; and auto-answers org-noter's prompts so sessions start without user interaction. ;; Starting from the PDF window (not the notes file) ensures org-noter splits the
;; Uses absolute NOTER_DOCUMENT paths (avoids symlink issues with file-relative-name). ;; current frame — PDF on one side, notes on the other — instead of creating a
;; hidden new frame.
(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.
Repairs any existing notes file with a wrong NOTER_DOCUMENT property, Repairs any wrong NOTER_DOCUMENT property in the notes file, then
then starts the session — no interactive prompts required." starts the session from the PDF window so the split appears in the
current frame."
(interactive) (interactive)
(require 'org-noter) (require 'org-noter)
(let* ((pdf-win (if (derived-mode-p 'pdf-view-mode) (let* ((pdf-win (if (derived-mode-p 'pdf-view-mode)
@@ -1274,32 +1277,33 @@ then starts the session — no interactive prompts required."
(notes-dir (car org-noter-notes-search-path)) (notes-dir (car org-noter-notes-search-path))
(target (expand-file-name notes-name notes-dir))) (target (expand-file-name notes-name notes-dir)))
(make-directory notes-dir t) (make-directory notes-dir t)
;; Repair existing notes file if NOTER_DOCUMENT is wrong (doesn't point to pdf-path). ;; Repair or create notes file with correct NOTER_DOCUMENT (absolute PDF path).
;; Uses absolute path for NOTER_DOCUMENT to avoid symlink resolution issues. ;; Absolute paths avoid symlink ambiguity (~/org/ may be an iCloud symlink).
(when (file-exists-p target)
(with-current-buffer (find-file-noselect target) (with-current-buffer (find-file-noselect target)
(let ((modified nil)) (let ((modified nil))
(save-excursion (save-excursion
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward (if (re-search-forward
(concat "^[ \t]*:" org-noter-property-doc-file ":[ \t]*\\(.*\\)$") (concat "^[ \t]*:" org-noter-property-doc-file ":[ \t]*\\(.*\\)$")
nil t) nil t)
;; Property exists — fix if it doesn't point to pdf-path
(let* ((stored (string-trim (match-string 1))) (let* ((stored (string-trim (match-string 1)))
(expanded (if (file-name-absolute-p stored) (expanded (if (file-name-absolute-p stored)
stored stored
(expand-file-name stored (file-name-directory target))))) (expand-file-name stored (file-name-directory target)))))
(unless (and (file-exists-p expanded) (unless (and (file-exists-p expanded)
(file-equal-p expanded pdf-path)) (file-equal-p expanded pdf-path))
;; Wrong or missing target — replace with absolute path to PDF (replace-match
(replace-match (concat ":" org-noter-property-doc-file (concat ":" org-noter-property-doc-file ": " pdf-path) t t)
": " pdf-path) (setq modified t)))
t t) ;; No property — insert a new org-noter heading
(setq modified t))))) (goto-char (point-min))
(when modified (save-buffer))))) (insert (format "* Notes: %s\n:PROPERTIES:\n:%s: %s\n:END:\n\n"
;; Auto-answer org-noter's two interactive prompts: base org-noter-property-doc-file pdf-path))
;; "What name do you want the notes to have?" -> notes-name (basename.org) (setq modified t)))
;; "Where do you want to save it?" -> target (notes-dir/basename.org) (when modified (save-buffer))))
;; org-noter will then create the heading with the correct NOTER_DOCUMENT itself. ;; Start from the PDF window. cl-letf auto-answers org-noter's prompts for
;; edge cases where it still asks (e.g. multiple candidate files).
(cl-letf* ((orig-cr (symbol-function 'completing-read)) (cl-letf* ((orig-cr (symbol-function 'completing-read))
((symbol-function 'completing-read) ((symbol-function 'completing-read)
(lambda (prompt collection &rest args) (lambda (prompt collection &rest args)