branch: elpa/adoc-mode
commit 745884359a1b8826ede2c4cfd2f0b5478953ac40
Author: sensorflo <sensor...@gmail.com>
Commit: sensorflo <sensor...@gmail.com>

    added imenu support
---
 adoc-mode-test.el | 22 ++++++++++++++++++++++
 adoc-mode.el      | 32 +++++++++++++++++++++++++++++++-
 2 files changed, 53 insertions(+), 1 deletion(-)

diff --git a/adoc-mode-test.el b/adoc-mode-test.el
index c88622efdd..51d7590c2e 100644
--- a/adoc-mode-test.el
+++ b/adoc-mode-test.el
@@ -921,6 +921,28 @@ removed before TRANSFORM is evaluated.
     (adoctest-trans "" "  x"   '(adoc-insert-indented "x" 1))
     (adoctest-trans "" "\t  x" '(adoc-insert-indented "x" 2))))
 
+(ert-deftest adoctest-test-imenu-create-index ()
+  (unwind-protect
+      (progn
+        (set-buffer (get-buffer-create "adoc-test"))
+        (insert "= document title\n"
+                "== chapter 1\n"
+                "=== sub chapter 1.1\n"
+                "chapter 2\n"
+                "----------\n"
+                "sub chapter 2.1\n"
+                "~~~~~~~~~~~~~~\n")
+        (should
+         (equal
+          (adoc-imenu-create-index)
+          (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)))))
+    (kill-buffer "adoc-test")))
+
 ;; purpose
 ;; - ensure that the latest version, i.e. the one currently in buffer(s), of
 ;;   adoc-mode and adoc-mode-test is used for the test
diff --git a/adoc-mode.el b/adoc-mode.el
index 46ca7c2324..87e9650664 100644
--- a/adoc-mode.el
+++ b/adoc-mode.el
@@ -155,7 +155,7 @@
 ;; - Other common Emacs functionality/features
 ;;   - demote/promote/create/delete titles/list-items. Also put emphasis on a
 ;;     convenient simple user interface.
-;;   - imenu / hideshow
+;;   - hideshow
 ;;   - outline mode shall support two line titles
 ;;   - tags tables for anchors, indixes, bibliography items, titles, ...
 ;;   - spell check shall ignore meta characters
@@ -2933,6 +2933,31 @@ LOCAL-ATTRIBUTE-FACE-ALIST before it is looked up in
                     (cdr (assoc name adoc-attribute-face-alist))))
        markup-value-face)))
 
+(defun adoc-imenu-create-index ()
+  (let* ((index-alist)
+         (re-all-titles-core
+          (mapconcat
+           (lambda (level)
+             (concat
+              (adoc-re-one-line-title level)
+              "\\|"
+              (adoc-re-two-line-title (nth level adoc-two-line-title-del))))
+           '(0 1 2 3 4)
+           "\\)\\|\\(?:"))
+         (re-all-titles
+          (concat "\\(?:" re-all-titles-core "\\)")))
+    (save-restriction
+      (widen)
+      (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))
+               (title-text (nth 3 descriptor))
+               (title-pos (nth 4 descriptor)))
+          (setq
+           index-alist
+           (cons (cons title-text title-pos) index-alist)))))
+    (nreverse index-alist)))
 
 
 ;;;###autoload
@@ -2994,6 +3019,11 @@ Turning on Adoc mode runs the normal hook 
`adoc-mode-hook'."
   (set (make-local-variable 'require-final-newline) t)
   (set (make-local-variable 'parse-sexp-lookup-properties) t)
   
+  ;; it's the user's decision whether he wants to set imenu-sort-function to
+  ;; nil, or even something else. See also similar comment in sgml-mode.
+  (set (make-local-variable 'imenu-create-index-function)
+       'adoc-imenu-create-index)
+
   ;; compilation
   (when (boundp 'compilation-error-regexp-alist-alist)
     (add-to-list 'compilation-error-regexp-alist-alist

Reply via email to