-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathauto-rename-tag.el
464 lines (384 loc) · 17.2 KB
/
auto-rename-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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
;;; auto-rename-tag.el --- Automatically rename paired HTML/XML tag -*- lexical-binding: t; -*-
;; Copyright (C) 2018-2025 Shen, Jen-Chieh
;; Created date 2018-12-01 23:56:15
;; Author: Shen, Jen-Chieh <[email protected]>
;; URL: https://github.com/emacs-vs/auto-rename-tag
;; Version: 0.3.5
;; Package-Requires: ((emacs "24.4"))
;; Keywords: convenience auto-complete html rename tag xml
;; This file is NOT part of GNU Emacs.
;; 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 <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;; Automatically rename paired HTML/XML tag.
;;
;;; Code:
(require 'cl-lib)
(defgroup auto-rename-tag nil
"Automatically rename paired HTML/XML tag."
:prefix "auto-rename-tag-"
:group 'tool
:link '(url-link :tag "Repository" "https://github.com/emacs-vs/auto-rename-tag"))
(defconst auto-rename-tag--tag-regexp "<[^>]*"
"Tag regular expression to find tag position.")
(defcustom auto-rename-tag-disabled-commands
'(query-replace
query-replace-regexp
replace-string
replace-regexp)
"List of disabled commands."
:type 'list
:group 'auto-rename-tag)
(defcustom auto-rename-tag-disabled-minor-modes
'(iedit-mode)
"List of disabled minor modes."
:type 'list
:group 'auto-rename-tag)
(defvar-local auto-rename-tag--pre-command-activated nil
"Check if `pre-command-hook' called.")
(defvar-local auto-rename-tag--record-prev-word ""
"Record down the word in `pre-command-hook'.")
;;; Util
(defun auto-rename-tag--is-beginning-of-buffer-p ()
"Is at the beginning of buffer?"
(= (point) (point-min)))
(defun auto-rename-tag--is-end-of-buffer-p ()
"Is at the end of buffer?"
(= (point) (point-max)))
(defun auto-rename-tag--current-char-equal-p (c)
"Check the current character equal to 'C'."
(if (or (auto-rename-tag--is-beginning-of-buffer-p)
(auto-rename-tag--is-end-of-buffer-p))
nil
(let ((current-char-string (string (char-before))))
(string= current-char-string c))))
;;; Core
(defun auto-rename-tag--delete-tag-name ()
"Delete the current tag name."
(let ((tag-start (auto-rename-tag--tag-name-start-pt))
(tag-end (auto-rename-tag--tag-name-end-pt)))
(delete-region tag-start tag-end)))
(defun auto-rename-tag--is-closing-tag-p ()
"Check if current tag a closing tag."
(save-excursion
(when (auto-rename-tag--current-char-equal-p "<")
(forward-char 1))
(auto-rename-tag--current-char-equal-p "/")))
(defun auto-rename-tag--inside-tag-p ()
"Check if current point inside the tag."
(let ((backward-less (save-excursion (search-backward "<" nil t)))
(backward-greater (save-excursion (search-backward ">" nil t)))
(forward-less (save-excursion (search-forward "<" nil t)))
(forward-greater (save-excursion (search-forward ">" nil t))))
(unless backward-less (setq backward-less -1))
(unless backward-greater (setq backward-greater -1))
(unless forward-less (setq forward-less -1))
(unless forward-greater (setq forward-greater -1))
(and (not (= -1 backward-less))
(not (= -1 forward-greater))
(< backward-greater backward-less)
(or (< forward-greater forward-less)
(= -1 forward-less)))))
(defun auto-rename-tag--self-tag-p ()
"Tag that can be use individually."
(if (auto-rename-tag--inside-tag-p)
(save-excursion
(auto-rename-tag--goto-the-start-of-tag-name)
(re-search-forward "/[ \t\n]*>" (auto-rename-tag--end-tag-point) t))
nil))
(defun auto-rename-tag--goto-backward-tag ()
"Goto the backward tag."
(when (auto-rename-tag--inside-tag-p)
(re-search-backward auto-rename-tag--tag-regexp nil t))
(forward-char -1)
(if (re-search-backward auto-rename-tag--tag-regexp nil t)
(forward-char 1)
(goto-char (point-min))))
(defun auto-rename-tag--goto-forward-tag ()
"Goto the forward tag."
(let ((search-back-result nil) (first-tag nil))
(save-excursion
(re-search-forward auto-rename-tag--tag-regexp nil t)
(forward-char -1)
(setq search-back-result (re-search-backward auto-rename-tag--tag-regexp nil t))
(setq first-tag (null search-back-result)))
(if first-tag
(if (re-search-forward auto-rename-tag--tag-regexp nil t)
(if (re-search-backward auto-rename-tag--tag-regexp nil t)
(forward-char 1)
(goto-char (point-max)))
(goto-char (point-max)))
(if (re-search-forward auto-rename-tag--tag-regexp nil t)
(if (re-search-backward auto-rename-tag--tag-regexp nil t)
(forward-char 1)
(goto-char (point-max)))
(goto-char (point-max))))))
(defun auto-rename-tag--goto-backward-tag-name (name)
"Goto the backward tag name 'NAME'."
(let ((current-word ""))
(auto-rename-tag--goto-backward-tag)
(unless (auto-rename-tag--is-beginning-of-buffer-p)
(setq current-word (auto-rename-tag--get-tag-name-at-point))
;; Ensure `current-word'/name is something other than nil.
(unless current-word (setq current-word ""))
(unless name (setq name ""))
(unless (string= current-word name)
(auto-rename-tag--goto-backward-tag-name name)))))
(defun auto-rename-tag--goto-forward-tag-name (name)
"Goto the forward tag name 'NAME'."
(let ((current-word ""))
(auto-rename-tag--goto-forward-tag)
(unless (auto-rename-tag--is-end-of-buffer-p)
(setq current-word (auto-rename-tag--get-tag-name-at-point))
;; Ensure `current-word'/name is something other than nil.
(unless current-word (setq current-word ""))
(unless name (setq name ""))
(unless (string= current-word name)
(auto-rename-tag--goto-forward-tag-name name)))))
(defun auto-rename-tag--backward-count-nested-close-tag (name &optional nc dnc)
"Search backward, return the count of the nested closing tag.
NAME : target word/tag name to check nested.
NC : recursive nested count.
DNC : duplicate nested count."
(save-excursion
(let ((nested-count (if nc nc 0)) (dup-nested-count (if dnc dnc 0))
(current-word "") (is-end-tag nil))
(auto-rename-tag--goto-backward-tag)
(unless (auto-rename-tag--is-beginning-of-buffer-p)
(setq is-end-tag (auto-rename-tag--is-closing-tag-p))
(setq current-word (auto-rename-tag--get-tag-name-at-point))
;; Ensure `current-word'/name is something other than nil.
(unless current-word (setq current-word ""))
(unless name (setq name ""))
;; Is the tag name aren't the same, we don't count up the nested level.
(unless (string= current-word name) (setq is-end-tag t))
;; If closing tag.
(when is-end-tag
(when (string= current-word name)
(setq nested-count (+ nested-count 1)) ; Count here.
(setq dup-nested-count (+ dup-nested-count 1)))
(setq nested-count
(auto-rename-tag--backward-count-nested-close-tag
name nested-count dup-nested-count)))
;; If opening tag.
(unless is-end-tag
(unless (= dup-nested-count 0)
(when (string= current-word name)
(setq dup-nested-count (- dup-nested-count 1))
(setq nested-count
(auto-rename-tag--backward-count-nested-close-tag
name nested-count dup-nested-count))))))
nested-count)))
(defun auto-rename-tag--forward-count-nested-open-tag (name &optional nc dnc)
"Search forward, return the count of the nested opening tag.
NAME : target word/tag name to check nested.
NC : recursive nested count.
DNC : duplicate nested count."
(save-excursion
(let ((nested-count (if nc nc 0)) (dup-nested-count (if dnc dnc 0))
(current-word "") (is-end-tag nil))
(auto-rename-tag--goto-forward-tag)
(unless (auto-rename-tag--is-end-of-buffer-p)
(setq is-end-tag (auto-rename-tag--is-closing-tag-p))
(setq current-word (auto-rename-tag--get-tag-name-at-point))
;; Ensure `current-word'/name is something other than nil.
(unless current-word (setq current-word ""))
(unless name (setq name ""))
;; Is the tag name aren't the same, we don't count up the nested level.
(unless (string= current-word name) (setq is-end-tag nil))
;; If closing tag.
(when is-end-tag
(unless (= dup-nested-count 0)
(when (string= current-word name)
(setq dup-nested-count (- dup-nested-count 1))
(setq nested-count
(auto-rename-tag--forward-count-nested-open-tag
name nested-count dup-nested-count)))))
;; If opening tag.
(unless is-end-tag
(when (string= current-word name)
(setq nested-count (+ nested-count 1)) ; Count here.
(setq dup-nested-count (+ dup-nested-count 1)))
(setq nested-count
(auto-rename-tag--forward-count-nested-open-tag
name nested-count dup-nested-count))))
nested-count)))
(defun auto-rename-tag--start-tag-point ()
"Return the point of the tag starting point."
(if (auto-rename-tag--inside-tag-p)
(save-excursion
(search-backward "<" nil t)
(point))
nil))
(defun auto-rename-tag--end-tag-point ()
"Return the point of the tag ending point."
(if (auto-rename-tag--inside-tag-p)
(save-excursion
;; TODO: There is some logic error about nested level.
;; Yet since, we are only going to use this for checking the
;; self tag. This logic error doesn't necessary has to be resolved!
(let ((start-tag-pt (auto-rename-tag--start-tag-point))
(nested-start-tag-pt nil))
(goto-char start-tag-pt)
(re-search-forward "[^=][ \t\n]*>" nil t)
(forward-char -1)
(setq nested-start-tag-pt (auto-rename-tag--start-tag-point))
(if (or (null nested-start-tag-pt)
(= start-tag-pt nested-start-tag-pt))
(progn
(forward-char 1)
(point))
(auto-rename-tag--end-tag-point))))
nil))
(defun auto-rename-tag--goto-the-start-of-tag-name ()
"Goto the start of the tag name, in order to get the name of the tag."
(re-search-backward "[<]" nil t)
(forward-char 1)
(when (auto-rename-tag--is-closing-tag-p)
(forward-char 1)))
(defun auto-rename-tag--goto-the-end-of-tag-name ()
"Goto the end of the tag name, in order to get the name of the tag."
(re-search-backward "[<]" nil t)
(re-search-forward "[^> \t\n]*" nil t))
(defun auto-rename-tag--tag-name-start-pt ()
"Return the point at the start of the tag name."
(save-excursion
(auto-rename-tag--goto-the-start-of-tag-name)
(point)))
(defun auto-rename-tag--tag-name-end-pt ()
"Return the point at the end of the tag name."
(save-excursion
(auto-rename-tag--goto-the-end-of-tag-name)
(point)))
(defun auto-rename-tag--get-tag-name-at-point ()
"Get the tag name at point."
(let ((tag-name ""))
(let ((tag-start (auto-rename-tag--tag-name-start-pt))
(tag-end (auto-rename-tag--tag-name-end-pt)))
(if (= tag-start tag-end)
(setq tag-name "")
(setq tag-name (buffer-substring-no-properties tag-start tag-end))))
tag-name))
(defun auto-rename-tag--resolve-nested (direct)
"Resolved nested level by DIRECT.
DIRECT can be either only 'backward and 'forward."
(let ((nested-count 0) (is-closing-tag nil))
;; Get nested count.
(setq nested-count
(cl-case direct
(backward (auto-rename-tag--backward-count-nested-close-tag auto-rename-tag--record-prev-word))
(forward (auto-rename-tag--forward-count-nested-open-tag auto-rename-tag--record-prev-word))))
;; Resolve nested.
(while (not (= nested-count 0))
(setq nested-count (- nested-count 1))
(cl-case direct
(backward
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word))
(forward
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word)
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word))))
;; Goto the target pair.
(cl-case direct
(backward
(auto-rename-tag--goto-backward-tag-name auto-rename-tag--record-prev-word))
(forward
(auto-rename-tag--goto-forward-tag-name auto-rename-tag--record-prev-word)
(ignore-errors (forward-char 1))))
(setq is-closing-tag (auto-rename-tag--is-closing-tag-p))
(cl-case direct
(backward
(when is-closing-tag (auto-rename-tag--resolve-nested direct)))
(forward
(unless is-closing-tag (auto-rename-tag--resolve-nested direct))))))
(defun auto-rename-tag--valid-do-p ()
"See if current change are valid to do rename tag action."
(and (not undo-in-progress)
(not (cl-some (lambda (m) (ignore-errors (symbol-value m))) auto-rename-tag-disabled-minor-modes))
(not (memq this-command auto-rename-tag-disabled-commands))
(auto-rename-tag--inside-tag-p)
(not (nth 4 (syntax-ppss))) ; check inside comment
(not (nth 8 (syntax-ppss))) ; check inside string
(not (auto-rename-tag--self-tag-p))))
(defun auto-rename-tag--before-action ()
"Before rename core action."
(setq auto-rename-tag--record-prev-word "" ; Reset record.
auto-rename-tag--pre-command-activated nil) ; Reset flag.
(when (auto-rename-tag--valid-do-p)
;; Set active flag.
(setq auto-rename-tag--pre-command-activated t)
(setq auto-rename-tag--record-prev-word (auto-rename-tag--get-tag-name-at-point))
(when (string= auto-rename-tag--record-prev-word "/")
(setq auto-rename-tag--record-prev-word ""))
;; Ensure `auto-rename-tag--record-prev-word' is something other than nil.
(unless auto-rename-tag--record-prev-word
(setq auto-rename-tag--record-prev-word ""))))
(defun auto-rename-tag--after-action ()
"After rename core action."
(when auto-rename-tag--pre-command-activated
(save-excursion
(let ((is-end-tag nil)
(current-word "") (pair-tag-word ""))
;; Goto the first character inside the tag.
(auto-rename-tag--goto-the-start-of-tag-name)
(setq is-end-tag (auto-rename-tag--is-closing-tag-p))
(setq current-word (auto-rename-tag--get-tag-name-at-point))
(unless (string= current-word auto-rename-tag--record-prev-word)
;; NOTE: Is closing tag.
(when is-end-tag
(ignore-errors (auto-rename-tag--resolve-nested 'backward))
;; Get the tag name and ready to be compare.
(setq pair-tag-word (auto-rename-tag--get-tag-name-at-point))
;; Ensure `pair-tag-word' is something other than nil.
(unless pair-tag-word (setq pair-tag-word ""))
(when (string= auto-rename-tag--record-prev-word pair-tag-word)
;; Delete the pair word.
(unless (string= pair-tag-word "")
(auto-rename-tag--delete-tag-name))
;; Insert new word.
(insert current-word)))
;; NOTE: Is opening tag.
(unless is-end-tag
(ignore-errors (auto-rename-tag--resolve-nested 'forward))
;; Get the tag name and ready to be compare.
(setq pair-tag-word (auto-rename-tag--get-tag-name-at-point))
;; Ensure `pair-tag-word' is something other than nil.
(unless pair-tag-word (setq pair-tag-word ""))
(when (string= auto-rename-tag--record-prev-word pair-tag-word)
;; Delete the pair word.
(unless (string= pair-tag-word "")
(auto-rename-tag--delete-tag-name))
;; Insert new word.
(insert current-word))))))))
(defun auto-rename-tag--before-change (&rest _args)
"Do stuff before buffer is changed."
(auto-rename-tag--before-action))
(defun auto-rename-tag--after-change (&rest _args)
"Do stuff after buffer is changed."
(auto-rename-tag--after-action))
;; Entry
(defun auto-rename-tag--enable ()
"Enable `auto-rename-tag' in current buffer."
(add-hook 'before-change-functions #'auto-rename-tag--before-change nil t)
(add-hook 'after-change-functions #'auto-rename-tag--after-change nil t))
(defun auto-rename-tag--disable ()
"Disable `auto-rename-tag' in current buffer."
(remove-hook 'before-change-functions #'auto-rename-tag--before-change t)
(remove-hook 'after-change-functions #'auto-rename-tag--after-change t))
;;;###autoload
(define-minor-mode auto-rename-tag-mode
"Minor mode 'auto-rename-tag' mode."
:lighter " ART"
:group auto-rename-tag
(if auto-rename-tag-mode (auto-rename-tag--enable) (auto-rename-tag--disable)))
(provide 'auto-rename-tag)
;;; auto-rename-tag.el ends here