Skip to content
This repository
tree: cea0c8167e
Fetching contributors…

Cannot retrieve contributors at this time

file 525 lines (395 sloc) 18.973 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 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 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
*smartinput.txt* Provide smart input assistant

Version 0.0.5
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 smart input assistant.
Whenever you write a text, especially source code, you have to input and deal
with pairs of punctuations such as (), [], {}, and so on. This plugin
provides various input assistants for such characters according to the current
context by default, and you can define your own rules to how the smart input
assistant behaves in Insert mode and Command-line mode.

For example, this plugin provides the following rules by default
(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> | #
   ()# | <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-customization| 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 expression
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.
See also |smartinput-special-keys|.

"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.
See also |smartinput-special-keys|.

"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 searching with |/|
? Command-line mode for searching 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.
See also |smartinput-check-syntax-highlighting|.

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.

*smartinput-special-keys*
Values of "char" and "input" are internally passed to |:map| commands. So
that:

- Special keys must be written like '<Left>', not "\<Left>".
- "|" must be written '<Bar>'.
- "\" must be written '<Bslash>'.

*smartinput-check-syntax-highlighting*
To investigate how the character under the cursor is highlighted, use the
following command:
>
  echo map(synstack(line('.'),col('.')),'synIDattr(synIDtrans(v:val),"name")')
<


------------------------------------------------------------------------------
DEFAULT RULES *smartinput-default-rules*

For convenience, there are many rules defined by default. And it's hard to
list and to describe each rule here, because the default rules will be often
updated.

See |smartinput-introduction| for a short digest of the default rules.

See the source code of |smartinput#define_default_rules()| for the details:
- If you read this document in Vim,
  open ../autoload/smartinput.vim (type |gf| at the file name).
- If you read this document with a Web browser, open the following:
  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*

*smartinput-customization-a*
(A) To define your own |smartinput-rules| for the smart input assistant,
    use |smartinput#define_rule()|. See also |smartinput-default-rules| for
    the real world examples.

*smartinput-customization-b*
(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()| or
    |smartinput#map_trigger_keys()|.

*smartinput-customization-c*
(C) Example #1: If you often write Ruby script and you want to insert "#{}"
    by typing "#" in a string literal to input string interpolation easily,
    add the following lines into your |vimrc|:
>
    call smartinput#map_to_trigger('i', '#', '#', '#')
    call smartinput#define_rule({
    \ 'at': '\%#',
    \ 'char': '#',
    \ 'input': '#{}<Left>',
    \ 'filetype': ['ruby'],
    \ 'syntax': ['Constant', 'Special'],
    \ })
<
*smartinput-customization-d*
(D) Example #2: If you often write Ruby script and you want to insert "||"
    by typing "|" to declare block-local variable easily,
    add the following lines into your |vimrc|:
>
    call smartinput#map_to_trigger('i', '<Bar>', '<Bar>', '<Bar>')
    call smartinput#define_rule({
    \ 'at': '\%#',
    \ 'char': '<Bar>',
    \ 'input': '<Bar><Bar><Left>',
    \ 'filetype': ['ruby'],
    \ })
<
    But the above rule is not so useful, because "||" is always inserted and
    the cursor is moved into the bars, even if you want to write the following
    idiom: "foo ||= bar". So that it's better to tweak the "at" value to
    activate the rule if and only if the cursor is just after "{" or "do":
>
    call smartinput#define_rule({
    \ 'at': '\({\|\<do\>\)\s*\%#',
    \ 'char': '<Bar>',
    \ 'input': '<Bar><Bar><Left>',
    \ 'filetype': ['ruby'],
    \ })
<



==============================================================================
BUGS *smartinput-bugs*

- Still this plugin is not mature, there might be problematic behavior and
  missing features. If you find any problem or have any suggestion, please
  give your feedback to the author via:
  https://github.com/kana/vim-smartinput/issues

*smartinput-disable-completion-temporarily*
- Though there are many default rules to support user input naturally, the
  smart input assistant is not perfect. It sometimes completes quotes etc
  where such completion is not necessary. In that case, use |i_CTRL-V| to
  input a character solely. For example, type "<C-v>(" to input only "(".

*smartinput-backspace-requirement*
- Several rules require that 'backspace' is set to "indent,eol,start" or 2.




==============================================================================
CHANGELOG *smartinput-changelog*

0.0.5 2012-04-11T21:48:41+09:00 *smartinput-changelog-0.0.5*
- Fix rules to leave from a (), [], or {} block. They did not work
properly with Visual-block Insert (|v_b_I|), etc. Thanks to Dennis
Sjögren for reporting the problem.
- Fix rules to expand a () or {} block. After expanding a block,
<Esc> had to be typed twice to return to Normal mode. Thanks to
Jonas Haag for reporting the problem.

0.0.4 2012-04-07T16:26:23+09:00 *smartinput-changelog-0.0.4*
- Add rules for Clojure. Thanks to Bodaniel Jeanes for the feedback.
- Add rules for Python-specific string literals such as r'...'.
Thanks to Adrian Sampson for the initial rules and the ideas.
- Add examples to write custom rules.
See |smartinput-customization-c| and |smartinput-customization-d|
for the details. Thanks to Guillaume for the feedback.

0.0.3 2012-04-04T21:33:34+09:00 *smartinput-changelog-0.0.3*
- Revise rules for expanding {} and () blocks to indent correctly for
some 'filetype's, especially for PHP. Thanks to Jon Duell for the
feedback on this problem.
- Add a note on how to disable completion temporarily.
See |smartinput-disable-completion-temporarily| for the details.
- Add a note on 'backspace'. See |smartinput-backspace-requirement|.

0.0.2 2012-04-01T16:11:14+09:00 *smartinput-changelog-0.0.2*
- Revise the default rules to easily leave from multiline {}, [] and
() blocks. Thanks to Curtis McEnroe for the suggestion.
- Revise the default rules for escaped ' and rules to leave from a ''
block. The former is used by default, and the latter is used only
for specific languages which \ doesn't have special meanings in ''
blocks, for example, Perl, Ruby, shell script and Vim script.
Thanks to Andrew Ray for the suggestion.

0.0.1 2012-03-30T19:38:53+09:00 *smartinput-changelog-0.0.1*
- Fix side effects in the default configuration. <C-h> in
Command-line mode did not update the screen immediately.
- Add the default rules to input triple quotes such as '''...''',
"""...""" and ```...```, for Python, Markdown, etc.
- Add the default rules to easily comment to edit Vim script. Because
|:comment| in Vim script starts with " while string literals are
quoted with ". Now " is completed only if the current context is to
input string literals.

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:
Something went wrong with that request. Please try again.