]> git.eshelyaron.com Git - emacs.git/commitdiff
New variable 'comment-setup-function' for multi-language modes.
authorJuri Linkov <juri@linkov.net>
Mon, 5 May 2025 16:57:29 +0000 (19:57 +0300)
committerEshel Yaron <me@eshelyaron.com>
Sat, 10 May 2025 08:50:46 +0000 (10:50 +0200)
* 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
lisp/textmodes/mhtml-ts-mode.el

index 4da3b55d2f0a757a1e0a5d3fdfffb74b7f6f8cbe..a6a1b7340a671f4de3c0ee67a3f6ade15cb47ee3 100644 (file)
@@ -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: ")))
index 6285d730efb2cb79d8abccc3813997f992f493a3..d001ab61a53b9600c7facad92a46d39c41949c14 100644 (file)
@@ -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-start-skip nil)
+         (setq-local comment-end " -->")
+         (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.