root/lang/vim/scratch/trunk/autoload/scratch.vim @ 4730

Revision 4730, 4.1 kB (checked in by kana, 7 years ago)

lang/vim/scratch:
* vim/dot.vim/autoload/scratch.vim:

  • scratch#open(): Modify not to use g:scratch_show_command to avoid showing
    the scratch buffer in two windows.
  • scratch#evaluate(): Modify to accept line-continuation in script.

* vim/dot.vim/doc/scratch.txt:

  • Update for the above changes.

* Mark as version 0.1+.

  • Property svn:keywords set to Id
Line 
1" scratch.vim - Emacs like scratch buffer.
2" Version: 0.1+
3" Copyright: Copyright (C) 2007 kana <http://nicht.s8.xrea.com/>
4" License: MIT license (see <http://www.opensource.org/licenses/mit-license>)
5" $Id$
6" Variables  "{{{1
7
8let s:bufnr = 0  " The buffer number of the scratch buffer
9
10
11
12
13
14
15
16
17" Interfaces  "{{{1
18function! scratch#open()  "{{{2
19  if !(s:bufnr && bufexists(s:bufnr))  " The scratch buffer does not exist.
20    " Create the scratch buffer.
21    " To avoid E303, we must process in the following order:
22    " (1) create unnamed buffer,
23    " (2) set 'noswapfile',
24    " (3) name as specified by g:scratch_buffer_name.
25    let original_bufnr = bufnr('%')
26    let original_bufhidden = &l:bufhidden
27    let &l:bufhidden = 'hide'
28    hide enew
29    setlocal bufhidden=hide buflisted buftype=nofile noswapfile
30    file `=g:scratch_buffer_name`
31    let s:bufnr = bufnr('%')
32
33    " Initialize the scratch buffer.
34    silent doautocmd User PluginScratchInitializeBefore
35    call s:initialize_scratch_buffer()
36    silent doautocmd User PluginScratchInitializeAfter
37
38    " Switch back to the original buffer.
39    if original_bufnr != s:bufnr
40      execute 'buffer' original_bufnr
41      let &l:bufhidden = original_bufhidden
42    endif
43
44    " Open the scratch buffer.
45    if original_bufnr == s:bufnr && tabpagewinnr(tabpagenr(), '$') == 1
46      " Do nothing -- in this case, the scratch buffer will be showed in two
47      " windows if g:scratch_show_command contains a command to split window.
48      " So don't use g:scratch_show_command to avoid such situation.
49    else
50      execute g:scratch_show_command s:bufnr
51    endif
52  else  " The scratch buffer exists.
53    " FIXME: DRY: 'switchbuf' useopen.
54    let winnr = bufwinnr(s:bufnr)
55    if winnr == -1  " The scratch buffer is not visible.
56      if bufname('')=='' && (!&l:modified) && tabpagewinnr(tabpagenr(),'$')==1
57        " The current tab is "empty", so don't use g:scratch_show_command to
58        " avoid show the scratch buffer in two windows.
59        execute 'buffer' s:bufnr
60      else
61        execute g:scratch_show_command s:bufnr
62      endif
63    else
64      execute winnr 'wincmd w'
65    endif
66  endif
67endfunction
68
69
70
71
72function! scratch#close()  "{{{2
73  let scratch_winnr = bufwinnr(s:bufnr)
74  if scratch_winnr == -1  " The scratch buffer is not visible.
75    return
76  endif
77
78  let max_winnr = winnr('$')
79  let original_winnr = winnr()
80  execute scratch_winnr 'wincmd w'
81  close
82  if max_winnr <= 2
83    " nop
84  else
85    execute (original_winnr - (scratch_winnr < original_winnr ? 1 : 0))
86          \ 'wincmd w'
87  endif
88endfunction
89
90
91
92
93function! scratch#evaluate_linewise(line1, line2, adjust_cursorp)  "{{{2
94  let bufnr = bufnr('')
95  call scratch#evaluate([bufnr, a:line1, 1, 0],
96     \                  [bufnr, a:line2, len(getline(a:line2)), 0],
97     \                  a:adjust_cursorp)
98endfunction
99
100
101
102
103function! scratch#evaluate(range_head, range_tail, adjust_cursorp)  "{{{2
104  " Yank the script.
105  let original_pos = getpos('.')
106  let original_reg_a = @a
107    call setpos('.', a:range_head)
108    normal! v
109    call setpos('.', a:range_tail)
110    silent normal! "ay
111    let script = @a
112  let @a = original_reg_a
113
114  " Evaluate it.
115  execute substitute(script, '\n\s*\\', '', 'g')
116
117  if a:adjust_cursorp
118    " Move to the next line of the script (add new line if necessary).
119    call setpos('.', a:range_tail)
120    if line('.') == line('$')
121      put =''
122    else
123      normal! +
124    endif
125  else
126    call setpos('.', original_pos)
127  endif
128endfunction
129
130
131
132
133
134
135
136
137" Misc.  "{{{1
138function! s:initialize_scratch_buffer()  "{{{2
139  if &l:filetype == ''
140    setlocal filetype=vim
141  endif
142
143  if &l:filetype == 'vim'
144    0 put ='\" This buffer is for notes you don''t want to save,'
145    put ='\" and for Vim script evaluation.'
146    put ='\"'
147    put ='\" In normal mode, <C-m> or <C-j> will evaluate the current line.'
148    put ='\" In visual mode, <C-m> or <C-j> will evaluate the selected text.'
149    put =''
150    $
151  endif
152
153  silent! map <buffer> <unique> <CR>  <Plug>scratch-evaluate
154  silent! map <buffer> <unique> <C-m>  <Plug>scratch-evaluate
155  silent! map <buffer> <unique> <C-j>  <Plug>scratch-evaluate
156endfunction
157
158
159
160
161
162
163
164
165" __END__  "{{{1
166" vim: foldmethod=marker
Note: See TracBrowser for help on using the browser.