emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Exporting blocks without removing indentation
@ 2009-10-28 13:29 Dan Davison
  2009-10-28 15:54 ` Carsten Dominik
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Davison @ 2009-10-28 13:29 UTC (permalink / raw)
  To: emacs org-mode mailing list

Fairly late in the export process, org-exp removes the common
indentation from lines inside blocks (line 2288 of org-exp.el):

(setq rtn (org-remove-indentation code))

However, what do we do if

1. A user has a block that she wishes to export with the indentation intact?

2. An org-exp-blocks plugin has a block that it wishes to export with
   the indentation intact?

I propose that we make (1) and (2) possible. An example where (1) arises
is if, when documenting a moderately complex program, different parts of
a class or function are contained in separate blocks.

Q1. Is that aim approved of?

(2) is a bit trickier than (1). To solve (1), we could introduce a fix
in org-exp.el like

-      (setq rtn (org-remove-indentation code))
+      (setq rtn (if org-src-preserve-indentation code
+                 (org-remove-indentation code)))

However in solving (2) the plugin cannot assume that the variable
org-src-preserve-indentation is in effect (in fact it very likely
isn't). So we need to come up with a way in which the block resulting
from an org-exp-blocks plugin can have its indentation survive through
to the export product, even if org-src-preserve-indentation is globally
nil. Two possibilities are

1. We adopt a convention whereby if all lines start with some character
   (say ,) then that character is removed (but the remaining indentation
   is left intact.)

2. We transplant the indentation-removing work from org-exp.el into
   org-exp-blocks.el, and arrange that it is only performed if the block
   is *not* handled by an org-exp-blocks plugin.

Q2. Has this made sense? What solution do you suggest?

Q3. Should we use org-src-preserve-indentation in this way to affect
export? (It currently has effect when switching between the org buffer
and the code edit buffer.)

Dan

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

* Re: Exporting blocks without removing indentation
  2009-10-28 13:29 Exporting blocks without removing indentation Dan Davison
@ 2009-10-28 15:54 ` Carsten Dominik
  2009-10-28 18:14   ` Samuel Wales
  2009-11-01  1:36   ` Dan Davison
  0 siblings, 2 replies; 6+ messages in thread
From: Carsten Dominik @ 2009-10-28 15:54 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs org-mode mailing list

Hi Dan,

maybe the easiest way to implement this would be a -i (or whatever)
switch a the src block.

Switches are being processed already, so it will be simple to add one,
I think.

- Carsten

On Oct 28, 2009, at 2:29 PM, Dan Davison wrote:

> Fairly late in the export process, org-exp removes the common
> indentation from lines inside blocks (line 2288 of org-exp.el):
>
> (setq rtn (org-remove-indentation code))
>
> However, what do we do if
>
> 1. A user has a block that she wishes to export with the indentation  
> intact?
>
> 2. An org-exp-blocks plugin has a block that it wishes to export with
>   the indentation intact?
>
> I propose that we make (1) and (2) possible. An example where (1)  
> arises
> is if, when documenting a moderately complex program, different  
> parts of
> a class or function are contained in separate blocks.
>
> Q1. Is that aim approved of?
>
> (2) is a bit trickier than (1). To solve (1), we could introduce a fix
> in org-exp.el like
>
> -      (setq rtn (org-remove-indentation code))
> +      (setq rtn (if org-src-preserve-indentation code
> +                 (org-remove-indentation code)))
>
> However in solving (2) the plugin cannot assume that the variable
> org-src-preserve-indentation is in effect (in fact it very likely
> isn't). So we need to come up with a way in which the block resulting
> from an org-exp-blocks plugin can have its indentation survive through
> to the export product, even if org-src-preserve-indentation is  
> globally
> nil. Two possibilities are
>
> 1. We adopt a convention whereby if all lines start with some  
> character
>   (say ,) then that character is removed (but the remaining  
> indentation
>   is left intact.)
>
> 2. We transplant the indentation-removing work from org-exp.el into
>   org-exp-blocks.el, and arrange that it is only performed if the  
> block
>   is *not* handled by an org-exp-blocks plugin.
>
> Q2. Has this made sense? What solution do you suggest?
>
> Q3. Should we use org-src-preserve-indentation in this way to affect
> export? (It currently has effect when switching between the org buffer
> and the code edit buffer.)
>
> Dan
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

- Carsten

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

* Re: Exporting blocks without removing indentation
  2009-10-28 15:54 ` Carsten Dominik
@ 2009-10-28 18:14   ` Samuel Wales
  2009-11-01  1:36   ` Dan Davison
  1 sibling, 0 replies; 6+ messages in thread
From: Samuel Wales @ 2009-10-28 18:14 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: Dan Davison, emacs org-mode mailing list

It's probably too late to suggest this, but it might be that long
options are more easy to remember for users than short options?  Also,
they might be easier to look up in the manual if you use a non-Emacs
browser.

On 2009-10-28, Carsten Dominik <carsten.dominik@gmail.com> wrote:
> Hi Dan,
>
> maybe the easiest way to implement this would be a -i (or whatever)
> switch a the src block.
>
> Switches are being processed already, so it will be simple to add one,
> I think.
>
> - Carsten
>
> On Oct 28, 2009, at 2:29 PM, Dan Davison wrote:
>
>> Fairly late in the export process, org-exp removes the common
>> indentation from lines inside blocks (line 2288 of org-exp.el):
>>
>> (setq rtn (org-remove-indentation code))
>>
>> However, what do we do if
>>
>> 1. A user has a block that she wishes to export with the indentation
>> intact?
>>
>> 2. An org-exp-blocks plugin has a block that it wishes to export with
>>   the indentation intact?
>>
>> I propose that we make (1) and (2) possible. An example where (1)
>> arises
>> is if, when documenting a moderately complex program, different
>> parts of
>> a class or function are contained in separate blocks.
>>
>> Q1. Is that aim approved of?
>>
>> (2) is a bit trickier than (1). To solve (1), we could introduce a fix
>> in org-exp.el like
>>
>> -      (setq rtn (org-remove-indentation code))
>> +      (setq rtn (if org-src-preserve-indentation code
>> +                 (org-remove-indentation code)))
>>
>> However in solving (2) the plugin cannot assume that the variable
>> org-src-preserve-indentation is in effect (in fact it very likely
>> isn't). So we need to come up with a way in which the block resulting
>> from an org-exp-blocks plugin can have its indentation survive through
>> to the export product, even if org-src-preserve-indentation is
>> globally
>> nil. Two possibilities are
>>
>> 1. We adopt a convention whereby if all lines start with some
>> character
>>   (say ,) then that character is removed (but the remaining
>> indentation
>>   is left intact.)
>>
>> 2. We transplant the indentation-removing work from org-exp.el into
>>   org-exp-blocks.el, and arrange that it is only performed if the
>> block
>>   is *not* handled by an org-exp-blocks plugin.
>>
>> Q2. Has this made sense? What solution do you suggest?
>>
>> Q3. Should we use org-src-preserve-indentation in this way to affect
>> export? (It currently has effect when switching between the org buffer
>> and the code edit buffer.)
>>
>> Dan
>>
>>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Remember: use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>
> - Carsten
>
>
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>


-- 
Q: How many CDC "scientists" does it take to change a lightbulb?
A: "You only think it's dark." [CDC has denied ME/"CFS" for 25 years]
=====================================================================
Retrovirus: http://www.wpinstitute.org/xmrv/xmrv_qa.html

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

* Re: Exporting blocks without removing indentation
  2009-10-28 15:54 ` Carsten Dominik
  2009-10-28 18:14   ` Samuel Wales
@ 2009-11-01  1:36   ` Dan Davison
  2009-11-01  7:53     ` Carsten Dominik
  1 sibling, 1 reply; 6+ messages in thread
From: Dan Davison @ 2009-11-01  1:36 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs org-mode mailing list

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

Carsten Dominik <carsten.dominik@gmail.com> writes:

> Hi Dan,
>
> maybe the easiest way to implement this would be a -i (or whatever)
> switch a the src block.

Thanks, I have gone ahead with your suggestion. As you suggest, I've
added a -i switch that prevents the indentation of a block from being
removed during export. If the variable org-src-preserve-indentation[1]
is non-nil, then that is equivalent to having a -i switch on every
source block.

Then the issue remained that an org-exp-block plugin is unable to return
a block with the original indentation if the indentation is removed
before it receives it. So I have made that indentation removal
contingent on the same criteria (non-nil variable or -i switch).

Those 2 commits are in branch ded-indent at
git://repo.or.cz/org-mode/babel.git, and also pasted below.

Dan

Footnotes:

[1] org-src-preserve-indentation also acts in org-src-mode, where it
prevents indentation from being removed when switching between org and
language mode buffers.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-code-indentation-to-be-preserved-on-export.patch --]
[-- Type: text/x-diff, Size: 2492 bytes --]

From d9447ad7b08332cac005bcb602ac1e55cf0588cf Mon Sep 17 00:00:00 2001
From: Dan Davison <davison@stats.ox.ac.uk>
Date: Sat, 31 Oct 2009 15:43:42 -0400
Subject: [PATCH 1/2] Allow code indentation to be preserved on export

This is controlled on a per-block basis with a new src block flag
-i. It can be controlled globally using the variable
org-src-preserve-indentation.
---
 lisp/org-exp.el |    4 +++-
 lisp/org-src.el |   12 +++++++-----
 2 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/lisp/org-exp.el b/lisp/org-exp.el
index f08ce41..c6ffe75 100644
--- a/lisp/org-exp.el
+++ b/lisp/org-exp.el
@@ -2273,6 +2273,8 @@ INDENT was the original indentation of the block."
 	    rpllbl (string-match "-r\\>" opts)
 	    keepp (string-match "-k\\>" opts)
 	    textareap (string-match "-t\\>" opts)
+	    preserve-indentp (or org-src-preserve-indentation
+				 (string-match "-i\\>" opts))
 	    cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
 		     (string-to-number (match-string 1 opts))
 		   80)
@@ -2285,7 +2287,7 @@ INDENT was the original indentation of the block."
 	;; we cannot use numbering or highlighting.
 	(setq num nil cont nil lang nil))
       (if keepp (setq rpllbl 'keep))
-      (setq rtn (org-remove-indentation code))
+      (setq rtn (if preserve-indentp code (org-remove-indentation code)))
       (when (string-match "^," rtn)
 	(setq rtn (with-temp-buffer
 		    (insert rtn)
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 463bfa8..de8f8a2 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -87,11 +87,13 @@ These are the regions where each line starts with a colon."
 
 (defcustom org-src-preserve-indentation nil
   "If non-nil, leading whitespace characters in source code
-  blocks are preserved. Otherwise, after editing with
-  \\[org-edit-src-code], the minimum (across-lines) number of
-  leading whitespace characters are removed from all lines, and
-  the code block is then uniformly indented according to the
-  value of `org-edit-src-content-indentation'."
+  blocks are preserved on export, and when switching between the
+  org buffer and the language mode edit buffer. If this variable
+  is nil then, after editing with \\[org-edit-src-code], the
+  minimum (across-lines) number of leading whitespace characters
+  are removed from all lines, and the code block is uniformly
+  indented according to the value of
+  `org-edit-src-content-indentation'."
   :group 'org-edit-structure
   :type 'boolean)
 
-- 
1.6.3.3


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Allow-org-exp-blocks-plugins-to-return-block-with-in.patch --]
[-- Type: text/x-diff, Size: 2776 bytes --]

From 60a5a63e6c6d66d1925f8cfc1191502bc34a7269 Mon Sep 17 00:00:00 2001
From: Dan Davison <davison@stats.ox.ac.uk>
Date: Sat, 31 Oct 2009 20:39:57 -0400
Subject: [PATCH 2/2] Allow org-exp-blocks plugins to return block with indentation unaltered.

With these changes, if org-src-preserve-indentation is non-nil, or if
the block has a -i switch, then the behaviour of org-exp-blocks is
altered as follows:

1. Indentation is not removed before passing the block contents to the
   block-transforming plugin.

2. The result returned by the plugin is not re-indented.
---
 lisp/org-exp-blocks.el |   17 ++++++++++-------
 1 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/lisp/org-exp-blocks.el b/lisp/org-exp-blocks.el
index 646688d..c92a9fa 100644
--- a/lisp/org-exp-blocks.el
+++ b/lisp/org-exp-blocks.el
@@ -166,7 +166,7 @@ specified in BLOCKS which default to the value of
   (save-window-excursion
     (let ((case-fold-search t)
 	  (types '())
-	  indentation type func start)
+	  indentation type func start body headers preserve-indent)
       (flet ((interblock (start end)
 			 (mapcar (lambda (pair) (funcall (second pair) start end))
 				 org-export-interblocks)))
@@ -176,17 +176,20 @@ specified in BLOCKS which default to the value of
 		"^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^\000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
           (setq indentation (length (match-string 1)))
 	  (setq type (intern (match-string 2)))
+	  (setq headers (save-match-data (split-string (match-string 3))))
+	  (setq body (match-string 4))
+	  (setq preserve-indent (or org-src-preserve-indentation (member "-i" headers)))
+	  (unless preserve-indent
+	    (setq body (save-match-data (org-remove-indentation body))))
 	  (unless (memq type types) (setq types (cons type types)))
 	  (save-match-data (interblock start (match-beginning 0)))
 	  (if (setq func (cadr (assoc type org-export-blocks)))
 	      (progn
                 (replace-match (save-match-data
-                                 (if (memq type org-export-blocks-witheld)
-                                     ""
-                                   (apply func (save-match-data (org-remove-indentation (match-string 4)))
-                                          (split-string (match-string 3) " ")))) t t)
-                ;; indent block
-                (indent-code-rigidly (match-beginning 0) (match-end 0) indentation)))
+                                 (if (memq type org-export-blocks-witheld) ""
+                                   (apply func body headers))) t t)
+                (unless preserve-indent
+		  (indent-code-rigidly (match-beginning 0) (match-end 0) indentation))))
 	  (setq start (match-end 0)))
 	(interblock start (point-max))))))
 
-- 
1.6.3.3


[-- Attachment #4: Type: text/plain, Size: 2569 bytes --]



>
> Switches are being processed already, so it will be simple to add one,
> I think.
>
> - Carsten
>
> On Oct 28, 2009, at 2:29 PM, Dan Davison wrote:
>
>> Fairly late in the export process, org-exp removes the common
>> indentation from lines inside blocks (line 2288 of org-exp.el):
>>
>> (setq rtn (org-remove-indentation code))
>>
>> However, what do we do if
>>
>> 1. A user has a block that she wishes to export with the indentation
>> intact?
>>
>> 2. An org-exp-blocks plugin has a block that it wishes to export with
>>   the indentation intact?
>>
>> I propose that we make (1) and (2) possible. An example where (1)
>> arises
>> is if, when documenting a moderately complex program, different
>> parts of
>> a class or function are contained in separate blocks.
>>
>> Q1. Is that aim approved of?
>>
>> (2) is a bit trickier than (1). To solve (1), we could introduce a fix
>> in org-exp.el like
>>
>> -      (setq rtn (org-remove-indentation code))
>> +      (setq rtn (if org-src-preserve-indentation code
>> +                 (org-remove-indentation code)))
>>
>> However in solving (2) the plugin cannot assume that the variable
>> org-src-preserve-indentation is in effect (in fact it very likely
>> isn't). So we need to come up with a way in which the block resulting
>> from an org-exp-blocks plugin can have its indentation survive through
>> to the export product, even if org-src-preserve-indentation is
>> globally
>> nil. Two possibilities are
>>
>> 1. We adopt a convention whereby if all lines start with some
>> character
>>   (say ,) then that character is removed (but the remaining
>> indentation
>>   is left intact.)
>>
>> 2. We transplant the indentation-removing work from org-exp.el into
>>   org-exp-blocks.el, and arrange that it is only performed if the
>> block
>>   is *not* handled by an org-exp-blocks plugin.
>>
>> Q2. Has this made sense? What solution do you suggest?
>>
>> Q3. Should we use org-src-preserve-indentation in this way to affect
>> export? (It currently has effect when switching between the org buffer
>> and the code edit buffer.)
>>
>> Dan
>>
>>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Remember: use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>
> - Carsten
>
>
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode


[-- Attachment #5: Type: text/plain, Size: 204 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Remember: use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: Exporting blocks without removing indentation
  2009-11-01  1:36   ` Dan Davison
@ 2009-11-01  7:53     ` Carsten Dominik
  2009-11-01 14:44       ` Dan Davison
  0 siblings, 1 reply; 6+ messages in thread
From: Carsten Dominik @ 2009-11-01  7:53 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs org-mode mailing list

Hi Dan,

this looks really good.  I have applied the patch and again
made the necessary entries in lisp/ChangeLog - it would be nice if
you could make them next time.  You know how that works?  Just press
C-c x 4 in each top level form you have changed and write a short
commentary in the ChangeLog file.  I recommend to do this in the very  
end,
to minimize merge conflicts - unfortunately ChamgeLog almost always  
creates
a simple conflict because changes are always written to the top of the  
file.
Usually I create a git diff in Emacs (if you use magit, press "D" and  
then
for example "origin/master" as base.  In the dif buffer, you can press  
RET
at each change to get to the source buffer, and then C-x 4 a to create  
the
ChangeLog entry.

I know this is annoying because git of course does record its changes so
nicely - but such are the rules for code in Emacs.

Also I have changed the indentation in the docstring that it complied to
Emacs rules: First line indented like TAB does it, but the second and
following lines not indented so that, when the docstring is displayed,
you do not get those lines with initial whitespace.

Thanks!

- Carsten


Changes at master~1
	Modified lisp/ChangeLog
diff --git a/lisp/ChangeLog b/lisp/ChangeLog
index 3bea93f..41f3734 100755
--- a/lisp/ChangeLog
+++ b/lisp/ChangeLog
@@ -1,3 +1,16 @@
+2009-11-01  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org-src.el (org-src-preserve-indentation): Document that this
+	variable is also used during export.
+
+	* org-exp.el (org-export-format-source-code-or-example): Preserve
+	indentation if a block has a -i option, or if
+	`org-src-preserve-indentation' is set.
+
+	* org-exp-blocks.el (org-export-blocks-preprocess): Preserve
+	indentation if a block has a -i option, or if
+	`org-src-preserve-indentation' is set.
+
  2009-10-31  Carsten Dominik  <carsten.dominik@gmail.com>

  	* org.el (org-mode-map): Define the new archiving keys.
	Modified lisp/org-src.el
diff --git a/lisp/org-src.el b/lisp/org-src.el
index de8f8a2..eba8ef3 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -87,13 +87,12 @@ These are the regions where each line starts with  
a colon."

  (defcustom org-src-preserve-indentation nil
    "If non-nil, leading whitespace characters in source code
-  blocks are preserved on export, and when switching between the
-  org buffer and the language mode edit buffer. If this variable
-  is nil then, after editing with \\[org-edit-src-code], the
-  minimum (across-lines) number of leading whitespace characters
-  are removed from all lines, and the code block is uniformly
-  indented according to the value of
-  `org-edit-src-content-indentation'."
+blocks are preserved on export, and when switching between the
+org buffer and the language mode edit buffer. If this variable
+is nil then, after editing with \\[org-edit-src-code], the
+minimum (across-lines) number of leading whitespace characters
+are removed from all lines, and the code block is uniformly
+indented according to the value of `org-edit-src-content-indentation'."
    :group 'org-edit-structure
    :type 'boolean)



On Nov 1, 2009, at 2:36 AM, Dan Davison wrote:

> Carsten Dominik <carsten.dominik@gmail.com> writes:
>
>> Hi Dan,
>>
>> maybe the easiest way to implement this would be a -i (or whatever)
>> switch a the src block.
>
> Thanks, I have gone ahead with your suggestion. As you suggest, I've
> added a -i switch that prevents the indentation of a block from being
> removed during export. If the variable org-src-preserve-indentation[1]
> is non-nil, then that is equivalent to having a -i switch on every
> source block.
>
> Then the issue remained that an org-exp-block plugin is unable to  
> return
> a block with the original indentation if the indentation is removed
> before it receives it. So I have made that indentation removal
> contingent on the same criteria (non-nil variable or -i switch).
>
> Those 2 commits are in branch ded-indent at
> git://repo.or.cz/org-mode/babel.git, and also pasted below.
>
> Dan
>
> Footnotes:
>
> [1] org-src-preserve-indentation also acts in org-src-mode, where it
> prevents indentation from being removed when switching between org and
> language mode buffers.
>
> From d9447ad7b08332cac005bcb602ac1e55cf0588cf Mon Sep 17 00:00:00 2001
> From: Dan Davison <davison@stats.ox.ac.uk>
> Date: Sat, 31 Oct 2009 15:43:42 -0400
> Subject: [PATCH 1/2] Allow code indentation to be preserved on export
>
> This is controlled on a per-block basis with a new src block flag
> -i. It can be controlled globally using the variable
> org-src-preserve-indentation.
> ---
> lisp/org-exp.el |    4 +++-
> lisp/org-src.el |   12 +++++++-----
> 2 files changed, 10 insertions(+), 6 deletions(-)
>
> diff --git a/lisp/org-exp.el b/lisp/org-exp.el
> index f08ce41..c6ffe75 100644
> --- a/lisp/org-exp.el
> +++ b/lisp/org-exp.el
> @@ -2273,6 +2273,8 @@ INDENT was the original indentation of the  
> block."
> 	    rpllbl (string-match "-r\\>" opts)
> 	    keepp (string-match "-k\\>" opts)
> 	    textareap (string-match "-t\\>" opts)
> +	    preserve-indentp (or org-src-preserve-indentation
> +				 (string-match "-i\\>" opts))
> 	    cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
> 		     (string-to-number (match-string 1 opts))
> 		   80)
> @@ -2285,7 +2287,7 @@ INDENT was the original indentation of the  
> block."
> 	;; we cannot use numbering or highlighting.
> 	(setq num nil cont nil lang nil))
>       (if keepp (setq rpllbl 'keep))
> -      (setq rtn (org-remove-indentation code))
> +      (setq rtn (if preserve-indentp code (org-remove-indentation  
> code)))
>       (when (string-match "^," rtn)
> 	(setq rtn (with-temp-buffer
> 		    (insert rtn)
> diff --git a/lisp/org-src.el b/lisp/org-src.el
> index 463bfa8..de8f8a2 100644
> --- a/lisp/org-src.el
> +++ b/lisp/org-src.el
> @@ -87,11 +87,13 @@ These are the regions where each line starts  
> with a colon."
>
> (defcustom org-src-preserve-indentation nil
>   "If non-nil, leading whitespace characters in source code
> -  blocks are preserved. Otherwise, after editing with
> -  \\[org-edit-src-code], the minimum (across-lines) number of
> -  leading whitespace characters are removed from all lines, and
> -  the code block is then uniformly indented according to the
> -  value of `org-edit-src-content-indentation'."
> +  blocks are preserved on export, and when switching between the
> +  org buffer and the language mode edit buffer. If this variable
> +  is nil then, after editing with \\[org-edit-src-code], the
> +  minimum (across-lines) number of leading whitespace characters
> +  are removed from all lines, and the code block is uniformly
> +  indented according to the value of
> +  `org-edit-src-content-indentation'."
>   :group 'org-edit-structure
>   :type 'boolean)
>
> -- 
> 1.6.3.3
>
> From 60a5a63e6c6d66d1925f8cfc1191502bc34a7269 Mon Sep 17 00:00:00 2001
> From: Dan Davison <davison@stats.ox.ac.uk>
> Date: Sat, 31 Oct 2009 20:39:57 -0400
> Subject: [PATCH 2/2] Allow org-exp-blocks plugins to return block  
> with indentation unaltered.
>
> With these changes, if org-src-preserve-indentation is non-nil, or if
> the block has a -i switch, then the behaviour of org-exp-blocks is
> altered as follows:
>
> 1. Indentation is not removed before passing the block contents to the
>   block-transforming plugin.
>
> 2. The result returned by the plugin is not re-indented.
> ---
> lisp/org-exp-blocks.el |   17 ++++++++++-------
> 1 files changed, 10 insertions(+), 7 deletions(-)
>
> diff --git a/lisp/org-exp-blocks.el b/lisp/org-exp-blocks.el
> index 646688d..c92a9fa 100644
> --- a/lisp/org-exp-blocks.el
> +++ b/lisp/org-exp-blocks.el
> @@ -166,7 +166,7 @@ specified in BLOCKS which default to the value of
>   (save-window-excursion
>     (let ((case-fold-search t)
> 	  (types '())
> -	  indentation type func start)
> +	  indentation type func start body headers preserve-indent)
>       (flet ((interblock (start end)
> 			 (mapcar (lambda (pair) (funcall (second pair) start end))
> 				 org-export-interblocks)))
> @@ -176,17 +176,20 @@ specified in BLOCKS which default to the value  
> of
> 		"^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[ \t]*\\(.*\\)?[\r\n]\\([^ 
> \000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
>           (setq indentation (length (match-string 1)))
> 	  (setq type (intern (match-string 2)))
> +	  (setq headers (save-match-data (split-string (match-string 3))))
> +	  (setq body (match-string 4))
> +	  (setq preserve-indent (or org-src-preserve-indentation (member "- 
> i" headers)))
> +	  (unless preserve-indent
> +	    (setq body (save-match-data (org-remove-indentation body))))
> 	  (unless (memq type types) (setq types (cons type types)))
> 	  (save-match-data (interblock start (match-beginning 0)))
> 	  (if (setq func (cadr (assoc type org-export-blocks)))
> 	      (progn
>                 (replace-match (save-match-data
> -                                 (if (memq type org-export-blocks- 
> witheld)
> -                                     ""
> -                                   (apply func (save-match-data  
> (org-remove-indentation (match-string 4)))
> -                                          (split-string (match- 
> string 3) " ")))) t t)
> -                ;; indent block
> -                (indent-code-rigidly (match-beginning 0) (match-end  
> 0) indentation)))
> +                                 (if (memq type org-export-blocks- 
> witheld) ""
> +                                   (apply func body headers))) t t)
> +                (unless preserve-indent
> +		  (indent-code-rigidly (match-beginning 0) (match-end 0)  
> indentation))))
> 	  (setq start (match-end 0)))
> 	(interblock start (point-max))))))
>
> -- 
> 1.6.3.3
>
>
>
>>
>> Switches are being processed already, so it will be simple to add  
>> one,
>> I think.
>>
>> - Carsten
>>
>> On Oct 28, 2009, at 2:29 PM, Dan Davison wrote:
>>
>>> Fairly late in the export process, org-exp removes the common
>>> indentation from lines inside blocks (line 2288 of org-exp.el):
>>>
>>> (setq rtn (org-remove-indentation code))
>>>
>>> However, what do we do if
>>>
>>> 1. A user has a block that she wishes to export with the indentation
>>> intact?
>>>
>>> 2. An org-exp-blocks plugin has a block that it wishes to export  
>>> with
>>>  the indentation intact?
>>>
>>> I propose that we make (1) and (2) possible. An example where (1)
>>> arises
>>> is if, when documenting a moderately complex program, different
>>> parts of
>>> a class or function are contained in separate blocks.
>>>
>>> Q1. Is that aim approved of?
>>>
>>> (2) is a bit trickier than (1). To solve (1), we could introduce a  
>>> fix
>>> in org-exp.el like
>>>
>>> -      (setq rtn (org-remove-indentation code))
>>> +      (setq rtn (if org-src-preserve-indentation code
>>> +                 (org-remove-indentation code)))
>>>
>>> However in solving (2) the plugin cannot assume that the variable
>>> org-src-preserve-indentation is in effect (in fact it very likely
>>> isn't). So we need to come up with a way in which the block  
>>> resulting
>>> from an org-exp-blocks plugin can have its indentation survive  
>>> through
>>> to the export product, even if org-src-preserve-indentation is
>>> globally
>>> nil. Two possibilities are
>>>
>>> 1. We adopt a convention whereby if all lines start with some
>>> character
>>>  (say ,) then that character is removed (but the remaining
>>> indentation
>>>  is left intact.)
>>>
>>> 2. We transplant the indentation-removing work from org-exp.el into
>>>  org-exp-blocks.el, and arrange that it is only performed if the
>>> block
>>>  is *not* handled by an org-exp-blocks plugin.
>>>
>>> Q2. Has this made sense? What solution do you suggest?
>>>
>>> Q3. Should we use org-src-preserve-indentation in this way to affect
>>> export? (It currently has effect when switching between the org  
>>> buffer
>>> and the code edit buffer.)
>>>
>>> Dan
>>>
>>>
>>> _______________________________________________
>>> Emacs-orgmode mailing list
>>> Remember: use `Reply All' to send replies to the list.
>>> Emacs-orgmode@gnu.org
>>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>>
>> - Carsten
>>
>>
>>
>>
>>
>> _______________________________________________
>> Emacs-orgmode mailing list
>> Remember: use `Reply All' to send replies to the list.
>> Emacs-orgmode@gnu.org
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>

- Carsten

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

* Re: Exporting blocks without removing indentation
  2009-11-01  7:53     ` Carsten Dominik
@ 2009-11-01 14:44       ` Dan Davison
  0 siblings, 0 replies; 6+ messages in thread
From: Dan Davison @ 2009-11-01 14:44 UTC (permalink / raw)
  To: Carsten Dominik; +Cc: emacs org-mode mailing list

Carsten Dominik <carsten.dominik@gmail.com> writes:

> Hi Dan,
>
> this looks really good.  I have applied the patch and again
> made the necessary entries in lisp/ChangeLog -  it would be nice if
> you could make them next time.  

Thanks, I think I will remember next time!

<...>
> I recommend to do this in the very
> end,
> to minimize merge conflicts - unfortunately ChamgeLog almost always
> creates
> a simple conflict because changes are always written to the top of the
> file.
> Usually I create a git diff in Emacs (if you use magit, press "D" and
> then
> for example "origin/master" as base.  In the dif buffer, you can press
> RET
> at each change to get to the source buffer, and then C-x 4 a to create
> the
> ChangeLog entry.

Thanks, that's really useful. I didn 't know any of that.

Dan

>
> I know this is annoying because git of course does record its changes so
> nicely - but such are the rules for code in Emacs.
>
> Also I have changed the indentation in the docstring that it complied to
> Emacs rules: First line indented like TAB does it, but the second and
> following lines not indented so that, when the docstring is displayed,
> you do not get those lines with initial whitespace.
>
> Thanks!
>
> - Carsten
>
>
> Changes at master~1
> 	Modified lisp/ChangeLog
> diff --git a/lisp/ChangeLog b/lisp/ChangeLog
> index 3bea93f..41f3734 100755
> --- a/lisp/ChangeLog
> +++ b/lisp/ChangeLog
> @@ -1,3 +1,16 @@
> +2009-11-01  Carsten Dominik  <carsten.dominik@gmail.com>
> +
> +	* org-src.el (org-src-preserve-indentation): Document that this
> +	variable is also used during export.
> +
> +	* org-exp.el (org-export-format-source-code-or-example): Preserve
> +	indentation if a block has a -i option, or if
> +	`org-src-preserve-indentation' is set.
> +
> +	* org-exp-blocks.el (org-export-blocks-preprocess): Preserve
> +	indentation if a block has a -i option, or if
> +	`org-src-preserve-indentation' is set.
> +
>  2009-10-31  Carsten Dominik  <carsten.dominik@gmail.com>
>
>  	* org.el (org-mode-map): Define the new archiving keys.
> 	Modified lisp/org-src.el
> diff --git a/lisp/org-src.el b/lisp/org-src.el
> index de8f8a2..eba8ef3 100644
> --- a/lisp/org-src.el
> +++ b/lisp/org-src.el
> @@ -87,13 +87,12 @@ These are the regions where each line starts with
> a colon."
>
>  (defcustom org-src-preserve-indentation nil
>    "If non-nil, leading whitespace characters in source code
> -  blocks are preserved on export, and when switching between the
> -  org buffer and the language mode edit buffer. If this variable
> -  is nil then, after editing with \\[org-edit-src-code], the
> -  minimum (across-lines) number of leading whitespace characters
> -  are removed from all lines, and the code block is uniformly
> -  indented according to the value of
> -  `org-edit-src-content-indentation'."
> +blocks are preserved on export, and when switching between the
> +org buffer and the language mode edit buffer. If this variable
> +is nil then, after editing with \\[org-edit-src-code], the
> +minimum (across-lines) number of leading whitespace characters
> +are removed from all lines, and the code block is uniformly
> +indented according to the value of `org-edit-src-content-indentation'."
>    :group 'org-edit-structure
>    :type 'boolean)
>
>
>
> On Nov 1, 2009, at 2:36 AM, Dan Davison wrote:
>
>> Carsten Dominik <carsten.dominik@gmail.com> writes:
>>
>>> Hi Dan,
>>>
>>> maybe the easiest way to implement this would be a -i (or whatever)
>>> switch a the src block.
>>
>> Thanks, I have gone ahead with your suggestion. As you suggest, I've
>> added a -i switch that prevents the indentation of a block from being
>> removed during export. If the variable org-src-preserve-indentation[1]
>> is non-nil, then that is equivalent to having a -i switch on every
>> source block.
>>
>> Then the issue remained that an org-exp-block plugin is unable to
>> return
>> a block with the original indentation if the indentation is removed
>> before it receives it. So I have made that indentation removal
>> contingent on the same criteria (non-nil variable or -i switch).
>>
>> Those 2 commits are in branch ded-indent at
>> git://repo.or.cz/org-mode/babel.git, and also pasted below.
>>
>> Dan
>>
>> Footnotes:
>>
>> [1] org-src-preserve-indentation also acts in org-src-mode, where it
>> prevents indentation from being removed when switching between org and
>> language mode buffers.
>>
>> From d9447ad7b08332cac005bcb602ac1e55cf0588cf Mon Sep 17 00:00:00 2001
>> From: Dan Davison <davison@stats.ox.ac.uk>
>> Date: Sat, 31 Oct 2009 15:43:42 -0400
>> Subject: [PATCH 1/2] Allow code indentation to be preserved on export
>>
>> This is controlled on a per-block basis with a new src block flag
>> -i. It can be controlled globally using the variable
>> org-src-preserve-indentation.
>> ---
>> lisp/org-exp.el |    4 +++-
>> lisp/org-src.el |   12 +++++++-----
>> 2 files changed, 10 insertions(+), 6 deletions(-)
>>
>> diff --git a/lisp/org-exp.el b/lisp/org-exp.el
>> index f08ce41..c6ffe75 100644
>> --- a/lisp/org-exp.el
>> +++ b/lisp/org-exp.el
>> @@ -2273,6 +2273,8 @@ INDENT was the original indentation of the
>> block."
>> 	    rpllbl (string-match "-r\\>" opts)
>> 	    keepp (string-match "-k\\>" opts)
>> 	    textareap (string-match "-t\\>" opts)
>> +	    preserve-indentp (or org-src-preserve-indentation
>> +				 (string-match "-i\\>" opts))
>> 	    cols (if (string-match "-w[ \t]+\\([0-9]+\\)" opts)
>> 		     (string-to-number (match-string 1 opts))
>> 		   80)
>> @@ -2285,7 +2287,7 @@ INDENT was the original indentation of the
>> block."
>> 	;; we cannot use numbering or highlighting.
>> 	(setq num nil cont nil lang nil))
>>       (if keepp (setq rpllbl 'keep))
>> -      (setq rtn (org-remove-indentation code))
>> +      (setq rtn (if preserve-indentp code (org-remove-indentation
>> code)))
>>       (when (string-match "^," rtn)
>> 	(setq rtn (with-temp-buffer
>> 		    (insert rtn)
>> diff --git a/lisp/org-src.el b/lisp/org-src.el
>> index 463bfa8..de8f8a2 100644
>> --- a/lisp/org-src.el
>> +++ b/lisp/org-src.el
>> @@ -87,11 +87,13 @@ These are the regions where each line starts
>> with a colon."
>>
>> (defcustom org-src-preserve-indentation nil
>>   "If non-nil, leading whitespace characters in source code
>> -  blocks are preserved. Otherwise, after editing with
>> -  \\[org-edit-src-code], the minimum (across-lines) number of
>> -  leading whitespace characters are removed from all lines, and
>> -  the code block is then uniformly indented according to the
>> -  value of `org-edit-src-content-indentation'."
>> +  blocks are preserved on export, and when switching between the
>> +  org buffer and the language mode edit buffer. If this variable
>> +  is nil then, after editing with \\[org-edit-src-code], the
>> +  minimum (across-lines) number of leading whitespace characters
>> +  are removed from all lines, and the code block is uniformly
>> +  indented according to the value of
>> +  `org-edit-src-content-indentation'."
>>   :group 'org-edit-structure
>>   :type 'boolean)
>>
>> --
>> 1.6.3.3
>>
>> From 60a5a63e6c6d66d1925f8cfc1191502bc34a7269 Mon Sep 17 00:00:00 2001
>> From: Dan Davison <davison@stats.ox.ac.uk>
>> Date: Sat, 31 Oct 2009 20:39:57 -0400
>> Subject: [PATCH 2/2] Allow org-exp-blocks plugins to return block
>> with indentation unaltered.
>>
>> With these changes, if org-src-preserve-indentation is non-nil, or if
>> the block has a -i switch, then the behaviour of org-exp-blocks is
>> altered as follows:
>>
>> 1. Indentation is not removed before passing the block contents to the
>>   block-transforming plugin.
>>
>> 2. The result returned by the plugin is not re-indented.
>> ---
>> lisp/org-exp-blocks.el |   17 ++++++++++-------
>> 1 files changed, 10 insertions(+), 7 deletions(-)
>>
>> diff --git a/lisp/org-exp-blocks.el b/lisp/org-exp-blocks.el
>> index 646688d..c92a9fa 100644
>> --- a/lisp/org-exp-blocks.el
>> +++ b/lisp/org-exp-blocks.el
>> @@ -166,7 +166,7 @@ specified in BLOCKS which default to the value of
>>   (save-window-excursion
>>     (let ((case-fold-search t)
>> 	  (types '())
>> -	  indentation type func start)
>> +	  indentation type func start body headers preserve-indent)
>>       (flet ((interblock (start end)
>> 			 (mapcar (lambda (pair) (funcall (second pair) start end))
>> 				 org-export-interblocks)))
>> @@ -176,17 +176,20 @@ specified in BLOCKS which default to the value
>> of
>> 		"^\\([ \t]*\\)#\\+begin_\\(\\S-+\\)[
>> \t]*\\(.*\\)?[\r\n]\\([^
>> \000]*?\\)[\r\n][ \t]*#\\+end_\\S-+.*" nil t)
>>           (setq indentation (length (match-string 1)))
>> 	  (setq type (intern (match-string 2)))
>> +	  (setq headers (save-match-data (split-string (match-string 3))))
>> +	  (setq body (match-string 4))
>> +	  (setq preserve-indent (or org-src-preserve-indentation
>> (member "-
>> i" headers)))
>> +	  (unless preserve-indent
>> +	    (setq body (save-match-data (org-remove-indentation body))))
>> 	  (unless (memq type types) (setq types (cons type types)))
>> 	  (save-match-data (interblock start (match-beginning 0)))
>> 	  (if (setq func (cadr (assoc type org-export-blocks)))
>> 	      (progn
>>                 (replace-match (save-match-data
>> -                                 (if (memq type org-export-blocks-
>> witheld)
>> -                                     ""
>> -                                   (apply func (save-match-data
>> (org-remove-indentation (match-string 4)))
>> -                                          (split-string (match-
>> string 3) " ")))) t t)
>> -                ;; indent block
>> -                (indent-code-rigidly (match-beginning 0) (match-end
>> 0) indentation)))
>> +                                 (if (memq type org-export-blocks-
>> witheld) ""
>> +                                   (apply func body headers))) t t)
>> +                (unless preserve-indent
>> +		  (indent-code-rigidly (match-beginning 0) (match-end
>> 0) indentation))))
>> 	  (setq start (match-end 0)))
>> 	(interblock start (point-max))))))
>>
>> --
>> 1.6.3.3
>>
>>
>>
>>>
>>> Switches are being processed already, so it will be simple to add
>>> one,
>>> I think.
>>>
>>> - Carsten
>>>
>>> On Oct 28, 2009, at 2:29 PM, Dan Davison wrote:
>>>
>>>> Fairly late in the export process, org-exp removes the common
>>>> indentation from lines inside blocks (line 2288 of org-exp.el):
>>>>
>>>> (setq rtn (org-remove-indentation code))
>>>>
>>>> However, what do we do if
>>>>
>>>> 1. A user has a block that she wishes to export with the indentation
>>>> intact?
>>>>
>>>> 2. An org-exp-blocks plugin has a block that it wishes to export
>>>> with
>>>>  the indentation intact?
>>>>
>>>> I propose that we make (1) and (2) possible. An example where (1)
>>>> arises
>>>> is if, when documenting a moderately complex program, different
>>>> parts of
>>>> a class or function are contained in separate blocks.
>>>>
>>>> Q1. Is that aim approved of?
>>>>
>>>> (2) is a bit trickier than (1). To solve (1), we could introduce a
>>>> fix
>>>> in org-exp.el like
>>>>
>>>> -      (setq rtn (org-remove-indentation code))
>>>> +      (setq rtn (if org-src-preserve-indentation code
>>>> +                 (org-remove-indentation code)))
>>>>
>>>> However in solving (2) the plugin cannot assume that the variable
>>>> org-src-preserve-indentation is in effect (in fact it very likely
>>>> isn't). So we need to come up with a way in which the block
>>>> resulting
>>>> from an org-exp-blocks plugin can have its indentation survive
>>>> through
>>>> to the export product, even if org-src-preserve-indentation is
>>>> globally
>>>> nil. Two possibilities are
>>>>
>>>> 1. We adopt a convention whereby if all lines start with some
>>>> character
>>>>  (say ,) then that character is removed (but the remaining
>>>> indentation
>>>>  is left intact.)
>>>>
>>>> 2. We transplant the indentation-removing work from org-exp.el into
>>>>  org-exp-blocks.el, and arrange that it is only performed if the
>>>> block
>>>>  is *not* handled by an org-exp-blocks plugin.
>>>>
>>>> Q2. Has this made sense? What solution do you suggest?
>>>>
>>>> Q3. Should we use org-src-preserve-indentation in this way to affect
>>>> export? (It currently has effect when switching between the org
>>>> buffer
>>>> and the code edit buffer.)
>>>>
>>>> Dan
>>>>
>>>>
>>>> _______________________________________________
>>>> Emacs-orgmode mailing list
>>>> Remember: use `Reply All' to send replies to the list.
>>>> Emacs-orgmode@gnu.org
>>>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>>>
>>> - Carsten
>>>
>>>
>>>
>>>
>>>
>>> _______________________________________________
>>> Emacs-orgmode mailing list
>>> Remember: use `Reply All' to send replies to the list.
>>> Emacs-orgmode@gnu.org
>>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>>
>
> - Carsten
>
>
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

end of thread, other threads:[~2009-11-01 14:44 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-28 13:29 Exporting blocks without removing indentation Dan Davison
2009-10-28 15:54 ` Carsten Dominik
2009-10-28 18:14   ` Samuel Wales
2009-11-01  1:36   ` Dan Davison
2009-11-01  7:53     ` Carsten Dominik
2009-11-01 14:44       ` Dan Davison

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