Initial import of my home directory
[home/.git] / .vim / ftplugin / latex-suite / packages.vim
1 "=============================================================================
2 "            File: packages.vim
3 "      Author: Mikolaj Machowski
4 "     Created: Tue Apr 23 06:00 PM 2002 PST
5 "         CVS: $Id: packages.vim 997 2006-03-20 09:45:45Z srinathava $
6
7 "  Description: handling packages from within vim
8 "=============================================================================
9
10 " avoid reinclusion.
11 if !g:Tex_PackagesMenu || exists('s:doneOnce')
12         finish
13 endif
14 let s:doneOnce = 1
15
16 let s:path = expand("<sfile>:p:h")
17
18 let s:menu_div = 20
19
20 com! -nargs=0 TPackageUpdate :silent! call Tex_pack_updateall(1)
21 com! -nargs=0 TPackageUpdateAll :silent! call Tex_pack_updateall(1)
22
23 " Custom command-line completion of Tcommands is very useful but this feature
24 " is available only in Vim 6.2 and above. Check number of version and choose
25 " proper command and function.
26 if v:version >= 602
27         com! -complete=custom,Tex_CompletePackageName -nargs=* TPackage let s:retVal = Tex_pack_one(<f-args>) <bar> normal! i<C-r>=s:retVal<CR>
28
29         " Tex_CompletePackageName: for completing names in TPackage command {{{
30         "       Description: get list of package names with globpath(), remove full path
31         "       and return list of names separated with newlines.
32         "
33         function! Tex_CompletePackageName(A,P,L)
34                 " Get name of packages from all runtimepath directories
35                 let packnames = Tex_FindInRtp('', 'packages')
36                 let packnames = substitute(packnames, '^,', '', 'e')
37                 " Separate names with \n not ,
38                 let packnames = substitute(packnames,',','\n','g')
39                 return packnames
40         endfunction
41         " }}}
42         
43 else 
44         com! -nargs=* TPackage let s:retVal = Tex_pack_one(<f-args>) <bar> normal! i<C-r>=s:retVal<CR>
45
46 endif
47
48 imap <silent> <plug> <Nop>
49 nmap <silent> <plug> i
50
51 let g:Tex_package_supported = ''
52 let g:Tex_package_detected = ''
53 " Remember the defaults because we want g:Tex_PromptedEnvironments to contain
54 " in addition to the default, \newenvironments, and the \newenvironments might
55 " change...
56 let g:Tex_PromptedEnvironmentsDefault = g:Tex_PromptedEnvironments
57 let g:Tex_PromptedCommandsDefault = g:Tex_PromptedCommands
58
59
60 " Tex_pack_check: creates the package menu and adds to 'dict' setting. {{{
61 "
62 function! Tex_pack_check(package)
63         " Use Tex_FindInRtp() function to get first name from packages list in all
64         " rtp directories conforming with latex-suite directories hierarchy
65         " Store names in variables to process functions only once.
66         let packname = Tex_FindInRtp(a:package, 'packages')
67         if packname != ''
68                 exe 'runtime! ftplugin/latex-suite/packages/' . a:package
69                 if has("gui_running")
70                         call Tex_pack(a:package)
71                 endif
72                 if g:Tex_package_supported !~ a:package
73                         let g:Tex_package_supported = g:Tex_package_supported.','.a:package
74                 endif
75         endif
76         " Return full list of dictionaries (separated with ,) for package in &rtp
77         call Tex_Debug("Tex_pack_check: searching for ".a:package." in dictionaries/ in &rtp", "pack")
78         let dictname = Tex_FindInRtp(a:package, 'dictionaries', ':p')
79         if dictname != ''
80                 exe 'setlocal dict^=' . dictname
81                 call Tex_Debug('Tex_pack_check: setlocal dict^=' . dictname, 'pack')
82                 if g:Tex_package_supported !~ a:package
83                         let g:Tex_package_supported = g:Tex_package_supported.','.a:package
84                 endif
85         endif
86         if g:Tex_package_detected !~ '\<'.a:package.'\>'
87                 let g:Tex_package_detected = g:Tex_package_detected.','.a:package
88         endif
89         let g:Tex_package_detected = substitute(g:Tex_package_detected, '^,', '', '')
90         let g:Tex_package_supported = substitute(g:Tex_package_supported, '^,', '', '')
91 endfunction
92
93 " }}}
94 " Tex_pack_uncheck: removes package from menu and 'dict' settings. {{{
95 function! Tex_pack_uncheck(package)
96         if has("gui_running") && Tex_FindInRtp(a:package, 'packages') != ''
97                 exe 'silent! aunmenu '.g:Tex_PackagesMenuLocation.'-sep'.a:package.'-'
98                 exe 'silent! aunmenu '.g:Tex_PackagesMenuLocation.a:package.'\ Options'
99                 exe 'silent! aunmenu '.g:Tex_PackagesMenuLocation.a:package.'\ Commands'
100         endif
101         if Tex_FindInRtp(a:package, 'dictionaries') != ''
102                 exe 'setlocal dict-='.Tex_FindInRtp(a:package, 'dictionaries')
103         endif
104 endfunction
105
106 " }}}
107 " Tex_pack_updateall: updates the TeX-Packages menu {{{
108 " Description:
109 "       This function first calls Tex_pack_all to scan for \usepackage's etc if
110 "       necessary. After that, it 'supports' and 'unsupports' packages as needed
111 "       in such a way as to not repeat work.
112 function! Tex_pack_updateall(force)
113         call Tex_Debug('+Tex_pack_updateall', 'pack')
114
115         " Find out which file we need to scan.
116         let fname = Tex_GetMainFileName(':p')
117
118         " If this is the same as last time, don't repeat.
119         if !a:force && exists('s:lastScannedFile') &&
120                                 \ s:lastScannedFile == fname
121                 return
122         endif
123         " Remember which file we scanned for next time.
124         let s:lastScannedFile = fname
125
126         " Remember which packages we detected last time.
127         if exists('g:Tex_package_detected')
128                 let oldpackages = g:Tex_package_detected
129         else
130                 let oldpackages = ''
131         endif
132
133         " This sets up a global variable of all detected packages.
134         let g:Tex_package_detected = ''
135         " reset the environments and commands.
136         let g:Tex_PromptedEnvironments = g:Tex_PromptedEnvironmentsDefault
137         let g:Tex_PromptedCommands = g:Tex_PromptedCommandsDefault
138
139         if expand('%:p') != fname
140                 call Tex_Debug(':Tex_pack_updateall: sview '.Tex_EscapeSpaces(fname), 'pack')
141                 exe 'sview '.Tex_EscapeSpaces(fname)
142         else
143                 call Tex_Debug(':Tex_pack_updateall: split', 'pack')
144                 split
145         endif
146                 
147         call Tex_ScanForPackages()
148         q
149
150         call Tex_Debug(':Tex_pack_updateall: detected ['.g:Tex_package_detected.'] in first run', 'pack')
151         
152         " Now for each package find out if this is a custom package and if so,
153         " scan that as well. We will use the ':find' command in vim to let vim
154         " search through the file paths for us.
155         "
156         " NOTE: This while loop will also take into account packages included
157         "       within packages to any level of recursion as long as
158         "       g:Tex_package_detected is always padded with new package names
159         "       from the end.
160         "
161         " First set the &path setting to the user's TEXINPUTS setting.
162         let _path = &path
163         let _suffixesadd = &suffixesadd
164
165         let &path = '.,'.g:Tex_TEXINPUTS
166         let &suffixesadd = '.sty,.tex'
167
168         let scannedPackages = ''
169
170         let i = 1
171         let packname = Tex_Strntok(g:Tex_package_detected, ',', i)
172         while packname != ''
173
174                 call Tex_Debug(':Tex_pack_updateall: scanning package '.packname, 'pack')
175
176                 " Scan this package only if we have not scanned it before in this
177                 " run.
178                 if scannedPackages =~ '\<'.packname.'\>'
179                         let i = i + 1
180
181                         call Tex_Debug(':Tex_pack_updateall: '.packname.' already scanned', 'pack')
182                         let packname = Tex_Strntok(g:Tex_package_detected, ',', i)
183                         continue
184                 endif 
185
186                 " Split this window in two. The packages/files being found will open
187                 " in this new window and we also need not bother with files being
188                 " modified etc.
189                 split
190
191                 call Tex_Debug(':Tex_pack_updateall: silent! find '.Tex_EscapeSpaces(packname).'.sty', 'pack')
192                 let thisbufnum = bufnr('%')
193                 exec 'silent! find '.Tex_EscapeSpaces(packname).'.sty'
194                 call Tex_Debug(':Tex_pack_updateall: present file = '.bufname('%'), 'pack')
195
196                 " If this file was not found, assume that it means its not a
197                 " custom package and mark it "scanned".
198                 " A package is not found if we stay in the same buffer as before and
199                 " its not the one where we want to go.
200                 if bufnr('%') == thisbufnum && bufnr('%') != bufnr(packname.'.sty')
201                         let scannedPackages = scannedPackages.','.packname
202                         q
203
204                         call Tex_Debug(':Tex_pack_updateall: '.packname.' not found anywhere', 'pack')
205                         let i = i + 1
206                         let packname = Tex_Strntok(g:Tex_package_detected, ',', i)
207                         continue
208                 endif
209
210                 " otherwise we are presently editing a custom package, scan it for
211                 " more \usepackage lines from the first line to the last.
212                 let packpath = expand('%:p')
213                 let &complete = &complete.'s'.packpath
214
215                 call Tex_Debug(':Tex_pack_updateall: found custom package '.packpath, 'pack')
216                 call Tex_ScanForPackages(line('$'), line('$'))
217                 call Tex_Debug(':Tex_pack_updateall: After scanning, g:Tex_package_detected = '.g:Tex_package_detected, 'pack')
218
219                 let scannedPackages = scannedPackages.','.packname
220                 " Do not use bwipe, but that leads to excessive buffer number
221                 " consumption. Besides, its intuitive for a custom package to remain
222                 " on the buffer list.
223                 q
224
225                 let i = i + 1
226                 let packname = Tex_Strntok(g:Tex_package_detected, ',', i)
227         endwhile
228
229         let &path = _path
230         let &suffixesadd = _suffixesadd
231
232         " Now only support packages we didn't last time.
233         " First remove packages which were used last time but are no longer used.
234         let i = 1
235         let oldPackName = Tex_Strntok(oldpackages, ',', i)
236         while oldPackName != ''
237                 if g:Tex_package_detected !~ oldPackName
238                         call Tex_pack_uncheck(oldPackName)
239                 endif
240                 let i = i + 1
241                 let oldPackName = Tex_Strntok(oldpackages, ',', i)
242         endwhile
243
244         " Then support packages which are used this time but weren't used last
245         " time.
246         let i = 1
247         let newPackName = Tex_Strntok(g:Tex_package_detected, ',', i)
248         while newPackName != ''
249                 if oldpackages !~ newPackName
250                         call Tex_pack_one(newPackName)
251                 endif
252                 let i = i + 1
253                 let newPackName = Tex_Strntok(g:Tex_package_detected, ',', i)
254         endwhile
255
256         " Throw an event that we are done scanning packages. Some packages might
257         " use this to change behavior based on which options have been used etc.
258         call Tex_Debug(":Tex_pack_updateall: throwing LatexSuiteScannedPackages event", "pack")
259         silent! do LatexSuite User LatexSuiteScannedPackages
260
261         call Tex_Debug("-Tex_pack_updateall", "pack")
262 endfunction
263
264 " }}}
265 " Tex_pack_one: supports each package in the argument list.{{{
266 " Description:
267 "   If no arguments are supplied, then the user is asked to choose from the
268 "   packages found in the packages/ directory
269 function! Tex_pack_one(...)
270         if a:0 == 0 || (a:0 > 0 && a:1 == '')
271                 let packlist = Tex_FindInRtp('', 'packages')
272                 let packname = Tex_ChooseFromPrompt(
273                                         \ "Choose a package: \n" . 
274                                         \ Tex_CreatePrompt(packlist, '3', ',') .
275                                         \ "\nEnter number or filename :", 
276                                         \ packlist, ',')
277                 if packname != ''
278                         return Tex_pack_one(packname)
279                 else
280                         return ''
281                 endif
282         else
283                 " Support the packages supplied. This function can be called with
284                 " multiple arguments in which case, support each of them in turn.
285                 let retVal = ''
286                 let omega = 1
287                 while omega <= a:0
288                         let packname = a:{omega}
289                         if Tex_FindInRtp(packname, 'packages') != ''
290                                 call Tex_pack_check(packname)
291                                 if exists('g:TeX_package_option_'.packname)
292                                                 \ && g:TeX_package_option_{packname} != ''
293                                         let retVal = retVal.'\usepackage[<++>]{'.packname.'}<++>'
294                                 else
295                                         let retVal = retVal.'\usepackage{'.packname.'}'."\<CR>"
296                                 endif
297                         else
298                                 let retVal = retVal.'\usepackage{'.packname.'}'."\<CR>"
299                         endif
300                         let omega = omega + 1
301                 endwhile
302                 return IMAP_PutTextWithMovement(substitute(retVal, "\<CR>$", '', ''), '<+', '+>')
303         endif
304 endfunction
305 " }}}
306 " Tex_ScanForPackages: scans the current file for \usepackage{} lines {{{
307 "   and if supported, loads the options and commands found in the
308 "   corresponding package file. Also scans for \newenvironment and
309 "   \newcommand lines and adds names to g:Tex_Prompted variables, they can be
310 "   easy available through <F5> and <F7> shortcuts 
311 function! Tex_ScanForPackages(...)
312         call Tex_Debug("+Tex_ScanForPackages", "pack")
313
314         let pos = line('.').' | normal! '.virtcol('.').'|'
315
316         " For package files without \begin and \end{document}, we might be told to
317         " search from beginning to end.
318         if a:0 < 2
319                 0
320                 let beginline = search('\\begin{document}', 'W')
321                 let endline = search('\\end{document}', 'W')
322                 0
323         else
324                 let beginline = a:1
325                 let endline = a:2
326         endif
327
328         call Tex_Debug(":Tex_ScanForPackages: Begining scans in [".bufname('%')."], beginline = ".beginline, "pack")
329         
330
331         " Scan the file. First open up all the folds, because the command
332         " /somepattern
333         " issued in a closed fold _always_ goes to the first match.
334         let erm = v:errmsg
335         silent! normal! ggVGzO
336         let v:errmsg = erm
337
338         call Tex_Debug(":Tex_ScanForPackages: beginning scan for \\usepackage lines", "pack")
339         " The wrap trick enables us to match \usepackage on the first line as
340         " well.
341         let wrap = 'w'
342         while search('^\s*\\usepackage\_.\{-}{\_.\+}', wrap)
343                 let wrap = 'W'
344
345                 if line('.') > beginline 
346                         break
347                 endif
348
349                 let saveA = @a
350
351                 " If there are options, then find those.
352                 if getline('.') =~ '\\usepackage\[.\{-}\]'
353                         let options = matchstr(getline('.'), '\\usepackage\[\zs.\{-}\ze\]')
354                 elseif getline('.') =~ '\\usepackage\['
355                         " Entering here means that the user has split the \usepackage
356                         " across newlines. Therefore, use yank.
357                         exec "normal! /{\<CR>\"ayi}"
358                         let options = @a
359                 else
360                         let options = ''
361                 endif
362
363                 " The following statement puts the stuff between the { }'s of a
364                 " \usepackage{stuff,foo} into @a. Do not use matchstr() and the like
365                 " because we can have things split across lines and such.
366         exec "normal! /{\<CR>\"ay/}\<CR>"
367
368                 " now remove all whitespace from @a. We need to remove \n and \r
369                 " because we can encounter stuff like
370                 " \usepackage{pack1,
371                 "             newpackonanotherline}
372                 let @a = substitute(@a, "[ \t\n\r]", '', 'g')
373
374                 " Now we have something like pack1,pack2,pack3 with possibly commas
375                 " and stuff before the first package and after the last package name.
376                 " Remove those.
377                 let @a = substitute(@a, '\(^\W*\|\W*$\)', '', 'g')
378
379                 " This gets us a string like 'pack1,pack2,pack3'
380                 " TODO: This will contain duplicates if the user has duplicates.
381                 "       Should we bother taking care of this?
382                 let g:Tex_package_detected = g:Tex_package_detected.','.@a
383
384                 " For each package found, form a global variable of the form
385                 " g:Tex_{packagename}_options 
386                 " which contains a list of the options.
387                 let j = 1
388                 while Tex_Strntok(@a, ',', j) != ''
389                         let g:Tex_{Tex_Strntok(@a, ',', j)}_options = options
390                         let j = j + 1
391                 endwhile
392
393                 " Finally convert @a into something like '"pack1","pack2"'
394                 let @a = substitute(@a, '^\|$', '"', 'g')
395                 let @a = substitute(@a, ',', '","', 'g')
396
397                 call Tex_Debug(":Tex_ScanForPackages: found package(s) [".@a."] on line ".line('.'), "pack")
398
399                 " restore @a
400                 let @a = saveA
401         endwhile
402         call Tex_Debug(":Tex_ScanForPackages: End scan \\usepackage, detected packages = ".g:Tex_package_detected, "pack")
403
404         " TODO: This needs to be changed. In the future, we might have
405         " functionality to remember the fold-state before opening up all the folds
406         " and then re-creating them. Use mkview.vim.
407         let erm = v:errmsg
408         silent! normal! ggVGzC
409         let v:errmsg = erm
410
411         " Because creating list of detected packages gives string
412         " ',pack1,pack2,pack3' remove leading ,
413         let g:Tex_package_detected = substitute(g:Tex_package_detected, '^,', '', '')
414
415         call Tex_Debug(":Tex_ScanForPackages: Beginning scan for \\newcommand's", "pack")
416         " Scans whole file (up to \end{document}) for \newcommand and adds this
417         " commands to g:Tex_PromptedCommands variable, it is easily available
418         " through <F7>
419         0 
420         while search('^\s*\\newcommand\*\?{.\{-}}', 'W')
421
422                 if line('.') > endline 
423                         break
424                 endif
425
426                 let newcommand = matchstr(getline('.'), '\\newcommand\*\?{\\\zs.\{-}\ze}')
427                 let g:Tex_PromptedCommands = g:Tex_PromptedCommands . ',' . newcommand
428
429         endwhile
430
431         " Scans whole file (up to \end{document}) for \newenvironment and adds this
432         " environments to g:Tex_PromptedEnvironments variable, it is easily available
433         " through <F5>
434         0
435         call Tex_Debug(":Tex_ScanForPackages: Beginning scan for \\newenvironment's", 'pack')
436
437         while search('^\s*\\newenvironment\*\?{.\{-}}', 'W')
438                 call Tex_Debug('found newenvironment on '.line('.'), 'pack')
439
440                 if line('.') > endline 
441                         break
442                 endif
443
444                 let newenvironment = matchstr(getline('.'), '\\newenvironment\*\?{\zs.\{-}\ze}')
445                 let g:Tex_PromptedEnvironments = g:Tex_PromptedEnvironments . ',' . newenvironment
446
447         endwhile
448
449         exe pos
450         " first make a random search so that we push at least one item onto the
451         " search history. Since vim puts only one item in the history per function
452         " call, this way we make sure that one and only item is put into the
453         " search history.
454         normal! /^<CR>
455         " now delete it...
456         call histdel('/', -1)
457
458         call Tex_Debug("-Tex_ScanForPackages", "pack")
459 endfunction
460    
461 " }}}
462 " Tex_pack_supp_menu: sets up a menu for package files {{{
463 "   found in the packages directory groups the packages thus found into groups
464 "   of 20...
465 function! Tex_pack_supp_menu()
466         let suplist = Tex_FindInRtp('', 'packages')
467
468         call Tex_MakeSubmenu(suplist, g:Tex_PackagesMenuLocation.'Supported.', 
469                 \ '<plug><C-r>=Tex_pack_one("', '")<CR>')
470 endfunction 
471
472 " }}}
473 " Tex_pack: loads the options (and commands) for the given package {{{
474 function! Tex_pack(pack)
475         if exists('g:TeX_package_'.a:pack)
476
477                 let optionList = g:TeX_package_option_{a:pack}.','
478                 let commandList = g:TeX_package_{a:pack}.','
479
480                 " Don't create separator if in package file are only Vim commands. 
481                 " Rare but possible.
482                 if !(commandList == ',' && optionList == ',')
483                         exec 'amenu '.g:Tex_PackagesMenuLocation.'-sep'.a:pack.'- <Nop>'
484                 endif
485
486                 if optionList != ''
487
488                         let mainMenuName = g:Tex_PackagesMenuLocation.a:pack.'\ Options.'
489                         call s:GroupPackageMenuItems(optionList, mainMenuName, 
490                                 \ '<plug><C-r>=IMAP_PutTextWithMovement("', ',")<CR>')
491
492                 endif
493
494                 if commandList != ''
495
496                         let mainMenuName = g:Tex_PackagesMenuLocation.a:pack.'\ Commands.'
497                         call s:GroupPackageMenuItems(commandList, mainMenuName, 
498                                 \ '<plug><C-r>=Tex_ProcessPackageCommand("', '")<CR>',
499                                 \ '<SID>FilterPackageMenuLHS')
500                 endif
501         endif
502 endfunction 
503
504 " }}}
505
506 " ==============================================================================
507 " Menu Functions
508 " Creating menu items for the all the package files found in the packages/
509 " directory as well as creating menus for each supported package found in the
510 " preamble.
511 " ============================================================================== 
512 " Tex_MakeSubmenu: makes a submenu given a list of items {{{
513 " Description: 
514 "   This function takes a comma seperated list of menu items and creates a
515 "   'grouped' menu. i.e, it groups the items into s:menu_div items each and
516 "   puts them in submenus of the given mainMenu.
517 "   Each menu item is linked to the HandlerFunc.
518 "   If an additional argument is supplied, then it is used to filter each of
519 "   the menu items to generate better names for the menu display.
520 "
521 function! Tex_MakeSubmenu(menuList, mainMenuName, 
522                                 \ handlerFuncLHS, handlerFuncRHS, ...)
523
524         let extractFunction = (a:0 > 0 ? a:1 : '' )
525         let menuList = substitute(a:menuList, '[^,]$', ',', '')
526
527         let doneMenuSubmenu = 0
528
529         while menuList != ''
530
531                 " Extract upto s:menu_div menus at once.
532                 let menuBunch = matchstr(menuList, '\v(.{-},){,'.s:menu_div.'}')
533
534                 " The remaining menus go into the list.
535                 let menuList = strpart(menuList, strlen(menuBunch))
536
537                 let submenu = ''
538                 " If there is something remaining, then we got s:menu_div items.
539                 " therefore put these menu items into a submenu.
540                 if strlen(menuList) || doneMenuSubmenu
541                         exec 'let firstMenu = '.extractFunction."(matchstr(menuBunch, '\\v^.{-}\\ze,'))"
542                         exec 'let lastMenu = '.extractFunction."(matchstr(menuBunch, '\\v[^,]{-}\\ze,$'))"
543
544                         let submenu = firstMenu.'\ \-\ '.lastMenu.'.'
545
546                         let doneMenuSubmenu = 1
547                 endif
548
549                 " Now for each menu create a menu under the submenu
550                 let i = 1
551                 let menuName = Tex_Strntok(menuBunch, ',', i)
552                 while menuName != ''
553                         exec 'let menuItem = '.extractFunction.'(menuName)'
554                         execute 'amenu '.a:mainMenuName.submenu.menuItem
555                                 \ '       '.a:handlerFuncLHS.menuName.a:handlerFuncRHS
556
557                         let i = i + 1
558                         let menuName = Tex_Strntok(menuBunch, ',', i)
559                 endwhile
560         endwhile
561 endfunction 
562
563 " }}}
564 " GroupPackageMenuItems: uses the sbr: to split menus into groups {{{
565 " Description: 
566 "   This function first splits up the menuList into groups based on the
567 "   special sbr: tag and then calls Tex_MakeSubmenu 
568
569 function! <SID>GroupPackageMenuItems(menuList, menuName, 
570                                         \ handlerFuncLHS, handlerFuncRHS,...)
571
572         if a:0 > 0
573                 let extractFunction = a:1
574         else
575                 let extractFunction = ''
576         endif
577         let menuList = a:menuList
578
579         while matchstr(menuList, 'sbr:') != ''
580                 let groupName = matchstr(menuList, '\v^sbr:\zs.{-}\ze,')
581                 let menuList = strpart(menuList, strlen('sbr:'.groupName.','))
582                 if matchstr(menuList, 'sbr:') != ''
583                         let menuGroup = matchstr(menuList, '\v^.{-},\zesbr:')
584                 else
585                         let menuGroup = menuList
586                 endif
587
588                 call Tex_MakeSubmenu(menuGroup, a:menuName.groupName.'.', 
589                         \ a:handlerFuncLHS, a:handlerFuncRHS, extractFunction)
590
591                 let menuList = strpart(menuList, strlen(menuGroup))
592         endwhile
593
594         call Tex_MakeSubmenu(menuList, a:menuName,
595                 \ a:handlerFuncLHS, a:handlerFuncRHS, extractFunction)
596
597 endfunction " }}}
598 " Definition of what to do for various package commands {{{
599 let s:CommandSpec_bra = '\<+replace+>{<++>}<++>'
600 let s:CommandSpec_brs = '\<+replace+><++>'
601 let s:CommandSpec_brd = '\<+replace+>{<++>}{<++>}<++>'
602 let s:CommandSpec_env = '\begin{<+replace+>}'."\<CR><++>\<CR>".'\end{<+replace+>}<++>'
603 let s:CommandSpec_ens = '\begin{<+replace+>}<+extra+>'."\<CR><++>\<CR>".'\end{<+replace+>}<++>'
604 let s:CommandSpec_eno = '\begin[<++>]{<+replace+>}'."\<CR><++>\<CR>".'\end{<+replace+>}'
605 let s:CommandSpec_nor = '\<+replace+>'
606 let s:CommandSpec_noo = '\<+replace+>[<++>]'
607 let s:CommandSpec_nob = '\<+replace+>[<++>]{<++>}{<++>}<++>'
608 let s:CommandSpec_spe = '<+replace+>'
609 let s:CommandSpec_    = '\<+replace+>'
610
611 let s:MenuLHS_bra = '\\&<+replace+>{}'
612 let s:MenuLHS_brs = '\\&<+replace+>{}'
613 let s:MenuLHS_brd = '\\&<+replace+>{}{}'
614 let s:MenuLHS_env = '&<+replace+>\ (E)'
615 let s:MenuLHS_ens = '&<+replace+>\ (E)'
616 let s:MenuLHS_eno = '&<+replace+>\ (E)'
617 let s:MenuLHS_nor = '\\&<+replace+>'
618 let s:MenuLHS_noo = '\\&<+replace+>[]'
619 let s:MenuLHS_nob = '\\&<+replace+>[]{}{}'
620 let s:MenuLHS_spe = '&<+replace+>'
621 let s:MenuLHS_sep = '-sep<+replace+>-'
622 let s:MenuLHS_    = '\\&<+replace+>'
623 " }}}
624 " Tex_ProcessPackageCommand: processes a command from the package menu {{{
625 " Description: 
626 function! Tex_ProcessPackageCommand(command)
627         if a:command =~ ':'
628                 let commandType = matchstr(a:command, '^\w\+\ze:')
629                 let commandName = matchstr(a:command, '^\w\+:\zs[^:]\+\ze:\?')
630                 let extrapart = strpart(a:command, strlen(commandType.':'.commandName.':'))
631         else
632                 let commandType = ''
633                 let commandName = a:command
634                 let extrapart = ''
635         endif
636
637         let command = s:CommandSpec_{commandType}
638         let command = substitute(command, '<+replace+>', commandName, 'g')
639         let command = substitute(command, '<+extra+>', extrapart, 'g')
640         return IMAP_PutTextWithMovement(command)
641 endfunction 
642 " }}}
643 " FilterPackageMenuLHS: filters the command description to provide a better menu item {{{
644 " Description: 
645 function! <SID>FilterPackageMenuLHS(command)
646         let commandType = matchstr(a:command, '^\w\+\ze:')
647         if commandType != ''
648                 let commandName = strpart(a:command, strlen(commandType.':'))
649         else
650                 let commandName = a:command
651         endif
652
653         return substitute(s:MenuLHS_{commandType}, '<+replace+>', commandName, 'g')
654 endfunction " }}}
655
656 if g:Tex_Menus
657         exe 'amenu '.g:Tex_PackagesMenuLocation.'&UpdatePackage :call Tex_pack(expand("<cword>"))<cr>'
658         exe 'amenu '.g:Tex_PackagesMenuLocation.'&UpdateAll :call Tex_pack_updateall(1)<cr>'
659
660         call Tex_pack_supp_menu()
661 endif
662
663 augroup LatexSuite
664         au LatexSuite User LatexSuiteFileType 
665                 \ call Tex_Debug('packages.vim: Catching LatexSuiteFileType event', 'pack') | 
666                 \ call Tex_pack_updateall(0)
667 augroup END
668
669 " vim:fdm=marker:ts=4:sw=4:noet:ff=unix