From 957850043a0eeea970e2b44fc6e08b312a8bc60e Mon Sep 17 00:00:00 2001 From: Nicolas Goaziou Date: Mon, 22 May 2017 15:36:28 +0200 Subject: [PATCH] Prevent filling before a "n" macro where it could create list items * lisp/org.el (org-fill-n-macro-as-item-nobreak-p): New function. (org-setup-filling): Use new function. * testing/lisp/test-org.el (test-org/fill-element): Add tests. Reported-by: Kaushal Modi --- lisp/org.el | 7 +++++++ testing/lisp/test-org.el | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/lisp/org.el b/lisp/org.el index 6a15e801c..946d8af8c 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -22905,6 +22905,7 @@ assumed to be significant there." (org-uniquify (append fill-nobreak-predicate '(org-fill-line-break-nobreak-p + org-fill-n-macro-as-item-nobreak-p org-fill-paragraph-with-timestamp-nobreak-p))))) (let ((paragraph-ending (substring org-element-paragraph-separate 1))) (setq-local paragraph-start paragraph-ending) @@ -22927,6 +22928,12 @@ assumed to be significant there." (and (org-at-timestamp-p 'lax) (not (looking-at org-ts-regexp-both)))) +(defun org-fill-n-macro-as-item-nobreak-p () + "Non-nil when a new line at point would create a new list." + ;; During export, a "n" macro followed by a dot or a closing + ;; parenthesis can end up being parsed as a new list item. + (looking-at-p "[ \t]*{{{n\\(?:([^\n)]*)\\)?}}}[.)]\\(?:$\\| \\)")) + (declare-function message-in-body-p "message" ()) (defvar orgtbl-line-start-regexp) ; From org-table.el (defun org-adaptive-fill-function () diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index 9ab14fa2d..e55ee077b 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -565,16 +565,45 @@ (org-fill-element) (buffer-string))))) ;; Do nothing at affiliated keywords. - (org-test-with-temp-text "#+NAME: para\nSome\ntext." - (let ((fill-column 20)) - (org-fill-element) - (should (equal (buffer-string) "#+NAME: para\nSome\ntext.")))) + (should + (equal "#+NAME: para\nSome\ntext." + (org-test-with-temp-text "#+NAME: para\nSome\ntext." + (let ((fill-column 20)) + (org-fill-element) + (buffer-string))))) ;; Do not move point after table when filling a table. (should-not (org-test-with-temp-text "| a | b |\n| c | d |\n" (forward-char) (org-fill-element) - (eobp)))) + (eobp))) + ;; Do not fill "n" macro, with or without arguments, followed by + ;; a dot or a closing parenthesis since it could be confused with + ;; a numbered bullet. + (should-not + (equal "123456789\n{{{n}}}." + (org-test-with-temp-text "123456789 {{{n}}}." + (let ((fill-column 10)) + (org-fill-element) + (buffer-string))))) + (should-not + (equal "123456789\n{{{n}}}\)" + (org-test-with-temp-text "123456789 {{{n}}}\)" + (let ((fill-column 10)) + (org-fill-element) + (buffer-string))))) + (should-not + (equal "123456789\n{{{n()}}}." + (org-test-with-temp-text "123456789 {{{n()}}}." + (let ((fill-column 10)) + (org-fill-element) + (buffer-string))))) + (should-not + (equal "123456789\n{{{n(counter)}}}." + (org-test-with-temp-text "123456789 {{{n(counter)}}}." + (let ((fill-column 10)) + (org-fill-element) + (buffer-string)))))) (ert-deftest test-org/auto-fill-function () "Test auto-filling features."