diff options
-rw-r--r-- | org-link-edit.el | 131 | ||||
-rw-r--r-- | test-org-link-edit.el | 352 |
2 files changed, 331 insertions, 152 deletions
diff --git a/org-link-edit.el b/org-link-edit.el index f99dac3..a303adf 100644 --- a/org-link-edit.el +++ b/org-link-edit.el @@ -29,10 +29,10 @@ ;; There are four commands, all which operate when point is on an Org ;; link. ;; -;; - org-link-edit-forward-slurp-word -;; - org-link-edit-backward-slurp-word -;; - org-link-edit-forward-barf-word -;; - org-link-edit-backward-barf-word +;; - org-link-edit-forward-slurp +;; - org-link-edit-backward-slurp +;; - org-link-edit-forward-barf +;; - org-link-edit-backward-barf ;; ;; Org Link Edit doesn't bind these commands to any keys. Finding ;; good keys for these commands is difficult because, while it's @@ -47,10 +47,10 @@ ;; (define-key org-mode-map YOUR-KEY ;; (defhydra hydra-org-link-edit () ;; "Org Link Edit" -;; ("j" org-link-edit-forward-slurp-word "forward slurp") -;; ("k" org-link-edit-forward-barf-word "forward barf") -;; ("u" org-link-edit-backward-slurp-word "backward slurp") -;; ("i" org-link-edit-backward-barf-word "backward barf") +;; ("j" org-link-edit-forward-slurp "forward slurp") +;; ("k" org-link-edit-forward-barf "forward barf") +;; ("u" org-link-edit-backward-slurp "backward slurp") +;; ("i" org-link-edit-backward-barf "backward barf") ;; ("q" nil "cancel"))) ;; ;; [1] https://github.com/abo-abo/hydra @@ -96,9 +96,35 @@ The list includes (t (error "What am I looking at?")))))) +(defun org-link-edit--forward-blob (n &optional no-punctuation) + "Move forward N blobs (backward if N is negative). + +A block of non-whitespace characters is a blob. If +NO-PUNCTUATION is non-nil, trailing punctuation characters are +not considered part of the blob when going in the forward +direction. + +If the edge of the buffer is reached before completing the +movement, return nil. Otherwise, return t." + (let* ((forward-p (> n 0)) + (nblobs (abs n)) + (skip-func (if forward-p 'skip-syntax-forward 'skip-syntax-backward)) + skip-func-retval) + (while (/= nblobs 0) + (funcall skip-func " ") + (setq skip-func-retval (funcall skip-func "^ ")) + (setq nblobs (1- nblobs))) + (when (and forward-p no-punctuation) + (let ((punc-tail-offset (save-excursion (skip-syntax-backward ".")))) + ;; Don't consider trailing punctuation as part of the blob + ;; unless the whole blob consists of punctuation. + (unless (= skip-func-retval (- punc-tail-offset)) + (goto-char (+ (point) punc-tail-offset))))) + (/= skip-func-retval 0))) + ;;;###autoload -(defun org-link-edit-forward-slurp-word (&optional n) - "Slurp N trailing words into link's description. +(defun org-link-edit-forward-slurp (&optional n) + "Slurp N trailing blobs into link's description. The \[\[http://orgmode.org/\]\[Org mode\]\] site @@ -107,22 +133,26 @@ The list includes The \[\[http://orgmode.org/\]\[Org mode site\]\] +A blob is a block of non-whitespace characters. When slurping +forward, trailing punctuation characters are not considered part +of a blob. + After slurping, return the slurped text and move point to the beginning of the link. -If N is negative, slurp leading words instead of trailing words." +If N is negative, slurp leading blobs instead of trailing blobs." (interactive "p") (setq n (or n 1)) (cond ((= n 0)) ((< n 0) - (org-link-edit-backward-slurp-word (- n))) + (org-link-edit-backward-slurp (- n))) (t (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) (goto-char (save-excursion (goto-char end) - (or (forward-word n) - (user-error "Not enough words after the link")) + (or (org-link-edit--forward-blob n 'no-punctuation) + (user-error "Not enough blobs after the link")) (point))) (let ((slurped (buffer-substring-no-properties end (point)))) (setq slurped (replace-regexp-in-string "\n" " " slurped)) @@ -137,8 +167,8 @@ If N is negative, slurp leading words instead of trailing words." slurped))))) ;;;###autoload -(defun org-link-edit-backward-slurp-word (&optional n) - "Slurp N leading words into link's description. +(defun org-link-edit-backward-slurp (&optional n) + "Slurp N leading blobs into link's description. The \[\[http://orgmode.org/\]\[Org mode\]\] site @@ -147,23 +177,24 @@ If N is negative, slurp leading words instead of trailing words." \[\[http://orgmode.org/\]\[The Org mode\]\] site +A blob is a block of non-whitespace characters. + After slurping, return the slurped text and move point to the beginning of the link. -If N is negative, slurp trailing words instead of leading -words." +If N is negative, slurp trailing blobs instead of leading blobs." (interactive "p") (setq n (or n 1)) (cond ((= n 0)) ((< n 0) - (org-link-edit-forward-slurp-word (- n))) + (org-link-edit-forward-slurp (- n))) (t (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) (goto-char (save-excursion (goto-char beg) - (or (forward-word (- n)) - (user-error "Not enough words before the link")) + (or (org-link-edit--forward-blob (- n)) + (user-error "Not enough blobs before the link")) (point))) (let ((slurped (buffer-substring-no-properties (point) beg))) (when (and (= (length desc) 0) @@ -177,43 +208,43 @@ words." (goto-char beg) slurped))))) -(defun org-link-edit--split-first-words (string n) - "Split STRING into (N first words . other) cons cell. -'N first words' contains all text from the start of STRING up to -the start of the N+1 word. 'other' includes the remaining text -of STRING. If the number of words in STRING is fewer than N, +(defun org-link-edit--split-first-blobs (string n) + "Split STRING into (N first blobs . other) cons cell. +'N first blobs' contains all text from the start of STRING up to +the start of the N+1 blob. 'other' includes the remaining text +of STRING. If the number of blobs in STRING is fewer than N, 'other' is nil." (when (< n 0) (user-error "N cannot be negative")) (with-temp-buffer (insert string) (goto-char (point-min)) (with-syntax-table org-mode-syntax-table - (let ((within-bound (forward-word n))) - (skip-syntax-forward "^w") + (let ((within-bound (org-link-edit--forward-blob n))) + (skip-syntax-forward " ") (cons (buffer-substring 1 (point)) (and within-bound (buffer-substring (point) (point-max)))))))) -(defun org-link-edit--split-last-words (string n) - "Split STRING into (other . N last words) cons cell. -'N last words' contains all text from the end of STRING back to -the end of the N+1 last word. 'other' includes the remaining text -of STRING. If the number of words in STRING is fewer than N, -'other' is nil." +(defun org-link-edit--split-last-blobs (string n) + "Split STRING into (other . N last blobs) cons cell. +'N last blobs' contains all text from the end of STRING back to +the end of the N+1 last blob. 'other' includes the remaining +text of STRING. If the number of blobs in STRING is fewer than +N, 'other' is nil." (when (< n 0) (user-error "N cannot be negative")) (with-temp-buffer (insert string) (goto-char (point-max)) (with-syntax-table org-mode-syntax-table - (let ((within-bound (forward-word (- n)))) - (skip-syntax-backward "^w") + (let ((within-bound (org-link-edit--forward-blob (- n)))) + (skip-syntax-backward " ") (cons (and within-bound (buffer-substring 1 (point))) (buffer-substring (point) (point-max))))))) ;;;###autoload -(defun org-link-edit-forward-barf-word (&optional n) - "Barf N trailing words from link's description. +(defun org-link-edit-forward-barf (&optional n) + "Barf N trailing blobs from link's description. The \[\[http://orgmode.org/\]\[Org mode\]\] site @@ -222,23 +253,25 @@ of STRING. If the number of words in STRING is fewer than N, The \[\[http://orgmode.org/\]\[Org\]\] mode site +A blob is a block of non-whitespace characters. + After barfing, return the barfed text and move point to the beginning of the link. -If N is negative, barf leading words instead of trailing words." +If N is negative, barf leading blobs instead of trailing blobs." (interactive "p") (setq n (or n 1)) (cond ((= n 0)) ((< n 0) - (org-link-edit-backward-barf-word (- n))) + (org-link-edit-backward-barf (- n))) (t (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) (when (= (length desc) 0) (user-error "Link has no description")) - (pcase-let ((`(,new-desc . ,barfed) (org-link-edit--split-last-words + (pcase-let ((`(,new-desc . ,barfed) (org-link-edit--split-last-blobs desc n))) - (unless new-desc (user-error "Not enough words in description")) + (unless new-desc (user-error "Not enough blobs in description")) (delete-region beg end) (insert (org-make-link-string link new-desc)) (if (string= new-desc "") @@ -252,8 +285,8 @@ If N is negative, barf leading words instead of trailing words." barfed))))) ;;;###autoload -(defun org-link-edit-backward-barf-word (&optional n) - "Barf N leading words from link's description. +(defun org-link-edit-backward-barf (&optional n) + "Barf N leading blobs from link's description. The \[\[http://orgmode.org/\]\[Org mode\]\] site @@ -262,23 +295,25 @@ If N is negative, barf leading words instead of trailing words." The Org \[\[http://orgmode.org/\]\[mode\]\] site +A blob is a block of non-whitespace characters. + After barfing, return the barfed text and move point to the beginning of the link. -If N is negative, barf trailing words instead of leading words." +If N is negative, barf trailing blobs instead of leading blobs." (interactive "p") (setq n (or n 1)) (cond ((= n 0)) ((< n 0) - (org-link-edit-forward-barf-word (- n))) + (org-link-edit-forward-barf (- n))) (t (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) (when (= (length desc) 0) (user-error "Link has no description")) - (pcase-let ((`(,barfed . ,new-desc) (org-link-edit--split-first-words + (pcase-let ((`(,barfed . ,new-desc) (org-link-edit--split-first-blobs desc n))) - (unless new-desc (user-error "Not enough words in description")) + (unless new-desc (user-error "Not enough blobs in description")) (delete-region beg end) (insert (org-make-link-string link new-desc)) (when (string= new-desc "") diff --git a/test-org-link-edit.el b/test-org-link-edit.el index c6b3535..c238ec8 100644 --- a/test-org-link-edit.el +++ b/test-org-link-edit.el @@ -46,47 +46,63 @@ otherwise place the point at the beginning of the inserted text." ;;; Slurping -(ert-deftest test-org-link-edit/forward-slurp-word () - "Test `org-link-edit-forward-slurp-word'." - ;; Slurp one word into plain link. +(ert-deftest test-org-link-edit/forward-slurp () + "Test `org-link-edit-forward-slurp'." + ;; Slurp one blob into plain link. (should (string= "\[\[http://orgmode.org/\]\[Org's\]\] website is" (org-test-with-temp-text "http://orgmode.org/ Org's website is" - (org-link-edit-forward-slurp-word) + (org-link-edit-forward-slurp) (buffer-string)))) - ;; Slurp one word into empty bracket link. + ;; Slurp one blob into empty bracket link. (should (string= "\[\[http://orgmode.org/\]\[Org's\]\] website is" (org-test-with-temp-text "\[\[http://orgmode.org/\]\] Org's website is" - (org-link-edit-forward-slurp-word) + (org-link-edit-forward-slurp) (buffer-string)))) - ;; Slurp one word into bracket link. + ;; Slurp one blob into bracket link. (should (string= "\[\[http://orgmode.org/\]\[Org's website\]\] is" (org-test-with-temp-text "\[\[http://orgmode.org/\]\[Org's\]\] website is" - (org-link-edit-forward-slurp-word) + (org-link-edit-forward-slurp) (buffer-string)))) - ;; Slurp two words into plain link. + ;; Slurp one blob, but not trailing punctuation, into bracket link. + (should + (string= + "\[\[http://orgmode.org/\]\[Org's website\]\]." + (org-test-with-temp-text + "\[\[http://orgmode.org/\]\[Org's\]\] website." + (org-link-edit-forward-slurp) + (buffer-string)))) + ;; Slurp all-punctuation blob into bracket link. + (should + (string= + "\[\[http://orgmode.org/\]\[Org's .?.?\]\]" + (org-test-with-temp-text + "\[\[http://orgmode.org/\]\[Org's\]\] .?.?" + (org-link-edit-forward-slurp) + (buffer-string)))) + ;; Slurp two blobs into plain link. (should (string= "\[\[http://orgmode.org/\]\[Org's website\]\] is" (org-test-with-temp-text "http://orgmode.org/ Org's website is" - (org-link-edit-forward-slurp-word 2) + (org-link-edit-forward-slurp 2) (buffer-string)))) - ;; Slurp two words into bracket link. + ;; Slurp two blobs into bracket link. (should (string= "\[\[http://orgmode.org/\]\[Org's website is\]\]" (org-test-with-temp-text "\[\[http://orgmode.org/\]\[Org's\]\] website is" - (org-link-edit-forward-slurp-word 2) + (org-link-edit-forward-slurp 2) (buffer-string)))) ;; Slurp new line as space. (should @@ -95,73 +111,89 @@ otherwise place the point at the beginning of the inserted text." (org-test-with-temp-text "\[\[http://orgmode.org/\]\[Org's\]\] website is" - (org-link-edit-forward-slurp-word 1) + (org-link-edit-forward-slurp 1) (buffer-string)))) - ;; Slurp word that has no whitespace. + ;; Slurp blob that has no whitespace. (should (string= "\[\[http://orgmode.org/\]\[website\]\]" (org-test-with-temp-text "\[\[http://orgmode.org/\]\]website" - (org-link-edit-forward-slurp-word 1) + (org-link-edit-forward-slurp 1) (buffer-string)))) - ;; Slurp word beginning with non-word, non-whitespace character. + ;; Slurp blob that isn't separated from link by whitespace. (should (string= "\[\[http://orgmode.org/\]\[-website\]\]" (org-test-with-temp-text "\[\[http://orgmode.org/\]\]-website" - (org-link-edit-forward-slurp-word 1) + (org-link-edit-forward-slurp 1) (buffer-string)))) - ;; Slurp beyond the number of present words. + ;; Slurp beyond the number of present blobs. (should-error (org-test-with-temp-text "\[\[http://orgmode.org/\]\[Org's\]\] website is" - (org-link-edit-forward-slurp-word 3) + (org-link-edit-forward-slurp 3) (buffer-string)) :type (list 'user-error))) -(ert-deftest test-org-link-edit/backward-slurp-word () - "Test `org-link-edit-backward-slurp-word'." - ;; Slurp one word into plain link. +(ert-deftest test-org-link-edit/backward-slurp () + "Test `org-link-edit-backward-slurp'." + ;; Slurp one blob into plain link. (should (string= "Here \[\[http://orgmode.org/\]\[is\]\] Org's website" (org-test-with-temp-text "Here is <point>http://orgmode.org/ Org's website" - (org-link-edit-backward-slurp-word) + (org-link-edit-backward-slurp) (buffer-string)))) - ;; Slurp one word into empty bracket link. + ;; Slurp one blob into empty bracket link. (should (string= "Here \[\[http://orgmode.org/\]\[is\]\] Org's website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\] Org's website" - (org-link-edit-backward-slurp-word) + (org-link-edit-backward-slurp) (buffer-string)))) - ;; Slurp one word into bracket link. + ;; Slurp one blob into bracket link. (should (string= "Here \[\[http://orgmode.org/\]\[is Org's\]\] website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-slurp-word) + (org-link-edit-backward-slurp) + (buffer-string)))) + ;; Slurp one blob with trailing punctuation into bracket link. + (should + (string= + "Here \[\[http://orgmode.org/\]\[is: Org's\]\] website." + (org-test-with-temp-text + "Here is: <point>\[\[http://orgmode.org/\]\[Org's\]\] website." + (org-link-edit-backward-slurp) + (buffer-string)))) + ;; Slurp all-punctuation blob into bracket link. + (should + (string= + "Here \[\[http://orgmode.org/\]\[... Org's\]\] website." + (org-test-with-temp-text + "Here ... <point>\[\[http://orgmode.org/\]\[Org's\]\] website." + (org-link-edit-backward-slurp) (buffer-string)))) - ;; Slurp two words into plain link. + ;; Slurp two blobs into plain link. (should (string= "\[\[http://orgmode.org/\]\[Here is\]\] Org's website" (org-test-with-temp-text "Here is <point>http://orgmode.org/ Org's website" - (org-link-edit-backward-slurp-word 2) + (org-link-edit-backward-slurp 2) (buffer-string)))) - ;; Slurp two words into bracket link. + ;; Slurp two blobs into bracket link. (should (string= "\[\[http://orgmode.org/\]\[Here is Org's\]\] website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-slurp-word 2) + (org-link-edit-backward-slurp 2) (buffer-string)))) ;; Slurp new line as space. (should @@ -170,149 +202,189 @@ website is" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's website\]\]" - (org-link-edit-backward-slurp-word 1) + (org-link-edit-backward-slurp 1) (buffer-string)))) - ;; Slurp word that has no whitespace. + ;; Slurp blob that has no whitespace. (should (string= "Here \[\[http://orgmode.org/\]\[is\]\] Org's website" (org-test-with-temp-text "Here is<point>\[\[http://orgmode.org/\]\] Org's website" - (org-link-edit-backward-slurp-word 1) + (org-link-edit-backward-slurp 1) (buffer-string)))) - ;; Slurp word ending with non-word, non-whitespace character. + ;; Slurp blob that isn't separated from link by whitespace. (should (string= "Here \[\[http://orgmode.org/\]\[is-\]\] Org's website" (org-test-with-temp-text "Here is-<point>\[\[http://orgmode.org/\]\] Org's website" - (org-link-edit-backward-slurp-word 1) + (org-link-edit-backward-slurp 1) (buffer-string)))) - ;; Slurp beyond the number of present words. + ;; Slurp beyond the number of present blobs. (should-error (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-slurp-word 3) + (org-link-edit-backward-slurp 3) (buffer-string)) :type (list 'user-error))) (ert-deftest test-org-link-edit/slurp-negative-argument () - "Test `org-link-edit-forward-slurp-word' and -`org-link-edit-backward-slurp-word' with negative arguments." + "Test `org-link-edit-forward-slurp' and +`org-link-edit-backward-slurp' with negative arguments." (should (string= (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-forward-slurp-word 1) + (org-link-edit-forward-slurp 1) (buffer-string)) (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-slurp-word -1) + (org-link-edit-backward-slurp -1) (buffer-string)))) (should (string= (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-forward-slurp-word -1) + (org-link-edit-forward-slurp -1) (buffer-string)) (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-slurp-word) + (org-link-edit-backward-slurp) (buffer-string))))) ;;; Barfing -(ert-deftest test-org-link-edit/forward-barf-word () - "Test `org-link-edit-forward-barf-word'." - ;; Barf last word. +(ert-deftest test-org-link-edit/forward-barf () + "Test `org-link-edit-forward-barf'." + ;; Barf last blob. (should (string= "Org's \[\[http://orgmode.org/\]\] website is" (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website\]\] is" - (org-link-edit-forward-barf-word) + (org-link-edit-forward-barf) + (buffer-string)))) + ;; Barf last blob with puctuation. + (should + (string= + "Org's \[\[http://orgmode.org/\]\] website," + (org-test-with-temp-text + "Org's <point>\[\[http://orgmode.org/\]\[website,\]\]" + (org-link-edit-forward-barf) + (buffer-string)))) + ;; Barf last blob, all punctuation. + (should + (string= + "Org's \[\[http://orgmode.org/\]\] ..." + (org-test-with-temp-text + "Org's <point>\[\[http://orgmode.org/\]\[...\]\]" + (org-link-edit-forward-barf) (buffer-string)))) - ;; Barf two last words. + ;; Barf two last blobs. (should (string= "Org's \[\[http://orgmode.org/\]\] website is" (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website is\]\]" - (org-link-edit-forward-barf-word 2) + (org-link-edit-forward-barf 2) (buffer-string)))) - ;; Barf one word, not last. + ;; Barf one blob, not last. (should (string= "Org's \[\[http://orgmode.org/\]\[website\]\] is" (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website is\]\]" - (org-link-edit-forward-barf-word 1) + (org-link-edit-forward-barf 1) (buffer-string)))) - ;; Barf beyond the number of present words. + ;; Barf beyond the number of present blobs. (should-error (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website is\]\]" - (org-link-edit-forward-barf-word 3) + (org-link-edit-forward-barf 3) (buffer-string)) :type (list 'user-error))) -(ert-deftest test-org-link-edit/backward-barf-word () - "Test `org-link-edit-backward-barf-word'." - ;; Barf last word. +(ert-deftest test-org-link-edit/backward-barf () + "Test `org-link-edit-backward-barf'." + ;; Barf last blob. (should (string= "Org's website \[\[http://orgmode.org/\]\] is" (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website\]\] is" - (org-link-edit-backward-barf-word) + (org-link-edit-backward-barf) + (buffer-string)))) + ;; Barf last blob with puctuation. + (should + (string= + "Org's website: \[\[http://orgmode.org/\]\]" + (org-test-with-temp-text + "Org's <point>\[\[http://orgmode.org/\]\[website:\]\]" + (org-link-edit-backward-barf) (buffer-string)))) - ;; Barf two last words. + ;; Barf last all-puctuation blob. + (should + (string= + "Org's ... \[\[http://orgmode.org/\]\]" + (org-test-with-temp-text + "Org's <point>\[\[http://orgmode.org/\]\[...\]\]" + (org-link-edit-backward-barf) + (buffer-string)))) + ;; Barf two last blobs. (should (string= "Org's website is \[\[http://orgmode.org/\]\]" (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website is\]\]" - (org-link-edit-backward-barf-word 2) + (org-link-edit-backward-barf 2) (buffer-string)))) - ;; Barf one word, not last. + ;; Barf one blob, not last. (should (string= "Org's website \[\[http://orgmode.org/\]\[is\]\]" (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website is\]\]" - (org-link-edit-backward-barf-word 1) + (org-link-edit-backward-barf 1) + (buffer-string)))) + ;; Barf one blob with punctuation, not last. + (should + (string= + "Org's website. \[\[http://orgmode.org/\]\[is\]\]" + (org-test-with-temp-text + "Org's <point>\[\[http://orgmode.org/\]\[website. is\]\]" + (org-link-edit-backward-barf 1) (buffer-string)))) - ;; Barf beyond the number of present words. + ;; Barf beyond the number of present blobs. (should-error (org-test-with-temp-text "Org's <point>\[\[http://orgmode.org/\]\[website is\]\]" - (org-link-edit-backward-barf-word 3) + (org-link-edit-backward-barf 3) (buffer-string)) :type (list 'user-error))) (ert-deftest test-org-link-edit/barf-negative-argument () - "Test `org-link-edit-forward-barf-word' and -`org-link-edit-backward-barf-word' with negative arguments." + "Test `org-link-edit-forward-barf' and +`org-link-edit-backward-barf' with negative arguments." (should (string= (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-forward-barf-word 1) + (org-link-edit-forward-barf 1) (buffer-string)) (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-barf-word -1) + (org-link-edit-backward-barf -1) (buffer-string)))) (should (string= (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-forward-barf-word -1) + (org-link-edit-forward-barf -1) (buffer-string)) (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-barf-word) + (org-link-edit-backward-barf) (buffer-string))))) @@ -327,35 +399,35 @@ website is" ;; space. (ert-deftest test-org-link-edit/slurp-barf-round-trip () - "Test `org-link-edit-forward-barf-word' and -`org-link-edit-backward-barf-word' reversibility." + "Test `org-link-edit-forward-barf' and +`org-link-edit-backward-barf' reversibility." (should (string= "Here is \[\[http://orgmode.org/\]\[Org's\]\] website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-forward-barf-word 1) - (org-link-edit-forward-slurp-word 1) + (org-link-edit-forward-barf 1) + (org-link-edit-forward-slurp 1) (buffer-string)))) (should (string= "Here is \[\[http://orgmode.org/\]\] Org's website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\] Org's website" - (org-link-edit-forward-slurp-word 1) - (org-link-edit-forward-barf-word 1) + (org-link-edit-forward-slurp 1) + (org-link-edit-forward-barf 1) (buffer-string)))) (should (string= "Here is \[\[http://orgmode.org/\]\[Org's\]\] website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-barf-word 1) - (org-link-edit-backward-slurp-word 1) + (org-link-edit-backward-barf 1) + (org-link-edit-backward-slurp 1) (buffer-string)))) (should (string= "Here is \[\[http://orgmode.org/\]\[Org's\]\] website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-backward-slurp-word 1) - (org-link-edit-backward-barf-word 1) + (org-link-edit-backward-slurp 1) + (org-link-edit-backward-barf 1) (buffer-string)))) ;; Failed round trip because of newline. (should @@ -363,8 +435,8 @@ website is" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\[Org's\]\] website" - (org-link-edit-forward-slurp-word 1) - (org-link-edit-forward-barf-word 1) + (org-link-edit-forward-slurp 1) + (org-link-edit-forward-barf 1) (buffer-string)))) ;; Failed round trip because of empty description and more than one ;; whitespace. @@ -372,8 +444,8 @@ website" (string= "Here is \[\[http://orgmode.org/\]\] website" (org-test-with-temp-text "Here is <point>\[\[http://orgmode.org/\]\] website" - (org-link-edit-forward-slurp-word 1) - (org-link-edit-forward-barf-word 1) + (org-link-edit-forward-slurp 1) + (org-link-edit-forward-barf 1) (buffer-string))))) @@ -394,35 +466,107 @@ website" (should (string= link "http://orgmode.org/")) (should (string= desc "org")))) -(ert-deftest test-org-link-edit/split-first-words () - "Test `org-link-edit--split-first-words'." - ;; Single word, n = 1 +(ert-deftest test-org-link-edit/forward-blob () + "Test `org-link-edit--forward-blob'." + ;; Move forward one blob. + (should + (string= + "one" + (org-test-with-temp-text "one two" + (org-link-edit--forward-blob 1) + (buffer-substring (point-min) (point))))) + ;; Move forward one blob with point mid. + (should + (string= + "one" + (org-test-with-temp-text "o<point>ne two" + (org-link-edit--forward-blob 1) + (buffer-substring (point-min) (point))))) + ;; Move forward two blobs. + (should + (string= + "one two" + (org-test-with-temp-text "one two" + (org-link-edit--forward-blob 2) + (buffer-substring (point-min) (point))))) + ;; Move forward blob, including punctuation. + (should + (string= + "one." + (org-test-with-temp-text "one." + (org-link-edit--forward-blob 1) + (buffer-substring (point-min) (point))))) + ;; Move forward blob, adjusting for punctuation. + (should + (string= + "one" + (org-test-with-temp-text "one." + (org-link-edit--forward-blob 1 t) + (buffer-substring (point-min) (point))))) + ;; Move forward blob consisting of only punctuation characters. + (should + (string= + "...." + (org-test-with-temp-text "...." + (org-link-edit--forward-blob 1 t) + (buffer-substring (point-min) (point))))) + ;; Move backward one blob. + (should + (string= + "two" + (org-test-with-temp-text "one two<point>" + (org-link-edit--forward-blob -1) + (buffer-substring (point) (point-max))))) + ;; Move backward two blobs. + (should + (string= + "one two" + (org-test-with-temp-text "one two<point>" + (org-link-edit--forward-blob -2) + (buffer-substring (point) (point-max))))) + ;; Move backward one blobs, including punctuation. + (should + (string= + ".two." + (org-test-with-temp-text "one .two.<point>" + (org-link-edit--forward-blob -1) + (buffer-substring (point) (point-max))))) + ;; Move beyond last blob. + (org-test-with-temp-text "one two" + (should (org-link-edit--forward-blob 1)) + (should-not (org-link-edit--forward-blob 2)) + (should (string= "one two" + (buffer-substring (point-min) (point)))))) + +(ert-deftest test-org-link-edit/split-firsts () + "Test `org-link-edit--split-first-blobs'." + ;; Single blob, n = 1 (should (equal '("one" . "") - (org-link-edit--split-first-words "one" 1))) - ;; Single word, out-of-bounds + (org-link-edit--split-first-blobs "one" 1))) + ;; Single blob, out-of-bounds (should (equal '("one" . nil) - (org-link-edit--split-first-words "one" 2))) - ;; Multiple words, n = 1 + (org-link-edit--split-first-blobs "one" 2))) + ;; Multiple blobs, n = 1 (should (equal '("one " . "two three") - (org-link-edit--split-first-words "one two three" 1))) - ;; Multiple words, n > 1 + (org-link-edit--split-first-blobs "one two three" 1))) + ;; Multiple blobs, n > 1 (should (equal '("one two " . "three") - (org-link-edit--split-first-words "one two three" 2)))) + (org-link-edit--split-first-blobs "one two three" 2)))) -(ert-deftest test-org-link-edit/split-last-words () - "Test `org-link-edit--split-last-words'." - ;; Single word, n = 1 +(ert-deftest test-org-link-edit/split-lasts () + "Test `org-link-edit--split-last-blobs'." + ;; Single blob, n = 1 (should (equal '("" . "one") - (org-link-edit--split-last-words "one" 1))) - ;; Single word, out-of-bounds + (org-link-edit--split-last-blobs "one" 1))) + ;; Single blob, out-of-bounds (should (equal '(nil . "one") - (org-link-edit--split-last-words "one" 2))) - ;; Multiple words, n = 1 + (org-link-edit--split-last-blobs "one" 2))) + ;; Multiple blobs, n = 1 (should (equal '("one two" . " three") - (org-link-edit--split-last-words "one two three" 1))) - ;; Multiple words, n > 1 + (org-link-edit--split-last-blobs "one two three" 1))) + ;; Multiple blobs, n > 1 (should (equal '("one" . " two three") - (org-link-edit--split-last-words "one two three" 2)))) + (org-link-edit--split-last-blobs "one two three" 2)))) (provide 'test-org-link-edit) ;;; test-org-link-edit.el ends here |