From b7112471b3e4b5334d98caf528e1e687232dee2f 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 (org-export-expand-include-keyword): Tests for automatic :minlevel. --- lisp/ox.el | 6 ++++++ testing/lisp/test-ox.el | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/lisp/ox.el b/lisp/ox.el index 99c4e9b..b65cea0 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3065,8 +3065,14 @@ storing and resolving footnotes. It is created automatically." (current-prefix 0) (footnotes (or footnotes (make-hash-table :test #'equal))) (include-re "^[ \t]*#\\+INCLUDE:")) + ;; Add :minlevel to all include words that no explicitly have one. (goto-char (point-min)) + (while (re-search-forward include-re nil t) + (unless (search-forward-regexp "[ \t]+:minlevel\\>" (line-end-position) t) + (end-of-line) + (insert (format " :minlevel %d" (1+ (org-outline-level)))))) ;; Expand INCLUDE keywords. + (goto-char (point-min)) (while (re-search-forward include-re nil t) (let ((element (save-match-data (org-element-at-point)))) (when (eq (org-element-type element) 'keyword) diff --git a/testing/lisp/test-ox.el b/testing/lisp/test-ox.el index 140c0a8..794de1f 100644 --- a/testing/lisp/test-ox.el +++ b/testing/lisp/test-ox.el @@ -854,7 +854,7 @@ text ;; Inclusion within a src-block. (should (equal - "#+BEGIN_SRC emacs-lisp\n(+ 2 1)\n#+END_SRC\n" + "#+BEGIN_SRC emacs-lisp :minlevel 1\n(+ 2 1)\n#+END_SRC\n" (org-test-with-temp-text (format "#+INCLUDE: \"%s/examples/include.org\" :lines \"4-5\" SRC emacs-lisp" @@ -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 is automatically inserted for src elements. + (should + (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 assigns the relative :minlevel conditional on narrowing. + (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 1 (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.1