adsr/mle

Question: Paragraph-filling functionality?

mvousden opened this issue · 4 comments

Coming from emacs, I'd quite like a fill-paragraph command, a bit like the one documented at https://www.gnu.org/software/emacs/manual/html_node/emacs/Fill-Commands.html, which redistributes line breaks in a paragraph so that all of the text fits within a certain width.

I don't see anything like this in the source or documentation, so I was wondering if anyone had written something similar as a Lua command? If not, I'm happy to have a go at it, but is there anywhere the Lua API is documented?

adsr commented

Hello. You might approximate something like that by filtering a selection through a shell command such as tr '\n' ' ' | sed -r 's|\s+| |g' | fold -s -w79. You can make a keybinding for that as well if you'd like (-k cmd_shell,...,...).

If you'd like to play around with Lua scripting, take a look at uscript.lua which should give you a rough idea of how to use the API. There is no documentation but it's auto-generated from the C API declared in mle.h and mlbuf.h. That said I may remove Lua scripting from a future version of the editor. I'm not fully convinced it belongs.

Another alternative is to write a built-in command which I'd merge if it's generally useful / not too specific.

I added

WRAP_LENGTH=80
[...]
echo "-kcmd_shell,M-q,tr '\n\t' ' ' | fmt --width=$((WRAP_LENGTH - 1)) | sed -r 's/\s+$//;s/\s+/ /g'"

to my ~./mlerc, which works a treat, given I select the region to fill beforehand.

Problem is, I'd like to be able to run fill-command from anywhere in the paragraph, and have it wrap immediately. This would require me to do some "pre-anchoring" to detect where the paragraph starts and ends, which can't be done using the shell interface on its own (because it just reads stdin).

A naive approach for this would be something like:

  1. Position cursor on the first blank line above the line the cursor is currently on.
  2. Move the cursor forward by one.
  3. Start anchor.
  4. Position cursor on the first blank line below the line the cursor is currently on.
  5. Move the cursor backward by one.
  6. Run fill-paragraph

So I'll play with the Lua API and see how I get on. Could also write this as part of the editor source instead, but I don't want to bloat your source with my prattle.

That said I may remove Lua scripting from a future version of the editor. I'm not fully convinced it belongs.

That's rather unfortunate - it's one of the reasons I started here in the first place. Fair enough though.

Edit: And thanks for your response/help!

adsr commented

Closing some stale tickets. Feel free to reopen.

Curious to hear if you got anywhere with the user script. People seem to like Lua extensibility so I've decided to keep it around for now by the way.

Closing some stale tickets. Feel free to reopen.

Curious to hear if you got anywhere with the user script. People seem to like Lua extensibility so I've decided to keep it around for now by the way.

Good to know - I never actually got around to writing the script for it sadly. Maybe one day...