-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpairtools.vba
2132 lines (1492 loc) · 61.4 KB
/
pairtools.vba
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
" Vimball Archiver by Charles E. Campbell, Jr., Ph.D.
UseVimball
finish
autoload/jigsaw.vim [[[1
110
" Jigsaw.vim - PairTools module handling various keys with side-effects
" Last Changed: 2011 May 18
" Maintainer: Martin Lafreniere <[email protected]>
"
" Copyright (C) 2011 by Martin Lafrenière
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and permit persons to whom the Software is furnished
" to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in all
" copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NOT EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
" SOFTWARE.
" Backspace Hook API {{{1
function! jigsaw#Backspace()
let line = getline('.')
let column = col('.') - 1
" Try each registered hook until one is actually executed
for [hook, value] in items(b:PTBackspaceHookTable)
exe 'let result = ' . hook . '()'
if result
return value
endif
endfor
return "\<BS>"
endfunction
" Public function provided to the user to add custom hooks
" A hook must 0 if not executing in its context, else 1
"
function! jigsaw#AddBackspaceHook(HookFullName, HookReturnValue)
if !exists('b:PTBackspaceHookTable')
let b:PTBackspaceHookTable = {}
endif
if !has_key(b:PTBackspaceHookTable, a:HookFullName)
let b:PTBackspaceHookTable[a:HookFullName] = a:HookReturnValue
endif
endfunction
function! jigsaw#NoErase()
return 0
endfunction
"}}}1
" Carriage Return Hook API {{{1
function! jigsaw#CarriageReturn()
let line = getline('.')
let column = col('.') - 1
" Try each registered hook until one is actually executed
for [hook, value] in items(b:PTCarriageReturnHookTable)
exe 'let result = ' . hook . '()'
if result
return value
endif
endfor
return "\<CR>"
endfunction
"
" Public function provided to the user to add custom hooks
" A hook must 0 if not executing in its context, else 1
"
function! jigsaw#AddCarriageReturnHook(HookFullName, HookReturnValue)
if !exists('b:PTCarriageReturnHookTable')
let b:PTCarriageReturnHookTable = {}
endif
if !has_key(b:PTCarriageReturnHookTable, a:HookFullName)
let b:PTCarriageReturnHookTable[a:HookFullName] = a:HookReturnValue
endif
endfunction
function! jigsaw#NoExpand()
return 0
endfunction
"}}}1
" vim: set fdm=marker :
autoload/pairclamp.vim [[[1
455
" PairClamp.vim - PairTools module handling single character pairs
" Last Changed: 2011 May 25
" Maintainer: Martin Lafreniere <[email protected]>
"
" Copyright (C) 2011 by Martin Lafrenière
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and permit persons to whom the Software is furnished
" to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in all
" copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NOT EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
" SOFTWARE.
" PairClamp Auto-Close {{{1
function! pairclamp#Close(Key)
if !b:PTAutoClose
return a:Key
endif
let line = getline('.')
let column = col('.') - 1
if (!has_key(b:PTWorkPairs, a:Key) || s:IsQuotes(a:Key)) && line[column] == a:Key
" Prevent step out on certain syntax fields unless the cursor is on
" the closing delimiter
if !s:IsAntimagic(a:Key, 1)
call cursor(line('.'), column + 2)
let value = ""
else
let value = a:Key
endif
else
" Prevent auto close on certain syntax fields
if !s:IsAntimagic(a:Key, 0) && s:IsAllowClose(a:Key)
call s:InsertClosing(a:Key)
endif
let value = a:Key
endif
return value
endfunction
function! s:IsQuotes(Key)
return has_key(b:PTWorkPairs, a:Key) && b:PTWorkPairs[a:Key] == a:Key
endfunction
function! s:IsAllowClose(Key)
let line = getline('.')
let column = col('.') - 1
if !has_key(b:PTWorkPairs, a:Key)
return 0
endif
if b:PTSmartClose && line[column] =~ s:SmartRegex()
return 0
endif
if b:PTApostrophe && line[column - 1] =~ '\a'
return 0
endif
if a:Key == b:PTWorkPairs[a:Key]
let isAllowed = s:CountPairs(a:Key) % 2 == 0
else
let isAllowed = s:CountPairs(a:Key) > -1
endif
return isAllowed
endfunction
function! s:CountPairs(Key)
if a:Key == b:PTWorkPairs[a:Key]
let unpaired = count(split(s:RemoveMatchedPairs(), '\zs'), a:Key)
else
let singles = split(getline('.'), '\zs')
let unpaired = count(singles, a:Key) - count(singles, b:PTWorkPairs[a:Key])
endif
return unpaired
endfunction
function! s:InsertClosing(Key)
let line = getline('.')
let column = col('.') - 1
if column > 0
call setline('.', line[:(column-1)] . b:PTWorkPairs[a:Key] . line[(column):])
else
call setline('.', b:PTWorkPairs[a:Key] . line[(column):])
endif
endfunction
function! s:RemoveMatchedPairs()
let line = getline('.')
" Setup regexes and check if cache need to be updated
if !exists('b:PTReQuoteCache') || b:PTReQuoteCache.pairs != b:PTClosePairs
let b:PTReQuoteCache = {}
let b:PTReQuoteCache.pairs = b:PTClosePairs
let b:PTReQuoteCache.regex = '\%(' . join(s:BuildRegex(), '\|') . '\)'
endif
" Remove apostrophe
if b:PTApostrophe
let line = substitute(line, '\a\zs''', '', 'g')
endif
" Remove matched pairs
let index = match(line, b:PTReQuoteCache.regex)
while index > -1
let match = matchstr(line, b:PTReQuoteCache.regex)
if index > 0
let line = line[:(index - 1)] . line[(strlen(match) + index):]
else
let line = line[strlen(match):]
endif
let index = match(line, b:PTReQuoteCache.regex)
endwhile
return line
endfunction
function! s:BuildRegex()
let regexes = []
for [key, value] in items(b:PTWorkPairs)
if key == value
let quote = s:EscapeSpecialValue(key)
" Double quotes string uses escaped characters!
if key == '"'
call add(regexes, quote . '[^\\' . quote . ']*\%(\\.[^\\' .quote . ']*\)*' . quote )
else
call add(regexes, quote.'[^'.quote.']*'.quote )
endif
endif
endfor
return regexes
endfunction
function! s:EscapeSpecialValue(Value)
let specialValues = {'*': '', '.': '', '$': ''}
if has_key(specialValues, a:Value)
let value = escape(a:Value, a:Value)
else
let value = a:Value
endif
return value
endfunction
function! s:SmartRegex()
" Cache regex to avoid recomputing everyting...
if !exists('b:PTReSmartCache') ||
\b:PTReSmartCache.pairs != b:PTClosePairs ||
\b:PTReSmartCache.rules != b:PTSmartCloseRules
let b:PTReSmartCache = {}
let b:PTReSmartCache.pairs = b:PTClosePairs
let b:PTReSmartCache.rules = b:PTSmartCloseRules
let rules = split(b:PTSmartCloseRules, ',')
" Replace ^ by all Work Pairs
let uparrow = index(rules, '^')
if uparrow > -1
call remove(rules, uparrow)
for key in keys(b:PTWorkPairs)
call add(rules, key)
endfor
endif
let b:PTReSmartCache.regex = '\%(' . join(rules, '\|') . '\)'
endif
return b:PTReSmartCache.regex
endfunction
"}}}1
" PairClamp Force Pair {{{1
function! pairclamp#Force(Key)
if b:PTForcePairs
call s:InsertClosing(a:Key)
endif
return a:Key
endfunction
"}}}1
" PairClamp Erase {{{1
function! pairclamp#Erase()
" This function only remove the closing key
let result = 0
let line = getline('.')
let column = col('.') - 1
let opening = line[column - 1]
let closing = line[column]
if has_key(b:PTWorkPairs, opening) && b:PTWorkPairs[opening] == closing
if column > 0
call setline('.', line[:(column - 1)] . line[(column + 1):])
else
call setline('.', line[(column + 1):])
endif
let result = 1
endif
return result
endfunction
"}}}1
" PairClamp Expansion {{{1
function! pairclamp#Expand()
let line = getline('.')
let column = col('.') - 1
let row = line('.')
for [key, value] in items(b:PTWorkPairs)
if line[(column-1):(column)] == key.value
" Keep indentation clean
let startSpace = match(line, '\S\+')
" Do actual expansion equivalent to <CR><CR><UP><TAB>
call setline('.', line[:(column-1)])
call append(row, repeat(' ', startSpace + &l:shiftwidth))
call append(row + 1, repeat(' ', startSpace) . line[(column):])
" Place the cursor as though the user pressed tab for indentation
call cursor(row + 1, startSpace + &l:shiftwidth + 1)
return 1
endif
endfor
return 0
endfunction
"}}}1
" Antimagic {{{1
function! s:IsAntimagic(Key, IsClosing)
if !b:PTAntimagic
return 0
endif
let currentSyntax = s:GetCurrentSyntax()
let stringSyntax = "None"
if b:PTAntimagicField =~? "String" && a:Key == '"'
let stringSyntax = s:IsString(a:Key)
if currentSyntax != "String" && stringSyntax == "String"
return 1
endif
endif
let constantSyntax = "None"
if b:PTAntimagicField =~? "Constant"
let constantSyntax = s:IsConstant(a:Key)
if currentSyntax != "Constant" && constantSyntax == "Constant"
return 1
endif
endif
return b:PTAntimagicField =~? currentSyntax && !a:IsClosing
endfunction
function! s:GetCurrentSyntax()
let column = col('.')
let cursor = (column == col('$') ? column-1 : column)
let syntaxName = synIDattr(synIDtrans(synID(line('.'), cursor, 1)), 'name')
return empty(syntaxName) ? "None" : syntaxName
endfunction
function! s:IsString(Key)
let line = getline('.')
let column = col('.')-1
let newline = s:RemoveMatchedPairs()
" Check syntax after removing backslash on left, because
" it could be the start of an escaped character
if line[column-1] == '\' && newline =~ '\'
call setline('.', line[:(column-2)].line[(column):])
call cursor(line('.'), column)
endif
let syntax = s:GetCurrentSyntax()
call cursor(line('.'), column + 1)
call setline('.', line)
return syntax
endfunction
function! s:IsConstant(Key)
let line = getline('.')
let column = col('.')-1
let prefix = (column == 0 ? "" : line[:(column-1)])
call setline('.', prefix . a:Key . line[(column):])
let syntax = s:GetCurrentSyntax()
call setline('.', line)
return syntax
endfunction
"}}}1
" Mapping Utilities {{{1
function! s:ToWorkPairs(String)
" Only convert the following format: "<o0>:<c0>,<o1>:<c1>,..."
let work = {}
for pair in split(a:String, ',')
let [key, value] = split(pair, ':')
let work[key] = value
endfor
return work
endfunction
function! pairclamp#UniquifyCloseKeys(...)
" Use provided pairs as working pairs if passed to function
let b:PTWorkPairs = s:ToWorkPairs(a:0 > 0 ? a:000[0] : b:PTClosePairs)
" Here, we basically filter all keys that can be pressed to start
" and end a pair, making sure that it only appears once inside the
" list.
let b:PTCloseKeys = keys(b:PTWorkPairs) + values(b:PTWorkPairs)
let list = []
for key in b:PTCloseKeys
if index(list, key) == -1
call add(list, key)
endif
endfor
return list
endfunction
function! pairclamp#SanitizeKey(Key)
let specialKeys = {'|': '<Bar>'}
return (has_key(specialKeys, a:Key) ? specialKeys[a:Key] : a:Key)
endfunction
" }}}1
" vim: set fdm=marker :
autoload/tagwrench.vim [[[1
506
" TagWrench.vim - PairTools module handling angle brackets pair and XML/HTML tags
" Last Changed: 2011 May 25
" Maintainer: Martin Lafreniere <[email protected]>
"
" Copyright (C) 2011 by Martin Lafrenière
"
" Permission is hereby granted, free of charge, to any person obtaining a copy
" of this software and associated documentation files (the "Software"), to deal
" in the Software without restriction, including without limitation the rights
" to use, copy, todify, merge, publish, distribute, sublicense, and/or sell
" copies of the Software, and permit persons to whom the Software is furnished
" to do so, subject to the following conditions:
"
" The above copyright notice and this permission notice shall be included in all
" copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
" IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
" FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NOT EVENT SHALL THE
" AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
" LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
" OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
" SOFTWARE.
" Angle Brackets Auto-Close Event/Context {{{1
function! tagwrench#StartContext(Value)
" Make sure no context was previously started to fire a Tag Event
if !exists('b:TWContext') || !b:TWContext
let b:TWTagEvent = 1
endif
let b:TWContext = 1
return a:Value
endfunction
function! tagwrench#StopContext(...)
let value = (a:0 > 0 ? a:000[0] : '')
let line = getline('.')
let cursor = col('.') - 1
if exists('b:TWContext') && b:TWContext
" Make sure the > wasn't pressed inside an attribute field
if s:IsAttributeContext() && value == '>'
return value
endif
" Pressed either '>' or 'Esc' left of '>'
if line[cursor] == '>' && value == '>'
" Call a hook for further processing...
call s:TagHook(b:TWContextBegin, cursor+1)
call cursor(line('.'), cursor+2)
let value = ''
endif
endif
if exists('b:TWContextBegin')
unlet b:TWContextBegin
endif
let b:TWContext = 0
let b:TWTagEvent = 0
return value
endfunction
function! s:IsAttributeContext()
let line = getline('.')
let column = col('.') - 1
let start = 0
let attr = 0
while start < column
if line[start] =~ '[''"]'
let attr = !attr
endif
let start += 1
endwhile
return attr
endfunction
function! tagwrench#StopContextIf(Direction, Value)
let line = getline('.')
let cursor = col('.') - 1
let position = (a:Direction == 'L' ? cursor - 1 : cursor)
" Stop only when getting out of < ... >
if exists('b:TWContext') && b:TWContext
if !s:IsAttributeContext() && line[position] == a:Value
if exists('b:TWContextBegin')
unlet b:TWContextBegin
endif
let b:TWContext = 0
let b:TWTagEvent = 0
endif
endif
return ""
endfunction
function! s:FindContextBegin(Delimit)
let line = getline('.')
let start = col('.') - 2
while line[start] != a:Delimit
let start -= 1
endwhile
return start
endfunction
function! s:IsContextUnclosed()
let line = split(getline('.'), '\zs')
let openings = count(line, '<')
let closings = count(line, '>')
return openings - closings
endfunction
function! tagwrench#EnterTagEvent()
if !exists('b:TWContextBegin')
let b:TWContextBegin = s:FindContextBegin('<')
endif
let line = getline('.')
let cursor = col('.') - 1
" Wait for first character typed
if line[cursor-1] == '<'
return
endif
let symbol = ''
if line[b:TWContextBegin + 1] =~ '[?%]'
let symbol = line[b:TWContextBegin + 1]
endif
" Check for legal context: |word|, !, %, ?, /
if line[b:TWContextBegin + 1] !~ '[[:alnum:]_!/]' && symbol == ''
unlet b:TWContextBegin
let b:TWContext = 0
else
if s:IsContextUnclosed() > 0
call setline('.', line[:(cursor-1)].symbol.'>'.line[(cursor):])
endif
endif
" Delimiters such as <%,%> and <?,?> do not trigger a context
if symbol != ''
unlet b:TWContextBegin
let b:TWContext = 0
endif
let b:TWTagEvent = 0
endfunction
function! tagwrench#IsTagEvent()
if !exists('b:TWTagEvent')
let b:TWContext = 0
let b:TWTagEvent = 0
endif
return b:TWTagEvent
endfunction
" }}}1
" Angle Brackets Erase {{{1
function! tagwrench#Erase()
let line = getline('.')
let column = col('.') - 1
" Easiest case is <|>
if line[(column - 1):(column)] == '<>'
return s:RemoveDelimiters(1)
endif
" Case <%|%> and <?|?>
if line[(column - 2):(column + 1)] =~ '<[%?][%?]>'
return s:RemoveDelimiters(2)
endif
" Next is <tag>|</tag>
if line[(column - 1):(column)] == '><'
if s:RemoveMatchedTags()
return 1
endif
endif
" Finally, it can be <tag>|, <tag/>|, <!tag>|, <!tag/>, </tag>
if line[column - 1] == '>'
return s:RemoveVoidTag()
endif
return 0
endfunction
function! s:RemoveDelimiters(Length)
let column = col('.') - 1
call s:RemoveRange(column - a:Length + 1, column - a:Length + 1, column + a:Length - 1)
return 1
endfunction
function! s:RemoveMatchedTags()
let result = 0
let line = getline('.')
let column = col('.') - 1
if line[column + 1] != '/'
return result
endif
" Get closing tag name (remove starting </)
let closingName = matchstr(line[(column + 2):], '^[[:alnum:]_:-]\+')
let closingLength = strlen(closingName)
let end = column + closingLength + 2
" Reverse look for <tagname because muliples tag with the same
" name on one line is possible
let start = column - 1
let skip = 0
while (skip || line[start] != '<') && start > -1
if line[start] =~ '["'']' && line[start - 1] != '\'
let skip = !skip
endif
let start -= 1
endwhile
if start > - 1 && line[(start + 1):(start + closingLength)] == closingName
" Compensate begin by one for <BS>
call s:RemoveRange(start+1, start + 1, end)
call cursor(line('.'), start + 2)
let result = 1
endif
return result
endfunction
function! s:RemoveVoidTag()
let result = 0
let line = getline('.')
let column = col('.') - 1
" Reverse look for the start of the void tag
let start = column - 1
let skip = 0
while (skip || line[(start):] !~ '^<[!/]\?\w') && start > -1
if line[start] =~ '["'']' && line[start - 1] != '\'
let skip = !skip
endif
let start -= 1
endwhile
if start > -1
call s:RemoveRange(start+1, start+1, column - 1)
call cursor(line('.'), start + 2)
let result = 1
endif
return result
endfunction
function! s:RemoveRange(Current, Begin, End)
let line = getline('.')
if a:Current > 0
call setline('.', line[:(a:Begin - 1)] . line[(a:End + 1):])
else
call setline('.', line[(a:End + 1):])
endif
endfunction
"}}}1
" Angle Brackets Expansion {{{1
function! tagwrench#Expand()
let line = getline('.')
let column = col('.') - 1
" Cases <%|%> and <?|?> / <tag>|</tag>
if (line[(column - 2):(column + 1)] =~ '<[%?][%?]>') ||
\(line[(column - 1):(column)] == '><')
return s:ExpandCR()
endif
return 0
endfunction
function! s:ExpandCR()
let line = getline('.')
let column = col('.') - 1
let row = line('.')
let startSpace = match(line, '\S\+')
" Do expansion
call setline('.', line[:(column - 1)])
call append(row, repeat(' ', startSpace + &l:shiftwidth))
call append(row + 1, repeat(' ', startSpace) . line[(column):])
call cursor(row + 1, startSpace + &l:shiftwidth + 1)
return 1
endfunction
"}}}1
" TagWrench Hook Public API {{{1
function! s:TagHook(ContextBegin, ContextEnd)
" Add built in hook if not already done
call tagwrench#AddHook('tagwrench#BuiltinNoHook')
call tagwrench#AddHook('tagwrench#BuiltinBasicTagHook')
call tagwrench#AddHook('tagwrench#BuiltinHTML5Hook')
if index(g:PTTagWrenchHookTable, b:PTTagWrenchHook) > -1
exe 'call ' . b:PTTagWrenchHook . '(' .a:ContextBegin . ',' . a:ContextEnd . ')'
endif
endfunction
" Public function provided to the user to add custom hooks
" A Hook is passed two arguments
" 1. Context Begin: this is the position of the < character
" 2. Context End: this is the position after the > character
" See BuiltinBasicTagHook for an example.
function! tagwrench#AddHook(HookFullName)
if !exists('g:PTTagWrenchHookTable')
let g:PTTagWrenchHookTable = []
endif
if index(g:PTTagWrenchHookTable, a:HookFullName) == -1
call add(g:PTTagWrenchHookTable, a:HookFullName)
endif
endfunction
function! tagwrench#BuiltinNoHook(ContextBegin, ContextEnd)
" Do nothing
return
endfunction
" Simple tag handling: complement starting <tag> with ending </tag>
" Arguments: Receives the context begin (tag <) and context end (tag >)
function! tagwrench#BuiltinBasicTagHook(ContextBegin, ContextEnd, ...)
let line = getline('.')
let startTag = line[(a:ContextBegin):(a:ContextEnd-1)]
let tagName = matchstr(startTag, '^<\zs!\?[[:alnum:]_:-]\+')
let voids = (a:0 > 0 ? a:000[0] : '')
if index(split(voids, ','), tolower(tagName)) == -1 && startTag !~ '/>$' && startTag !~ '^<[!/]'
let closingPos = s:SurroundClosingTag(a:ContextEnd)
if closingPos > 0
call setline('.', line[:(closingPos-1)] . '</' . tagName . '>' . line[(closingPos):])
endif
endif
endfunction