" HTML Commands. " Author: Michael Geddes " Version: 2.2 " Please feel free to use and modify all or part of this script. " I would appreciate being acknowledged in any derived scripts, and would " appreciate any updates/modifications. " Define htmlcmd_NOHEADER to not include style " Define htmlcmd_BF for some of Benji's commands that haven't been adopted quite yet. " Define htmlcmd_NOAUTOCLOSE to not map > to auto-close open tags. " &usersign is your username for modification " This code allows htmlcmd to behave as a FT plugin and as a buffoptions.vim " style plugin. if !exists('DoingSOURCE') if exists("b:did_ftplugin") finish endif if exists('*ReadFileTypeMap') SO finish endif " Else we assume we are behaving as a plugin endif function! s:HTMLCloseAll( line1, column1, line2, column2, cline,ccolumn) if g:htmlCmd_debug| call input( a:line1.':'.a:column1.':'.a:line2.':'.a:column2) |endif let ic=&ic set ic let cursor=-1 let foundcursor=0 if(a:line1==a:line2) let txt=strpart(getline(a:line1),a:column1, a:column2-a:column1) if a:line1==a:cline let cursor=a:ccolumn-a:column1 let foundcursor=1 endif else let txt=strpart(getline(a:line1),a:column1,65535) let here=a:line1+1 let cursor=0 while(here < a:line2) let ll=getline(here) if !foundcursor if here==a:cline let foundcursor=1 let cursor=cursor+a:ccolumn else let cursor=cursor+strlen(ll) endif endif let txt=txt.ll let here=here+1 endwhile let txt=txt.strpart(getline(a:line2),0,a:column2) endif let pastcursor=0 let stillopen='' let badlist='' let mx='<\s*\(/\=\)\(\k\+\)\>[^>]\{-}\(/\=\)>' " if g:htmlCmd_debug| call input( txt) |endif let indextotal =0 while txt != "" let index=match(txt,mx) if index < 0 break endif let match=matchstr(txt,mx) if g:htmlCmd_debug| echo '-----'.'^.\{'.index.'}'.mx |endif let lenpart=index+strlen(match) let indextotal=indextotal+lenpart let txt=strpart(txt,lenpart,65535) if foundcursor && !pastcursor && indextotal >= cursor let pastcursor=1 let openbeforecursor=stillopen endif let type=substitute(match,mx,'\1','') let seq=substitute(match,mx,'\2','') let endtype=substitute(match,mx,'\3','') if endtype=='' if g:htmlCmd_debug| echo stillopen |endif if type=='/' if stillopen !~? '\<'.seq.'\>,' let badlist=badlist.',' "break elseif pastcursor && openbeforecursor ==? stillopen if g:htmlCmd_debug|echo 'END:'.seq.':'.stillopen|endif let stillopen=substitute(stillopen,'\c\<'.seq.'\>,.\{-}$','','i') break else let my='\(^.\{-}\)\<'.seq.'\>,' let badopen=substitute(matchstr(stillopen,my),my,'\1','') let badlist=badlist.substitute(badopen,'\<\k\+\>','<&>','g') let stillopen=substitute(stillopen,my,'','i') endif else let stillopen=seq.','.stillopen endif endif endwhile let stillopen=substitute(stillopen,'\<\k\+\>','','g') let stillopen=substitute(stillopen,',','','g') if badlist!='' echo "Badly formed tags :".substitute(badlist,',$','','') endif let &ic=ic if g:htmlCmd_debug| call input(stillopen ) |endif return stillopen endfun if !exists("g:htmlCmd_debug") let g:htmlCmd_debug=0 endif function! s:FindBack(option) return s:Find__Back(a:option,1) endfun function! s:Find__Fwd() let mx='<\s*\(/\=\)\(\k\+\)\>[^>]*>' let curline=line('.') let curcol=col('.') let text=getline(curline) let text=strpart(text,curcol,65535) while 1 endwhile endfun function! s:Find__Back(option, insertit) let ws=&ws let ic=&ic let sc=&sc set nows set ic set nosc let stillopen="" let find='<\(/\=\)'.a:option.'\>[^>]*>' let here=line('.') let text=strpart(getline('.'),0,col('.')+1) let end="" let closed="" while 1 let t='' let i=matchend(text,find) if g:htmlCmd_debug | call input(text.':'.i) |endif let column=0 let str=0 while i >= 0 let str=match(text,find) if g:htmlCmd_debug | call input(column.':'.text.':'.str) |endif let t=matchstr(text,find) let text=strpart(text,i,1024) let i=matchend(text,find) if i >=0 let column=column+str endif endwhile let column=column+str if g:htmlCmd_debug| call input('<'.str.'>'.column) |endif if t != '' let t=substitute(t,find,'\1','') if t != '/' let end='/' if ! a:insertit let column = column+2 endif let closed=s:HTMLCloseAll(here, column, line('.'),col('.')+1,0,0) endif break endif let here=here-1 if here<0 break endif let text=getline(here) endwhile if end=="" && a:insertit let openit='<'.a:option.'>' else let openit="" endif let &sc=sc let &ic=ic let &ws=ws return closed.openit endfun if has('menu') amenu H&TML.GenerateTOC :call GenerateTOC() endif function! GenerateTOC(...) let types="" if a:0 > 0 let types=a:1 endif let line=1 let found=0 let TOC=0 let end=line('$') while line' let TOC=line if getline(line+1) !~ "" exe (line+1).',//-1 d' endif let found=1 break elseif getline(line) =~ 'Table [oO]f Contents' let TOC=line endif let line=line+1 endwhile if TOC ==0 exe "norm /\\C/>\a\

Table of Contents

\\" let TOC=line('.')-1 endif if !found if g:htmlCmd_debug| echo '------' |endif call append(TOC,"") let TOC=TOC+1 call append(TOC,"") let end=end+2 endif let line=TOC let sTOC="" let firstlevel=0 let lastlevel=0 while line < end let hx='\(<[hH]\([1-5]\)>\)\s*\(.\{-}\)\(]*\)>\(.\{-}\)' if txt!~xrf let refname=substitute(substitute(txt,'\W','','g'),'<[^>]*>','','g') " let refname=substitute(refname,'&\k\+\>;\=','','g') call setline(line,substitute(here,hx,'\1'.escape(txt,'\&' ) . '\4','')) else let refname=substitute(matchstr(txt,xrf),xrf,'\1','') let txt=substitute(matchstr(txt,xrf),xrf,'\2','') endif let ty=types[lvl-1] if ty!="" let ty=' type="'.ty.'"' endif if lastlevel==0 let firstlevel=lvl-1 let lastlevel=lvl let sTOC=sTOC."\" elseif lvl <= firstlevel break elseif lastlevel < lvl while lastlevel < lvl let lastlevel=lastlevel+1 let sTOC=sTOC."\" endwhile elseif lastlevel > lvl while lastlevel > lvl let sTOC=sTOC."\" let lastlevel=lastlevel-1 endwhile endif let sTOC=sTOC."\".'
  • '.txt.'
  • ' endif let line=line+1 endwhile while lastlevel > firstlevel let sTOC=sTOC."\" let lastlevel=lastlevel-1 endwhile exe TOC exe 'norm o'.sTOC."\" endfun if v:version < 600 fun! s:Surround(begin,end) range exe "norm `>a".a:end."\" exe "norm `" endfun else fun! s:Surround(begin,end) range exe "norm `>a".a:end."\`" endfun endif fun! s:SurroundHTM(tag) range call s:Surround('<'.a:tag.'>','') endfun "FileTypes: html xml vmap imap =FindBack('big') imap =FindBack('b') imap =FindBack('i') imap =FindBack('u') imap =FindBack('p') imap =FindBack('kbd') imap =FindBack('td') vmap :call SurroundHTM('big') vmap :call SurroundHTM('b') vmap :call SurroundHTM('i') vmap :call SurroundHTM('u') vmap :call SurroundHTM('p') vmap :call SurroundHTM('kbd') vmap :call SurroundHTM('td') nmap i nmap i nmap i nmap i nmap i "nmap i nmap i imap
  • F i vmap :call SurroundHTM('li') imap
    F nmap o imap ul>F nmap o if !exists('htmlcmd_NOHEADER') imap =substitute(g_html_htmlheader,'%VERSION%',strpart(v:version,0,1).'.'.strpart(v:version,1,1),'') endif vmap :call SurroundHTM('h1') vmap :call SurroundHTM('h2') vmap :call SurroundHTM('h3') vmap :call SurroundHTM('h4') vmap :call SurroundHTM('h5') vmap :call SurroundHTM('h6') imap =FindBack('h1') imap =FindBack('h2') imap =FindBack('h3') imap =FindBack('h4') imap =FindBack('h5') imap =FindBack('h6') nmap i nmap i nmap i nmap i nmap i nmap i imap o imap br/> imap hr/> imap &   imap && & imap &< < imap &> > if 0 imap &'< ‘ imap &'> ’ imap &"< “ imap &"> ” endif fun! s:HTMLQuote( type) if col('.') ==1 || getline('.')[col('.')-2] =~'\s' return '&l'.a:type.';' else return '&r'.a:type.';' endif endfun imap &' =HTMLQuote('squo') imap &" =HTMLQuote('dquo') "fun! HTMLFindQuote( quotename ) " if a:quotename = "'" " let q='squo' " elseif a:quotename ='"' " let q='dquo' " else " return "no" " endif " let oldline=line('.') " let line=search( '&\([lr]\)'.q, 'bW' ) > 0 " if line>0 " let type = substitu " let type= " endif "endfun "imap if exists("htmlcmd_BF") imap < imap > > imap & imap imap endif " Add a html link to the location stored in the windows clipboard " (external link) " Add internal link to loc. in reg 0 nmap ,al "=''p nmap ,aL "='
  • 'p nmap ,l "=''p "nmap ,yf :let @*='file:///'.expand('%:p:gs+\+/+') nmap ,yf :let @*=URLPathOf(expand('%:p')) fun! s:URLPathOf( filename) let file=s:UNCPathOf(a:filename) if file =~ '^//' return 'file:'.file else return 'file:///'.file endif endfun if has('win32') fun! s:UNCPathOf( filename ) let shares=system('net share') let shares=substitute(shares,"\n[^ \n]\\+[$]\\s[^\n]\\+","",'g') let newname='' let current=fnamemodify(a:filename,':gs+/+\\+') let do_it=1 while do_it let tx=fnamemodify(current, ':h') if tx==current let do_it=0 else let current=tx let mx= '\c'."\n".'\([^ '."\n".']\+\)\s\+'.escape(current,'$\.&').'\s\+' let match=matchstr(shares,mx) if match != "" let sharename=substitute(match,mx,'\1','') let drivepart=substitute(strpart(a:filename, strlen(current)),'\\','/','g') if drivepart!~ '^/' let drivepart='/'.drivepart endif return '//'.$COMPUTERNAME.'/'.sharename.drivepart endif endif endwhile return a:filename endfun else fun! s:UNCPathOf( filename ) return filename endfun endif fun! s:EndTag() let l=line(".") let c=col(".") let txt=strpart(getline(l),c) if txt[0]=='/' return s:CloseLast(1) " return s:Find__Back( matchstr(txt,'\k\+') ,0 ) endif return '' endfun fun! s:CloseLast( returnall ) let reA='\v\<(/=<\k+>)([^/>"]+|"[^"]*")*[/>]' let reMatch='\c\v\<(<\k+>)\>.{-}\\>' if &syntax == 'html' let reSingles='br|hr' else let reSingles='--' endif let reIncompleteStart='<.*$' let reIncompleteEnd='^.\{-}>' let l=line('.') let txt=strpart(getline(l),0,col('.')-1) " Search Forwards for the next enclosing let fl=line('.') let ftxt=strpart(getline(fl),col('.')-1) let ahead='' while fl < line('$') let i=0 while 1 let j=matchend(ftxt,reA, i) if j==-1 break else if ftxt[j-1]=='>' let word=substitute(matchstr(ftxt,reA,i),reA,'\1','') if word !~ '\c\v<('.reSingles.')>' let ahead=ahead.'<'.word.'>' endif let i=j else let i=j+1 endif endif endwhile let ahead=substitute(ahead, reMatch, '', 'g') if ahead =~ '\ze\>') " Search backwards for the unclosed let left='' while l > 1 let i=0 let lleft='' while 1 let j=matchend(txt,reA, i) if j==-1 break else if txt[j-1]=='>' let word=substitute(matchstr(txt,reA,i),reA,'\1','') if word !~ '\v\c<('.reSingles.')>' let lleft=lleft.'<'.word.'>' endif let i=j else let i=j+1 endif endif endwhile let left=substitute(lleft.left, reMatch, '', 'g') if left =~ '<[^/]' if ! a:returnall || matchend(left,'\c\v\<'.closedahead.'\>')== strlen(left) break endif endif let l=l-1 let iend=matchend(txt,reIncompleteEnd) let first=matchend(txt,reA) if iend>=0 && txt[iend-1] == '>' && (first < 0 || ( iend < first )) let txt=getline(l).strpart(txt,0,iend) else let txt=getline(l) endif endwhile let left = substitute(left,'\v^.*\\>', '', 'g') let left=substitute(left,'\c^.*<'.closedahead.'>','','') if a:returnall let ret='' while left != '' let ret=matchstr(left,'^<[^>]*>').ret let left=substitute(left,'^<[^>]*>','','') endwhile return substitute(ret,'\v<\k+>','/&','g') else return substitute(matchstr(left,'\v<\k+>\>$'),'\<',' =CloseLast(0) imap =CloseLast(1) imap º "FileTypes: html,xml " Close everything that hasn't been closed if ! exists('htmlcmd_NOAUTOCLOSE') inoremap > >mzF<=EndTag()f> endif " Close everything imap ¾ > imap > =HTMLCloseAll(0,0,line('$')+1,0,line('.'),col('.')) if !exists("usersign") let usersign=$USERNAME endif if !exists('htmlcmd_NOHEADER') aug htmlwrite " In earlier versions, a failed ex // didn't cause an error if v:version < 600 au! BufWrite *.html /\(\[^<]*\s*'."'".',"'.strftime("%d %b %Y %H:%M").' by '.usersign.'",""))' else au! BufWrite *.html sil! /\(\[^<]*\s*'."'".',"\\1'.strftime("%d %b %Y %H:%M").' by '.usersign.'",""))' endif aug END let g_html_gocursor="\?#CURSOR#\cf#" let g_html_htmlheader=" \\ \\ \\ \\ \\ \#CURSOR#\ \\ \\\ \\ \
    \ \\ \\ \
    \ \Last Modified: 00 Aaa 0000 00:00 by NobodyPowered by Vim
    \ \\ \".g_html_gocursor endif