Skip to content
This repository
Browse code

First commit.

  • Loading branch information...
commit 1eb3c5c953cfba1c686303043ae95dec482da1db 0 parents
Israel Chauca Fuentes authored

Showing 1 changed file with 210 additions and 0 deletions. Show diff stats Hide diff stats

  1. +210 0 delimater.vim
210 delimater.vim
... ... @@ -0,0 +1,210 @@
  1 +" DELIMITERS MADE LESS ANNOYING:{{{1
  2 +"
  3 +" Main novelty here (if it is one): this does NOT try to be helpful by
  4 +" inserting the closing delimiter for you when you type an opening one.
  5 +" Instead it only tries to be smart about what to do when you type a closing
  6 +" delimiter yourself.
  7 +"
  8 +" If you just typed an empty delimiter pair, it'll move the cursor back
  9 +" inside. If you type a closing delimiter the cursor is already on (or to
  10 +" the left of, if the cursor is on a space), it'll skip the cursor past that
  11 +" delimiter without inserting it.
  12 +"
  13 +" That way you never end with superfluous delimiters to delete, which deletion
  14 +" itself can be tricky to perform, since the editor might be trying to be
  15 +" helpful about deletions as well. Instead, you only ever get delimiters you
  16 +" explicitly typed yourself.
  17 +"
  18 +" I had trained myself into the good habit of typing pairs together anyway.
  19 +" The only annoying part of that habit is the manual cursor placement work;
  20 +" but that work is quite predictable almost all of the time. That's exactly
  21 +" the sort of work that computers are for.
  22 +
  23 +" Init:{{{1
  24 +if exists("loaded_annoying_delimiters")
  25 + " finish
  26 +endif
  27 +if v:version < 700
  28 + echoerr "AnnoyingDelimiters: this plugin requires vim >= 7!"
  29 + finish
  30 +endif
  31 +let loaded_annoying_delimiters = 1
  32 +
  33 +if !exists("g:annoyDelims_delims_list")
  34 + let s:delims = split (") } ] ' \" `")
  35 +else
  36 + let s:delims = g:annoyDelims_delims_list
  37 +endif
  38 +
  39 +if !exists("g:annoyDelims_autocomplete")
  40 + let s:autocomplete = 1
  41 +else
  42 + let s:autocomplete = g:annoyDelims_autocomplete
  43 +endif
  44 +
  45 +let s:paired_delims = split( &matchpairs, ',' )
  46 +let s:quote_delims = split("\" ' ` ´")
  47 +"let s:left_delims = []
  48 +"let s:right_delims = []
  49 +
  50 +"for pair in s:paired_delims
  51 +"let pairl = split(pair,":")
  52 +"let s:left_delims = s:left_delims + pairl[0]
  53 +"let s:right_delims = s:right_delims + pairl[1]
  54 +"endfor
  55 +
  56 +let s:left_delims = split(&matchpairs, ':.,\=')
  57 +let s:right_delims = split(&matchpairs, ',\=.:')
  58 +
  59 +let s:leader = "q"
  60 +
  61 +" Functions:{{{1
  62 +function! IsEmptyPair(str)
  63 + for pair in split( &matchpairs, ',' ) + [ "''", '""', '``' ]
  64 + if a:str == join( split( pair, ':' ),'' )
  65 + return 1
  66 + endif
  67 + endfor
  68 + return 0
  69 +endfunc
  70 +
  71 +function! WithinEmptyPair()
  72 + let cur = strpart( getline('.'), col('.')-2, 2 )
  73 + return IsEmptyPair( cur )
  74 +endfunc
  75 +
  76 +function! SkipDelim(char)
  77 + let cur = strpart( getline('.'), col('.')-2, 3 )
  78 + if cur[0] == "\\"
  79 + return a:char
  80 + elseif cur[1] == a:char
  81 + return "\<Right>"
  82 + elseif cur[1] == ' ' && cur[2] == a:char
  83 + return "\<Right>\<Right>"
  84 + elseif IsEmptyPair( cur[0] . a:char )
  85 + return a:char . "\<Left>"
  86 + else
  87 + return a:char
  88 + endif
  89 +endfunc
  90 +
  91 +function! QuoteDelim(char)
  92 + let line = getline('.')
  93 + let col = col('.')
  94 + if line[col - 2] == "\\"
  95 + "Inserting a quoted quotation mark into the string
  96 + return a:char
  97 + elseif line[col - 1] == a:char
  98 + "Escaping out of the string
  99 + return "\<Right>"
  100 + else
  101 + "Starting a string
  102 + return a:char.a:char."\<Left>"
  103 + endif
  104 +endf
  105 +
  106 +function! ClosePair(char)
  107 + if getline('.')[col('.') - 1] == a:char
  108 + return "\<Right>"
  109 + else
  110 + return a:char
  111 + endif
  112 +endf
  113 +
  114 +function! ResetMappings()
  115 + for delim in s:right_delims + s:left_delims + s:quote_delims
  116 + silent! exec 'iunmap ' . delim
  117 + silent! exec 'vunmap ' . s:leader . delim
  118 + endfor
  119 +endfunction
  120 +
  121 +" Mappings:{{{1
  122 +
  123 +call ResetMappings()
  124 +if s:autocomplete == 0
  125 + " Don't auto-complete:{{{2
  126 + let test_string = "Don't"
  127 + "inoremap <expr> ) SkipDelim(')')
  128 + for delim in s:right_delims + s:quote_delims
  129 + exec 'imap <expr> ' . delim . ' SkipDelim("\' . delim . '")'
  130 + endfor
  131 +
  132 + " Wrap the selection with delimiters:
  133 + "vmap <expr> q( visualmode() == "<C-V>" ? "I(\<Esc>" : "s(\<C-R>\")\<Esc>"
  134 + let s:i = 0
  135 + while s:i < len(s:paired_delims)
  136 + exec 'vmap <expr> ' . s:leader . s:left_delims[s:i] .
  137 + \' visualmode() == "<C-V>" ? "I' . s:left_delims[s:i] .
  138 + \'\<Esc>" : "s' . s:left_delims[s:i] . '\<C-R>\"' .
  139 + \s:right_delims[s:i] . '\<Esc>"'
  140 + exec 'vmap <expr> ' . s:leader . s:right_delims[s:i] .
  141 + \' visualmode() == "<C-V>" ? "I' . s:left_delims[s:i] .
  142 + \'\<Esc>" : "s' . s:left_delims[s:i] . '\<C-R>\"' .
  143 + \s:right_delims[s:i] . '\<Esc>"'
  144 + let s:i = s:i + 1
  145 + endwhile
  146 +
  147 + for quote in s:quote_delims
  148 + exec 'vmap <expr> ' . s:leader . quote .
  149 + \' visualmode() == "<C-V>" ? "I' . quote .
  150 + \'\<Esc>" : "s' . quote . '\<C-R>\"' . quote . '\<Esc>"'
  151 + endfor
  152 +
  153 +else
  154 + " Do auto-complete:{{{2
  155 + let test_string = "Do"
  156 + let s:i = 0
  157 + while s:i < len(s:paired_delims)
  158 + exec 'imap ' . s:left_delims[s:i] . ' ' . s:left_delims[s:i] . s:right_delims[s:i] . '<Left>'
  159 + let s:i = s:i + 1
  160 + endwhile
  161 + "imap ( ()<Left>
  162 + "imap [ []<Left>
  163 + "imap { {}<Left>
  164 + "autocmd Syntax html,vim imap < <lt>><Left>
  165 + "let test_list = []
  166 + let s:i = 0
  167 + for delim in s:quote_delims
  168 + exec 'imap ' . delim . ' <c-r>=QuoteDelim("\' . delim . '")<CR>'
  169 + endfor
  170 + for delim in s:right_delims
  171 + exec 'imap ' . delim . ' <c-r>=ClosePair("\' . delim . '")<CR>'
  172 + endfor
  173 + "imap " <c-r>=QuoteDelim('"')<CR>
  174 + "imap ' <c-r>=QuoteDelim("'")<CR>
  175 + "imap ) <c-r>=ClosePair(')')<CR>
  176 + "imap ] <c-r>=ClosePair(']')<CR>
  177 + "imap } <c-r>=ClosePair('}')<CR>
  178 +
  179 + " Wrap the selection with delimiters:
  180 + "vmap <expr> q( visualmode() == "<C-V>" ? "I(\<Esc>" : "s()\<C-R>\"\<Esc>"
  181 + let s:i = 0
  182 + while s:i < len(s:paired_delims)
  183 + exec 'vmap <expr> ' . s:leader . s:left_delims[s:i] . ' visualmode() == "<C-V>" ? "I' . s:left_delims[s:i] . '\<Esc>" : "s' . s:left_delims[s:i] . s:right_delims[s:i] . '\<C-R>\"\<Esc>"'
  184 + exec 'vmap <expr> ' . s:leader . s:right_delims[s:i] . ' visualmode() == "<C-V>" ? "I' . s:left_delims[s:i] . '\<Esc>" : "s' . s:left_delims[s:i] . s:right_delims[s:i] . '\<C-R>\"\<Esc>"'
  185 + let s:i = s:i + 1
  186 + endwhile
  187 +
  188 + for quote in s:quote_delims
  189 + exec 'vmap <expr> ' . s:leader . quote .
  190 + \' visualmode() == "<C-V>" ? "I' . quote .
  191 + \'\<Esc>" : "s' . quote . '\<C-R>\"' . quote . '\<Esc>"'
  192 + endfor
  193 + "vmap ( <ESC>`>a)<ESC>`<i(<ESC>
  194 + "vmap ) <ESC>`>a)<ESC>`<i(<ESC>
  195 + "vmap { <ESC>`>a}<ESC>`<i{<ESC>
  196 + "vmap } <ESC>`>a}<ESC>`<i{<ESC>
  197 + "vmap " <ESC>`>a"<ESC>`<i"<ESC>
  198 + "vmap ' <ESC>`>a'<ESC>`<i'<ESC>
  199 + "vmap ` <ESC>`>a`<ESC>`<i`<ESC>
  200 + "vmap [ <ESC>`>a]<ESC>`<i[<ESC>
  201 + "vmap ] <ESC>`>a]<ESC>`<i[<ESC>
  202 +
  203 +
  204 +endif
  205 +" Expansions:{{{2
  206 +imap <expr> <BS> WithinEmptyPair() ? "\<Right>\<BS>\<BS>" : "\<BS>"
  207 +imap <expr> <CR> WithinEmptyPair() ? "\<CR>\<CR>\<Up>" : "\<CR>"
  208 +imap <expr> <Space> WithinEmptyPair() ? "\<Space>\<Space>\<Left>" : "\<Space>"
  209 +
  210 +" vim:foldmethod=marker:foldcolumn=2

0 comments on commit 1eb3c5c

Please sign in to comment.
Something went wrong with that request. Please try again.