ui.vim 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. let s:buf_nr = -1
  2. "OpenWindow opens a new scratch window and put's the content into the window
  3. function! go#ui#OpenWindow(title, content, filetype) abort
  4. " Ensure there's only one return window in this session/tabpage
  5. call go#util#Windo("unlet! w:vim_go_return_window")
  6. " Mark the window we're leaving as such
  7. let w:vim_go_return_window = 1
  8. " reuse existing buffer window if it exists otherwise create a new one
  9. if !bufexists(s:buf_nr)
  10. execute 'botright new'
  11. file `="[" . a:title . "]"`
  12. let s:buf_nr = bufnr('%')
  13. elseif bufwinnr(s:buf_nr) == -1
  14. execute 'botright new'
  15. execute s:buf_nr . 'buffer'
  16. elseif bufwinnr(s:buf_nr) != bufwinnr('%')
  17. execute bufwinnr(s:buf_nr) . 'wincmd w'
  18. endif
  19. " Resize window to content length
  20. exe 'resize' . len(a:content)
  21. execute "setlocal filetype=".a:filetype
  22. " some sane default values for a readonly buffer
  23. setlocal bufhidden=delete
  24. setlocal buftype=nofile
  25. setlocal noswapfile
  26. setlocal nobuflisted
  27. setlocal winfixheight
  28. setlocal cursorline " make it easy to distinguish
  29. setlocal nonumber
  30. setlocal norelativenumber
  31. setlocal showbreak=""
  32. " we need this to purge the buffer content
  33. setlocal modifiable
  34. "delete everything first from the buffer
  35. %delete _
  36. " add the content
  37. call append(0, a:content)
  38. " delete last line that comes from the append call
  39. $delete _
  40. " set it back to non modifiable
  41. setlocal nomodifiable
  42. " Remove the '... [New File]' message line from the command line
  43. echon
  44. endfunction
  45. function! go#ui#GetReturnWindow() abort
  46. for l:wn in range(1, winnr("$"))
  47. if !empty(getwinvar(l:wn, "vim_go_return_window"))
  48. return l:wn
  49. endif
  50. endfor
  51. endfunction
  52. " CloseWindow closes the current window
  53. function! go#ui#CloseWindow() abort
  54. " Close any window associated with the ui buffer, if it's there
  55. if bufexists(s:buf_nr)
  56. let ui_window_number = bufwinnr(s:buf_nr)
  57. if ui_window_number != -1
  58. execute ui_window_number . 'close'
  59. endif
  60. endif
  61. "return to original window, if it's there
  62. let l:rw = go#ui#GetReturnWindow()
  63. if !empty(l:rw)
  64. execute l:rw . 'wincmd w'
  65. unlet! w:vim_go_return_window
  66. endif
  67. endfunction
  68. " OpenDefinition parses the current line and jumps to it by openening a new
  69. " tab
  70. function! go#ui#OpenDefinition(filter) abort
  71. let curline = getline('.')
  72. " don't touch our first line or any blank line
  73. if curline =~ a:filter || curline =~ "^$"
  74. " suppress information about calling this function
  75. echo ""
  76. return
  77. endif
  78. " format: 'interface file:lnum:coln'
  79. let mx = '^\(^\S*\)\s*\(.\{-}\):\(\d\+\):\(\d\+\)'
  80. " parse it now into the list
  81. let tokens = matchlist(curline, mx)
  82. " convert to: 'file:lnum:coln'
  83. let expr = tokens[2] . ":" . tokens[3] . ":" . tokens[4]
  84. " jump to it in a new tab, we use explicit lgetexpr so we can later change
  85. " the behaviour via settings (like opening in vsplit instead of tab)
  86. lgetexpr expr
  87. tab split
  88. ll 1
  89. " center the word
  90. norm! zz
  91. endfunction
  92. " vim: sw=2 ts=2 et