org-comment-line-break-function: Avoid built-in Emacs comment machinery

* lisp/org.el (org-comment-line-break-function): Rely on Org
parser (`org-adaptive-fill-function') to determine comment filling.
Handle nil values of `fill-prefix' correctly.

* testing/lisp/test-org.el (test-org/default-indent-new-line): New
test.  Written by Kaushal Modi <kaushal.modi@gmail.com>
https://list.orgmode.org/CAFyQvY36DkBSNy2mPxDNZWeoTjUK8mAqgJM-zHxNamfReqGkuQ@mail.gmail.com/

Reported-by: Richard Lawrence <richard.lawrence@uni-tuebingen.de>
Link: https://list.orgmode.org/87lf18fue9.fsf@aquinas.i-did-not-set--mail-host-address--so-tickle-me/
This commit is contained in:
Ihor Radchenko 2022-09-29 13:02:46 +08:00
parent 9db57aee3e
commit 506d5b12dd
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
2 changed files with 32 additions and 6 deletions

View File

@ -19296,12 +19296,18 @@ filling the current element."
"Break line at point and indent, continuing comment if within one. "Break line at point and indent, continuing comment if within one.
The inserted newline is marked hard if variable The inserted newline is marked hard if variable
`use-hard-newlines' is true, unless optional argument SOFT is `use-hard-newlines' is true, unless optional argument SOFT is
non-nil." non-nil.
(if soft (insert-and-inherit ?\n) (newline 1))
(save-excursion (forward-char -1) (delete-horizontal-space)) This function is a simplified version of `comment-indent-new-line'
(delete-horizontal-space) that bypasses the complex Emacs machinery dealing with comments.
(indent-to-left-margin) We instead rely on Org parser, utilizing `org-adaptive-fill-function'"
(insert-before-markers-and-inherit fill-prefix)) (let ((fill-prefix (org-adaptive-fill-function)))
(if soft (insert-and-inherit ?\n) (newline 1))
(save-excursion (forward-char -1) (delete-horizontal-space))
(delete-horizontal-space)
(indent-to-left-margin)
(when fill-prefix
(insert-before-markers-and-inherit fill-prefix))))
;;; Fixed Width Areas ;;; Fixed Width Areas

View File

@ -1405,6 +1405,26 @@ CLOCK: [2022-09-17 sam. 11:00]--[2022-09-17 sam. 11:46] => 0:46"
(org-indent-region (point-min) (point-max)) (org-indent-region (point-min) (point-max))
(buffer-string))))) (buffer-string)))))
(ert-deftest test-org/default-indent-new-line ()
"Test behavior of default binding `M-j'."
;; Calling `M-j' when point is not in an Org comment:
(should
(equal "* Some heading\n"
(org-test-with-temp-text "* Some heading<point>"
(call-interactively #'default-indent-new-line)
(buffer-string))))
;; Calling `M-j' when point is in an Org comment:
(should
(equal "# Some Org comment\n# "
(org-test-with-temp-text "# Some Org comment<point>"
(call-interactively #'default-indent-new-line)
(buffer-string))))
(should
(equal "# Some Org\n# comment"
(org-test-with-temp-text "# Some Org <point>comment"
(call-interactively #'default-indent-new-line)
(buffer-string)))))
;;; Editing ;;; Editing