Repository navigation
Expand file tree
/
Copy pathHTML.vim
More file actions
3276 lines (2895 loc) · 125 KB
/
Copy pathHTML.vim
File metadata and controls
3276 lines (2895 loc) · 125 KB
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
" ---- Author & Copyright: ---------------------------------------------- {{{1
"
" Author: Christian J. Robinson <[email protected]>
" URL: http://www.infynity.spodzone.com/vim/HTML/
" Last Change: September 19, 2009
" Version: 0.36.1
" Original Concept: Doug Renze
"
"
" The original Copyright goes to Doug Renze, although nearly all of his
" efforts have been modified in this implementation. My changes and additions
" are Copyrighted by me, on the dates marked in the ChangeLog.
"
" (Doug Renze has authorized me to place the original "code" under the GPL.)
"
" ----------------------------------------------------------------------------
"
" 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 2 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.
"
" ---- Original Author's Notes: ----------------------------------------------
"
" HTML Macros
" I wrote these HTML macros for my personal use. They're
" freely-distributable and freely-modifiable.
"
" If you do make any major additions or changes, or even just
" have a suggestion for improvement, feel free to let me
" know. I'd appreciate any suggestions.
"
" Credit must go to Eric Tilton, Carl Steadman and Tyler
" Jones for their excellent book "Web Weaving" which was
" my primary source.
"
" Doug Renze
"
" ---- TODO: ------------------------------------------------------------ {{{1
"
" - Specific browser mappings for Win32 with "start <browser> ..." ?
" - Find a way to make "gv" after executing a visual mapping re-select the
" right text. (Currently my extra code that wraps around the visual
" mappings can tweak the selected area significantly.)
" + This should probably exclude the newly created tags, so things like
" visual selection ;ta, then gv and ;tr, then gv and ;td work.
" - Add :HTMLmappingsreload/html/xhtml to the HTML menu?
"
" ---- RCS Information: ------------------------------------------------- {{{1
" $Id: HTML.vim,v 1.210 2009/09/19 19:43:45 infynity Exp $
" ----------------------------------------------------------------------- }}}1
" ---- Initialization: -------------------------------------------------- {{{1
if v:version < 700
echoerr "HTML.vim no longer supports Vim versions prior to 7."
sleep 2
finish
"elseif v:version < 700
" let s:tmp =
" \ "The HTML macros support for Vim versions prior to 7\n" .
" \ "will be abandoned in future versions.\n\n" .
" \ "You should seriously consider upgrading your version of Vim."
" call confirm(s:tmp, "&Dismiss", 1, 'Warning')
" unlet s:tmp
endif
" Save cpoptions and remove some junk that will throw us off (reset at the end
" of the script):
let s:savecpo = &cpoptions
set cpoptions&vim
let s:doing_internal_html_mappings = 1
if ! exists("b:did_html_mappings_init")
let b:did_html_mappings_init = 1
setlocal matchpairs+=<:>
" ---- Init Functions: -------------------------------------------------- {{{2
" s:BoolVar() {{{3
"
" Given a string, test to see if a variable by that string name exists, and if
" so, whether it's set to 1|true|yes / 0|false|no (Actually, anything not
" listed here also returns as 1.)
"
" Arguments:
" 1 - String: The name of the variable to test (not its value!)
" Return Value:
" 1/0
"
" Limitations:
" This /will not/ work on function-local variable names.
function! s:BoolVar(var)
if a:var =~ '^[bgstvw]:'
let var = a:var
else
let var = 'g:' . a:var
endif
if s:IsSet(var)
execute "let varval = " . var
return s:Bool(varval)
else
return 0
endif
endfunction
" s:Bool() {{{3
"
" Helper to s:BoolVar() -- Test the string passed to it and return true/false
" based on that string.
"
" Arguments:
" 1 - String: 1|true|yes / 0|false|no
" Return Value:
" 1/0
function! s:Bool(str)
return a:str !~? '^no$\|^false$\|^0$\|^$'
endfunction
" SetIfUnset() {{{3
"
" Set a variable if it's not already set.
"
" Arguments:
" 1 - String: The variable name
" 2 ... N - String: The default value to use, "-" for the null string
" Return Value:
" 0 - The variable already existed
" 1 - The variable didn't exist and was set
" -1 - An error occurred
function! SetIfUnset(var, ...)
if a:var =~ '^[bgstvw]:'
let var = a:var
else
let var = 'g:' . a:var
endif
if a:0 == 0
echohl ErrorMsg
echomsg "E119: Not enough arguments for function: SetIfUnset"
echohl None
return -1
else
let val = join(a:000, ' ')
endif
if ! s:IsSet(var)
if val == "-"
execute "let " . var . "= \"\""
else
execute "let " . var . "= val"
endif
return 1
endif
return 0
endfunction
" s:IsSet() {{{3
"
" Given a string, test to see if a variable by that string name exists.
"
" Arguments:
" 1 - String: The variable name
" Return Value:
" 1/0
function! s:IsSet(str)
execute "let varisset = exists(\"" . a:str . "\")"
return varisset
endfunction "}}}3
" ----------------------------------------------------------------------- }}}2
command! -nargs=+ SetIfUnset call SetIfUnset(<f-args>)
SetIfUnset g:html_bgcolor #FFFFFF
SetIfUnset g:html_textcolor #000000
SetIfUnset g:html_linkcolor #0000EE
SetIfUnset g:html_alinkcolor #FF0000
SetIfUnset g:html_vlinkcolor #990066
SetIfUnset g:html_tag_case uppercase
SetIfUnset g:html_map_leader ;
SetIfUnset g:html_map_entity_leader &
SetIfUnset g:html_default_charset iso-8859-1
" No way to know sensible defaults here so just make sure the
" variables are set:
SetIfUnset g:html_authorname -
SetIfUnset g:html_authoremail -
if g:html_map_entity_leader ==# g:html_map_leader
echohl ErrorMsg
echomsg 'ERROR! "g:html_map_entity_leader" and "g:html_map_leader" have the same value!'
echomsg ' Resetting "g:html_map_entity_leader" to "&".'
echohl None
sleep 2
let g:html_map_entity_leader = '&'
endif
if exists('b:html_tag_case')
let b:html_tag_case_save = b:html_tag_case
endif
" Detect whether to force uppper or lower case: {{{2
if &filetype ==? "xhtml"
\ || s:BoolVar('g:do_xhtml_mappings')
\ || s:BoolVar('b:do_xhtml_mappings')
let b:do_xhtml_mappings = 1
else
let b:do_xhtml_mappings = 0
if s:BoolVar('g:html_tag_case_autodetect')
\ && (line('$') != 1 || getline(1) != '')
let s:found_upper = search('\C<\(\s*/\)\?\s*\u\+\_[^<>]*>', 'wn')
let s:found_lower = search('\C<\(\s*/\)\?\s*\l\+\_[^<>]*>', 'wn')
if s:found_upper && ! s:found_lower
let b:html_tag_case = 'uppercase'
elseif ! s:found_upper && s:found_lower
let b:html_tag_case = 'lowercase'
endif
unlet s:found_upper s:found_lower
endif
endif
if s:BoolVar('b:do_xhtml_mappings')
let b:html_tag_case = 'lowercase'
endif
" }}}2
call SetIfUnset('b:html_tag_case', g:html_tag_case)
let s:thisfile = expand("<sfile>:p")
" ----------------------------------------------------------------------------
" ---- Functions: ------------------------------------------------------- {{{1
if ! exists("g:did_html_functions")
let g:did_html_functions = 1
" HTMLencodeString() {{{2
"
" Encode the characters in a string into their HTML &#...; representations.
"
" Arguments:
" 1 - String: The string to encode.
" 2 - String: Optional, whether to decode rather than encode the string:
" d/decode: Decode the &#...; elements of the provided string
" anything else: Encode the string (default)
" Return Value:
" String: The encoded string.
function! HTMLencodeString(string, ...)
let out = ''
if a:0 > 0
if a:1 =~? '^d\(ecode\)\=$'
let out = substitute(a:string, '&#\(\d\+\);', '\=nr2char(submatch(1))', 'g')
let out = substitute(out, '%\(\x\{2}\)', '\=nr2char("0x".submatch(1))', 'g')
return out
elseif a:1 == '%'
let out = substitute(a:string, '\(.\)', '\=printf("%%%02X", char2nr(submatch(1)))', 'g')
return out
endif
endif
let string = split(a:string, '\zs')
for c in string
let out = out . '&#' . char2nr(c) . ';'
endfor
return out
endfunction
" HTMLmap() {{{2
"
" Define the HTML mappings with the appropriate case, plus some extra stuff.
"
" Arguments:
" 1 - String: Which map command to run.
" 2 - String: LHS of the map.
" 3 - String: RHS of the map.
" 4 - Integer: Optional, applies only to visual maps:
" -1: Don't add any extra special code to the mapping.
" 0: Mapping enters insert mode.
" Applies only when filetype indenting is on:
" 1: re-selects the region, moves down a line, and re-indents.
" 2: re-selects the region and re-indents.
" (Don't use these two arguments for maps that enter insert
" mode!)
let s:modes = {
\ 'n': 'normal',
\ 'v': 'visual',
\ 'o': 'operator-pending',
\ 'i': 'insert',
\ 'c': 'command-line',
\ 'l': 'langmap',
\}
function! HTMLmap(cmd, map, arg, ...)
let mode = strpart(a:cmd, 0, 1)
let map = substitute(a:map, '^<lead>\c', escape(g:html_map_leader, '&~\'), '')
let map = substitute(map, '^<elead>\c', escape(g:html_map_entity_leader, '&~\'), '')
if exists('s:modes[mode]') && s:MapCheck(map, mode) >= 2
return
endif
let arg = s:ConvertCase(a:arg)
if ! s:BoolVar('b:do_xhtml_mappings')
let arg = substitute(arg, ' \?/>', '>', 'g')
endif
if mode == 'v'
" If 'selection' is "exclusive" all the visual mode mappings need to
" behave slightly differently:
let arg = substitute(arg, "`>a\\C", "`>i<C-R>=<SID>VI()<CR>", 'g')
if a:0 >= 1 && a:1 < 0
execute a:cmd . " <buffer> <silent> " . map . " " . arg
elseif a:0 >= 1 && a:1 >= 1
execute a:cmd . " <buffer> <silent> " . map . " <C-C>:call <SID>TO(0)<CR>gv" . arg
\ . ":call <SID>TO(1)<CR>m':call <SID>ReIndent(line(\"'<\"), line(\"'>\"), " . a:1 . ")<CR>``"
elseif a:0 >= 1
execute a:cmd . " <buffer> <silent> " . map . " <C-C>:call <SID>TO(0)<CR>gv" . arg
\ . "<C-O>:call <SID>TO(1)<CR>"
else
execute a:cmd . " <buffer> <silent> " . map . " <C-C>:call <SID>TO(0)<CR>gv" . arg
\ . ":call <SID>TO(1)<CR>"
endif
else
execute a:cmd . " <buffer> <silent> " . map . " " . arg
endif
if exists('s:modes[mode]')
let b:HTMLclearMappings = b:HTMLclearMappings . ':' . mode . "unmap <buffer> " . map . "\<CR>"
else
let b:HTMLclearMappings = b:HTMLclearMappings . ":unmap <buffer> " . map . "\<CR>"
endif
call s:ExtraMappingsAdd(':call HTMLmap("' . a:cmd . '", "' . escape(a:map, '"\')
\ . '", "' . escape(a:arg, '"\') . (a:0 >= 1 ? ('", ' . a:1) : '"' ) . ')')
endfunction
" HTMLmapo() {{{2
"
" Define a map that takes an operator to its corresponding visual mode
" mapping.
"
" Arguments:
" 1 - String: The mapping.
" 2 - Boolean: Whether to enter insert mode after the mapping has executed.
" (A value greater than 1 tells the mapping not to move right one
" character.)
function! HTMLmapo(map, insert)
let map = substitute(a:map, "^<lead>", g:html_map_leader, '')
if s:MapCheck(map, 'o') >= 2
return
endif
execute 'nnoremap <buffer> <silent> ' . map
\ . " :let b:htmltagaction='" . map . "'<CR>"
\ . ":let b:htmltaginsert=" . a:insert . "<CR>"
\ . ':set operatorfunc=<SID>WR<CR>g@'
let b:HTMLclearMappings = b:HTMLclearMappings . ":nunmap <buffer> " . map . "\<CR>"
call s:ExtraMappingsAdd(':call HTMLmapo("' . escape(a:map, '"\') . '", ' . a:insert . ')')
endfunction
" s:MapCheck() {{{2
"
" Check to see if a mapping for a mode already exists. If there is, and
" overriding hasn't been suppressed, print an error.
"
" Arguments:
" 1 - String: The map sequence (LHS).
" 2 - Character: The mode for the mapping.
" Return Value:
" 0 - No mapping was found.
" 1 - A mapping was found, but overriding has /not/ been suppressed.
" 2 - A mapping was found and overriding has been suppressed.
" 3 - The mapping to be defined was suppressed by g:no_html_maps.
"
" (Note that suppression only works for the internal mappings.)
function! s:MapCheck(map, mode)
if exists('s:doing_internal_html_mappings') &&
\ ( (exists('g:no_html_maps') && a:map =~# g:no_html_maps) ||
\ (exists('b:no_html_maps') && a:map =~# b:no_html_maps) )
return 3
elseif exists('s:modes[a:mode]') && maparg(a:map, a:mode) != ''
if s:BoolVar('g:no_html_map_override') && exists('s:doing_internal_html_mappings')
return 2
else
echohl WarningMsg
echomsg "WARNING: A mapping to \"" . a:map . "\" for " . s:modes[a:mode] . " mode has been overridden for this buffer."
echohl None
return 1
endif
endif
return 0
endfunction
" s:WR() {{{2
" Function set in 'operatorfunc' for mappings that take an operator:
function! s:WR(type)
let sel_save = &selection
let &selection = "inclusive"
if a:type == 'line'
execute "normal `[V`]" . b:htmltagaction
elseif a:type == 'block'
execute "normal `[\<C-V>`]" . b:htmltagaction
else
execute "normal `[v`]" . b:htmltagaction
endif
let &selection = sel_save
if b:htmltaginsert
if b:htmltaginsert < 2
execute "normal \<Right>"
endif
startinsert
endif
" Leave these set so .-repeating of operator mappings works:
"unlet b:htmltagaction b:htmltaginsert
endfunction
" s:ExtraMappingsAdd() {{{2
"
" Add to the b:HTMLextraMappings variable if necessary.
"
" Arguments:
" 1 - String: The command necessary to re-define the mapping.
function! s:ExtraMappingsAdd(arg)
if ! exists('s:doing_internal_html_mappings') && ! exists('s:doing_extra_html_mappings')
if ! exists('b:HTMLextraMappings')
let b:HTMLextraMappings = ''
endif
let b:HTMLextraMappings = b:HTMLextraMappings . a:arg . ' |'
endif
endfunction
" s:TO() {{{2
"
" Used to make sure the 'showmatch', 'indentexpr', and 'formatoptions' options
" are off temporarily to prevent the visual mappings from causing a
" (visual)bell or inserting improperly.
"
" Arguments:
" 1 - Integer: 0 - Turn options off.
" 1 - Turn options back on, if they were on before.
function! s:TO(s)
if a:s == 0
let s:savesm=&l:sm | let &l:sm=0
let s:saveinde=&l:inde | let &l:inde=''
let s:savefo=&l:fo | let &l:fo=''
" A trick to make leading indent on the first line of visual-line
" selections is handled properly (turn it into a character-wise
" selection and exclude the leading indent):
if visualmode() ==# 'V'
let s:visualmode_save = visualmode()
exe "normal `<^v`>\<C-C>"
endif
else
let &l:sm=s:savesm | unlet s:savesm
let &l:inde=s:saveinde | unlet s:saveinde
let &l:fo=s:savefo | unlet s:savefo
" Restore the last visual mode if it was changed:
if exists('s:visualmode_save')
exe "normal gv" . s:visualmode_save . "\<C-C>"
unlet s:visualmode_save
endif
endif
endfunction
" s:TC() {{{2
"
" Used to make sure the 'comments' option is off temporarily to prevent
" certain mappings from inserting unwanted comment leaders.
"
" Arguments:
" 1 - Integer: 0 - Turn options off.
" 1 - Turn options back on, if they were on before.
function! s:TC(s)
if a:s == 0
let s:savecom=&l:com | let &l:com=''
else
let &l:com=s:savecom | unlet s:savecom
endif
endfunction
" s:VI() {{{2
"
" Used by HTMLmap() to enter insert mode in Visual mappings in the right
" place, depending on what 'selection' is set to.
"
" Arguments:
" None
" Return Value:
" The proper movement command based on the value of 'selection'.
function! s:VI()
if &selection == 'inclusive'
return "\<right>"
else
return "\<C-O>`>"
endif
endfunction
" s:ConvertCase() {{{2
"
" Convert special regions in a string to the appropriate case determined by
" b:html_tag_case.
"
" Arguments:
" 1 - String: The string with the regions to convert surrounded by [{...}].
" Return Value:
" The converted string.
function! s:ConvertCase(str)
if (! exists('b:html_tag_case')) || b:html_tag_case =~? 'u\(pper\(case\)\?\)\?' || b:html_tag_case == ''
let str = substitute(a:str, '\[{\(.\{-}\)}\]', '\U\1', 'g')
elseif b:html_tag_case =~? 'l\(ower\(case\)\?\)\?'
let str = substitute(a:str, '\[{\(.\{-}\)}\]', '\L\1', 'g')
else
echohl WarningMsg
echomsg "WARNING: b:html_tag_case = '" . b:html_tag_case . "' invalid, overriding to 'upppercase'."
echohl None
let b:html_tag_case = 'uppercase'
let str = s:ConvertCase(a:str)
endif
return str
endfunction
" s:ReIndent() {{{2
"
" Re-indent a region. (Usually called by HTMLmap.)
" Nothing happens if filetype indenting isn't enabled or 'indentexpr' is
" unset.
"
" Arguments:
" 1 - Integer: Start of region.
" 2 - Integer: End of region.
" 3 - Integer: 1: Add an extra line below the region to re-indent.
" *: Don't add an extra line.
function! s:ReIndent(first, last, extraline)
" To find out if filetype indenting is enabled:
let save_register = @x
redir @x | silent! filetype | redir END
let filetype_output = @x
let @x = save_register
if filetype_output =~ "indent:OFF" && &indentexpr == ''
return
endif
" Make sure the range is in the proper order:
if a:last >= a:first
let firstline = a:first
let lastline = a:last
else
let lastline = a:first
let firstline = a:last
endif
" Make sure the full region to be re-indendted is included:
if a:extraline == 1
if firstline == lastline
let lastline = lastline + 2
else
let lastline = lastline + 1
endif
endif
execute firstline . ',' . lastline . 'norm =='
endfunction
" s:ByteOffset() {{{2
"
" Return the byte number of the current position.
"
" Arguments:
" None
" Return Value:
" The byte offset
function! s:ByteOffset()
return line2byte(line('.')) + col('.') - 1
endfunction
" HTMLnextInsertPoint() {{{2
"
" Position the cursor at the next point in the file that needs data.
"
" Arguments:
" 1 - Character: Optional, the mode the function is being called from. 'n'
" for normal, 'i' for insert. If 'i' is used the function
" enables an extra feature where if the cursor is on the start
" of a closing tag it places the cursor after the tag.
" Default is 'n'.
" Return Value:
" None.
" Known problems:
" Due to the necessity of running the search twice (why doesn't Vim support
" cursor offset positioning in search()?) this function
" a) won't ever position the cursor on an "empty" tag that starts on the
" first character of the first line of the buffer
" b) won't let the cursor "escape" from an "empty" tag that it can match on
" the first line of the buffer when the cursor is on the first line and
" tab is successively pressed
function! HTMLnextInsertPoint(...)
let saveerrmsg = v:errmsg
let v:errmsg = ''
let saveruler = &ruler | let &ruler=0
let saveshowcmd = &showcmd | let &showcmd=0
let byteoffset = s:ByteOffset()
" Tab in insert mode on the beginning of a closing tag jumps us to
" after the tag:
if a:0 >= 1 && a:1 == 'i'
if strpart(getline(line('.')), col('.') - 1, 2) == '</'
normal %
let done = 1
elseif strpart(getline(line('.')), col('.') - 1, 4) =~ ' *-->'
normal f>
let done = 1
else
let done = 0
endif
if done == 1
if col('.') == col('$') - 1
startinsert!
else
normal l
endif
return
endif
endif
normal 0
" Running the search twice is inefficient, but it squelches error
" messages and the second search puts my cursor where it's needed...
if search('<\([^ <>]\+\)\_[^<>]*>\_s*<\/\1>\|<\_[^<>]*""\_[^<>]*>\|<!--\_s*-->', 'w') == 0
if byteoffset == -1
go 1
else
execute ':go ' . byteoffset
if a:0 >= 1 && a:1 == 'i' && col('.') == col('$') - 1
startinsert!
endif
endif
else
normal 0
silent! execute ':go ' . (s:ByteOffset() - 1)
execute 'silent! normal! /<\([^ <>]\+\)\_[^<>]*>\_s*<\/\1>\|<\_[^<>]*""\_[^<>]*>\|<!--\_s*-->/;/>\_s*<\|""\|<!--\_s*-->/e' . "\<CR>"
" Handle cursor positioning for comments and/or open+close tags spanning
" multiple lines:
if getline('.') =~ '<!-- \+-->'
execute "normal F\<space>"
elseif getline('.') =~ '^ *-->' && getline(line('.')-1) =~ '<!-- *$'
normal 0
normal t-
elseif getline('.') =~ '^ *-->' && getline(line('.')-1) =~ '^ *$'
normal k$
elseif getline('.') =~ '^ *<\/[^<>]\+>' && getline(line('.')-1) =~ '^ *$'
normal k$
endif
call histdel('search', -1)
let @/ = histget('search', -1)
endif
let v:errmsg = saveerrmsg
let &ruler = saveruler
let &showcmd = saveshowcmd
endfunction
" s:tag() {{{2
"
" Causes certain tags (such as bold, italic, underline) to be closed then
" opened rather than opened then closed where appropriate, if syntax
" highlighting is on.
"
" Arguments:
" 1 - String: The tag name.
" 2 - Character: The mode:
" 'i' - Insert mode
" 'v' - Visual mode
" Return Value:
" The string to be executed to insert the tag.
" s:smarttags[tag][mode][open/close] = keystrokes {{{
" tag - The literal tag, without the <>'s
" mode - i = insert, v = visual
" (no "o", because o-mappings invoke visual mode)
" open/close - c = When inside an equivalent tag, close then open it
" o = When not inside an equivalent tag
" keystrokes - The mapping keystrokes to execute
let s:smarttags = {}
let s:smarttags['i'] = {
\ 'i': {
\ 'o': "<[{I></I}]>\<C-O>F<",
\ 'c': "<[{/I><I}]>\<C-O>F<",
\ },
\ 'v': {
\ 'o': "`>a</[{I}]>\<C-O>`<<[{I}]>",
\ 'c': "`>a<[{I}]>\<C-O>`<</[{I}]>",
\ }
\ }
let s:smarttags['em'] = {
\ 'i': {
\ 'o': "<[{EM></EM}]>\<C-O>F<",
\ 'c': "<[{/EM><EM}]>\<C-O>F<",
\ },
\ 'v': {
\ 'o': "`>a</[{EM}]>\<C-O>`<<[{EM}]>",
\ 'c': "`>a<[{EM}]>\<C-O>`<</[{EM}]>",
\ }
\ }
let s:smarttags['b'] = {
\ 'i': {
\ 'o': "<[{B></B}]>\<C-O>F<",
\ 'c': "<[{/B><B}]>\<C-O>F<",
\},
\ 'v': {
\ 'o': "`>a</[{B}]>\<C-O>`<<[{B}]>",
\ 'c': "`>a<[{B}]>\<C-O>`<</[{B}]>",
\ }
\ }
let s:smarttags['strong'] = {
\ 'i': {
\ 'o': "<[{STRONG></STRONG}]>\<C-O>F<",
\ 'c': "<[{/STRONG><STRONG}]>\<C-O>F<",
\},
\ 'v': {
\ 'o': "`>a</[{STRONG}]>\<C-O>`<<[{STRONG}]>",
\ 'c': "`>a<[{STRONG}]>\<C-O>`<</[{STRONG}]>",
\ }
\ }
let s:smarttags['u'] = {
\ 'i': {
\ 'o': "<[{U></U}]>\<C-O>F<",
\ 'c': "<[{/U><U}]>\<C-O>F<",
\},
\ 'v': {
\ 'o': "`>a</[{U}]>\<C-O>`<<[{U}]>",
\ 'c': "`>a<[{U}]>\<C-O>`<</[{U}]>",
\ }
\ }
let s:smarttags['comment'] = {
\ 'i': {
\ 'o': "<!-- -->\<C-O>F ",
\ 'c': " --><!-- \<C-O>F<",
\},
\ 'v': {
\ 'o': "`>a -->\<C-O>`<<!-- ",
\ 'c': "`>a<!-- \<C-O>`< -->",
\ }
\ }
" }}}
function! s:tag(tag, mode)
let attr=synIDattr(synID(line('.'), col('.') - 1, 1), "name")
if ( a:tag == 'i' && attr =~? 'italic' )
\ || ( a:tag == 'em' && attr =~? 'italic' )
\ || ( a:tag == 'b' && attr =~? 'bold' )
\ || ( a:tag == 'strong' && attr =~? 'bold' )
\ || ( a:tag == 'u' && attr =~? 'underline' )
\ || ( a:tag == 'comment' && attr =~? 'comment' )
let ret=s:ConvertCase(s:smarttags[a:tag][a:mode]['c'])
else
let ret=s:ConvertCase(s:smarttags[a:tag][a:mode]['o'])
endif
if a:mode == 'v'
" If 'selection' is "exclusive" all the visual mode mappings need to
" behave slightly differently:
let ret = substitute(ret, "`>a\\C", "`>i" . s:VI(), 'g')
endif
return ret
endfunction
" s:DetectCharset() {{{2
"
" Detects the HTTP-EQUIV Content-Type charset based on Vim's current
" encoding/fileencoding.
"
" Arguments:
" None
" Return Value:
" The value for the Content-Type charset based on 'fileencoding' or
" 'encoding'.
" TODO: This table needs to be expanded:
let s:charsets = {}
let s:charsets['latin1'] = 'iso-8859-1'
let s:charsets['utf_8'] = 'UTF-8'
let s:charsets['utf_16'] = 'UTF-16'
let s:charsets['shift_jis'] = 'Shift_JIS'
let s:charsets['euc_jp'] = 'EUC-JP'
let s:charsets['cp950'] = 'Big5'
let s:charsets['big5'] = 'Big5'
function! s:DetectCharset()
if exists("g:html_charset")
return g:html_charset
endif
if &fileencoding != ''
let enc=tolower(&fileencoding)
else
let enc=tolower(&encoding)
endif
" The iso-8859-* encodings are valid for the Content-Type charset header:
if enc =~? '^iso-8859-'
return enc
endif
let enc=substitute(enc, '\W', '_', 'g')
if s:charsets[enc] != ''
return s:charsets[enc]
endif
return g:html_default_charset
endfunction
" HTMLgenerateTable() {{{2
"
" Interactively creates a table.
"
" Arguments:
" None
" Return Value:
" None
function! HTMLgenerateTable()
let byteoffset = s:ByteOffset()
let rows = inputdialog("Number of rows: ") + 0
let columns = inputdialog("Number of columns: ") + 0
if ! (rows > 0 && columns > 0)
echo "Rows and columns must be integers."
return
endif
let border = inputdialog("Border width of table [none]: ") + 0
if border
execute s:ConvertCase("normal o<[{TABLE BORDER}]=" . border . ">\<ESC>")
else
execute s:ConvertCase("normal o<[{TABLE}]>\<ESC>")
endif
for r in range(rows)
execute s:ConvertCase("normal o<[{TR}]>\<ESC>")
for c in range(columns)
execute s:ConvertCase("normal o<[{TD}]>\<CR></[{TD}]>\<ESC>")
endfor
execute s:ConvertCase("normal o</[{TR}]>\<ESC>")
endfor
execute s:ConvertCase("normal o</[{TABLE}]>\<ESC>")
execute ":go " . (byteoffset <= 0 ? 1 : byteoffset)
normal jjj^
endfunction
" s:MappingsControl() {{{2
"
" Disable/enable all the mappings defined by HTMLmap()/HTMLmapo().
"
" Arguments:
" 1 - String: Whether to disable or enable the mappings:
" d/disable: Clear the mappings
" e/enable: Redefine the mappings
" r/reload: Completely reload the script
" h/html: Reload the mapppings in HTML mode
" x/xhtml: Reload the mapppings in XHTML mode
" Return Value:
" None
silent! function! s:MappingsControl(dowhat)
if ! exists('b:did_html_mappings_init')
echohl ErrorMsg
echomsg "The HTML mappings were not sourced for this buffer."
echohl None
return
endif
if b:did_html_mappings_init < 0
unlet b:did_html_mappings_init
endif
if a:dowhat =~? '^d\(isable\)\=\|off$'
if exists('b:did_html_mappings')
silent execute b:HTMLclearMappings
unlet b:did_html_mappings
if exists("g:did_html_menus")
call s:MenuControl('disable')
endif
elseif ! exists('s:quiet_errors')
echohl ErrorMsg
echomsg "The HTML mappings are already disabled."
echohl None
endif
elseif a:dowhat =~? '^e\(nable\)\=\|on$'
if exists('b:did_html_mappings')
echohl ErrorMsg
echomsg "The HTML mappings are already enabled."
echohl None
else
execute "source " . s:thisfile
if exists('b:HTMLextraMappings')
let s:doing_extra_html_mappings = 1
silent execute b:HTMLextraMappings
unlet s:doing_extra_html_mappings
endif
endif
elseif a:dowhat =~? '^r\(eload\|einit\)\=$'
let s:quiet_errors = 1
HTMLmappings off
let b:did_html_mappings_init=-1
silent! unlet g:did_html_menus g:did_html_toolbar g:did_html_functions
silent! unmenu HTML
silent! unmenu! HTML
HTMLmappings on
unlet s:quiet_errors
elseif a:dowhat =~? '^h\(tml\)\=$'
if exists('b:html_tag_case_save')
let b:html_tag_case = b:html_tag_case_save
endif
let b:do_xhtml_mappings=0
HTMLmappings off
let b:did_html_mappings_init=-1
HTMLmappings on
elseif a:dowhat =~? '^x\(html\)\=$'
let b:do_xhtml_mappings=1
HTMLmappings off
let b:did_html_mappings_init=-1
HTMLmappings on
else
echohl ErrorMsg
echomsg "Invalid argument: " . a:dowhat
echohl None
endif
endfunction
command! -nargs=1 HTMLmappings call <SID>MappingsControl(<f-args>)
" s:MenuControl() {{{2
"
" Disable/enable the HTML menu and toolbar.
"
" Arguments:
" 1 - String: Optional, Whether to disable or enable the mappings:
" empty: Detect which to do
" "disable": Disable the menu and toolbar
" "enable": Enable the menu and toolbar
" Return Value:
" None
function! s:MenuControl(...)
if a:0 > 0
if a:1 !~? '^\(dis\|en\)able$'
echoerr "Invalid argument: " . a:1
return
else
let bool = a:1
endif
else
let bool = ''
endif