From mboxrd@z Thu Jan 1 00:00:00 1970 From: Rasmus Subject: Re: [bug, patch, ox] INCLUDE and footnotes Date: Fri, 19 Dec 2014 17:44:15 +0100 Message-ID: <87y4q3a8tc.fsf@pank.eu> References: <87h9x5hwso.fsf@gmx.us> <87oarcbppe.fsf@nicolasgoaziou.fr> <87fvcozfhf.fsf@gmx.us> <87h9x4bj33.fsf@nicolasgoaziou.fr> <87iohks4ne.fsf@gmx.us> <87d27rbvio.fsf@nicolasgoaziou.fr> <87bnnbhg2x.fsf@gmx.us> <878uifbjc7.fsf@nicolasgoaziou.fr> <87388j9qbv.fsf@gmx.us> <87y4q57t2i.fsf@nicolasgoaziou.fr> <87lhm4n9ky.fsf@pank.eu> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:45022) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y20fC-0007kM-1W for emacs-orgmode@gnu.org; Fri, 19 Dec 2014 11:44:32 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Y20f5-0005hf-JO for emacs-orgmode@gnu.org; Fri, 19 Dec 2014 11:44:25 -0500 Received: from mout.gmx.net ([212.227.17.21]:53990) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Y20f5-0005hb-9U for emacs-orgmode@gnu.org; Fri, 19 Dec 2014 11:44:19 -0500 Received: from W530 ([46.166.186.238]) by mail.gmx.com (mrgmx103) with ESMTPSA (Nemesis) id 0LaoMe-1XdzVR1KxM-00kRnW for ; Fri, 19 Dec 2014 17:44:17 +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: emacs-orgmode@gnu.org --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Rasmus writes: >> AFAICT, there's no reason to include a rule about whitespace separating >> anything. Just make sure that any INCLUDE keyword that doesn't have >> a :minlevel property gets one set to 1+N, where N is the current level >> (or 0 if at top level). >> >> Another option is to delay insertion of included files: expand them >> completely in different strings, then replace keywords with appropriate >> strings. IOW, just make sure expansion doesn't happen sequentially. > > OK. Solution one sounds easier. A quick attempt, without tests, is given > in the second patch. I'll add patches if you agree with the easy > approach. It seems to work, though I'm not sure if the matching of > headlines which should have :minlevel added is robust enough. Here's a new version of the second patch with tests. The recognition regexp is still not great, but the idea of the regexp is to only act on includes where there's no :minlevel already and no plain words (most obviously src and example, but any block really) when disregarding ":key=C2=A0value" pairs. At least all tests are passed... =E2=80=94Rasmus --=20 This is the kind of tedious nonsense up with which I will not put --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0002-ox.el-Guess-the-minlevel-for-INCLUDE-keywords.patch >From 5ae354993662b3a3d2fc1c995861401f28b2af1c Mon Sep 17 00:00:00 2001 From: Rasmus Date: Thu, 18 Dec 2014 16:48:49 +0100 Subject: [PATCH 2/2] ox.el: Guess the :minlevel for INCLUDE-keywords * ox.el (org-export-expand-include-keyword): Guess :minlevel if missing and relevant. * test-ox.el: Tests for automatic :minlevel. --- lisp/ox.el | 17 +++++++++++++++++ testing/lisp/test-ox.el | 39 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 1 deletion(-) diff --git a/lisp/ox.el b/lisp/ox.el index 99c4e9b..a9b88fe 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3066,6 +3066,23 @@ storing and resolving footnotes. It is created automatically." (footnotes (or footnotes (make-hash-table :test #'equal))) (include-re "^[ \t]*#\\+INCLUDE:")) (goto-char (point-min)) + ;; Add :minlevel to all include words that no explicitly have one. + (save-excursion + (while (re-search-forward + (concat include-re "[ \t]*" "\\(?:\".+?\"\\|[^ \t]+\\)[ \t]*" "\\(.*\\)$") + nil t) + (let ((matched (match-string 1))) + (unless (or (string-match-p ":minlevel" matched) + ;; matched a normal word + (string-match "\\(?:^\\|[ \t]+\\)\\(\\w\\)" + (replace-regexp-in-string "\\(^\\|[ t]+\\):\\w+[ \t]+\\w+" "" + matched))) + (goto-char (line-end-position)) + (insert (format " :minlevel %d" + (1+ (org-with-wide-buffer + (if (search-backward-regexp org-heading-regexp nil t) + (length (match-string 1)) + 0))))))))) ;; Expand INCLUDE keywords. (while (re-search-forward include-re nil t) (let ((element (save-match-data (org-element-at-point)))) diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 140c0a8..47fe5a0 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -1045,7 +1045,44 @@ baz (org-test-with-temp-text (format "#+INCLUDE: \"%s/examples/include.org::#dh\" :only-contents t" org-test-dir) (org-export-expand-include-keyword) - (buffer-string))))) + (buffer-string)))) + ;; Adjacent INCLUDE-keywords should have the same :minlevel if unspecified + (should + (org-every (lambda (level) (zerop (1- level))) + (org-test-with-temp-text + (concat + (format "#+INCLUDE: \"%s/examples/include.org::#ah\"\n" org-test-dir) + (format "#+INCLUDE: \"%s/examples/include.org::*Heading\"" org-test-dir)) + (org-export-expand-include-keyword) + (org-element-map (org-element-parse-buffer) 'headline + (lambda (head) (org-element-property :level head)))))) + ;; INCLUDE source code must not have a :minlevel keyword + (should-not + (equal + (org-test-with-temp-text + (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp" org-test-dir) + (org-export-expand-include-keyword) + (buffer-string)) + (org-test-with-temp-text + (format "#+INCLUDE: \"%s/examples/include2.org\" src emacs-lisp :minlevel 1" org-test-dir) + (org-export-expand-include-keyword) + (buffer-string)))) + ;; INCLUDE should get the correct :minlevel even if narrowed. + (should + (org-test-with-temp-text + (format "* h1\n#+INCLUDE: \"%s/examples/include.org::#ah\"" org-test-dir) + (org-narrow-to-element) + (org-export-expand-include-keyword) + (goto-char (point-min)) + (eq 2 (org-element-property :level (org-element-at-point))))) + ;; If :minlevel is present do not alter it + (should + (org-test-with-temp-text + (format "* h1\n#+INCLUDE: \"%s/examples/include.org::#ah\" :minlevel 1" org-test-dir) + (org-narrow-to-element) + (org-export-expand-include-keyword) + (goto-char (point-min)) + (eq 1 (org-element-property :level (org-element-at-point)))))) (ert-deftest test-org-export/expand-macro () "Test macro expansion in an Org buffer." -- 2.2.0 --=-=-=--