forked from deactivated/multi-mode
/
html-js.el
62 lines (52 loc) · 2.31 KB
/
html-js.el
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
;;; html-js.el --- multi-mode PHP embedded in HTML
;; Copyright (C) 2008 Dave Love
;; Author: Andreas Raster <lazor@affenbande.org>
;; URL: http://github.com/rakete
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
(require 'multi-mode)
(require 'multi-util)
(defun html-js-mode ()
"Mode for editing PHP embedded in HTML, using multi-mode."
(interactive)
(set (make-local-variable 'multi-alist)
'((html-mode)
(javascript-mode . html-js-chunk-region)))
(add-hook 'multi-indirect-buffer-hook
(lambda ()
(when (eq major-mode 'html-mode)
(setq indent-line-function 'sgml-indent-line)))
t t)
(multi-mode-install-modes))
(defun html-js-chunk-region (pos)
(interactive)
(let* ((re-start "<script[^>]*>")
(re-end "<\/script>")
(chunk-inside (condition-case nil (multi-mode-position-chunk pos re-start re-end) (error nil)))
(inside-start (cdr (assoc :inner-start chunk-inside)))
(inside-end (cdr (assoc :inner-end chunk-inside))))
(if (and chunk-inside
inside-start
inside-end
(/= inside-start inside-end)
(>= pos inside-start)
(< pos inside-end))
(list 'javascript-mode inside-start inside-end)
(let* ((chunk-outside (condition-case nil (multi-mode-position-chunk pos re-end re-start t) (error nil)))
(outside-start (cdr (assoc :outer-start chunk-outside)))
(outside-end (cdr (assoc :outer-end chunk-outside))))
(list 'html-mode outside-start outside-end)))))
(defun test-html-js ()
(interactive)
(print (html-js-chunk-region (point))))
(provide 'html-js)