(fix): allow title changes that don't modify filename (#1189)

Prevent error when trying to move file to same name

Prevent string matching in org from messing with the current re-search-forward
position which has caused infinite broken search
This commit is contained in:
Herbert Jones
2020-10-15 13:19:15 -05:00
committed by GitHub
parent cbf1b585ac
commit a0c4abf579

View File

@ -1296,15 +1296,12 @@ during the next idle slot."
(org-roam--org-roam-file-p file)) (org-roam--org-roam-file-p file))
(org-roam-db--clear-file (expand-file-name file)))) (org-roam-db--clear-file (expand-file-name file))))
(defun org-roam--replace-link (old-path new-path &optional old-desc new-desc) (defun org-roam--get-link-replacement (old-path new-path old-desc new-desc)
"Replace Org-roam file links with path OLD-PATH to path NEW-PATH. "Create replacement text for link at point if OLD-PATH is a match.
If OLD-DESC is passed, and is not the same as the link Will update link to NEW-PATH. If OLD-DESC is set, and is not the
description, it is assumed that the user has modified the same as the link description, it is assumed that the user has
description, and the description will not be updated. Else, modified the description, and the description will not be
update with NEW-DESC." updated. Else, update with NEW-DESC."
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-link-any-re nil t)
(when-let ((link (org-element-lineage (org-element-context) '(link) t))) (when-let ((link (org-element-lineage (org-element-context) '(link) t)))
(let ((type (org-element-property :type link)) (let ((type (org-element-property :type link))
(path (org-element-property :path link))) (path (org-element-property :path link)))
@ -1316,7 +1313,34 @@ update with NEW-DESC."
(new-label (if (string-equal label old-desc) (new-label (if (string-equal label old-desc)
new-desc new-desc
label))) label)))
(replace-match (org-roam-format-link new-path new-label type))))))))) (org-roam-format-link new-path new-label type))))))
(defun org-roam--replace-link (old-path new-path &optional old-desc new-desc)
"Replace Org-roam file links with path OLD-PATH to path NEW-PATH.
If OLD-DESC is passed, and is not the same as the link
description, it is assumed that the user has modified the
description, and the description will not be updated. Else,
update with NEW-DESC."
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-link-any-re nil t)
(when-let ((new-link (save-match-data
(org-roam--get-link-replacement
old-path new-path old-desc new-desc))))
(replace-match new-link)))))
(defun org-roam--get-relative-link-replacement (old-path)
"Create file-relative link for link at point if needed.
File relative links are assumed to originate from OLD-PATH. The
replaced links are made relative to the current buffer."
(when-let ((link (org-element-lineage (org-element-context) '(link) t)))
(let ((type (org-element-property :type link))
(path (org-element-property :path link)))
(when (and (f-relative-p path)
(org-in-regexp org-link-bracket-re 1))
(let* ((file-path (expand-file-name path (file-name-directory old-path)))
(new-path (org-roam-link-get-path file-path)))
(concat type ":" new-path))))))
(defun org-roam--fix-relative-links (old-path) (defun org-roam--fix-relative-links (old-path)
"Fix file-relative links in current buffer. "Fix file-relative links in current buffer.
@ -1325,15 +1349,9 @@ replaced links are made relative to the current buffer."
(save-excursion (save-excursion
(goto-char (point-min)) (goto-char (point-min))
(while (re-search-forward org-link-any-re nil t) (while (re-search-forward org-link-any-re nil t)
(when-let ((link (org-element-lineage (org-element-context) '(link) t))) (when-let ((new-link (save-match-data
(let ((type (org-element-property :type link)) (org-roam--get-relative-link-replacement old-path))))
(path (org-element-property :path link))) (replace-match new-link nil t nil 1)))))
(when (and (f-relative-p path)
(org-in-regexp org-link-bracket-re 1))
(let* ((file-path (expand-file-name path (file-name-directory old-path)))
(new-path (org-roam-link-get-path file-path)))
(replace-match (concat type ":" new-path)
nil t nil 1))))))))
(defcustom org-roam-rename-file-on-title-change t (defcustom org-roam-rename-file-on-title-change t
"If non-nil, alter the filename on title change. "If non-nil, alter the filename on title change.
@ -1401,10 +1419,11 @@ To be added to `org-roam-title-change-hook'."
(when (string-match-p old-slug file-name) (when (string-match-p old-slug file-name)
(let* ((new-slug (funcall org-roam-title-to-slug-function new-title)) (let* ((new-slug (funcall org-roam-title-to-slug-function new-title))
(new-file-name (replace-regexp-in-string old-slug new-slug file-name))) (new-file-name (replace-regexp-in-string old-slug new-slug file-name)))
(unless (string-match-p file-name new-file-name)
(rename-file file-name new-file-name) (rename-file file-name new-file-name)
(set-visited-file-name new-file-name t t) (set-visited-file-name new-file-name t t)
(add-to-list 'org-roam--file-update-queue new-file-name) (add-to-list 'org-roam--file-update-queue new-file-name)
(org-roam-message "File moved to %S" (abbreviate-file-name new-file-name))))))) (org-roam-message "File moved to %S" (abbreviate-file-name new-file-name))))))))
(defun org-roam--rename-file-advice (old-file new-file-or-dir &rest _args) (defun org-roam--rename-file-advice (old-file new-file-or-dir &rest _args)
"Rename backlinks of OLD-FILE to refer to NEW-FILE-OR-DIR. "Rename backlinks of OLD-FILE to refer to NEW-FILE-OR-DIR.