My dotfiles. Period.

command.vim 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. "=============================================================================
  2. " $Id: command.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  3. " File: autoload/lh/command.vim {{{1
  4. " Author: Luc Hermitte <EMAIL:hermitte {at} free {dot} fr>
  5. " <URL:http://code.google.com/p/lh-vim/>
  6. " License: GPLv3 with exceptions
  7. " <URL:http://code.google.com/p/lh-vim/wiki/License>
  8. " Version: 3.0.0
  9. " Created: 08th Jan 2007
  10. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $ (08th Jan 2007)
  11. "------------------------------------------------------------------------
  12. " Description:
  13. " Helpers to define commands that:
  14. " - support subcommands
  15. " - support autocompletion
  16. "
  17. "------------------------------------------------------------------------
  18. " Installation:
  19. " Drop it into {rtp}/autoload/lh/
  20. " Vim 7+ required.
  21. " History:
  22. " v3.0.0: GPLv3
  23. " v2.0.0: Code moved from other plugins
  24. " TODO: «missing features»
  25. " }}}1
  26. "=============================================================================
  27. "=============================================================================
  28. let s:cpo_save=&cpo
  29. set cpo&vim
  30. " ## Debug {{{1
  31. function! lh#command#verbose(level)
  32. let s:verbose = a:level
  33. endfunction
  34. function! s:Verbose(expr)
  35. if exists('s:verbose') && s:verbose
  36. echomsg a:expr
  37. endif
  38. endfunction
  39. function! lh#command#debug(expr)
  40. return eval(a:expr)
  41. endfunction
  42. "------------------------------------------------------------------------
  43. " ## Functions {{{1
  44. " Tool functions {{{2
  45. " Function: lh#command#Fargs2String(aList) {{{3
  46. " @param[in,out] aList list of params from <f-args>
  47. " @see tests/lh/test-Fargs2String.vim
  48. function! lh#command#Fargs2String(aList)
  49. if empty(a:aList) | return '' | endif
  50. let quote_char = a:aList[0][0]
  51. let res = a:aList[0]
  52. call remove(a:aList, 0)
  53. if quote_char !~ '["'."']"
  54. return res
  55. endif
  56. " else
  57. let end_string = '[^\\]\%(\\\\\)*'.quote_char.'$'
  58. while !empty(a:aList) && res !~ end_string
  59. let res .= ' ' . a:aList[0]
  60. call remove(a:aList, 0)
  61. endwhile
  62. return res
  63. endfunction
  64. "------------------------------------------------------------------------
  65. " ## Experimental Functions {{{1
  66. " Internal functions {{{2
  67. " Function: s:SaveData({Data}) {{{3
  68. " @param Data Command definition
  69. " Saves {Data} as s:Data{s:data_id++}. The definition will be used by
  70. " automatically generated commands.
  71. " @return s:data_id
  72. let s:data_id = 0
  73. function! s:SaveData(Data)
  74. if has_key(a:Data, "command_id")
  75. " Avoid data duplication
  76. return a:Data.command_id
  77. else
  78. let s:Data{s:data_id} = a:Data
  79. let id = s:data_id
  80. let s:data_id += 1
  81. let a:Data.command_id = id
  82. return id
  83. endif
  84. endfunction
  85. " BTWComplete(ArgLead, CmdLine, CursorPos): Auto-complete {{{3
  86. function! lh#command#complete(ArgLead, CmdLine, CursorPos)
  87. let tmp = substitute(a:CmdLine, '\s*\S*', 'Z', 'g')
  88. let pos = strlen(tmp)
  89. if 0
  90. call confirm( "AL = ". a:ArgLead."\nCL = ". a:CmdLine."\nCP = ".a:CursorPos
  91. \ . "\ntmp = ".tmp."\npos = ".pos
  92. \, '&Ok', 1)
  93. endif
  94. if 2 == pos
  95. " First argument: a command
  96. return s:commands
  97. elseif 3 == pos
  98. " Second argument: first arg of the command
  99. if -1 != match(a:CmdLine, '^BTW\s\+echo')
  100. return s:functions . "\n" . s:variables
  101. elseif -1 != match(a:CmdLine, '^BTW\s\+\%(help\|?\)')
  102. elseif -1 != match(a:CmdLine, '^BTW\s\+\%(set\|add\)\%(local\)\=')
  103. " Adds a filter
  104. " let files = globpath(&rtp, 'compiler/BT-*')
  105. " let files = files . globpath(&rtp, 'compiler/BT_*')
  106. " let files = files . globpath(&rtp, 'compiler/BT/*')
  107. let files = s:FindFilter('*')
  108. let files = substitute(files,
  109. \ '\(^\|\n\).\{-}compiler[\\/]BTW[-_\\/]\(.\{-}\)\.vim\>\ze\%(\n\|$\)',
  110. \ '\1\2', 'g')
  111. return files
  112. elseif -1 != match(a:CmdLine, '^BTW\s\+remove\%(local\)\=')
  113. " Removes a filter
  114. return substitute(s:FiltersList(), ',', '\n', 'g')
  115. endif
  116. endif
  117. " finally: unknown
  118. echoerr 'BTW: unespected parameter ``'. a:ArgLead ."''"
  119. return ''
  120. endfunction
  121. function! s:BTW(command, ...)
  122. " todo: check a:0 > 1
  123. if 'set' == a:command | let g:BTW_build_tool = a:1
  124. if exists('b:BTW_build_tool')
  125. let b:BTW_build_tool = a:1
  126. endif
  127. elseif 'setlocal' == a:command | let b:BTW_build_tool = a:1
  128. elseif 'add' == a:command | call s:AddFilter('g', a:1)
  129. elseif 'addlocal' == a:command | call s:AddFilter('b', a:1)
  130. " if exists('b:BTW_filters_list') " ?????
  131. " call s:AddFilter('b', a:1)
  132. " endif
  133. elseif 'remove' == a:command | call s:RemoveFilter('g', a:1)
  134. elseif 'removelocal' == a:command | call s:RemoveFilter('b', a:1)
  135. elseif 'rebuild' == a:command " wait for s:ReconstructToolsChain()
  136. elseif 'echo' == a:command | exe "echo s:".a:1
  137. " echo s:{a:f1} ## don't support «echo s:f('foo')»
  138. elseif 'reloadPlugin' == a:command
  139. let g:force_reload_BuildToolsWrapper = 1
  140. let g:BTW_BTW_in_use = 1
  141. exe 'so '.s:sfile
  142. unlet g:force_reload_BuildToolsWrapper
  143. unlet g:BTW_BTW_in_use
  144. return
  145. elseif a:command =~ '\%(help\|?\)'
  146. call s:Usage()
  147. return
  148. endif
  149. call s:ReconstructToolsChain()
  150. endfunction
  151. " ##############################################################
  152. " Public functions {{{2
  153. function! s:FindSubcommand(definition, subcommand)
  154. for arg in a:definition.arguments
  155. if arg.name == a:subcommand
  156. return arg
  157. endif
  158. endfor
  159. throw "NF"
  160. endfunction
  161. function! s:execute_function(definition, params)
  162. if len(a:params) < 1
  163. throw "(lh#command) Not enough arguments"
  164. endif
  165. let l:Fn = a:definition.action
  166. echo "calling ".string(l:Fn)
  167. echo "with ".string(a:params)
  168. " call remove(a:params, 0)
  169. call l:Fn(a:params)
  170. endfunction
  171. function! s:execute_sub_commands(definition, params)
  172. try
  173. if len(a:params) < 1
  174. throw "(lh#command) Not enough arguments"
  175. endif
  176. let subcommand = s:FindSubcommand(a:definition, a:params[0])
  177. call remove(a:params, 0)
  178. call s:int_execute(subcommand, a:params)
  179. catch /NF.*/
  180. throw "(lh#command) Unexpected subcommand `".a:params[0]."'."
  181. endtry
  182. endfunction
  183. function! s:int_execute(definition, params)
  184. echo "params=".string(a:params)
  185. call s:execute_{a:definition.arg_type}(a:definition, a:params)
  186. endfunction
  187. function! s:execute(definition, ...)
  188. try
  189. let params = copy(a:000)
  190. call s:int_execute(a:definition, params)
  191. catch /(lh#command).*/
  192. echoerr v:exception . " in `".a:definition.name.' '.join(a:000, ' ')."'"
  193. endtry
  194. endfunction
  195. function! lh#command#new(definition)
  196. let cmd_name = a:definition.name
  197. " Save the definition as an internal script variable
  198. let id = s:SaveData(a:definition)
  199. exe "command! -nargs=* ".cmd_name." :call s:execute(s:Data".id.", <f-args>)"
  200. endfunction
  201. " Functions }}}1
  202. "------------------------------------------------------------------------
  203. let &cpo=s:cpo_save
  204. "=============================================================================
  205. " vim600: set fdm=marker: