From 0541dd9f5d154b8334bc1e71712d9e689db8d57e Mon Sep 17 00:00:00 2001 From: Peter Mao Date: Mon, 26 Jun 2023 18:20:05 -0700 Subject: [PATCH 1/2] org-noter--sync-notes-rename-in-notes and enable/disable functions `..--sync-notes..' updates org noter references to docs in notes files when they are moved to a different directory. added enable/disable functions for the --sync-..-rename-.. advising functions. --- org-noter-core.el | 87 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 82 insertions(+), 5 deletions(-) diff --git a/org-noter-core.el b/org-noter-core.el index 1228524..209ce33 100644 --- a/org-noter-core.el +++ b/org-noter-core.el @@ -1739,6 +1739,9 @@ This is delegated to each document mode (eg pdf)." DOCUMENT-PATH is the original filename. NEW-DOCUMENT-PATH is the new filename. +Call `org-noter-enable-sync-renames' to enable this feature and +`org-noter-disable-sync-renames' to disable it. + This advice runs after `dired-rename-file' completes successfully on files with `file-name-extension' in `org-noter--doc-extensions'. @@ -1752,8 +1755,8 @@ of the notes file will not be changed, as there may be other documents referenced in the notes file. An `org-noter' session can still be initiated from the notes file, but not vice-versa, nor will future renames of the document be synced in the notes -file. -" +file." + (when (and (member-ignore-case (file-name-extension document-path) org-noter--doc-extensions) (not (file-exists-p document-path)) @@ -1815,9 +1818,63 @@ file. (rename-file notes-path new-notes-path))) (throw 'break t)))))))))))) -(advice-add 'dired-rename-file :after 'org-noter--sync-doc-rename-in-notes) -;;uncomment or eval next line to disable. -;;(advice-remove 'dired-rename-file 'org-noter--sync-doc-rename-in-notes) +(defun org-noter--sync-notes-rename-in-notes (notes-path new-notes-path &optional _ok-if-already-exists) + "Update org-noter references to docs when notes file is moved. + +NOTES-PATH is the original filename. +NEW-NOTES-PATH is the new filename. + +Call `org-noter-enable-sync-renames' to enable this feature and +`org-noter-disable-sync-renames' to disable it. + +This advice runs after `dired-rename-file' moves an '.org' file to +a different directory. + +If the notes file is moved to a path below any of its linked +documents, a warning will be issued, but the sync will proceed. +An `org-noter' session can still be initiated from the notes +file, but not vice-versa, but future renames of the notes file +will continue to sync the document references." + + (when (and (string-equal (file-name-extension notes-path) "org") + (not (file-exists-p notes-path)) + (file-exists-p new-notes-path) + (not (string-equal (file-name-directory notes-path) + (file-name-directory new-notes-path)))) + ;; continue if it is an org file + ;; and the rename was successful + ;; and the directory changes + (let* (;;(document-name (file-name-nondirectory document-path)) + ;;(document-base (file-name-base document-name)) + ( notes-directory (file-name-directory notes-path)) + (new-notes-directory (file-name-directory new-notes-path)) + (problem-path-list nil) + (this-org-file-uses-noter nil)) + + ;; update each document's relative path + (with-temp-buffer + (insert-file-contents new-notes-path) + (org-with-point-at (point-min) + (while (re-search-forward (org-re-property org-noter-property-doc-file) nil t) + (let* (( doc-file-rel-path (match-string 3)) + ( doc-file-abs-path (expand-file-name doc-file-rel-path notes-directory)) + (new-doc-file-rel-path (file-relative-name doc-file-abs-path new-notes-directory))) + (setq this-org-file-uses-noter t) + ;; sync the document path to the new notes file + (org-set-property org-noter-property-doc-file new-doc-file-rel-path) + (next-line) + ;; add problematic paths to the list + (when (string-prefix-p "../" new-doc-file-rel-path) + (push new-doc-file-rel-path problem-path-list))))) + ;; warn against docs that reside above notes in path + (when problem-path-list + (let ((msg (format-message + "Notes file has moved below some documents. `org-noter' will not be able to find the notes file from the document path for these files:"))) + (dolist (doc-path problem-path-list) + (setq msg (concat msg (format-message "\n%s" doc-path)))) + (display-warning 'org-noter msg :warning))) + (when this-org-file-uses-noter + (write-file new-notes-path nil)))))) ;; -------------------------------------------------------------------------------- ;;; User commands @@ -2466,6 +2523,26 @@ As such, it will only work when the notes window exists." (user-error "There is no next note")))) (select-window (org-noter--get-doc-window))) +(defun org-noter-enable-sync-renames () + "Enable `dired-rename-file' advice for moving docs and notes. +Enables `org-noter--sync-doc-rename-in-notes' and +`org-noter--sync-notes-rename-in-notes' as advice :after +`dired-rename-file'. + +In dired, this affects the renaming of supported document files +and .org files." + (interactive) + (advice-add 'dired-rename-file :after 'org-noter--sync-doc-rename-in-notes) + (advice-add 'dired-rename-file :after 'org-noter--sync-notes-rename-in-notes)) + +(defun org-noter-disable-sync-renames () + "Disable `dired-rename-file' advice for moving docs and notes. +Run this if you change your mind about using the rename +synchronization features." + (interactive) + (advice-remove 'dired-rename-file 'org-noter--sync-doc-rename-in-notes) + (advice-remove 'dired-rename-file 'org-noter--sync-notes-rename-in-notes)) + (define-minor-mode org-noter-doc-mode "Minor mode for the document buffer. Keymap: From f2d5188d219a6b48e35ccd14d709410bf8fb61e7 Mon Sep 17 00:00:00 2001 From: Peter Mao Date: Mon, 26 Jun 2023 18:34:49 -0700 Subject: [PATCH 2/2] Add documentation of sync-rename functions. --- README.org | 6 ++++++ org-noter-core.el | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/README.org b/README.org index 94e99a4..fbbf9ef 100644 --- a/README.org +++ b/README.org @@ -105,6 +105,12 @@ the document to annotate. *** new (all formats) + - ~org-noter-enable-sync-renames~ :: Optional feature to update document + paths when renaming or moving document files or notes files. This allows + you to change your mind later about the names and locations of your + document files and notes files without having to manually update all the + links. + - ~org-noter-toggle-notes-window-location~ (~M-T~) :: Toggle between horizontal and vertical document/notes layout. diff --git a/org-noter-core.el b/org-noter-core.el index 209ce33..293ad28 100644 --- a/org-noter-core.el +++ b/org-noter-core.el @@ -2530,7 +2530,9 @@ Enables `org-noter--sync-doc-rename-in-notes' and `dired-rename-file'. In dired, this affects the renaming of supported document files -and .org files." +and .org files. + +This feature can be turn off with `org-noter-disable-sync-renames'." (interactive) (advice-add 'dired-rename-file :after 'org-noter--sync-doc-rename-in-notes) (advice-add 'dired-rename-file :after 'org-noter--sync-notes-rename-in-notes))