all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Alexis <flexibeast@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: fill-paragraph with pre and postfix
Date: Wed, 26 Nov 2014 19:48:12 +1100	[thread overview]
Message-ID: <87wq6i4a20.fsf@gmail.com> (raw)
In-Reply-To: <CAA6UvuEmtkLrz4DcQuBimEbUDR4DoOhuEEWppe7Dq8zxVw60nA@mail.gmail.com>


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	other threads:[~2014-11-26  8:48 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-11-05  3:00 fill-paragraph with pre and postfix Jacob Gerlach
2014-11-05 11:34 ` Alexis
     [not found]   ` <CAA6UvuGBeVzLqqvzY5E8DX7kuMtRiJBuP6-AORsrjPR1uuxTtg@mail.gmail.com>
2014-11-05 12:18     ` Jacob Gerlach
2014-11-06  3:45       ` Alexis
2014-11-10 15:24         ` Jacob Gerlach
2014-11-11  0:19           ` Alexis
     [not found]             ` <CAA6UvuEQYgVUkgWD_MkU_mnjYVt6PSXA72RSe9S2YOJmxnNOUg@mail.gmail.com>
2014-11-11  8:26               ` Alexis
2014-11-24 19:26                 ` Jacob Gerlach
2014-11-26  8:48                   ` Alexis [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wq6i4a20.fsf@gmail.com \
    --to=flexibeast@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.