emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/adoc-mode a08c7f26cc 053/199: bugfixes for promote / denot


From: ELPA Syncer
Subject: [nongnu] elpa/adoc-mode a08c7f26cc 053/199: bugfixes for promote / denote title
Date: Sun, 3 Sep 2023 06:59:33 -0400 (EDT)

branch: elpa/adoc-mode
commit a08c7f26cc8094a940a790b7c3def2274b2f0415
Author: Florian Kaufmann <sensorflo@gmail.com>
Commit: Florian Kaufmann <sensorflo@gmail.com>

    bugfixes for promote / denote title
---
 adoc-mode-test.el | 47 +++++++++++++++++++++++++++++++++++++++++++++++
 adoc-mode.el      | 53 ++++++++++++++++++++++++++++++++++++++++++-----------
 2 files changed, 89 insertions(+), 11 deletions(-)

diff --git a/adoc-mode-test.el b/adoc-mode-test.el
index 17aa293fd8..d72a663edf 100644
--- a/adoc-mode-test.el
+++ b/adoc-mode-test.el
@@ -57,6 +57,33 @@
     ;; tear-down
     (kill-buffer buf-name)))
 
+(defun adoctest-trans (original-text expected-text transform-fn &optional args)
+  (let ((pos 0)
+       (line 0))
+    (while (and (< pos (length original-text))
+               (setq pos (string-match "\n\\|\\'" original-text pos)))
+      (adoctest-trans-inner original-text expected-text transform-fn args line)
+      (setq line (1+ line))
+      (setq pos (1+ pos)))))
+
+(defun adoctest-trans-inner (original-text expected-text transform-fn args 
line)
+  (let ((not-done t)
+       (font-lock-support-mode))
+    (unwind-protect
+       (progn
+         ;; setup
+         (set-buffer (get-buffer-create "adoctest-trans")) 
+         (delete-region (point-min) (point-max))
+         (adoc-mode)
+         (insert original-text)
+         (goto-line line)
+         ;; exercise
+         (funcall transform-fn args)
+         ;; verify
+         (should (string-equal (buffer-substring (point-min) (point-max)) 
expected-text))))
+    ;; tear-down
+    (kill-buffer "adoctest-trans")))
+
 (ert-deftest adoctest-test-titles-simple-one-line-before ()
   (adoctest-faces "titles-simple-one-line-before"
    "= " markup-meta-hide-face "document title" markup-title-0-face "\n" nil
@@ -461,6 +488,26 @@
    "lorem ** ipsum " markup-gen-face "::" markup-list-face " " nil "sit ** 
dolor\n" 'no-face
    "lorem ** ipsum " markup-gen-face "::" markup-list-face " " nil "sit ** 
dolor" 'no-face))
 
+(ert-deftest adoctest-test-promote-title ()
+  (adoctest-trans "= foo" "== foo" 'adoc-promote-title 1)
+  (adoctest-trans "===== foo" "= foo" 'adoc-promote-title 1)
+  (adoctest-trans "== foo" "==== foo" 'adoc-promote-title 2)
+
+  (adoctest-trans "= foo =" "== foo ==" 'adoc-promote-title 1)
+  (adoctest-trans "===== foo =====" "= foo =" 'adoc-promote-title 1)
+  (adoctest-trans "== foo ==" "==== foo ====" 'adoc-promote-title 2)
+
+  (adoctest-trans "foo\n===" "foo\n---" 'adoc-promote-title 1)
+  (adoctest-trans "foo\n+++" "foo\n===" 'adoc-promote-title 1)
+  (adoctest-trans "foo\n---" "foo\n^^^" 'adoc-promote-title 2))
+
+;; since it's a whitebox test we know denote and promote only differ by inverse
+;; arg. So denote doesn't need to be throuhly tested again
+(ert-deftest adoctest-test-denote-title ()
+  (adoctest-trans "= foo" "===== foo" 'adoc-denote-title 1)
+  (adoctest-trans "= foo =" "===== foo =====" 'adoc-denote-title 1)
+  (adoctest-trans "foo\n===" "foo\n+++" 'adoc-denote-title 1))
+
 (ert-deftest adoctest-pre-test-byte-compile ()
   ;; todo: also test for warnings
   (should (byte-compile-file (locate-library "adoc-mode.el" t)))
diff --git a/adoc-mode.el b/adoc-mode.el
index 51240ab2be..d9c6d3648b 100644
--- a/adoc-mode.el
+++ b/adoc-mode.el
@@ -422,11 +422,37 @@ match-data has this sub groups:
   (let ((del (make-string (+ level 1) ?=)))
     (concat del " " text (when (eq sub-type 2) (concat " " del)))))   
 
-;; for first line, 2nd line is not a regex but python code
+;; AsciiDoc handles that by source code, there is no regexp in AsciiDoc
+(defun adoc-re-two-line-title-undlerline (&optional del)
+  "Returns a regexp matching the underline of a two line title.
+
+DEL is an element of `adoc-two-line-title-del' or nil. If nil,
+any del is matched.
+
+Note that even if this regexp matches it still doesn't mean it is
+a two line title underline, see also `adoc-re-two-line-title'."
+  (concat
+   "\\("
+   (mapconcat
+    (lambda(x)
+      (concat
+       "\\(?:"
+       "\\(?:" (regexp-quote x) "\\)+" 
+       (regexp-quote (substring x 0 1)) "?"
+       "\\)"))
+    (if del (list del) adoc-two-line-title-del) "\\|")          
+   "\\)[ \t]*$"))
+
+;; asciidoc.conf regexps for _first_ line
 ;; ^(?P<title>.*?)$   
 (defun adoc-re-two-line-title (del)
-  "Note that even if this regexp matches it still doesn't mean it is a two 
line title.
-You additionaly have to test if the underline has the correct length.
+  "Returns a regexps that matches a two line title.
+
+Note that even if this regexp matches it still doesn't mean it is
+a two line title. You additionaly have to test if the underline
+has the correct length.
+
+DEL is described in `adoc-re-two-line-title-undlerline'.
 
 match-data has his this sub groups:
 1 title's text
@@ -435,13 +461,11 @@ match-data has his this sub groups:
   (when (not (eq (length del) 2))
     (error "two line title delimiters must be 2 chars long"))
   (concat
-   ;; title text (the first line) must contain at least one \w character. You
-   ;; don't see that in asciidoc.conf, only in asciidoc source code.
+   ;; 1st line: title text must contain at least one \w character. You don't 
see
+   ;; that in asciidoc.conf, only in asciidoc source code.
    "\\(^.*?[a-zA-Z0-9_].*?\\)[ \t]*\n" 
-   "\\("
-     "\\(?:" (regexp-quote del) "\\)+"
-     (regexp-quote (substring del 0 1)) "?"   
-   "\\)[ \t]*$" ))
+   ;; 2nd line: underline
+   (adoc-re-two-line-title-undlerline del)))
 
 (defun adoc-make-two-line-title (del text)
   "Returns a two line title using given DEL containing given TEXT."
@@ -1664,7 +1688,7 @@ anchors in the [[id]] style."
   (re-search-forward (concat "^\\[\\[" (match-string 1) "\\]\\]")))
 
 (defun adoc-promote-title (&optional arg)
-  "Promotes the title point is on ARG levels.
+  "Promotes the title at point ARG levels.
 
 When ARG is nil (i.e. when no prefix arg is given), it defaults
 to 1. When ARG is negative, level is denoted that many levels. If
@@ -1756,7 +1780,11 @@ 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.
-         ((looking-at (adoc-re-two-line-title (nth level 
adoc-two-line-title-del)))
+         ((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)))))
           (setq type 2)
           (setq text (match-string 1))
           (setq found t))
@@ -1846,6 +1874,9 @@ and title's text are not preserved, afterwards its always 
one space."
       (setcar (nthcdr 1 descriptor) new-sub-type-val)
       (setcar (nthcdr 2 descriptor) new-level)
       (beginning-of-line)
+      (when (and (eq type 2) (looking-at (adoc-re-two-line-title-undlerline)))
+       (forward-line -1)
+       (beginning-of-line))
       (delete-region start end)
       (insert (adoc-make-title descriptor))
       (when (eq new-type-val 2)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]