PageRenderTime 37ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/sources_non_forked/tlib/autoload/tlib/scratch.vim

https://gitlab.com/jeichert/vimrc
Vim Script | 143 lines | 88 code | 9 blank | 46 comment | 24 complexity | 8ec3e3801510e77d7b4b5331e793d180 MD5 | raw file
  1. " scratch.vim
  2. " @Author: Tom Link (micathom AT gmail com?subject=[vim])
  3. " @Website: http://www.vim.org/account/profile.php?user_id=4037
  4. " @License: GPL (see http://www.gnu.org/licenses/gpl.txt)
  5. " @Created: 2007-07-18.
  6. " @Last Change: 2014-02-06.
  7. " @Revision: 0.0.252
  8. if &cp || exists("loaded_tlib_scratch_autoload")
  9. finish
  10. endif
  11. let loaded_tlib_scratch_autoload = 1
  12. " Scratch window position. By default the list window is opened on the
  13. " bottom. Set this variable to 'topleft' or '' to change this behaviour.
  14. " See |tlib#input#List()|.
  15. TLet g:tlib_scratch_pos = 'botright'
  16. " If you want the scratch buffer to be fully removed, you might want to
  17. " set this variable to 'wipe'.
  18. " See also https://github.com/tomtom/tlib_vim/pull/16
  19. TLet g:tlib#scratch#hidden = 'hide'
  20. " :def: function! tlib#scratch#UseScratch(?keyargs={})
  21. " Display a scratch buffer (a buffer with no file). See :TScratch for an
  22. " example.
  23. " Return the scratch buffer's number.
  24. " Values for keyargs:
  25. " scratch_split ... 1: split, 0: window, -1: tab
  26. function! tlib#scratch#UseScratch(...) "{{{3
  27. exec tlib#arg#Let([['keyargs', {}]])
  28. " TLogDBG string(keys(keyargs))
  29. let id = get(keyargs, 'scratch', '__Scratch__')
  30. " TLogVAR id, bufwinnr(id)
  31. " TLogVAR bufnr(id), bufname(id)
  32. " TLogVAR 1, winnr(), bufnr('%'), bufname("%")
  33. if bufwinnr(id) != -1
  34. " echom 'DBG noautocmd keepalt keepj' bufwinnr(id) 'wincmd w'
  35. exec 'noautocmd keepalt keepj' bufwinnr(id) 'wincmd w'
  36. " TLogVAR "reuse", bufnr("%"), bufname("%")
  37. else
  38. let winpos = ''
  39. let bn = bufnr(id)
  40. let wpos = get(keyargs, 'scratch_pos', g:tlib_scratch_pos)
  41. " TLogVAR keyargs.scratch_vertical
  42. if get(keyargs, 'scratch_vertical')
  43. let wpos .= ' vertical'
  44. let winpos = tlib#fixes#Winpos()
  45. endif
  46. " TLogVAR wpos
  47. let scratch_split = get(keyargs, 'scratch_split', 1)
  48. if bn != -1
  49. " TLogVAR bn
  50. let wn = bufwinnr(bn)
  51. if wn != -1
  52. " TLogVAR wn
  53. exec 'noautocmd keepalt keepj' (wn .'wincmd w')
  54. else
  55. if scratch_split == 1
  56. let cmd = wpos.' sbuffer!'
  57. elseif scratch_split == -1
  58. let cmd = wpos.' tab sbuffer!'
  59. else
  60. let cmd = 'buffer!'
  61. endif
  62. " TLogVAR cmd, bn
  63. silent exec 'noautocmd keepalt keepj' cmd bn
  64. endif
  65. else
  66. " TLogVAR id
  67. if scratch_split == 1
  68. let cmd = wpos.' split'
  69. elseif scratch_split == -1
  70. let cmd = wpos.' tab split'
  71. else
  72. let cmd = 'edit'
  73. endif
  74. " TLogVAR cmd, id
  75. silent exec 'noautocmd keepalt keepj' cmd escape(id, '%#\ ')
  76. " silent exec 'split '. id
  77. endif
  78. let ft = get(keyargs, 'scratch_filetype', '')
  79. " TLogVAR ft, winpos
  80. if !empty(winpos)
  81. exec winpos
  82. endif
  83. setlocal buftype=nofile
  84. let &l:bufhidden = get(keyargs, 'scratch_hidden', g:tlib#scratch#hidden)
  85. setlocal noswapfile
  86. setlocal nobuflisted
  87. setlocal foldmethod=manual
  88. setlocal foldcolumn=0
  89. setlocal modifiable
  90. setlocal nospell
  91. " TLogVAR &ft, ft
  92. if !empty(ft)
  93. let &l:ft = ft
  94. endif
  95. endif
  96. let keyargs.scratch = bufnr('%')
  97. let keyargs.scratch_tabpagenr = tabpagenr()
  98. let keyargs.scratch_winnr = winnr()
  99. " TLogVAR 2, winnr(), bufnr('%'), bufname("%"), keyargs.scratch
  100. return keyargs.scratch
  101. endf
  102. " Close a scratch buffer as defined in keyargs (usually a World).
  103. " Return 1 if the scratch buffer is closed (or if it already was
  104. " closed).
  105. function! tlib#scratch#CloseScratch(keyargs, ...) "{{{3
  106. TVarArg ['reset_scratch', 1]
  107. let scratch = get(a:keyargs, 'scratch', '')
  108. " TLogVAR scratch, reset_scratch
  109. " TLogDBG string(tlib#win#List())
  110. if !empty(scratch) && winnr('$') > 1
  111. let wn = bufwinnr(scratch)
  112. " TLogVAR wn
  113. try
  114. if wn != -1
  115. " TLogDBG winnr()
  116. let wb = tlib#win#Set(wn)
  117. let winpos = tlib#fixes#Winpos()
  118. wincmd c
  119. if get(a:keyargs, 'scratch_vertical') && !empty(winpos)
  120. exec winpos
  121. endif
  122. " exec wb
  123. " redraw
  124. " TLogVAR winnr()
  125. endif
  126. return 1
  127. finally
  128. if reset_scratch
  129. let a:keyargs.scratch = ''
  130. endif
  131. endtry
  132. endif
  133. return 0
  134. endf