Reflow Line Breaks by External Tool

Random Void User
Posts: 22
Joined: 13.07.2024 21:19

Reflow Line Breaks by External Tool

Post by Random Void User »

Task: select text containing line breaks, convert them to single spaces, replace original selection. Here is a bash alias I use on whole text files.

Code: Select all

$ alias reflow='awk -v RS='\'''\'' -v ORS='\''\n\n'\'' '\''{gsub(/\n/, " "); print; }'\'' '
$ reflow some_file.txt > new_file.txt
I'd like to do the same thing in CudaText on the current selection. Is there a plugin or tool that already does this tweak?

It can also be handy to reverse direction, converting line wrap to hard line breaks at the window boundary. One may adjust the window with a mouse to the desired break width.
main Alexey
Posts: 2245
Joined: 25.08.2021 18:15

Re: Reflow Line Breaks by External Tool

Post by main Alexey »

Yes, plugin CudaExt has 2 commands

1. Join lines
2. Re-wrap/split lines by margin
Random Void User
Posts: 22
Joined: 13.07.2024 21:19

Re: Reflow Line Breaks by External Tool

Post by Random Void User »

I see no option to use the current window boundary. There is a dialog asking for a character count (default 2000) and something about "carets" but it's easier to adjust a window. Maybe the window boundary option could be a future improvement.

I would still like to know how to configure a "tool" with the awk line, to function on the current selection, without writing anything to disk (just using stdin, stdout). I suppose it might take a stand-alone bash script.

Thank you!
main Alexey
Posts: 2245
Joined: 25.08.2021 18:15

Re: Reflow Line Breaks by External Tool

Post by main Alexey »

1.
>no option to use the current window boundary
plugin don't support column of window boundary, you need to enter the char-column. you may turn on 'View / Toggle ruler', then enter the window boundary to the dialog.

>and something about "carets"

CudaExt 'Re-wrap lines' dialog fields:
- 'Margin'
- 'Comment sign' - this is to keep code comments in eg C++
- [x] Keep indent

2.
to run tools via AWK, you need one of plugins:
- External Tools
- Runner
docs: https://wiki.freepascal.org/CudaText_plugins

in the External_Tools: in the tool properties dialog, just enter awk command (or filename of Bash script to call awk), and

- check 'Shell command'
- change 'Capture output' to 'Replace selection'

It helps?
Random Void User
Posts: 22
Joined: 13.07.2024 21:19

Re: Reflow Line Breaks by External Tool

Post by Random Void User »

I tried External_Tools before posting. External_Tools cannot use the current selection as stdin to awk. Perhaps it needs a new setup section for stdin, mirroring the one for stdout, the bottom group of fields in "Tool properties."

Standard input is not a "parameter" like "--flag" or "-f" or "filename.txt"; it needs special options just like stdout. I think I need a missing feature, "Capture selection as stdin" mirroring "Replace selection" with stdout.

These ideas would apply to many external utilities, not just awk. I am only using awk for tests. Thanks!
main Alexey
Posts: 2245
Joined: 25.08.2021 18:15

Re: Reflow Line Breaks by External Tool

Post by main Alexey »

External_Tools author is not active. so you need to try to add the feature by yourself.

Maybe you can add {SelectedTextAsTemp} macro, then
"< {SelectedTextAsTemp}" ?

And try Runner plugin.
Random Void User
Posts: 22
Joined: 13.07.2024 21:19

Re: Reflow Line Breaks by External Tool

Post by Random Void User »

Working code from settings/extttools.json :

Code: Select all

"id": 782608,
"nm": "awktest",
"file": "/usr/bin/bash",
"ddir": "{FileDir}",
"shll": false,
"prms": "-c \"/usr/bin/awk -v RS='' -v ORS='\\n\\n' '{gsub(/\\n/, \\\" \\\"); print; }'  <(echo '{SelectedText}')\"",
"savs": "N",
"rslt": "SEL",
"encd": "",
"lxrs": "",
"pttn": ""
main Alexey
Posts: 2245
Joined: 25.08.2021 18:15

Re: Reflow Line Breaks by External Tool

Post by main Alexey »

<(echo '{SelectedText}')
it replaces the need of new macro {SelectedAsTemp}.
nice solution!
but not sure 'echo .....' works OK when param is multi-line.
Random Void User
Posts: 22
Joined: 13.07.2024 21:19

Re: Reflow Line Breaks by External Tool

Post by Random Void User »

It works for multi-line. The whole purpose was to reflow line breaks. As an experiment it shows a possibility, but is not elegant. {SelectedTextAsStdIn} would be nice, or rather, a full GUI field group as exists for StdOut. I hope future versions of CudaText will have such choices! Thanks.
main Alexey
Posts: 2245
Joined: 25.08.2021 18:15

Re: Reflow Line Breaks by External Tool

Post by main Alexey »

{SelectedTextAsTemp} - to create temp text file with selection - will help. Tool can use it with "<" stdin redirection sign.
I suggest that you make the patch to ExtTools python plugin.
one such macro {.....AsTemp} exists there. make by analogy.
Or you can wait until other ppl make it.
Post Reply