* bug#60972: 30.0.50; [PATCH]: Add html-ts-mode
@ 2023-01-20 20:10 Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-20 20:28 ` Eli Zaretskii
2023-01-21 17:50 ` Juri Linkov
0 siblings, 2 replies; 7+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-20 20:10 UTC (permalink / raw)
To: 60972; +Cc: eliz
[-- Attachment #1: Type: text/plain, Size: 249 bytes --]
Hi!
Attached is a ts-mode for HTML support.
@Eli, is this ok for emacs-29, or should it go to master? If ok for 29
I'll remove the sentence/sexp-related stuff and commit that later on
master. Otherwise I'll just add everything to master.
Theo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Add-html-ts-mode.patch --]
[-- Type: text/x-patch, Size: 5015 bytes --]
From 370e8478723af410f240a20f8b6640bdaf0a6594 Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Fri, 20 Jan 2023 21:05:41 +0100
Subject: [PATCH] Add html-ts-mode
* lisp/textmodes/html-ts-mode.el: New major mode for HTML support
powered by Tree-sitter.
---
lisp/textmodes/html-ts-mode.el | 134 +++++++++++++++++++++++++++++++++
1 file changed, 134 insertions(+)
create mode 100644 lisp/textmodes/html-ts-mode.el
diff --git a/lisp/textmodes/html-ts-mode.el b/lisp/textmodes/html-ts-mode.el
new file mode 100644
index 0000000000..6016a3dd72
--- /dev/null
+++ b/lisp/textmodes/html-ts-mode.el
@@ -0,0 +1,134 @@
+;;; html-ts-mode.el --- tree-sitter support for HTML -*- lexical-binding: t; -*-
+
+;; Copyright (C) 2022-2023 Free Software Foundation, Inc.
+
+;; Author : Theodor Thornhill <theo@thornhill.no>
+;; Maintainer : Theodor Thornhill <theo@thornhill.no>
+;; Created : November 2022
+;; Keywords : html languages tree-sitter
+
+;; This file is part of GNU Emacs.
+
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
+;; GNU Emacs is distributed in the hope that it will be useful,
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
+
+;;; Commentary:
+;;
+
+;;; Code:
+
+(require 'treesit)
+(require 'sgml-mode)
+
+(declare-function treesit-parser-create "treesit.c")
+(declare-function treesit-node-type "treesit.c")
+
+(defcustom html-ts-mode-indent-offset 2
+ "Number of spaces for each indentation step in `html-ts-mode'."
+ :version "29.1"
+ :type 'integer
+ :safe 'integerp
+ :group 'html)
+
+(defvar html-ts-mode--indent-rules
+ `((html
+ ((parent-is "fragment") parent-bol 0)
+ ((node-is "/>") parent-bol 0)
+ ((node-is ">") parent-bol 0)
+ ((node-is "end_tag") parent-bol 0)
+ ((parent-is "comment") prev-adaptive-prefix 0)
+ ((parent-is "element") parent-bol html-ts-mode-indent-offset)
+ ((parent-is "script_element") parent-bol html-ts-mode-indent-offset)
+ ((parent-is "style_element") parent-bol html-ts-mode-indent-offset)
+ ((parent-is "start_tag") parent-bol html-ts-mode-indent-offset)
+ ((parent-is "self_closing_tag") parent-bol html-ts-mode-indent-offset)))
+ "Tree-sitter indent rules.")
+
+(defvar html-ts-mode--font-lock-settings
+ (treesit-font-lock-rules
+ :language 'html
+ :override t
+ :feature 'comment
+ `((comment) @font-lock-comment-face)
+ :language 'html
+ :override t
+ :feature 'keyword
+ `("doctype" @font-lock-keyword-face)
+ :language 'html
+ :override t
+ :feature 'definition
+ `((tag_name) @font-lock-function-name-face)
+ :language 'html
+ :override t
+ :feature 'string
+ `((quoted_attribute_value) @font-lock-string-face)
+ :language 'html
+ :override t
+ :feature 'property
+ `((attribute_name) @font-lock-variable-name-face))
+ "Tree-sitter font-lock settings for `html-ts-mode'.")
+
+(defun html-ts-mode--defun-name (node)
+ "Return the defun name of NODE.
+Return nil if there is no name or if NODE is not a defun node."
+ (when (equal (treesit-node-type node) "tag_name")
+ (treesit-node-text node t)))
+
+;;;###autoload
+(define-derived-mode html-ts-mode html-mode "HTML"
+ "Major mode for editing Html, powered by tree-sitter."
+ :group 'html
+
+ (unless (treesit-ready-p 'html)
+ (error "Tree-sitter for HTML isn't available"))
+
+ (treesit-parser-create 'html)
+
+ ;; Comments.
+ (setq-local treesit-text-type-regexp
+ (regexp-opt '("comment" "text")))
+
+ ;; Indent.
+ (setq-local treesit-simple-indent-rules html-ts-mode--indent-rules)
+
+ ;; Navigation.
+ (setq-local treesit-defun-type-regexp "element")
+
+ (setq-local treesit-defun-name-function #'html-ts-mode--defun-name)
+
+ (setq-local treesit-sentence-type-regexp
+ (regexp-opt '("start_tag"
+ "self_closing_tag"
+ "end_tag")))
+
+ (setq-local treesit-sexp-type-regexp
+ (regexp-opt '("tag"
+ "text"
+ "attribute"
+ "value")))
+
+ ;; Font-lock.
+ (setq-local treesit-font-lock-settings html-ts-mode--font-lock-settings)
+ (setq-local treesit-font-lock-feature-list
+ '((comment keyword definition)
+ (property string)
+ () ()))
+
+ ;; Imenu.
+ (setq-local treesit-simple-imenu-settings
+ '(("Element" "\\`tag_name\\'" nil nil)))
+ (treesit-major-mode-setup))
+
+(provide 'html-ts-mode)
+
+;;; html-ts-mode.el ends here
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* bug#60972: 30.0.50; [PATCH]: Add html-ts-mode
2023-01-20 20:10 bug#60972: 30.0.50; [PATCH]: Add html-ts-mode Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-01-20 20:28 ` Eli Zaretskii
2023-01-20 20:40 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-21 17:50 ` Juri Linkov
1 sibling, 1 reply; 7+ messages in thread
From: Eli Zaretskii @ 2023-01-20 20:28 UTC (permalink / raw)
To: Theodor Thornhill; +Cc: 60972
> Cc: eliz@gnu.org
> From: Theodor Thornhill <theo@thornhill.no>
> Date: Fri, 20 Jan 2023 21:10:56 +0100
>
> Attached is a ts-mode for HTML support.
Thanks.
> @Eli, is this ok for emacs-29, or should it go to master? If ok for 29
> I'll remove the sentence/sexp-related stuff and commit that later on
> master. Otherwise I'll just add everything to master.
Please install on master.
But please also add setting of auto-mode-alist, similarly to what we
do in other *-ts-mode's. And a NEWS entry.
^ permalink raw reply [flat|nested] 7+ messages in thread
* bug#60972: 30.0.50; [PATCH]: Add html-ts-mode
2023-01-20 20:28 ` Eli Zaretskii
@ 2023-01-20 20:40 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 0 replies; 7+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-20 20:40 UTC (permalink / raw)
To: Eli Zaretskii; +Cc: 60972
Eli Zaretskii <eliz@gnu.org> writes:
>> Cc: eliz@gnu.org
>> From: Theodor Thornhill <theo@thornhill.no>
>> Date: Fri, 20 Jan 2023 21:10:56 +0100
>>
>> Attached is a ts-mode for HTML support.
>
> Thanks.
>
>> @Eli, is this ok for emacs-29, or should it go to master? If ok for 29
>> I'll remove the sentence/sexp-related stuff and commit that later on
>> master. Otherwise I'll just add everything to master.
>
> Please install on master.
Will do :)
>
> But please also add setting of auto-mode-alist, similarly to what we
> do in other *-ts-mode's. And a NEWS entry.
Will do!
Theo
^ permalink raw reply [flat|nested] 7+ messages in thread
* bug#60972: 30.0.50; [PATCH]: Add html-ts-mode
2023-01-20 20:10 bug#60972: 30.0.50; [PATCH]: Add html-ts-mode Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-20 20:28 ` Eli Zaretskii
@ 2023-01-21 17:50 ` Juri Linkov
2023-01-21 19:08 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
1 sibling, 1 reply; 7+ messages in thread
From: Juri Linkov @ 2023-01-21 17:50 UTC (permalink / raw)
To: Theodor Thornhill; +Cc: 60972
> Attached is a ts-mode for HTML support.
Thanks. Could you please clarify the difference between
forward-sentence and forward-sexp in html-ts-mode.
I expected that one of them is moving over the HTML tags
and another over HTML elements.
^ permalink raw reply [flat|nested] 7+ messages in thread
* bug#60972: 30.0.50; [PATCH]: Add html-ts-mode
2023-01-21 17:50 ` Juri Linkov
@ 2023-01-21 19:08 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-22 17:02 ` Juri Linkov
0 siblings, 1 reply; 7+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-21 19:08 UTC (permalink / raw)
To: Juri Linkov; +Cc: 60972
[-- Attachment #1: Type: text/plain, Size: 334 bytes --]
Juri Linkov <juri@linkov.net> writes:
>> Attached is a ts-mode for HTML support.
>
> Thanks. Could you please clarify the difference between
> forward-sentence and forward-sexp in html-ts-mode.
> I expected that one of them is moving over the HTML tags
> and another over HTML elements.
Is this more like you were thinking?
Theo
[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Use-element-as-a-sentence-in-html-ts-mode.patch --]
[-- Type: text/x-patch, Size: 990 bytes --]
From a79b4f280c22e937a24f077f0aa7f5ea18a92a0c Mon Sep 17 00:00:00 2001
From: Theodor Thornhill <theo@thornhill.no>
Date: Sat, 21 Jan 2023 20:05:52 +0100
Subject: [PATCH] Use element as a sentence in html-ts-mode
* lisp/textmodes/html-ts-mode.el (html-ts-mode): Tweak the regexp.
---
lisp/textmodes/html-ts-mode.el | 5 +----
1 file changed, 1 insertion(+), 4 deletions(-)
diff --git a/lisp/textmodes/html-ts-mode.el b/lisp/textmodes/html-ts-mode.el
index 7e4360747a..a2d85bff7d 100644
--- a/lisp/textmodes/html-ts-mode.el
+++ b/lisp/textmodes/html-ts-mode.el
@@ -106,10 +106,7 @@ html-ts-mode
(setq-local treesit-defun-name-function #'html-ts-mode--defun-name)
- (setq-local treesit-sentence-type-regexp
- (regexp-opt '("start_tag"
- "self_closing_tag"
- "end_tag")))
+ (setq-local treesit-sentence-type-regexp "element")
(setq-local treesit-sexp-type-regexp
(regexp-opt '("tag"
--
2.34.1
^ permalink raw reply related [flat|nested] 7+ messages in thread
* bug#60972: 30.0.50; [PATCH]: Add html-ts-mode
2023-01-21 19:08 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
@ 2023-01-22 17:02 ` Juri Linkov
2023-01-22 18:20 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 1 reply; 7+ messages in thread
From: Juri Linkov @ 2023-01-22 17:02 UTC (permalink / raw)
To: Theodor Thornhill; +Cc: 60972
>> I expected that one of them is moving over the HTML tags
>> and another over HTML elements.
>
> Is this more like you were thinking?
>
> - (setq-local treesit-sentence-type-regexp
> - (regexp-opt '("start_tag"
> - "self_closing_tag"
> - "end_tag")))
> + (setq-local treesit-sentence-type-regexp "element")
According to the discussion in bug#60894 I expected it to be
rather like this:
(setq-local treesit-sentence-type-regexp "tag")
(setq-local treesit-sexp-type-regexp
(regexp-opt '("element"
"text"
"attribute"
"value")))
^ permalink raw reply [flat|nested] 7+ messages in thread
* bug#60972: 30.0.50; [PATCH]: Add html-ts-mode
2023-01-22 17:02 ` Juri Linkov
@ 2023-01-22 18:20 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
0 siblings, 0 replies; 7+ messages in thread
From: Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2023-01-22 18:20 UTC (permalink / raw)
To: Juri Linkov; +Cc: 60972
Juri Linkov <juri@linkov.net> writes:
>>> I expected that one of them is moving over the HTML tags
>>> and another over HTML elements.
>>
>> Is this more like you were thinking?
>>
>> - (setq-local treesit-sentence-type-regexp
>> - (regexp-opt '("start_tag"
>> - "self_closing_tag"
>> - "end_tag")))
>> + (setq-local treesit-sentence-type-regexp "element")
>
> According to the discussion in bug#60894 I expected it to be
> rather like this:
>
> (setq-local treesit-sentence-type-regexp "tag")
> (setq-local treesit-sexp-type-regexp
> (regexp-opt '("element"
> "text"
> "attribute"
> "value")))
Ok, let's try that :-)
Theo
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2023-01-22 18:20 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-20 20:10 bug#60972: 30.0.50; [PATCH]: Add html-ts-mode Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-20 20:28 ` Eli Zaretskii
2023-01-20 20:40 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-21 17:50 ` Juri Linkov
2023-01-21 19:08 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
2023-01-22 17:02 ` Juri Linkov
2023-01-22 18:20 ` Theodor Thornhill via Bug reports for GNU Emacs, the Swiss army knife of text editors
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).