summaryrefslogtreecommitdiff
path: root/base/vim/plugin/scratch.vim
blob: 9b83ea4bbf16b49340666cd14bb07af71c7d1e0e (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
" scratch.vim
" Author: Abhilash Koneri (abhilash_koneri at hotmail dot com)
" Improved By: Hari Krishna Dara (hari_vim at yahoo dot com)
" Last Change: 25-Feb-2004 @ 09:48
" Created: 17-Aug-2002
" Version: 1.0.0
" Download From:
"     http://www.vim.org/script.php?script_id=389
"----------------------------------------------------------------------
" This is a simple plugin that creates a scratch buffer for your
" vim session and helps to access it when you need it.  
"
" If you like the custom mappings provided in the script - hitting
" <F8> should create a new scratch buffer. You can do your scribes
" here and if you want to get rid of it, hit <F8> again inside scratch buffer
" window. If you want to get back to the scratch buffer repeat <F8>. Use
" <Plug>ShowScratchBuffer and <Plug>InsShowScratchBuffer to customize these
" mappings.
"
" If you want to designate a file into which the scratch buffer contents
" should automatically be dumped to, when Vim exits, set its path to
" g:scratchBackupFile global variable. This file can be accessed just in case
" you happen to have some important information in the scratch buffer and quit
" Vim (or shutdown the m/c) forgetting to copy it over. The target file is
" force overwritten using the :write! command so make sure you set a file name
" that can accidentally be used for other purposes (especially when you use
" relative paths). I recommend a value of '/tmp/scratch.txt'.
" CAUTION: This feature works only when Vim generates VimLeavePre autocommad.
"
" Custom mappings
" ---------------
" The ones defined below are not very ergonomic!
"----------------------------------------------------------------------
"Standard Inteface:  <F8> to make a new ScratchBuffer, <F8>-again to hide one

if exists('loaded_scratch')
  finish
endif
let loaded_scratch = 1

" Make sure line-continuations won't cause any problem. This will be restored
"   at the end
let s:save_cpo = &cpo
set cpo&vim

if (! exists("no_plugin_maps") || ! no_plugin_maps) &&
      \ (! exists("no_scratch_maps") || ! no_scratch_maps)
  if !hasmapto('<Plug>ShowScratchBuffer',"n")
    nmap <unique> <silent> <F8> <Plug>ShowScratchBuffer
  endif
  if !hasmapto('<Plug>InsShowScratchBuffer',"i")
    imap <unique> <silent> <F8> <Plug>InsShowScratchBuffer
  endif
endif

" User Overrideable Plugin Interface
nmap <script> <silent> <Plug>ShowScratchBuffer
      \ :silent call <SID>ShowScratchBuffer()<cr>
imap <script> <silent> <Plug>InsShowScratchBuffer
      \ <c-o>:silent call <SID>ShowScratchBuffer()<cr>

command! -nargs=0 Scratch :call <SID>ShowScratchBuffer()

if !exists('g:scratchBackupFile')
  let g:scratchBackupFile = '' " So that users can easily find this var.
endif
aug ScratchBackup
  au!
  au VimLeavePre * :call <SID>BackupScratchBuffer()
aug END

let s:SCRATCH_BUFFER_NAME="[Scratch]"
if !exists('s:buffer_number') " Supports reloading.
  let s:buffer_number = -1
endif

"----------------------------------------------------------------------
" Diplays the scratch buffer. Creates one if it is an already 
" present
"----------------------------------------------------------------------
function! <SID>ShowScratchBuffer()
  if(s:buffer_number == -1 || bufexists(s:buffer_number) == 0)
    " Temporarily modify isfname to avoid treating the name as a pattern.
    let _isf = &isfname
    set isfname-=\
    set isfname-=[
    if exists('+shellslash')
      exec "sp \\\\". s:SCRATCH_BUFFER_NAME
    else
      exec "sp \\". s:SCRATCH_BUFFER_NAME
    endif
    let &isfname = _isf
    let s:buffer_number = bufnr('%')
  else
    let buffer_win=bufwinnr(s:buffer_number)
    if(buffer_win == -1)
      exec 'sb '. s:buffer_number
    else
      exec buffer_win.'wincmd w'
    endif
  endif
  " Do setup always, just in case.
  setlocal buftype=nofile
  setlocal bufhidden=hide
  setlocal nobuflisted
  setlocal noswapfile
  setlocal noro
  nmap <buffer> <silent> <Plug>ShowScratchBuffer :hide<cr>
  imap <buffer> <silent> <Plug>InsShowScratchBuffer <c-o>:hide<cr>
  command! -buffer -nargs=0 Scratch :hide
endfunction

function! s:BackupScratchBuffer()
  if s:buffer_number != -1 && exists('g:scratchBackupFile') &&
        \ g:scratchBackupFile != ''
    exec 'split #' . s:buffer_number
    " Avoid writing empty scratch buffers.
    if line('$') > 1 || getline(1) !~ '^\s*$'
      let _cpo=&cpo
      try
        set cpo-=A
        exec 'write!' g:scratchBackupFile
      finally
        let &cpo=_cpo
      endtry
    endif
  endif
endfunction

" Restore cpo.
let &cpo = s:save_cpo
unlet s:save_cpo

" vim6: sw=2 et