The initial patch was just slightly off. Here's a correct version.

Timothy <tecos...@gmail.com> writes:
> As such, I have attached a patch which adds fill “cuts” around `\[' and
> `\]', when `\[' occurs at the start of a line. This leaves the display 
> equation
> delimiters on their own line.

>From d6d2221a7a9bf5e3cf93265c13cb643bcfe46929 Mon Sep 17 00:00:00 2001
From: TEC <t...@tecosaur.com>
Date: Fri, 1 Oct 2021 01:14:21 +0800
Subject: [PATCH] org: Don't fill displayed equations into text

* list/org.el (org-fill-element): If a displayed equation (\[ ... \])
starts on its own line, it should not be filled into the rest of the
text. I.e.,

some nice text
\[
  1+1=2
\]
more text.

should not become,

some nice text \[ 1+1=3 \] more text.

While the above example may not look bad, with non-trivial equations
this can become quite messy.
---
 lisp/org.el | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/lisp/org.el b/lisp/org.el
index 405f0f0f9..ff86a9dd0 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -19551,16 +19551,26 @@ (defun org-fill-element (&optional justify)
 			 (org-element-property :contents-end element))))
 	   ;; Do nothing if point is at an affiliated keyword.
 	   (if (< (line-end-position) beg) t
-	     ;; Fill paragraph, taking line breaks into account.
 	     (save-excursion
 	       (goto-char beg)
 	       (let ((cuts (list beg)))
+                 ;; Cut fill on line breaks.
 		 (while (re-search-forward "\\\\\\\\[ \t]*\n" end t)
 		   (when (eq 'line-break
 			     (org-element-type
 			      (save-excursion (backward-char)
 					      (org-element-context))))
 		     (push (point) cuts)))
+                 ;; Cut fill on displayed equations.
+                 (while (re-search-forward "^[ \t]*\\\\\\[" end t)
+                   (let ((el (org-element-context)))
+                     (when (eq 'latex-fragment (org-element-type el))
+                       (setf cuts (append
+                                   (list (org-element-property :end el)
+                                         (- (org-element-property :end el) 2)
+                                         (+ (org-element-property :begin el) 2)
+                                         (org-element-property :begin el))
+                                   cuts)))))
 		 (dolist (c (delq end cuts))
 		   (fill-region-as-paragraph c end justify)
 		   (setq end c))))
-- 
2.33.0

Reply via email to