From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Bad footnotes when including org files Date: Wed, 26 Mar 2014 15:41:51 +0100 Message-ID: <874n2luimo.fsf@gmail.com> References: <5324E5F1.5080207@gmail.com> <5325DF5C.9040606@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:58525) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSp1G-0005rp-6K for emacs-orgmode@gnu.org; Wed, 26 Mar 2014 10:41:35 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WSp1A-0005gk-Ap for emacs-orgmode@gnu.org; Wed, 26 Mar 2014 10:41:30 -0400 Received: from mail-wg0-x230.google.com ([2a00:1450:400c:c00::230]:33739) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WSp19-0005gY-WC for emacs-orgmode@gnu.org; Wed, 26 Mar 2014 10:41:24 -0400 Received: by mail-wg0-f48.google.com with SMTP id l18so1387304wgh.19 for ; Wed, 26 Mar 2014 07:41:23 -0700 (PDT) In-Reply-To: <5325DF5C.9040606@gmail.com> (Xavier Garrido's message of "Sun, 16 Mar 2014 18:29:00 +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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Xavier Garrido Cc: James Harkins , emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain Hello, Xavier Garrido writes: > Thanks for your answer. Maybe I will try your solution. Otherwise > I will run a "before-parse-hook" to change fn:XX to something unique > (by adding buffer name for example. We could do it by default. One problem is that INCLUDE keyword is a very simple feature. For example, if you include two files in a row and the first one ends with a footnote section, the other one will be included in that section and, therefore, not exported. So if we start to make it smart, we could be tempted to add too many other checks. Anyway, here's a patch for that. WDYT? Regards, -- Nicolas Goaziou --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-ox-Make-footnotes-file-specific-when-including-Org-f.patch >From 2a22d4dc3beb300094c9ee28158f227dbf467cda Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Wed, 26 Mar 2014 15:34:59 +0100 Subject: [PATCH] ox: Make footnotes file specific when including Org files * lisp/ox.el (org-export-expand-include-keyword, org-export--prepare-file-contents): Make footnotes file specific when including Org files. http://permalink.gmane.org/gmane.emacs.orgmode/83606 --- lisp/ox.el | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/lisp/ox.el b/lisp/ox.el index 9f77af4..cf70643 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3279,7 +3279,9 @@ with their line restriction, when appropriate. It is used to avoid infinite recursion. Optional argument DIR is the current working directory. It is used to properly resolve relative paths." - (let ((case-fold-search t)) + (let ((case-fold-search t) + (file-prefix (make-hash-table :test #'equal)) + (current-prefix 0)) (goto-char (point-min)) (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t) (let ((element (save-match-data (org-element-at-point)))) @@ -3349,13 +3351,16 @@ paths." (with-temp-buffer (let ((org-inhibit-startup t)) (org-mode)) (insert - (org-export--prepare-file-contents file lines ind minlevel)) + (org-export--prepare-file-contents + file lines ind minlevel + (or (gethash file file-prefix) + (puthash file (incf current-prefix) file-prefix)))) (org-export-expand-include-keyword (cons (list file lines) included) (file-name-directory file)) (buffer-string))))))))))))) -(defun org-export--prepare-file-contents (file &optional lines ind minlevel) +(defun org-export--prepare-file-contents (file &optional lines ind minlevel id) "Prepare the contents of FILE for inclusion and return them as a string. When optional argument LINES is a string specifying a range of @@ -3369,7 +3374,12 @@ headline encountered. Optional argument MINLEVEL, when non-nil, is an integer specifying the level that any top-level headline in the included -file should have." +file should have. + +Optional argument ID is an integer that will be inserted before +each footnote definition and reference if FILE is an Org file. +This is useful to avoid clashes when more than one Org file with +footnotes is included in a document." (with-temp-buffer (insert-file-contents file) (when lines @@ -3428,6 +3438,20 @@ file should have." (org-map-entries (lambda () (if (< offset 0) (delete-char (abs offset)) (insert (make-string offset ?*))))))))))) + ;; Append ID to all footnote references and definitions, so they + ;; are file specific and cannot collide with other included files. + (goto-char (point-min)) + (while (re-search-forward org-footnote-re nil t) + (let ((reference (org-element-context))) + (when (memq (org-element-type reference) + '(footnote-reference footnote-definition)) + (goto-char (org-element-property :begin reference)) + (forward-char) + (let ((label (org-element-property :label reference))) + (cond ((not label)) + ((org-string-match-p "\\`[0-9]+\\'" label) + (insert (format "fn:%d-" id))) + (t (forward-char 3) (insert (format "%d-" id)))))))) (org-element-normalize-string (buffer-string)))) (defun org-export-execute-babel-code () -- 1.9.1 --=-=-=--