Subject: Re: bug#79180: 31.0.50; Customize-browse emits treesit warnings

From: Juri Linkov <[email-masked]>
Date: Fri, 08 Aug 2025 20:57:27 +0300

>> I have a patch that removes these warnings.
>> Should I post it here or on emacs-devel?
>
> Please post here, and thanks.  If it turns out a more general
> discussion is needed, we will start it on emacs-devel then.

This new option was discussed many times on emacs-devel,
and here it's finally implemented.  As an example the patch
demonstrates the required changes only for markdown-ts-mode, and
similarly the same changes will be applied to all other ts-modes:

diff --git a/lisp/textmodes/markdown-ts-mode.el b/lisp/textmodes/markdown-ts-mode.el
index a94d1590fa0..e44ee583edd 100644
--- a/lisp/textmodes/markdown-ts-mode.el
+++ b/lisp/textmodes/markdown-ts-mode.el
@@ -403,8 +403,14 @@ markdown-ts-mode
 
 (derived-mode-add-parents 'markdown-ts-mode '(markdown-mode))
 
-(if (treesit-ready-p 'markdown)
-    (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-ts-mode)))
+;;;###autoload
+(when (treesit-available-p)
+  (unless (fboundp 'markdown-mode)
+    (defalias 'markdown-mode #'ignore))
+  (unless (rassq 'markdown-mode auto-mode-alist)
+    (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode)))
+  (add-to-list 'treesit-mode-remap-alist
+               '(markdown-mode . markdown-ts-mode)))
 
 (provide 'markdown-ts-mode)
 ;;; markdown-ts-mode.el ends here
diff --git a/lisp/treesit.el b/lisp/treesit.el
index dc17515ff99..69b48e0402b 100644
--- a/lisp/treesit.el
+++ b/lisp/treesit.el
@@ -5397,6 +5397,35 @@ treesit-ensure-installed
         ;; Check that the grammar was installed successfully
         (treesit-ready-p lang))))
 
+;;; Treesit enabled modes
+
+(defun treesit--enabled-modes-type ()
+  `(choice (const :tag "Disable all automatic associations" nil)
+           (const :tag "Enable all available ts-modes" t)
+           (set :tag "List of enabled ts-modes"
+                ,@(sort (mapcar (lambda (m) `(const ,(cdr m)))
+                                treesit-mode-remap-alist)))))
+
+;;;###autoload
+(defcustom treesit-enabled-modes nil
+  "Specify what treesit modes to enable by default."
+  :type (treesit--enabled-modes-type)
+  :initialize #'custom-initialize-default
+  :set (lambda (sym val)
+         (mapc (lambda (m)
+                 (if (stringp (car m))
+                     (setq auto-mode-alist
+                           (if (or (eq val t) (memq (cdr m) val))
+                               (cons m auto-mode-alist)
+                             (delete m auto-mode-alist)))
+                   (setq major-mode-remap-alist
+                         (if (or (eq val t) (memq (cdr m) val))
+                             (cons m major-mode-remap-alist)
+                           (delete m major-mode-remap-alist)))))
+               treesit-mode-remap-alist)
+         (set-default sym val))
+  :version "31.1")
+
 ;;; Shortdocs
 
 (defun treesit--generate-shortdoc-examples ()
diff --git a/src/treesit.c b/src/treesit.c
index bb720589c85..e9dc6596d89 100644
--- a/src/treesit.c
+++ b/src/treesit.c
@@ -5299,6 +5299,16 @@ cons (REGEXP . FN), which is a combination of a regexp and a predicate
 buffer.  */);
   Vtreesit_languages_require_line_column_tracking = Qnil;
 
+  DEFVAR_LISP ("treesit-mode-remap-alist",
+	       Vtreesit_mode_remap_alist,
+	       doc:
+	       /* An alist remapping mode symbols.
+
+The value should be an alist of (MODE . TS-MODE).  This alist
+is used to modify the value of `major-mode-remap-alist'
+depending on the customization of `treesit-enabled-modes'.  */);
+  Vtreesit_mode_remap_alist = Qnil;
+
   staticpro (&Vtreesit_str_libtree_sitter);
   Vtreesit_str_libtree_sitter = build_string ("libtree-sitter-");
   staticpro (&Vtreesit_str_tree_sitter);