Stefan Monnier [22/Jun  8:25am -04] wrote:
>>>       Join lines of this paragraph and fix up whitespace at joins.
>>>     Interactively, if the region is active, join lines of each paragraph in
>>>     the region.
>>>
>>> Why not calling fill-paragraph-function in a loop on each paragraph in 
>>> region?
>
> For the same reason `fill-region` doesn't do that: the caller of
> `fill-paragraph-function` has too little control over the region that it
> should fill.
>
>> AFAIU, unfill-paragraph should produce a single paragraph, so looping
>> over several paragraphs and filling each one might not produce the
>> same result.
>>
>> Stefan, WDYT?
>
> When BEG..END are not provided, I guess it could make sense to make it
> rely on `fill-paragraph-function`, to make it more like
> `fill-paragraph`.
>
> When BEG..END are provided, its current working (i.e. to call
> `fill-region`) makes a lot of sense, and it should work just as well or
> as poorly as `fill-region`.
>
> `fill-region` suffers from a lack of hooks for use by major modes, but
> we've lived with that for eons so it seems good enough for the
> corresponding use case of `unfill-paragraph`.

I've prepared a patch doing this but it breaks a simple 'M-x
unfill-paragraph' within an Emacs Lisp docstring.  I think this is
because lisp-fill-paragraph respects emacs-lisp-docstring-fill-column
instead of fill-column.  And theoretically this could happen with any
other major mode's fill-paragraph-function.

I think therefore that unfill-paragraph ought *not* to respect
fill-paragraph-function, because in that case, binding fill-column is
not actually sufficient to unfill the paragraph.

-- >8 --
 lisp/textmodes/fill.el | 24 ++++++++++++------------
 1 file changed, 12 insertions(+), 12 deletions(-)

diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el
index 270cb388971..30b6c1fc1a6 100644
--- a/lisp/textmodes/fill.el
+++ b/lisp/textmodes/fill.el
@@ -972,20 +972,10 @@ unfill-paragraph
 means to join the lines of each paragraph in the region delimited by BEG
 and END."
   (interactive "P\nR")
-  (when (or arg (not beg))
-    (let ((arg (prefix-numeric-value arg)))
-      (when (zerop arg)
-        (user-error "Invalid numeric argument to `unfill-paragraph'"))
-      (save-excursion
-        (fill-forward-paragraph 1)
-        (fill-forward-paragraph -1)
-        (setq beg (point))
-        (fill-forward-paragraph arg)
-        (setq end (point)))))
   ;; FIXME: It would be better to use
   ;;
   ;;    (let ((fill-column (* (max 2 tab-width) (point-max))))
-  ;;      (fill-region beg end))
+  ;;      ...)
   ;;
   ;; multiplying by at least 2 to account for any wide characters in the
   ;; region to be filled and by at least `tab-width' to account for any
@@ -993,7 +983,17 @@ unfill-paragraph
   ;; prove that filling the region will actually unfill it.
   ;; However, `fill-region' fails if `fill-column' is not a fixnum.
   (let ((fill-column most-positive-fixnum))
-    (fill-region beg end)))
+    (if (or arg (not beg))
+        (let ((arg (prefix-numeric-value arg)))
+          (when (zerop arg)
+            (user-error "Invalid numeric argument to `unfill-paragraph'"))
+          (save-excursion
+            (dotimes (_ arg)
+              ;; Call `fill-paragraph' in order to respect
+              ;; `fill-paragraph-function' (bug#81038).
+              (fill-paragraph)
+              (fill-forward-paragraph 1))))
+      (fill-region beg end))))
 
 (declare-function comment-search-forward "newcomment" (limit &optional 
noerror))
 (declare-function comment-string-strip "newcomment" (str beforep afterp))
-- 
Sean Whitton

Reply via email to