1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
| | ;;; mhtml-mode.el --- HTML editing mode that handles CSS and JS -*- lexical-binding:t -*-
;; Copyright (C) 2017 Free Software Foundation, Inc.
;; Keywords: wp, hypermedia, comm, languages
;; 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 <http://www.gnu.org/licenses/>.
;;; Code:
(eval-and-compile
(require 'sgml-mode)
(require 'smie))
(require 'js)
(require 'css-mode)
(require 'prog-mode)
(cl-defstruct mhtml--submode
;; Name of this mode.
name
;; HTML end tag.
end-tag
;; Syntax table.
syntax-table
;; Propertize function.
propertize
;; Indentation function.
indenter
;; Keymap.
keymap)
(defconst mhtml--css-submode
(make-mhtml--submode :name "CSS"
:end-tag "</style>"
:syntax-table css-mode-syntax-table
:propertize css-syntax-propertize-function
:indenter #'css-advertized-indent-line
:keymap css-mode-map))
(defconst mhtml--js-submode
(make-mhtml--submode :name "JS"
:end-tag "</script>"
:syntax-table js-mode-syntax-table
:propertize #'js-syntax-propertize
:indenter #'js-indent-line
:keymap js-mode-map))
(defun mhtml--submode-lighter ()
"Mode-line lighter indicating the current submode."
(let ((submode (get-text-property (point) 'mhtml-submode)))
(if submode
(mhtml--submode-name submode)
"")))
(defun mhtml--syntax-propertize-submode (submode end)
(save-excursion
(when (search-forward (mhtml--submode-end-tag submode) end t)
(setq end (match-beginning 0))))
(set-text-properties (point) end
(list 'mhtml-submode submode
'syntax-table (mhtml--submode-syntax-table submode)
;; We want local-map here so that we act
;; more like the sub-mode and don't
;; override minor mode maps.
'local-map (mhtml--submode-keymap submode)
'cursor-sensor-functions
(list (lambda (_window _old-point _action)
(force-mode-line-update)))))
(funcall (mhtml--submode-propertize submode) (point) end)
(goto-char end))
(defun mhtml-syntax-propertize (start end)
(goto-char start)
(when (get-text-property (point) 'mhtml-submode)
(mhtml--syntax-propertize-submode (get-text-property (point) 'mhtml-submode)
end))
(funcall
(syntax-propertize-rules
("<style.*?>"
(0 (ignore
(goto-char (match-end 0))
(mhtml--syntax-propertize-submode mhtml--css-submode end))))
("<script.*?>"
(0 (ignore
(goto-char (match-end 0))
(mhtml--syntax-propertize-submode mhtml--js-submode end))))
sgml-syntax-propertize-rules)
;; Make sure to handle the situation where
;; mhtml--syntax-propertize-submode moved point.
(point) end))
(defun mhtml-indent-line ()
"Indent the current line as HTML, JS, or CSS, according to its context."
(interactive)
(let ((submode (save-excursion
(back-to-indentation)
(get-text-property (point) 'mhtml-submode))))
(if submode
(save-restriction
(let* ((region-start (previous-single-property-change (point)
'mhtml-submode))
(base-indent (save-excursion
(goto-char region-start)
(sgml-calculate-indent))))
(narrow-to-region region-start (point-max))
(let ((prog-indentation-context (list base-indent
(cons (point-min) nil)
nil)))
(funcall (mhtml--submode-indenter submode)))))
;; HTML.
(sgml-indent-line))))
;;;###autoload
(define-derived-mode mhtml-mode html-mode
'((sgml-xml-mode "XHTML+" "HTML+") (:eval (mhtml--submode-lighter)))
"Major mode based on `html-mode', but works with embedded JS and CSS.
Code inside a <script> element is indented using the rules from
`js-mode'; and code inside a <style> element is indented using
the rules from `css-mode'."
(cursor-sensor-mode)
(setq-local indent-line-function #'mhtml-indent-line)
(setq-local parse-sexp-lookup-properties t)
(setq-local syntax-propertize-function #'mhtml-syntax-propertize)
(add-hook 'syntax-propertize-extend-region-functions
#'syntax-propertize-multiline 'append 'local))
(provide 'mhtml-mode)
;;; mhtml-mode.el ends here
|