-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathhtml-mode-expansions.el
104 lines (86 loc) · 3.49 KB
/
html-mode-expansions.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
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
;;; html-mode-expansions.el --- HTML-specific expansions for expand-region -*- lexical-binding: t; -*-
;; Copyright (C) 2011-2023 Free Software Foundation, Inc
;; Author: Magnar Sveen <[email protected]>
;; Keywords: marking region
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Extra expansions for HTML that I've found useful so far:
;;
;; er/mark-html-attribute
;; er/mark-inner-tag
;; er/mark-outer-tag
;;
;; Feel free to contribute any other expansions for HTML at
;;
;; https://github.com/magnars/expand-region.el
;;; Code:
(require 'expand-region-core)
(require 'sgml-mode)
(defun er/mark-html-attribute ()
"Mark html-attribute.
Presumes that point is at the assignment part of attr=\"value\".
If point is inside the value-string, the quotes will be marked
first anyway. Does not support html-attributes with spaces
around the equal sign or unquoted attributes atm."
(interactive)
(when (or (looking-at "\\(\\s_\\|\\sw\\)*=")
(er/looking-back-exact "="))
(search-backward " ")
(forward-char 1)
(set-mark (point))
(search-forward "=")
(forward-sexp 1)
(exchange-point-and-mark)))
(defun er--looking-at-marked-tag ()
"Is point looking at a tag that is entirely marked?"
(and (looking-at "<")
(>= (mark)
(save-excursion
(sgml-skip-tag-forward 1)
(point)))))
(defun er--inside-tag-p ()
"Is point inside a tag?"
(save-excursion
(not (null (sgml-get-context)))))
(defun er/mark-outer-tag ()
"Mark from opening to closing tag, including the tags."
(interactive)
(when (and (er--inside-tag-p)
(or (not (looking-at "<"))
(er--looking-at-marked-tag)))
(goto-char (aref (car (last (sgml-get-context))) 2)))
(when (looking-at "<")
(set-mark (point))
(sgml-skip-tag-forward 1)
(exchange-point-and-mark)))
(defun er/mark-inner-tag ()
"Mark the contents of an open tag, not including the tags."
(interactive)
(goto-char (aref (car (last (sgml-get-context))) 3))
(set-mark (point))
(backward-char 1)
(sgml-skip-tag-forward 1)
(search-backward "</")
(exchange-point-and-mark))
(defun er/add-html-mode-expansions ()
"Adds HTML-specific expansions for buffers in html-mode"
(set (make-local-variable 'er/try-expand-list) (append
er/try-expand-list
'(er/mark-html-attribute
er/mark-inner-tag
er/mark-outer-tag))))
(er/enable-mode-expansions 'html-mode #'er/add-html-mode-expansions)
(er/enable-mode-expansions 'rhtml-mode #'er/add-html-mode-expansions)
(er/enable-mode-expansions 'nxhtml-mode #'er/add-html-mode-expansions)
(er/enable-mode-expansions 'web-mode #'er/add-html-mode-expansions)
(provide 'html-mode-expansions)
;; html-mode-expansions.el ends here