Hi, @mpheath and All,
@mpheath, from your advice, I finally chose the following process :
First, run a Edit > Blank Operations > TAB to Space option
Then run the regex replacement below, where C represents the maximum number of chars after you want a line-break :
SEARCH [\x20\r\n]*\z|(?-s)^(?=.{C+1,}).{1,C}\K(?:\x20*\R|\x20+)
REPLACE \r\n for Windows files or \n for Unix files
Finally, run a Edit > Blank operations > Trim Trailing Space option
Remarks :
I began the regex with [\x20\r\n]*\z which will replace possible mixed range of spaces and new lines, at the very end of current file, with a single line-ending
I used the (?-s)^(?=.{C+1,}) ( instead of the (?-s)^(?=.{C+1}) as it seems slightly faster within huge files !
Compare to your regex, I still wanted to consider all spaces, possibly followed with a line-break. So, the part .{1,C}\K(?:\x20*\R|\x20+)
Note that the final Trim Trailing Space action is necessary because of this specific case with a splitting after column 40
12345678901234567890123456789012345 ABCDE
12345678901234567890123456789012345
ABCDE
Now, let’s try it out with a concrete example :
Double-click on https://www.gutenberg.org/cache/epub/2701/pg2701.txt => The text of the novel Moby Dick or The Whale should appear
Go to the second occurrence of the string CHAPTER 1. Loomings.
Select from this line to the line END OF THE PROJECT GUTENBERG EBOOK MOBY DICK; OR, THE WHALE, excluded
Paste it in a N++ new tab and name it Moby_ORG.txt
So, the Moby.ORG.txt file should begin and end with :
Call me Ishmael. Some years ago—never mind how long precisely—having
little or no money in my purse, and nothing particular to interest me
on shore, I thought I would sail about a little and see the watery part
of the world. It is a way I have of driving off the spleen and
regulating the circulation. Whenever I find myself growing grim about
...
...
...
...
unharming sharks, they glided by as if with padlocks on their mouths;
the savage sea-hawks sailed with sheathed beaks. On the second day, a
sail drew near, nearer, and picked me up at last. It was the
devious-cruising Rachel, that in her retracing search after her missing
children, only found another orphan.
In other words :
Line 1 EMPTY
Line 2 FIRST line of text
Line 20,707 LAST line of text
Line 20,708 = \z
As this file seems already wrapped to 71 chars, I decided to do, FIRST, an intelligent un-wrapping !
I verified that there was no tabulation, nor any trailing space in the Moby_ORG.txt file
Then I noticed that 43 lines began with one or two space characters, without any real utility. So, I ran the Edit > Blanck operations > Trim Trailing Space option
To get a correct un-wrapping process, we need to use the following regex S/R :
SEARCH (?-s).+\K\R(?=.+) ( Do not insert a ^ character between the parts (?-s) and .+ ) of this regex ! )
REPLACE \x20
Finally, I saved the changes and renamed the Moby_ORG.txt as Moby_To_Wrap.txt
So, the Moby_To_Wrap.txt file begins and ends as below :
Call me Ishmael. Some years ago—never mind how long precisely—having little ...
There now is your insular city of the Manhattoes, belted round by wharves as ...
Circumambulate the city of a dreamy Sabbath afternoon. Go from Corlears Hook ...
...
...
...
...
“AND I ONLY AM ESCAPED ALONE TO TELL THEE” Job.
The drama’s done. Why then here does any one step forth?—Because one ...
It so chanced, that after the Parsee’s disappearance, I was he whom ...
In other words :
Line 1 EMPTY
Line 2 FIRST line of text
Line 4,884 LAST line of text
Line 4,885 = \z
The second line of your script is always :
local maxLineLength = 78
And the Settings > Preferences... Margins/Border/Edge > Vertival Edge Settings only contains the 78 value
Then I copied the Moby_ORG.txt file three times, as Moby_Regex.txt, Moby_Lua.txt and Moby_Ctrl-I.txt and I applied :
Your script against the Moby_Lua.txt file,
The Edit > Line Operations > Slipt Lines option against the Moby_Ctrl-I.txt
The following regex S/R against the Moby_Regex.txt
SEARCH [\x20\r\n]*\z|(?-s)^(?=.{79,}).{1,78}\K(?:\x20*\R|\x20+)
REPLACE \r\n
Upon comparison, it turns out that these three files are completely different ! Compared to the Moby_Regex.txt file , I noticed that your Moby_Lua.txt sometimes grasps :
More than it should, as, for example, in the lines :
“Who-e debel you?”—he at last said—“you no speak-e, dam-me, I kill-e.” And so saying with a wrapping after 84 chars
[image: 1793374535176-a1299916-7d59-4c0b-a524-c5edf237ba4e-image.jpeg]
“Oh, I don’t mind ’em, sir,” said Archy; “I knew it all before now. Didn’t I hear ’em with a wrapping after 85 chars
[image: 1793374577115-f376a0b5-7d95-4f82-80f8-54c71394606e-image.jpeg]
Less than it should, as, for example, in lines :
the Stoics to enable you to grin and bear it. But even this wears off in, with a wrapping after 72 chars
[image: 1793374617791-130645ce-362a-43a6-bf62-c511f338cdac-image.jpeg]
knowingly, would thereby render yourself liable to a criminal, with a wrapping after 61 chars
[image: 1793374649953-eb346fc2-0eb6-445c-bc6f-1d4ef581b4e9-image.jpeg]
Not important, though. Just in case you would work on your script a little more !
Now, using this long text ( the Moby_To Rewrap.txt file ), I confirm that the regex, below, exactly mimics the Ctrl + I feature :
SEARCH (?-s)^(?=.{78})(?:.{1,76}\x20|.{77})\K
REPLACE \r\n for Windows files or \n for Unix files
However, note that the Edit > Line Operations > Split lines operation leaves a trailing space on all lines of a paragrah but the last one !
I think that further investigation would be useless and I’ll personally just use the method, shown at the beginning of this post, when I need to wrap any text !
Best Regards,
guy038
Oh, I almost forgot something important :
When you select some text or all contents of a file and execute the Edit > Line Operations > Split Lines option :
The View > Word wrap must be OFF
The View > Show Symbol must be OFF