emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] incorrect indentation when tangling with org-src-preserve-indentation
@ 2010-10-09 11:09 Antti Kaihola
  2010-10-09 12:22 ` [babel] " Antti Kaihola
  0 siblings, 1 reply; 7+ messages in thread
From: Antti Kaihola @ 2010-10-09 11:09 UTC (permalink / raw)
  To: emacs-orgmode

As pointed out in the documentation, when tangling Python code, it's
important to set org-src-preserve-indentation to a non-nil value.
However, tangling still doesn't seem to work correcly: If the first
line of a source code block is indented, it ends up dedented to the
first column in the resulting .py file.


An example:

--8<----8<----8<----8<-- test.org
-*- org-src-preserve-indentation: t -*-

* Main heading

A bit of introduction

#+begin_src python :tangle yes
class ClassDefinition(IsNotIndented):
    class_variable = 'is correctly indented'
#+end_src

#+begin_src python :tangle yes
    def method_definition(self, is_not_indented_correctly):
        print 'the method body'
        print 'is indented correctly'
#+end_src
--8<----8<----8<----8<-- test.org ends


The resulting file after tangling:

--8<----8<----8<----8<-- test.py

class ClassDefinition(IsNotIndented):
    class_variable = 'is correctly indented'

def method_definition(self, is_not_indented_correctly):
        print 'the method body'
        print 'is indented correctly'
--8<----8<----8<----8<-- test.py ends


Note that the "def method_definition" line should be indented with four spaces.

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

* Re: [babel] [BUG] incorrect indentation when tangling with org-src-preserve-indentation
  2010-10-09 11:09 [BUG] incorrect indentation when tangling with org-src-preserve-indentation Antti Kaihola
@ 2010-10-09 12:22 ` Antti Kaihola
  2010-10-09 15:36   ` Antti Kaihola
  0 siblings, 1 reply; 7+ messages in thread
From: Antti Kaihola @ 2010-10-09 12:22 UTC (permalink / raw)
  To: emacs-orgmode

The following changes fix the indenting problem for first lines of
source code blocks but introduces extra blank lines:


diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index c172756..27936e8 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -58,3 +58,3 @@
     (nth 1 (or processed-params (org-babel-process-params params))) "\n")
-   "\n" (org-babel-trim body) "\n"))
+   "\n" body "\n"))

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index a9429c4..ba6387a 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -365,3 +365,3 @@ form
       (insert (format "%s\n" (replace-regexp-in-string
-                             "^," "" (org-babel-trim body))))
+                             "^," "" body)))
       (when link-p



2010/10/9 Antti Kaihola <akaihola@gmail.com>:
> As pointed out in the documentation, when tangling Python code, it's
> important to set org-src-preserve-indentation to a non-nil value.
> However, tangling still doesn't seem to work correcly: If the first
> line of a source code block is indented, it ends up dedented to the
> first column in the resulting .py file.

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

* Re: [babel] [BUG] incorrect indentation when tangling with org-src-preserve-indentation
  2010-10-09 12:22 ` [babel] " Antti Kaihola
@ 2010-10-09 15:36   ` Antti Kaihola
  2010-10-12 14:54     ` Dan Davison
  0 siblings, 1 reply; 7+ messages in thread
From: Antti Kaihola @ 2010-10-09 15:36 UTC (permalink / raw)
  To: emacs-orgmode

Here's another stab at fixing the problem. This one enhances the
existing trimming algorithm by preserving any leading whitespace on
the first non-blank line of a block. This probably breaks tangling of
other languages than Python, but someone more familiar with the babel
codebase will be able to come up with a saner solution.


diff --git a/lisp/ob-python.el b/lisp/ob-python.el
index c172756..39665c2 100644
--- a/lisp/ob-python.el
+++ b/lisp/ob-python.el
@@ -58,3 +58,3 @@
     (nth 1 (or processed-params (org-babel-process-params params))) "\n")
-   "\n" (org-babel-trim body) "\n"))
+   "\n" (org-babel-indent-safe-trim body) "\n"))

diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 3a0426a..e619e90 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -368,3 +368,3 @@ form
       (insert (format "%s\n" (replace-regexp-in-string
-			      "^," "" (org-babel-trim body))))
+			      "^," "" (org-babel-indent-safe-trim body))))
       (when link-p
diff --git a/lisp/ob.el b/lisp/ob.el
index b05100c..9d750e7 100644
--- a/lisp/ob.el
+++ b/lisp/ob.el
@@ -1723,2 +1723,9 @@ overwritten by specifying a regexp as a second argument."

+(defun org-babel-indent-safe-trim (string)
+  "Strip leading and trailing spaces and carriage returns from STRING.
+Like `org-babel-chomp' only it runs on both the front and back
+of the string. Preserves leading tabs and spaces on first non-blank line."
+  (string-match "\\`\\(?:[ \f\t\n\r\v]*\n\\)?\\(\\(?:\n?.*?\\)*?\\)[
\f\t\n\r\v]*\\'" string)
+  (match-string 1 string))
+
 (defun org-babel-trim (string &optional regexp)

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

* Re: [babel] [BUG] incorrect indentation when tangling with org-src-preserve-indentation
  2010-10-09 15:36   ` Antti Kaihola
@ 2010-10-12 14:54     ` Dan Davison
  2010-10-12 16:40       ` Achim Gratz
  2010-10-15  7:14       ` Antti Kaihola
  0 siblings, 2 replies; 7+ messages in thread
From: Dan Davison @ 2010-10-12 14:54 UTC (permalink / raw)
  Cc: emacs-orgmode

Antti Kaihola <akaihola@gmail.com> writes:

> Here's another stab at fixing the problem. This one enhances the
> existing trimming algorithm by preserving any leading whitespace on
> the first non-blank line of a block. This probably breaks tangling of
> other languages than Python, but someone more familiar with the babel
> codebase will be able to come up with a saner solution.

Hi Antti,

Thanks for the report and patches. I've just pushed a modification of
your patch, along the lines of

-   "\n" (org-babel-trim body) "\n"))
+   "\n" (org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]")) "\n"))

Thus spaces and tabs should no longer be trimmed when
`org-src-preserve-indentation' is non-nil. Hopefully this fixes things.

Dan

>
>
> diff --git a/lisp/ob-python.el b/lisp/ob-python.el
> index c172756..39665c2 100644
> --- a/lisp/ob-python.el
> +++ b/lisp/ob-python.el
> @@ -58,3 +58,3 @@
>      (nth 1 (or processed-params (org-babel-process-params params))) "\n")
> -   "\n" (org-babel-trim body) "\n"))
> +   "\n" (org-babel-indent-safe-trim body) "\n"))
>
> diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
> index 3a0426a..e619e90 100644
> --- a/lisp/ob-tangle.el
> +++ b/lisp/ob-tangle.el
> @@ -368,3 +368,3 @@ form
>        (insert (format "%s\n" (replace-regexp-in-string
> -			      "^," "" (org-babel-trim body))))
> +			      "^," "" (org-babel-indent-safe-trim body))))
>        (when link-p
> diff --git a/lisp/ob.el b/lisp/ob.el
> index b05100c..9d750e7 100644
> --- a/lisp/ob.el
> +++ b/lisp/ob.el
> @@ -1723,2 +1723,9 @@ overwritten by specifying a regexp as a second argument."
>
> +(defun org-babel-indent-safe-trim (string)
> +  "Strip leading and trailing spaces and carriage returns from STRING.
> +Like `org-babel-chomp' only it runs on both the front and back
> +of the string. Preserves leading tabs and spaces on first non-blank line."
> +  (string-match "\\`\\(?:[ \f\t\n\r\v]*\n\\)?\\(\\(?:\n?.*?\\)*?\\)[
> \f\t\n\r\v]*\\'" string)
> +  (match-string 1 string))
> +
>  (defun org-babel-trim (string &optional regexp)
>
> _______________________________________________
> Emacs-orgmode mailing list
> Please 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] 7+ messages in thread

* Re: [babel] [BUG] incorrect indentation when tangling with org-src-preserve-indentation
  2010-10-12 14:54     ` Dan Davison
@ 2010-10-12 16:40       ` Achim Gratz
  2010-10-13  5:03         ` Eric Schulte
  2010-10-15  7:14       ` Antti Kaihola
  1 sibling, 1 reply; 7+ messages in thread
From: Achim Gratz @ 2010-10-12 16:40 UTC (permalink / raw)
  To: emacs-orgmode


This commit produces a warning from the byte compiler on Emacs 23.1:

In org-babel-expand-body:python:
ob-python.el:60:29:Warning: reference to free variable
    `org-src-preserve-indentation'
Wrote /home/gratz/lisp/org-mode/lisp/ob-python.elc


Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

Waldorf MIDI Implementation & additional documentation:
http://Synth.Stromeko.net/Downloads.html#WaldorfDocs

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

* Re: Re: [babel] [BUG] incorrect indentation when tangling with org-src-preserve-indentation
  2010-10-12 16:40       ` Achim Gratz
@ 2010-10-13  5:03         ` Eric Schulte
  0 siblings, 0 replies; 7+ messages in thread
From: Eric Schulte @ 2010-10-13  5:03 UTC (permalink / raw)
  To: Achim Gratz; +Cc: emacs-orgmode

Fixed, Thanks -- Eric

Achim Gratz <Stromeko@nexgo.de> writes:

> This commit produces a warning from the byte compiler on Emacs 23.1:
>
> In org-babel-expand-body:python:
> ob-python.el:60:29:Warning: reference to free variable
>     `org-src-preserve-indentation'
> Wrote /home/gratz/lisp/org-mode/lisp/ob-python.elc
>
>
> Achim.

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

* Re: Re: [babel] [BUG] incorrect indentation when tangling with org-src-preserve-indentation
  2010-10-12 14:54     ` Dan Davison
  2010-10-12 16:40       ` Achim Gratz
@ 2010-10-15  7:14       ` Antti Kaihola
  1 sibling, 0 replies; 7+ messages in thread
From: Antti Kaihola @ 2010-10-15  7:14 UTC (permalink / raw)
  To: Dan Davison; +Cc: emacs-orgmode

2010/10/12 Dan Davison <davison@stats.ox.ac.uk>:
> Thanks for the report and patches. I've just pushed a modification of
> your patch, along the lines of
>
> -   "\n" (org-babel-trim body) "\n"))
> +   "\n" (org-babel-trim body (if org-src-preserve-indentation "[\f\n\r\v]")) "\n"))
>
> Thus spaces and tabs should no longer be trimmed when
> `org-src-preserve-indentation' is non-nil. Hopefully this fixes things.

Yes, your patch along with Eric's fix seems to do the job. Python code
is now tangled correctly. Great!

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

end of thread, other threads:[~2010-10-15  7:14 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-10-09 11:09 [BUG] incorrect indentation when tangling with org-src-preserve-indentation Antti Kaihola
2010-10-09 12:22 ` [babel] " Antti Kaihola
2010-10-09 15:36   ` Antti Kaihola
2010-10-12 14:54     ` Dan Davison
2010-10-12 16:40       ` Achim Gratz
2010-10-13  5:03         ` Eric Schulte
2010-10-15  7:14       ` Antti Kaihola

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