Lines containing only whitespace - a continuing annoyance

Is there some way to avoid this very frequent occurrence? When I am editing tiddlers I often add what looks like a blank line but is in fact a “blank” line with some embedded white space. As a result, as I understand it, TiddlyWiki does not perform the WikiText parser mode transition that I expect.

For me this happens all the time. I am constantly editing tiddlers and, almost invariably, when intending to add a new block, end up separating them with “\n \n” - two newlines as required but with some intervening whitespace. I tend to not have the preview pane visible, although even when I do I have to manually remove the excess whitespace once I notice the preview does not match my intentions.

One solution might be that, as a tiddler is saved, all trailing whitespace is removed from lines. In Emacs I’ve used ws-butler for so long I never think about this; other IDEs have similar settings. Another possible solution would be a way to change TiddlyWiki’s definition of “blank line” as applied to tiddler content from ‘2 consecutive newlines’ to ‘newline any amount of whitespace newline’.

Is my use case that unique, do others not encounter this? Am I just missing some setting? Is there a common practical use for lines that appear to be blank to be treated as lines with content?

If I am alone, is there some something I can implement - maybe using the HookMechanism - to simply go through a tiddler as it is saved and replace any lines with whitespace only with an actually blank line? I am almost certain I have no use for them.

1 Like

Never. I’m not even sure I understand your issue. Is it like this?

[blank]\n
[space(s)]\n
[blank]\n

?

Yes, there is a save-tiddler hook, that can be used by plugins.

There are 2 possibilities which have to be considered.

  1. space new-line
  2. content space new-line

IMO 1) is what you describe. … It should be easy to remove them, when saving a tiddler.

  1. is more complex if we have a look at the whole TW ecosystem.

We do have many users, which want to be able to transfer content from and to Markdown systems, where space-space-new-line can be the syntax for a “hard linebreak

I did create a plugin with the “creative” name: space-space-linebreak that allows TW syntax to do the same thing. (may be I should rename that plugin)

So removing whitespace at the end of lines, that also contain content may be a “no go” for some users. Especially if they have space-space-newline plugin installed.

From time to time I would have wanted to have that functionality too.

But …

That’s a “very destructive” behaviour if done blindly. … In TW wikitext whitespace matters, except if there is the \whitespace trim option, which is “rendering only”

I think I’ll create a plugin, that creates a new edit-button which allows us to trigger the “trim trailing whitespace” from a selection. … If nothing is selected, the whole text will be trimmed.

This will allow us to trim whitespace from “empty lines”. May be with an option to also trim it from “content lines”. …

If done “on save” there are many edge-cases to be considered. Using an explicit user trigger will remove all those concerns.

There are some tm-edit-text-operation messages. IMO there should be some “trim-selection” commands too.

I’ll create a plugin soon.

have fun!
mario

How would a “trim icon” look like?

May be grafik or so

1 Like

A POC looks as follows.

modes: leading, trailing, full, white-line

default: white-line, which is the behaviour in the screenshot.

leading … will remove whitespace from the start of the line
trailing … will remove whitespace from the end of the line
full … will remove trailing whitespace
white-line … will only remove whitespace from “empty” looking lines.

What do you think?

What I invariably create is

… some content, possibly ending in space(s)\n
[space(s)]\n
more content …

As for a practical reason, yes, markdown formatting would be one. In Tiddly Wikitext I am surprised this does not occur for others often enough to be a frequent request. But then, I’ve been putting up with it for decades and maybe results from my way of using TW. I tend to edit tiddlers a lot. Blank lines with embedded whitespace result when an addition causes the need for a new paragraph or a bullet list or some new block.

Lines with content ending in spaces like the top one above don’t much bother me. (Although in Emacs I activate whitespace-mode to make them at least visually apparent and do similarly in IDEs, e.g. vscode as a way to syntax highlight occurrences and avoid them that way too.)

I can pretty much guarantee that I will forget to routinely click a button to clean up, so a save-tiddler hook would be ideal. Thanks for that clue.

I’m thinking about it. It’s very destructive to auto remove whitespace with a global setting. I think it can cause more problems than advantages.

Since toolbar buttons can execute actions it’s possible to do this by default. This only works with a user created selection and needs 3 shortcuts to trim and save a tiddler. It’s a fine grained control and it’s “safe”

  • CTRL A … select all
  • CTRL Q … trim whitespace if shortcuts are defined
  • CTRL Enter … Save and close tiddler

Important: The following code needs a plugin, that’s not available … yet.

<!-- modes: leading, trailing, full, white-line -->
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="trim-whitespace"
	mode={{{ [{$:/config/trim-whitespace/mode}trim[]!is[blank]] ~[[white-line]] }}}
/>

but users can clone the default button and make it their own, as follows.

  • 1st new $param=“select-all” … dynamically selects all text
  • trim-whitespace will trim the white space
  • tm-save-tiddler will save the tiddler

So if the shortcut is eg: CTRL-Enter, it should do everything with 1 click. So users can be “as destrcurtive” as they want. It’s their decision :wink:

<$action-sendmessage	$message="tm-edit-text-operation" $param="select-all"/>

<!-- modes: leading, trailing, full, white-line -->
<$action-sendmessage
	$message="tm-edit-text-operation"
	$param="trim-whitespace"
	mode={{{ [{$:/config/trim-whitespace/mode}trim[]!is[blank]] ~[[white-line]] }}}
/>

<$action-sendmessage $message="tm-save-tiddler" $param=<<targetTiddler>>/>

Is this editortoolbar button created by @etardiff of any use in your case

This is somewhat off topic, or is it?

Dot paragraph (yes @pmario)

  • I already have a dot paragraph symbol, that you select all lines and click, rather than visually appending lines separated by one blank line, it generates a “paragraph” for each set of sentences until the first \n.
  • Multiple blank lines are reduced to one blank line between paragraphs even with the aforementioned whitespace in them, this is true for simple wikitext of course.
  • However now every line begins with a · you can apply a class ·.classname or transition directly to other wikitext like;
·
·
·
;Details
:note
·
* list items

And all wiki text works, without a proceeding blank line.

  • I also added · to the autolist plugin so enter adds · to each new line until \n\n

Perhaps someone reading this thread can help with this Refine the editor toolbar bullet buttons to avoid blank lines?

Here’s the intro of my new Trim Whitespace plugin.

have fun!
mario

2 Likes

Thanks, I’ll take a look

Yes, thank you, this does what it advertises. As it requires a 3 step process - select the tiddler content, remove the configured kind of whitespace, and close/save the tiddler - I’ll probably end up cloning / customizing it as suggested in the write up and maybe take stab at adapting the save-tiddler hook when I get a chance.

A scan of my tiddler collection tells me I’ve perpetrated tiddlers containing lines with only white space 61 times (in a ~500 tiddler collection) – so you can see I really do do this a lot – where in those cases I have not noticed (yet) if they are causing any Wikitext parsing impact.

A similar scan[1] informs me I wouldn’t want to remove leading whitespace: I’ve included a lot of code snippets in my tiddler collection over the years – embedded in quoted text delimiters – documenting various lessons observed that I wouldn’t want to ‘trim’ generally significant leading whitespace from. So I will likely adapt a variation using whitespace only and trailing whitespace lines for my use case.

Thanks again!
[1] Tip: the ripgrep tool, aka rg, is really handy for this kind of quick scan of lots of files in a directory tree job.