emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Dynamically demote included file relatively to the current heading level
@ 2011-12-12 19:27 Daniel Dehennin
  2012-04-23 12:57 ` Bastien
  0 siblings, 1 reply; 2+ messages in thread
From: Daniel Dehennin @ 2011-12-12 19:27 UTC (permalink / raw)
  To: emacs-orgmode

[-- 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 --]

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] Dynamically demote included file relatively to the current heading level
  2011-12-12 19:27 [PATCH] Dynamically demote included file relatively to the current heading level Daniel Dehennin
@ 2012-04-23 12:57 ` Bastien
  0 siblings, 0 replies; 2+ messages in thread
From: Bastien @ 2012-04-23 12:57 UTC (permalink / raw)
  To: Daniel Dehennin; +Cc: emacs-orgmode

Hi Daniel,

Daniel Dehennin <daniel.dehennin@baby-gnu.org> writes:

> 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.

I applied your patch with a small modification that fixes it.
Thanks for your contribution and sorry for the delay.

Best,

-- 
 Bastien

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-04-23 13:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-12-12 19:27 [PATCH] Dynamically demote included file relatively to the current heading level Daniel Dehennin
2012-04-23 12:57 ` Bastien

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).