" ragtag.vim - Ghetto XML/HTML mappings (formerly allml.vim)
" Author: Tim Pope
" Version: 2.0
" GetLatestVimScripts: 1896 1 :AutoInstall: ragtag.vim
if exists("g:loaded_ragtag") || &cp || v:version < 700
finish
endif
let g:loaded_ragtag = 1
if !exists('g:html_indent_inctags')
let g:html_indent_inctags = 'body,head,html,tbody,p,li,dt,dd'
endif
if !exists('g:html_indent_autotags')
let g:html_indent_autotags = 'wbr'
endif
if !exists('g:html_indent_script1')
let g:html_indent_script1 = 'inc'
endif
if !exists('g:html_indent_style1')
let g:html_indent_style1 = 'inc'
endif
augroup ragtag
autocmd!
autocmd BufReadPost * if ! did_filetype() && getline(1)." ".getline(2).
\ " ".getline(3) =~? '<\%(!DOCTYPE \)\=html\>' | setf html | endif
autocmd FileType *html*,wml,jsp,gsp,mustache,smarty call s:Init()
autocmd FileType php,asp*,cf,mason,eruby,liquid,jst,eelixir call s:Init()
autocmd FileType xml,xslt,xsd,docbk call s:Init()
autocmd FileType javascript.jsx,jsx,javascriptreact,handlebars call s:Init()
autocmd FileType typescript.tsx,tsx,typescriptreact call s:Init()
autocmd InsertLeave * call s:Leave()
autocmd CursorHold * if exists("b:loaded_ragtag") | call s:Leave() | endif
augroup END
inoremap ragtagHtmlComplete =htmlEn()=htmlDis()
" Public interface, for if you have your own filetypes to activate on
function! RagtagInit()
call s:Init()
endfunction
function! AllmlInit()
call s:Init()
endfunction
function! s:isFiletype(ft)
return index(split(&filetype, '\.'), a:ft) >= 0
endfunction
function! s:Init()
let b:loaded_ragtag = 1
if s:subtype() == "xml"
imap O
else
imap ] >O
endif
" <% %>
if s:isFiletype('eruby') || s:isFiletype('jst')
inoremap - <%%>2hi
inoremap _ I<%A%>Fs
elseif s:isFiletype('cf')
inoremap -
inoremap _
elseif s:isFiletype('smarty')
imap - >i
imap _ IA>Fs
else
imap - >2hi
imap _ IA>Fs
endif
" Comments
if s:isFiletype('aspperl') || s:isFiletype('aspvbs')
imap ' '>2hi
imap " I'A>Fs
let b:surround_35 = maparg("","i")."' \r ".maparg(">","i")
elseif s:isFiletype('jsp') || s:isFiletype('gsp')
inoremap ' %----%>4hi
inoremap " I<%--A--%>Fs
let b:surround_35 = "<%-- \r --%>"
elseif s:isFiletype('cf')
inoremap ' !------>4hi
inoremap " IFs
setlocal commentstring=
let b:surround_35 = ""
elseif &filetype ==# 'html' || &filetype ==# 'xml' || &filetype ==# 'xhtml'
inoremap ' !---->3hi
inoremap " IFs
let b:surround_35 = ""
elseif s:isFiletype('django') || s:isFiletype('htmldjango') || s:isFiletype('htmljinja')
inoremap ' {##}2hi
inoremap " I{#A#}Fs
let b:surround_35 = "{# \r #}"
elseif s:isFiletype('liquid')
inoremap ' {%comment%}{%endcomment%}15hi
inoremap " I{%comment%}A{%endcomment%}Fs
let b:surround_35 = "{% comment %}\r{% endcomment %}"
elseif s:isFiletype('smarty')
inoremap ' {**}2hi
inoremap " I{*A*}Fs
let b:surround_35 = "{* \r *}"
elseif s:isFiletype('handlebars')
imap "
endif
endif
return s:insertTag(b:ragtag_javascript_include_tag)
endfunction
function! s:insertTag(tag)
let tag = a:tag
if s:subtype() == "html"
let tag = substitute(a:tag,'\s*/>','>','g')
endif
let before = matchstr(tag,'^.\{-\}\ze\r')
let after = matchstr(tag,'\r\zs\%(.*\r\)\@!.\{-\}$')
" middle isn't currently used
let middle = matchstr(tag,'\r\zs.\{-\}\ze\r')
return before.after.s:repeat("\",s:length(after))
endfunction
function! s:htmlEn()
let b:ragtag_omni = &l:omnifunc
let b:ragtag_isk = &l:isk
let b:ragtag_completeopt = &l:completeopt
" : is for namespaced xml attributes
setlocal omnifunc=htmlcomplete#CompleteTags isk+=: completeopt=
return ""
endfunction
function! s:htmlDis()
if exists("b:ragtag_omni")
let &l:omnifunc = b:ragtag_omni
unlet b:ragtag_omni
endif
if exists("b:ragtag_isk")
let &l:isk = b:ragtag_isk
unlet b:ragtag_isk
endif
if exists("b:ragtag_completeopt")
let &l:completeopt = b:ragtag_completeopt
unlet b:ragtag_completeopt
endif
return ""
endfunction
function! s:subtype()
let top = getline(1)."\n".getline(2)
if (top =~ '' && &ft !~? 'html') || &ft =~? '^\%(xml\|xsd\|xslt\|docbk\)$'
return "xml"
elseif top =~? '\'
return 'xhtml'
elseif top =~? ''
return 'html5'
elseif top =~? '[^<]\'
return "html"
elseif s:isFiletype('xhtml')
return "xhtml"
elseif exists("b:loaded_ragtag")
return "html5"
else
return ""
endif
endfunction
function! s:closetagback()
if s:subtype() == "html"
return ">\"
else
return " />\\\"
endif
endfunction
function! s:closetag()
if s:subtype() == "html"
return ">"
else
return " />"
endif
endfunction
function! s:charset()
let enc = &fileencoding
if enc == ""
let enc = &encoding
endif
if enc == "latin1"
return "ISO-8859-1"
elseif enc == ""
return "US-ASCII"
else
return enc
endif
endfunction
function! s:charsetTag()
if s:subtype() == 'html5'
return ' urlspace =getinput()=~?'\%([?&]\&\)[%a-z0-9._~+-]*=[%a-z0-9._~+-]*$'?'+':'%20'
function! s:urltab(htmlesc)
let line = s:getinput()
let g:line = line
if line =~ '[^ <>"'."'".']\@"'."'".']\@ ragtagBSUrl
inoremap %0A
imap