Skip to content

feat: Remove blank lines at end of transcluded content.#292

Draft
AndreasMatthias wants to merge 5 commits intonobiot:mainfrom
AndreasMatthias:fix/blank-lines
Draft

feat: Remove blank lines at end of transcluded content.#292
AndreasMatthias wants to merge 5 commits intonobiot:mainfrom
AndreasMatthias:fix/blank-lines

Conversation

@AndreasMatthias
Copy link
Copy Markdown
Contributor

This PR removes blank lines at the end of transcluded content.

For example

-----
#+transclude: [[./test.el]]  :src emacs-lisp
-----
#+transclude: [[./test.org]]
-----

will become after transclusion

-----
#+begin_src emacs-lisp
(message "foo")
#+end_src
-----
another test
-----

Note that there are no blank lines at the end of the transcluded content although
the original files (see below) have blank lines at their ends.

File test.org:

another test


File test.el:

(message "foo")


@AndreasMatthias AndreasMatthias marked this pull request as draft February 5, 2026 11:18
@AndreasMatthias
Copy link
Copy Markdown
Contributor Author

Sorry. I guess this is not the right way to fix it. And it won't work for live-sync too.

@AndreasMatthias
Copy link
Copy Markdown
Contributor Author

This is much harder than I tought it would be. I close it for now.

@nobiot
Copy link
Copy Markdown
Owner

nobiot commented Feb 10, 2026

@AndreasMatthias , I am not sure if I can reproduce the issue, but what exactly are you trying to "fix"? Maybe it's a matter for you to have require-final-newline t and control its values with .dir-locals.el.

I don't know if I have seen an issue raised and we talked about it, but perhaps you can try to create a new issue and describe it with repro procedure.

@AndreasMatthias
Copy link
Copy Markdown
Contributor Author

I'm sorry for the word "fix". I called my branch "fix" and thus used "fix" in the PR. But actually it was meant to be a proposal:

I prefer to put one (only one) blank line between paragraphs. And I do the same for transcluded contents, i.e I prefer to put one blank line between the transcluded content and the other content of my org file.

But sometimes the transcluded content itself already contains one or more blank lines at its end which leads to several blank lines in between. So I tried to remove the additional blank lines in the transcluded content.

I have a much better solution now, working with live-sync as well, but it still breaks sometimes. I'll get back to you once I've sorted things out.

@nobiot
Copy link
Copy Markdown
Owner

nobiot commented Feb 11, 2026

No worries about the word "fix". I was not implying any my emotional state (I had none 😄 ). I was simply saying that I do not seem to be able to reproduce the "issue", and without one I could not think of a "fix"...

But sometimes the transcluded content itself already contains one or more blank lines at its end which leads to several blank lines in between [my emphasis].

I would like to explore the conditions for this "sometimes". Do you have any ideas? In my case it appears the transcluded contents never contain extra blank lines at their end (when the original does not contain them) -- so they are always the same.

Are you saying that you want to have extra blank lines in your original but want to trim them when transcluded? If so, then it makes sense that live edit does not work because the original and copy do not match exactly.

@AndreasMatthias
Copy link
Copy Markdown
Contributor Author

Yes excatly. I don't want the extra blank lines of the original file be transcluded.
Here's an example.

This is file.el. (Note the blank lines at the end of the file.)

(message "hello")



Now I transclude this file with:

first paragraph

#+transclude: [[./file.el]]

last paragraph

What I'd expect is:

first paragraph

(message "hello")

last paragraph

But the actual result is this:

first paragraph

(message "hello")




last paragraph

@nobiot
Copy link
Copy Markdown
Owner

nobiot commented Feb 12, 2026

In my case, I have whitespace-cleanup in before-save-hook so I don't have files with extra blanks lines. If you have control over all your files, perhaps this may be something you might also prefer.

If you need to work with files with extra blank lines, because you don't have full control over these files...or you'd simply prefer to have extra blank lines anyway in the original files...

I would look into the following two methods within the framework of Org-transclusion. If my design is correct and I implemented it correctly, they should not affect live sync edit (live sync would still include the full content of the file including the extra blank lines).

I may prefer Method 2 (at least that's what I'd try first), because Method 1 may cause discrepancy between the content and the overlay's range.... But in either methods, more testing should be done.

I assumed you only needed to cater to text and program source files. Org files have a different set of content and content-format functions, so I didn't consider them (see variable org-transclusion-content-format-functions).

Just a thought from me.

Method 1. Modify the content in the payload

For text files (except for program source)
In org-transclusion-add-other-file
:src-content (buffer-string)

For program source:
org-transclusion-content-src-lines

Method 2. Modify the content's format when content is inserted

For text files (except for program source)
org-transclusion-content-format

For program source:
org-transclusion-content-format-src-lines

During transclusion:
  Blank lines at the end of the contents are not transcluded.

During live-edit:
  Blank lines added by the user at the end of the contents are removed.
@AndreasMatthias
Copy link
Copy Markdown
Contributor Author

I guess this could work. However be careful, this commit is not tested thoroughly.

This commit ignores/removes blank lines

  • During transclusion:
    Blank lines at the end of the contents are ignored (not transcluded).

  • During live-edit:
    Blank lines added by the user at the end of the contents are removed.

File tmp/blank-lines.org contains a few example showing the new functionality.
Live-edit should work successfully in these examples as well.

In org-transclusion--live-sync-inside-edit-p() I used the 'face property of
the overlay to find the end of the edit region. This is kind of hacky, but
I couldn't find a better way.

@AndreasMatthias AndreasMatthias changed the title fix: Remove blank lines at end of transcluded content. feat: Remove blank lines at end of transcluded content. Feb 12, 2026
@AndreasMatthias
Copy link
Copy Markdown
Contributor Author

Reported some time ago: #221

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants