Browse Source

org.el (org-open-link-in-comment-or-property): Refactor into `org-open-at-point'

* org.el (org-open-link-in-comment-or-property): Delete.
(org-open-at-point): Handle links in comments and property
drawers.  Update docstring.
Bastien Guerry 11 years ago
parent
commit
500252abf3
1 changed files with 27 additions and 29 deletions
  1. 27 29
      lisp/org.el

+ 27 - 29
lisp/org.el

@@ -10547,28 +10547,6 @@ Functions in this hook must return t if they identify and follow
 a link at point.  If they don't find anything interesting at point,
 they must return nil.")
 
-(defun org-open-link-in-comment-or-property ()
-  "Open the link at point in a comment or in a property."
-  (let* ((comment-or-prop "\\s-*# \\|[ \t]*:[^:]+:[ \t]*")
-	 (string-rear
-	  (replace-regexp-in-string
-	   comment-or-prop ""
-	   (buffer-substring (point) (line-beginning-position))))
-	 (string-front
-	  (replace-regexp-in-string
-	   comment-or-prop ""
-	   (buffer-substring (point) (line-end-position))))
-	 (value (org-element-property :value (org-element-at-point))))
-    (with-temp-buffer
-      (let ((org-inhibit-startup t)) (org-mode))
-      (insert value)
-      (goto-char (point-min))
-      (while (and (re-search-forward (regexp-quote string-rear) nil t)
-		  (re-search-forward (regexp-quote string-front) nil t))
-	(goto-char (match-beginning 0))	
-	(org-open-at-point)
-	(when (string= string-rear "") (forward-char))))))
-
 (defvar org-link-search-inhibit-query nil) ;; dynamically scoped
 (defvar clean-buffer-list-kill-buffer-names) ; Defined in midnight.el
 (defun org-open-at-point (&optional arg reference-buffer)
@@ -10593,7 +10571,11 @@ is on a tag, call `org-tags-view' instead.
 
 When optional argument REFERENCE-BUFFER is non-nil, it should
 specify a buffer from where the link search should happen.  This
-is used internally by `org-open-link-from-string'."
+is used internally by `org-open-link-from-string'.
+
+On top of syntactically correct links, this function will open
+the link at point in comments and the first link in a property
+drawer line."
   (interactive "P")
   ;; On a code block, open block's results.
   (unless (call-interactively #'org-babel-open-src-block-result)
@@ -10602,7 +10584,7 @@ is used internally by `org-open-link-from-string'."
     (setq org-window-config-before-follow-link (current-window-configuration))
     (org-remove-occur-highlights nil nil t)
     (unless (run-hook-with-args-until-success 'org-open-at-point-functions)
-      (let* ((context (org-element-context)) type)
+      (let* ((context (org-element-context)) type value)
 	;; On an unsupported type, check if point is contained within
 	;; a support one.
 	(while (and (not (memq (setq type (org-element-type context))
@@ -10610,14 +10592,30 @@ is used internally by `org-open-link-from-string'."
 					 footnote-definition footnote-reference
 					 node-property timestamp)))
 		    (setq context (org-element-property :parent context))))
+	(setq value (org-element-property :value context))
 	(cond
 	 ;; Blank lines at the beginning of buffer: bail out.
 	 ((not context) (user-error "No link found"))
-	 ;; WARNING: Before checking for syntactically correct
-	 ;; contexts, we make two exceptions as we open links in
-	 ;; comments and properties.
-	 ((memq type '(comment node-property))
-	  (org-open-link-in-comment-or-property))
+	 ;; Exception n°1: links in property drawers
+	 ((eq type 'node-property)
+	  (org-open-link-from-string
+	   (and (string-match org-any-link-re value)
+		(match-string-no-properties 0 value))))
+	 ;; Exception n°2: links in comments.
+	 ((eq type 'comment)
+	  (let ((string-rear (replace-regexp-in-string
+			      "^[ \t]*# [ \t]*" ""
+			      (buffer-substring (point) (line-beginning-position))))
+		(string-front (buffer-substring (point) (line-end-position))))
+	    (with-temp-buffer
+	      (let ((org-inhibit-startup t)) (org-mode))
+	      (insert value)
+	      (goto-char (point-min))
+	      (when (and (search-forward string-rear nil t)
+			 (search-forward string-front (line-end-position) t))
+		(goto-char (match-beginning 0))
+		(org-open-at-point)
+		(when (string= string-rear "") (forward-char))))))
 	 ;; On a headline or an inlinetask, but not on a timestamp,
 	 ;; a link, a footnote reference or on tags.
 	 ((and (memq type '(headline inlinetask))