From 48ebfc757f713484b5427d0ca775fd1ae62f4775 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 3 Mar 2017 00:02:26 -0500 Subject: barf: Fix edge cases where point is beyond link When a point is on a blank space beyond the link but before the next element, org-element-context considers point to be a part of the link. This leads to incorrect behavior (shown in the added tests) when either barfing backward or forward. Avoid these issues by moving point to the beginning of the link before deleting the old link and inserting a new one. As a bonus, this makes some of the later goto-char calculations unnecessary. --- org-link-edit.el | 11 ++++------- test-org-link-edit.el | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 7 deletions(-) diff --git a/org-link-edit.el b/org-link-edit.el index 1323bb9..b3ff941 100644 --- a/org-link-edit.el +++ b/org-link-edit.el @@ -273,14 +273,11 @@ If N is negative, barf leading blobs instead of trailing blobs." (pcase-let ((`(,new-desc . ,barfed) (org-link-edit--split-last-blobs desc n))) (unless new-desc (user-error "Not enough blobs in description")) + (goto-char beg) (delete-region beg end) (insert (org-make-link-string link new-desc)) - (if (string= new-desc "") - ;; Two brackets are dropped when an empty description is - ;; passed to `org-make-link-string'. - (progn (goto-char (- end (+ 2 (length desc)))) - (setq barfed (concat " " barfed))) - (goto-char (- end (- (length desc) (length new-desc))))) + (when (string= new-desc "") + (setq barfed (concat " " barfed))) (insert barfed) (goto-char beg) barfed))))) @@ -315,13 +312,13 @@ If N is negative, barf trailing blobs instead of leading blobs." (pcase-let ((`(,barfed . ,new-desc) (org-link-edit--split-first-blobs desc n))) (unless new-desc (user-error "Not enough blobs in description")) + (goto-char beg) (delete-region beg end) (insert (org-make-link-string link new-desc)) (when (string= new-desc "") (setq barfed (concat barfed " "))) (goto-char beg) (insert barfed) - (goto-char (+ beg (length barfed))) barfed))))) (provide 'org-link-edit) diff --git a/test-org-link-edit.el b/test-org-link-edit.el index e37be95..6cf936a 100644 --- a/test-org-link-edit.el +++ b/test-org-link-edit.el @@ -88,6 +88,15 @@ otherwise place the point at the beginning of the inserted text." "\[\[http://orgmode.org/\]\[Org's\]\] .?.?" (org-link-edit-forward-slurp) (buffer-string)))) + ;; Slurping blob with point beyond link, but technically still + ;; within link element. + (should + (string= + "Org's \[\[http://orgmode.org/\]\[website is\]\]" + (org-test-with-temp-text + "Org's \[\[http://orgmode.org/\]\[website\]\] is" + (org-link-edit-forward-slurp) + (buffer-string)))) ;; Slurp two blobs into plain link. (should (string= @@ -188,6 +197,15 @@ website is" "Here ... \[\[http://orgmode.org/\]\[Org's\]\] website." (org-link-edit-backward-slurp) (buffer-string)))) + ;; Slurping blob with point beyond link, but technically still + ;; within link element. + (should + (string= + "\[\[http://orgmode.org/\]\[Org's website\]\] is" + (org-test-with-temp-text + "Org's \[\[http://orgmode.org/\]\[website\]\] is" + (org-link-edit-backward-slurp) + (buffer-string)))) ;; Slurp two blobs into plain link. (should (string= @@ -283,6 +301,15 @@ website is" "Org's \[\[http://orgmode.org/\]\[website\]\] is" (org-link-edit-forward-barf) (buffer-string)))) + ;; Barfing last blob with point beyond link, but technically still + ;; within link element. + (should + (string= + "Org's \[\[http://orgmode.org/\]\] website is" + (org-test-with-temp-text + "Org's \[\[http://orgmode.org/\]\[website\]\] is" + (org-link-edit-forward-barf) + (buffer-string)))) ;; Barf last blob with puctuation. (should (string= @@ -333,6 +360,15 @@ website is" "Org's \[\[http://orgmode.org/\]\[website\]\] is" (org-link-edit-backward-barf) (buffer-string)))) + ;; Barfing last blob with point beyond link, but technically still + ;; within link element. + (should + (string= + "Org's website \[\[http://orgmode.org/\]\] is" + (org-test-with-temp-text + "Org's \[\[http://orgmode.org/\]\[website\]\] is" + (org-link-edit-backward-barf) + (buffer-string)))) ;; Barf last blob with puctuation. (should (string= -- cgit v1.2.3