emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Orgmode] Exporting blocks without removing indentation


From: Dan Davison
Subject: Re: [Orgmode] Exporting blocks without removing indentation
Date: Sat, 31 Oct 2009 21:36:58 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

Carsten Dominik <address@hidden> 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 <address@hidden>
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 <address@hidden>
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.
>> address@hidden
>> http://lists.gnu.org/mailman/listinfo/emacs-orgmode
>
> - Carsten
>
>
>
>
>
> _______________________________________________
> Emacs-orgmode mailing list
> Remember: use `Reply All' to send replies to the list.
> address@hidden
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode


reply via email to

[Prev in Thread] Current Thread [Next in Thread]