From unknown Sat Jun 21 04:59:35 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#78816 <78816@debbugs.gnu.org> To: bug#78816 <78816@debbugs.gnu.org> Subject: Status: [PATCH] Add variable `fill-region-as-paragraph-function' Reply-To: bug#78816 <78816@debbugs.gnu.org> Date: Sat, 21 Jun 2025 11:59:35 +0000 retitle 78816 [PATCH] Add variable `fill-region-as-paragraph-function' reassign 78816 emacs submitter 78816 Roi Martin severity 78816 normal tag 78816 patch thanks From debbugs-submit-bounces@debbugs.gnu.org Tue Jun 17 06:52:25 2025 Received: (at submit) by debbugs.gnu.org; 17 Jun 2025 10:52:25 +0000 Received: from localhost ([127.0.0.1]:54771 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uRTvU-0008HH-Tq for submit@debbugs.gnu.org; Tue, 17 Jun 2025 06:52:25 -0400 Received: from lists.gnu.org ([2001:470:142::17]:39156) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uRTvR-0008Ge-Gx for submit@debbugs.gnu.org; Tue, 17 Jun 2025 06:52:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uRTvJ-0001VJ-85 for bug-gnu-emacs@gnu.org; Tue, 17 Jun 2025 06:52:13 -0400 Received: from mail-wm1-x32c.google.com ([2a00:1450:4864:20::32c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1uRTvE-0007X8-VD for bug-gnu-emacs@gnu.org; Tue, 17 Jun 2025 06:52:12 -0400 Received: by mail-wm1-x32c.google.com with SMTP id 5b1f17b1804b1-451d3f72391so63148405e9.3 for ; Tue, 17 Jun 2025 03:52:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750157526; x=1750762326; darn=gnu.org; h=mime-version:message-id:date:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=aiB3XvIBDlOmQS4jIyvicPo3/zV/dqT88sy16/+J9rc=; b=LtOgNWKkTnAiZrShkGHGC2CHwWZtWVOKoj9j3whL+WusovFvdxDfy7c6/c+Ac+XEIT WkZK1syzwMYFKP1stWzCD5y8jIl+K5Au63CEgBQUVszrMyPSQemZgLWOL4Z8gX66XXIT sxJxP4LBD+v6DN1ibyku5gLveo4DYo7HRJqPI5p4yLHJZNJTHYb2BJe3wL7VfS+ptZUt dxM+AS0OC8Ve/7oqjxI/QmTMEG0MUXqog0P4ZE9VYFyHrrNSYYAEJwKwoliN5I/ENopa bysuzPIR1F2d5RBh3noRTcsoVJM7YcBM9Yc8zM0gFqflNA2Faqrgjeeaw9ls/09ZdyJX zFBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750157526; x=1750762326; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=aiB3XvIBDlOmQS4jIyvicPo3/zV/dqT88sy16/+J9rc=; b=WnhvHbExl9bWfS1ywa3BMnVio9P6IKdxNikx7ZPLVyDLz5wplGiRTnqn3BHDSzC6S+ ATSoWUuA9Fy2BxACAKu75egfXPrsXjOrUGhDxJaD0R69UGyROIafKWyMXoCPIUeIr70M PVUwxAd37kM+KvqaCo0c7L+q0ujMINxIjbmZOjr6w6YHAszs3cwsNeBUtuxja6ApXe90 MbXOFnAEyQK9jGUcaZtFhhEykLpMbmagnBPWxGOfFvvVtyPjQ7AkSW4AyaFYtwojGWTD fUcvPwGvy1Vmr2Fv17DeF51SUZUshF6hmjZPW/zhyGUQQ9XxU22rAHNuadye8H+RKKER wCew== X-Gm-Message-State: AOJu0YyK7BudcCrzmDM8fcRV+nDbrHSi3cm55TobJVaBamM+03km5VBw 63fzpSD6T9YuKTFn8W1W/uKSjGq2u51ZgP9vMXy1v2Ykr+eF1T58krBQ1PZEsA== X-Gm-Gg: ASbGncsma7kxVV6F8fhbFaS+Hd29nttVycuSGJECnHKEbopccnaCFtTzBKoMChoH7KY UcWzek5j9tKV7qy/tlq7a1tCkd2LdfScLStpySmZjynrWv3hpB3q2VUasxfQdiZxUlAaM+BVwws YwMqlIpHy+4fIfGCbApL0QKIS83HBHivlZGFwl4L5PnxxdJ7f2nzoI5QFujgbDjyDGL6AF5ehQq sZg8MA7oQrSr+0FYX5JiO/TgSWc4PRQdMbNEP7U5p0K0oqaGfHn5iA2rZGy6AfaRtigyD3Ti//N 7KZE40dTedcRZ/Yk2SJu9JTzeTR+DR3VHN6qH6VIxtWhw8Yc6zY+w2gfJDbu5CIXTPCp0Cw= X-Google-Smtp-Source: AGHT+IGGCyLuhhLEobl5wSvFzoM0i1lNvvtvcGGsen7zt9O3/SmojZKKZJ/8EkaGs+uGA3duEXLSSg== X-Received: by 2002:a05:600c:8b88:b0:441:b3eb:574e with SMTP id 5b1f17b1804b1-4533ca5e547mr114169225e9.5.1750157526032; Tue, 17 Jun 2025 03:52:06 -0700 (PDT) Received: from localhost ([84.79.192.255]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4532e195768sm173143985e9.0.2025.06.17.03.52.05 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:52:05 -0700 (PDT) From: Roi Martin To: bug-gnu-emacs@gnu.org Subject: [PATCH] Add variable `fill-region-as-paragraph-function' X-Debbugs-Cc: Marcin Borkowski , monnier@iro.umontreal.ca, eliz@gnu.org, juri@linkov.net Date: Tue, 17 Jun 2025 12:52:01 +0200 Message-ID: <87o6umy71q.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2a00:1450:4864:20::32c; envelope-from=jroi.martin@gmail.com; helo=mail-wm1-x32c.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 1.0 (+) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.0 (/) --=-=-= Content-Type: text/plain Tags: patch Right now it is not possible to customize how the `fill-region' function fills text. This patch adds a new variable named `fill-region-as-paragraph-function' that allows to override the default behavior. For more details, see the discussion in Bug#78561. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-variable-fill-region-as-paragraph-function.patch >From 4533782f57f43befda22ab62f58f02c3fb9ed040 Mon Sep 17 00:00:00 2001 From: Roi Martin Date: Tue, 17 Jun 2025 10:45:13 +0200 Subject: [PATCH] Add variable `fill-region-as-paragraph-function' Add the variable `fill-region-as-paragraph-function' to provide a way to override how the function `fill-region' fills text. * doc/emacs/text.texi (Fill Commands): * doc/lispref/text.texi (Filling): Document `fill-region-as-paragraph-function'. * lisp/textmodes/fill.el (fill-region-as-paragraph-function): Add variable. (fill-region): Call `fill-region-as-paragraph-function' internally. * test/lisp/textmodes/fill-tests.el (fill-test-fill-region): Add test case for the `fill-region' function. * test/lisp/textmodes/fill-resources/fill-region.erts: Add test data. --- doc/emacs/text.texi | 9 ++++++++ doc/lispref/text.texi | 9 ++++++++ lisp/textmodes/fill.el | 13 +++++++++-- .../textmodes/fill-resources/fill-region.erts | 23 +++++++++++++++++++ test/lisp/textmodes/fill-tests.el | 11 +++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/lisp/textmodes/fill-resources/fill-region.erts diff --git a/doc/emacs/text.texi b/doc/emacs/text.texi index 5635171f5cfe..cc21d7c8756a 100644 --- a/doc/emacs/text.texi +++ b/doc/emacs/text.texi @@ -607,6 +607,15 @@ Fill Commands deletes any blank lines within the region, so separate blocks of text end up combined into one block. +@vindex fill-region-as-paragraph-function + The variable @code{fill-region-as-paragraph-function} provides a way +to override how the function @code{fill-region} fills text. Its value +should be a function, which is called with the arguments @var{from}, +@var{to}, @var{justify} and @var{nosqueeze}, defined by +@code{fill-region-as-paragraph}. It should return the +@code{fill-prefix} used for filling. The default value of this variable +is @code{fill-region-as-paragraph}. + @cindex justification A numeric argument to @kbd{M-q} tells it to @dfn{justify} the text as well as filling it. This means that extra spaces are inserted to diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 75b2b1c3d60e..fb6eacfc3ed9 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -1841,6 +1841,15 @@ Filling Manual}. @end defvar +@defvar fill-region-as-paragraph-function +This variable provides a way to override how the function +@code{fill-region} fills text. Its value should be a function, which is +called with the arguments @var{from}, @var{to}, @var{justify} and +@var{nosqueeze}, defined by @code{fill-region-as-paragraph}. It should +return the @code{fill-prefix} used for filling. The default value of +this variable is @code{fill-region-as-paragraph}. +@end defvar + @defvar use-hard-newlines If this variable is non-@code{nil}, the filling functions do not delete newlines that have the @code{hard} text property. These hard diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el index 386a88bda3a8..bf7749b885d2 100644 --- a/lisp/textmodes/fill.el +++ b/lisp/textmodes/fill.el @@ -1040,6 +1040,12 @@ fill-comment-paragraph ;; Make sure we don't return nil. t)))))) +(defvar fill-region-as-paragraph-function 'fill-region-as-paragraph + "Function to fill the region as if it were a single paragraph. +It is called with the arguments FROM, TO, JUSTIFY and NOSQUEEZE, defined +by `fill-region-as-paragraph'. It should return the `fill-prefix' used +for filling.") + (defun fill-region (from to &optional justify nosqueeze to-eop) "Fill each of the paragraphs in the region. A prefix arg means justify as well. @@ -1058,7 +1064,10 @@ fill-region Return the `fill-prefix' used for filling the last paragraph. If `sentence-end-double-space' is non-nil, then period followed by one -space does not end a sentence, so don't break a line there." +space does not end a sentence, so don't break a line there. + +The variable `fill-region-as-paragraph-function' can be used to override +how paragraphs are filled." (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) @@ -1095,7 +1104,7 @@ fill-region (goto-char beg)) (if (and (>= (point) initial) (< (point) end)) (setq fill-pfx - (fill-region-as-paragraph (point) end justify nosqueeze)) + (funcall fill-region-as-paragraph-function (point) end justify nosqueeze)) (goto-char end)))) (goto-char start-point) (set-marker start-point nil) diff --git a/test/lisp/textmodes/fill-resources/fill-region.erts b/test/lisp/textmodes/fill-resources/fill-region.erts new file mode 100644 index 000000000000..95e08248309f --- /dev/null +++ b/test/lisp/textmodes/fill-resources/fill-region.erts @@ -0,0 +1,23 @@ +Point-Char: | + +Name: fill region + +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +|Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-=-= diff --git a/test/lisp/textmodes/fill-tests.el b/test/lisp/textmodes/fill-tests.el index 8fbd9919bad0..40095fc99417 100644 --- a/test/lisp/textmodes/fill-tests.el +++ b/test/lisp/textmodes/fill-tests.el @@ -122,6 +122,17 @@ test-fill-haskell ;; w "))) +(ert-deftest fill-test-fill-region () + "Test the `fill-region' function." + (ert-test-erts-file (ert-resource-file "fill-region.erts") + (lambda () + (fill-region + (point) + (progn + (goto-char (point-max)) + (forward-line -1) + (beginning-of-line) + (point)))))) (ert-deftest fill-test-fill-region-as-paragraph-semlf () "Test the `fill-region-as-paragraph-semlf' function." -- 2.49.0 --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Tue Jun 17 06:59:41 2025 Received: (at 78816) by debbugs.gnu.org; 17 Jun 2025 10:59:41 +0000 Received: from localhost ([127.0.0.1]:54873 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uRU2W-0000YM-Kb for submit@debbugs.gnu.org; Tue, 17 Jun 2025 06:59:41 -0400 Received: from mail-wr1-x42d.google.com ([2a00:1450:4864:20::42d]:60780) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1uRU2T-0000XZ-SW for 78816@debbugs.gnu.org; Tue, 17 Jun 2025 06:59:38 -0400 Received: by mail-wr1-x42d.google.com with SMTP id ffacd0b85a97d-3a57c8e247cso2150827f8f.1 for <78816@debbugs.gnu.org>; Tue, 17 Jun 2025 03:59:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750157972; x=1750762772; darn=debbugs.gnu.org; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :from:to:cc:subject:date:message-id:reply-to; bh=DAOlD7hh2vVAT/g5yowhaqK8jf7a4omlzs/CkrHxWkk=; b=bGTW3sMmDIagMISB5NUGPBORbxWHGG93+79RZ/m3oQONDhSPo+PyPw7Og86iPJstkb yTdpsO9xvTSSl7DhTu+7u59BxPy7EutlldI8W0t2SKr1XXlLd3DZKNfDea2gPNxxz5VU kcRDLENkvSdSGvuXNarRt0kOfLEWrSqUxkMy/TX50o7ikFyf7VITmA+Eekajb1ZISRFB vfFI9XozJMWB7lOmBHgxq46NkaaEic7PQ93lfrq8JNEpzCUSQjj9NAimPngwJ+yfa0ca QyDdpxvow3ICcmXHwmfqIBpd+R+gqvYXe+5lOXI4ii/CEBGetJ4r2GHHiu/46Cs2AKYr 8Omw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750157972; x=1750762772; h=mime-version:message-id:date:references:in-reply-to:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=DAOlD7hh2vVAT/g5yowhaqK8jf7a4omlzs/CkrHxWkk=; b=CJml2PXQYYsjUPR+xUCl1bCie36wFtYpQoCtrTd3cL0dPML0reKqHybSnDlkyWmIwG OUk1OMEB+cwhucEkQRwgntjnvFWObgiDcVLMuwHdR3UEQ+2tVdCEMevycpwbewY76OPg hbF35jd4v7ezddYPTIpvIPzV1/xQ4rkrKfKqTb+JVtsAUCIH8vm3DdU0vBJba65uiFV/ 4ccS/+LxW0zxfZ3wljQMVAjsrWCuk+pfRFbNqxwyJ+xZnttLmg7FOUCp+Suw0Z34TtXs VtKlyX2pDdAguiDQQ5ilU4T0zKNClyxPH3/gnRz/caCAG7DjRiuuR2kVq6BHxqnjHaBj JIEw== X-Gm-Message-State: AOJu0YyzkClIQ1xX0o8ZQ6lIRxGsrG3eYlHH4+TE19BBwWFH3eziTpZM c3vP/QNGLyIRATMHRIMGeSEsfQc2pveuffQ2hy91N/DMHZJKnuroQh3adQSkW+Cs X-Gm-Gg: ASbGnctEAvWtaSTyO9ppaVbFD708yxIbdH102FFF6eVbS3fPLJhCNhmWleoovGmEsd8 sD+K03y0yiDCGg+x/RHHsb2akRQ++Zz/K7M8v9YVYByiP2KryQsQJLOSYEbknBFVTg93D0AJyZK 9DKJBqd+S2L02DZwNa5cacOkoDiwiKHmd+2ReajsutVGSoWLqzMGqU2C8Ml5fZ9T07fb77pJdXY Oziv/HgM/txlFzfyXekXVWEZG1vb/UI/q4Ew1TJzHhcwYUFXfAvxGe9TJVuyQ4FCr4wgfjAAJVh 4/etJaRdH+T/bCGG7Q2Mbj00n7s5eDabSswDpJ2i/xCD0jEY7ps116rZX+pmLS6a8Unj+P8= X-Google-Smtp-Source: AGHT+IGSdZFNW8/C7ds6/lFTeBSJfPLVH6jMptkHM5LlKOuh0DKQ8KrRxvumomyfgknGQMBK0RDUUA== X-Received: by 2002:a05:6000:2404:b0:3a3:652d:1640 with SMTP id ffacd0b85a97d-3a57238b3dbmr10737739f8f.2.1750157971504; Tue, 17 Jun 2025 03:59:31 -0700 (PDT) Received: from localhost ([84.79.192.255]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a578f3ba84sm9598341f8f.65.2025.06.17.03.59.30 for <78816@debbugs.gnu.org> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 03:59:30 -0700 (PDT) From: Roi Martin To: 78816@debbugs.gnu.org Subject: Re: bug#78816: Acknowledgement ([PATCH] Add variable `fill-region-as-paragraph-function') In-Reply-To: References: <87o6umy71q.fsf@gmail.com> Date: Tue, 17 Jun 2025 12:59:30 +0200 Message-ID: <87ikkuy6p9.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 78816 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --=-=-= Content-Type: text/plain Please, find attached a new version of the patch. It adds the bug number to the commit message and it also adds a missing white space after period. Sorry for the noise. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-variable-fill-region-as-paragraph-function.patch >From 38710130e291dc799a93990e61e94826e8f145c0 Mon Sep 17 00:00:00 2001 From: Roi Martin Date: Tue, 17 Jun 2025 10:45:13 +0200 Subject: [PATCH] Add variable `fill-region-as-paragraph-function' Add the variable `fill-region-as-paragraph-function' to provide a way to override how the function `fill-region' fills text. * doc/emacs/text.texi (Fill Commands): * doc/lispref/text.texi (Filling): Document `fill-region-as-paragraph-function'. * lisp/textmodes/fill.el (fill-region-as-paragraph-function): Add variable. (fill-region): Call `fill-region-as-paragraph-function' internally. * test/lisp/textmodes/fill-tests.el (fill-test-fill-region): Add test case for the `fill-region' function. * test/lisp/textmodes/fill-resources/fill-region.erts: Add test data. (Bug#78816) --- doc/emacs/text.texi | 9 ++++++++ doc/lispref/text.texi | 9 ++++++++ lisp/textmodes/fill.el | 13 +++++++++-- .../textmodes/fill-resources/fill-region.erts | 23 +++++++++++++++++++ test/lisp/textmodes/fill-tests.el | 11 +++++++++ 5 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 test/lisp/textmodes/fill-resources/fill-region.erts diff --git a/doc/emacs/text.texi b/doc/emacs/text.texi index 5635171f5cfe..cc21d7c8756a 100644 --- a/doc/emacs/text.texi +++ b/doc/emacs/text.texi @@ -607,6 +607,15 @@ Fill Commands deletes any blank lines within the region, so separate blocks of text end up combined into one block. +@vindex fill-region-as-paragraph-function + The variable @code{fill-region-as-paragraph-function} provides a way +to override how the function @code{fill-region} fills text. Its value +should be a function, which is called with the arguments @var{from}, +@var{to}, @var{justify} and @var{nosqueeze}, defined by +@code{fill-region-as-paragraph}. It should return the +@code{fill-prefix} used for filling. The default value of this variable +is @code{fill-region-as-paragraph}. + @cindex justification A numeric argument to @kbd{M-q} tells it to @dfn{justify} the text as well as filling it. This means that extra spaces are inserted to diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 75b2b1c3d60e..fb6eacfc3ed9 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -1841,6 +1841,15 @@ Filling Manual}. @end defvar +@defvar fill-region-as-paragraph-function +This variable provides a way to override how the function +@code{fill-region} fills text. Its value should be a function, which is +called with the arguments @var{from}, @var{to}, @var{justify} and +@var{nosqueeze}, defined by @code{fill-region-as-paragraph}. It should +return the @code{fill-prefix} used for filling. The default value of +this variable is @code{fill-region-as-paragraph}. +@end defvar + @defvar use-hard-newlines If this variable is non-@code{nil}, the filling functions do not delete newlines that have the @code{hard} text property. These hard diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el index 386a88bda3a8..8186f9e04314 100644 --- a/lisp/textmodes/fill.el +++ b/lisp/textmodes/fill.el @@ -1040,6 +1040,12 @@ fill-comment-paragraph ;; Make sure we don't return nil. t)))))) +(defvar fill-region-as-paragraph-function 'fill-region-as-paragraph + "Function to fill the region as if it were a single paragraph. +It is called with the arguments FROM, TO, JUSTIFY and NOSQUEEZE, defined +by `fill-region-as-paragraph'. It should return the `fill-prefix' used +for filling.") + (defun fill-region (from to &optional justify nosqueeze to-eop) "Fill each of the paragraphs in the region. A prefix arg means justify as well. @@ -1058,7 +1064,10 @@ fill-region Return the `fill-prefix' used for filling the last paragraph. If `sentence-end-double-space' is non-nil, then period followed by one -space does not end a sentence, so don't break a line there." +space does not end a sentence, so don't break a line there. + +The variable `fill-region-as-paragraph-function' can be used to override +how paragraphs are filled." (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) @@ -1095,7 +1104,7 @@ fill-region (goto-char beg)) (if (and (>= (point) initial) (< (point) end)) (setq fill-pfx - (fill-region-as-paragraph (point) end justify nosqueeze)) + (funcall fill-region-as-paragraph-function (point) end justify nosqueeze)) (goto-char end)))) (goto-char start-point) (set-marker start-point nil) diff --git a/test/lisp/textmodes/fill-resources/fill-region.erts b/test/lisp/textmodes/fill-resources/fill-region.erts new file mode 100644 index 000000000000..95e08248309f --- /dev/null +++ b/test/lisp/textmodes/fill-resources/fill-region.erts @@ -0,0 +1,23 @@ +Point-Char: | + +Name: fill region + +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +|Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-=-= diff --git a/test/lisp/textmodes/fill-tests.el b/test/lisp/textmodes/fill-tests.el index 8fbd9919bad0..40095fc99417 100644 --- a/test/lisp/textmodes/fill-tests.el +++ b/test/lisp/textmodes/fill-tests.el @@ -122,6 +122,17 @@ test-fill-haskell ;; w "))) +(ert-deftest fill-test-fill-region () + "Test the `fill-region' function." + (ert-test-erts-file (ert-resource-file "fill-region.erts") + (lambda () + (fill-region + (point) + (progn + (goto-char (point-max)) + (forward-line -1) + (beginning-of-line) + (point)))))) (ert-deftest fill-test-fill-region-as-paragraph-semlf () "Test the `fill-region-as-paragraph-semlf' function." -- 2.49.0 --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Tue Jun 17 18:07:35 2025 Received: (at 78816) by debbugs.gnu.org; 17 Jun 2025 22:07:35 +0000 Received: from localhost ([127.0.0.1]:36843 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uReSt-0005N6-1x for submit@debbugs.gnu.org; Tue, 17 Jun 2025 18:07:35 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:39865) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uReSr-0005MS-0G for 78816@debbugs.gnu.org; Tue, 17 Jun 2025 18:07:33 -0400 Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id BBBBD441BF3; Tue, 17 Jun 2025 18:07:26 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1750198045; bh=mMmE0eOK+cXr3Z8Thjd/dlWD3Cdj7KhL3cihiPM04xI=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=Sq0wQ9zNZxO+GYCiTlxG5Dmoi9UFRdbs/Yj60A5i5HnkMxwFw5nkChwH0sqRZpQDI j4tEwkgCyYtUF1as0m+IsvE0aAdzB8hUuzfN4kmOQKfuaxMaLGqLrG6OW/XH2e/BEW 5O+JUR3YV+14dJzeYP5xEsS5ah5bc0z/z0xbNhyFq+Xk1zuqb0t9cwZtmi+N3Y3MDY kjTuRXX+8MlHAv78sMWXqprS3rU4V14vfr3a1ACHryZwGvXnb8KQSc7f1vbGJZBb1P d1pX3jA6H3gOk08E4TAQA6WdwsATjBbkcvnHa8d5S3XLghYLQ123aO3w33EywmMvyH PqkX3PPPZMhUA== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 62B69441BD4; Tue, 17 Jun 2025 18:07:25 -0400 (EDT) Received: from lechazo (lechon.iro.umontreal.ca [132.204.27.242]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 46D9E12023B; Tue, 17 Jun 2025 18:07:25 -0400 (EDT) From: Stefan Monnier To: Roi Martin Subject: Re: bug#78816: [PATCH] Add variable `fill-region-as-paragraph-function' In-Reply-To: <87o6umy71q.fsf@gmail.com> Message-ID: References: <87o6umy71q.fsf@gmail.com> Date: Tue, 17 Jun 2025 18:07:24 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL 0.162 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 78816 Cc: eliz@gnu.org, Marcin Borkowski , 78816@debbugs.gnu.org, juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) > --- a/doc/emacs/text.texi > +++ b/doc/emacs/text.texi > @@ -607,6 +607,15 @@ Fill Commands > deletes any blank lines within the region, so separate blocks of text > end up combined into one block. >=20=20 > +@vindex fill-region-as-paragraph-function > + The variable @code{fill-region-as-paragraph-function} provides a way > +to override how the function @code{fill-region} fills text. Its value > +should be a function, which is called with the arguments @var{from}, > +@var{to}, @var{justify} and @var{nosqueeze}, defined by > +@code{fill-region-as-paragraph}. It should return the > +@code{fill-prefix} used for filling. The default value of this variable > +is @code{fill-region-as-paragraph}. Does it need to be in the Emacs manual also? I have the impression that it belongs only in the ELisp manual. > +@defvar fill-region-as-paragraph-function > +This variable provides a way to override how the function > +@code{fill-region} fills text. I don't think this should be specific to `fill-region`. E.g. it should also affect `fill-paragraph` and any other user of `fill-region-as-paragraph`. IOW, I'd say something like "... how functions like @code{fill-region} fill text." > Its value should be a function, which is > +called with the arguments @var{from}, @var{to}, @var{justify} and > +@var{nosqueeze}, defined by @code{fill-region-as-paragraph}. It should > +return the @code{fill-prefix} used for filling. The default value of > +this variable is @code{fill-region-as-paragraph}. > +@end defvar [ I think "fill prefix" above is used in a "generic" sense (e.g. it may be chosen without using the `fill-prefix` variable), so I'm not sure `@code` is warranted for it. ] > +(defvar fill-region-as-paragraph-function 'fill-region-as-paragraph ^^^ #' > + "Function to fill the region as if it were a single paragraph. > +It is called with the arguments FROM, TO, JUSTIFY and NOSQUEEZE, defined > +by `fill-region-as-paragraph'. It should return the `fill-prefix' used > +for filling.") Please use two spaces after the ".". Better yet, use semlf-style filling here. =F0=9F=99=82 > @@ -1095,7 +1104,7 @@ fill-region > (goto-char beg)) > (if (and (>=3D (point) initial) (< (point) end)) > (setq fill-pfx > - (fill-region-as-paragraph (point) end justify nosqueeze)) > + (funcall fill-region-as-paragraph-function (point) end justify nosqu= eeze)) I think I'd leave this alone and instead do (defun fill-region-as-paragraph (&rest args) (apply fill-region-as-paragraph-function args)) and rename `fill-region-as-paragraph` as `fill-region-as-paragraph-default`. Stefan From debbugs-submit-bounces@debbugs.gnu.org Tue Jun 17 19:34:59 2025 Received: (at 78816) by debbugs.gnu.org; 17 Jun 2025 23:34:59 +0000 Received: from localhost ([127.0.0.1]:37967 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uRfpR-0005lo-St for submit@debbugs.gnu.org; Tue, 17 Jun 2025 19:34:58 -0400 Received: from mail-wm1-x336.google.com ([2a00:1450:4864:20::336]:51650) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1uRfpO-0005lJ-Ew for 78816@debbugs.gnu.org; Tue, 17 Jun 2025 19:34:55 -0400 Received: by mail-wm1-x336.google.com with SMTP id 5b1f17b1804b1-442e9c00bf4so55922555e9.3 for <78816@debbugs.gnu.org>; Tue, 17 Jun 2025 16:34:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750203288; x=1750808088; darn=debbugs.gnu.org; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=6LnanSFCLpjoOBJrJLEy5IpxN4N+K57s0bJCGtl2joY=; b=BkkLVLvJCNHjGyV4bXeoAC0sS/hHdybJjBxcQ6Ua9oGnaA3kt+imPdE7Jekwo4NC8R FFcwLF8t8TrYEraeGDVFdf1FGrGW1uMgeOs6TVvn6kJWV8flO0mNhD/Oz3ycHhnLcjT0 TYs71z+bR/z9ZXAK1LgxEImOAm5yn9h8mouY/YDdXYptbvSk+m+AWlpTO/ZXxW4ZULeT jfMFSzkHw0+yglaYEgSO/NZvNUqKhsPA7PPMbTf8tdgEOVH7cK7ZeseVAlGLfD/EGdA+ LeGZ1C3ejObgNxGzYmP53NcoiNYhKjgZ5VTKz0kx3uogeQ3HwG9tTT9m5FNJbuziDTwn aLAA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750203288; x=1750808088; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=6LnanSFCLpjoOBJrJLEy5IpxN4N+K57s0bJCGtl2joY=; b=R1Qz0WdVORindNxlDvNwVVtesN+D+4mfDIJ9L+s7gSFniyJKNvus1fIcwxTdFbz2R0 SXjWuejU109u1ZPnZBQxXgV7fbJNv/gecFMORr1BivVWyHEKFGHW2PENCgL7LGKhNtlj ZaF7LdGqG/IwXdezPJN0ALWemKoyKh+ZzMFwMTxaXmVGF0uu5uOAbOEE9tAl8XNXC1ED MDivnYQha6IUQoqPSjCcfBYSK0XX7KkRjgvSPtPshcLpAgXmyLwFypHw9vbhQ3fycPom trfE3xxRRvLV6CupmVyCM1khb5XDkhu71IUSCC72qjzmbhq5Cf1n1hBA+H+aIqplG1If t2kg== X-Gm-Message-State: AOJu0YwG2eEFrKbWTBqqhVXGfGnhBTj5WjKiM+xYmq8JgzCIP8iW2YyV n7NSoCu0ZWq59z7Ge83dB5TRK03bWAP31TC1HLvvz/6jk7geC5Qf1duJ X-Gm-Gg: ASbGncv4n+MbOwkuaDXy3KLtTDh4VKGIS0Mqh/DKLpGVhHT0AvLFgaI9eeCQJ8zLnWn w/0suTKhoPv54H4cSr4O5qIlaQZQv5HaIOBaDuPYsDvkeLQmg4Kp9eiIqVVVM4kBqL4qdIDhpPd 1HNu50r0AJkBMGzDwxbly0CBbvbOk8kWWJBBS45VaFnDJiidi2Ux9ywJQwhw6aQBf2k75hK9acu DmnAeC8OWgyRQrPqaKa1a5fY5Do6oKIkDkJ7tqyOwrQAqK3jESckq3YivHqeyC/OLqYkj214Nnj nRutIFpiF+5LLRmEQgEpYTPPF70dbvX4yv4bTzfBTX+TRbCYF0mnJ88hKUJbz7zux+qfrUk= X-Google-Smtp-Source: AGHT+IFFgx3dDnwGkXNPUEfq7XnqeiwZtAIwuBznxY2qprWG5GQbmhRXBwU3jce5K8QvYfL2oVf4Zg== X-Received: by 2002:a05:600c:83c5:b0:453:697:6f30 with SMTP id 5b1f17b1804b1-4533cb02209mr130747765e9.32.1750203287417; Tue, 17 Jun 2025 16:34:47 -0700 (PDT) Received: from localhost ([84.79.192.255]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a568a7389bsm15330723f8f.37.2025.06.17.16.34.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Jun 2025 16:34:46 -0700 (PDT) From: Roi Martin To: Stefan Monnier Subject: Re: bug#78816: [PATCH] Add variable `fill-region-as-paragraph-function' In-Reply-To: References: <87o6umy71q.fsf@gmail.com> Date: Wed, 18 Jun 2025 01:34:26 +0200 Message-ID: <87msa6rlh9.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 78816 Cc: eliz@gnu.org, Marcin Borkowski , 78816@debbugs.gnu.org, juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --=-=-= Content-Type: text/plain Stefan Monnier writes: > I think I'd leave this alone and instead do > > (defun fill-region-as-paragraph (&rest args) > (apply fill-region-as-paragraph-function args)) > > and rename `fill-region-as-paragraph` as `fill-region-as-paragraph-default`. Done. However, I kept the parameters and the doc string in `fill-region-as-paragraph', so almost nothing changed from the user perspective. Thanks for the review! I've attached a new version of the patch. Roi --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-variable-fill-region-as-paragraph-function.patch >From bda4f9cb256ca85f39298cf29f20ee5cbcb846fb Mon Sep 17 00:00:00 2001 From: Roi Martin Date: Tue, 17 Jun 2025 10:45:13 +0200 Subject: [PATCH] Add variable `fill-region-as-paragraph-function' Add the variable `fill-region-as-paragraph-function' to provide a way to override how functions like `fill-region' fill text. * doc/lispref/text.texi (Filling): Document `fill-region-as-paragraph-function' variable. * lisp/textmodes/fill.el (fill-region-as-paragraph-function): Add variable. (fill-region-as-paragraph): Convert into `fill-region-as-paragraph-function' wrapper. (fill-region-as-paragraph-default): Rename old `fill-region-as-paragraph' function. (fill-region-as-paragraph-semlf): Update calls to `fill-region-as-paragraph-default'. (fill-region): Add reference to `fill-region-as-paragraph-function' in doc string. * test/lisp/textmodes/fill-tests.el (fill-test-fill-region): Add test case for the `fill-region' function. * test/lisp/textmodes/fill-resources/fill-region.erts: Add test data. (Bug#78816) --- doc/lispref/text.texi | 8 ++ lisp/textmodes/fill.el | 94 +++++++++++-------- .../textmodes/fill-resources/fill-region.erts | 23 +++++ test/lisp/textmodes/fill-tests.el | 11 +++ 4 files changed, 98 insertions(+), 38 deletions(-) create mode 100644 test/lisp/textmodes/fill-resources/fill-region.erts diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 75b2b1c3d60e..ed13b73971f3 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -1841,6 +1841,14 @@ Filling Manual}. @end defvar +@defvar fill-region-as-paragraph-function +This variable provides a way to override how functions like +@code{fill-region} fill text. Its value should be a function, which is +called with the arguments defined by @code{fill-region-as-paragraph}. +It should return the fill prefix used for filling. The default value of +this variable is @code{fill-region-as-paragraph-default}. +@end defvar + @defvar use-hard-newlines If this variable is non-@code{nil}, the filling functions do not delete newlines that have the @code{hard} text property. These hard diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el index 386a88bda3a8..e0d2bd2e1d37 100644 --- a/lisp/textmodes/fill.el +++ b/lisp/textmodes/fill.el @@ -644,34 +644,10 @@ fill-indent-to-left-margin (indent-line-to (current-left-margin)) (put-text-property beg (point) 'face 'default))) -(defun fill-region-as-paragraph (from to &optional justify - nosqueeze squeeze-after) +(defun fill-region-as-paragraph-default (from to &optional justify + nosqueeze squeeze-after) "Fill the region as if it were a single paragraph. -This command removes any paragraph breaks in the region and -extra newlines at the end, and indents and fills lines between the -margins given by the `current-left-margin' and `current-fill-column' -functions. (In most cases, the variable `fill-column' controls the -width.) It leaves point at the beginning of the line following the -region. - -Normally, the command performs justification according to -the `current-justification' function, but with a prefix arg, it -does full justification instead. - -When called from Lisp, optional third arg JUSTIFY can specify any -type of justification; see `default-justification' for the possible -values. -Optional fourth arg NOSQUEEZE non-nil means not to make spaces -between words canonical before filling. -Fifth arg SQUEEZE-AFTER, if non-nil, should be a buffer position; it -means canonicalize spaces only starting from that position. -See `canonically-space-region' for the meaning of canonicalization -of spaces. - -Return the `fill-prefix' used for filling. - -If `sentence-end-double-space' is non-nil, then period followed by one -space does not end a sentence, so don't break a line there." +This is the default implementation of `fill-region-as-paragraph'." (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) @@ -797,6 +773,45 @@ fill-region-as-paragraph ;; Return the fill-prefix we used fill-prefix))) +(defvar fill-region-as-paragraph-function #'fill-region-as-paragraph-default + "Function to fill the region as if it were a single paragraph. +It is called with the arguments defined by `fill-region-as-paragraph'. +It should return the `fill-prefix' used for filling.") + +(defun fill-region-as-paragraph (from to &optional justify + nosqueeze squeeze-after) + "Fill the region as if it were a single paragraph. +This command removes any paragraph breaks in the region and +extra newlines at the end, and indents and fills lines between the +margins given by the `current-left-margin' and `current-fill-column' +functions. (In most cases, the variable `fill-column' controls the +width.) It leaves point at the beginning of the line following the +region. + +Normally, the command performs justification according to +the `current-justification' function, but with a prefix arg, it +does full justification instead. + +When called from Lisp, optional third arg JUSTIFY can specify any +type of justification; see `default-justification' for the possible +values. +Optional fourth arg NOSQUEEZE non-nil means not to make spaces +between words canonical before filling. +Fifth arg SQUEEZE-AFTER, if non-nil, should be a buffer position; it +means canonicalize spaces only starting from that position. +See `canonically-space-region' for the meaning of canonicalization +of spaces. + +Return the `fill-prefix' used for filling. + +If `sentence-end-double-space' is non-nil, then period followed by one +space does not end a sentence, so don't break a line there. + +The variable `fill-region-as-paragraph-function' can be used to override +how text is filled." + (funcall fill-region-as-paragraph-function + from to justify nosqueeze squeeze-after)) + (defsubst skip-line-prefix (prefix) "If point is inside the string PREFIX at the beginning of line, move past it." (when (and prefix @@ -1058,7 +1073,10 @@ fill-region Return the `fill-prefix' used for filling the last paragraph. If `sentence-end-double-space' is non-nil, then period followed by one -space does not end a sentence, so don't break a line there." +space does not end a sentence, so don't break a line there. + +The variable `fill-region-as-paragraph-function' can be used to override +how paragraphs are filled." (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) @@ -1618,19 +1636,19 @@ fill-region-as-paragraph-semlf (with-restriction (line-beginning-position) to (let ((fill-column (point-max))) (setq pfx (or (save-excursion - (fill-region-as-paragraph (point) - (point-max) - nil - nosqueeze - squeeze-after)) + (fill-region-as-paragraph-default (point) + (point-max) + nil + nosqueeze + squeeze-after)) ""))) (while (not (eobp)) (let ((fill-prefix pfx)) - (fill-region-as-paragraph (point) - (progn (forward-sentence) (point)) - justify - nosqueeze - squeeze-after)) + (fill-region-as-paragraph-default (point) + (progn (forward-sentence) (point)) + justify + nosqueeze + squeeze-after)) (when (and (> (point) (line-beginning-position)) (< (point) (line-end-position))) (delete-horizontal-space) diff --git a/test/lisp/textmodes/fill-resources/fill-region.erts b/test/lisp/textmodes/fill-resources/fill-region.erts new file mode 100644 index 000000000000..95e08248309f --- /dev/null +++ b/test/lisp/textmodes/fill-resources/fill-region.erts @@ -0,0 +1,23 @@ +Point-Char: | + +Name: fill region + +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +|Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-=-= diff --git a/test/lisp/textmodes/fill-tests.el b/test/lisp/textmodes/fill-tests.el index 8fbd9919bad0..40095fc99417 100644 --- a/test/lisp/textmodes/fill-tests.el +++ b/test/lisp/textmodes/fill-tests.el @@ -122,6 +122,17 @@ test-fill-haskell ;; w "))) +(ert-deftest fill-test-fill-region () + "Test the `fill-region' function." + (ert-test-erts-file (ert-resource-file "fill-region.erts") + (lambda () + (fill-region + (point) + (progn + (goto-char (point-max)) + (forward-line -1) + (beginning-of-line) + (point)))))) (ert-deftest fill-test-fill-region-as-paragraph-semlf () "Test the `fill-region-as-paragraph-semlf' function." -- 2.49.0 --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Wed Jun 18 15:58:35 2025 Received: (at 78816) by debbugs.gnu.org; 18 Jun 2025 19:58:35 +0000 Received: from localhost ([127.0.0.1]:56009 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uRyvb-0003JH-Aj for submit@debbugs.gnu.org; Wed, 18 Jun 2025 15:58:35 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:60395) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uRyvY-0003IX-N8 for 78816@debbugs.gnu.org; Wed, 18 Jun 2025 15:58:33 -0400 Received: from pmg3.iro.umontreal.ca (localhost [127.0.0.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 94376442580; Wed, 18 Jun 2025 15:58:26 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1750276705; bh=UUAeA+kDRTNAoEuCsTkDYEf1mKtWQgU63MAPtjm7qtE=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=oMAbY8E5IskTBnr9HCFGXmo6Um04MuMJ+DbzW/k+l+PCW0SQtOllJvjuelDj7qOb7 teC3YwucXFDSmrSSu3O3WN/fC+V1XNB2gt6javPUYptsegEm8xJ5nIcsVkb6T3SmMT EGOaGlmq0ut8+d7+8D6l57YewM+8oRgl4LfXyfofjTlJpoqSRrtiHwbaTZXFvrDcxP MT5hPwYWz17rgNQMswp4786f3MRuL8BIFCQDadaaG03knSe5LtZwStyH8UtCec1KFu DKVyOgK7YHZXuRycX7jnBXh5SAxc69B5bROWmjVsapFyzG3APsLj/NNJ7faKSYks2h vm89WoO1d7yCQ== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg3.iro.umontreal.ca (Proxmox) with ESMTP id 936EA44257E; Wed, 18 Jun 2025 15:58:25 -0400 (EDT) Received: from alfajor (modemcable005.21-80-70.mc.videotron.ca [70.80.21.5]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 6A82F1201DA; Wed, 18 Jun 2025 15:58:25 -0400 (EDT) From: Stefan Monnier To: Roi Martin Subject: Re: bug#78816: [PATCH] Add variable `fill-region-as-paragraph-function' In-Reply-To: <87msa6rlh9.fsf@gmail.com> Message-ID: References: <87o6umy71q.fsf@gmail.com> <87msa6rlh9.fsf@gmail.com> Date: Wed, 18 Jun 2025 15:58:24 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.194 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 78816 Cc: eliz@gnu.org, Marcin Borkowski , 78816@debbugs.gnu.org, juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) > Done. However, I kept the parameters and the doc string in > `fill-region-as-paragraph', so almost nothing changed from the user > perspective. > > Thanks for the review! I've attached a new version of the patch. LGTM. Eli? Stefan From debbugs-submit-bounces@debbugs.gnu.org Thu Jun 19 01:41:01 2025 Received: (at 78816) by debbugs.gnu.org; 19 Jun 2025 05:41:01 +0000 Received: from localhost ([127.0.0.1]:59629 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uS81C-0006Px-So for submit@debbugs.gnu.org; Thu, 19 Jun 2025 01:41:00 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:45350) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uS819-0006OT-UF for 78816@debbugs.gnu.org; Thu, 19 Jun 2025 01:40:57 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uS80w-00062B-8O; Thu, 19 Jun 2025 01:40:46 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=IcIVhwQkilhda9m/hHU//6UjIKKOYQ1G+LV05p4/wVA=; b=AW8Fel4HgfVT hSE2YLFlnxQfi3NpqB6T3SmryaoKsDirtXPRlKXNpYCxatpRXqOtn9nDt9DlfoEKWNzJwivrW+zDz wIEH+ejjcn8/0ppvhFUkSqHS+V1ZUMv4fM5GdBrvH2KEeH3DP4orAHo3KtPfBUWTIQ6uWO29+6CIE wzpNcRINzXowvw73z1Qq6VrWMQ393V9fFEUduhR//6hO9Cz6Vpt0CA5BcDY7nX7O4US8RZd5IoXgT ieijha8MW2lGR8Fy6OO6SU/j/e0N53ega9DvVUfclBtMEwcrdNnbjacCcaQp7vB3oxWfDK07+pQ/v tgW+0ya1YPjLQ1Xr9EH96w==; Date: Thu, 19 Jun 2025 08:40:38 +0300 Message-Id: <86zfe4l25l.fsf@gnu.org> From: Eli Zaretskii To: Stefan Monnier In-Reply-To: (message from Stefan Monnier on Wed, 18 Jun 2025 15:58:24 -0400) Subject: Re: bug#78816: [PATCH] Add variable `fill-region-as-paragraph-function' References: <87o6umy71q.fsf@gmail.com> <87msa6rlh9.fsf@gmail.com> X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 78816 Cc: juri@linkov.net, mbork@mbork.pl, jroi.martin@gmail.com, 78816@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) > From: Stefan Monnier > Cc: 78816@debbugs.gnu.org, Marcin Borkowski , > eliz@gnu.org, juri@linkov.net > Date: Wed, 18 Jun 2025 15:58:24 -0400 > > > Done. However, I kept the parameters and the doc string in > > `fill-region-as-paragraph', so almost nothing changed from the user > > perspective. > > > > Thanks for the review! I've attached a new version of the patch. > > LGTM. Eli? I have a problem with the documentation of the functions and commands we had before, which can now be overridden by the new fill-region-as-paragraph-function. The documentation was basically left unaltered as to what filling does, with just a single sentence, at the end of a very long doc string, saying that fill-region-as-paragraph-function could change the behavior. But the truth is that such a function can completely change the result and invalidate the description in the doc string; in particular, the function which implements semantic line-feeds does exactly that. So at the very least, the doc strings should: . mention the fill-region-as-paragraph-function early on . say that the rest of the description is about the default function, mentioning its symbol I also think some minimal addition to the user manual is in order, to the effect that the manual describes the default filling style, while the major mode or the user could change it via fill-region-as-paragraph-function. From debbugs-submit-bounces@debbugs.gnu.org Thu Jun 19 13:46:14 2025 Received: (at 78816) by debbugs.gnu.org; 19 Jun 2025 17:46:14 +0000 Received: from localhost ([127.0.0.1]:36842 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uSJL3-00021k-Aj for submit@debbugs.gnu.org; Thu, 19 Jun 2025 13:46:14 -0400 Received: from mail-wm1-x32a.google.com ([2a00:1450:4864:20::32a]:54744) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1uSJKz-00020e-Jb for 78816@debbugs.gnu.org; Thu, 19 Jun 2025 13:46:11 -0400 Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-453398e90e9so7569585e9.1 for <78816@debbugs.gnu.org>; Thu, 19 Jun 2025 10:46:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1750355163; x=1750959963; darn=debbugs.gnu.org; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc:subject:date:message-id:reply-to; bh=cVcbMULrPDKWboPRHR68syzexoza/XSqnKmDT9GpZk8=; b=GmUtpXLB7BU/hv2xuCZOCWtP0aCBfjRmIvDnGSVPO3e16HZVpXGD16qcChIhbjE1Ss SNORYfQH9a23ODR+pEdwQDNWB1eUH6CQo8GM4JAu0+5mA5ybHF59v4JPFLanKdOuuac3 ocSEADRBZmCtRKg3HjHaa3+roacUzwVDOaLiiMeXSwt2kYChGiaFJ80LrCccMP9jhKX3 7jMtvna/M6RJHSqCaFHXofDQAXMF1hbgAldUL0maV0RZD/qZhc+H12GOn7vUN/2OqxxJ Oe9whKg+sucDEwvILHN3c8GkTQRSVU39eMbkxtns7RwHPekpu7WpAteOK0wrJxX98BJ5 Pc1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1750355163; x=1750959963; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=cVcbMULrPDKWboPRHR68syzexoza/XSqnKmDT9GpZk8=; b=AQJtWEMWkbR5eLWUxP45iVoV82MkyRCXoQrRF4TVwLrFNU4KfQ5uV9jh8eDojA4S3W wwX3QA5ULmRujnQBHFzNkhLJukbmCKkIEcqI6O/sDGgyUSjR+H0cfJhYlLh8viOJy6AM sf32pST4nGAR07SbbZ2xhQxb2nDeOafAYLe7oYa3ZimhTA1NQRA07f1GHHIy84uRuXX2 dghZXIynYlBkTigPMaimE1KL6qlTKimY9w03rq8+90lYK+SaIOugo+NshJV0AZjOX1uo Fhp9/bP4O+cKuSTozC5xG61kW0CYwKya/eQWdWCgdCJIUTW4rV9aPAy/EuF72wYB3JaY 8DLQ== X-Gm-Message-State: AOJu0Yy2ESxtz/NsRN42kB5QFRDsL7BZZzawgHPddKT7Z4h1NN+zXgd8 jN2EOdOCdOwnJKClccomIRpc6aDUQbZSjglWboPPj+ehJtYnR5DUqV4j X-Gm-Gg: ASbGncsNqQXRYqfKD3GckJhdQ/s6ciAQPf7t424yOCRO4HJBzeMHwnfEx/Wbzv8YRqu oOGDW9S9bsO322IDclAB2h2d9EFUlOy9dhdNknT9IGSXCF2/nu6wia9Coi34VznsGxpF1vb+xyJ bNIgQrAYMgUktsPTDetPehxU9rA3s0ZHUySE8zrBjYaBZGKgf9TrkcK129pazVrX96ocMGNNg9t N7RQrimBKvuXdbKQ0ju7CEsjDjqgzGy0zuUGkktKvzOTedXxdRE689VkUud71UYTxVHMlO5qsZT NzViKIwFe6+oNOMkEeettcujsKNJdlk86rPajbMKO11oA5m69uimqUUSdWjO X-Google-Smtp-Source: AGHT+IFIW7Jd6A4lav0QuPtJlCrdAAig7A1nc2wsyJzd3HboNN4Z1kPZR6MrtvX3WFfddGGsLsG+6Q== X-Received: by 2002:a05:600c:1c28:b0:440:6a79:6df0 with SMTP id 5b1f17b1804b1-4533cb4bc8fmr177539905e9.22.1750355162940; Thu, 19 Jun 2025 10:46:02 -0700 (PDT) Received: from localhost ([84.79.192.255]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3a6d0f19f9esm24250f8f.39.2025.06.19.10.46.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 19 Jun 2025 10:46:02 -0700 (PDT) From: Roi Martin To: Eli Zaretskii , Stefan Monnier Subject: Re: bug#78816: [PATCH] Add variable `fill-region-as-paragraph-function' In-Reply-To: <86zfe4l25l.fsf@gnu.org> References: <87o6umy71q.fsf@gmail.com> <87msa6rlh9.fsf@gmail.com> <86zfe4l25l.fsf@gnu.org> Date: Thu, 19 Jun 2025 19:45:57 +0200 Message-ID: <8734bvd3qi.fsf@gmail.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 78816 Cc: mbork@mbork.pl, 78816@debbugs.gnu.org, juri@linkov.net X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) --=-=-= Content-Type: text/plain Eli Zaretskii writes: > I have a problem with the documentation of the functions and commands > we had before, which can now be overridden by the new > fill-region-as-paragraph-function. The documentation was basically > left unaltered as to what filling does, with just a single sentence, > at the end of a very long doc string, saying that > fill-region-as-paragraph-function could change the behavior. But the > truth is that such a function can completely change the result and > invalidate the description in the doc string; in particular, the > function which implements semantic line-feeds does exactly that. > > So at the very least, the doc strings should: > > . mention the fill-region-as-paragraph-function early on > . say that the rest of the description is about the default function, > mentioning its symbol > > I also think some minimal addition to the user manual is in order, to > the effect that the manual describes the default filling style, while > the major mode or the user could change it via > fill-region-as-paragraph-function. Please, find attached a new version of the patch with the following changes: - In the user manual, I added a reference to `fill-region-as-paragraph-function'. I also made explicit that the `fill-region-as-paragraph' behavior described by the manual corresponds to the default implementation. - In the Emacs Lisp reference, I made explicit that the behavior described for `fill-region-as-paragraph' corresponds to the default implementation. - Moved the original docstring of `fill-region-as-paragraph' to `fill-region-as-paragraph-default' given that it describes its specific behavior. - I used the docstring of `fill-region-as-paragraph' to describe the general purpose of the command and the expected arguments. I also moved the mention to `fill-region-as-paragraph-function' to the beginning of the docstring. - I noticed that, after my previous patches, `fill-region-as-paragraph' was not a command anymore. Now it is a command again. Roi --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Add-variable-fill-region-as-paragraph-function.patch >From 71483d1d149970b2d52c1244244512ec5ae4e023 Mon Sep 17 00:00:00 2001 From: Roi Martin Date: Tue, 17 Jun 2025 10:45:13 +0200 Subject: [PATCH] Add variable `fill-region-as-paragraph-function' Add the variable `fill-region-as-paragraph-function' to provide a way to override how functions like `fill-region' fill text. * doc/lispref/text.texi (Filling): Document `fill-region-as-paragraph-function' variable. * doc/emacs/text.texi (Fill Commands): Reference `fill-region-as-paragraph-function' variable. * lisp/textmodes/fill.el (fill-region-as-paragraph-function): Add variable. (fill-region-as-paragraph): Convert into `fill-region-as-paragraph-function' wrapper. (fill-region-as-paragraph-default): Rename old `fill-region-as-paragraph' function. (fill-region-as-paragraph-semlf): Update calls to `fill-region-as-paragraph-default'. (fill-region): Add reference to `fill-region-as-paragraph-function' in doc string. * test/lisp/textmodes/fill-tests.el (fill-test-fill-region): Add test case for the `fill-region' function. * test/lisp/textmodes/fill-resources/fill-region.erts: Add test data. (Bug#78816) --- doc/emacs/text.texi | 11 ++-- doc/lispref/text.texi | 15 ++++- lisp/textmodes/fill.el | 62 +++++++++++++++---- .../textmodes/fill-resources/fill-region.erts | 23 +++++++ test/lisp/textmodes/fill-tests.el | 11 ++++ 5 files changed, 104 insertions(+), 18 deletions(-) create mode 100644 test/lisp/textmodes/fill-resources/fill-region.erts diff --git a/doc/emacs/text.texi b/doc/emacs/text.texi index 5635171f5cfe..2708b2e93121 100644 --- a/doc/emacs/text.texi +++ b/doc/emacs/text.texi @@ -600,12 +600,15 @@ Fill Commands the region. @findex fill-region-as-paragraph +@vindex fill-region-as-paragraph-function @kbd{M-q} and @code{fill-region} use the usual Emacs criteria for finding paragraph boundaries (@pxref{Paragraphs}). For more control, -you can use @kbd{M-x fill-region-as-paragraph}, which refills -everything between point and mark as a single paragraph. This command -deletes any blank lines within the region, so separate blocks of text -end up combined into one block. +you can use @kbd{M-x fill-region-as-paragraph}, which refills everything +between point and mark as a single paragraph. The behavior of this +command is controlled by the variable +@code{fill-region-as-paragraph-function}. By default, it deletes any +blank lines within the region, so separate blocks of text end up +combined into one block. @cindex justification A numeric argument to @kbd{M-q} tells it to @dfn{justify} the text diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index 75b2b1c3d60e..9ab06b6d613c 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -1737,7 +1737,12 @@ Filling @deffn Command fill-region-as-paragraph start end &optional justify nosqueeze squeeze-after This command considers a region of text as a single paragraph and fills -it. If the region was made up of many paragraphs, the blank lines +it. The behavior of this command is controlled by the variable +@code{fill-region-as-paragraph-function}, with the default +implementation being @code{fill-region-as-paragraph-default}, which is +described in detail below. + +If the region was made up of many paragraphs, the blank lines between paragraphs are removed. This function justifies as well as filling when @var{justify} is non-@code{nil}. @@ -1841,6 +1846,14 @@ Filling Manual}. @end defvar +@defvar fill-region-as-paragraph-function +This variable provides a way to override how functions like +@code{fill-region} fill text. Its value should be a function, which +should accept the arguments defined by @code{fill-region-as-paragraph} +and return the fill prefix used for filling. The default value of this +variable is @code{fill-region-as-paragraph-default}. +@end defvar + @defvar use-hard-newlines If this variable is non-@code{nil}, the filling functions do not delete newlines that have the @code{hard} text property. These hard diff --git a/lisp/textmodes/fill.el b/lisp/textmodes/fill.el index 386a88bda3a8..c552add7f8f0 100644 --- a/lisp/textmodes/fill.el +++ b/lisp/textmodes/fill.el @@ -644,8 +644,8 @@ fill-indent-to-left-margin (indent-line-to (current-left-margin)) (put-text-property beg (point) 'face 'default))) -(defun fill-region-as-paragraph (from to &optional justify - nosqueeze squeeze-after) +(defun fill-region-as-paragraph-default (from to &optional justify + nosqueeze squeeze-after) "Fill the region as if it were a single paragraph. This command removes any paragraph breaks in the region and extra newlines at the end, and indents and fills lines between the @@ -797,6 +797,39 @@ fill-region-as-paragraph ;; Return the fill-prefix we used fill-prefix))) +(defvar fill-region-as-paragraph-function #'fill-region-as-paragraph-default + "Function to fill the region as if it were a single paragraph. +It should accept the arguments defined by `fill-region-as-paragraph' and +return the `fill-prefix' used for filling.") + +(defun fill-region-as-paragraph (from to &optional justify + nosqueeze squeeze-after) + "Fill the region as if it were a single paragraph. +The behavior of this command is controlled by the variable +`fill-region-as-paragraph-function', with the default implementation +being `fill-region-as-paragraph-default'. + +The arguments FROM and TO define the boundaries of the region. + +The optional third argument JUSTIFY, when called interactively with a +prefix arg, is assigned the value `full'. +When called from Lisp, JUSTIFY can specify any type of justification; +see `default-justification' for the possible values. +Optional fourth arg NOSQUEEZE non-nil means not to make spaces between +words canonical before filling. +Fifth arg SQUEEZE-AFTER, if non-nil, should be a buffer position; it +means canonicalize spaces only starting from that position. +See `canonically-space-region' for the meaning of canonicalization of +spaces. + +It returns the `fill-prefix' used for filling." + (interactive (progn + (barf-if-buffer-read-only) + (list (region-beginning) (region-end) + (if current-prefix-arg 'full)))) + (funcall fill-region-as-paragraph-function + from to justify nosqueeze squeeze-after)) + (defsubst skip-line-prefix (prefix) "If point is inside the string PREFIX at the beginning of line, move past it." (when (and prefix @@ -1058,7 +1091,10 @@ fill-region Return the `fill-prefix' used for filling the last paragraph. If `sentence-end-double-space' is non-nil, then period followed by one -space does not end a sentence, so don't break a line there." +space does not end a sentence, so don't break a line there. + +The variable `fill-region-as-paragraph-function' can be used to override +how paragraphs are filled." (interactive (progn (barf-if-buffer-read-only) (list (region-beginning) (region-end) @@ -1618,19 +1654,19 @@ fill-region-as-paragraph-semlf (with-restriction (line-beginning-position) to (let ((fill-column (point-max))) (setq pfx (or (save-excursion - (fill-region-as-paragraph (point) - (point-max) - nil - nosqueeze - squeeze-after)) + (fill-region-as-paragraph-default (point) + (point-max) + nil + nosqueeze + squeeze-after)) ""))) (while (not (eobp)) (let ((fill-prefix pfx)) - (fill-region-as-paragraph (point) - (progn (forward-sentence) (point)) - justify - nosqueeze - squeeze-after)) + (fill-region-as-paragraph-default (point) + (progn (forward-sentence) (point)) + justify + nosqueeze + squeeze-after)) (when (and (> (point) (line-beginning-position)) (< (point) (line-end-position))) (delete-horizontal-space) diff --git a/test/lisp/textmodes/fill-resources/fill-region.erts b/test/lisp/textmodes/fill-resources/fill-region.erts new file mode 100644 index 000000000000..95e08248309f --- /dev/null +++ b/test/lisp/textmodes/fill-resources/fill-region.erts @@ -0,0 +1,23 @@ +Point-Char: | + +Name: fill region + +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +|Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-= +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do +eiusmod tempor. Incididunt ut labore et dolore magna aliqua. + +Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor. Incididunt ut labore et dolore magna aliqua. +=-=-= diff --git a/test/lisp/textmodes/fill-tests.el b/test/lisp/textmodes/fill-tests.el index 8fbd9919bad0..40095fc99417 100644 --- a/test/lisp/textmodes/fill-tests.el +++ b/test/lisp/textmodes/fill-tests.el @@ -122,6 +122,17 @@ test-fill-haskell ;; w "))) +(ert-deftest fill-test-fill-region () + "Test the `fill-region' function." + (ert-test-erts-file (ert-resource-file "fill-region.erts") + (lambda () + (fill-region + (point) + (progn + (goto-char (point-max)) + (forward-line -1) + (beginning-of-line) + (point)))))) (ert-deftest fill-test-fill-region-as-paragraph-semlf () "Test the `fill-region-as-paragraph-semlf' function." -- 2.49.0 --=-=-=--