-
Notifications
You must be signed in to change notification settings - Fork 31
/
smartinput.txt
404 lines (285 loc) · 13.3 KB
/
smartinput.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
*smartinput.txt* Smart input assistant for punctuations
Version 0.0.0
Script ID: ****
Copyright (C) 2012 Kana Natsuno <http://whileimautomaton.net/>
License: So-called MIT/X license {{{
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
}}}
CONTENTS *smartinput-contents*
Introduction |smartinput-introduction|
Interface |smartinput-interface|
Functions |smartinput-functions|
Key Mappings |smartinput-key-mappings|
Rules |smartinput-rules|
Customization |smartinput-customization|
Bugs |smartinput-bugs|
Changelog |smartinput-changelog|
==============================================================================
INTRODUCTION *smartinput-introduction*
*smartinput* is a Vim plugin to provide a smart input assistant for
punctuations. Whenever you write a text, especially source code, you have to
input and deal with a pair of punctuations such as (), [], {}, and so on.
This plugin provides various input assistants according to the current context
and 'filetype' as follows (note that "#" indicates the cursor position in the
following examples):
Complete the corresponding character automatically:
Before | Input | After ~
-------+-------+-------- ~
# | ( | (#)
# | { | {#}
# | " | "#"
Leave from the current block easily:
Before | Input | After ~
-------+-------+-------- ~
(#) | ) | ()#
[#] | ] | []#
"foo# | " | "foo"#
Undo the completion easily:
Before | Input | After ~
-------+-------+-------- ~
(#) | <BS> | #
Care to input strings and regular expressions:
Before | Input | After ~
-------+-------+-------- ~
\# | " | \"#
\# | < | \<#
Care to input English words:
Before | Input | After ~
-------+-------+-------- ~
let# | 's | let's#
foo(# | ' | foo('#'
Care to write Lisp/Scheme source code:
Before | Input | After ~
-------+-------+-------- ~
# | 'foo | 'foo#
"#" | 'hi | "'hi#'"
Care to write C-like syntax source code:
Before | Input | After ~
-------------+---------+------------- ~
if (...) {#} | <Enter> | if (...) {
| | #
| | }
if (...) | <Enter> | if (...)
{#} | | {
| | #
| | }
Requirements:
- Vim 7.3 or later
Latest version:
https://github.com/kana/vim-smartinput
Document in HTML format:
http://vim-doc.heroku.com/view?https://github.com/kana/vim-smartinput/blob/master/doc/smartinput.txt
==============================================================================
INTERFACE *smartinput-interface*
------------------------------------------------------------------------------
FUNCTIONS *smartinput-functions*
smartinput#clear_rules() *smartinput#clear_rules()*
Clear all defined rules.
smartinput#define_default_rules() *smartinput#define_default_rules()*
Define |smartinput-default-rules|.
smartinput#define_rule({rule}) *smartinput#define_rule()*
Define a rule for the smart input assistant.
See |smartinput-rules| for the details of {rule}.
*smartinput#map_to_trigger()*
smartinput#map_to_trigger({mode}, {lhs}, {rhs-char}, {rhs-fallback})
Map {lhs} to trigger the smart input assistant for
{rhs-char} in {mode}. If there is no proper rule
for the current context, {rhs-fallback} will be
inserted instead of {lhs}.
{lhs}, {rhs-char} and {rhs-fallback} are strings to
denote key sequences.
{lhs} is mapped in Insert mode if {mode} is 'i'.
{lhs} is mapped in Command-line mode if {mode} is 'c'.
See also |smartinput-exampels| for the details.
smartinput#map_trigger_keys([{overridep}]) *smartinput#map_trigger_keys()*
Map keys to trigger smart input assistant according to
rules which are currently defined.
Existing key mappings are overridden if {overridep} is
true, otherwise existing key mappings are kept.
If {overridep} is omitted, it is treated as a false
value.
------------------------------------------------------------------------------
KEY MAPPINGS *smartinput-key-mappings*
Several characters in Insert mode and Command-line mode are mapped by default
with |smartinput#map_to_trigger()| to trigger the smart input assistant.
See |smartinput-default-rules| for the details.
Set any value to *g:smartinput_no_default_key_mappings*
if you don't want to define the default key mappings.
Each default key mapping will not be defined if the {lhs} is already mapped to
something. For example, the default key mapping on "(" will not be defined if
you map "(" to something in your vimrc.
==============================================================================
RULES *smartinput-rules*
------------------------------------------------------------------------------
RULE DEFINITION *smartinput-rule-definition*
Smart input assistants are defined by "rules". Each "rule" is represented as
a |Dictionary| which contains the following items:
"at" (string) (required)
Specifies the context to use the smart input assistant.
This value is treated as a |regular-expression|.
The smart input assistant will be used if the regular exprssion
matches to the cursor position.
Tips: You can use |/\%#| to indicate the cursor position.
For example, '(\%#)' means that the smart input assistant will be used
if the cursor is located between parentheses.
Though |/\%#| works only for matching with the current buffer,
you can still use it for "at" to specify the cursor position in
Command-line mode.
"char" (string) (required)
Specifies a character to trigger the smart input assistant.
"input" (string) (required)
Specifies a result of the smart input assistant.
This value is used like {rhs} for |:map| commands.
This value is inputted instead of the "char" value.
"mode" (string) (optional)
Specifies the context to use the smart input assistant.
This value is a string of characters to represent modes in which the
smart input assistant will be used. Valid characters and
corresponding modes are as follows:
Character Mode ~
--------- ----------------- ~
i Insert mode
: Command-line mode for normal Ex commands
> Command-line mode for |debug-mode| commands
/ Command-line mode for serching with |/|
? Command-line mode for serching with |?|
@ Command-line mode for |input()|
- Command-line mode for |:insert| or |:append|
The default value is 'i'.
"filetype" (list of strings) (optional)
Specifies the context to use the smart input assistant.
This value is a list of 'filetype' names like "lisp", "scheme" etc.
The smart input assistant will be used if:
- This item is not given, or
- The current buffer's 'filetype' equals one of this value.
If 'filetype' is a compound one which has two or more names
concatenated by ".", each name will be tested instead of the whole
value of 'filetype'.
This specification works only for Insert mode.
"syntax" (list of strings) (optional)
Specifies the context to use the smart input assistant.
This value is a list of syntax names like "Comment", "Constant" etc.
The smart input assistant will be used if:
- This item is not given, or
- The cursor position is highlighted as one of this value.
See also |group-name| for examples of available values.
Note that only "wide" syntax items are effective. In other words,
syntax items which is linked to another is not effective, and they
will never be matched. For example:
* "Constant" is effective because it is not linked to anything.
* "String" is not effective because it is linked to "Constant".
* "lispString" is not effective because it is linked to "String".
This specification works only for Insert mode.
------------------------------------------------------------------------------
DEFAULT RULES *smartinput-default-rules*
For convenience, there are many rules defined by default.
If you read this document in Vim,
see ../autoload/smartinput.vim for the details (type |gf| at the file name).
If you read this document with a Web browser, see the following instead:
https://github.com/kana/vim-smartinput/blob/master/autoload/smartinput.vim
------------------------------------------------------------------------------
PRIORITIES *smartinput-rule-priorities*
Rules have priorities. It is used to select the best rule for the smart input
assistant.
Rule priorities are calculated as follows:
(a) "at" length.
(b) 100 / {the number of elements} if "filetype" is given.
0 if "filetype" is not given.
(c) 100 / {the number of elements} if "syntax" is given.
0 if "syntax" is not given.
(d) 100 / {the length of "mode"}.
Rule priority = (a) + (b) + (c) + (d)
For example:
{'at': '\%#', 'char': "'", 'input': "''\<Left>"} ~
(a) 3
(b) 0
(c) 0
(d) 100 / 1 = 100
Priority = 3 + 0 + 0 + 100 = 103
{'at': '\%#', 'char': "'", 'input': "'", ~
'filetype': ['lisp', 'scheme']} ~
(a) 3
(b) 100 / 2 = 50
(c) 0
(d) 100 / 1 = 100
Priority = 3 + 50 + 0 + 100 = 153
{'at': '\%#', 'char': "'", 'input': "'", ~
'filetype': ['lisp', 'scheme'], ~
'syntax': ['Comment', 'Constant']} ~
(a) 3
(b) 100 / 2 = 50
(c) 100 / 2 = 50
(d) 100 / 1 = 100
Priority = 3 + 50 + 50 + 100 = 203
------------------------------------------------------------------------------
RULE SELECTION ALGORITHM *smartinput-rule-selection-algorithm*
Whenever you type a key in Insert mode or Command-line mode, the plugin
selects the best rule for the current context with the following steps:
(1) Select available rules which satisfy all the following conditions:
(c) "char" value is equal to the typed key.
(a) The text under the cursor is matched to "at" pattern.
(f) The 'filetype' of the current buffer is contained in "filetype" list,
or "filetype" list is omitted,
or the current mode is not Insert mode.
(s) The character at the cursor is highlighted as one of "syntax" names,
or "syntax" names are omitted,
or the current mode is not Insert mode.
(m) The current mode matches to "mode" value.
(2) If two or more rules are selected at the step (1),
select the rule with the highest |smartinput-rule-priority|.
(3) If two or more rules are selected at the step (2),
select the rule with the greatest "at".
Note that this step is just to avoid unstable selection.
------------------------------------------------------------------------------
RULE CONVENTIONS *smartinput-rule-conventions*
Like |i_<BS>| and |i_CTRL-H|, there are several keys which does the same
effects. If you want to define rules for such keys,
* Use the most descriptive keys for "char"s of rules.
* Map other keys to trigger rules for the descriptive keys.
For example, if you want to define rules on both |i_<BS>| and |i_CTRL-H|,
* Define rules only for <BS>.
* Map <C-h> to trigger rules for <BS>, with the following command:
>
smartinput#map_to_trigger('<C-h>', '<BS>', '<C-h>')
<
This convention is to avoid rule duplication. See also the following table:
"char" keys | Mapped keys ~
------------+------------------------------- ~
<BS> | <C-h>
<Enter> | <C-m> <C-j> <CR> <NL> <Return>
<Esc> | <C-[>
<Tab> | <C-i>
==============================================================================
CUSTOMIZATION *smartinput-customization*
(A) To define your own |smartpunk-rules| for the smart input assistant,
use |smartinput#define_rule()|. See also |smartinput-default-rules| for
the real world examples.
(B) If you define your own |smartinput-rules| which "char" values are
different from ones of |smartinput-default-rules|, you have to define
trigger keys for your own rules with |smartinput#map_to_trigger()|.
==============================================================================
BUGS *smartinput-bugs*
- Currently there is no known issue.
- If you find any problem or have any suggestion, please give your feedback to
the author by: https://github.com/kana/vim-smartinput/issues
==============================================================================
CHANGELOG *smartinput-changelog*
0.0.0 2012-02-09T22:35:02+09:00 *smartinput-changelog-0.0.0*
- Initial version.
==============================================================================
vim:tw=78:ts=8:ft=help:norl:fen:fdl=0:fdm=marker: