branch: externals/org
commit 8829a05bfecfe798b2bc5e83ddfd0b4bf31c8c29
Author: Ihor Radchenko <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    org-adaptive-fill-function: Prevent Org markup from being recognized as 
fill prefix
    
    * lisp/org.el (org-adaptive-fill-paragraph-function): New helper
    function producing fill prefix via `fill-match-adaptive-prefix' but
    ignoring Org markup characters at bol (for example, *bold* at bol).
    (org-adaptive-fill-function): Use the new function when filling
    paragraphs.
    * testing/lisp/test-org.el (test-org/fill-element): New test case.
    
    Reported-by: Jacob S. Gordon <[email protected]>
    Link: 
https://orgmode.org/list/[email protected]
---
 lisp/org.el              | 25 ++++++++++++++++++++++---
 testing/lisp/test-org.el |  9 +++++++++
 2 files changed, 31 insertions(+), 3 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 7905b79ed1..7b455a18a3 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -19905,6 +19905,24 @@ Signal an error when not at a block."
   ;; parenthesis can end up being parsed as a new list item.
   (looking-at-p "[ \t]*{{{n\\(?:([^\n)]*)\\)?}}}[.)]\\(?:$\\| \\)"))
 
+(defun org-adaptive-fill-paragraph-function ()
+  "Compute a fill prefix for the current line in paragraph.
+Return fill prefix, as a string, or nil if current line isn't meant to
+be filled.  Use `adaptive-fill-regexp', but ignore Org markup at the
+beginning of the line."
+  (let ((context (org-element-context)))
+    ;; Skip over markup symbols, if any.
+    (defvar org-element-all-objects) ; org-element.el
+    (when (and (org-element-type-p context org-element-all-objects)
+               (org-element-contents-begin context)
+               (> (org-element-contents-begin context)
+                  (org-element-begin context)))
+      (goto-char (org-element-contents-begin context)))
+    ;; Delegate to `fill-match-adaptive-prefix' to handle
+    ;; `adaptive-fill-regexp'.  *Assume* that
+    ;; `fill-match-adaptive-prefix' matches at point.
+    (let (adaptive-fill-function) (fill-match-adaptive-prefix))))
+
 (defun org-adaptive-fill-function ()
   "Compute a fill prefix for the current line.
 Return fill prefix, as a string, or nil if current line isn't
@@ -19939,11 +19957,12 @@ matches in paragraphs or comments, use it."
                                     (org-element-begin parent))
                                    ?\s))
                      ((and adaptive-fill-regexp
-                           ;; Locally disable
+                           ;; Locally override
                            ;; `adaptive-fill-function' to let
                            ;; `fill-context-prefix' handle
-                           ;; `adaptive-fill-regexp' variable.
-                           (let (adaptive-fill-function)
+                           ;; `adaptive-fill-regexp' variable, but
+                            ;; ignore Org markup, like "*" at bol.
+                           (let ((adaptive-fill-function 
#'org-adaptive-fill-paragraph-function))
                              (fill-context-prefix
                               post-affiliated
                               (org-element-end element)))))
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 6b1ccb5f16..530584e8d3 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -752,6 +752,15 @@ Otherwise, evaluate RESULT as an sexp and return its 
result."
                  (adaptive-fill-regexp "[ \t]*>+[ \t]*"))
              (org-fill-element)
              (buffer-string)))))
+  ;; But do not fill markup
+  (should
+   (equal "*The* quick brown fox jumps over the lazy dog
+while the sphinx of black quartz judges my vow."
+         (org-test-with-temp-text "*The* quick brown fox jumps over the lazy 
dog while the sphinx of black quartz judges my vow."
+           (let ((fill-column 50)
+                 (adaptive-fill-regexp "[*     ]*"))
+             (org-fill-element)
+             (buffer-string)))))
   ;; Special case: Fill first paragraph when point is at an item or
   ;; a plain-list or a footnote reference.
   (should

Reply via email to