[racket-dev] Wrapping lines

From: Eli Barzilay (eli at barzilay.org)
Date: Sun May 6 08:49:19 EDT 2012

Just now, Robby Findler wrote:
> Would it be possible to base a meta-q like binding on this and be
> able to use it when editing scribble files in DrRacket? (That would
> be totally awesome if it were!)

Ah, I didn't think about it...  Yes, it should be very easy to do
that -- you just need to decide which style to use...  For example, if
you want to do the thing that auctex is doing:

   |blah blah blah blah @foo{blah blah blah blah blah
   |  blah blah blah blah blah blah blah blah blah
   |  blah blah.}

Then the work that you need to do is get the string that is to be
wrapped, and you need two numbers -- the width of the first line and
the width of the rest.  In the above case I'm using a width of 50
(after the "|" prefix for email formatting), which leaves me with 25
characters for the first line, and 48 for the rest (50 minus 2 for the
indentation).  So now you use

    (wrap-line text '(25 . 48))

and you get back a list of lines which is now easy to slap together
with the indentation:

    (string-append* (add-between (wrap-line text '(25 . 48)) "\n  "))

And that's about it.

Some notes:

* You can also do reflowing of semicolon comments in a similar way:
  - find the consistent prefix using #rx"^ *;+ *"
  - combine the following strings to one line
  - wrap with the width minus the prefix width
  - recombine with the prefix and newlines between the strings

* See also `do-wrapped-output' in "collects/xrepl/xrepl.rkt" -- it
  does something similar, and even parameterized on the prefix etc.
  (But it's intended to wrap each line in an output of a thunk, which
  is most of the work there.)

* I guess that when there's a markdown language we'll need a more
  sophisticated rewrapper.  For example, something that can do this
  bulletized paragraph, possibly including comment characters and
  more.  It should be easy to build something that handles all of
  that.  (For example, I'm using something in Emacs that knows about
  things like "|" that should be repeated on every line, and things
  like "*" or "4.".)

* A quick explanation on the width argument to `wrap-line': it's
  either a number, or an improper list of numbers -- one number for
  each line, and the last for the rest.  (Improper lists make sense
  here, because you can't tell in advance how many lines you'll have.)

          ((lambda (x) (x x)) (lambda (x) (x x)))          Eli Barzilay:
                    http://barzilay.org/                   Maze is Life!

Posted on the dev mailing list.