1
votes

I'd like to quickly generate text in a buffer that looks like this:

(fact                    "This is some text which will hang out
                          only on this part of the screen, ideally
                          automatically flowing to the correct
                          margins as I type."
  (+ 1 1) => 2
  ;; more Clojure tests...
  )

I have an Elisp keybinding which quickly spits out a starting template and puts my cursor in the right place:

(global-set-key "\C-of" (lambda ()
                           (interactive)
                           (insert "(fact                               \"\"\n\n  )")
                           (backward-char 6)))

Now, when I am typing in the string portion ("This is some text..."), it'd be awesome if I could get Emacs to automatically flow text to the "correct" margins. Is there some way Emacs can be made to adjust margins and wraparound behavior based on where you're typing? At least, the first time you are typing there?

Barring that, for a given selection of text, how can I do the equivalent of fill-region, but with the desired left and right margins? Currently fill-region deletes all space between fact and "This is...., and left-justifies the rest.

2

2 Answers

1
votes

There might be a simpler way that I'm overlooking now, but I would just do this:

  1. Configure the text block in a temporary buffer, by doing this there:

    a. Set fill-column to the width of the text block that you want.

    b. Put the text at the beginning of the line, i.e., not indented.

    c. Fill the text.

    d. Use indent-rigidly to indent the text to the column you want, except for the first line.

  2. Insert into your target buffer (fact followed by the indentation you want for the first line of the text block. Then insert the contents of the temporary buffer. Then insert whatever other text/code you need.

IOW, I would separate filling the text block from indenting it.

0
votes

The following seems to work for the moment for my alternative (weaker) case:

;; Set up Midje fact with mark inserted at beginning of comment text
;; (refill as needed in appropriate columns, using C-oF).
(global-set-key "\C-of" (lambda ()
              (interactive)
              (insert "(fact                               \"\"\n\n  )")
              (backward-char 6)
              (set-mark (point))))
;; Perform the refill operation to properly reformat the text string
;; in a Midje fact, started with C-of:
(global-set-key "\C-oF" (lambda ()
              (interactive)
              (set-left-margin (mark) (point) 37)
              (fill-region (mark) (point))))

I expect I'll have to tweak this as I get experience using it, but it is pretty close. Still, it'd be nice to figure out how to have this happen automatically, while I'm typing inside the string.