summaryrefslogtreecommitdiff
path: root/autoload/l9/tempvariables.vim
blob: ee847ee45e9282a3a721daae6277dedb5164ec54 (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
"=============================================================================
" Copyright (C) 2010 Takeshi NISHIDA
"
"=============================================================================
" LOAD GUARD {{{1

if !l9#guardScriptLoading(expand('<sfile>:p'), 0, 0, [])
  finish
endif

" }}}1
"=============================================================================
" TEMPORARY VARIABLES {{{1

"
let s:origMap = {}

" set temporary variables
function l9#tempvariables#set(group, name, value)
  if !exists('s:origMap[a:group]')
    let s:origMap[a:group] = {}
  endif
  if !exists('s:origMap[a:group][a:name]')
    let s:origMap[a:group][a:name] = eval(a:name)
  endif
  execute 'let ' . a:name . ' = a:value'
endfunction

" set temporary variables
function l9#tempvariables#setList(group, variables)
  for [name, value] in a:variables
    call l9#tempvariables#set(a:group, name, value)
    unlet value " to avoid E706
  endfor
endfunction

" get temporary variables
function l9#tempvariables#getList(group)
  if !exists('s:origMap[a:group]')
    return []
  endif
  return map(keys(s:origMap[a:group]), '[v:val, eval(v:val)]')
endfunction

" restore original variables and clean up.
function l9#tempvariables#end(group)
  if !exists('s:origMap[a:group]')
    return
  endif
  for [name, value] in items(s:origMap[a:group])
    execute 'let ' . name . ' = value'
    unlet value " to avoid E706
  endfor
  unlet s:origMap[a:group]
endfunction

" }}}1
"=============================================================================
" vim: set fdm=marker: