aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/tlib_vim/autoload/tlib/qfl.vim
blob: 148620a5c7fc6982520ef291b1d15e187b650c86 (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
" @Author:      Tom Link (mailto:micathom AT gmail com?subject=[vim])
" @Website:     https://github.com/tomtom
" @License:     GPL (see http://www.gnu.org/licenses/gpl.txt)
" @Last Change: 2015-12-06
" @Revision:    60

" :nodoc:
TLet g:tlib#qfl#world = {
            \ 'type': 'mi',
            \ 'query': 'Select entry',
            \ 'pick_last_item': 0,
            \ 'resize_vertical': 0,
            \ 'resize': 20,
            \ 'scratch': '__TLibQFL__',
            \ 'tlib_UseInputListScratch': 'call tlib#qfl#InitListBuffer(world)',
            \ 'key_handlers': [
                \ {'key':  5, 'agent': 'tlib#qfl#AgentWithSelected', 'key_name': '<c-e>', 'help': 'Run a command on selected lines'},
                \ {'key': 16, 'agent': 'tlib#qfl#AgentPreviewQFE',   'key_name': '<c-p>', 'help': 'Preview'},
                \ {'key': 60, 'agent': 'tlib#qfl#AgentGotoQFE',      'key_name': '<',     'help': 'Jump (don''t close the list)'},
                \ {'key': 19, 'agent': 'tlib#qfl#AgentSplitBuffer',  'key_name': '<c-s>', 'help': 'Show in split buffer'},
                \ {'key': 20, 'agent': 'tlib#qfl#AgentTabBuffer',    'key_name': '<c-t>', 'help': 'Show in tab'},
                \ {'key': 22, 'agent': 'tlib#qfl#AgentVSplitBuffer', 'key_name': '<c-v>', 'help': 'Show in vsplit buffer'},
                \ {'key': 12, 'agent': 'tlib#qfl#AgentEditLine',     'key_name': '<c-l>', 'help': 'Edit selected line(s)'},
                \ {'key': "\<c-insert>", 'agent': 'tlib#qfl#SetFollowCursor', 'key_name': '<c-ins>', 'help': 'Toggle trace cursor'},
            \ ],
            \ 'return_agent': 'tlib#qfl#AgentEditQFE',
            \ }


function! tlib#qfl#FormatQFLE(qfe) dict abort "{{{3
    let filename = tlib#qfl#QfeFilename(a:qfe)
    if get(self, 'qfl_short_filename', '')
        let filename = pathshorten(filename)
    endif
    return printf("%s|%d| %s", filename, a:qfe.lnum, get(a:qfe, "text"))
endf


function! tlib#qfl#QfeFilename(qfe) abort "{{{3
    let filename = get(a:qfe, 'filename')
    if empty(filename)
        let filename = bufname(get(a:qfe, 'bufnr'))
    endif
    return filename
endf


function! tlib#qfl#InitListBuffer(world) "{{{3
    let set_syntax = get(a:world, 'set_syntax', 'tlib#qfl#SetSyntax')
    call call(set_syntax, [], a:world)
    if has('balloon_eval')
        setlocal ballooneval balloonexpr=tlib#qfl#Balloon()
    endif
endf


function! tlib#qfl#SetSyntax() dict abort "{{{3
    let syntax = get(self, 'qfl_list_syntax', '')
    let nextgroup = get(self, 'qfl_list_syntax_nextgroup', '')
    " TLogVAR syntax, nextgroup
    if !empty(syntax)
        exec printf('runtime syntax/%s.vim', syntax)
    endif
    syn match TTagedFilesFilename /\%(\f\+\| \)\+\ze|\d\+| / nextgroup=TTagedFilesLNum
    if !empty(nextgroup)
        exec 'syn match TTagedFilesLNum /|\d\+|\s\+/ nextgroup='. nextgroup
    else
        syn match TTagedFilesLNum /|\d\+|/
    endif
    hi def link TTagedFilesFilename Directory
    hi def link TTagedFilesLNum LineNr
endf


function! tlib#qfl#Balloon() "{{{3
    let world = getbufvar(v:beval_bufnr, 'tlibDisplayListWorld')
    let current = max([1, world.offset]) + v:beval_lnum - 1
    if current > len(world.table)
        let current = len(world.table)
    endif
    let baseidx = world.GetBaseIdx0(current)
    " TLogVAR world.offset, v:beval_lnum, current, baseidx
    let item = world.data[baseidx]
    let bufnr = get(item, 'bufnr', 0)
    let bufname = get(item, 'filename', '')
    if bufnr == 0 && !empty(bufname)
        let bufnr = bufnr(bufname)
    endif
    if empty(bufname) && bufnr > 0
        let bufname = bufname(bufnr)
    endif
    " TLogVAR item
    if bufnr == 0
        return ''
    else
        let lines = [printf("%d#%d: %s", bufnr, item.lnum, bufname)]
        if has('balloon_multiline')
            let desc = {'nr': 'Error number', 'type': 'Error type', 'text': ''}
            for key in ['nr', 'type', 'text']
                if has_key(item, key) && !empty(item[key])
                    let keydesc = get(desc, key, key)
                    if empty(keydesc)
                        let text = item[key]
                    else
                        let text = printf("%s: %s", key, item[key])
                    endif
                    call add(lines, text)
                endif
            endfor
        endif
        return join(lines, "\n")
    endif
    " v:beval_bufnr	number of the buffer in which balloon is going to show
    " v:beval_winnr	number of the window
    " v:beval_lnum	line number
    " v:beval_col	column number (byte index)
    " v:beval_text	word under or after the mouse pointer
endf


function! tlib#qfl#AgentEditQFE(world, selected, ...) "{{{3
    TVarArg ['cmd_edit', ''], ['cmd_buffer', '']
    " TVarArg ['cmd_edit', 'edit'], ['cmd_buffer', 'buffer']
    " TLogVAR a:selected
    if empty(a:selected)
        call a:world.RestoreOrigin()
        " call a:world.ResetSelected()
    else
        call a:world.RestoreOrigin()
        for idx in a:selected
            let idx -= 1
            " TLogVAR idx
            if idx >= 0
                " TLogVAR a:world.data
                " call tlog#Debug(string(map(copy(a:world.data), 'v:val.bufnr')))
                " TLogVAR idx, a:world.data[idx]
                let qfe = a:world.data[idx]
                " let back = a:world.SwitchWindow('win')
                " TLogVAR cmd_edit, cmd_buffer, qfe
                let fn = tlib#qfl#QfeFilename(qfe)
                " TLogVAR cmd_edit, cmd_buffer, fn
                if empty(cmd_edit) && empty(cmd_buffer)
                    if tlib#file#Edit(fn)
                        call tlib#buffer#ViewLine(qfe.lnum)
                    endif
                else
                    call tlib#file#With(cmd_edit, cmd_buffer, [fn], a:world)
                    " TLogDBG bufname('%')
                    " TLogVAR &filetype
                    call tlib#buffer#ViewLine(qfe.lnum)
                    " call a:world.SetOrigin()
                    " exec back
                endif
            endif
        endfor
    endif
    return a:world
endf 


function! tlib#qfl#AgentPreviewQFE(world, selected) "{{{3
    " TLogVAR a:selected
    let back = a:world.SwitchWindow('win')
    call tlib#qfl#AgentEditQFE(a:world, a:selected[0:0])
    exec back
    redraw
    let a:world.state = 'redisplay'
    return a:world
endf


function! tlib#qfl#AgentGotoQFE(world, selected) "{{{3
    let world = a:world
    if !empty(a:selected)
        let world = tlib#agent#Suspend(world, a:selected)
        call tlib#qfl#AgentEditQFE(world, a:selected[0:0])
    endif
    return world
endf


function! tlib#qfl#AgentWithSelected(world, selected, ...) "{{{3
    let cmd = a:0 >= 1 ? a:1 : input('Ex command: ', '', 'command')
    let world = a:world
    if !empty(cmd)
        let world = tlib#qfl#RunCmdOnSelected(world, a:selected, cmd)
    else
        let world.state = 'redisplay'
    endif
    return world
endf


function! tlib#qfl#RunCmdOnSelected(world, selected, cmd, ...) "{{{3
    let close_scratch = a:0 >= 1 ? a:1 : 1
    if close_scratch
        call a:world.CloseScratch()
    endif
    " TLogVAR a:cmd
    for entry in a:selected
        " TLogVAR entry, a:world.GetBaseItem(entry)
        call tlib#qfl#AgentEditQFE(a:world, [entry])
        " TLogDBG bufname('%')
        exec a:cmd
        " let item = a:world.data[a:world.GetBaseIdx(entry - 1)]
        " <+TODO+>
        let item = a:world.data[entry - 1]
        " TLogVAR entry, item, getline('.')
        if has_key(a:world, 'GetBufferLines')
            let lines = a:world.GetBufferLines('.', '.')
        else
            let lines = getline('.', '.')
        endif
        let item['text'] = tlib#string#Strip(lines[0])
    endfor
    if has_key(a:world, 'AfterRunCmd')
        if bufnr('%') == a:world.bufnr
            call a:world.AfterRunCmd()
        else
            " <+TODO+> Run in other buffer
        endif
    endif
    " call s:FormatBase(a:world)
    call a:world.RestoreOrigin()
    let a:world.state = 'reset'
    return a:world
endf


function! tlib#qfl#AgentSplitBuffer(world, selected) "{{{3
    call a:world.CloseScratch()
    return tlib#qfl#AgentEditQFE(a:world, a:selected, 'split', 'sbuffer')
endf


function! tlib#qfl#AgentTabBuffer(world, selected) "{{{3
    call a:world.CloseScratch()
    return tlib#qfl#AgentEditQFE(a:world, a:selected, 'tabedit', 'tab sbuffer')
endf


function! tlib#qfl#AgentVSplitBuffer(world, selected) "{{{3
    call a:world.CloseScratch()
    return tlib#qfl#AgentEditQFE(a:world, a:selected, 'vertical split', 'vertical sbuffer')
endf


" function! tlib#qfl#AgentOpenBuffer(world, selected) "{{{3
" endf


function! tlib#qfl#AgentEditLine(world, selected) "{{{3
    call a:world.CloseScratch()
    let cmd = 'call tlib#qfl#EditLine(".")'
    return tlib#qfl#RunCmdOnSelected(a:world, a:selected, cmd)
    let a:world.state = 'reset'
    return a:world
endf


function! tlib#qfl#EditLine(lnum) "{{{3
    call inputsave()
    let line = input('', getline(a:lnum))
    call inputrestore()
    if !empty(line)
        call setline(line(a:lnum), line)
    endif
endf


function! tlib#qfl#SetFollowCursor(world, selected) "{{{3
    if empty(a:world.follow_cursor)
        let a:world.follow_cursor = 'tlib#qfl#AgentPreviewQFE'
    else
        let a:world.follow_cursor = ''
    endif
    let a:world.state = 'redisplay'
    return a:world
endf


function! tlib#qfl#QflList(list, ...) abort "{{{3
    TVarArg ['world_dict', {}], ['anyway', 0], ['suspended', 0]
    Tlibtrace 'tlib', world_dict, anyway, suspended
    " TLogVAR a:list, world_dict, anyway, suspended
    if !anyway && empty(a:list)
        return
    endif
    let world = copy(g:tlib#qfl#world)
    if !empty(world_dict)
        let world = tlib#eval#Extend(world, world_dict)
    endif
    " TLogVAR world
    let world = tlib#World#New(world)
    " echom "DBG world" string(sort(keys(world)))
    let world.data  = copy(a:list)
    if !has_key(world, 'format_data')
        let world.format_data = 'tlib#qfl#FormatQFLE'
    endif
    " TLogVAR world
    " TLogVAR world.data
    " call s:FormatBase(world)
    " TLogVAR world.base
    return tlib#input#ListW(world, suspended ? 'hibernate' : '')
endf


function! tlib#qfl#Browse(...) abort "{{{3
    let list = getqflist()
    return call(function('tlib#qfl#QflList'), [list] + a:000)
endf