Browse Source

Fix `org-return-follows-link' on links with emphasis

* lisp/org.el (org-return): Properly follow links when description is
  emphasized.  Also tolerate links and timestamps in otherwise forbidden
  areas (e.g., comments, node properties...), much like
  `org-open-at-point'.

* testing/lisp/test-org.el (test-org/return): Add tests.

Reported-by: Samuel Wales <samologist@gmail.com>
<http://permalink.gmane.org/gmane.emacs.orgmode/101977>
Nicolas Goaziou 9 years ago
parent
commit
4e864643bd
2 changed files with 25 additions and 14 deletions
  1. 9 11
      lisp/org.el
  2. 16 3
      testing/lisp/test-org.el

+ 9 - 11
lisp/org.el

@@ -21325,25 +21325,23 @@ will not happen if point is in a table or on a \"dead\"
 object (e.g., within a comment).  In these case, you need to use
 `org-open-at-point' directly."
   (interactive)
-  (let* ((context (if org-return-follows-link (org-element-context)
-		    (org-element-at-point)))
-	 (type (org-element-type context)))
+  (let ((context (if org-return-follows-link (org-element-context)
+		   (org-element-at-point))))
     (cond
      ;; In a table, call `org-table-next-row'.
-     ((or (and (eq type 'table)
+     ((or (and (eq (org-element-type context) 'table)
 	       (>= (point) (org-element-property :contents-begin context))
 	       (< (point) (org-element-property :contents-end context)))
 	  (org-element-lineage context '(table-row table-cell) t))
       (org-table-justify-field-maybe)
       (call-interactively #'org-table-next-row))
-     ;; On a link or a timestamp but not on white spaces after it,
-     ;; call `org-open-line' if `org-return-follows-link' allows it.
+     ;; On a link or a timestamp, call `org-open-line' if
+     ;; `org-return-follows-link' allows it.  Tolerate fuzzy
+     ;; locations, e.g., in a comment, as `org-open-line'.
      ((and org-return-follows-link
-	   (memq type '(link timestamp))
-	   (< (point)
-	      (save-excursion (goto-char (org-element-property :end context))
-			      (skip-chars-backward " \t")
-			      (point))))
+	   (or (org-at-timestamp-p t)
+	       (org-at-date-range-p t)
+	       (org-in-regexp org-any-link-re)))
       (call-interactively #'org-open-at-point))
      ;; Insert newline in heading, but preserve tags.
      ((and (not (bolp))

+ 16 - 3
testing/lisp/test-org.el

@@ -887,18 +887,31 @@
   (should
    (org-test-with-temp-text "Link [[target<point>]] <<target>>"
      (let ((org-return-follows-link t)
-	   (org-link-search-must-match-exact-headline nil)) (org-return))
+	   (org-link-search-must-match-exact-headline nil))
+       (org-return))
      (org-looking-at-p "<<target>>")))
   (should-not
    (org-test-with-temp-text "Link [[target<point>]] <<target>>"
      (let ((org-return-follows-link nil)) (org-return))
      (org-looking-at-p "<<target>>")))
-  ;; Link in heading should also be opened when
-  ;; `org-return-follows-link` is non-nil.
   (should
    (org-test-with-temp-text "* [[b][a<point>]]\n* b"
      (let ((org-return-follows-link t)) (org-return))
      (org-looking-at-p "* b")))
+  (should
+   (org-test-with-temp-text "Link [[target][/descipt<point>ion/]] <<target>>"
+     (let ((org-return-follows-link t)
+	   (org-link-search-must-match-exact-headline nil))
+       (org-return))
+     (org-looking-at-p "<<target>>")))
+  ;; When `org-return-follows-link' is non-nil, tolerate links and
+  ;; timestamps in comments, node properties, etc.
+  (should
+   (org-test-with-temp-text "# Comment [[target<point>]]\n <<target>>"
+     (let ((org-return-follows-link t)
+	   (org-link-search-must-match-exact-headline nil))
+       (org-return))
+     (org-looking-at-p "<<target>>")))
   ;; However, do not open link when point is in a table.
   (should
    (org-test-with-temp-text "| [[target<point>]] |\n| between |\n| <<target>> |"