Improve autowrap paragraph formatting#5392
Conversation
I dedicate any and all copyright interest in this software to the public domain. I make this dedication for the benefit of the public at large and to the detriment of my heirs and successors. I intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
Previously, typing at the end of a line in the middle of an existing paragraph would result in an empty new line being created in the middle of the paragraph. We fix this by ensuring that the fmtcmd is run even if the wrapping takes place on this character. The hook will no longer be run on insersion of a space. This is to ensure that a wrapping space does not create an empty line and prevent the paragraph formatter from working. kak -e 'autowrap-enable; set-option window autowrap_format_paragraph yes'
mawww
left a comment
There was a problem hiding this comment.
I do not quite remember why autowrap does not run the command if we are at the end of the line, I suppose it is for performance reasons but that seems like a weak argument for me as this needs to be fast enough to be unnoticeable when editing the middle of a paragraph anyway.
I may have missed something, but isn´t the try block now redundant with whatever the format command will do afterwards ? If we move towards always running the format command we may remove the whole manual wrapping logic as well.
Anybody familiar with this script wants to chime in ?
rc/tools/autowrap.kak
Outdated
There was a problem hiding this comment.
This comment seems out of date now, AFAIU this change runs the format command regardless of if we are adding character at the end or in the middle of the sentence as we do not only run in the case where the previous execute-keys failed anymore.
|
The try block is necessary to move the cursor onto the next line. If, when inserting characters past the autowrap limit, we only ran the format command then the cursor would remain on the first line when the text you just inserted moves to the next line. We might be able to simplify the logic a bit with something like: 1. record the column number you are on (call this n) 2. run the formatting command. 3. move n characters into the block of text you just ran the format command on. This way the cursor would (hopefully) be in the right place even when going onto a newline. I'll have a play with this idea and see if I can make it work nicely. |
|
The fundamental problem with using autoformat commands such as I've been trying out the following. When the user inserts a character we 1. replace it with a rare unicode symbol (∅ in this case) 2. run The obvious downside of this is that it will break if you actually want to use this unicode character in your paragraph. Also it means when autowrap is enabled your selection will be reduced to a single character wide when you insert text. But apart from that I have found it to be quite robust. Any thoughts on this idea? I will try using this for a while and see how it goes. |
Hum, but that try block just contains an
We could work-around this by extending the current piping diff logic to work on characters instead of lines which should detect that we inserted newlines and correctly preserve selections. This might however prove quite expensive, but may be something to explore. |
|
Thats a great idea, I think extending the piping diff logic is the way to go if turns out to be fast enough. |
To demonstrate my problem with the existing
autowrap_format_paragraphoption, try the following, bring a cursor to the end of one of the middle lines and type some text until it wraps. You will notice that a new line is created without running theautowrap_fmtcmdon the remainder of the text. The behaviour I expect is similar to what happens if you were to start typing in the middle of one of the lines.I do not see why the try catch was used in the initial implementation.