From 13f2b01e2c7b2dc7d11c7c90640d075f02fcc6c3 Mon Sep 17 00:00:00 2001
From: Federico Beffa <beffa@fbengineering.ch>
Date: Tue, 5 Aug 2014 23:08:26 +0200
Subject: [PATCH] org.el: make org-paragraph-fill ignore \[...\] regions
 starting and ending a line

* lisp/org.el (org-paragraph-fill): If a LaTeX \[...\] macro starts
a line and ends a line (possibly spanning multiple lines), then do not
change that part of the paragraph.
---
 lisp/org.el |  128 ++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 113 insertions(+), 15 deletions(-)
 mode change 100644 => 100755 lisp/org.el

diff --git a/lisp/org.el b/lisp/org.el
old mode 100644
new mode 100755
index 0f7a4ef..6cb0cd3
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -22775,6 +22775,118 @@ matches in paragraphs or comments, use it."
 
 (declare-function message-goto-body "message" ())
 (defvar message-cite-prefix-regexp)	; From message.el
+
+(defun org-paragraph-find-dmr (beg end)
+  "Find \\=\\[...\\=\\] LaTeX macros starting and ending a line.
+
+This function returns a list of the form ((dm1-beg dm1-end)
+... (dmN-beg dmN-end)) with dmi-beg and dmi-end, i=1,...,N
+denoting the start respectively the end positions of the i-th
+\\=\\[...\\=\\] display math LaTeX macros starting and ending a
+line (possibly spanning multiple-lines).  This means that \\=\\[
+must be the first non space character of a line and \\=\\] must
+the the last non space character of a line.  The two can be on
+different lines."
+  (let ((dmrl nil) (dmr nil))
+    (save-excursion
+      (goto-char end)
+      (while (setq dmr 
+		   (if
+		       (re-search-backward 
+			(concat 
+			 "\\(^[ \t]*\\\\\\[\\).*\\([\r]?\n\\)*"
+			 "\\(.+[\r]?\n\\)*"
+			 ".*\\\\\\]"
+			 "[ \t]*[\r]?\n") beg t)
+		       (list (match-beginning 1) (match-end 0))
+		     nil))
+	(setq dmrl (append (list dmr) dmrl))))
+    dmrl))
+
+(defun org-paragraph-find-line-breaks (beg end)
+  "Find org `line-break' objects in a paragraph.
+
+Find the positions of org `line-break' objects and returns a list
+with their position, including BEG and END."
+  (append 
+   (cons beg
+	 (org-element-map
+	     (org-element--parse-objects
+	      beg end nil (org-element-restriction 'paragraph))
+	     'line-break
+	   (lambda (lb) (org-element-property :end lb))))
+   (list end)))
+
+(defun org-fill-paragraph-construct-regions (lbl dmrl)
+  "Construct paragraph regions to be filled.
+
+This function takes a list LBL with the positions of org
+`line-break' objects and a list DMRL with the start and end
+positions of \\=\\[...\\=\\] LaTeX macros beginning and ending a
+line.  It returns a list of the form ((r1-beg r1-end) ... (rN-beg
+rN-end)) with the start end end positions of the paragraph
+regions to be filled."
+  (let ((lbl-new lbl))
+    ;; first we make sure there are no line-breaks within displayed
+    ;; math regions
+    (dolist (lb-elt lbl)
+      (mapc (lambda (x) 
+	      (when (and (> lb-elt (nth 0 x)) (< lb-elt (nth 1 x)))
+		(setq lbl-new (remove lb-elt lbl-new))))
+	    dmrl)
+      )
+    ;; then we construct the regions to be filled
+    (let ((lb-this (car lbl-new)) (dmrl-local dmrl) 
+	  dmr-elt regions lb-done)
+      (dolist (lb-next (cdr lbl-new))
+	(setq lb-done nil)
+	;; we have to keep the current line break until we put it at
+	;; the right place, possibly after some displayed math regions
+	(while (not lb-done)
+	  (if (setq dmr-elt (car dmrl-local))
+	    (cond 
+	     ;; line break before next displayed math region
+	     ((< lb-next (nth 0 dmr-elt))
+	      (setq regions (append regions (list (list lb-this lb-next))))
+	      (setq lb-done t)
+	      (setq lb-this lb-next))
+	     ;; line break after display math region
+	     (t
+	      (setq regions (append 
+			     regions 
+			     (list (list lb-this (nth 0 dmr-elt)))))
+	      (setq lb-this (nth 1 dmr-elt))
+	      (setq dmrl-local (cdr dmrl-local))
+	      (setq lb-done nil)))
+	    ;; no displayed math regions left
+	    (progn 
+	      (setq lb-done t)
+	      ;; if a displayed math region finishes a paragraph we do
+	      ;; not need to fill the region after the ending
+	      ;; displayed math region.
+	      (unless (eq lb-this lb-next)
+		(setq regions (append 
+			       regions 
+			       (list (list lb-this lb-next)))))
+	      (setq lb-this lb-next)))))
+      regions)))
+
+(defun org-fill-paragraph-regions (beg end justify)
+  "Fill paragraph starting at BEG and ending at END.
+
+This function is called by `org-fill-paragraph' to fill a
+paragraph.  If a LaTeX \\=\\[...\\=\\] macro starts a line and
+ends a line (possibly spanning multiple lines), then do not
+change that part of the paragraph. Respect org `line-break'
+objects."
+  (let* ((dmrl (org-paragraph-find-dmr beg end))
+	(lbl (org-paragraph-find-line-breaks beg end))
+	(fill-regions (org-fill-paragraph-construct-regions lbl dmrl)))
+    ;; fill the various regions starting from the last one
+    (mapc (lambda (r) 
+	    (fill-region-as-paragraph (nth 0 r) (nth 1 r) justify))
+	  (nreverse fill-regions))))
+
 (defun org-fill-paragraph (&optional justify)
   "Fill element at point, when applicable.
 
@@ -22846,21 +22958,7 @@ a footnote definition, try to fill the first paragraph within."
 	       ;; separators, and fill the parts in reverse order to
 	       ;; avoid messing with markers.
 	       (save-excursion
-		 (goto-char end)
-		 (mapc
-		  (lambda (pos)
-		    (fill-region-as-paragraph pos (point) justify)
-		    (goto-char pos))
-		  ;; Find the list of ending positions for line breaks
-		  ;; in the current paragraph.  Add paragraph
-		  ;; beginning to include first slice.
-		  (nreverse
-		   (cons beg
-			 (org-element-map
-			     (org-element--parse-objects
-			      beg end nil (org-element-restriction 'paragraph))
-			     'line-break
-			   (lambda (lb) (org-element-property :end lb)))))))
+		 (org-fill-paragraph-regions beg end justify))
 	       t)))
 	  ;; Contents of `comment-block' type elements should be
 	  ;; filled as plain text, but only if point is within block
-- 
1.7.9

