emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Dan Davison <davison@stats.ox.ac.uk>
To: Carsten Dominik <carsten.dominik@gmail.com>
Cc: emacs org-mode mailing list <emacs-orgmode@gnu.org>
Subject: Re: Exporting blocks without removing indentation
Date: Sat, 31 Oct 2009 21:36:58 -0400	[thread overview]
Message-ID: <87iqdvyqit.fsf@stats.ox.ac.uk> (raw)
In-Reply-To: 064DBD81-9D10-46D6-911B-67E83EE10A11@gmail.com

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

  parent reply	other threads:[~2009-11-01  1:37 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2009-11-01  7:53     ` Carsten Dominik
2009-11-01 14:44       ` Dan Davison

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=87iqdvyqit.fsf@stats.ox.ac.uk \
    --to=davison@stats.ox.ac.uk \
    --cc=carsten.dominik@gmail.com \
    --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).