aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/vim-addon-mw-utils/autoload/tovl/scratch_buffer.vim
blob: 217dca119fb57c3a8c7f046c08012bf0fc10dfca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
" old code

augroup TOVLWrite
augroup end

" =========== scratch buffer =========================================
" a scratch buffer is a temporary buffer where the user can enter some text
" It can be used to get commit messages, edit configuration options and so on

function! tovl#scratch_buffer#KeepIntactLineNr()
  let i = 0
  while getline(i)!= b:keepIntact && i < line('$')
    let i = i+1
  endwhile
  if i > line('$')
    return -1
  else
    return i
  endif
endfunction

" opens a buffer and runs an action when the buffer is written
" keys: 
"  name :   the name of the buffer
"  onWrite : will be called on write
"            onWrite is responsible for setlocal nomodified to indicate that
"            saving has been successful
"  help  : callback returning additional information lines
"  getContent : callback returning lines
"  cmds    : extra commands to be run (optional)
"            (maybe you prefer adding them the default way afer the
"            ScratchBuffer call. They'll be rerun on GetContents
"  sp_cmd  : the command to use to create the new buffer. Defaults to :e
"  buftype : ...
"  modifiable : 1 / 0 defaults to 1
function! tovl#scratch_buffer#ScratchBuffer(opts)
  let a:opts['name'] = get(a:opts,'name', 'strach_buffer_without_name')
  exec get(a:opts, 'sp_cmd', 'e').' '.escape(a:opts['name'],' ')
  let b:settings = a:opts
  let b:settings['modifiable'] = get(a:opts,'modifiable', 1)
  setlocal buftype=acwrite
  command! -buffer -nargs=0 Help call tovl#scratch_buffer#Help()

  " setup write notification
  au TOVLWrite BufWriteCmd <buffer> call tovl#scratch_buffer#Write()

  if has_key(a:opts,'getContent')
    command! -buffer -nargs=0 GetContents call tovl#scratch_buffer#GetContents()
    GetContents
    if !b:settings['modifiable']
      setlocal nomodifiable
    endif
  endif
  "let u=&undolevels
  "setlocal undolevels=-1
  "exec 'setlocal undolevels='.u

  " mark buffer as not modified
  setlocal nomodified

  au BufReadCmd <buffer> GetContents

  " run addittional commands
  for cmd in get(a:opts,'cmds',[])
    exec cmd
  endfor
  silent echo get(a:opts,'echo_help', "type :Help for help")
endfunction

" =========== utility functions ======================================

function! tovl#scratch_buffer#Write()
  if has_key(b:settings, 'onWrite')
    call funcref#Call(b:settings['onWrite'])
  else
    echo "don't know how to write. Option hasn't been passed"
  endif
endfunction

function! tovl#scratch_buffer#GetContents()
  setlocal modifiable
  " empty buffer
  %g!//d
  call append(0, funcref#Call(b:settings['getContent']))
  if !b:settings['modifiable']
    setlocal nomodifiable
  endif
  for cmd in get(b:settings,'cmds',[])
    exec cmd
  endfor
endfunction

function! tovl#scratch_buffer#Help()
  let help = ["use :e! to reload contents, ZZ or :w(q) to write and quit"
          \ ,""
          \ ,"Help for this scratch buffer:"
          \ ,"=======================================================","",""]
    \ + funcref#Call(get(b:settings, 'help', []))
  call tovl#scratch_buffer#ScratchBuffer({
        \ 'name' : "return Help of ".b:settings['name'],
        \ 'getContent' : help
        \ })
endfunction