From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: How to keep correct filepaths when using the #+INCLUDE derivative? Date: Sat, 03 Mar 2018 02:24:28 +0100 Message-ID: <878tbahrxv.fsf@nicolasgoaziou.fr> References: <87woyxhuk2.fsf@nicolasgoaziou.fr> <87o9k7ir3i.fsf@nicolasgoaziou.fr> <87h8pzifk7.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:37638) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ervuc-0004s7-Jl for emacs-orgmode@gnu.org; Fri, 02 Mar 2018 20:24:35 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ervuZ-0001Ic-G1 for emacs-orgmode@gnu.org; Fri, 02 Mar 2018 20:24:34 -0500 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:42801) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1ervuZ-0001Hx-8E for emacs-orgmode@gnu.org; Fri, 02 Mar 2018 20:24:31 -0500 In-Reply-To: (Daniel P. Gomez's message of "Fri, 2 Mar 2018 15:16:21 +0100") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: Daniel P Gomez Cc: emacs-orgmode@gnu.org Hello, Daniel P Gomez writes: > I've adapted the code such that it handles both bracketed and > unbracketed links, and links with descriptions. > As it is now, the changes are always automatically applied. Thank you. > I couldn't find a simple way of rewriting links without making them > absolute, as `org-export--prepare-file-contents` does not have access > to the path of the including file, only of the included file. `org-export--prepare-file-contents' is called from the including document, so you can get its path with (buffer-file-name (buffer-base-buffer)). However, we need to handle the case where the including buffer is not associated to a file, i.e., the Sexp above returns nil. > + (goto-char (point-min)) > + (while (re-search-forward org-any-link-re nil t) > + (let ((link (save-excursion > + (backward-char) > + (org-element-context)))) It would be nice to add a comment explaining what we are going to do. > + (when (string= (org-element-property :type link) "file") > + (let* ((has-bracket (string= > + (org-element-property :format link) "bracket")) > + (has-content (org-element-property :contents-begin link)) > + (old-path (org-element-property :path link)) > + (new-path (expand-file-name old-path > + (file-name-directory file))) > + (raw-new-link > + (concat "file:" new-path)) > + (new-link > + (cond > + ((and has-bracket (not has-content)) > + (concat "[[" raw-new-link "]]")) > + ((and has-bracket has-content) > + (let ((description > + (buffer-substring > + (org-element-property :contents-begin link) > + (org-element-property :contents-end link)))) > + (concat "[[" raw-new-link "][" description "]]"))) > + (t raw-new-link)))) > + (apply #'delete-region (list (org-element-property :begin link) > + (org-element-property :end link))) > + (insert new-link))))) I suggest the following inner part: (when (string= "file" (org-element-property :type link)) (let* ((old-path (org-element-property :path link)) (new-path (expand-file-name old-path (file-name-directory file)))) (delete-region (org-element-property :begin link) (org-element-property :end link)) (insert (let ((new (org-element-copy link))) (org-element-put-property new :path new-path) (when (org-element-property :contents-begin link) (org-element-adopt-elements new (buffer-substring (org-element-property :contents-begin link) (org-element-property :contents-end link)))) (org-element-interpret-data new))))) Also, would you mind adding a test in "text-ox.el", within `test-org-export/expand-include'? Regards, -- Nicolas Goaziou