From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Alexis Newsgroups: gmane.emacs.help Subject: Re: fill-paragraph with pre and postfix Date: Wed, 26 Nov 2014 19:48:12 +1100 Message-ID: <87wq6i4a20.fsf@gmail.com> References: <87fvdxvpq0.fsf@gmail.com> <877fz9ugu1.fsf@gmail.com> <87vbmmr337.fsf@gmail.com> <87sihq6s3v.fsf@gmail.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1416992062 20393 80.91.229.3 (26 Nov 2014 08:54:22 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 26 Nov 2014 08:54:22 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Nov 26 09:54:17 2014 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1XtYMX-000387-LF for geh-help-gnu-emacs@m.gmane.org; Wed, 26 Nov 2014 09:54:13 +0100 Original-Received: from localhost ([::1]:60902 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XtYMX-0006V9-9J for geh-help-gnu-emacs@m.gmane.org; Wed, 26 Nov 2014 03:54:13 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:58018) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XtYMG-0006V0-B6 for help-gnu-emacs@gnu.org; Wed, 26 Nov 2014 03:54:01 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XtYMA-0001ik-JP for help-gnu-emacs@gnu.org; Wed, 26 Nov 2014 03:53:56 -0500 Original-Received: from mail-pa0-x22b.google.com ([2607:f8b0:400e:c03::22b]:43866) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XtYMA-0001if-Be for help-gnu-emacs@gnu.org; Wed, 26 Nov 2014 03:53:50 -0500 Original-Received: by mail-pa0-f43.google.com with SMTP id kx10so2460211pab.16 for ; Wed, 26 Nov 2014 00:53:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=references:from:to:subject:date:in-reply-to:message-id:mime-version :content-type; bh=HBzj01zUdXY1RW+aIZPA1+cacwljc/eSLbE9wZF/ckM=; b=zfvKWO7XInEtSP0X3pl6OxiXhYKvXY9MBr4dkOvjSACHhA3VVDsSQfsYSOiGEE8S6V Kh7o4oDkCMlAUTloBjHslcdOcpSyS4G4QZ3C5ja1KC1Wr+VbU4SWDCXxl8IyfgR74AE6 ffdctqmStr8VT6bdlOhskdW79JpCs+Yo2pSBP+bVAuA7Az6L/UZSHPU7LrWL/pcH9VkS +yiCmu0Fy224WTBxeU31DgMFnR49adCBMZjNk5rc4YQgYo6KQrQpHL4nOvV+0Q7xqU5w u+iim9dGaclRGJvVBCucVpbHrUVb402I6tsMKmjJaNMpacHPLeKO9L+aeCJBEchVzsfI 0YbA== X-Received: by 10.66.193.8 with SMTP id hk8mr29742318pac.141.1416992029386; Wed, 26 Nov 2014 00:53:49 -0800 (PST) Original-Received: from localhost (ppp118-209-40-19.lns20.mel4.internode.on.net. [118.209.40.19]) by mx.google.com with ESMTPSA id ko4sm3650195pdb.31.2014.11.26.00.53.46 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 26 Nov 2014 00:53:48 -0800 (PST) In-reply-to: X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2607:f8b0:400e:c03::22b X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:101212 Archived-At: 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.