home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / runtime / dos / ftplugin / AppendMatchGroup.vim < prev    next >
Encoding:
Text File  |  2007-10-26  |  4.3 KB  |  126 lines

  1. " Vim filetype plugin file utility
  2. " Language:    * (various)
  3. " Maintainer:  Dave Silvia <dsilvia@mchsi.com>
  4. " Date:        6/30/2004
  5.  
  6. " The start of match (b:SOM) default is:
  7. "       '\<'
  8. " The end of match (b:EOM) default is:
  9. "       '\>'
  10. "
  11. " If you want to use some other start/end of match, just assign the
  12. " value to the b:SOM|EOM variable in your filetype script.
  13. "
  14. " SEE: :h pattern.txt
  15. "      :h pattern-searches
  16. "      :h regular-expression
  17. "      :h matchit
  18.  
  19. let s:myName=expand("<sfile>:t")
  20.  
  21. " matchit.vim not loaded -- don't do anyting
  22. if !exists("loaded_matchit")
  23.     echomsg s:myName.": matchit.vim not loaded -- finishing without loading"
  24.     finish
  25. endif
  26.  
  27. " already been here -- don't redefine
  28. if exists("*AppendMatchGroup")
  29.     finish
  30. endif
  31.  
  32. " Function To Build b:match_words
  33. " The following function, 'AppendMatchGroup', helps to increase
  34. " readability of your filetype script if you choose to use matchit.
  35. " It also precludes many construction errors, reducing the
  36. " construction to simply invoking the function with the match words.
  37. " As an example, let's take the ubiquitous if/then/else/endif type
  38. " of construct.  This is how the entry in your filetype script would look.
  39. "
  40. "     " source the AppendMatchGroup function file
  41. "     runtime ftplugin/AppendMatchGroup.vim
  42. "
  43. "     " fill b:match_words
  44. "     call AppendMatchGroup('if,then,else,endif')
  45. "
  46. " And the b:match_words constructed would look like:
  47. "
  48. "     \<if\>:\<then\>:\<else\>:\<endif\>
  49. " Use of AppendMatchGroup makes your filetype script is a little
  50. " less busy and a lot more readable.  Additionally, it
  51. " checks three critical things:
  52. "
  53. "      1)  Do you have at least 2 entries in your match group.
  54. "
  55. "      2)  Does the buffer variable 'b:match_words' exist?  if not, create it.
  56. "
  57. "      3)  If the buffer variable 'b:match_words' does exist, is the last
  58. "          character a ','?  If not, add it before appending.
  59. " You should now be able to match 'if/then/else/endif' in succession
  60. " in your source file, in just about any construction you may have
  61. " chosen for them.
  62. "
  63. " To add another group, simply call 'AppendMatchGroup again.  E.G.:
  64. "
  65. "      call AppendMatchGroup('while,do,endwhile')
  66.  
  67. function AppendMatchGroup(mwordList)
  68.     let List=a:mwordList
  69.     let Comma=match(List,',')
  70.     if Comma == -1 || Comma == strlen(List)-1
  71.         echoerr "Must supply a comma separated list of at least 2 entries."
  72.         echoerr "Supplied list: <".List.">"
  73.         return
  74.     endif
  75.     let listEntryBegin=0
  76.     let listEntryEnd=Comma
  77.     let listEntry=strpart(List,listEntryBegin,listEntryEnd-listEntryBegin)
  78.     let List=strpart(List,Comma+1)
  79.     let Comma=match(List,',')
  80.     " if listEntry is all spaces || List is empty || List is all spaces
  81.     if (match(listEntry,'\s\+') == 0 && match(listEntry,'\S\+') == -1)
  82.             \ || List == '' || (match(List,'\s\+') == 0 && match(List,'\S\+') == -1)
  83.         echoerr "Can't use all spaces for an entry <".listEntry.">"
  84.         echoerr "Remaining supplied list: <".List.">"
  85.         return
  86.     endif
  87.  
  88.     if !exists("b:SOM")
  89.         let b:SOM='\<'
  90.     endif
  91.     if !exists("b:EOM")
  92.         let b:EOM='\>'
  93.     endif
  94.     if !exists("b:match_words")
  95.         let b:match_words=''
  96.     endif
  97.     if b:match_words != '' && match(b:match_words,',$') == -1
  98.         let b:match_words=b:match_words.','
  99.     endif
  100.     " okay, all set add first entry in this list
  101.     let b:match_words=b:match_words.b:SOM.listEntry.b:EOM.':'
  102.     while Comma != -1
  103.         let listEntryEnd=Comma
  104.         let listEntry=strpart(List,listEntryBegin,listEntryEnd-listEntryBegin)
  105.         let List=strpart(List,Comma+1)
  106.         let Comma=match(List,',')
  107.         " if listEntry is all spaces
  108.         if match(listEntry,'\s\+') == 0 && match(listEntry,'\S\+') == -1
  109.             echoerr "Can't use all spaces for an entry <".listEntry."> - skipping"
  110.             echoerr "Remaining supplied list: <".List.">"
  111.             continue
  112.         endif
  113.         let b:match_words=b:match_words.b:SOM.listEntry.b:EOM.':'
  114.     endwhile
  115.     let listEntry=List
  116.     let b:match_words=b:match_words.b:SOM.listEntry.b:EOM
  117. endfunction
  118.  
  119. " TODO:  Write a wrapper to handle multiple groups in one function call.
  120. "        Don't see a lot of utility in this as it would undoubtedly warrant
  121. "        continuation lines in the filetype script and it would be a toss
  122. "        up as to which is more readable: individual calls one to a line or
  123. "        a single call with continuation lines.  I vote for the former.
  124.