Browse Source

Fix storing links to headlines containing multiple links

* lisp/org.el (org-store-link): Fix storing links to headlines
  containing multiple links.

* testing/lisp/test-org.el (test-org/store-link): New test.

Reported-by: Georgiy Tugai <georgiy.tugai@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/106606>
Nicolas Goaziou 9 years ago
parent
commit
e818699113
2 changed files with 49 additions and 10 deletions
  1. 6 10
      lisp/org.el
  2. 43 0
      testing/lisp/test-org.el

+ 6 - 10
lisp/org.el

@@ -9931,17 +9931,13 @@ active region."
        (if (consp link) (setq cpltxt (car link) link (cdr link)))
        (if (consp link) (setq cpltxt (car link) link (cdr link)))
        (setq link (or link cpltxt)
        (setq link (or link cpltxt)
 	     desc (or desc cpltxt))
 	     desc (or desc cpltxt))
-       (cond ((equal desc "NONE") (setq desc nil))
-	     ((and desc (string-match org-bracket-link-analytic-regexp desc))
-	      (let ((d0 (match-string 3 desc))
-		    (p0 (match-string 5 desc)))
-		(setq desc
+       (cond ((not desc))
+	     ((equal desc "NONE") (setq desc nil))
+	     (t (setq desc
 		      (replace-regexp-in-string
 		      (replace-regexp-in-string
-		       org-bracket-link-regexp
-		       (concat (or p0 d0)
-			       (if (equal (length (match-string 0 desc))
-					  (length desc)) "*" "")) desc)))))
-
+		       org-bracket-link-analytic-regexp
+		       (lambda (m) (or (match-string 5 m) (match-string 3 m)))
+		       desc))))
        ;; Return the link
        ;; Return the link
        (if (not (and (or (org-called-interactively-p 'any)
        (if (not (and (or (org-called-interactively-p 'any)
 			 executing-kbd-macro)
 			 executing-kbd-macro)

+ 43 - 0
testing/lisp/test-org.el

@@ -1848,6 +1848,49 @@ drops support for Emacs 24.1 and 24.2."
      (org-open-at-point)
      (org-open-at-point)
      (eq (org-element-type (org-element-context)) 'radio-target))))
      (eq (org-element-type (org-element-context)) 'radio-target))))
 
 
+;;;; Stored links
+
+(ert-deftest test-org/store-link ()
+  "Test `org-store-link' specifications."
+  ;; On a headline, link to that headline.  Use heading as the
+  ;; description of the link.
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* H1"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*H1][H1]]" file)
+		(org-store-link nil))))))
+  ;; On a headline, remove any link from description.
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* [[#l][d]]"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*%s][d]]"
+			file
+			(org-link-escape "[[#l][d]]"))
+		(org-store-link nil))))))
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* [[l]]"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*%s][l]]" file (org-link-escape "[[l]]"))
+		(org-store-link nil))))))
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* [[l1][d1]] [[l2][d2]]"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*%s][d1 d2]]"
+			file
+			(org-link-escape "[[l1][d1]] [[l2][d2]]"))
+		(org-store-link nil))))))
+  ;; On a named element, link to that element.
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "#+NAME: foo\nParagraph"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::foo][foo]]" file)
+		(org-store-link nil)))))))
+
 
 
 ;;; Node Properties
 ;;; Node Properties