nobiot / org-transclusion

Emacs package to enable transclusion with Org Mode
https://nobiot.github.io/org-transclusion/
GNU General Public License v3.0
926 stars 47 forks source link

Indentation of some lines of transcluded python src block is modified #203

Open sebmiq opened 11 months ago

sebmiq commented 11 months ago

Hi,

I have an in.org file, whose content is

* Crible d’Ératosthène

#+BEGIN_SRC python
def crible(n):
    l = [True] * (n+1)
    l[0], l[1] = False, False
    i = 2
    while i*i <= n:
        if l[i]:
            k = 2
            while i*k <= n:
                l[i*k] = False
                k += 1
        i += 1
    return l
#+END_SRC

and an out.org file, whose content is

#+transclude: [[file:in.org::*Crible d’Ératosthène][Crible d’Ératosthène]]

When I activate the transclusion, it shows

* Crible d’Ératosthène

#+begin_src python 
def crible(n):
    l = [True] * (n+1)
    l[0], l[1] = False, False
    i = 2
    while i*i <= n:
        if l[i]:
            k = 2
            while i*k <= n:
                l[i*k] = False
                k += 1
                i += 1
    return l
#+end_src

Note that the i += 1 line had its indentation modified.

sebmiq commented 11 months ago

The issue is that org-transclusion-content-format-org calls org-indent-region, which, when org-src-tab-acts-natively is t, calls python-indent-region, which indents as shown.

I'm not sure what part is wrong, but this can be fixed in org-transclusion-content-format-org by setting org-src-tab-acts-natively to nil.

nobiot commented 11 months ago

Thank you for the report and the investigation. I am not sure what the best way to support this within the code base. The source code notes this:

;; Fix indentation when `org-adapt-indentation' is non-nil org-transclusion-content-format-org

I don't think I can support every problem case, and this is my main reason to have the flexible mechanism to call org-transclusion-content-format-org as an abnormal hook.

(defvar org-transclusion-content-format-functions
  '(org-transclusion-content-format-org
    org-transclusion-content-format))

My immediate suggestion for you is to either override the function org-transclusion-content-format-org or define a custom function and set the variable org-transclusion-content-format-functions so that your custom function gets called.

I will be in a vacation from the end of this week for five weeks or so; I will not be able to provide an immmediate fix. I trust that with your skill you will be able to implement your own solution for now.

I will need to come back to the issue of indentation at a later stage -- at the moment, I don't see a good way to support the Org-src's indentation behavior in every case... I will be very happy if you or anyone can find a good solution.

Thank you.