Browse Source

Fix previous patch

* lisp/org.el (org-update-radio-target-regexp): Fix previous patch.

* testing/lisp/test-org.el (test-org/update-radio-target-regexp): Add test.
Nicolas Goaziou 11 years ago
parent
commit
3702dce1db
2 changed files with 40 additions and 15 deletions
  1. 22 14
      lisp/org.el
  2. 18 1
      testing/lisp/test-org.el

+ 22 - 14
lisp/org.el

@@ -6107,6 +6107,8 @@ by a #."
 Also refresh fontification if needed."
   (interactive)
   (let ((old-regexp org-target-link-regexp)
+	(before-re "\\(?:^\\|[^[:alnum:]]\\)\\(")
+	(after-re "\\)\\(?:$\\|[^[:alnum:]]\\)")
 	(targets
 	 (org-with-wide-buffer
 	  (goto-char (point-min))
@@ -6120,25 +6122,31 @@ Also refresh fontification if needed."
 	    rtn))))
     (setq org-target-link-regexp
 	  (and targets
-	       (concat "\\(?:^\\|[^[:alnum:]]\\)\\("
+	       (concat before-re
 		       (mapconcat
-			(lambda (x)
-			  (replace-regexp-in-string
-			   " +" "\\s-+" (regexp-quote x) t t))
+			#'(lambda (x)
+			    (replace-regexp-in-string
+			     " +" "\\s-+" (regexp-quote x) t t))
 			targets
 			"\\|")
-		       "\\)\\(?:$\\|[^[:alnum:]]\\)")))
+		       after-re)))
     (unless (equal old-regexp org-target-link-regexp)
       ;; Clean-up cache.
-      (let ((regexp (cond
-		     ((not old-regexp) org-target-link-regexp)
-		     ((not org-target-link-regexp) old-regexp)
-		     (t (concat old-regexp "\\|" org-target-link-regexp)))))
-	(when regexp
-	  (org-with-wide-buffer
-	   (goto-char (point-min))
-	   (while (re-search-forward regexp nil t)
-	     (org-element-cache-refresh (match-beginning 1))))))
+      (let ((regexp (cond ((not old-regexp) org-target-link-regexp)
+			  ((not org-target-link-regexp) old-regexp)
+			  (t
+			   (concat before-re
+				   (mapconcat
+				    #'(lambda (re)
+					(substring re (length before-re)
+						   (- (length after-re))))
+				    (list old-regexp org-target-link-regexp)
+				    "\\|")
+				   after-re)))))
+	(org-with-wide-buffer
+	 (goto-char (point-min))
+	 (while (re-search-forward regexp nil t)
+	   (org-element-cache-refresh (match-beginning 1)))))
       ;; Re fontify buffer.
       (when (memq 'radio org-highlight-links)
 	(org-restart-font-lock)))))

+ 18 - 1
testing/lisp/test-org.el

@@ -1742,7 +1742,7 @@ Text.
 
 (ert-deftest test-org/update-radio-target-regexp ()
   "Test `org-update-radio-target-regexp' specifications."
-  ;; Properly update cache.
+  ;; Properly update cache with no previous radio target regexp.
   (should
    (eq 'link
        (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
@@ -1752,6 +1752,23 @@ Text.
 	 (insert ">>>")
 	 (org-update-radio-target-regexp)
 	 (goto-char (point-max))
+	 (org-element-type (org-element-context)))))
+  ;; Properly update cache with previous radio target regexp.
+  (should
+   (eq 'link
+       (org-test-with-temp-text "radio\n\nParagraph\n\nradio"
+	 (save-excursion (goto-char (point-max)) (org-element-context))
+	 (insert "<<<")
+	 (search-forward "o")
+	 (insert ">>>")
+	 (org-update-radio-target-regexp)
+	 (search-backward "r")
+	 (delete-char 5)
+	 (insert "new")
+	 (org-update-radio-target-regexp)
+	 (goto-char (point-max))
+	 (delete-region (line-beginning-position) (point))
+	 (insert "new")
 	 (org-element-type (org-element-context))))))