From 99009e8c5ec706b7aa1da6d0f6818c4f78567d40 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 3 Mar 2017 00:10:00 -0500 Subject: Add NEWS file --- NEWS | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 NEWS diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..e4faa59 --- /dev/null +++ b/NEWS @@ -0,0 +1,5 @@ +Org Link Edit NEWS -- history of user-visible changes -*- mode: org; -*- + +* master (unreleased) + +** New features -- cgit v1.2.3 From 5dd5901cf6850f2bfdba64c896d10b017633600e Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 3 Mar 2017 00:10:00 -0500 Subject: org-link-edit--get-link-data: Rename to drop "get" --- org-link-edit.el | 10 +++++----- test-org-link-edit.el | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/org-link-edit.el b/org-link-edit.el index b3ff941..49e9aa0 100644 --- a/org-link-edit.el +++ b/org-link-edit.el @@ -61,7 +61,7 @@ (require 'org-element) (require 'cl-lib) -(defun org-link-edit--get-link-data () +(defun org-link-edit--link-data () "Return list with information about the link at point. The list includes - the position at the start of the link @@ -149,7 +149,7 @@ If N is negative, slurp leading blobs instead of trailing blobs." ((< n 0) (org-link-edit-backward-slurp (- n))) (t - (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) + (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data) (goto-char (save-excursion (goto-char end) (or (org-link-edit--forward-blob n 'no-punctuation) @@ -191,7 +191,7 @@ If N is negative, slurp trailing blobs instead of leading blobs." ((< n 0) (org-link-edit-forward-slurp (- n))) (t - (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) + (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data) (goto-char (save-excursion (goto-char beg) (or (org-link-edit--forward-blob (- n)) @@ -267,7 +267,7 @@ If N is negative, barf leading blobs instead of trailing blobs." ((< n 0) (org-link-edit-backward-barf (- n))) (t - (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) + (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data) (when (= (length desc) 0) (user-error "Link has no description")) (pcase-let ((`(,new-desc . ,barfed) (org-link-edit--split-last-blobs @@ -306,7 +306,7 @@ If N is negative, barf trailing blobs instead of leading blobs." ((< n 0) (org-link-edit-forward-barf (- n))) (t - (cl-multiple-value-bind (beg end link desc) (org-link-edit--get-link-data) + (cl-multiple-value-bind (beg end link desc) (org-link-edit--link-data) (when (= (length desc) 0) (user-error "Link has no description")) (pcase-let ((`(,barfed . ,new-desc) (org-link-edit--split-first-blobs diff --git a/test-org-link-edit.el b/test-org-link-edit.el index 6cf936a..893cec5 100644 --- a/test-org-link-edit.el +++ b/test-org-link-edit.el @@ -514,17 +514,17 @@ website" ;;; Other (ert-deftest test-org-link-edit/get-link-data () - "Test `org-link-edit--get-link-data'." + "Test `org-link-edit--link-data'." ;; Plain link (cl-multiple-value-bind (beg end link desc) (org-test-with-temp-text "http://orgmode.org/" - (org-link-edit--get-link-data)) + (org-link-edit--link-data)) (should (string= link "http://orgmode.org/")) (should-not desc)) ;; Bracket link (cl-multiple-value-bind (beg end link desc) (org-test-with-temp-text "\[\[http://orgmode.org/\]\[org\]\]" - (org-link-edit--get-link-data)) + (org-link-edit--link-data)) (should (string= link "http://orgmode.org/")) (should (string= desc "org")))) -- cgit v1.2.3 From ae8e918c873febd58abfc332ebf8f59448e8fff4 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 3 Mar 2017 00:10:00 -0500 Subject: Add a separate function for testing if point is on a link The slurping and barfing commands are interested in point being on a link, so it is sufficient for them to use org-link-edit--get-link-data and get a user-error when point is not on a link. But an upcoming command will want to know that point is _not_ in a link. --- org-link-edit.el | 14 +++++++++----- test-org-link-edit.el | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/org-link-edit.el b/org-link-edit.el index 49e9aa0..833d620 100644 --- a/org-link-edit.el +++ b/org-link-edit.el @@ -61,6 +61,14 @@ (require 'org-element) (require 'cl-lib) +(defun org-link-edit--on-link-p (&optional element) + (let ((el (or element (org-element-context)))) + ;; Don't use `org-element-lineage' because it isn't available + ;; until Org version 8.3. + (while (and el (not (memq (car el) '(link)))) + (setq el (org-element-property :parent el))) + (eq (car el) 'link))) + (defun org-link-edit--link-data () "Return list with information about the link at point. The list includes @@ -69,11 +77,7 @@ The list includes - the link text - the link description (nil when on a plain link)" (let ((el (org-element-context))) - ;; Don't use `org-element-lineage' because it isn't available - ;; until Org version 8.3. - (while (and el (not (memq (car el) '(link)))) - (setq el (org-element-property :parent el))) - (unless (eq (car el) 'link) + (unless (org-link-edit--on-link-p el) (user-error "Point is not on a link")) (save-excursion (goto-char (org-element-property :begin el)) diff --git a/test-org-link-edit.el b/test-org-link-edit.el index 893cec5..4f006e7 100644 --- a/test-org-link-edit.el +++ b/test-org-link-edit.el @@ -513,6 +513,28 @@ website" ;;; Other +(ert-deftest test-org-link-edit/on-link-p () + "Test `org-link-edit--on-link-p'." + ;; On plain link + (should + (org-test-with-temp-text "http://orgmode.org/" + (org-link-edit--on-link-p))) + ;; On bracket link + (should + (org-test-with-temp-text "\[\[http://orgmode.org/\]\[org\]\]" + (org-link-edit--on-link-p))) + ;; Point beyond link, but technically still within link element. + (should + (org-test-with-temp-text "\[\[http://orgmode.org/\]\[org\]\] " + (org-link-edit--on-link-p))) + ;; Not on a link + (should-not + (org-test-with-temp-text " \[\[http://orgmode.org/\]\[org\]\]" + (org-link-edit--on-link-p))) + (should-not + (org-test-with-temp-text "not a link" + (org-link-edit--on-link-p)))) + (ert-deftest test-org-link-edit/get-link-data () "Test `org-link-edit--link-data'." ;; Plain link -- cgit v1.2.3 From 2cda5ae7d6141c06fc72e4c1b7f9f792cb8c2e05 Mon Sep 17 00:00:00 2001 From: Kyle Meyer Date: Fri, 3 Mar 2017 00:13:59 -0500 Subject: Add org-link-edit-transport-next-link command --- NEWS | 4 ++++ org-link-edit.el | 64 +++++++++++++++++++++++++++++++++++++++++++++++++-- test-org-link-edit.el | 44 +++++++++++++++++++++++++++++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index e4faa59..f55d26b 100644 --- a/NEWS +++ b/NEWS @@ -3,3 +3,7 @@ Org Link Edit NEWS -- history of user-visible changes -*- mode: org; -*- * master (unreleased) ** New features + +- New command ~org-link-edit-transport-next-link~ searches for the + next (or previous) link and moves it to point, using the word at + point or the selected region as the link's description. diff --git a/org-link-edit.el b/org-link-edit.el index 833d620..87dcd6c 100644 --- a/org-link-edit.el +++ b/org-link-edit.el @@ -26,8 +26,8 @@ ;; Org Link Edit provides Paredit-inspired slurping and barfing ;; commands for Org link descriptions. ;; -;; There are four commands, all which operate when point is on an Org -;; link. +;; There are four slurp and barf commands, all which operate when +;; point is on an Org link. ;; ;; - org-link-edit-forward-slurp ;; - org-link-edit-backward-slurp @@ -53,6 +53,11 @@ ;; ("i" org-link-edit-backward-barf "backward barf") ;; ("q" nil "cancel"))) ;; +;; In addition to the slurp and barf commands, the command +;; `org-link-edit-transport-next-link' searches for the next (or +;; previous) link and moves it to point, using the word at point or +;; the selected region as the link's description. +;; ;; [1] https://github.com/abo-abo/hydra ;;; Code: @@ -325,5 +330,60 @@ If N is negative, barf trailing blobs instead of leading blobs." (insert barfed) barfed))))) +(defun org-link-edit--next-link-data (&optional previous) + (save-excursion + (if (funcall (if previous #'re-search-backward #'re-search-forward) + org-any-link-re nil t) + (org-link-edit--link-data) + (user-error "No %s link found" (if previous "previous" "next"))))) + +;;;###autoload +(defun org-link-edit-transport-next-link (&optional previous beg end) + "Move the next link to point. + +If the region is active, use the selected text as the link's +description. Otherwise, use the word at point. + +With prefix argument PREVIOUS, move the previous link instead of +the next link. + +Non-interactively, use the text between BEG and END as the +description, moving the next (or previous) link relative BEG and +END." + (interactive (cons current-prefix-arg + (and (use-region-p) + (list (region-beginning) (region-end))))) + (let ((pt (point)) + (desc-bounds (cond + ((and beg end) + (cons (progn (goto-char beg) + (point-marker)) + (progn (goto-char end) + (point-marker)))) + ((not (looking-at-p "\\s-")) + (progn (skip-syntax-backward "w") + (let ((beg (point-marker))) + (skip-syntax-forward "w") + (cons beg (point-marker)))))))) + (when (or (and desc-bounds + (or (progn (goto-char (car desc-bounds)) + (org-link-edit--on-link-p)) + (progn (goto-char (cdr desc-bounds)) + (org-link-edit--on-link-p)))) + (progn (goto-char pt) + (org-link-edit--on-link-p))) + (user-error "Cannot transport next link with point on a link")) + (goto-char (car desc-bounds)) + (cl-multiple-value-bind (link-beg link-end link desc) + (org-link-edit--next-link-data previous) + (unless (or (not desc-bounds) (= (length desc) 0)) + (user-error "Link already has a description")) + (delete-region link-beg link-end) + (insert (org-make-link-string + link + (and desc-bounds + (delete-and-extract-region (car desc-bounds) + (cdr desc-bounds)))))))) + (provide 'org-link-edit) ;;; org-link-edit.el ends here diff --git a/test-org-link-edit.el b/test-org-link-edit.el index 4f006e7..7e4d2af 100644 --- a/test-org-link-edit.el +++ b/test-org-link-edit.el @@ -510,6 +510,50 @@ website" (org-link-edit-forward-barf 1) (buffer-string))))) + +;;; Transport + +(ert-deftest test-org-link-edit/transport-next-link () + "Test `org-link-edit-transport-next-link'." + (should + (string= "Here is \[\[http://orgmode.org/\]\[Org's\]\] website " + (org-test-with-temp-text + "Here is Org's website http://orgmode.org/" + (org-link-edit-transport-next-link) + (buffer-string)))) + (should + (string= " Here is \[\[http://orgmode.org/\]\[Org's\]\] website" + (org-test-with-temp-text + "http://orgmode.org/ Here is Org's website" + (org-link-edit-transport-next-link 'previous) + (buffer-string)))) + (should + (string= "\[\[http://orgmode.org/\]\[Here is Org's\]\] website " + (org-test-with-temp-text + "Here is Org's website http://orgmode.org/" + (org-link-edit-transport-next-link + nil (point-min) (point)) + (buffer-string)))) + (should + (string= " Here is \[\[http://orgmode.org/\]\[Org's website\]\]" + (org-test-with-temp-text + "http://orgmode.org/ Here is Org's website" + (org-link-edit-transport-next-link + 'previous (point) (point-max)) + (buffer-string)))) + (should-error + (org-test-with-temp-text + "Here is Org's website http://orgmode.org/" + (org-link-edit-transport-next-link) + (buffer-string))) + (should-error + (org-test-with-temp-text + "Here is Org's website http://orgmode.org/" + (org-link-edit-transport-next-link + nil (point-min) (point)) + (buffer-string))) + ) + ;;; Other -- cgit v1.2.3