org-lint-misplaced-heading: Reduce false-positive rate

* lisp/org-lint.el (org-lint-misplaced-heading): Be more strict
matching potential misplaced headings - only do it on another heading
and inside paragraphs.

Link: https://orgmode.org/list/87a5jv77qs.fsf@gmail.com
This commit is contained in:
Ihor Radchenko 2024-06-08 21:02:40 +02:00
parent 6c862699a6
commit 915e883645
No known key found for this signature in database
GPG Key ID: 6470762A7DA11D8B
1 changed files with 12 additions and 5 deletions

View File

@ -388,17 +388,24 @@ called with one argument, the key used for comparison."
(dolist (e originals reports) (funcall make-report (cdr e) (car e))))) (dolist (e originals reports) (funcall make-report (cdr e) (car e)))))
(defun org-lint-misplaced-heading (ast) (defun org-lint-misplaced-heading (ast)
"Check for accidentally misplaced heading lines." "Check for accidentally misplaced heading lines.
Example:
** Heading 1
** Heading 2** Oops heading 3
** Heading 4"
(org-with-point-at ast (org-with-point-at ast
(goto-char (point-min)) (goto-char (point-min))
(let (result) (let (result)
;; Heuristics for 2+ level heading not at bol. ;; Heuristics for 2+ level heading not at bol.
(while (re-search-forward (rx (not (any "*\n\r ,")) ;; Not a bol; not escaped ,** heading; not " *** words" (while (re-search-forward (rx (not (any "*\n\r ,")) ;; Not a bol; not escaped ,** heading; not " *** words"
"*" (1+ "*") " ") nil t) "*" (1+ "*") " ") nil t)
(unless (org-element-type-p ;; Limit false-positive rate by only complaining about
(org-element-at-point) ;; ** Heading** Heading and
'(comment-block example-block export-block src-block) ;; ** Oops heading
) ; Inside a block, where the chances to have heading a slim. ;; Paragraph** Oops heading
(when (org-element-type-p
(org-element-at-point)
'(paragraph headline))
(push (list (match-beginning 0) "Possibly misplaced heading line") result))) (push (list (match-beginning 0) "Possibly misplaced heading line") result)))
result))) result)))