PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/bundle/exvim/autoload/exUtility.vim

https://github.com/showgood/myvim
Vim Script | 1314 lines | 779 code | 190 blank | 345 comment | 165 complexity | 43c469ec790a26d6d527ce778882bdd3 MD5 | raw file
  1. " ======================================================================================
  2. " File : exUtility.vim
  3. " Author : Wu Jie
  4. " Last Change : 11/30/2009 | 18:49:57 PM | Monday,November
  5. " Description :
  6. " ======================================================================================
  7. " check if plugin loaded
  8. if exists('loaded_exutility_auto') || &cp
  9. finish
  10. endif
  11. let loaded_exutility_auto=1
  12. "/////////////////////////////////////////////////////////////////////////////
  13. " variables
  14. "/////////////////////////////////////////////////////////////////////////////
  15. " ========================================================
  16. " local variable initialization
  17. " ========================================================
  18. " ------------------------------------------------------------------
  19. " Desc: store the highlight strings
  20. " ------------------------------------------------------------------
  21. let s:ex_hlRegMap = ["","q","w","e","r"]
  22. " ------------------------------------------------------------------
  23. " Desc: local script vairable initialization
  24. " ------------------------------------------------------------------
  25. let s:ex_editbuf_num = -1
  26. let s:ex_pluginbuf_num = -1
  27. " ------------------------------------------------------------------
  28. " Desc: swap buf infos
  29. " ------------------------------------------------------------------
  30. let s:ex_swap_buf_num = -1
  31. let s:ex_swap_buf_pos = []
  32. " ------------------------------------------------------------------
  33. " Desc: file browse
  34. " ------------------------------------------------------------------
  35. let s:ex_level_list = []
  36. " ------------------------------------------------------------------
  37. " Desc: special mark text
  38. " ------------------------------------------------------------------
  39. let s:ex_special_mark_pattern = 'todo\|xxx\|fixme'
  40. if exists('g:ex_todo_keyword')
  41. for item in split(tolower(g:ex_todo_keyword), ' ')
  42. let s:ex_special_mark_pattern .= '\|\<' . item . '\>'
  43. endfor
  44. endif
  45. if exists('g:ex_comment_lable_keyword')
  46. for item in split(tolower(g:ex_comment_lable_keyword), ' ')
  47. let s:ex_special_mark_pattern .= '\|\<' . item . '\>'
  48. endfor
  49. endif
  50. " ------------------------------------------------------------------
  51. " Desc: language file maps
  52. " ------------------------------------------------------------------
  53. let s:ex_exvim_lang_map = {}
  54. let s:ex_exvim_lang_map['asm'] = [ 'asm', 'ASM' ]
  55. let s:ex_exvim_lang_map['awk'] = [ 'awk', 'gawk', 'mawk' ]
  56. let s:ex_exvim_lang_map['batch'] = [ 'bat' ]
  57. let s:ex_exvim_lang_map['c'] = [ 'c', 'C' ]
  58. "changed by showgood: remove unnecessary extension type
  59. " let s:ex_exvim_lang_map['cpp'] = ['c++', 'cc', 'cp', 'cpp', 'cxx', 'h', 'H', 'h++', 'hh', 'hp', 'hpp', 'hxx', 'inl', 'ipp' ]
  60. let s:ex_exvim_lang_map['cpp'] = ['cpp', 'h', 'hpp']
  61. let s:ex_exvim_lang_map['c#'] = [ 'cs' ]
  62. let s:ex_exvim_lang_map['doxygen'] = [ 'dox', 'doxygen' ]
  63. let s:ex_exvim_lang_map['debug'] = [ 'log', 'err', 'exe' ]
  64. let s:ex_exvim_lang_map['html'] = [ 'htm', 'html' ]
  65. let s:ex_exvim_lang_map['ini'] = [ 'ini', 'cfg' ]
  66. let s:ex_exvim_lang_map['java'] = [ 'java' ]
  67. let s:ex_exvim_lang_map['javascript'] = [ 'js' ]
  68. let s:ex_exvim_lang_map['lua'] = [ 'lua' ]
  69. let s:ex_exvim_lang_map['make'] = [ 'mak', 'mk', 'Makefile', 'makefile' ]
  70. let s:ex_exvim_lang_map['matlab'] = [ 'm' ]
  71. " let s:ex_exvim_lang_map['python'] = [ 'py', 'pyw', 'pyx', 'pxd' ]
  72. let s:ex_exvim_lang_map['python'] = [ 'py']
  73. let s:ex_exvim_lang_map['ruby'] = [ 'rb', 'ruby' ]
  74. " let s:ex_exvim_lang_map['sh'] = [ 'sh', 'SH', 'bsh', 'bash', 'ksh', 'zsh' ]
  75. let s:ex_exvim_lang_map['sh'] = [ 'sh']
  76. let s:ex_exvim_lang_map['shader'] = [ 'hlsl', 'vsh', 'psh', 'fx', 'fxh', 'cg', 'shd', 'glsl' ]
  77. let s:ex_exvim_lang_map['uc'] = [ 'uc' ]
  78. let s:ex_exvim_lang_map['vim'] = [ 'vim' ]
  79. let s:ex_exvim_lang_map['wiki'] = [ 'wiki' ]
  80. let s:ex_exvim_lang_map['xml'] = [ 'xml', 'xsd' ]
  81. let s:ex_exvim_lang_map['qt'] = [ 'qrc', 'pro', 'pri' ]
  82. let s:ex_exvim_lang_map['swig'] = [ 'i', 'swg' ]
  83. " ------------------------------------------------------------------
  84. " Desc: ctags language file maps
  85. " ------------------------------------------------------------------
  86. let s:ex_ctags_lang_map = {}
  87. let s:ex_ctags_lang_map['asm'] = [ 'asm', 'ASM', 's', 'S', 'A51' ]
  88. let s:ex_ctags_lang_map['asp'] = [ 'asp', 'asa' ]
  89. let s:ex_ctags_lang_map['awk'] = [ 'awk', 'gawk', 'mawk' ]
  90. let s:ex_ctags_lang_map['basic'] = [ 'bas', 'bi', 'bb', 'pb' ]
  91. let s:ex_ctags_lang_map['beta'] = [ 'bet' ]
  92. let s:ex_ctags_lang_map['c'] = [ 'c' ]
  93. let s:ex_ctags_lang_map['cpp'] = [ 'c++', 'cc', 'cp', 'cpp', 'cxx', 'h', 'h++', 'hh', 'hp', 'hpp', 'hxx' ]
  94. let s:ex_ctags_lang_map['c#'] = [ 'cs' ]
  95. let s:ex_ctags_lang_map['cobol'] = [ 'cbl', 'cob', 'CBL', 'COB' ]
  96. let s:ex_ctags_lang_map['eiffel'] = [ 'e' ]
  97. let s:ex_ctags_lang_map['erlang'] = [ 'erl', 'ERL', 'hrl', 'HRL' ]
  98. let s:ex_ctags_lang_map['fortran'] = [ 'fo', 'ft', 'f7', 'f9', 'f95' ]
  99. let s:ex_ctags_lang_map['html'] = [ 'htm', 'html' ]
  100. let s:ex_ctags_lang_map['java'] = [ 'java' ]
  101. let s:ex_ctags_lang_map['javascript'] = [ 'js' ]
  102. let s:ex_ctags_lang_map['lisp'] = [ 'cl', 'clisp', 'el', 'l', 'lisp', 'lsp', 'ml' ]
  103. let s:ex_ctags_lang_map['lua'] = [ 'lua' ]
  104. let s:ex_ctags_lang_map['make'] = [ 'mak', 'mk', 'Makefile', 'makefile' ]
  105. let s:ex_ctags_lang_map['pascal'] = [ 'p', 'pas' ]
  106. let s:ex_ctags_lang_map['perl'] = [ 'pl', 'pm', 'plx', 'perl' ]
  107. let s:ex_ctags_lang_map['php'] = [ 'php', 'php3', 'phtml' ]
  108. let s:ex_ctags_lang_map['python'] = [ 'py', 'pyx', 'pxd', 'scons' ]
  109. let s:ex_ctags_lang_map['rexx'] = [ 'cmd', 'rexx', 'rx' ]
  110. let s:ex_ctags_lang_map['ruby'] = [ 'rb', 'ruby' ]
  111. let s:ex_ctags_lang_map['scheme'] = [ 'SCM', 'SM', 'sch', 'scheme', 'scm', 'sm' ]
  112. let s:ex_ctags_lang_map['sh'] = [ 'sh', 'SH', 'bsh', 'bash', 'ksh', 'zsh' ]
  113. let s:ex_ctags_lang_map['slang'] = [ 'sl' ]
  114. let s:ex_ctags_lang_map['sml'] = ['sml', 'sig' ]
  115. let s:ex_ctags_lang_map['sql'] = ['sql' ]
  116. let s:ex_ctags_lang_map['tcl'] = ['tcl', 'tk', 'wish', 'itcl' ]
  117. let s:ex_ctags_lang_map['vera'] = ['vr', 'vri', 'vrh' ]
  118. let s:ex_ctags_lang_map['verilog'] = [ 'v' ]
  119. let s:ex_ctags_lang_map['vim'] = [ 'vim' ]
  120. let s:ex_ctags_lang_map['yacc'] = [ 'y' ]
  121. let s:ex_ctags_lang_map['matlab'] = [ 'm' ]
  122. " ------------------------------------------------------------------
  123. " Desc: project file filter
  124. " ------------------------------------------------------------------
  125. " let s:ex_project_file_filter = 'c,cpp,cxx,c++,C,cc,'
  126. " let s:ex_project_file_filter .= 'h,H,hh,hxx,hpp,inl,'
  127. " let s:ex_project_file_filter .= 'cs,'
  128. " let s:ex_project_file_filter .= 'uc,'
  129. " let s:ex_project_file_filter .= 'hlsl,vsh,psh,fx,fxh,cg,shd,glsl,'
  130. " let s:ex_project_file_filter .= 'py,pyw,'
  131. " let s:ex_project_file_filter .= 'vim,awk,m,'
  132. " let s:ex_project_file_filter .= 'dox,doxygen,'
  133. " let s:ex_project_file_filter .= 'ini,cfg,wiki,'
  134. " let s:ex_project_file_filter .= 'mk,err,exe,bat,sh,'
  135. " changed by showgood, create my own list of filter
  136. let s:ex_project_file_filter = 'c,cpp,c++,C,'
  137. let s:ex_project_file_filter .= 'h,hpp,'
  138. let s:ex_project_file_filter .= 'py,pyw,vim,awk,sh,bat,err,dll,lib'
  139. let s:ex_project_file_filter .= 'xml,xsd,ini,cfg,wiki,mak,makefile'
  140. " ------------------------------------------------------------------
  141. " Desc: project dir filter
  142. " ------------------------------------------------------------------
  143. let s:ex_project_dir_filter = '' " null-string means include all directories
  144. " ------------------------------------------------------------------
  145. " Desc:
  146. " ------------------------------------------------------------------
  147. " TODO: change to proper variable {
  148. let s:ex_MapHelpText={}
  149. let s:ex_MapHelpMode={}
  150. let s:ex_MapHelpOldMode={}
  151. let s:ex_MapLastCursorLine={}
  152. " } TODO end
  153. "/////////////////////////////////////////////////////////////////////////////
  154. " window functions
  155. "/////////////////////////////////////////////////////////////////////////////
  156. " ------------------------------------------------------------------
  157. " Desc: Create window
  158. " buffer_name : a string of the buffer_name
  159. " window_direction : 'topleft', 'botright'
  160. " use_vertical : 0, 1
  161. " edit_mode : 'none', 'append', 'replace'
  162. " init_func_name: 'none', 'function_name'
  163. " ------------------------------------------------------------------
  164. function exUtility#CreateWindow( buffer_name, window_direction, window_size, use_vertical, edit_mode, init_func_name ) " <<<
  165. " If the window is open, jump to it
  166. let winnum = bufwinnr(a:buffer_name)
  167. if winnum != -1
  168. "Jump to the existing window
  169. if winnr() != winnum
  170. exe winnum . 'wincmd w'
  171. endif
  172. if a:edit_mode == 'append'
  173. exe 'normal! G'
  174. elseif a:edit_mode == 'replace'
  175. silent exec '1,$d _'
  176. endif
  177. return
  178. endif
  179. " Create a new window. If user prefers a horizontal window, then open
  180. " a horizontally split window. Otherwise open a vertically split
  181. " window
  182. if a:use_vertical
  183. " Open a vertically split window
  184. let win_dir = 'vertical '
  185. else
  186. " Open a horizontally split window
  187. let win_dir = ''
  188. endif
  189. " If the tag listing temporary buffer already exists, then reuse it.
  190. " Otherwise create a new buffer
  191. let bufnum = bufnr(a:buffer_name)
  192. if bufnum == -1
  193. " Create a new buffer
  194. let wcmd = a:buffer_name
  195. else
  196. " Edit the existing buffer
  197. let wcmd = '+buffer' . bufnum
  198. endif
  199. " Create the ex_window
  200. exe 'silent! ' . win_dir . a:window_direction . ' 10' . ' split ' . wcmd
  201. exe win_dir . 'resize ' . a:window_size
  202. " Initialize the window
  203. if bufnum == -1
  204. call exUtility#InitWindow( a:init_func_name )
  205. endif
  206. " Set direction
  207. let w:use_vertical = a:use_vertical
  208. let w:window_direction = a:window_direction
  209. " adjust with edit_mode
  210. if a:edit_mode == 'append'
  211. exe 'normal! G'
  212. elseif a:edit_mode == 'replace'
  213. silent exec '1,$d _'
  214. endif
  215. " after create the window, record the bufname into the plugin buffer name list
  216. let short_bufname = fnamemodify(a:buffer_name,":p:t")
  217. if index( g:ex_plugin_registered_bufnames, short_bufname ) == -1
  218. silent call add( g:ex_plugin_registered_bufnames, short_bufname )
  219. endif
  220. " record the filetype into the plugin filetype list
  221. let buf_filetype = getbufvar(a:buffer_name,'&filetype')
  222. if index( g:ex_plugin_registered_filetypes, buf_filetype ) == -1
  223. silent call add( g:ex_plugin_registered_filetypes, buf_filetype )
  224. endif
  225. endfunction " >>>
  226. " ------------------------------------------------------------------
  227. " Desc: Init window
  228. " init_func_name: 'none', 'function_name'
  229. " ------------------------------------------------------------------
  230. function exUtility#InitWindow(init_func_name) " <<<
  231. silent! setlocal filetype=ex_plugin
  232. " Folding related settings
  233. silent! setlocal foldenable
  234. silent! setlocal foldminlines=0
  235. silent! setlocal foldmethod=manual
  236. silent! setlocal foldlevel=9999
  237. " Mark buffer as scratch
  238. silent! setlocal buftype=nofile
  239. silent! setlocal bufhidden=hide
  240. silent! setlocal noswapfile
  241. silent! setlocal nobuflisted
  242. silent! setlocal nowrap
  243. " If the 'number' option is set in the source window, it will affect the
  244. " exTagSelect window. So forcefully disable 'number' option for the exTagSelect
  245. " window
  246. silent! setlocal nonumber
  247. " the winfix height width will let plugin-window not join into the <c-w>= operations
  248. silent! setlocal winfixheight
  249. silent! setlocal winfixwidth
  250. " Define the ex autocommands
  251. augroup ex_auto_cmds
  252. autocmd WinLeave * call exUtility#RecordCurrentBufNum()
  253. autocmd BufLeave * call exUtility#RecordSwapBufInfo()
  254. augroup end
  255. " avoid cwd change problem
  256. if exists( 'g:exES_CWD' )
  257. "au BufEnter * silent exec 'lcd ' . escape(g:exES_CWD, " ")
  258. endif
  259. " call the user define init_function
  260. if a:init_func_name != 'none'
  261. exe 'call ' . a:init_func_name . '()'
  262. endif
  263. " Define syntax highlight
  264. " NOTE: define the syntax highlight after user init. this can prevent user
  265. " override the basic syntax.
  266. syntax match ex_SynError '^Error:.*'
  267. endfunction " >>>
  268. " ------------------------------------------------------------------
  269. " Desc: Open window
  270. " buffer_name : a string of the buffer_name
  271. " window_direction : 'topleft', 'botright'
  272. " use_vertical : 0, 1
  273. " edit_mode : 'none', 'append', 'replace'
  274. " backto_editbuf : 0, 1
  275. " init_func_name: 'none', 'function_name'
  276. " call_func_name: 'none', 'function_name'
  277. " ------------------------------------------------------------------
  278. function exUtility#OpenWindow( buffer_name, window_direction, window_size, use_vertical, edit_mode, backto_editbuf, init_func_name, call_func_name ) " <<<
  279. " close ex_plugin window in same position
  280. call exUtility#ClosePluginWindowInSamePosition ( a:use_vertical, a:window_direction )
  281. " go to edit buffer first, then open the window, this will avoid some bugs
  282. call exUtility#RecordCurrentBufNum()
  283. call exUtility#GotoEditBuffer()
  284. " If the window is open, jump to it
  285. let winnum = bufwinnr(a:buffer_name)
  286. if winnum != -1
  287. " Jump to the existing window
  288. if winnr() != winnum
  289. exe winnum . 'wincmd w'
  290. endif
  291. if a:edit_mode == 'append'
  292. exe 'normal! G'
  293. elseif a:edit_mode == 'replace'
  294. silent exec '1,$d _'
  295. endif
  296. return
  297. endif
  298. " Open window
  299. call exUtility#CreateWindow( a:buffer_name, a:window_direction, a:window_size, a:use_vertical, a:edit_mode, a:init_func_name )
  300. if a:call_func_name != 'none'
  301. exe 'call ' . a:call_func_name . '()'
  302. endif
  303. "
  304. if a:backto_editbuf
  305. " Need to jump back to the original window only if we are not
  306. " already in that window
  307. call exUtility#GotoEditBuffer()
  308. endif
  309. endfunction " >>>
  310. " ------------------------------------------------------------------
  311. " Desc: Close window
  312. " ------------------------------------------------------------------
  313. function exUtility#CloseWindow( buffer_name ) " <<<
  314. "Make sure the window exists
  315. let winnum = bufwinnr(a:buffer_name)
  316. if winnum == -1
  317. call exUtility#WarningMsg('Error: ' . a:buffer_name . ' window is not open')
  318. return
  319. endif
  320. " close window
  321. exe winnum . 'wincmd w'
  322. " if this is not the only window
  323. if winbufnr(2) != -1
  324. " If a window other than the a:buffer_name window is open,
  325. " then only close the a:buffer_name window.
  326. close
  327. endif
  328. " go back to edit buffer
  329. call exUtility#GotoEditBuffer()
  330. call exUtility#ClearObjectHighlight()
  331. "if winnr() == winnum
  332. " let last_buf_num = bufnr('#')
  333. " " Already in the window. Close it and return
  334. " if winbufnr(2) != -1
  335. " " If a window other than the a:buffer_name window is open,
  336. " " then only close the a:buffer_name window.
  337. " close
  338. " endif
  339. " " Need to jump back to the original window only if we are not
  340. " " already in that window
  341. " let winnum = bufwinnr(last_buf_num)
  342. " if winnr() != winnum
  343. " exe winnum . 'wincmd w'
  344. " endif
  345. "else
  346. " " Goto the a:buffer_name window, close it and then come back to the
  347. " " original window
  348. " let cur_buf_num = bufnr('%')
  349. " exe winnum . 'wincmd w'
  350. " close
  351. " " Need to jump back to the original window only if we are not
  352. " " already in that window
  353. " let winnum = bufwinnr(cur_buf_num)
  354. " if winnr() != winnum
  355. " exe winnum . 'wincmd w'
  356. " endif
  357. "endif
  358. endfunction " >>>
  359. " ------------------------------------------------------------------
  360. " Desc: Toggle window
  361. " ------------------------------------------------------------------
  362. function exUtility#ToggleWindow( buffer_name, window_direction, window_size, use_vertical, edit_mode, backto_editbuf, init_func_name, call_func_name ) " <<<
  363. " If exTagSelect window is open then close it.
  364. let winnum = bufwinnr(a:buffer_name)
  365. if winnum != -1
  366. call exUtility#CloseWindow(a:buffer_name)
  367. return
  368. endif
  369. call exUtility#OpenWindow( a:buffer_name, a:window_direction, a:window_size, a:use_vertical, a:edit_mode, a:backto_editbuf, a:init_func_name, a:call_func_name )
  370. endfunction " >>>
  371. " ------------------------------------------------------------------
  372. " Desc: Resize window use increase value
  373. " ------------------------------------------------------------------
  374. function exUtility#ResizeWindow( use_vertical, original_size, increase_size ) " <<<
  375. if a:use_vertical
  376. let new_size = a:original_size
  377. if winwidth('.') <= a:original_size
  378. let new_size = a:original_size + a:increase_size
  379. endif
  380. silent exe 'vertical resize ' . new_size
  381. else
  382. let new_size = a:original_size
  383. if winheight('.') <= a:original_size
  384. let new_size = a:original_size + a:increase_size
  385. endif
  386. silent exe 'resize ' . new_size
  387. endif
  388. endfunction " >>>
  389. " ------------------------------------------------------------------
  390. " Desc:
  391. " ------------------------------------------------------------------
  392. function exUtility#OperateWindow( title, close_when_selected, backto_edibut, hl_obj_line ) " <<<
  393. if a:close_when_selected
  394. call exUtility#CloseWindow(a:title)
  395. call exUtility#GotoEditBuffer()
  396. else
  397. " go back to edit buffer first
  398. call exUtility#GotoEditBuffer()
  399. " highlight object line when
  400. " 1: we not close selected window
  401. " 2: if we needed
  402. if a:hl_obj_line
  403. call exUtility#HighlightObjectLine()
  404. exe 'normal! zz'
  405. endif
  406. " if not back to edit buffer, we jump back to specificed window
  407. if !a:backto_edibut
  408. let winnum = bufwinnr(a:title)
  409. if winnr() != winnum
  410. exe winnum . 'wincmd w'
  411. endif
  412. endif
  413. endif
  414. endfunction " >>>
  415. " ------------------------------------------------------------------
  416. " Desc:
  417. " ------------------------------------------------------------------
  418. function exUtility#ClosePluginWindowInSamePosition ( use_vertical, window_direction ) " <<<
  419. " NOTE: the CloseWindow should be called in each situation.
  420. " Since you can: in a exProject/Minibuffer window try to open another plugin window.
  421. " or just open a plugin window that already have another plugin window take the position.
  422. " check if a ex window exists on the target position, if yes, close it.
  423. for nr_win in range(1,winnr("$"))
  424. if getwinvar(nr_win, "use_vertical") == a:use_vertical && getwinvar(nr_win, "window_direction") == a:window_direction
  425. " get the ex window, change window to the target window
  426. silent exe nr_win . 'wincmd w'
  427. call exUtility#CloseWindow( bufname('%') )
  428. return
  429. endif
  430. endfor
  431. " check if current window is a plugin window ( except minibuf, exProject), if yes close it
  432. if exUtility#IsRegisteredPluginBuffer ( bufname('%') ) &&
  433. \ fnamemodify(bufname('%'),":p:t") !=# "-MiniBufExplorer-" &&
  434. \ &filetype != 'ex_project' &&
  435. \ &filetype != 'nerdtree'
  436. call exUtility#CloseWindow( bufname('%') )
  437. return
  438. endif
  439. endfunction " >>>
  440. " ------------------------------------------------------------------
  441. " Desc:
  442. " ------------------------------------------------------------------
  443. function exUtility#CloseAllExpluginWindow() " <<<
  444. " walk through all window in exvim
  445. let i = 1
  446. let bufnum_list = []
  447. while i <= winnr("$")
  448. let bnum = winbufnr(i)
  449. let buf_filetype = getbufvar(bnum, '&filetype')
  450. if bnum != -1 && exUtility#IsRegisteredPluginBuffer ( bufname('%') )
  451. silent call add ( bufnum_list, bnum )
  452. endif
  453. let i += 1
  454. endwhile
  455. "
  456. for bnum in bufnum_list
  457. call exUtility#CloseWindow ( bufname(bnum) )
  458. endfor
  459. endfunction " >>>
  460. "/////////////////////////////////////////////////////////////////////////////
  461. " string functions
  462. "/////////////////////////////////////////////////////////////////////////////
  463. " ------------------------------------------------------------------
  464. " Desc:
  465. " ------------------------------------------------------------------
  466. function exUtility#PutLine( len, line_type ) " <<<
  467. let plen = a:len - strlen(getline('.'))
  468. if (plen > 0)
  469. execute 'normal! ' plen . 'A' . a:line_type
  470. endif
  471. endfunction " >>>
  472. " ------------------------------------------------------------------
  473. " Desc:
  474. " ------------------------------------------------------------------
  475. function exUtility#PutSegment() " <<<
  476. silent normal! o''
  477. let space = printf('%*s',indent('.'),'')
  478. silent call setline ( '.', space . b:ECcommentOpen . "/////////////////////////////////////////////////////////////////////////////" . b:ECcommentClose )
  479. silent call append ( '.', space . b:ECcommentOpen . "" . b:ECcommentClose )
  480. silent normal! j
  481. silent call append ( '.', space . b:ECcommentOpen . "/////////////////////////////////////////////////////////////////////////////" . b:ECcommentClose )
  482. silent normal! j
  483. silent call append ( '.', "" )
  484. silent normal! j
  485. endfunction " >>>
  486. " ------------------------------------------------------------------
  487. " Desc:
  488. " ------------------------------------------------------------------
  489. function exUtility#PutNote() " <<<
  490. silent normal! o''
  491. let space = printf('%*s',indent('.'),'')
  492. silent call setline ( '.', space . b:ECcommentOpen . " ############################################################################ " . b:ECcommentClose )
  493. silent call append ( '.', space . b:ECcommentOpen . " Note: " . b:ECcommentClose )
  494. silent normal! j
  495. silent call append ( '.', space . b:ECcommentOpen . " ############################################################################ " . b:ECcommentClose )
  496. silent normal! j
  497. silent call append ( '.', "" )
  498. silent normal! j
  499. endfunction " >>>
  500. " ------------------------------------------------------------------
  501. " Desc:
  502. " ------------------------------------------------------------------
  503. function exUtility#PutNamespaceStart( space_name ) " <<<
  504. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  505. silent normal! j
  506. silent call append ( '.', "namespace " . a:space_name . " { " )
  507. silent normal! j
  508. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  509. silent normal! j
  510. silent call append ( '.', "" )
  511. silent normal! j
  512. endfunction " >>>
  513. " ------------------------------------------------------------------
  514. " Desc:
  515. " ------------------------------------------------------------------
  516. function exUtility#PutNamespaceEnd( space_name ) " <<<
  517. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  518. silent normal! j
  519. silent call append ( '.', "} // end namespace " . a:space_name . " " )
  520. silent normal! j
  521. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  522. silent normal! j
  523. silent call append ( '.', "" )
  524. silent normal! j
  525. endfunction " >>>
  526. " ------------------------------------------------------------------
  527. " Desc:
  528. " ------------------------------------------------------------------
  529. function exUtility#PutNamespace( space_name, line1, line2 ) " <<<
  530. "
  531. let first_line = a:line1
  532. let last_line = a:line2
  533. " put namespace end first
  534. silent call cursor( last_line, 1 )
  535. silent put = ''
  536. call exUtility#PutNamespaceEnd(a:space_name)
  537. " then go back to first line and put namespace start
  538. silent call cursor( first_line - 1, 1 )
  539. call exUtility#PutNamespaceStart(a:space_name)
  540. endfunction " >>>
  541. " ------------------------------------------------------------------
  542. " Desc:
  543. " ------------------------------------------------------------------
  544. function exUtility#PutExternC( line1, line2 ) " <<<
  545. "
  546. let first_line = a:line1
  547. let last_line = a:line2
  548. " put namespace end first
  549. silent call cursor( last_line, 1 )
  550. silent put = ''
  551. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  552. silent normal! j
  553. silent call append ( '.', "#ifdef __cplusplus" )
  554. silent normal! j
  555. silent call append ( '.', "} // end extern C " )
  556. silent normal! j
  557. silent call append ( '.', "#endif" )
  558. silent normal! j
  559. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  560. silent normal! j
  561. silent call append ( '.', "" )
  562. silent normal! j
  563. " then go back to first line and put namespace start
  564. silent call cursor( first_line - 1, 1 )
  565. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  566. silent normal! j
  567. silent call append ( '.', "#ifdef __cplusplus" )
  568. silent normal! j
  569. silent call append ( '.', 'extern "C" { ' )
  570. silent normal! j
  571. silent call append ( '.', "#endif" )
  572. silent normal! j
  573. silent call append ( '.', b:ECcommentOpen . " ######################### " . b:ECcommentClose )
  574. silent normal! j
  575. silent call append ( '.', "" )
  576. silent normal! j
  577. endfunction " >>>
  578. " ------------------------------------------------------------------
  579. " Desc:
  580. " ------------------------------------------------------------------
  581. function exUtility#PutSeparate() " <<<
  582. silent normal! o''
  583. let space = printf('%*s',indent('.'),'')
  584. silent call setline ( '.', space . b:ECcommentOpen . " ======================================================== " . b:ECcommentClose )
  585. silent call append ( '.', space . b:ECcommentOpen . " " . b:ECcommentClose )
  586. silent normal! j
  587. silent call append ( '.', space . b:ECcommentOpen . " ======================================================== " . b:ECcommentClose )
  588. silent normal! j
  589. silent call append ( '.', "" )
  590. silent normal! j
  591. endfunction " >>>
  592. " ------------------------------------------------------------------
  593. " Desc:
  594. " ------------------------------------------------------------------
  595. function exUtility#PutDescription() " <<<
  596. silent normal! o''
  597. let space = printf('%*s',indent('.'),'')
  598. silent call setline ( '.', space . b:ECcommentOpen . " ------------------------------------------------------------------ " . b:ECcommentClose )
  599. silent call append ( '.', space . b:ECcommentOpen . " Desc: " . b:ECcommentClose )
  600. silent normal! j
  601. silent call append ( '.', space . b:ECcommentOpen . " ------------------------------------------------------------------ " . b:ECcommentClose )
  602. silent normal! j
  603. silent call append ( '.', "" )
  604. silent normal! j
  605. endfunction " >>>
  606. " ------------------------------------------------------------------
  607. " Desc:
  608. " ------------------------------------------------------------------
  609. function exUtility#PutDefine() " <<<
  610. silent normal! o''
  611. let space = printf('%*s',indent('.'),'')
  612. silent call setline ( '.', space . b:ECcommentOpen . " ------------------------------------------------------------------ " . b:ECcommentClose )
  613. silent call append ( '.', space . b:ECcommentOpen . " Desc: " . b:ECcommentClose )
  614. silent normal! j
  615. silent call append ( '.', space . b:ECcommentOpen . " ------------------------------------------------------------------ " . b:ECcommentClose )
  616. silent normal! j
  617. silent call append ( '.', "" )
  618. silent normal! j
  619. endfunction " >>>
  620. " ------------------------------------------------------------------
  621. " Desc:
  622. " ------------------------------------------------------------------
  623. function exUtility#PutDeclaration() " <<<
  624. silent normal! o''
  625. let space = printf('%*s',indent('.'),'')
  626. silent call setline ( '.', space . b:ECcommentOpen . "/////////////////////////////////////////////////////////////////////////////" . b:ECcommentClose )
  627. silent call append ( '.', space . b:ECcommentOpen . " class " . b:ECcommentClose )
  628. silent normal! j
  629. silent call append ( '.', space . b:ECcommentOpen . " " . b:ECcommentClose )
  630. silent normal! j
  631. silent call append ( '.', space . b:ECcommentOpen . " Purpose: " . b:ECcommentClose )
  632. silent normal! j
  633. silent call append ( '.', space . b:ECcommentOpen . " " . b:ECcommentClose )
  634. silent normal! j
  635. silent call append ( '.', space . b:ECcommentOpen . "/////////////////////////////////////////////////////////////////////////////" . b:ECcommentClose )
  636. silent normal! j
  637. silent call append ( '.', "" )
  638. silent normal! j
  639. endfunction " >>>
  640. " ------------------------------------------------------------------
  641. " Desc:
  642. " ------------------------------------------------------------------
  643. function exUtility#PutHeader() " <<<
  644. let old_lan = v:lang
  645. silent exec 'language C'
  646. if getline(1) =~# b:ECcommentOpen . " ======================================================================================" . b:ECcommentClose
  647. if getline(2) =~# b:ECcommentOpen . " File : .*" . b:ECcommentClose
  648. if getline(3) =~# b:ECcommentOpen . " Author : .*" . b:ECcommentClose
  649. if getline(4) =~# b:ECcommentOpen . " Last Change : .*" . b:ECcommentClose
  650. if getline(5) =~# b:ECcommentOpen . " Description : .*" . b:ECcommentClose
  651. silent call setline ( 2, b:ECcommentOpen . " File : " . fnamemodify(expand('%'), ":t") . b:ECcommentClose )
  652. silent call setline ( 3, b:ECcommentOpen . " Author : " . g:ex_usr_name . " " . b:ECcommentClose )
  653. silent call setline ( 4, b:ECcommentOpen . " Last Change : " . strftime("%m/%d/%Y | %H:%M:%S %p | %A,%B") . b:ECcommentClose )
  654. silent call cursor ( 7, 0 )
  655. return
  656. endif
  657. endif
  658. endif
  659. endif
  660. endif
  661. silent call append ( 0, b:ECcommentOpen . " ======================================================================================" . b:ECcommentClose )
  662. silent call append ( 1, b:ECcommentOpen . " File : " . fnamemodify(expand('%'), ":t") . b:ECcommentClose )
  663. silent call append ( 2, b:ECcommentOpen . " Author : " . g:ex_usr_name . " " . b:ECcommentClose )
  664. silent call append ( 3, b:ECcommentOpen . " Last Change : " . strftime("%m/%d/%Y | %H:%M:%S %p | %A,%B") . b:ECcommentClose )
  665. silent call append ( 4, b:ECcommentOpen . " Description : " . b:ECcommentClose )
  666. silent call append ( 5, b:ECcommentOpen . " ======================================================================================" . b:ECcommentClose )
  667. silent call append ( 6, "" )
  668. silent call cursor ( 7, 0 )
  669. " FIXME: cannot reset {
  670. " silent exec 'language ' . old_lan
  671. " } FIXME end
  672. endfunction " >>>
  673. " ------------------------------------------------------------------
  674. " Desc:
  675. " ------------------------------------------------------------------
  676. function exUtility#PutMain() " <<<
  677. execute 'normal! ' . 'o' . "int main( int argc, char* argv[] )"
  678. execute 'normal! ' . "o" . "{"
  679. execute 'normal! ' . "o" . "}\<CR>"
  680. execute 'normal! ' . "2k"
  681. endfunction " >>>
  682. " ------------------------------------------------------------------
  683. " Desc:
  684. " ------------------------------------------------------------------
  685. function exUtility#PutClass( class_type, class_name ) " <<<
  686. execute 'normal! ' . 'o' . "///////////////////////////////////////////////////////////////////////////////"
  687. execute 'normal! ' . 'o' . "// class " . a:class_name
  688. execute 'normal! ' . 'o' . "// "
  689. execute 'normal! ' . 'o' . "// Purpose: "
  690. execute 'normal! ' . 'o' . "// "
  691. execute 'normal! ' . 'o' . "///////////////////////////////////////////////////////////////////////////////"
  692. execute 'normal! ' . 'o'
  693. execute 'normal! ' . 'o' . a:class_type . " " . a:class_name
  694. execute 'normal! ' . 'o' . "{"
  695. execute 'normal! ' . 'o' . "public:"
  696. execute 'normal! ' . 'o' . "// internal typedef"
  697. execute 'normal! ' . 'o' . "typedef " . a:class_name . " self_t;"
  698. execute 'normal! ' . 'o'
  699. execute 'normal! ' . 'o' . "public:"
  700. execute 'normal! ' . 'o' . "// con/de-structor"
  701. execute 'normal! ' . 'o' . a:class_name . " ();"
  702. execute 'normal! ' . 'o' . "virtual ~" . a:class_name . " ();"
  703. execute 'normal! ' . 'o'
  704. execute 'normal! ' . 'o' . "public:"
  705. execute 'normal! ' . 'o' . "// copy constructor"
  706. execute 'normal! ' . 'o' . a:class_name . " ( const self_t& _copy );"
  707. execute 'normal! ' . 'o' . "self_t& operator = ( const self_t& _copy );"
  708. execute 'normal! ' . 'o'
  709. execute 'normal! ' . 'o' . "}; // end " . a:class_type . " " . a:class_name
  710. endfunction " >>>
  711. " ------------------------------------------------------------------
  712. " Desc:
  713. " ------------------------------------------------------------------
  714. function exUtility#MarkText( text, line1, line2 ) " <<<
  715. "
  716. let first_line = a:line1
  717. let last_line = a:line2
  718. " check if it is special mark, special mark will use uppercase
  719. let text = ''
  720. for item in split(a:text, ' ')
  721. if item =~? s:ex_special_mark_pattern
  722. let text .= toupper(item) . ' '
  723. else
  724. let text .= item . ' '
  725. endif
  726. endfor
  727. " remove last space
  728. let text = strpart ( text, 0, len(text) - 1 )
  729. "
  730. let lstline = last_line + 1
  731. let space = printf('%*s',indent(first_line),'')
  732. call append( last_line , space . b:ECcommentOpen . ' } ' . text . ' end ' . b:ECcommentClose )
  733. call append( first_line -1 , space . b:ECcommentOpen . ' ' . text . ' { ' . b:ECcommentClose )
  734. silent exec ":" . lstline
  735. endfunction " >>>
  736. " ------------------------------------------------------------------
  737. " Desc:
  738. " ------------------------------------------------------------------
  739. function exUtility#RemoveSpecialMarkText () " <<<
  740. let save_cursor = getpos(".")
  741. let save_line = getline(".")
  742. let cur_line = save_line
  743. let start_lnum = -1
  744. let end_lnum = -1
  745. let start_pattern = b:ECcommentOpen . '.*' . '\(' . s:ex_special_mark_pattern . '\)' . '.* { ' . b:ECcommentClose
  746. let end_pattern = b:ECcommentOpen . ' }.*' . '\(' . s:ex_special_mark_pattern . '\)' . '.* end ' . b:ECcommentClose
  747. " found '#if 0' first
  748. while match(cur_line, start_pattern ) == -1
  749. let cur_line_nr = line(".")
  750. silent normal! ^[{
  751. let cur_line = getline(".")
  752. let lnum = line(".")
  753. if lnum == cur_line_nr
  754. if match( cur_line, start_pattern ) == -1
  755. call exUtility#WarningMsg("special mark pattern not found")
  756. silent call cursor(save_cursor[1], save_cursor[2])
  757. return
  758. endif
  759. endif
  760. endwhile
  761. " record the line
  762. let start_lnum = line(".")
  763. silent normal! $]}
  764. let end_lnum = line(".")
  765. " delete the if/else/endif
  766. if end_lnum != -1
  767. silent exe end_lnum
  768. silent normal! dd
  769. endif
  770. if start_lnum != -1
  771. silent exe start_lnum
  772. silent normal! dd
  773. endif
  774. silent call setpos('.', save_cursor)
  775. if match(save_line, start_pattern) == -1 && match(save_line, end_pattern) == -1
  776. silent call search('\V'.save_line, 'b')
  777. endif
  778. silent call cursor(line('.'), save_cursor[2])
  779. if match(save_line, end_pattern) != -1
  780. silent normal! kk
  781. endif
  782. endfunction " >>>
  783. " ------------------------------------------------------------------
  784. " Desc:
  785. " ------------------------------------------------------------------
  786. function exUtility#AlignDigit( align_nr, digit ) " <<<
  787. let print_fmt = '%'.a:align_nr.'d'
  788. let str_digit = printf(print_fmt,a:digit)
  789. retur substitute(str_digit,' ', '0','g')
  790. endfunction " >>>
  791. " ------------------------------------------------------------------
  792. " Desc:
  793. " ------------------------------------------------------------------
  794. function exUtility#InsertIFZero() range " <<<
  795. let lstline = a:lastline + 1
  796. call append( a:lastline , "#endif")
  797. call append( a:firstline -1 , "#if 0")
  798. silent exec ":" . lstline
  799. endfunction " >>>
  800. " ------------------------------------------------------------------
  801. " Desc:
  802. " ------------------------------------------------------------------
  803. function exUtility#RemoveIFZero() range " <<<
  804. " FIXME: when line have **, it will failed
  805. let save_cursor = getpos(".")
  806. let save_line = getline(".")
  807. let cur_line = save_line
  808. let if_lnum = -1
  809. let else_lnum = -1
  810. let endif_lnum = -1
  811. " found '#if 0' first
  812. while match(cur_line, "#if.*0") == -1
  813. let cur_line_nr = line(".")
  814. silent normal! [#
  815. let cur_line = getline(".")
  816. let lnum = line(".")
  817. if lnum == cur_line_nr
  818. if match(cur_line, "#if.*0") == -1
  819. call exUtility#WarningMsg(" not #if 0 matched")
  820. silent call cursor(save_cursor[1], save_cursor[2])
  821. return
  822. endif
  823. endif
  824. endwhile
  825. " record the line
  826. let if_lnum = line(".")
  827. silent normal! ]#
  828. let cur_line = getline(".")
  829. if match(cur_line, "#else") != -1
  830. let else_lnum = line(".")
  831. silent normal! ]#
  832. let endif_lnum = line(".")
  833. else
  834. let endif_lnum = line(".")
  835. endif
  836. " delete the if/else/endif
  837. if endif_lnum != -1
  838. silent exe endif_lnum
  839. silent normal! dd
  840. endif
  841. if else_lnum != -1
  842. silent exe else_lnum
  843. silent call setline('.',"// XXX #else XXX")
  844. endif
  845. if if_lnum != -1
  846. silent exe if_lnum
  847. silent normal! dd
  848. endif
  849. silent call setpos('.', save_cursor)
  850. if match(save_line, "#if.*0") == -1 && match(save_line, "#endif.*") == -1
  851. silent call search('\V'.save_line, 'b')
  852. endif
  853. silent call cursor(line('.'), save_cursor[2])
  854. if match(save_line, "#endif.*") != -1
  855. silent normal! kk
  856. endif
  857. endfunction " >>>
  858. " ------------------------------------------------------------------
  859. " Desc: TODO: check if the last character is space, if not, add space
  860. " TODO: used the longest column as the base column to insert \
  861. " ------------------------------------------------------------------
  862. function exUtility#InsertRemoveExtend() range " <<<
  863. let line = getline('.')
  864. if (strpart(line,strlen(line)-1,1) == "\\")
  865. exec ":" . a:firstline . "," . a:lastline . "s/\\\\$//"
  866. else
  867. exec ":" . a:firstline . "," . a:lastline . "s/$/ \\\\/"
  868. endif
  869. endfunction " >>>
  870. " ------------------------------------------------------------------
  871. " Desc:
  872. " ------------------------------------------------------------------
  873. function exUtility#Yank( string ) " <<<
  874. let @" = a:string
  875. let @* = a:string
  876. endfunction " >>>
  877. " ------------------------------------------------------------------
  878. " Desc:
  879. " ------------------------------------------------------------------
  880. function exUtility#SmartCaseCompare( text, pattern ) " <<<
  881. if match(a:pattern, '\u') != -1 " if we have upper case, use case compare
  882. return match ( a:text, '\C'.a:pattern ) != -1
  883. else " ignore case compare
  884. return match ( a:text, a:pattern ) != -1
  885. endif
  886. endfunction " >>>
  887. "/////////////////////////////////////////////////////////////////////////////
  888. " buffer functions
  889. "/////////////////////////////////////////////////////////////////////////////
  890. " ------------------------------------------------------------------
  891. " Desc:
  892. " ------------------------------------------------------------------
  893. function exUtility#IsRegisteredPluginBuffer ( buffer_name ) " <<<
  894. " check if the buffer filetype is register in the plugin filetype list
  895. if index( g:ex_plugin_registered_filetypes, getbufvar( a:buffer_name, '&filetype' ), 0, 1 ) >= 0
  896. return 1
  897. endif
  898. " check if the buffer name is register in the plugin buffername list
  899. if index( g:ex_plugin_registered_bufnames, fnamemodify( a:buffer_name, ":p:t" ), 0, 1 ) >= 0
  900. return 1
  901. endif
  902. return 0
  903. endfunction " >>>
  904. " ------------------------------------------------------------------
  905. " Desc: Record current buf num when leave
  906. " FIXME: when you split window/open the same file in two window, you can only get the original bufwinnr() by the bufnr().
  907. " FIXME: :sp will trigger the WinEnter, find a way to use it.
  908. " ------------------------------------------------------------------
  909. function exUtility#RecordCurrentBufNum() " <<<
  910. let short_bufname = fnamemodify(bufname('%'),":p:t")
  911. if !exUtility#IsRegisteredPluginBuffer(bufname('%'))
  912. let s:ex_editbuf_num = bufnr('%')
  913. elseif short_bufname !=# "-MiniBufExplorer-"
  914. let s:ex_pluginbuf_num = bufnr('%')
  915. endif
  916. endfunction " >>>
  917. " ------------------------------------------------------------------
  918. " Desc: Record current buf pos
  919. " ------------------------------------------------------------------
  920. function exUtility#RecordSwapBufInfo() " <<<
  921. let bufnr = bufnr('%')
  922. let short_bufname = fnamemodify(bufname(bufnr),":p:t")
  923. if buflisted(bufnr) && bufloaded(bufnr) && bufexists(bufnr) && !exUtility#IsRegisteredPluginBuffer(bufname('%'))
  924. let s:ex_swap_buf_num = bufnr
  925. let s:ex_swap_buf_pos = getpos('.')
  926. endif
  927. endfunction " >>>
  928. " ------------------------------------------------------------------
  929. " Desc:
  930. " ------------------------------------------------------------------
  931. function exUtility#SwapToLastEditBuffer() " <<<
  932. " check if current buffer can use switch
  933. let cur_bufnr = bufnr('%')
  934. let cru_short_bufname = fnamemodify(bufname('%'),":p:t")
  935. if exUtility#IsRegisteredPluginBuffer ( bufname('%') ) " check it is plugin window or not
  936. call exUtility#WarningMsg("Buffer: " .bufname(cur_bufnr). " can't be switch.")
  937. return
  938. endif
  939. " check if last buffer existed and listed, swap if accessable
  940. let last_bufnr = bufnr("#")
  941. let last_short_bufname = fnamemodify(bufname(last_bufnr),":p:t")
  942. if buflisted(last_bufnr) && bufloaded(last_bufnr) && bufexists(last_bufnr) && !exUtility#IsRegisteredPluginBuffer(bufname('%'))
  943. let tmp_swap_buf_pos = deepcopy(s:ex_swap_buf_pos)
  944. let tmp_swap_buf_nr = s:ex_swap_buf_num
  945. let s:ex_swap_buf_pos = getpos('.')
  946. silent exec last_bufnr."b!"
  947. " only recover the pos when we have the right last buffer recorded
  948. if last_bufnr == tmp_swap_buf_nr
  949. silent call setpos('.',tmp_swap_buf_pos)
  950. endif
  951. else
  952. call exUtility#WarningMsg("Buffer: " .bufname(last_bufnr). " can't be accessed.")
  953. endif
  954. endfunction " >>>
  955. " ------------------------------------------------------------------
  956. " Desc:
  957. " ------------------------------------------------------------------
  958. function exUtility#GotoBuffer(cmd) " <<<
  959. " NOTE: there has a bug, in window (not fullscree) mode, some times the buffer will jump to other display screen ( if you use double screen ).
  960. " save current win pos x,y.
  961. if has("gui_running")
  962. let gui_win_pos_x = getwinposx()
  963. let gui_win_pos_y = getwinposy()
  964. endif
  965. " if this is a registered plugin buffer, then go to the edit buffer first
  966. if exUtility#IsRegisteredPluginBuffer(bufname('%'))
  967. call exUtility#GotoEditBuffer()
  968. endif
  969. " jump buffer
  970. if a:cmd ==# 'next'
  971. silent exec "bn!"
  972. elseif a:cmd ==# 'prev'
  973. silent exec "bp!"
  974. endif
  975. " restore windows pos if needed ( if windows pos changed )
  976. if has("gui_running")
  977. if gui_win_pos_x != getwinposx() || gui_win_pos_y != getwinposy()
  978. silent exec "winpos " . gui_win_pos_x " " . gui_win_pos_y
  979. endif
  980. endif
  981. endfunction " >>>
  982. " ------------------------------------------------------------------
  983. " Desc: Update current buffer
  984. " ------------------------------------------------------------------
  985. function exUtility#UpdateCurrentBuffer() " <<<
  986. if exists(':UMiniBufExplorer')
  987. silent exe "UMiniBufExplorer"
  988. endif
  989. endfunction " >>>
  990. " ------------------------------------------------------------------
  991. " Desc:
  992. " ------------------------------------------------------------------
  993. function exUtility#GotoEditBuffer() " <<<
  994. " check and jump to the buffer first
  995. let winnum = bufwinnr(s:ex_editbuf_num)
  996. if winnr() != winnum && winnum != -1 " this will fix the jump error in the vimentry buffer, cause the winnum for vimentry buffer will be -1
  997. exe winnum . 'wincmd w'
  998. endif
  999. call exUtility#RecordCurrentBufNum()
  1000. endfunction " >>>
  1001. " ------------------------------------------------------------------
  1002. " Desc:
  1003. " ------------------------------------------------------------------
  1004. function exUtility#GotoPluginBuffer() " <<<
  1005. " check and jump to the buffer first
  1006. let winnum = bufwinnr(s:ex_pluginbuf_num)
  1007. if winnr() != winnum
  1008. exe winnum . 'wincmd w'
  1009. endif
  1010. call exUtility#RecordCurrentBufNum()
  1011. endfunction " >>>
  1012. " ------------------------------------------------------------------
  1013. " Desc:
  1014. " ------------------------------------------------------------------
  1015. function exUtility#GetEditBufferNum() " <<<
  1016. return s:ex_editbuf_num
  1017. endfunction " >>>
  1018. " ------------------------------------------------------------------
  1019. " Desc:
  1020. " ------------------------------------------------------------------
  1021. function exUtility#YankBufferNameForCode() " <<<
  1022. let buf_name = exUtility#Pathfmt( bufname('%'), 'unix' )
  1023. silent call exUtility#Yank( fnamemodify(buf_name,"") )
  1024. endfunction " >>>
  1025. " ------------------------------------------------------------------
  1026. " Desc:
  1027. " ------------------------------------------------------------------
  1028. function exUtility#YankFilePathForCode() " <<<
  1029. let buf_name = exUtility#Pathfmt( bufname('%'), 'unix' )
  1030. silent call exUtility#Yank( fnamemodify(buf_name,":h") )
  1031. endfunction " >>>
  1032. " ------------------------------------------------------------------
  1033. " Desc:
  1034. " ------------------------------------------------------------------
  1035. function exUtility#SwitchBuffer() " <<<
  1036. " this will fix no jump error < bufwinnr() == -1 >
  1037. silent call exUtility#RecordCurrentBufNum()
  1038. " if current window is same as edit buffer window, jump to last edit window
  1039. if winnr() == bufwinnr(s:ex_editbuf_num)
  1040. call exUtility#GotoPluginBuffer()
  1041. else
  1042. call exUtility#GotoEditBuffer()
  1043. endif
  1044. endfunction " >>>
  1045. " ------------------------------------------------------------------
  1046. " Desc: VimTip #1119: How to use Vim like an IDE
  1047. " delete the buffer; keep windows; create a scratch buffer if no buffers left
  1048. " Using this Kwbd function (:call Kwbd(1)) will make Vim behave like an IDE; or maybe even better.
  1049. " ------------------------------------------------------------------
  1050. function exUtility#Kwbd(kwbdStage) " <<<
  1051. if(a:kwbdStage == 1)
  1052. " check it is plugin window, if yes, close it directly to prevent use \bd
  1053. " close, reopen will loose plugin ability problem
  1054. let cru_short_bufname = fnamemodify(bufname('%'),":p:t")
  1055. if exUtility#IsRegisteredPluginBuffer(bufname('%'))
  1056. silent exec 'close'
  1057. call exUtility#GotoEditBuffer()
  1058. return
  1059. endif
  1060. "
  1061. if(!buflisted(winbufnr(0)))
  1062. bd!
  1063. return
  1064. endif
  1065. let g:kwbdBufNum = bufnr("%")
  1066. let g:kwbdWinNum = winnr()
  1067. windo call exUtility#Kwbd(2)
  1068. exe g:kwbdWinNum . 'wincmd w'
  1069. let g:buflistedLeft = 0
  1070. let g:bufFinalJump = 0
  1071. let l:nBufs = bufnr("$")
  1072. let l:i = 1
  1073. while(l:i <= l:nBufs)
  1074. if(l:i != g:kwbdBufNum)
  1075. if(buflisted(l:i))
  1076. let g:buflistedLeft = g:buflistedLeft + 1
  1077. else
  1078. if(bufexists(l:i) && !strlen(bufname(l:i)) && !g:bufFinalJump)
  1079. let g:bufFinalJump = l:i
  1080. endif
  1081. endif
  1082. endif
  1083. let l:i = l:i + 1
  1084. endwhile
  1085. if(!g:buflistedLeft)
  1086. if(g:bufFinalJump)
  1087. windo if(buflisted(winbufnr(0))) | execute "b! " . g:bufFinalJump | endif
  1088. else
  1089. enew
  1090. let l:newBuf = bufnr("%")
  1091. windo if(buflisted(winbufnr(0))) | execute "b! " . l:newBuf | endif
  1092. endif
  1093. exe g:kwbdWinNum . 'wincmd w'
  1094. endif
  1095. if(buflisted(g:kwbdBufNum) || g:kwbdBufNum == bufnr("%"))
  1096. execute "bd! " . g:kwbdBufNum
  1097. endif
  1098. if(!g:buflistedLeft)
  1099. set buflisted
  1100. set bufhidden=delete
  1101. set buftype=nofile
  1102. setlocal noswapfile
  1103. normal athis is the scratch buffer
  1104. endif
  1105. else
  1106. if(bufnr("%") == g:kwbdBufNum)
  1107. let prevbufvar = bufnr("#")
  1108. if(prevbufvar > 0 && buflisted(prevbufvar) && prevbufvar != g:kwbdBufNum)
  1109. b #
  1110. else
  1111. bn
  1112. endif
  1113. endif
  1114. endif
  1115. endfunction " >>>
  1116. " ------------------------------------------------------------------
  1117. " Desc:
  1118. " ------------------------------------------------------------------
  1119. function exUtility#SaveRestoreInfo() " <<<
  1120. if exists ('g:exES_RestoreInfo')
  1121. let nb_buffers = bufnr('$') " Get the number of the last buffer.
  1122. let idx = 1 " Set the buffer index to one, NOTE: zero is represent to last edit buffer.
  1123. let cmdlist = []
  1124. " store liste