-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnippit.vim
More file actions
75 lines (65 loc) · 2.2 KB
/
snippit.vim
File metadata and controls
75 lines (65 loc) · 2.2 KB
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
" Vim global plugin for
" Last Change:
" Maintainer: Jakub Mrowiec, Alkagar
" Contact: alkagar@gmail.com
" License: This file is placed in the public domain.
let s:pluginName = "snippit"
let s:pluginVersion = "0.1"
let s:snippitDirectory = "/home/alkagar/repos/templates"
let s:listPosition = 1
" check if plugin was loaded earlier
if exists("g:loaded_snippit")
finish
endif
let g:loaded_snippit = 1
" defining basic mapping
if !hasmapto('<Plug>SnippitOpenTab')
map <unique> <Leader>s <Plug>SnippitOpenTab
endif
noremap <unique> <script> <Plug>SnippitOpenTab :Snippit<CR>
function! s:OpenTab()
execute ":new"
setlocal buftype=nofile
setlocal modifiable
call <SID>CreateSelectorHeader()
let templateList = split(system("ls " . s:snippitDirectory), "\n")
let lineCount = 2
for singleFile in templateList
call setline(lineCount, singleFile)
let lineCount += 1
endfor
setlocal nomodifiable
nnoremap <buffer> <silent> q :call <SID>Close()<CR>
nnoremap <buffer> <silent> <cr> :call <SID>GetSnippet()<CR>
endfunction
function! s:GetSnippet()
"get file name
let s:snippetFile = getline(".")
"if trying get header don't do anything
if (s:snippetFile < s:listPosition)
return
endif
"get file content
let s:snippitContent = split(system("cat " . s:snippitDirectory . "/" . s:snippetFile), "\n")
"close scratch windows
call s:Close()
"set current line
let s:previousCursorPosition = line('.')
"print file content starting with current line
for s:contentLine in s:snippitContent
let s:newLine = s:previousCursorPosition . "G"
execute "normal " . s:newLine . "i\<CR>\<ESC>k"
call setline(s:previousCursorPosition, s:contentLine)
let s:previousCursorPosition += 1
endfor
endfunction
function! s:Close()
execute ":q"
endfunction
function! s:CreateSelectorHeader()
call setline(1, s:pluginName . " version " . s:pluginVersion)
let s:listPosition += 1
endfunction
if !exists(":Snippit")
command -nargs=0 Snippit :call <SID>OpenTab()
endif