Browse Source

Fix coderefs links in non-source buffers

* lisp/org-src.el (org-src-source-file-name): New variable.
(org-src--edit-element): Set new variable.
* lisp/org.el (org-store-link): Store the source file along with the
  coderef so as to insert link in other documents than the one
  where the code block is located.

Reported-by: stardiviner <numbchild@gmail.com>
<http://lists.gnu.org/r/emacs-orgmode/2018-10/msg00293.html>
Nicolas Goaziou 6 years ago
parent
commit
503a1d4d94
2 changed files with 14 additions and 3 deletions
  1. 6 0
      lisp/org-src.el
  2. 8 3
      lisp/org.el

+ 6 - 0
lisp/org-src.el

@@ -283,6 +283,10 @@ However, if `indent-tabs-mode' is nil in that buffer, its value
 is 0.")
 (put 'org-src--tab-width 'permanent-local t)
 
+(defvar-local org-src-source-file-name nil
+  "File name associated to Org source buffer, or nil.")
+(put 'org-src-source-file-name 'permanent-local t)
+
 (defun org-src--construct-edit-buffer-name (org-buffer-name lang)
   "Construct the buffer name for a source editing buffer."
   (concat "*Org Src " org-buffer-name "[ " lang " ]*"))
@@ -487,6 +491,7 @@ Leave point in edit buffer."
 	(with-current-buffer old-edit-buffer (org-src--remove-overlay))
 	(kill-buffer old-edit-buffer))
       (let* ((org-mode-p (derived-mode-p 'org-mode))
+	     (source-file-name (buffer-file-name (buffer-base-buffer)))
 	     (source-tab-width (if indent-tabs-mode tab-width 0))
 	     (type (org-element-type datum))
 	     (ind (org-with-wide-buffer
@@ -538,6 +543,7 @@ Leave point in edit buffer."
 	(setq org-src--preserve-indentation preserve-ind)
 	(setq org-src--overlay overlay)
 	(setq org-src--allow-write-back write-back)
+	(setq org-src-source-file-name source-file-name)
 	;; Start minor mode.
 	(org-src-mode)
 	;; Move mark and point in edit buffer to the corresponding

+ 8 - 3
lisp/org.el

@@ -9244,7 +9244,12 @@ non-nil."
 
        ;; Store a link from a remote editing buffer.
        ((org-src-edit-buffer-p)
-	(let ((coderef-format (org-src-coderef-format)))
+	(let ((coderef-format (org-src-coderef-format))
+	      (format-link
+	       (lambda (label)
+		 (if org-src-source-file-name
+		     (format "file:%s::(%s)" org-src-source-file-name label)
+		   (format "(%s)" label)))))
 	  (cond
 	   ;; Code references do not exist in this type of buffer.
 	   ;; Pretend we're linking from the source buffer directly.
@@ -9258,7 +9263,7 @@ non-nil."
 	      (re-search-forward (org-src-coderef-regexp coderef-format)
 				 (line-end-position)
 				 t))
-	    (setq link (format "(%s)" (match-string-no-properties 3))))
+	    (setq link (funcall format-link (match-string-no-properties 3))))
 	   ;; No code reference.  Create a new one then store the link
 	   ;; to it, but only in the function is called interactively.
 	   (interactive?
@@ -9270,7 +9275,7 @@ non-nil."
 		  (org-move-to-column gc t)
 		(insert " "))
 	      (insert reference)
-	      (setq link (format "(%s)" label))))
+	      (setq link (funcall format-link label))))
 	   ;; No code reference, and non-interactive call.  Don't know
 	   ;; what to do.  Give up.
 	   (t (setq link nil)))))