Skip to content

Commit

Permalink
[Fix sensorflo#17] Don't add non-title entries to imenu (sensorflo#18)
Browse files Browse the repository at this point in the history
Fixes sensorflo#17 by checking that 2-line titles have the same number of
under characters as the title text and doesn't look like the start
of an attribute block.

Also check when (adoc-title-descriptor) returns nil, which it will at
times when the regexes match but more stringent tests fail, a nil list
will be inserted into the imenu list, which it doesn't like much, so
this condition is checked for as well.

This also add a strict mode to adoc-title-descriptor so it can work for some of
the sloppy editing scenarios needed. Also make it conform to asciidoc
standard of allowing as many as two character difference in two line
titles, in strict mode.
  • Loading branch information
ambihelical authored Sep 19, 2022
1 parent 2c4847d commit 183e983
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
30 changes: 20 additions & 10 deletions adoc-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2402,9 +2402,12 @@ Returns nil if there was no xref found."
(> saved-point (match-end 0)))))
id)))

(defun adoc-title-descriptor()
(defun adoc-title-descriptor (&optional strict-match )
"Returns title descriptor of title point is in.
When STRICT-MATCH is t, and 2 line title is used, the lengths of the underline
text and title must not differ by more than 2 characters.
Title descriptor looks like this: (TYPE SUB-TYPE LEVEL TEXT START END)
0 TYPE: 1 fore one line title, 2 for two line title.
Expand Down Expand Up @@ -2435,11 +2438,17 @@ trailing delimiter ('== my title ==').
;; method ensuring the correct length of the underline, be aware that
;; due to adoc-adjust-title-del we sometimes want to find a title which has
;; the wrong underline length.
((or (looking-at (adoc-re-two-line-title (nth level adoc-two-line-title-del)))
(save-excursion
(forward-line -1)
(beginning-of-line)
(looking-at (adoc-re-two-line-title (nth level adoc-two-line-title-del)))))
((and (or (looking-at (adoc-re-two-line-title (nth level adoc-two-line-title-del)))
(save-excursion
(forward-line -1)
(beginning-of-line)
(looking-at (adoc-re-two-line-title (nth level adoc-two-line-title-del)))))
;; If strict-mode, expect title and underline text lengths to be at most +-2 characters different
(or (not strict-match)
(<= (abs (- (length (match-string 3))
(length (match-string 2))))
2))
(not (string-prefix-p "[" (match-string 2))))
(setq type 2)
(setq text (match-string 2))
(setq found t))
Expand Down Expand Up @@ -2650,12 +2659,13 @@ LOCAL-ATTRIBUTE-FACE-ALIST before it is looked up in
(goto-char 0)
(while (re-search-forward re-all-titles nil t)
(backward-char) ; skip backwards the trailing \n of a title
(let* ((descriptor (adoc-title-descriptor))
(let* ((descriptor (adoc-title-descriptor t))
(title-text (nth 3 descriptor))
(title-pos (nth 4 descriptor)))
(setq
index-alist
(cons (cons title-text title-pos) index-alist)))))
(unless (null title-text)
(setq
index-alist
(cons (cons title-text title-pos) index-alist))))))
(nreverse index-alist)))

(defvar adoc-mode-syntax-table
Expand Down
21 changes: 18 additions & 3 deletions test/adoc-mode-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -946,9 +946,24 @@ removed before TRANSFORM is evaluated.
(set-buffer (get-buffer-create "adoc-test"))
(insert "= document title\n"
"== chapter 1\n"
"[IMPORTANT]\n"
".Important announcement\n"
"====\n"
"This should not be a title\n"
"====\n"
"=== sub chapter 1.1\n"
"[source,rust]\n"
"----\n"
"// here is a comment\n"
"----\n"
"chapter 2\n"
"----------\n"
"[latexmath#einstein]\n"
"++++\n"
"\begin{equation}\n"
"e = mc^{2}\n"
"\end{equation}\n"
"++++\n"
"sub chapter 2.1\n"
"~~~~~~~~~~~~~~\n")
(should
Expand All @@ -957,9 +972,9 @@ removed before TRANSFORM is evaluated.
(list
(cons "document title" 1)
(cons "chapter 1" 18)
(cons "sub chapter 1.1" 31)
(cons "chapter 2" 51)
(cons "sub chapter 2.1" 72)))))
(cons "sub chapter 1.1" 104)
(cons "chapter 2" 169)
(cons "sub chapter 2.1" 262)))))
(kill-buffer "adoc-test")))

;; purpose
Expand Down

0 comments on commit 183e983

Please sign in to comment.