Browse Source

ol: Do not save tags when storing a link from a headline

* lisp/ol.el (org-link-heading-search-string): Assume optional
argument is already valid.  Remove TODO keyword, priority cookie,
COMMENT keyword, and tags.
(org-link-search): Remove COMMENT keyword.
* testing/lisp/test-ol.el (test-ol/store-link): Add tests.
Nicolas Goaziou 5 years ago
parent
commit
97e415c5df
2 changed files with 42 additions and 16 deletions
  1. 14 14
      lisp/ol.el
  2. 28 2
      testing/lisp/test-ol.el

+ 14 - 14
lisp/ol.el

@@ -1180,17 +1180,14 @@ of matched result, which is either `dedicated' or `fuzzy'."
 			  org-outline-regexp-bol
 			  org-outline-regexp-bol
 			  org-comment-string
 			  org-comment-string
 			  (mapconcat #'regexp-quote words ".+")))
 			  (mapconcat #'regexp-quote words ".+")))
-		 (cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]")
-		 (comment-re (format "\\`%s[ \t]+" org-comment-string)))
+		 (cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"))
 	     (goto-char (point-min))
 	     (goto-char (point-min))
 	     (catch :found
 	     (catch :found
 	       (while (re-search-forward title-re nil t)
 	       (while (re-search-forward title-re nil t)
 		 (when (equal words
 		 (when (equal words
 			      (split-string
 			      (split-string
 			       (replace-regexp-in-string
 			       (replace-regexp-in-string
-				cookie-re " "
-				(replace-regexp-in-string
-				 comment-re "" (org-get-heading t t t)))))
+				cookie-re " " (org-get-heading t t t t))))
 		   (throw :found t)))
 		   (throw :found t)))
 	       nil)))
 	       nil)))
       (beginning-of-line)
       (beginning-of-line)
@@ -1242,18 +1239,21 @@ of matched result, which is either `dedicated' or `fuzzy'."
 
 
 (defun org-link-heading-search-string (&optional string)
 (defun org-link-heading-search-string (&optional string)
   "Make search string for the current headline or STRING.
   "Make search string for the current headline or STRING.
-When optional argument STRING is non-nil, assume it a headline.
+
 Search string starts with an asterisk.  COMMENT keyword and
 Search string starts with an asterisk.  COMMENT keyword and
 statistics cookies are removed, and contiguous spaces are packed
 statistics cookies are removed, and contiguous spaces are packed
-into a single one."
-  (let ((context
+into a single one.
+
+When optional argument STRING is non-nil, assume it a headline,
+without any TODO or COMMENT keyword, and without any priority
+cookie or tag."
+  (let ((cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]")
+	(context
 	 (if (not string)
 	 (if (not string)
-	     (concat "*" (org-trim (org-get-heading nil nil nil t)))
-	   (let ((s (org-trim string))
-		 (comment-re (format "\\`%s[ \t]+" org-comment-string)))
-	     (unless (string-prefix-p "*" s) (setq s (concat "*" s)))
-	     (replace-regexp-in-string comment-re "" s))))
-	(cookie-re "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]"))
+	     (concat "*" (org-trim (org-get-heading t t t t)))
+	   (let ((s (org-trim string)))
+	     (if (string-prefix-p "*" s) s
+	       (setq s (concat "*" s)))))))
     (org-link--squeeze-white-spaces
     (org-link--squeeze-white-spaces
      (replace-regexp-in-string cookie-re " " context))))
      (replace-regexp-in-string cookie-re " " context))))
 
 

+ 28 - 2
testing/lisp/test-ol.el

@@ -133,6 +133,32 @@
        (let ((file (buffer-file-name)))
        (let ((file (buffer-file-name)))
 	 (equal (format "[[file:%s::*H1][H1]]" file)
 	 (equal (format "[[file:%s::*H1][H1]]" file)
 		(org-store-link nil))))))
 		(org-store-link nil))))))
+  ;; On a headline, remove TODO and COMMENT keywords, priority cookie,
+  ;; and tags.
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* TODO H1"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*H1][H1]]" file)
+		(org-store-link nil))))))
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* COMMENT H1"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*H1][H1]]" file)
+		(org-store-link nil))))))
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* [#A] H1"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*H1][H1]]" file)
+		(org-store-link nil))))))
+  (should
+   (let (org-store-link-props org-stored-links)
+     (org-test-with-temp-text-in-file "* H1 :tag:"
+       (let ((file (buffer-file-name)))
+	 (equal (format "[[file:%s::*H1][H1]]" file)
+		(org-store-link nil))))))
   ;; On a headline, remove any link from description.
   ;; On a headline, remove any link from description.
   (should
   (should
    (let (org-store-link-props org-stored-links)
    (let (org-store-link-props org-stored-links)
@@ -217,7 +243,7 @@
        (fundamental-mode)
        (fundamental-mode)
        (let ((file (buffer-file-name)))
        (let ((file (buffer-file-name)))
 	 (equal (format "[[file:%s][file:%s]]" file file)
 	 (equal (format "[[file:%s][file:%s]]" file file)
-	 	(org-store-link nil))))))
+		(org-store-link nil))))))
   ;; C-u prefix reverses `org-context-in-file-links' in non-Org
   ;; C-u prefix reverses `org-context-in-file-links' in non-Org
   ;; buffer.
   ;; buffer.
   (should
   (should
@@ -237,7 +263,7 @@
        (fundamental-mode)
        (fundamental-mode)
        (let ((file (buffer-file-name)))
        (let ((file (buffer-file-name)))
 	 (equal (format "[[file:%s][file:%s]]" file file)
 	 (equal (format "[[file:%s][file:%s]]" file file)
-	 	(org-store-link '(16))))))))
+		(org-store-link '(16))))))))
 
 
 
 
 ;;; Radio Targets
 ;;; Radio Targets