Subject: Re: bug#60176: 29.0.60; Fallback file variable mode if treesitter is not usable

From: Juri Linkov <[email-masked]>
Date: Mon, 09 Jan 2023 19:30:20 +0200

>> 'major-mode-remap-alist' should be able to handle this requirement
>> since its design is similar to MIME.TYPES and MAILCAP.  But actually
>> MAILCAP supports an additional predicate TEST that is a command
>> executed to determine whether or not the mailcap line applies.
>>
>> So to completely cover all needs, 'major-mode-remap-alist' should
>> support a predicate as well.  For example,
>>
>> (setq major-mode-remap-alist '((c-mode #'treesit-available-p c-ts-mode)))
>
> The current syntax is rather
>
>     (setq major-mode-remap-alist '((c-mode . treesit-available-p c-ts-mode)))

Is it possible at least to make this forward-compatible to support
such a predicate in future?  To enable ts-modes only in some buffers
currently requires first to enable non-ts-mode, then conditionally ts-mode:

(add-hook 'find-file-hook
          (lambda ()
            (when (and (eq major-mode 'c-mode)
                       ;; Unless in internal buffers:
                       (not (string-prefix-p " " (buffer-name))))
              (c-ts-mode))))

With a predicate in major-mode-remap-alist it would immediately
enable the required mode:

(setq major-mode-remap-alist
      '((c-mode . (lambda () (not (string-prefix-p " " (buffer-name))))
                c-ts-mode)))

Such a predicate would be useful in many other cases.  For example,
by default ".pl" is assigned to perl-mode, but this would help to
reassign it to prolog-mode using some heuristics:

(setq major-mode-remap-alist
      '((perl-mode . (lambda () (string-match-p "src/prolog" buffer-file-name))
                prolog-mode)))