This repository has been archived by the owner on Aug 20, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
gista.vim
81 lines (76 loc) · 1.92 KB
/
gista.vim
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
let s:V = vital#of('vim_gista')
let s:Compat = s:V.import('Vim.Compat')
function! gista#vital() abort
return s:V
endfunction
function! gista#throw(msg) abort
throw printf('vim-gista: %s', a:msg)
endfunction
function! gista#define_variables(prefix, defaults) abort
" Note:
" Funcref is not supported while the variable must start with a capital
let prefix = empty(a:prefix)
\ ? 'g:gista'
\ : printf('g:gista#%s', a:prefix)
for [key, value] in items(a:defaults)
let name = printf('%s#%s', prefix, key)
if !exists(name)
silent execute printf('let %s = %s', name, string(value))
endif
unlet value
endfor
endfunction
let s:_pattern1 = printf(
\ '^gista://\(%s\)/\(%s\)/\(%s\)$',
\ '[^/]\+',
\ '[^/]\+\|[^/]\+/[^/]\+',
\ '[^/]\+',
\)
let s:_pattern2 = printf(
\ '^gista://\(%s\)/\(%s\)\.json$',
\ '[^/]\+',
\ '[^/]\+\|[^/]\+/[^/]\+',
\)
let s:schemes = [
\ [s:_pattern1, {
\ 'apiname': 1,
\ 'gistid': 2,
\ 'filename': 3,
\ 'content_type': 'raw',
\ }],
\ [s:_pattern2, {
\ 'apiname': 1,
\ 'gistid': 2,
\ 'content_type': 'json',
\ }],
\]
function! gista#parse_filename(filename) abort
for scheme in s:schemes
if a:filename !~# scheme[0]
continue
endif
let m = matchlist(a:filename, scheme[0])
let o = {}
for [key, value] in items(scheme[1])
if type(value) == type(0)
let o[key] = m[value]
else
let o[key] = value
endif
unlet value
endfor
return o
endfor
return {}
endfunction
function! gista#get(expr) abort
let filename = expand(a:expr)
let gista = s:Compat.getbufvar(a:expr, 'gista', {})
let gista = extend(copy(gista), gista#parse_filename(filename))
return gista
endfunction
call gista#define_variables('', {
\ 'test': 0,
\ 'debug': 0,
\ 'develop': 0,
\})