From a554aaa3db12b684b24fc103ee3efccdc5cfbb5b Mon Sep 17 00:00:00 2001 From: Juri Linkov Date: Mon, 5 May 2025 19:57:29 +0300 Subject: [PATCH] New variable 'comment-setup-function' for multi-language modes. * lisp/newcomment.el (comment-setup-function): New variable. (comment-normalize-vars): Call non-nil 'comment-setup-function'. * lisp/textmodes/mhtml-ts-mode.el (mhtml-ts-mode--comment-current-lang): New internal variable. (mhtml-ts-mode--comment-setup): New function. (mhtml-ts-mode): Set 'comment-setup-function' to 'mhtml-ts-mode--comment-setup' instead of using 'c-ts-common-comment-setup' only for JavaScript. https://lists.gnu.org/archive/html/emacs-devel/2025-05/msg00025.html (cherry picked from commit 61cb73a2dbe756b060d4256c3b7ebe09f71ed2b7) --- lisp/newcomment.el | 5 +++++ lisp/textmodes/mhtml-ts-mode.el | 23 +++++++++++++++++++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/lisp/newcomment.el b/lisp/newcomment.el index 4da3b55d2f0..a6a1b7340a6 100644 --- a/lisp/newcomment.el +++ b/lisp/newcomment.el @@ -344,6 +344,9 @@ terminated by the end of line (i.e., `comment-end' is empty)." "Return the mirror image of string S, without any trailing space." (comment-string-strip (concat (nreverse (string-to-list s))) nil t)) +(defvar comment-setup-function nil + "Function to set up variables needed by commenting functions.") + ;;;###autoload (defun comment-normalize-vars (&optional noerror) "Check and set up variables needed by other commenting functions. @@ -351,6 +354,8 @@ All the `comment-*' commands call this function to set up various variables, like `comment-start', to ensure that the commenting functions work correctly. Lisp callers of any other `comment-*' function should first call this function explicitly." + (when (functionp comment-setup-function) + (funcall comment-setup-function)) (unless (and (not comment-start) noerror) (unless comment-start (let ((cs (read-string "No comment syntax is defined. Use: "))) diff --git a/lisp/textmodes/mhtml-ts-mode.el b/lisp/textmodes/mhtml-ts-mode.el index 6285d730efb..d001ab61a53 100644 --- a/lisp/textmodes/mhtml-ts-mode.el +++ b/lisp/textmodes/mhtml-ts-mode.el @@ -356,6 +356,26 @@ Return nil if there is no name or if NODE is not a defun node." (js-name js-name) (css-name css-name)))) +(defvar-local mhtml-ts-mode--comment-current-lang nil) + +(defun mhtml-ts-mode--comment-setup () + (let ((lang (treesit-language-at (point)))) + (unless (eq mhtml-ts-mode--comment-current-lang lang) + (setq mhtml-ts-mode--comment-current-lang lang) + (pcase lang + ('html + (setq-local comment-start "") + (setq-local comment-end-skip nil)) + ('css + (setq-local comment-start "/*") + (setq-local comment-start-skip "/\\*+[ \t]*") + (setq-local comment-end "*/") + (setq-local comment-end-skip "[ \t]*\\*+/")) + ('javascript + (c-ts-common-comment-setup)))))) + ;;; Flymake integration (defvar-local mhtml-ts-mode--flymake-process nil @@ -430,9 +450,8 @@ Powered by tree-sitter." ;; just like it's done in the original mode. ;; Comment. - ;; indenting settings for js-ts-mode. - (c-ts-common-comment-setup) (setq-local comment-multi-line t) + (setq-local comment-setup-function #'mhtml-ts-mode--comment-setup) ;; Font-lock. -- 2.39.5