Subject: Imenu in markdown-ts-mode

From: Juri Linkov <[email-masked]>
Date: Thu, 20 Feb 2025 19:59:38 +0200

markdown-ts-mode uses the function as the second element of
treesit-simple-imenu-settings:

  (setq-local treesit-simple-imenu-settings
              `(("Headings" markdown-ts-imenu-node-p nil markdown-ts-imenu-name-function)))

This breaks treesit-outline-predicate--from-imenu since it expects only a string.

So I tried to fix this with the patch below.  But then noticed that the
REGEXP element of treesit-simple-imenu-settings is documented only as
a string:

  REGEXP should be a regexp matching the type of nodes that
  belong to CATEGORY.

So there is two possible variants:

1. Support a function by applying this patch and changing the
documentation of treesit-simple-imenu-settings;
2. Maybe markdown-ts-mode doesn't need to use a function
and it should be sufficient to use just "section" as string.
Then markdown-ts-imenu-name-function can extract "atx_heading" from it.

diff --git a/lisp/treesit.el b/lisp/treesit.el
index 30efd4d4599..6eaa7a761e3 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -3611,7 +3611,10 @@ treesit-outline-predicate--from-imenu
   ;; you can directly set a proper value to `treesit-outline-predicate'.
   (seq-some
    (lambda (setting)
-     (and (string-match-p (nth 1 setting) (treesit-node-type node))
+     (and (or (and (stringp (nth 1 setting))
+                   (string-match-p (nth 1 setting) (treesit-node-type node)))
+              (and (functionp (nth 1 setting))
+                   (funcall (nth 1 setting) node)))
           (or (null (nth 2 setting))
               (funcall (nth 2 setting) node))))
    treesit-simple-imenu-settings))