├── LICENSE ├── README.md └── smartinput.plugin.zsh /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 momo-lab 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # zsh-smartinput 2 | This is a zsh plugin to provided smart input. 3 | When brackets/quotes are inputted, the corresponding brackets/quotes are automatically inputted. 4 | Support character is ` () `, ` [] `, ` {} `, ` " `, ` ' ` and `` ` ``. 5 | 6 | This plugin consulted [vim-smartinput](http://github.com/kana/vim-smartinput). 7 | 8 | # Installation 9 | ### Using [zplug](https://github.com/b4b4r07/zplug) 10 | 11 | ```zsh 12 | zplug "momo-lab/zsh-smartinput" 13 | ``` 14 | 15 | ### Manually 16 | 17 | Clone this repository somewhere (`~/.zsh-smartinput` for example) and source it in your `.zshrc` 18 | 19 | ```zsh 20 | git clone https://github.com/momo-lab/zsh-smartinput.git 21 | ``` 22 | 23 | ```zsh 24 | source ~/.zsh-smartinput/smartinput.plugin.zsh 25 | ``` 26 | 27 | # For Example 28 | 29 | This plugin provides the following rules 30 | (note that "#" indicates the cursor position in the following examples): 31 | 32 | Automatically complements the corresponding brackets/quotes: 33 | 34 | | Before | Input | After | 35 | |--------|--------|--------| 36 | | # | ( | (#) | 37 | | # | [ | [#] | 38 | | # | { | {#} | 39 | | # | " | "#" | 40 | | # | ' | '#' | 41 | | # | \` | \`#\` | 42 | 43 | Input right brackets/quotes to leave the block: 44 | 45 | | Before | Input | After | 46 | |--------|--------|--------| 47 | | (#) | ) | ()# | 48 | | (foo#) | ) | (foo)# | 49 | 50 | Input the backspace key to remove the corresponding brackets/quotes: 51 | 52 | | Before | Input | After | 53 | |--------|--------|--------| 54 | | (#) | \ | # | 55 | | ()# | \ | # | 56 | 57 | Care to escaping characters: 58 | 59 | | Before | Input | After | 60 | |--------|--------|--------| 61 | | \\# | ( | \\(# | 62 | 63 | Care to English words: 64 | 65 | | Before | Input | After | 66 | |--------|--------|--------| 67 | | foo# | 's | foo's# | 68 | -------------------------------------------------------------------------------- /smartinput.plugin.zsh: -------------------------------------------------------------------------------- 1 | declare -g __smartinput_rules=( 2 | "'" 3 | "\"" 4 | '`' 5 | '()' 6 | '[]' 7 | '{}' 8 | ) 9 | 10 | function __smartinput:widget:input_left_bracket { 11 | if [[ "$LBUFFER[-1]" == '\' ]]; then 12 | # Support Escape "\" 13 | : 14 | else 15 | local rule 16 | for rule in $__smartinput_rules; do 17 | if [[ $#rule == 2 && "$rule[1]" == "$KEYS" ]]; then 18 | RBUFFER="$rule[2]$RBUFFER" 19 | break 20 | fi 21 | done 22 | fi 23 | zle self-insert 24 | } 25 | 26 | function __smartinput:widget:input_right_bracket { 27 | local left right 28 | if [[ "$RBUFFER[1]" == "$KEYS" ]]; then 29 | # [xxxxxx|] 30 | zle forward-char 31 | return 32 | fi 33 | zle self-insert 34 | } 35 | 36 | function __smartinput:widget:input_quote { 37 | if [[ "$LBUFFER[-1]" == '\' ]]; then 38 | # Support Escape "\" 39 | : 40 | elif [[ "$RBUFFER[1]" == "$KEYS" ]]; then 41 | # 'xxxxxx|' 42 | zle forward-char 43 | return 44 | elif [[ "$KEYS" == "'" && "'$LBUFFER[-1]" =~ '\w' ]]; then 45 | # Support English "someone's" 46 | : 47 | else 48 | RBUFFER="$KEYS$RBUFFER" 49 | fi 50 | zle self-insert 51 | } 52 | 53 | function __smartinput:widget:backward_delete_char { 54 | local rule left right 55 | for rule in $__smartinput_rules; do 56 | if [[ $#rule == 1 ]]; then 57 | left="$rule" 58 | right="$rule" 59 | elif [[ $#rule == 2 ]]; then 60 | left="$rule[1]" 61 | right="$rule[2]" 62 | else 63 | # Not Support rule 64 | continue 65 | fi 66 | 67 | # [|] 68 | if [[ "$LBUFFER[-1]" == "$left" && "$RBUFFER[1]" == "$right" ]]; then 69 | zle backward-delete-char 70 | zle delete-char 71 | return 72 | fi 73 | 74 | # []| 75 | if [[ "$LBUFFER[-2,-1]" == "$left$right" ]]; then 76 | zle backward-delete-char 77 | zle backward-delete-char 78 | return 79 | fi 80 | done 81 | 82 | zle backward-delete-char 83 | } 84 | 85 | () { 86 | zle -N __smartinput:widget:input_quote 87 | zle -N __smartinput:widget:input_left_bracket 88 | zle -N __smartinput:widget:input_right_bracket 89 | local rule 90 | for rule in $__smartinput_rules; do 91 | if [[ $#rule == 1 ]]; then 92 | # quote 93 | bindkey "$rule" __smartinput:widget:input_quote 94 | elif [[ $#rule == 2 ]]; then 95 | # brackets 96 | bindkey "$rule[1]" __smartinput:widget:input_left_bracket 97 | bindkey "$rule[2]" __smartinput:widget:input_right_bracket 98 | fi 99 | done 100 | 101 | zle -N __smartinput:widget:backward_delete_char 102 | bindkey "^H" __smartinput:widget:backward_delete_char 103 | bindkey "^?" __smartinput:widget:backward_delete_char 104 | } 105 | --------------------------------------------------------------------------------