-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhighlight-matching-tag.el
249 lines (215 loc) · 8.35 KB
/
highlight-matching-tag.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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
;;; highlight-matching-tag.el --- Highlight matching tag
;; Filename: highlight-matching-tag.el
;; Description: Highlight matching tag
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2019, Andy Stewart, all rights reserved.
;; Created: 2019-03-14 22:14:00
;; Version: 0.3
;; Last-Updated: 2019-06-28 19:46:30
;; By: Andy Stewart
;; URL: http://www.emacswiki.org/emacs/download/highlight-matching-tag.el
;; Keywords:
;; Compatibility: GNU Emacs 26.1.92
;;
;; Features that might be required by this library:
;;
;; `web-mode' `sgml-mode'
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; 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, 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; This plugin will highlight matching tag instantaneously.
;;
;;; Installation:
;;
;; Put highlight-matching-tag.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'highlight-matching-tag)
;; (highlight-matching-tag 1)
;;
;; No need more.
;;; Customize:
;;
;;
;;
;; All of the above can customize by:
;; M-x customize-group RET highlight-matching-tag RET
;;
;;; Change log:
;;
;; 2019/06/28
;; * Unmark tag overlay when cursor under comment.
;; * Fix error of `highlight-matching-tag-get-close-tag-bound'
;;
;; 2019/06/27
;; * First released.
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;; Require
(require 'web-mode)
(require 'sgml-mode)
;;; Code:
(defgroup highlight-matching-tag nil
"Highlight matching tag."
:group 'highlight-matching-tag)
(defface highlight-matching-tag-mark-face
'((t (:underline "#DAB645")))
"Face tag."
:group 'highlight-matching-tag)
(defun highlight-matching-tag-monitor-cursor-move ()
(when (derived-mode-p 'web-mode)
(unless (boundp 'highlight-matching-tag-cursor-position)
(set (make-local-variable 'highlight-matching-tag-cursor-position) -1))
(unless (equal (point) 'highlight-matching-tag-cursor-position)
(cond
((highlight-matching-tag-in-comment-p)
(highlight-matching-tag-unmark))
((highlight-matching-tag-in-open-tag-p)
(highlight-matching-tag-mark))
((highlight-matching-tag-in-close-tag-p)
(highlight-matching-tag-mark))
(t
(highlight-matching-tag-unmark))))
(set (make-local-variable 'highlight-matching-tag-cursor-position) (point))))
(defun highlight-matching-tag (&optional arg)
(if (> arg 0)
(add-hook 'post-command-hook #'highlight-matching-tag-monitor-cursor-move)
(remove-hook 'post-command-hook #'highlight-matching-tag-monitor-cursor-move)))
(defun highlight-matching-tag-is-marking ()
(and (boundp 'highlight-matching-tag-is-mark)
highlight-matching-tag-is-mark))
(defun highlight-matching-tag-mark ()
(highlight-matching-tag-unmark)
(let* ((open-tag-bound (highlight-matching-tag-get-open-tag-bound)))
(if open-tag-bound
(let* ((start-pos (nth 0 open-tag-bound))
(end-pos (nth 1 open-tag-bound)))
(set (make-local-variable 'highlight-matching-tag-open-overlay) (make-overlay start-pos end-pos))
(overlay-put highlight-matching-tag-open-overlay 'face 'highlight-matching-tag-mark-face))
(set (make-local-variable 'highlight-matching-tag-open-overlay) nil)))
(let ((close-tag-bound (highlight-matching-tag-get-close-tag-bound)))
(if close-tag-bound
(let* ((start-pos (nth 0 close-tag-bound))
(end-pos (nth 1 close-tag-bound)))
(set (make-local-variable 'highlight-matching-tag-close-overlay) (make-overlay start-pos end-pos))
(overlay-put highlight-matching-tag-close-overlay 'face 'highlight-matching-tag-mark-face))
(set (make-local-variable 'highlight-matching-tag-close-overlay) nil))))
(defun highlight-matching-tag-unmark ()
(when (and (boundp 'highlight-matching-tag-open-overlay)
highlight-matching-tag-open-overlay)
(delete-overlay highlight-matching-tag-open-overlay)
(set (make-local-variable 'highlight-matching-tag-open-overlay) nil))
(when (and (boundp 'highlight-matching-tag-close-overlay)
highlight-matching-tag-close-overlay)
(delete-overlay highlight-matching-tag-close-overlay)
(set (make-local-variable 'highlight-matching-tag-close-overlay) nil)))
(defun highlight-matching-tag-in-open-tag-p ()
(ignore-errors
(let ((open-tag-pos (save-excursion
(sgml-skip-tag-backward 1)
(point))))
(and (equal (line-number-at-pos open-tag-pos) (line-number-at-pos (point)))
(save-excursion
(search-backward-regexp "<" open-tag-pos t))
(not (save-excursion
(search-backward-regexp "\\s-" open-tag-pos t)))))))
(defun highlight-matching-tag-in-close-tag-p ()
(ignore-errors
(let ((close-tag-pos
(save-excursion
(web-mode-element-beginning)
(cond ((looking-at "<>")
(web-mode-tag-match))
(t
(sgml-skip-tag-forward 1)))
(search-backward-regexp "</")
(point))))
(and (equal (line-number-at-pos close-tag-pos) (line-number-at-pos (point)))
(save-excursion
(search-backward-regexp "</" close-tag-pos t))
(not (save-excursion
(search-backward-regexp "\\s-" close-tag-pos t)))))))
(defun highlight-matching-tag-get-open-tag-bound ()
(let* ((open-tag-pos (save-excursion
(web-mode-element-beginning)
(point)))
(start-pos (save-excursion
(goto-char open-tag-pos)
(forward-char 1)
(point)))
(end-pos (save-excursion
(goto-char start-pos)
(unless (looking-at ">")
(forward-symbol 1))
(point))))
(if (and start-pos end-pos)
(list start-pos end-pos)
nil)))
(defun highlight-matching-tag-get-close-tag-bound ()
(ignore-errors
(save-excursion
(let (open-tag-start-pos
close-tag-start-pos
close-tag-end-pos)
;; Move cursor to open tab beginning point.
(web-mode-element-beginning)
;; Record beginning point.
(setq open-tag-start-pos (point))
;; Jump to end tag.
(cond ((looking-at "<>")
(web-mode-tag-match))
(t
(sgml-skip-tag-forward 1)))
(search-backward-regexp "</")
(if (<= (point) open-tag-start-pos)
;; Return nil if end tag point small than start tag beginning point.
nil
;; Otherwise return close tag bound.
(setq close-tag-start-pos
(save-excursion
(forward-char 2)
(point)))
(setq close-tag-end-pos
(save-excursion
(goto-char close-tag-start-pos)
(unless (looking-at ">")
(forward-symbol 1))
(point)))
(list close-tag-start-pos close-tag-end-pos)
)))))
(defun highlight-matching-tag-current-parse-state ()
(let ((point (point)))
(beginning-of-defun)
(parse-partial-sexp (point) point)))
(defun highlight-matching-tag-in-comment-p (&optional state)
(save-excursion
(or (nth 4 (or state (highlight-matching-tag-current-parse-state)))
(eq (get-text-property (point) 'face) 'font-lock-comment-face)
(eq (get-text-property (point) 'face) 'web-mode-comment-face))))
(provide 'highlight-matching-tag)
;;; highlight-matching-tag.el ends here