summaryrefslogtreecommitdiff
path: root/.vim/plugin/tEchoPair.vim
blob: b1f3c19b21c7418f979509ccb0f518a123fbdff5 (plain)
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
" tEchoPair.vim
" @Author:      Thomas Link (mailto:samul AT web de?subject=vim-tEchoPair)
" @Website:     http://www.vim.org/account/profile.php?user_id=4037
" @License:     GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Created:     2007-03-24.
" @Last Change: 2007-03-27.
" @Revision:    0.1.175

if &cp || exists("loaded_techopair")
    finish
endif
let loaded_techopair = 1

" if !exists('g:tEchoPairStyle') | let g:tEchoPairStyle = 'inner' | endif
if !exists('g:tEchoPairStyle')   | let g:tEchoPairStyle = 'indicate' | endif
if !exists('g:tEchoPairInstall') | let g:tEchoPairInstall = [] | endif

if !exists('g:tEchoPairIndicateOpen')   | let g:tEchoPairIndicateOpen = ' <<<&'       | endif
if !exists('g:tEchoPairIndicateClose')  | let g:tEchoPairIndicateClose = '&>>> '      | endif
if !exists('g:tEchoPairIndicateCursor') | let g:tEchoPairIndicateCursor = ' <<<&>>> ' | endif

if !exists('g:tEchoPairs')
                " \ 'viki': [
                "     \ 'fold;-1',
                " \ ], 
    let g:tEchoPairs = {
                \ 'ruby': [
                    \ ['(', ')'],
                    \ ['{', '}'],
                    \ ['[', ']'],
                    \ ['\<\(module\|class\|def\|begin\|do\|if\|unless\)\>', '', '\<end\>', 'TEchoSkipRuby()'],
                \ ],
                \ 'vim': [
                    \ ['(', ')'],
                    \ ['{', '}'],
                    \ ['[', ']'],
                    \ ['\<for\>', '\<endfor\?\>'],
                    \ ['\<wh\(i\(l\(e\)\?\)\?\)\?\>', '\<endw\(h\(i\(l\(e\)\?\)\?\)\?\)\?\>'],
                    \ ['\<if\>', '\<end\(i\(f\)\?\)\?\>'],
                    \ ['\<try\>', '\<endt\(r\(y\)\?\)\?\>'],
                    \ ['\<fu\(n\(c\(tion\)\?\)\?\)\?\>', '\<endf\(u\(n\(c\(tion\)\?\)\?\)\?\)\?\>'],
                \ ]
            \ }
endif

if !exists('g:tEchoPairStyle_inner')
    let g:tEchoPairStyle_inner = ['lisp', 'scheme']
endif

if !exists('g:tEchoPairStyle_indicate')
    let g:tEchoPairStyle_indicate = []
endif

if !exists('g:tEchoPairStyles')
endif

let s:tEchoPairStyles = []

fun! TEchoCollectStyles()
    redir => vars
    silent let
    redir END
    let s:tEchoPairStyles = split(vars, '\n')
    call filter(s:tEchoPairStyles, 'v:val =~ ''^tEchoPairStyle_''')
    call map(s:tEchoPairStyles, 'matchstr(v:val, ''^tEchoPairStyle_\zs\S\+'')')
endf

" fun! TEchoPair(backwards, type, ?what, ?[args])
fun! TEchoPair(backwards, type, ...)
    let lz = &lazyredraw
    set lazyredraw
    try
        let w0 = line('w0')
        let l0 = line('.')
        let c0 = col('.')
        " let c0 = col['.']
        let pos = getpos('.')
        let what = a:0 >= 1 ? a:1 : '.'
        if a:type == 'rx'
            let args0 = a:0 >= 2 ? a:2 : []
            let args = []
            " call add(args, '\V'. escape(get(args0, 0, '('), '\'))
            call add(args, '\V'. get(args0, 0, '('))
            " let m = escape(get(args0, 1, ''), '\')
            let m = get(args0, 1, '')
            call add(args, empty(m) ? m : '\V'.m)
            " call add(args, '\V'. escape(get(args0, 2, ')'), '\'))
            call add(args, '\V'. get(args0, 2, ')'))
            call add(args, a:backwards ? 'bW' : 'W')
            let args += args0[3:-1]
            echom "DBG searchpairs: ". string(args)
            " echom "DBG TEchoPair: ". string(args)
            let what = a:backwards ? args[0] : args[2]
            let this = a:backwards ? args[2] : args[0]
            " echom 'DBG '. what .' '. a:backwards .' '. expand('<cword>') .' '. (expand('<cword>')=~ '\V\^'. what .'\$')
            if a:backwards
                if expand('<cword>') =~ '\V\^'. this .'\$'
                    call search('\V'. this, 'cb', l0)
                endif
            else
                if expand('<cword>') =~ '\V\^'. this .'\$'
                    call search('\V'. this, 'c', l0)
                endif
            endif
            call call('searchpair', args)
        elseif a:type =~ '^fold'
            let fold_args = split(a:type, ';')
            if a:backwards
                call s:Normal('[z', 'silent!')
            else
                call s:Normal(']z', 'silent!')
            endif
            let lineshift = get(fold_args, a:backwards ? 1 : 2, 0)
            if lineshift > 0
                call s:Normal(lineshift .'j', 'silent!')
            elseif lineshift < 0
                call s:Normal((-lineshift) .'k', 'silent!')
            endif
        endif
        " else
        "     if a:backwards
        "         exec 'norm! ['. a:what
        "     else
        "         exec 'norm! ]'. a:what
        "     endif
        " endif
        let l1 = line('.')
        let c1 = col('.')
        if l1 != l0
            if a:backwards
                let c0 = col('$')
            else
                " let c0 = matchend(getline(l1), '^\s*') - 1
                let c0 = matchend(getline(l1), '^\s*')
            endif
        endif
        let text  = getline(l1)
        if empty(s:tEchoPairStyles)
            call TEchoCollectStyles()
        endif
        if exists('b:tEchoPairStyle')
            let style = b:tEchoPairStyle
        else
            let style = g:tEchoPairStyle
            for s in s:tEchoPairStyles
                if index(g:tEchoPairStyle_{s}, &filetype) != -1
                    let style = s
                endif
            endfor
        endif
        " if l1 < l0 || c1 < c0
        if a:backwards
            let text = TEchoPair_open_{style}(what, text, c0, l0, c1, l1)
        else
            let text = TEchoPair_close_{style}(what, text, c0, l0, c1, l1)
        endif
        if &debug != ''
            echo text . ' '. a:backwards .':'. c0.'x'.l0.'-'.c1.'x'.l1
        else
            echo text
        endif
        let b:tEchoPair = text
        call s:Normal(w0 .'zt')
        call setpos('.', pos)
        " return a:what
    finally
        let &lz = lz
    endtry
endf

fun! s:Normal(cmd, ...)
    let p = a:0 >= 1 ? a:1 : ''
    let m = mode()
    if m ==? 's' || m == ''
        exec p .' norm! '. a:cmd
    else
        exec p .'norm! '. a:cmd
    endif
endf

fun! TEchoPair_open_inner(what, text, c0, l0, c1, l1)
    return strpart(a:text, a:c1 - 1, a:c0 - a:c1)
endf

fun! TEchoPair_close_inner(what, text, c0, l0, c1, l1)
    return strpart(a:text, a:c0, a:c1 - a:c0)
endf

fun! TEchoPair_open_indicate(what, text, c0, l0, c1, l1)
    let text = s:IndicateCursor(a:text, a:c0, a:l0, a:c1, a:l1)
    " let text = a:l1.': '. substitute(text, '\V\%'. a:c1 .'c'. a:what, ' <<<&<<< ', '')
    let text = a:l1.': '. substitute(text, '\V\%'. a:c1 .'c'. a:what, g:tEchoPairIndicateOpen, '')
    let cmdh = s:GetCmdHeight()
    if cmdh > 1
        let acc = [text]
        for i in range(a:l1 + 1, a:l1 + cmdh - 1)
            if i > a:l0
                break
            endif
            " call add(acc, i.': '. s:IndicateCursor(getline(i), a:c0, a:l0, a:c1, i))
            call add(acc, i.': '. getline(i))
        endfor
        let text = join(acc, "\<c-j>")
    endif
    return text
endf

fun! TEchoPair_close_indicate(what, text, c0, l0, c1, l1)
    " let text = substitute(a:text, '\V\%'. a:c1 .'c'. a:what, ' >>>&>>> ', '')
    let text = substitute(a:text, '\V\%'. a:c1 .'c'. a:what, g:tEchoPairIndicateClose, '')
    let text = a:l1.': '. s:IndicateCursor(text, a:c0, a:l0, a:c1, a:l1)
    let cmdh = s:GetCmdHeight()
    if cmdh > 1
        let acc = [text]
        for i in range(a:l1 - 1, a:l1 - cmdh + 1, -1)
            if i < a:l0
                break
            endif
            " call insert(acc, i.': '. s:IndicateCursor(getline(i), a:c0, a:l0, a:c1, i))
            call insert(acc, i.': '. getline(i))
        endfor
        let text = join(acc, "\<c-j>")
    endif
    return text
endf

fun! s:IndicateCursor(text, c0, l0, c1, l1)
    if a:l0 == a:l1
        return substitute(a:text, '\%'. a:c0 .'c.', g:tEchoPairIndicateCursor, '')
    else
        return a:text
    endif
endf

fun! s:GetCmdHeight()
    let ch = &cmdheight
    if mode() == 'i' && &showmode
        let ch -= 1
    endif
    return ch
endf

fun! TEchoSkipRuby()
    let n = synIDattr(synID(line('.'), col('.'), 1), 'name')
    return (n =~ '^ruby\(Comment\|String\)$')
endf

fun! TEchoPairReset()
    augroup TEchoPair
        au!
    augroup END
endf
" call TEchoPairReset()

fun! TEchoPairInstall(pattern, ...)
    augroup TEchoPair
        if a:0 >= 1
            let list = a:1
        elseif has_key(g:tEchoPairs, &filetype)
            let list = g:tEchoPairs[&filetype]
        else
            " [a b]
            " [['(', ')'], ['{', '}']]
            let list = split(&matchpairs, ',')
            call map(list, 'split(v:val, ":")')
        endif
        for i in list
            if type(i) == 1
                if i =~ '^fold'
                    exec 'au CursorMoved '. a:pattern .' if foldclosed(line(".")) == -1 '.
                                \ ' | keepjumps call TEchoPair(1, '. string(i) .') | endif'
                endif
            elseif type(i) == 3
                if len(i) == 2
                    let [io, ie] = i
                    call insert(i, '', 1)
                    let im = ''
                else
                    let [io, im, ie; rest] = i
                endif
                if len(io) == 1
                    let condo = 'getline(".")[col(".") - 1] == '. string(io)
                else
                    " let condo = 'strpart(getline("."), col(".") - 1) =~ ''\V\^'''. string(io)
                    let condo = 'expand("<cword>") =~ ''\V\^'. io .'\$'''
                endif
                if len(ie) == 1
                    let conde = 'getline(".")[col(".") - 1] == '. string(ie)
                else
                    " let conde = 'strpart(getline("."), col(".") - 1) =~ ''\V\^'''. string(io)
                    let conde = 'expand("<cword>") =~ ''\V\^'. ie .'\$'''
                endif
                exec 'au CursorMoved '. a:pattern .' if '. condo . 
                            \ ' | keepjumps call TEchoPair(0, "rx", '. string(ie) .', '. string(i) .') | endif'
                exec 'au CursorMoved '. a:pattern .' if '. conde . 
                            \ ' | keepjumps call TEchoPair(1, "rx", '. string(io) .', '. string(i) .') | endif'
                exec 'au CursorMovedI '. a:pattern .' if '. condo . 
                            \ ' | keepjumps call TEchoPair(0, "rx", '. string(ie) .', '. string(i) .') | endif'
                exec 'au CursorMovedI '. a:pattern .' if '. conde . 
                            \ ' | keepjumps call TEchoPair(1, "rx", '. string(io) .', '. string(i) .') | endif'
            endif
        endfor
    augroup END
endf

" command! TEchoPairInstallGlobal call TEchoPairInstall('*')
command! TEchoPairInstallBuffer call TEchoPairInstall(expand('%:p'))

for ft in g:tEchoPairInstall
    exec 'au Filetype '. ft .' TEchoPairInstallBuffer'
endfor


finish
____________________________________________________________________

tEchoPair.vim -- Display matching parenthesis in the echo area


VIM is an excellent editor but in comparison too e.g. emacs it lacks a 
minor feature that makes editing lisp code somewhat cumbersome. While 
VIM can highlight the matching parenthesis, this doesn't help much with 
long functions when the matching parenthesis is off the screen. 
Emacs users are better off in such a situation, since emace displays the 
matching line in the echo area.

This plugin tries to mimic Emacs's behaviour.


In order to enable this plugin, you choose between the following  
options:
    TEchoPairInstallBuffer ... enable for the current buffer

    call TEchoPairInstall('*') ... enable globally

    let g:tEchoPairInstall = ['lisp', 'scheme'] ... enable for certain 
      filetypes


Currently, there are the following display modes:
    indicate ... Display the whole line and highlight the matching 
      parenthesis. If 'cmdheight' is greater than 1, additional lines 
      are display.

    inner ... Display the inner text Emacs-style.

In order to see the matching parenthesis when 'showmode' is on, set 
'cmdheight' to something greater than 1.

You can select the preferred display mode on a filetype basis, by 
setting g:tEchoPairStyle_{STYLE}.

Example:
    let g:tEchoPairStyle_inner = ['lisp', 'scheme']
    let g:tEchoPairStyle_indicate = ['java']


The pairs are usually deduced from the value of 'matchpairs' unless 
there is an entry for the current buffer's filetype in g:tEchoPairs. For 
the following filetypes custom pairs are pre-defined:
    - ruby
    - vim


CHANGES

0.1:
Initial release