root/woc/woc.vim

Revision 1077, 25.0 kB (checked in by alpt, 1 year ago)

asoidjasid

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1
2 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
3 " Name:         woc.vim
4 " Shortdesc:    WOC client for Vim
5 "
6 " Desc:         Detailed documentation can be found in
7 "               http://dev.dyne.org/trac.cgi/wiki/Woc
8 "               
9 "               See the woc.psy file for an overview of the internals of this
10 "               script, or jump to {-overview-}
11 "
12 " WARNINGS:     - Right now this plugin is not portable! It should work only
13 "                 in a GNU environment (tested), and maybe in a *BSD one (not
14 "                 tested).
15 "               
16 "               - ~/.vim/woc/ must be a writable directory.
17 "                 It is hardcoded, its path cannot cannot be changed.
18 "
19 "               - ~/.vim/woc/ must contain the {-wocdownloader-} script!
20 "
21 " Dependencies: woc.vim depends on these vim plugins:
22 "                       netrw
23 "               and on these shell programs:
24 "                       wget, sed, awk, cut
25 "
26 " Usage:        In the simplest form, just use CTRL-] and CTRL-T to respectively
27 "               jump to a tag and to go back.
28 "               Use    :Woctags help   to get online help.
29 "               
30 "               The :Woctags command executes a tag command
31 "               It's optional arguments are:
32 "
33 "                       :Woctags [tagcmd [tag]]
34 "               
35 "               `tagcmd' specifies the tag command to be executed. It can be
36 "               one of the following:
37 "
38 "                       --- WoC tags commands ---
39 "                       j:  Jump to this tag (default)
40 "                       r:  Reverse jump: find references to this tag
41 "                       fj: File Jump: Open this file
42 "               
43 "                       --- cscope find commands ---
44 "                       c: Find functions calling this function
45 "                       d: Find functions called by this function
46 "                       e: Find this egrep pattern
47 "                       f: Find this file
48 "                       g: Find this definition
49 "                       i: Find files #including this file
50 "                       s: Find this C symbol
51 "                       t: Find assignments to
52 "
53 "               `tag' is the tag. If it's not specified, it will be set to the
54 "               tag under the cursor.
55 "
56 "               The command specified in `tag' takes priority over that
57 "               specified in `tagcmd'.
58 "               For example, if the tag is {-mytag:"c-} and `tagcmd' is 'j', then
59 "               the action would be "Jump to this tag" and not "Find functions
60 "               calling this function"
61 "
62 " Mappings:     These mappings are automatically defined.
63 "
64 "                       <C-]>   -->  :Woctags
65 "       
66 "                       <C-_>s  -->  :Woctags s
67 "                       <C-_>g  -->  :Woctags g
68 "                       <C-_>c  -->  :Woctags c
69 "                       <C-_>t  -->  :Woctags t
70 "                       <C-_>e  -->  :Woctags e
71 "                       <C-_>r  -->  :Woctags r
72 "                       <C-_>f  -->  :Woctags fj
73 "                       <C-_>i  -->  :Woctags i
74 "                       <C-_>d  -->  :Woctags d
75 "
76 "               If you don't like them, put the following line inside
77 "               your .vimrc and define there your own mappings:
78 "
79 "                       let g:woc_mappings = 1
80 "
81 "
82 " Options:      If is g:woc_clear_cache is set to 1 (default), the woc cache
83 "               will be automatically cleaned on exit.
84 "
85 "               If the cache isn't cleaned, then the files, which are already
86 "               there, won't be downloaded a second time. This is useful is
87 "               you want to do an "offline browsing".
88 "
89 "               If g:woc_syntax_hl is set to 1 (default), the WoC tags will be
90 "               highlighted.
91 "
92 "               g:woc_global_index can be set to a global ``index.woc'' file.
93 "               For example, you can put something like this in your .vimrc:
94 "                       
95 "                       let g:woc_global_index = "/my/path/to/my_index.woc"
96 "
97 " Version:      1.2
98 " Author:       AlpT (@freaknet.org)
99 " Licence:      This program is free software; you can redistribute it
100 "               and/or modify it under the terms of the GNU General Public
101 "               License.  See http://www.gnu.org/copyleft/gpl.txt
102 "
103 " Credits:      Katolaz, Efphe, frogonwheels
104 "
105 " RippedSrc:    Some codes here derive from the following plugins:
106 "               
107 "                 Vimball, Vimspell
108 "
109 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
110
111 if exists("g:loaded_wocscript") || exists("g:woc_disabled")
112         finish
113 endif
114 let g:loaded_wocscript=1
115 let s:save_cpo = &cpo
116 set cpo&vim
117
118 augroup WOC
119   au!
120    au BufReadPre                *       call s:WOCOpenFile(expand("<afile>"))
121    au BufReadPost               *       if g:woc_syntax_hl|call s:WOCSyntax()|endif
122
123    " {-TODO wocpath-} . It is hardcoded.
124    au BufReadCmd        */woc/cache/*   call s:WOCOpenFile(expand("<afile>"))
125
126    au VimLeave                  *       if g:woc_clear_cache|call s:WOCClearCache()|endif
127 augroup END
128
129 " User command
130 com! -nargs=*           Woctags     call s:WOCTagCmd(<f-args>)
131 com! -nargs=*           WocClearCache     call s:WOCClearCache(1)
132
133 " Add our mappings
134 if !exists("g:woc_mappings") || !g:woc_mappings
135         nmap <C-]>  :call <SID>WOCTagCmd()<cr>
136
137         " See {-TODO mouse support-}
138         "nmap <C-LeftMouse>  :call <SID>WOCTagCmd()<cr>
139         "nmap g<LeftMouse>  :call <SID>WOCTagCmd()<cr>
140
141         nmap <C-_>s :Woctags s<cr>
142         nmap <C-_>g :Woctags g<cr>
143         nmap <C-_>c :Woctags c<cr>
144         nmap <C-_>t :Woctags t<cr>
145         nmap <C-_>e :Woctags e<cr>
146         nmap <C-_>r :Woctags r<cr>
147         nmap <C-_>f :Woctags fj<cr>
148         nmap <C-_>i :Woctags i<cr>
149         nmap <C-_>d :Woctags d<cr>
150
151         let g:woc_mappings = 1
152 endif
153
154 " Initialize:
155 "
156 if !exists("g:woc_index_fname")
157         let g:woc_index_fname="index.woc"
158 endif
159 if !exists("g:woc_global_index")
160         let g:woc_global_index=""
161 endif
162 if !exists("g:woc_clear_cache")
163         let g:woc_clear_cache = 1
164 endif
165 if !exists("g:woc_syntax_hl")
166         let g:woc_syntax_hl = 1
167 endif
168
169 " WOCHome: determine/get home directory path (usually from rtp)
170 " (Function ripped from the VimBall plugin)
171 fun! s:WOCHome()
172         " go to vim plugin home
173         for home in split(&rtp,',') + ['']
174                 if isdirectory(home) && filewritable(home) | break | endif
175         endfor
176         if home == ""
177                 " just pick the first directory
178                 let home= substitute(&rtp,',.*$','','')
179         endif
180         if (has("win32") || has("win95") || has("win64") || has("win16"))
181                 let home= substitute(home,'/','\\','g')
182         endif
183
184         " {-TODO wocpath-} . It is hardcoded.
185         return home . '/woc'
186 endfun
187 if !exists("$WOC_HOME")
188         let $WOC_HOME = s:WOCHome()
189 endif
190
191
192 "
193 fun! s:WOCInitialize()
194         if !exists("b:woc_index")       " Executed for each new buffer
195                 let b:woc_index={
196                         \       "name"          : "",
197                         \       "description"   : "",
198                         \       "alias"         : {},
199                         \       "syn_def_left"     : '|{',
200                         \       "syn_def_right"    : '}|',
201                         \       "syn_ref_left"     : '{-',
202                         \       "syn_ref_right"    : '-}',
203                         \       "tags_file"        : 'auto',
204                         \       "woc_tags_file"    : 'tags.woc',
205                         \       "woc_revtags_file" : 'tags.rev.woc',
206                         \       "tfile_compress"   : 'yes' }
207         endif
208
209         let s:woc_last_path  = !exists("s:woc_cur_path") ? "" :  s:woc_cur_path
210         let s:woc_cur_path   =  expand("%:p:h")
211         let b:woc_cur_path   =  expand("%:p:h")
212
213         if !exists("s:woc_indexes")     " Executed only once
214                 let s:woc_indexes = { s:woc_cur_path : b:woc_index }
215         endif
216         if !exists("s:woc_cache_url")
217                 let s:woc_cache_url = {}
218         endif
219
220         exe "set tags+=" . b:woc_index.woc_tags_file
221 endfun
222
223 "
224 " WOCAddIndexOpt: Parses a:ml and properly adds it to b:woc_index 
225 "
226 " a:ml is a list:
227 "       a:ml[0] is the entire line where the option was defined in index.woc
228 "       a:ml[1] is the option name
229 "       a:ml[2, ...] are the option values
230 "
231 "
232 fun! s:WOCAddIndexOpt(ml)
233         let opt=a:ml[1]
234         let value=a:ml[2]
235
236         if opt == "alias" && value != ''
237        
238                 "call Decho("opt alias: ", opt," ",  value)
239
240                 " `value' is in the form:
241                 "       alias     shortname     url
242                 let al = matchlist(value, '\s*\(\w\+\)\s\+\(.\+\)')
243                 if al == []
244                         echoerr 'WoC: Missing long name for alias "'.al[1] . '". Skipping.'
245                         return
246                 endif
247
248                 let short=al[1]
249                 let long=al[2]
250
251                 if short == "" || long == ""
252                         return
253                 endif
254                 "call Decho("opt alias: short ", short ," long: ", long)
255                 let b:woc_index.alias[short] = substitute(long, '\s*$', '', "e")
256         elseif value == ""
257                 echoerr "WoC: no value specified for " . opt . " Skipping."
258                 return
259         elseif opt != ""
260                 let b:woc_index[opt] = value
261         endif
262 endfun
263
264 fun! s:WoCLoadIndexFile(file)
265         let file=a:file
266
267         if !filereadable(file)
268                 let s:index_loaded=0
269
270                 " No index.woc could be found. Try to read the option from the
271                 " last loaded ones
272                 if has_key(s:woc_indexes, s:woc_cur_path)
273                         let path = s:woc_cur_path
274                 else
275                         let path = s:woc_last_path
276                 endif
277                 if has_key(s:woc_indexes, path)
278                         "call Decho("Loading b:woc_index from s:woc_indexes. path: " . path)
279                         let b:woc_index = s:woc_indexes[path]
280                 endif
281
282                 return
283         endif
284         let s:index_loaded=1
285
286         "call Decho("Loading index.woc")
287
288         let ln=0
289         for line in readfile(file)
290                 let ln+=1
291                 if line =~ '^\s*#' || line =~ "^\s*$"
292                         " skip comments and new lines
293                         continue
294                 endif
295                                          " option       values
296                 let ml = matchlist(line, '\s*\(\w\+\)\s\+\(.*\)')
297                 if ml != []
298                         if !has_key(b:woc_index, ml[1])
299                                 " This option doesn't exist
300                                 echoerr 'WoC: Invalid option "' . ml[1] . '" at '.file.':'. ln . '. Skipping.'
301                                 continue
302                         else
303                                 " Add the option in b:woc_index
304                                 call s:WOCAddIndexOpt(ml)
305                         endif
306                 else
307                         echoerr 'WoC: Invalid syntax at line '.file.':'. ln . '. Skipping.'
308                         continue
309                 endif
310         endfor
311 endfun
312
313 "
314 " WOCLoadIndex: loads "index.woc" and the options defined in it.
315 fun! s:WOCLoadIndex()
316
317         " Initialize
318         call s:WOCInitialize()
319
320         " Load options from the global index
321         if g:woc_global_index != ""
322                 call s:WoCLoadIndexFile(g:woc_global_index)
323         endif
324         let file=expand("%:p:h") . '/' . g:woc_index_fname
325
326         " Load options from ./index.woc
327         call s:WoCLoadIndexFile(file)
328
329         " Save options for future references
330         let s:woc_indexes[s:woc_cur_path]=b:woc_index
331 endfun
332
333 "
334 " WOCExtractTag: returns the WoC tag under the cursor
335 "
336 " A WoC tag is either a standard tagword or a word delimited by
337 " b:woc_index.syn_ref_left and b:woc_index.syn_ref_right
338 "
339 fun! s:WOCExtractTag()
340         "
341         " Extract the tag
342         "
343        
344         " Find the left delimiter of the tag: {-
345         let [lnum, leftcol] = searchpos('\V' . b:woc_index.syn_ref_left, 'bcnW')
346         " Find the right delimiter of the tag: -}
347         let [rnum, rightcol] = searchpos('\V' . b:woc_index.syn_ref_right, 'cnW')
348         let [curbuf, curlin, curcol, curoff]  = getpos(".")
349         " Have we found something?
350         let rlfound=([lnum, leftcol] != [0,0] && [rnum, rightcol] != [0,0] && lnum == rnum && lnum == curlin)
351
352         " Do we have at our left a right delimiter ?
353         let [lrnum, lrightcol] = searchpos('\V' . b:woc_index.syn_ref_right, 'bcnW')
354         let leftright=([lrnum, lrightcol] != [0,0] && lrightcol > leftcol && lrightcol < rightcol && lrnum == curlin)
355         " Do we have at our right a left delimiter ?
356         let [rlnum, rleftcol] = searchpos('\V' . b:woc_index.syn_ref_left, 'cnW')
357         let rightleft=([rlnum, rleftcol] != [0,0] && rleftcol < rightcol && rleftcol > leftcol && rlnum == curlin)
358        
359         "call Decho("TAGextract: " . string([lnum, leftcol]) . string([rnum, rightcol]) . " ", curcol, " ", rlfound, " ", leftright, " ", rightleft, " ", lrightcol, " ", rleftcol)
360         if rlfound && leftcol < curcol && curcol < rightcol && !(leftright && rightleft)
361                 let l=leftcol + strlen(b:woc_index.syn_ref_left) - 1
362                 let r=rightcol - strlen(b:woc_index.syn_ref_right)
363                 let tagword = getline(".")[l : r]
364         else
365                 " Get the tagword in the standard way
366                 let tagword = expand("<cword>")
367                 "call Decho("Falling back to normal tagword")
368         endif
369
370         return tagword
371 endfun
372
373 "
374 " WOCExpandAlias: If `alias' is mapped, it returns its expantion, otherwise
375 "                 "" is returned.
376 fun! s:WOCExpandAlias(alias)
377         if has_key(b:woc_index.alias, a:alias)
378                 return b:woc_index.alias[a:alias]
379         else
380                 return ""
381         endif
382 endfun
383
384
385 "
386 " WOCExtractVIandTAGcmd: Given a tag, it extracts the vi command and the tag
387 "                        command.
388 " It returns [baretag, vicmd, tagcmd]
389 " where baretag is `tag' without vicmd and tagcmd
390 "
391 " If vicmd wasn't specified in the tag, then vicmd = ""
392 " the same applies to tagcmd.
393 "
394 fun! s:WOCExtractVIandTAGcmd(tag)
395
396         " Do you know a better way to do it without 'VERYMAGICCOLON' ? ;)
397         let fullvicmd = substitute(a:tag, '\\:', 'VERYMAGICCOLON', 'g')
398         let baretag   = substitute(fullvicmd, ':[^:]*$', '', '')
399         let baretag   = substitute(baretag, 'VERYMAGICCOLON', ':', 'g')
400         let fullvicmd = matchstr(fullvicmd, ':[^:]*$')
401         let fullvicmd = substitute(fullvicmd, 'VERYMAGICCOLON', ':', 'g')
402        
403         let vicmd  = substitute(fullvicmd, '"[^"]*$', '', '')
404         let tagcmd = matchstr(fullvicmd, '"[^"]*$')[1:]
405
406         return [baretag, vicmd, tagcmd]
407 endfun
408
409 "
410 " WOCFakeTagJump: hack: create a temp tag file, write on it a fake tag, which points 
411 " to a:file as a tag. Finally jump on the tag, and remove the temp file.
412 "
413 fun! s:WOCFakeTagJump(file)
414         let file=a:file
415
416         let tmpfile = tempname()
417         let tag=substitute(tmpfile, '/','', 'g')
418         let tagline = tag . "\t".fnamemodify(file, ":p")."\t"."0"
419         call writefile([tagline], tmpfile)
420         "call Decho("Writing ", tagline," in ", tmpfile)
421         exe "set tags+=" . tmpfile
422         exe "tag ". tag
423         exe "set tags-=" . tmpfile
424         call system("rm " . tmpfile)
425 endfun
426
427
428 "
429 " WOCTagCmd: executes a tag command. This is the main function.
430 "       
431 " `a:1' is one of the following:
432 "
433 "       --- WoC tags commands ---
434 "       j:  Jump to this tag (default)
435 "       r:  Reverse jump: find references to this tag
436 "       fj: File Jump: Open this file
437 "
438 "       --- cscope find commands ---
439 "       c: Find functions calling this function
440 "       d: Find functions called by this function
441 "       e: Find this egrep pattern
442 "       f: Find this file
443 "       g: Find this definition
444 "       i: Find files #including this file
445 "       s: Find this C symbol
446 "       t: Find assignments to
447 "
448 " `a:2' is the tag. If it's not specified, it will be set to the tag
449 " under the cursor.
450 "
451 " The command specified in `a:1' takes priority over that specified in `a:2'.
452 " For example, if the tag is {-mytag:"c-} and `a:1' is 'j', then the action
453 " would be "Jump to this tag" and not "Find functions calling this function"
454 "
455 fun! s:WOCTagCmd(...)
456        
457         let cmd = ""
458         if a:0 >= 1
459                 let cmd = a:1
460         endif
461
462         if cmd == "help"
463                 echo "\nUsage:  Woctags [tagcmd [tag]]\n" .
464 \"              \n".
465 \"      `tagcmd` specifies the tag command to be executed. It can be\n".
466 \"      one of the following:\n".
467 \"\n".
468 \"      --- WoC tags commands ---\n".
469 \"      j:  Jump to this tag (default)\n".
470 \"      r:  Reverse jump: find references to this tag\n".
471 \"      fj: File Jump: Open this file\n".
472 \"\n".
473 \"      --- cscope find commands ---\n".
474 \"        c: Find functions calling this function\n".
475 \"        d: Find functions called by this function\n".
476 \"        e: Find this egrep pattern\n".
477 \"        f: Find this file\n".
478 \"        g: Find this definition\n".
479 \"        i: Find files #including this file\n".
480 \"        s: Find this C symbol\n".
481 \"        t: Find assignments to\n".
482 \"\n"
483                 return
484         endif
485
486
487         " Get the tag under the cursor if a:2 wasn't specified
488         if a:0 == 2
489                 let fulltag = a:2
490         else
491                 let fulltag = s:WOCExtractTag()
492                 "call Decho("D: Extracted tag: ", fulltag)
493         endif
494
495         " Extract the bare tag, the VI cmd, and the TAG cmd
496         let [fulltag, vicmd, tagcmd] = s:WOCExtractVIandTAGcmd(fulltag)
497         let tag=fulltag
498
499         " Extract the URL from the tag (if any)
500         let fullurl = matchstr(fulltag, '.*/')
501         let url = matchstr(fullurl, '[-.[:alnum:]_~]\+/')[:-2]
502         if url != ""
503                 " The tag has a `/' in it, it might be a remote tag
504                 let alias = url
505                
506                 " expand the alias (if any)
507                 let url = s:WOCExpandAlias(alias)
508                 if url == ""
509                         " alias not found.
510                         " Use all the '.*/' as URL
511                         let url = fullurl
512                 else
513                         " Include in the url the path defined between the alias
514                         " and the tag
515                         let url = substitute(fullurl, '\V'.alias, url, '')
516                 endif
517                
518                 if url !~ '/$'
519                         let url .= '/'
520                 endif
521
522                 "call Decho("D: URL: ",url)
523                 if tagcmd != "fj"
524                 " If the tag command isn't `file jump':
525
526                         " download the remote tags file, and
527                         " prepare for the hyperjump
528                         " Note, if successful, it does a lcd to
529                         " the cache dir
530                         call s:WOCLoadRemote(url)       
531
532                         " Reload the tags file
533                         exe "set tags-=" . b:woc_index.woc_tags_file
534                         exe "set tags+=" . b:woc_index.woc_tags_file
535
536                 "call Decho("D: curdir " . getcwd() . ". tagfiles: ". string(tagfiles()))
537                 endif
538
539                 let tag = matchstr(fulltag, '/[^/]*$')[1:]
540                 "call Decho("D: tag: ", tag)
541                 "call Decho("D: taglist: ", tagfiles())
542         endif
543
544         if cmd == ""
545                 let cmd = tagcmd
546         endif
547         if cmd == ""
548                 " If no command has been specified, assume the default (jump)
549                 let cmd = 'j'
550         endif
551        
552         "call Decho('D: vicmdtag: ' . string([tag, vicmd, tagcmd, cmd]))
553         let oldfile=expand("%:p")
554
555         if cmd == "fj"
556                 " File jump
557
558                 " Revert back to old dir
559                 exe 'lcd ' . b:woc_cur_path
560
561                 call s:WOCFakeTagJump(url . tag)
562                 "exe "edit " . url . tag
563         elseif tag != ""
564                 " tag is not null, at least
565
566                 if has("cscope")
567                         set nocsverb
568                         if filereadable("cscope.out")
569                                 cs add cscope.out
570                         endif
571                         if filereadable(".woc/cscope.out")
572                                 cs add .woc/cscope.out
573                         endif
574                 endif
575
576                 if cmd == "j"
577                         " Jump
578                         "         <o/
579                         "          |_
580                         "         /'
581                         "
582                         exe "tag " . tag
583
584                 elseif cmd == "r"
585                         " Reverse jump
586                         "
587                         "         \._
588                         "          |
589                         "         <0\
590                         "      ~~~~~~~~~
591                         "call Decho("D: revjump ", tag)
592                         exe "set tags+=" . b:woc_index.woc_revtags_file
593                         "call Decho("D: revjump list ",  tagfiles())
594                         exe "tag " . tag
595                         exe "set tags-=" . b:woc_index.woc_revtags_file
596                 else
597                         " A cscope command
598                         exe "cscope find " . cmd . " " . tag
599                 endif
600         endif
601
602         if expand("%:p") == oldfile
603                 " We didn't jump to a new file, thus revert back to our
604                 " previous dir
605                 exe 'lcd ' . b:woc_cur_path
606         endif
607        
608         " And that's all folks
609         "call Decho("D: Executing ", vicmd)
610         execute vicmd
611 endfun
612
613 "
614 fun! s:WOCLoadRemote(url)
615         let url=a:url
616
617         "
618         " Check if the url is local
619         "
620         if url =~ '^/' || url =~ '^file://' || url !~ '^\w\+://'
621                 let url = substitute(url, 'file://', '', '')
622                 let url=substitute(url, '/$', '', '')
623                 "call Decho("Loading local url: " . url )
624
625                 " Check if we are in the cache dir
626                 let cachedir=simplify(fnamemodify(expand("$WOC_HOME"), ":p").'/cache')
627                 let fullpath=getcwd()
628                 "call Decho("Loading local url: cur dir " . fullpath )
629                 if fullpath =~ cachedir
630                         " Load the tags files
631                         let md5dir=matchlist(fullpath, 'cache/\(\w\+\)')[1]
632                         if has_key(s:woc_cache_url, md5dir)
633                                 let url2=s:woc_cache_url[md5dir]
634
635                                 if url2 =~ '%s'
636                                         let url3=url
637                                         if url =~ '^/'| let url3 = url[1:]| endif
638                                         let url2=substitute(url2, '%s', url3, '')
639                                 else
640                                         if url2 !~ '/$' | let url2 .= '/' | endif
641                                         let url2 = url2 . url
642                                 endif
643                                
644                                 let prefix=matchstr(url2, '^\w\+://')
645                                 let url3=matchstr(url2, '://.*')[3:]
646                                 let url2=prefix . simplify(url3)
647
648                                 "call Decho("Calling s:WOCLoadRemote a II time. url2: ". url2)
649                                 call s:WOCLoadRemote(url2)
650                         else
651                                 " We are in the cache dir, and the directory doesn't
652                                 " exist. Just create it.
653                                 call mkdir('./'.url, 'p')
654                         endif
655                 else
656                         if isdirectory('./'.url)
657                                 exe 'lcd ' . './'.url
658                         endif
659                 endif
660
661                 return
662         endif
663
664         " The URL passed to `wocdownloader', must always have one %s, which
665         " will be substituted with the file to download
666         if url !~ '%s'
667                 if url =~ '/$'
668                         let url.='%s'
669                 else
670                         let url.='/%s'
671                 endif
672         endif
673
674         echo "Downloading " . url
675         "call Decho("Downloading " . url)
676         "call Decho(expand("$WOC_HOME") . '/'. "wocdownloader " . '"'.url.'"')
677
678         "let md5cache=system('bash -x ' . expand("$WOC_HOME") . '/'. "wocdownloader " . '"'.url.'"'. ' 2> /tmp/LOOGS')
679         let md5cache=system(expand("$WOC_HOME") . '/'. "wocdownloader " . '"'.url.'"')
680         if v:shell_error == 0
681                 if md5cache != ""
682                         "call Decho("Entering in " . md5cache )
683                         exe 'lcd ' . md5cache
684                         let md5=matchlist(md5cache, 'cache/\(\w\+\)')[1]
685                         if url !~ '^\w\+://'
686                                 let url = 'http://' . url
687                         endif
688
689                         " |{woc_cache_url}|
690                         let s:woc_cache_url[md5]=url
691                 else
692                         echoerr "No tag files could be found in \"" . url . '"'
693                 endif
694         endif
695 endfun
696
697 "
698 " WOCClearCache: clear the woc cache
699 "
700 " If a:1 is set to a non-zero value, the whole cache dir will be removed,
701 " otherwise only the used subdirs will be cleaned.
702 "
703 fun! s:WOCClearCache(...)
704         let home=fnamemodify(expand("$WOC_HOME"), ":p")
705
706         if home == "/" || (home !~ simplify(expand("$HOME").'/').'.*\w\+' && home !~ "/tmp")
707                 echoerr "$WOC_HOME is set to a dangerous place (".home.")!\n" . "No cache cleaning"
708                 return
709         endif
710
711         if a:0 >= 1 && a:1
712                 echo "WoC: Cleaning the whole cache"
713                 "call Decho("rm -rf " . expand("$WOC_HOME").'/cache/')
714                 call system("rm -rf " . expand("$WOC_HOME").'/cache/')
715         else
716                 if exists("s:woc_cache_url") && s:woc_cache_url != {}
717                         echo "WoC: Cleaning the last used cache"
718                         for key in keys(s:woc_cache_url)
719                                 "call Decho("rm -rf " . expand("$WOC_HOME").'/cache/'.key)
720                                 call system("rm -rf " . expand("$WOC_HOME").'/cache/'.key)
721                         endfor
722                 endif
723         endif
724 endfun
725
726
727 "
728 " WOCOpenFile: called when a file is opened
729 "
730 " If a:file is in the woc cache dir, then try to download it from the remote
731 " url
732 fun! s:WOCOpenFile(file)
733         call s:WOCLoadIndex()
734        
735         let cachedir=simplify(fnamemodify(expand("$WOC_HOME"), ":p").'/cache')
736         let fullpath=fnamemodify(expand(a:file), ":p")
737         let url=""
738         "call Decho("Wrapping " . fullpath)
739
740         if fullpath =~ cachedir
741                 let file=matchlist(fullpath, 'cache/\w\+/\(.*\)$')[1]
742                 let md5dir=matchlist(fullpath, 'cache/\(\w\+\)')[1]
743                 let md5dirpath=matchstr(fullpath, '.*/cache/\(\w\+\)')
744                 if has_key(s:woc_cache_url, md5dir)
745                         let url=s:woc_cache_url[md5dir]
746                 endif
747                 "call Decho("Wrapping cache " . fullpath, " url: ", url, " md5: ", md5dirpath," ", md5dir  )
748
749                 exe "lcd " . md5dirpath
750                 echo "Downloading file: " . file
751                 "call Decho("Downloading file: " . file, " url: ", url)
752                 "call Decho("woc_cache_url: ", string(s:woc_cache_url))
753                 let md5cache = system(expand("$WOC_HOME") . '/'. "wocdownloader " . '"'.url.'"' . ' "' . file .'"')
754                 if v:shell_error == 1 || md5cache == ""
755                         echoerr "File \"" . file ."\" could not be downloaded"
756                         " going back
757                         pop
758                 else
759                         exe "edit " . fullpath
760                         exe "silent doau BufRead ".fullpath
761                 endif
762         endif
763 endfun
764
765
766 ""
767 """" Syntax stuff
768 ""
769
770 "
771 " Function: s:WOCTuneCommentSyntax()
772 " Entirely ripped from the vimspell plugin
773 "
774 " --
775 " Add support to do spell checking inside comment. Idea from engspchk.vim from
776 " Dr. Charles E. Campbell, Jr. <Charles.Campbell.1@gsfc.nasa.gov>.
777 " This can be done only for those syntax files' comment blocks that
778 " contains=@cluster.
779 function! s:WOCTuneCommentSyntax(ft)
780   if !exists("b:woc_syntax_ft") || b:woc_syntax_ft != a:ft
781     let b:woc_syntax_ft = a:ft
782     " Special treatment for filetype which do not use @Spell cluster.
783     if     a:ft == "amiga"
784       syn cluster amiCommentGroup               add=wocHyperTextJump,wocHyperTextEntry
785       " highlight only in comments (i.e. if wocHyperTextJump are contained).
786       let b:woc_syntax_options = "contained"
787     elseif a:ft == "bib"
788       syn cluster bibVarContents        contains=wocHyperTextJump,wocHyperTextEntry
789       syn cluster bibCommentContents    contains=wocHyperTextJump,wocHyperTextEntry
790       let b:woc_syntax_options = "contained"
791     elseif a:ft == "c" || a:ft == "cpp"
792       syn cluster cCommentGroup         add=wocHyperTextJump,wocHyperTextEntry
793       let b:woc_syntax_options = "contained"
794     elseif a:ft == "python" || a:ft == "py"
795     "  syn cluster pythonComment        add=wocHyperTextJump,wocHyperTextEntry
796       syn match pythonComment /#.*$/  contains=pythonTodo,wocHyperTextJump,wocHyperTextEntry
797       let b:woc_syntax_options = "contained"
798     elseif a:ft == "csh"
799       syn cluster cshCommentGroup               add=wocHyperTextJump,wocHyperTextEntry
800       let b:woc_syntax_options = "contained"
801     elseif a:ft == "dcl"
802       syn cluster dclCommentGroup               add=wocHyperTextJump,wocHyperTextEntry
803       let b:woc_syntax_options = "contained"
804     elseif a:ft == "fortran"
805       syn cluster fortranCommentGroup   add=wocHyperTextJump,wocHyperTextEntry
806       syn match   fortranGoodWord contained     "^[Cc]\>"
807       syn cluster fortranCommentGroup   add=fortranGoodWord
808       hi link fortranGoodWord fortranComment
809       let b:woc_syntax_options = "contained"
810     elseif a:ft == "sh" || a:ft == "ksh" || a:ft == "bash"
811       syn cluster shCommentGroup                add=wocHyperTextJump,wocHyperTextEntry
812     elseif a:ft == "b"
813       syn cluster bCommentGroup         add=wocHyperTextJump,wocHyperTextEntry
814       let b:woc_syntax_options = "contained"
815     elseif a:ft == "xml"
816       syn cluster xmlText               add=wocHyperTextJump,wocHyperTextEntry
817       syn cluster xmlString             add=wocHyperTextJump,wocHyperTextEntry
818       syn cluster xmlRegionHook add=wocHyperTextJump,wocHyperTextEntry
819
820       let b:woc_syntax_options = "contained"
821     elseif a:ft == "tex"
822       syn cluster texCommentGroup               add=wocHyperTextJump,wocHyperTextEntry
823
824       syn cluster texMatchGroup         add=wocHyperTextJump,wocHyperTextEntry
825
826     elseif a:ft == "vim"
827       syn cluster vimCommentGroup               add=wocHyperTextJump,wocHyperTextEntry
828
829       let b:woc_syntax_options = "contained"
830     elseif a:ft == "otl"
831       syn cluster otlGroup              add=wocHyperTextJump,wocHyperTextEntry
832       let b:woc_syntax_options = "contained"
833     endif
834
835     " by default, only errors in Spell cluster are highlight
836     if !exists("b:woc_syntax_options")
837       let b:woc_syntax_options = "contained"
838     endif
839   endif
840 endfunction
841
842 " WOCSyntax: set the syntax for the WoC tags
843 fun! s:WOCSyntax()
844         " Our syntax
845         " Highlights {-TAG-} and |{TAG}|
846         syn match wocHyperTextJump   "{-\(\({-\)\@!.\)*-}" contains=wocLeftRef,wocRightRef
847                                      "\({-\).\+-}" (old regexp)
848         syn match wocHyperTextEntry  "|{\(\(|{\)\@!.\)*}|" contains=wocLeftDef,wocRightDef
849                                      "|{.\+}|" (old regexp)
850         syn match wocLeftRef               contained "{-"
851         syn match wocRightRef              contained "-}"
852         syn match wocLeftDef               contained "|{"
853         syn match wocRightDef              contained "}|"
854 "       hi def link wocHyperTextJump   Identifier
855         hi def wocHyperTextJump ctermfg=Cyan guifg=Cyan cterm=underline gui=underline term=reverse
856         hi def link wocHyperTextEntry  String
857         hi def link wocLeftRef         Ignore
858         hi def link wocLeftDef         Ignore
859         hi def link wocRightRef        Ignore
860         hi def link wocRightDef        Ignore
861
862         call s:WOCTuneCommentSyntax(&l:ft)
863 endfun
864
865 let &cpo = s:save_cpo
866 finish
867 " EOF
868
869 Disable debug:
870 {-:%s/\([^"]\)c[a]ll Decho/\1"call\ Decho/g | '' "-}
871 Enable debug:
872 {-:%s/"c[a]ll Decho/call\ Decho/g | '' "-}
Note: See TracBrowser for help on using the browser.