My dotfiles. Period.

dialog.vim 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. "=============================================================================
  2. " $Id: dialog.vim 520 2012-03-19 18:09:15Z luc.hermitte $
  3. " File: autoload/lh/buffer/dialog.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: 21st Sep 2007
  10. " Last Update: $Date: 2012-03-19 19:09:15 +0100 (Mon, 19 Mar 2012) $
  11. "------------------------------------------------------------------------
  12. " Description: «description»
  13. "
  14. "------------------------------------------------------------------------
  15. " Installation:
  16. " Drop it into {rtp}/autoload/lh/
  17. " Vim 7+ required.
  18. " History:
  19. " v1.0.0 First Version
  20. " (*) Functions imported from Mail_mutt_alias.vim
  21. " v3.0.0 GPLv3
  22. " TODO:
  23. " (*) --abort-- line
  24. " (*) custom messages
  25. " (*) do not mess with search history
  26. " (*) support any &magic
  27. " (*) syntax
  28. " (*) add number/letters
  29. " (*) tag with '[x] ' instead of '* '
  30. " }}}1
  31. "=============================================================================
  32. "=============================================================================
  33. let s:cpo_save=&cpo
  34. set cpo&vim
  35. "=============================================================================
  36. " ## Globals {{{1
  37. let s:LHdialog = {}
  38. "=============================================================================
  39. " ## Functions {{{1
  40. " # Debug {{{2
  41. function! lh#buffer#dialog#verbose(level)
  42. let s:verbose = a:level
  43. endfunction
  44. function! s:Verbose(expr)
  45. if exists('s:verbose') && s:verbose
  46. echomsg a:expr
  47. endif
  48. endfunction
  49. function! lh#buffer#dialog#debug(expr)
  50. return eval(a:expr)
  51. endfunction
  52. "=============================================================================
  53. " # Dialog functions {{{2
  54. "------------------------------------------------------------------------
  55. function! s:Mappings(abuffer)
  56. " map <enter> to edit a file, also dbl-click
  57. exe "nnoremap <silent> <buffer> <esc> :silent call ".a:abuffer.action."(-1, ".a:abuffer.id.")<cr>"
  58. exe "nnoremap <silent> <buffer> q :call lh#buffer#dialog#select(-1, ".a:abuffer.id.")<cr>"
  59. exe "nnoremap <silent> <buffer> <cr> :call lh#buffer#dialog#select(line('.'), ".a:abuffer.id.")<cr>"
  60. " nnoremap <silent> <buffer> <2-LeftMouse> :silent call <sid>GrepEditFileLine(line("."))<cr>
  61. " nnoremap <silent> <buffer> Q :call <sid>Reformat()<cr>
  62. " nnoremap <silent> <buffer> <Left> :set tabstop-=1<cr>
  63. " nnoremap <silent> <buffer> <Right> :set tabstop+=1<cr>
  64. if a:abuffer.support_tagging
  65. nnoremap <silent> <buffer> t :silent call <sid>ToggleTag(line("."))<cr>
  66. nnoremap <silent> <buffer> <space> :silent call <sid>ToggleTag(line("."))<cr>
  67. endif
  68. nnoremap <silent> <buffer> <tab> :silent call <sid>NextChoice('')<cr>
  69. nnoremap <silent> <buffer> <S-tab> :silent call <sid>NextChoice('b')<cr>
  70. exe "nnoremap <silent> <buffer> h :silent call <sid>ToggleHelp(".a:abuffer.id.")<cr>"
  71. endfunction
  72. "----------------------------------------
  73. " Tag / untag the current choice {{{
  74. function! s:ToggleTag(lineNum)
  75. if a:lineNum > s:Help_NbL()
  76. " If tagged
  77. if (getline(a:lineNum)[0] == '*')
  78. let b:NbTags = b:NbTags - 1
  79. silent exe a:lineNum.'s/^\* / /e'
  80. else
  81. let b:NbTags = b:NbTags + 1
  82. silent exe a:lineNum.'s/^ /* /e'
  83. endif
  84. " Move after the tag ; there is something with the two previous :s. They
  85. " don't leave the cursor at the same position.
  86. silent! normal! 3|
  87. call s:NextChoice('') " move to the next choice
  88. endif
  89. endfunction
  90. " }}}
  91. function! s:Help_NbL()
  92. " return 1 + nb lines of BuildHelp
  93. return 2 + len(b:dialog['help_'.b:dialog.help_type])
  94. endfunction
  95. "----------------------------------------
  96. " Go to the Next (/previous) possible choice. {{{
  97. function! s:NextChoice(direction)
  98. " echomsg "next!"
  99. call search('^[ *]\s*\zs\S\+', a:direction)
  100. endfunction
  101. " }}}
  102. "------------------------------------------------------------------------
  103. function! s:RedisplayHelp(dialog)
  104. silent! 2,$g/^@/d_
  105. normal! gg
  106. for help in a:dialog['help_'.a:dialog.help_type]
  107. silent put=help
  108. endfor
  109. endfunction
  110. function! lh#buffer#dialog#update(dialog)
  111. set noro
  112. exe (s:Help_NbL()+1).',$d_'
  113. for choice in a:dialog.choices
  114. silent $put=' '.choice
  115. endfor
  116. set ro
  117. endfunction
  118. function! s:Display(dialog, atitle)
  119. set noro
  120. 0 put = a:atitle
  121. call s:RedisplayHelp(a:dialog)
  122. for choice in a:dialog.choices
  123. silent $put=' '.choice
  124. endfor
  125. set ro
  126. exe s:Help_NbL()+1
  127. endfunction
  128. function! s:ToggleHelp(bufferId)
  129. call lh#buffer#find(a:bufferId)
  130. call b:dialog.toggle_help()
  131. endfunction
  132. function! lh#buffer#dialog#toggle_help() dict
  133. let self.help_type
  134. \ = (self.help_type == 'short')
  135. \ ? 'long'
  136. \ : 'short'
  137. call s:RedisplayHelp(self)
  138. endfunction
  139. function! lh#buffer#dialog#new(bname, title, where, support_tagging, action, choices)
  140. " The ID will be the buffer id
  141. let res = {}
  142. let where_it_started = getpos('.')
  143. let where_it_started[0] = bufnr('%')
  144. let res.where_it_started = where_it_started
  145. try
  146. call lh#buffer#scratch(a:bname, a:where)
  147. catch /.*/
  148. echoerr v:exception
  149. return res
  150. endtry
  151. let res.id = bufnr('%')
  152. let b:NbTags = 0
  153. let b:dialog = res
  154. let s:LHdialog[res.id] = res
  155. let res.help_long = []
  156. let res.help_short = []
  157. let res.help_type = 'short'
  158. let res.support_tagging = a:support_tagging
  159. let res.action = a:action
  160. let res.choices = a:choices
  161. " Long help
  162. call lh#buffer#dialog#add_help(res, '@| <cr>, <double-click> : select this', 'long')
  163. call lh#buffer#dialog#add_help(res, '@| <esc>, q : Abort', 'long')
  164. if a:support_tagging
  165. call lh#buffer#dialog#add_help(res, '@| <t>, <space> : Tag/Untag the current item', 'long')
  166. endif
  167. call lh#buffer#dialog#add_help(res, '@| <up>/<down>, <tab>, +/- : Move between entries', 'long')
  168. call lh#buffer#dialog#add_help(res, '@|', 'long')
  169. " call lh#buffer#dialog#add_help(res, '@| h : Toggle help', 'long')
  170. call lh#buffer#dialog#add_help(res, '@+'.repeat('-', winwidth(bufwinnr(res.id))-3), 'long')
  171. " Short Help
  172. " call lh#buffer#dialog#add_help(res, '@| h : Toggle help', 'short')
  173. call lh#buffer#dialog#add_help(res, '@+'.repeat('-', winwidth(bufwinnr(res.id))-3), 'short')
  174. let res.toggle_help = function("lh#buffer#dialog#toggle_help")
  175. let title = '@ ' . a:title
  176. let helpstr = '| Toggle (h)elp'
  177. let title = title
  178. \ . repeat(' ', winwidth(bufwinnr(res.id))-lh#encoding#strlen(title)-lh#encoding#strlen(helpstr)-1)
  179. \ . helpstr
  180. call s:Display(res, title)
  181. call s:Mappings(res)
  182. return res
  183. endfunction
  184. function! lh#buffer#dialog#add_help(abuffer, text, help_type)
  185. call add(a:abuffer['help_'.a:help_type],a:text)
  186. endfunction
  187. "=============================================================================
  188. function! lh#buffer#dialog#quit()
  189. let bufferId = b:dialog.where_it_started[0]
  190. echohl WarningMsg
  191. echo "Abort"
  192. echohl None
  193. quit
  194. call lh#buffer#find(bufferId)
  195. endfunction
  196. " Function: lh#buffer#dialog#select(line, bufferId [,overriden-action])
  197. function! lh#buffer#dialog#select(line, bufferId, ...)
  198. if a:line == -1
  199. call lh#buffer#dialog#quit()
  200. return
  201. " elseif a:line <= s:Help_NbL() + 1
  202. elseif a:line <= s:Help_NbL()
  203. echoerr "Unselectable item"
  204. return
  205. else
  206. let dialog = s:LHdialog[a:bufferId]
  207. let results = { 'dialog' : dialog, 'selection' : [] }
  208. if b:NbTags == 0
  209. " -1 because first index is 0
  210. " let results = [ dialog.choices[a:line - s:Help_NbL() - 1] ]
  211. let results.selection = [ a:line - s:Help_NbL() - 1 ]
  212. else
  213. silent g/^* /call add(results.selection, line('.')-s:Help_NbL()-1)
  214. endif
  215. endif
  216. if a:0 > 0 " action overriden
  217. exe 'call '.dialog.action.'(results, a:000)'
  218. else
  219. exe 'call '.dialog.action.'(results)'
  220. endif
  221. endfunction
  222. function! lh#buffer#dialog#Select(line, bufferId, ...)
  223. echomsg "lh#buffer#dialog#Select() is deprecated, use lh#buffer#dialog#select() instead"
  224. if a:0 > 0 " action overriden
  225. exe 'call lh#buffer#dialog#select(a:line, a:bufferId, a:1)'
  226. else
  227. exe 'call lh#buffer#dialog#select(a:line, a:bufferId)'
  228. endif
  229. endfunction
  230. function! Action(results)
  231. let dialog = a:results.dialog
  232. let choices = dialog.choices
  233. for r in a:results.selection
  234. echomsg '-> '.choices[r]
  235. endfor
  236. endfunction
  237. "=============================================================================
  238. let &cpo=s:cpo_save
  239. "=============================================================================
  240. " vim600: set fdm=marker: