emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Daniel Dehennin <daniel.dehennin@baby-gnu.org>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Dynamically demote included file relatively to the current heading level
Date: Mon, 12 Dec 2011 20:27:21 +0100	[thread overview]
Message-ID: <87d3btpp46.fsf@hati.baby-gnu.org> (raw)

[-- Attachment #1: Type: text/plain, Size: 8056 bytes --]

Hello,

You can get the following patchset by pulling:

 git pull git://git.baby-gnu.net/org-mode dad/add-level-to-headings

Regards.

This patch permits a more flexible usage than :minlevel.

It's possible to include files with their heading demoted by 'X' levels
relatively to the current one.

This is useful to make some glue documentation based on many little
parts:

- each little part starts its heading at level 1
- each part can include other parts:
  * as children by setting ":addlevel 1"
  * as same level by setting ":addlevel 0"

The test files are provided but 'org-test-compare-with-file' is TODO.

* lisp/org-exp.el (org-export-handle-include-files): Save heading level
  of the parent of the included file as "currentlevel".
  Included files are demoted relatively by "addlevel".

* lisp/org-exp.el (org-get-file-contents): Demote included file
  relatively to "partentlevel" by "addlevel" increment.

* testing/examples/include-master.org: Entry point of the "addlevel" test
  case.

* testing/examples/include-1.org: File included by include-master.org,
  both at level1 (i.e. no addlevel) and level2.

* testing/examples/include-2.org: File included by include-1.org, level
  incremented by one relatively to include-1.org

* testing/examples/include-addlevel.txt: Attended result to ASCII
  export.
---
 lisp/org-exp.el                       |   24 ++++++++++---
 testing/examples/include-1.org        |    8 ++++
 testing/examples/include-2.org        |    4 ++
 testing/examples/include-addlevel.txt |   58 +++++++++++++++++++++++++++++++++
 testing/examples/include-master.org   |   15 ++++++++
 5 files changed, 103 insertions(+), 6 deletions(-)
 create mode 100644 testing/examples/include-1.org
 create mode 100644 testing/examples/include-2.org
 create mode 100644 testing/examples/include-addlevel.txt
 create mode 100644 testing/examples/include-master.org

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index eae5be7..7e0512d 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -2381,13 +2381,14 @@ TYPE must be a string, any of:
 (defun org-export-handle-include-files ()
   "Include the contents of include files, with proper formatting."
   (let ((case-fold-search t)
-	params file markup lang start end prefix prefix1 switches all minlevel lines)
+	params file markup lang start end prefix prefix1 switches all currentlevel minlevel addlevel lines)
     (goto-char (point-min))
     (while (re-search-forward "^#\\+INCLUDE:?[ \t]+\\(.*\\)" nil t)
       (setq params (read (concat "(" (match-string 1) ")"))
 	    prefix (org-get-and-remove-property 'params :prefix)
 	    prefix1 (org-get-and-remove-property 'params :prefix1)
 	    minlevel (org-get-and-remove-property 'params :minlevel)
+	    addlevel (org-get-and-remove-property 'params :addlevel)
 	    lines (org-get-and-remove-property 'params :lines)
 	    file (org-symname-or-string (pop params))
 	    markup (org-symname-or-string (pop params))
@@ -2396,6 +2397,8 @@ TYPE must be a string, any of:
 	    switches (mapconcat #'(lambda (x) (format "%s" x)) params " ")
 	    start nil end nil)
       (delete-region (match-beginning 0) (match-end 0))
+      (setq currentlevel (or (org-current-level)
+			     0))
       (if (or (not file)
 	      (not (file-exists-p file))
 	      (not (file-readable-p file)))
@@ -2411,7 +2414,7 @@ TYPE must be a string, any of:
 		  end  (format "#+end_%s" markup))))
 	(insert (or start ""))
 	(insert (org-get-file-contents (expand-file-name file)
-				       prefix prefix1 markup minlevel lines))
+				       prefix prefix1 markup currentlevel minlevel addlevel lines))
 	(or (bolp) (newline))
 	(insert (or end ""))))
     all))
@@ -2428,13 +2431,15 @@ TYPE must be a string, any of:
 	(when intersection
 	  (error "Recursive #+INCLUDE: %S" intersection))))))
 
-(defun org-get-file-contents (file &optional prefix prefix1 markup minlevel lines)
+(defun org-get-file-contents (file &optional prefix prefix1 markup parentlevel minlevel addlevel lines)
   "Get the contents of FILE and return them as a string.
 If PREFIX is a string, prepend it to each line.  If PREFIX1
 is a string, prepend it to the first line instead of PREFIX.
 If MARKUP, don't protect org-like lines, the exporter will
-take care of the block they are in.  If LINES is a string
-specifying a range of lines, include only those lines ."
+take care of the block they are in.  If ADDLEVEL is a number,
+demote included file to current heading level+ADDLEVEL.
+If LINES is a string specifying a range of lines,
+include only those lines."
   (if (stringp markup) (setq markup (downcase markup)))
   (with-temp-buffer
     (insert-file-contents file)
@@ -2467,7 +2472,14 @@ specifying a range of lines, include only those lines ."
     (when minlevel
       (dotimes (lvl minlevel)
 	(org-map-region 'org-demote (point-min) (point-max))))
-    (buffer-string)))
+    (when addlevel
+      (let ((inclevel (or (if (org-before-first-heading-p)
+			      (- (outline-next-heading) 1)
+			    (- (org-current-level) 1))
+			  0)))
+	(dotimes (level (- (+ parentlevel addlevel) inclevel))
+	  (org-map-region 'org-demote (point-min) (point-max)))))
+      (buffer-string)))
 
 (defun org-get-and-remove-property (listvar prop)
   "Check if the value of LISTVAR contains PROP as a property.
diff --git a/testing/examples/include-1.org b/testing/examples/include-1.org
new file mode 100644
index 0000000..0c7ed29
--- /dev/null
+++ b/testing/examples/include-1.org
@@ -0,0 +1,8 @@
+
+* Include 1 header (initial level is 1)
+
+Some text in include1.
+
+Followed by a demoted include.
+
+#+INCLUDE: "include-2.org" :addlevel 1
diff --git a/testing/examples/include-2.org b/testing/examples/include-2.org
new file mode 100644
index 0000000..8558947
--- /dev/null
+++ b/testing/examples/include-2.org
@@ -0,0 +1,4 @@
+
+* Include 2 header (initial level is 1)
+
+Some text in include 2.
diff --git a/testing/examples/include-addlevel.txt b/testing/examples/include-addlevel.txt
new file mode 100644
index 0000000..c4fd1c3
--- /dev/null
+++ b/testing/examples/include-addlevel.txt
@@ -0,0 +1,58 @@
+                       Test #+INCLUDE: addlevel
+                       ========================
+
+Author: Test user
+Date: 
+
+
+Table of Contents
+=================
+1 Top level1 header1 
+2 Include 1 header (initial level is 1) 
+    2.1 Include 2 header (initial level is 1) 
+3 Top level1 header2 
+    3.1 Include 1 header (initial level is 1) 
+        3.1.1 Include 2 header (initial level is 1) 
+
+
+1 Top level1 header1 
+---------------------
+
+Following come a level1
+
+
+2 Include 1 header (initial level is 1) 
+----------------------------------------
+
+Some text in include1.
+
+Followed by a demoted include.
+
+
+2.1 Include 2 header (initial level is 1) 
+==========================================
+
+Some text in include 2.
+
+
+
+3 Top level1 header2 
+---------------------
+
+Following come a level1 header demoted to level2
+
+
+3.1 Include 1 header (initial level is 1) 
+==========================================
+
+Some text in include1.
+
+Followed by a demoted include.
+
+
+3.1.1 Include 2 header (initial level is 1) 
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+Some text in include 2.
+
+
diff --git a/testing/examples/include-master.org b/testing/examples/include-master.org
new file mode 100644
index 0000000..f63b883
--- /dev/null
+++ b/testing/examples/include-master.org
@@ -0,0 +1,15 @@
+#+TITLE: Test #+INCLUDE: addlevel
+#+AUTHOR: Test user
+#+DATE:
+
+* Top level1 header1
+
+Following come a level1
+
+#+INCLUDE: "include-1.org"
+
+* Top level1 header2
+
+Following come a level1 header demoted to level2
+
+#+INCLUDE: "include-1.org" :addlevel 1
-- 
1.7.7.3



-- 
Daniel Dehennin
Récupérer ma clef GPG:
gpg --keyserver pgp.mit.edu --recv-keys 0x6A2540D1

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

             reply	other threads:[~2011-12-12 19:27 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-12 19:27 Daniel Dehennin [this message]
2012-04-23 12:57 ` [PATCH] Dynamically demote included file relatively to the current heading level Bastien

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d3btpp46.fsf@hati.baby-gnu.org \
    --to=daniel.dehennin@baby-gnu.org \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).