help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: fill-paragraph with pre and postfix


From: Alexis
Subject: Re: fill-paragraph with pre and postfix
Date: Wed, 26 Nov 2014 19:48:12 +1100

Jacob Gerlach writes:

> 3 .
>
> blk("This is a sentence ")
> blk("stretching over    ")
> blk("three lines.       ")
>
> #3 Is exactly what I'm looking for

Okay, so i think the following is roughly what you're after; for
increased clarity, it makes use of Magnar Sveen's string-manipulation
library s.el (https://github.com/magnars/s.el, available via both MELPA
and Marmalade):

    (require 's)
    (defun format-paragraph ()
      (interactive)
      (let* ((leading "blk(\"")
             (trailing "\")\n")
             (width 65)
             (para-start (save-excursion
                           (backward-paragraph)
                           (point)))
             (para-end (save-excursion
                         (forward-paragraph)
                         (point)))
             (para (s-replace
                    "\n" " "
                    (s-trim
                     (buffer-substring-no-properties para-start para-end))))
             (line "")
             (result ""))
        (while (not (string= "" para))
          (let ((w (if (> (length para) width)
                       width
                     (length para))))
            (setq line (substring para 0 w))
            (if (and (> (length para) width) 
                     (string-match "[^[:space:]]+$" line))
                (setq w (match-beginning 0))
              (setq w (length para)))
            (setq line (s-pad-right width " " (substring para 0 w)))
            (setq result (concat result leading line trailing))
            (setq para (substring para w))))
        (save-excursion
          (kill-region para-start para-end)
          (goto-char (1+ para-start))
          (insert result))))

Hope that helps!


Alexis.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]