|
| 1 | +" File: scratch.vim |
| 2 | +" Author: Yegappan Lakshmanan (yegappan AT yahoo DOT com) |
| 3 | +" Version: 1.0 |
| 4 | +" Last Modified: June 3, 2003 |
| 5 | +" |
| 6 | +" Overview |
| 7 | +" -------- |
| 8 | +" You can use the scratch plugin to create a temporary scratch buffer to store |
| 9 | +" and edit text that will be discarded when you quit/exit vim. The contents |
| 10 | +" of the scratch buffer are not saved/stored in a file. |
| 11 | +" |
| 12 | +" Installation |
| 13 | +" ------------ |
| 14 | +" 1. Copy the scratch.vim plugin to the $HOME/.vim/plugin directory. Refer to |
| 15 | +" the following Vim help topics for more information about Vim plugins: |
| 16 | +" |
| 17 | +" :help add-plugin |
| 18 | +" :help add-global-plugin |
| 19 | +" :help runtimepath |
| 20 | +" |
| 21 | +" 2. Restart Vim. |
| 22 | +" |
| 23 | +" Usage |
| 24 | +" ----- |
| 25 | +" You can use the following command to open/edit the scratch buffer: |
| 26 | +" |
| 27 | +" :Scratch |
| 28 | +" |
| 29 | +" To open the scratch buffer in a new split window, use the following command: |
| 30 | +" |
| 31 | +" :Sscratch |
| 32 | +" |
| 33 | +" When you close the scratch buffer window, the buffer will retain the |
| 34 | +" contents. You can again edit the scratch buffer by openeing it using one of |
| 35 | +" the above commands. There is no need to save the scatch buffer. |
| 36 | +" |
| 37 | +" When you quit/exit Vim, the contents of the scratch buffer will be lost. |
| 38 | +" You will not be prompted to save the contents of the modified scratch |
| 39 | +" buffer. |
| 40 | +" |
| 41 | +" You can have only one scratch buffer open in a single Vim instance. If the |
| 42 | +" current buffer has unsaved modifications, then the scratch buffer will be |
| 43 | +" opened in a new window |
| 44 | +" |
| 45 | +" ****************** Do not modify after this line ************************ |
| 46 | +if exists('loaded_scratch') || &cp |
| 47 | + finish |
| 48 | +endif |
| 49 | +let loaded_scratch=1 |
| 50 | + |
| 51 | +" Scratch buffer name |
| 52 | +let ScratchBufferName = "__Scratch__" |
| 53 | + |
| 54 | +" ScratchBufferOpen |
| 55 | +" Open the scratch buffer |
| 56 | +function! s:ScratchBufferOpen(new_win) |
| 57 | + let split_win = a:new_win |
| 58 | + |
| 59 | + " If the current buffer is modified then open the scratch buffer in a new |
| 60 | + " window |
| 61 | + if !split_win && &modified |
| 62 | + let split_win = 1 |
| 63 | + endif |
| 64 | + |
| 65 | + " Check whether the scratch buffer is already created |
| 66 | + let scr_bufnum = bufnr(g:ScratchBufferName) |
| 67 | + if scr_bufnum == -1 |
| 68 | + " open a new scratch buffer |
| 69 | + if split_win |
| 70 | + exe "new " . g:ScratchBufferName |
| 71 | + else |
| 72 | + exe "edit " . g:ScratchBufferName |
| 73 | + endif |
| 74 | + else |
| 75 | + " Scratch buffer is already created. Check whether it is open |
| 76 | + " in one of the windows |
| 77 | + let scr_winnum = bufwinnr(scr_bufnum) |
| 78 | + if scr_winnum != -1 |
| 79 | + " Jump to the window which has the scratch buffer if we are not |
| 80 | + " already in that window |
| 81 | + if winnr() != scr_winnum |
| 82 | + exe scr_winnum . "wincmd w" |
| 83 | + endif |
| 84 | + else |
| 85 | + " Create a new scratch buffer |
| 86 | + if split_win |
| 87 | + exe "split +buffer" . scr_bufnum |
| 88 | + else |
| 89 | + exe "buffer " . scr_bufnum |
| 90 | + endif |
| 91 | + endif |
| 92 | + endif |
| 93 | +endfunction |
| 94 | + |
| 95 | +" ScratchMarkBuffer |
| 96 | +" Mark a buffer as scratch |
| 97 | +function! s:ScratchMarkBuffer() |
| 98 | + setlocal buftype=nofile |
| 99 | + setlocal bufhidden=hide |
| 100 | + setlocal noswapfile |
| 101 | + setlocal buflisted |
| 102 | +endfunction |
| 103 | + |
| 104 | +autocmd BufNewFile __Scratch__ call s:ScratchMarkBuffer() |
| 105 | + |
| 106 | +" Command to edit the scratch buffer in the current window |
| 107 | +command! -nargs=0 Scratch call s:ScratchBufferOpen(0) |
| 108 | +" Command to open the scratch buffer in a new split window |
| 109 | +command! -nargs=0 Sscratch call s:ScratchBufferOpen(1) |
| 110 | + |
0 commit comments