Stripped trailing blanks

95 views
Skip to first unread message

Rob

unread,
May 23, 2021, 3:50:16 PM5/23/21
to leo-editor
Before I create an issue on GitHub, I thought I would ask here first to see if the outcome is as intended or is a bug.

I use the 'Extract' command frequently to create child nodes from selected text (Edit->Edit Text->Create Sections->Extract). I find it annoying that the command strips out all trailing blank lines.

The same thing happens with a script I use to capture a node and its children and copy to the clipboard (perhaps the code is related, I don't know). The script begins:

~~~
s = g.getScript(c,p,useSentinels=False,forcePythonSentinels=False)
g.app.gui.replaceClipboardWith(s)
~~~

The result of both the Extract command and the script is the same; all trailing blank lines are stripped out.

Is this by design? If so, what is the rationale?

Steve Litt

unread,
May 23, 2021, 4:18:33 PM5/23/21
to leo-e...@googlegroups.com
Rob said on Sun, 23 May 2021 12:50:15 -0700 (PDT)
I lack the knowledge to speak for Leo, but in my travels I've found
trailing whitespace to be nothing more than an invitation to bugs and
problems.

What meaning could possibly be assigned to trailing whitespace? What
purpose could trailing whitespace have? Wouldn't there be a
better way of signifying whatever is intended than trailing
whitespace you can't visually detect? Note that Python's string
strip() and rstrip() both remove trailing whitespace, including the
newline. This gets the string in a known state, from which it can be
processed, and then, if desired, the newline can be added back in.

Apropos to nothing but a strong opinion I have, what's worse than
trailing whitespace is the mixing of spaces and tabs. Because
there's no visual way to tell the difference, and the games Vim
and other editors play in converting between the two, mixing spaces and
tabs is a horror movie just waiting to happen.

SteveT

Steve Litt
Spring 2021 featured book: Troubleshooting Techniques of the Successful
Technologist http://www.troubleshooters.com/techniques

Rob

unread,
May 23, 2021, 4:27:30 PM5/23/21
to leo-editor
Thanks for the reply. 

What meaning could possibly be assigned to trailing whitespace? What
purpose could trailing whitespace have?

The trailing white space serves a very specific purpose for me. I'm not writing computer code, I'm writing documents. After running my script, I have to manually go in and add back the blank lines between sections (paragraphs, etc.)
 
Wouldn't there be a
better way of signifying whatever is intended than trailing
whitespace you can't visually detect?

The whitespace is clearly visible by its absence after it's been stripped out. Perhaps for coding, that's desirable (don't know as I don't code much), for documents, I need white space between paragraphs and sections, especially how I'm using it, needing visual separation for easier reading while giving a presentation.
 
Apropos to nothing but a strong opinion I have, what's worse than
trailing whitespace is the mixing of spaces and tabs. Because
there's no visual way to tell the difference, and the games Vim
and other editors play in converting between the two, mixing spaces and
tabs is a horror movie just waiting to happen.

I agree with this and don't use tabs, just spaces.

Rob...
 

Robert-Felix

unread,
May 23, 2021, 4:34:19 PM5/23/21
to leo-e...@googlegroups.com
Thanks for this Rob. Never noticed this before :)

I tried, and it's true that extract (a common command I also use often) seems to remove trailing (white) lines. (tested with trailing empty lines and some with space characters in them. they were all removed. (never really noticed this before because I lazily select what I want from the top and dont care where i stop after the chunk of code in many empty lines below it.) 

But in cases where people are writing text that is not code, or code that wants some empty lines here and there for some reason, this can be annoying. I propose that there should be an option to turn off the cleanup of empty trailing lines when using the extract command, so that selected empty lines are part of the cold node being created.

(Steve please notice Rob is talking about trailing empty lines. not trailing whitespace at the end of lines containing some text.)

Thanks again - and yeah. maybe fill an issue on github for that :)

--
Félix



--
You received this message because you are subscribed to the Google Groups "leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email to leo-editor+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/leo-editor/eb666ced-e825-4787-98b4-10a6535d0d13n%40googlegroups.com.

Félix

unread,
May 23, 2021, 4:37:45 PM5/23/21
to leo-editor
(hehe... typo on 'cold' node wording, just meant 'code' node)

Rob

unread,
May 23, 2021, 4:42:54 PM5/23/21
to leo-editor
Based on the feedback, it sounds like the result is intentional, so I'll file it as an enhancement, not a bug. That's what I wanted to know.

I also wonder if the script I use might be modified in some way to 'not' strip blank lines, i.e., copy all 'as is', making no modifications. That would be the best fix for me in my use case.

I got the original script code from Vitalije as I couldn't have done that on my own.

Rob...

tbp1...@gmail.com

unread,
May 23, 2021, 5:02:02 PM5/23/21
to leo-editor
You could collect the body text yourself,  It's not hard. For a selected node, you can find the entire subtree like this:

p = c.p
tree = [p]
tree.extend(p.subtree()) # tree includes entire tree starting at selected node

You can collect all the text from these nodes easily as long as you don't need to indent them according to their child level:

collected = ''
collected = [n.b for n in tree]
collected_text = '\n'.join(collected)

This won't work if you are using @others or << named sections>>.  It will also collect all Leo @-directives, so if you use them you would have to filter them out.  But as long as you only need the collected body text, this code should do the job.

Steve Litt

unread,
May 23, 2021, 6:18:48 PM5/23/21
to leo-e...@googlegroups.com
Rob said on Sun, 23 May 2021 13:27:30 -0700 (PDT)

>Thanks for the reply.
>
>What meaning could possibly be assigned to trailing whitespace? What
>> purpose could trailing whitespace have?
>
>
>The trailing white space serves a very specific purpose for me. I'm
>not writing computer code, I'm writing documents. After running my
>script, I have to manually go in and add back the blank lines between
>sections (paragraphs, etc.)

Ooohhh, you meant blank lines. I thought "blank" meant the space
character. I agree with you that any software stripping out blank lines
would make my life a lot harder.

I don't know whether you mean it strips out blank lines within body
text, or whether it fails to insert a blank line between headlines.
Either way, you could end the line that should be followed by a blank
with an easily parsed and never-used string, and then run a simple
script that, on the back end, converts that string to a newline. In
Linux I'd use AWK to do that, in Windows I'd use Python. It's simple. I
can write such a script for you if you tell me the string.

Rob

unread,
May 23, 2021, 9:26:24 PM5/23/21
to leo-editor
Ooohhh, you meant blank lines. I thought "blank" meant the space
character. I agree with you that any software stripping out blank lines
would make my life a lot harder.

I don't know whether you mean it strips out blank lines within body
text, or whether it fails to insert a blank line between headlines.

It strips out blank lines at the end of the node, not within body text.
 
Either way, you could end the line that should be followed by a blank
with an easily parsed and never-used string, and then run a simple
script that, on the back end, converts that string to a newline. In
Linux I'd use AWK to do that, in Windows I'd use Python. It's simple. I
can write such a script for you if you tell me the string.


That seems too complicated and unnecessary. I'd prefer to capture the exact text (blank lines, white space and all).

Rob... 

Rob

unread,
May 23, 2021, 9:35:37 PM5/23/21
to leo-editor
I do use @others and occasionally << named sections >> (though rarely).

Also, where does this 'collected' text go? How do I copy into another application (which is the whole point).

Rob

unread,
May 23, 2021, 9:52:58 PM5/23/21
to leo-editor
Never mind, just plugged that into the clipboard code I already had and it works, even w/ @others and << named sections >>.

I'll have to play around w/ it a bit more, but that should work for me, though it doesn't address the 'Extract' command directly.

Rob...

Steve Litt

unread,
May 24, 2021, 12:05:53 AM5/24/21
to leo-e...@googlegroups.com
Rob said on Sun, 23 May 2021 18:52:58 -0700 (PDT)
So is your problem with blank lines solved?

Thanks,

Rob

unread,
May 24, 2021, 7:20:08 AM5/24/21
to leo-editor

So is your problem with blank lines solved?

Mostly, yes. The revised script gives me what I need when collecting text from a tree. It doesn't fix the Extract command preference of *not* stripping blank lines, which isn't as big a deal for me as the former.

Rob...

Edward K. Ream

unread,
May 24, 2021, 8:49:49 AM5/24/21
to leo-editor
On Sun, May 23, 2021 at 2:50 PM Rob <lar...@gmail.com> wrote:

The result of both the Extract command and [g.getScript] is the same; all trailing blank lines are stripped out.

Is this by design? If so, what is the rationale?

These are two very different questions:

- For Extract, the present behavior comes from the call to rstrip in the createLastChildNode helper function.

I have just created #1955 for this, and fixed it via PR #1956. The new code is in the ekr-extract branch. Please give it a look. I'll merge ekr-extract into devel tomorrow

Notes:

1: #1742 and PR #1745 (concerning LeoBody.getSelectionLines) is not involved, apparently.

2: The new code caused several unit tests to fail. Happily, the fix was to remove a strange hack (@nonl) in a headline of the tests.

- For g.getScript, the present behavior arise from at.stringToString, which can not (will not :-) be changed.

Edward

Rob

unread,
May 24, 2021, 6:35:42 PM5/24/21
to leo-editor


I have just created #1955 for this, and fixed it via PR #1956. The new code is in the ekr-extract branch. Please give it a look. I'll merge ekr-extract into devel tomorrow

Initial look suggests this works as expected; extra blank lines are preserved. Thanks and sorry I didn't get to posting the GitHub issue yet; pretty crazy week!
 

Notes:

1: #1742 and PR #1745 (concerning LeoBody.getSelectionLines) is not involved, apparently.

2: The new code caused several unit tests to fail. Happily, the fix was to remove a strange hack (@nonl) in a headline of the tests.

- For g.getScript, the present behavior arise from at.stringToString, which can not (will not :-) be changed.


No big deal there as Thomas' suggested script changes work for me now.

Rob...
 
Reply all
Reply to author
Forward
0 new messages