forked from vim-jp/vital.vim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Guard.txt
193 lines (164 loc) · 6.42 KB
/
Guard.txt
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
*vital/Vim/Guard.txt* Vim options/variables guard utility
Maintainer: lambdalisue <[email protected]>
==============================================================================
CONTENTS *Vital.Vim.Guard-contents*
INTRODUCTION |Vital.Vim.Guard-introduction|
USAGE |Vital.Vim.Guard-usage|
INTERFACE |Vital.Vim.Guard-interface|
FUNCTIONS |Vital.Vim.Guard-functions|
==============================================================================
INTRODUCTION *Vital.Vim.Guard-introduction*
*Vital.Vim.Guard* is used to restore previous values of options/variables.
The followings are supported
- Restoring values of options
- |options|
- |global-local|
- |local-options|
- Restoring values of existing environmental variables
- |let-environment|
- Restoring values of registers
- |let-register|
- Restoring values of variables
- |buffer-variable| (b:)
- |window-variable| (w:)
- |tabpage-variable| (t:)
- |global-variable| (g:)
- Restoring values in a dictionary
- |local-variable| (l:), in Vim 7.3.560 or later
- |script-variable| (s:)
- |dict|
- Restoring instance
- |List|
- |Dictionary|
==============================================================================
USAGE *Vital.Vim.Guard-usage*
Specify options/variables to |Vital.Vim.Guard.store()| and restore previous
values with |Vital.Vim.Guard-instance.restore()| like:
>
let s:G = s:V.import('Vim.Guard')
let g:foo = 'foo'
function! s:foobar() abort
call setreg('a', 'foo')
let $EDITOR = 'vim'
let foo = 'foo'
let l:list1 = ['foo']
let l:dict1 = {'foo': 'bar'}
let l:list2 = [['foo']]
let l:dict2 = {'foo': ['bar']}
" Guard options/variables
let guard = s:G.store([
\ '&backup',
\ '@a',
\ '$EDITOR',
\ 'g:foo',
\ ['foo', l:],
\ [l:list1],
\ [l:dict1],
\ [l:list2, 1],
\ [l:dict2, 1],
\])
" Assign temporary values
set nobackup
call setreg('a', 'bar')
let $EDITOR = 'nvim'
unlet g:foo
let g:foo = 1
unlet foo
let foo = []
call remove(l:list1, 0)
unlet l:dict1['foo']
call remove(l:list2, 0)
unlet l:dict2['foo']
" restore previous values
call guard.restore()
" Check if the value is restored
echo &backup
" => 1
echo getreg('a')
" => foo
echo $EDITOR
" => vim
echo g:foo
" => 'foo'
echo foo
" => foo
echo l:list1
" => ['foo']
echo l:dict1
" => {'foo': 'bar'}
echo l:list2
" => [['foo']]
echo l:dict2
" => {'foo': ['bar']}
endfunction
<
==============================================================================
INTERFACE *Vital.Vim.Guard-interface*
------------------------------------------------------------------------------
CONSTANTS *Vital.Vim.Guard-constants*
*Vital.Vim.Guard.is_local_variable_supported*
is_local_variable_supported
1 or 0 to indicate whether the |local-variable| is supported.
A |lockvar| flag of |local-variable| in Vim 7.3.559 or earlier is not
initialized and the value of the flag is undetermined. That mean the
method |Vital.Vim.Guard-instance.restore()| may fail due to |E741|.
Developers should not use this module to store/restore |local-variable|
when this constant is 0.
*Vital.Vim.Guard.is_third_argument_of_getreg_supported*
is_third_argument_of_getreg_supported
1 or 0 to indicate whether the third argument of |getreg()| function.
------------------------------------------------------------------------------
FUNCTIONS *Vital.Vim.Guard-functions*
store({targets}) *Vital.Vim.Guard.store()*
Create a new guard instance. Values of options/variables listed in
{targets} |List| will be stored in the instance and can be restored by
calling .restore() method of the instance.
The following type of value is allowed for items ({target}) of the
{targets}:
|List|
If the {target} contains:
- One item, the item is assumed as an instance of |List| or |Dictionary|
and the instance will be guarded. Note that if you assign a
different instance after store, it won't restore the values.
It works equal to [{instance}, 0] explained below.
- Two items, the list is assumed as 1.) [{attr}, {namespace}] or 2.)
[{instance}, {shallow}] described individually below:
1.) Where {attr} is a name of attribute in {namespace} |Dictionary|.
If {attr} does not exist in {namespace}, The {attr} in {namespace}
will be removed from {namespace} if exists when
.restore() method of the guard instance is called.
Use this type to guard |local-variable| (|l:|), |script-variable| (|s:|),
or attributes in |dict|.
See |Vital.Vim.Guard.is_local_variable_supported| if you would like
to store/restore |local-variable| in Vim 7.3.559 or earlier.
2.) Where {instance} is an instance of |List| or |Dictionary| and
{shallow} is a flag to use |copy| instead of |deepcopy| to store
contents of the instance. When {shallow} is 1, the content of the
instance is copied rather than deepcopied, indicating that values
could not be restored when user changes the contents inside the
contents of the instance. Use {shallow} when the reference of the
each contents are prior to the content itself.
It will raise an exception when {target} contains less or more number
of items than expect.
|String|
If the {target} starts from:
- &, &g:, or &l:, the named option would be guarded. Note that
individual assignment of option does not work on Vim 7.3.
- @, the named register would be guarded. Note that unnamed register
should use @@ instead of @, which is explained in |let-register|.
- $, the named environment variable would be guarded. Note that Vim
does not allow to unlet environment variables so Vim.Guard cannot
guard non existing environment variable and will throw an exception
when the variable is going to be guarded.
- b:, w:, t:, or g:, a named |buffer-variable|, |window-variable|,
|tabpage-variable|, or |global-variable| would be guarded
Note that the guard instance is insensible of the change of the
current buffer, mean that |buffer-variable|, |window-variable|, or
|tabpage-variable| would not be correctly re-stored when users move the
buffer to the other before |Vital.Vim.Guard-instance.restore()|.
------------------------------------------------------------------------------
INSTANCE *Vital.Vim.Guard-instance*
restore() *Vital.Vim.Guard-instance.restore()*
Restore guarded values.
==============================================================================
vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl