From 503a1d4d94fba7adeda50fcb1bf411285d566af6 Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Sun, 4 Nov 2018 16:37:59 +0100 Subject: [PATCH] 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 --- lisp/org-src.el | 6 ++++++ lisp/org.el | 11 ++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lisp/org-src.el b/lisp/org-src.el index 4cdfad259..12163156f 100644 --- a/lisp/org-src.el +++ b/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 diff --git a/lisp/org.el b/lisp/org.el index 9d6820cd7..33c846765 100644 --- a/lisp/org.el +++ b/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)))))