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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
" scriptease.vim - An amalgamation of crap for editing runtime files
" Maintainer:   Tim Pope <http://tpo.pe/>
" Version:      1.0

if exists('g:loaded_scriptease') || &cp
  finish
endif
let g:loaded_scriptease = 1

" Utility {{{1

function! s:function(name) abort
  return function(substitute(a:name,'^s:',matchstr(expand('<sfile>'), '.*\zs<SNR>\d\+_'),''))
endfunction

function! s:sub(str,pat,rep) abort
  return substitute(a:str,'\v\C'.a:pat,a:rep,'')
endfunction

function! s:gsub(str,pat,rep) abort
  return substitute(a:str,'\v\C'.a:pat,a:rep,'g')
endfunction

function! s:shellesc(arg) abort
  if a:arg =~ '^[A-Za-z0-9_/.-]\+$'
    return a:arg
  elseif &shell =~# 'cmd' && a:arg !~# '"'
    return '"'.a:arg.'"'
  else
    return shellescape(a:arg)
  endif
endfunction

function! s:fnameescape(file) abort
  if exists('*fnameescape')
    return fnameescape(a:file)
  else
    return escape(a:file," \t\n*?[{`$\\%#'\"|!<")
  endif
endfunction

function! s:shellslash(path) abort
  if exists('+shellslash') && !&shellslash
    return s:gsub(a:path,'\\','/')
  else
    return a:path
  endif
endfunction

" }}}1
" Completion {{{1

function! s:Complete(A,L,P)
  let sep = !exists("+shellslash") || &shellslash ? '/' : '\'
  let cheats = {
        \ 'a': 'autoload',
        \ 'd': 'doc',
        \ 'f': 'ftplugin',
        \ 'i': 'indent',
        \ 'p': 'plugin',
        \ 's': 'syntax'}
  if a:A =~# '^\w[\\/]' && has_key(cheats,a:A[0])
    let request = cheats[a:A[0]].a:A[1:-1]
  else
    let request = a:A
  endif
  let pattern = substitute(request,'/\|\'.sep,'*'.sep,'g').'*'
  let found = {}
  for glob in split(&runtimepath, ',')
    for path in map(split(glob(glob), "\n"), 'fnamemodify(v:val, ":p")')
      let matches = split(glob(path.sep.pattern),"\n")
      call map(matches,'isdirectory(v:val) ? v:val.sep : v:val')
      call map(matches,'fnamemodify(v:val, ":p")[strlen(path)+1:-1]')
      for match in matches
        let found[match] = 1
      endfor
    endfor
  endfor
  return sort(keys(found))
endfunction

" }}}1
" :PP, :PPmsg {{{1

let s:escapes = {
      \ "\b": '\b',
      \ "\e": '\e',
      \ "\f": '\f',
      \ "\n": '\n',
      \ "\r": '\r',
      \ "\t": '\t',
      \ "\"": '\"',
      \ "\\": '\\'}

function! scriptease#dump(object, ...) abort
  let opt = extend({'width': 0, 'level': 0, 'indent': 1, 'tail': 0, 'seen': []}, a:0 ? copy(a:1) : {})
  let opt.seen = copy(opt.seen)
  let childopt = copy(opt)
  let childopt.tail += 1
  let childopt.level += 1
  for i in range(len(opt.seen))
    if a:object is opt.seen[i]
      return type(a:object) == type([]) ? '[...]' : '{...}'
    endif
  endfor
  if type(a:object) ==# type('')
    if a:object =~# "[\001-\037']"
      let dump = '"'.s:gsub(a:object, "[\001-\037\"\\\\]", '\=get(s:escapes, submatch(0), printf("\\%03o", char2nr(submatch(0))))').'"'
    else
      let dump = string(a:object)
    endif
  elseif type(a:object) ==# type([])
    let childopt.seen += [a:object]
    let dump = '['.join(map(copy(a:object), 'scriptease#dump(v:val, {"seen": childopt.seen, "level": childopt.level})'), ', ').']'
    if opt.width && opt.level + len(s:gsub(dump, '.', '.')) > opt.width
      let space = repeat(' ', opt.level)
      let dump = "[".join(map(copy(a:object), 'scriptease#dump(v:val, childopt)'), ",\n ".space).']'
    endif
  elseif type(a:object) ==# type({})
    let childopt.seen += [a:object]
    let keys = sort(keys(a:object))
    let dump = '{'.join(map(copy(keys), 'scriptease#dump(v:val) . ": " . scriptease#dump(a:object[v:val], {"seen": childopt.seen, "level": childopt.level})'), ', ').'}'
    if opt.width && opt.level + len(s:gsub(dump, '.', '.')) > opt.width
      let space = repeat(' ', opt.level)
      let lines = []
      let last = get(keys, -1, '')
      for k in keys
        let prefix = scriptease#dump(k) . ':'
        let suffix = scriptease#dump(a:object[k]) . ','
        if len(space . prefix . ' ' . suffix) >= opt.width - (k ==# last ? opt.tail : '')
          call extend(lines, [prefix, scriptease#dump(a:object[k], childopt) . ','])
        else
          call extend(lines, [prefix . ' ' . suffix])
        endif
      endfor
      let dump = s:sub("{".join(lines, "\n " . space), ',$', '}')
    endif
  elseif type(a:object) ==# type(function('tr'))
    let dump = s:sub(string(a:object), '^function\(''(\d+)''\)$', 'function(''{\1}'')')
  else
    let dump = string(a:object)
  endif
  return dump
endfunction

function! s:backslashdump(value, indent)
    let out = scriptease#dump(a:value, {'level': 0, 'width': &textwidth - &shiftwidth * 3 - a:indent})
    return s:gsub(out, '\n', "\n".repeat(' ', a:indent + &shiftwidth * 3).'\\')
endfunction

function! s:dump(bang, lnum, value) abort
  if v:errmsg !=# ''
    return
  elseif a:lnum == 999998
    echo scriptease#dump(a:value, {'width': a:bang ? 0 : &columns-1})
  else
    exe a:lnum
    let indent = indent(prevnonblank('.'))
    if a:bang
      let out = scriptease#dump(a:value)
    else
      let out = s:backslashdump(a:value, indent)
    endif
    put =repeat(' ', indent).'PP '.out
    '[
  endif
endfunction

function! s:dumpmsg(bang, count, value) abort
  if v:errmsg !=# ''
    return
  elseif &verbose >= a:count
    for line in split(scriptease#dump(a:value, {'width': a:bang ? 0 : &columns-1}), "\n")
      echomsg line
    endfor
  endif
endfunction

command! -bang -range=999998 -nargs=? -complete=expression PP
      \ if empty(<q-args>) |
      \   let s:more = &more |
      \   try |
      \     set nomore |
      \     while 1 |
      \       let s:input = input('PP> ', '', 'expression') |
      \       if empty(s:input) |
      \         break |
      \       endif |
      \       echon "\n" |
      \       let v:errmsg = '' |
      \       try |
      \         call s:dump(<bang>0, 999998, eval(s:input)) |
      \       catch |
      \         echohl ErrorMsg |
      \         echo v:exception |
      \         echo v:throwpoint |
      \         echohl NONE |
      \       endtry |
      \     endwhile |
      \ finally |
      \   let &more = s:more |
      \ endtry |
      \ else |
      \   let v:errmsg = '' |
      \   call s:dump(<bang>0, <count>, eval(<q-args>)) |
      \ endif

command! -bang -range=0      -nargs=? -complete=expression PPmsg
      \ if !empty(<q-args>) |
      \   let v:errmsg = '' |
      \   call s:dumpmsg(<bang>0, <count>, empty(<q-args>) ? expand('<sfile>') : eval(<q-args>)) |
      \ elseif &verbose >= <count> && !empty(expand('<sfile>')) |
      \  echomsg expand('<sfile>').', line '.expand('<slnum>') |
      \ endif

" }}}1
" g! {{{1

function! s:opfunc(type) abort
  let sel_save = &selection
  let cb_save = &clipboard
  let reg_save = @@
  try
    set selection=inclusive clipboard-=unnamed clipboard-=unnamedplus
    if a:type =~ '^\d\+$'
      silent exe 'normal! ^v'.a:type.'$hy'
    elseif a:type =~# '^.$'
      silent exe "normal! `<" . a:type . "`>y"
    elseif a:type ==# 'line'
      silent exe "normal! '[V']y"
    elseif a:type ==# 'block'
      silent exe "normal! `[\<C-V>`]y"
    else
      silent exe "normal! `[v`]y"
    endif
    redraw
    return @@
  finally
    let @@ = reg_save
    let &selection = sel_save
    let &clipboard = cb_save
  endtry
endfunction

function! s:filterop(type) abort
  let reg_save = @@
  try
    let expr = s:opfunc(a:type)
    let @@ = matchstr(expr, '^\_s\+').scriptease#dump(eval(s:gsub(expr,'\n%(\s*\\)=',''))).matchstr(expr, '\_s\+$')
    if @@ !~# '^\n*$'
      normal! gvp
    endif
  catch /^.*/
    echohl ErrorMSG
    echo v:errmsg
    echohl NONE
  finally
    let @@ = reg_save
  endtry
endfunction

nnoremap <silent> <Plug>ScripteaseFilter :<C-U>set opfunc=<SID>filterop<CR>g@
xnoremap <silent> <Plug>ScripteaseFilter :<C-U>call <SID>filterop(visualmode())<CR>
nmap g! <Plug>ScripteaseFilter
nmap g!! <Plug>ScripteaseFilter_
xmap g! <Plug>ScripteaseFilter

" }}}1
" :Verbose {{{1

command! -range=999998 -nargs=1 -complete=command Verbose
      \ :exe s:Verbose(<count> == 999998 ? '' : <count>, <q-args>)

function! s:Verbose(level, excmd)
  let temp = tempname()
  let verbosefile = &verbosefile
  call writefile([':'.a:level.'Verbose '.a:excmd], temp, 'b')
  return
        \ 'try|' .
        \ 'let &verbosefile = '.string(temp).'|' .
        \ 'silent '.a:level.'verbose exe '.string(a:excmd).'|' .
        \ 'finally|' .
        \ 'let &verbosefile = '.string(verbosefile).'|' .
        \ 'endtry|' .
        \ 'pedit '.temp.'|wincmd P|nnoremap <buffer> q :bd<CR>'
endfunction

" }}}1
" :Scriptnames {{{1

function! scriptease#capture(excmd) abort
  try
    redir => out
    exe 'silent! '.a:excmd
  finally
    redir END
  endtry
  return out
endfunction

function! s:names() abort
  let names = scriptease#capture('scriptnames')
  let list = []
  for line in split(names, "\n")
    if line =~# ':'
      call add(list, {'text': matchstr(line, '\d\+'), 'filename': expand(matchstr(line, ': \zs.*'))})
    endif
  endfor
  return list
endfunction

function! scriptease#scriptname(file) abort
  if a:file =~# '^\d\+$'
    return get(s:names(), a:file-1, {'filename': a:file}).filename
  else
    return a:file
  endif
endfunction

function! scriptease#scriptid(filename) abort
  let filename = fnamemodify(expand(a:filename), ':p')
  for script in s:names()
    if script.filename ==# filename
      return +script.text
    endif
  endfor
  return ''
endfunction

command! -bar Scriptnames call setqflist(s:names())|copen

" }}}1
" :Runtime {{{1

function! s:unlet_for(files) abort
  let guards = []
  for file in a:files
    if filereadable(file)
      let lines = readfile(file, '', 500)
      if len(lines)
        for i in range(len(lines)-1)
          let unlet = matchstr(lines[i], '^if .*\<exists *( *[''"]\%(\g:\)\=\zs[0-9A-Za-z_#]\+\ze[''"]')
          if unlet !=# '' && index(guards, unlet) == -1
            for j in range(0, 4)
              if lines[i+j] =~# '^\s*finish\>'
                call extend(guards, [unlet])
                break
              endif
            endfor
          endif
        endfor
      endif
    endif
  endfor
  if empty(guards)
    return ''
  else
    return 'unlet! '.join(map(guards, '"g:".v:val'), ' ')
  endif
endfunction

function! s:lencompare(a, b)
  return len(a:a) - len(a:b)
endfunction

function! s:findinrtp(path)
  let path = fnamemodify(a:path, ':p')
  let candidates = []
  for glob in split(&runtimepath, ',')
    let candidates += filter(split(glob(glob), "\n"), 'path[0 : len(v:val)-1] ==# v:val && path[len(v:val)] =~# "[\\/]"')
  endfor
  if empty(candidates)
    return ['', '']
  endif
  let preferred = sort(candidates, s:function('s:lencompare'))[-1]
  return [preferred, path[strlen(preferred)+1 : -1]]
endfunction

function! s:runtime(bang, ...) abort
  let unlets = []
  let do = []
  let predo = ''

  if a:0
    let files = a:000
  elseif &filetype ==# 'vim' || expand('%:e') ==# 'vim'
    let files = [s:findinrtp(expand('%:p'))[1]]
    if empty(files[0])
      let files = ['%']
    endif
    if &modified && (&autowrite || &autowriteall)
      let predo = 'silent wall|'
    endif
  else
    for ft in split(&filetype, '\.')
      for pattern in ['ftplugin/%s.vim', 'ftplugin/%s_*.vim', 'ftplugin/%s/*.vim', 'indent/%s.vim', 'syntax/%s.vim', 'syntax/%s/*.vim']
        call extend(unlets, split(globpath(&rtp, printf(pattern, ft)), "\n"))
      endfor
    endfor
    let run = s:unlet_for(unlets)
    if run !=# ''
      let run .= '|'
    endif
    let run .= 'filetype detect'
    echo ':'.run
    return run
  endif

  for request in files
    if request =~# '^\.\=[\\/]\|^\w:[\\/]\|^[%#~]\|^\d\+$'
      let request = scriptease#scriptname(request)
      let unlets += split(glob(request), "\n")
      let do += map(copy(unlets), '"source ".escape(v:val, " \t|!")')
    else
      if get(do, 0, [''])[0] !~# '^runtime!'
        let do += ['runtime!']
      endif
      let unlets += split(globpath(&rtp, request, 1), "\n")
      let do[-1] .= ' '.escape(request, " \t|!")
    endif
  endfor
  call extend(do, ['filetype detect'])
  let run = s:unlet_for(unlets)
  if run !=# ''
    let run .= '|'
  endif
  let run .= join(do, '|')
  echo ':'.run
  return predo.run
endfunction

command! -bang -bar -nargs=* -complete=customlist,s:Complete Runtime
      \ :exe s:runtime('<bang>', <f-args>)

" }}}1
" :Disarm {{{1

function! scriptease#disarm(file)
  let augroups = filter(readfile(a:file), 'v:val =~# "^\\s*aug\\%[roup]\\s"')
  call filter(augroups, 'v:val !~# "^\\s*aug\\%[roup]\\s\\+END"')
  for augroup in augroups
    exe augroup
    autocmd!
    augroup END
    exe s:sub(augroup, 'aug\%[roup]', '&!')
  endfor
  call s:disable_maps_and_commands(a:file, 0)
  let tabnr = tabpagenr()
  let winnr = winnr()
  let altwinnr = winnr('#')
  tabdo windo call s:disable_maps_and_commands(a:file, 1)
  exe 'tabnext '.tabnr
  exe altwinnr.'wincmd w'
  exe winnr.'wincmd w'
  return s:unlet_for([a:file])
endfunction

function! s:disable_maps_and_commands(file, buf)
  let last_set = "\tLast set from " . fnamemodify(a:file, ':~')
  for line in split(scriptease#capture('verbose command'), "\n")
    if line ==# last_set
      if last[2] ==# (a:buf ? 'b' : ' ')
        exe 'delcommand '.matchstr(last[4:-1], '^\w\+')
      endif
    else
      let last = line
    endif
  endfor
  for line in split(scriptease#capture('verbose map').scriptease#capture('verbose map!'), "\n")
    if line ==# last_set
      let map = matchstr(last, '^.\s\+\zs\S\+')
      let rest = matchstr(last, '^.\s\+\S\+\s\+\zs[&* ][ @].*')
      if rest[1] ==# (a:buf ? '@' : ' ')
        let cmd = last =~# '^!' ? 'unmap! ' : last[0].'unmap '
        exe cmd.(a:buf ? '<buffer>' : '').map
      endif
    else
      let last = line
    endif
  endfor
endfunction

function! s:disarm(...) abort
  let files = []
  let unlets = []
  for request in a:000
    if request =~# '^\.\=[\\/]\|^\w:[\\/]\|^[%#~]\|^\d\+$'
      let request = expand(scriptease#scriptname(request))
      if isdirectory(request)
        let request .= "/**/*.vim"
      endif
      let files += split(glob(request), "\n")
    else
      let files += split(globpath(&rtp, request, 1), "\n")
    endif
  endfor
  for file in files
    let unlets += [scriptease#disarm(expand(file))]
  endfor
  echo join(files, ' ')
  return join(filter(unlets, 'v:val !=# ""'), '|')
endfunction

command! -bang -bar -nargs=* -complete=customlist,s:Complete Disarm
      \ :exe s:disarm(<f-args>)

" }}}1
" :Breakadd, :Breakdel {{{1

augroup scriptease_breakadd
  autocmd!
  autocmd FileType vim command!
        \   -buffer -bar -nargs=? -complete=custom,s:Complete_breakadd Breakadd
        \ :exe s:break('add',<q-args>)
  autocmd FileType vim command!
        \   -buffer -bar -nargs=? -complete=custom,s:Complete_breakdel Breakdel
        \ :exe s:break('del',<q-args>)
augroup END

function! s:breaksnr(arg) abort
  let id = scriptease#scriptid('%')
  if id
    return s:gsub(a:arg, '^func.*\zs%(<s:|\<SID\>)', '<SNR>'.id.'_')
  else
    return a:arg
  endif
endfunction

function! s:break(type, arg) abort
  if a:arg ==# 'here' || a:arg ==# ''
    let lnum = searchpair('^\s*fu\%[nction]\>.*(', '', '^\s*endf\%[unction]\>', 'Wbn')
    if lnum && lnum < line('.')
      let function = matchstr(getline(lnum), '^\s*\w\+!\=\s*\zs[^( ]*')
      if function =~# '^s:\|^<SID>'
        let id = scriptease#scriptid('%')
        if id
          let function = s:sub(function, '^s:|^\<SID\>', '<SNR>'.id.'_')
        else
          return 'echoerr "Could not determine script id"'
        endif
      endif
      if function =~# '\.'
        return 'echoerr "Dictionary functions not supported"'
      endif
      return 'break'.a:type.' func '.(line('.')==lnum ? '' : line('.')-lnum).' '.function
    else
      return 'break'.a:type.' here'
    endif
  endif
  return 'break'.a:type.' '.s:breaksnr(a:arg)
endfunction

function! s:Complete_breakadd(A, L, P)
  let functions = join(sort(map(split(scriptease#capture('function'), "\n"), 'matchstr(v:val, " \\zs[^(]*")')), "\n")
  if a:L =~# '^\w\+\s\+\w*$'
    return "here\nfile\nfunc"
  elseif a:L =~# '^\w\+\s\+func\s*\d*\s\+s:'
    let id = scriptease#scriptid('%')
    return s:gsub(functions,'\<SNR\>'.id.'_', 's:')
  elseif a:L =~# '^\w\+\s\+func '
    return functions
  elseif a:L =~# '^\w\+\s\+file '
    return glob(a:A."*")
  else
    return ''
  endif
endfunction

function! s:Complete_breakdel(A, L, P)
  let args = matchstr(a:L, '\s\zs\S.*')
  let list = split(scriptease#capture('breaklist'), "\n")
  call map(list, 's:sub(v:val, ''^\s*\d+\s*(\w+) (.*)  line (\d+)$'', ''\1 \3 \2'')')
  if a:L =~# '^\w\+\s\+\w*$'
    return "*\nhere\nfile\nfunc"
  elseif a:L =~# '^\w\+\s\+func\s'
    return join(map(filter(list, 'v:val =~# "^func"'), 'v:val[5 : -1]'), "\n")
  elseif a:L =~# '^\w\+\s\+file\s'
    return join(map(filter(list, 'v:val =~# "^file"'), 'v:val[5 : -1]'), "\n")
  else
    return ''
  endif
endfunction

" }}}1
" :Vopen, :Vedit, ... {{{1

function! s:previewwindow()
  for i in range(1, winnr('$'))
    if getwinvar(i, '&previewwindow') == 1
      return i
    endif
  endfor
  return -1
endfunction

function! s:runtime_globpath(file)
  return split(globpath(escape(&runtimepath, ' '), a:file), "\n")
endfunction

function! s:find(count,cmd,file,lcd)
  let found = s:runtime_globpath(a:file)
  let file = get(found, a:count - 1, '')
  if file ==# ''
    return "echoerr 'E345: Can''t find file \"".a:file."\" in runtimepath'"
  elseif a:cmd ==# 'read'
    return a:cmd.' '.s:fnameescape(file)
  elseif a:lcd
    let path = file[0:-strlen(a:file)-2]
    return a:cmd.' '.s:fnameescape(file) . '|lcd '.s:fnameescape(path)
  else
    let window = 0
    let precmd = ''
    let postcmd = ''
    if a:cmd =~# '^pedit'
      try
        exe 'silent ' . a:cmd
      catch /^Vim\%((\a\+)\)\=:E32/
      endtry
      let window = s:previewwindow()
      let precmd = printf('%d wincmd w|', window)
      let postcmd = '|wincmd w'
    elseif a:cmd !~# '^edit'
      exe a:cmd
    endif
    call setloclist(window, map(found,
          \ '{"filename": v:val, "text": v:val[0 : -len(a:file)-2]}'))
    return precmd . 'll'.matchstr(a:cmd, '!$').' '.a:count . postcmd
  endif
endfunction

command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Ve
      \ :execute s:find(<count>,'edit<bang>',<q-args>,0)
command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Vedit
      \ :execute s:find(<count>,'edit<bang>',<q-args>,0)
command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Vopen
      \ :execute s:find(<count>,'edit<bang>',<q-args>,1)
command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Vsplit
      \ :execute s:find(<count>,'split',<q-args>,<bang>0)
command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Vvsplit
      \ :execute s:find(<count>,'vsplit',<q-args>,<bang>0)
command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Vtabedit
      \ :execute s:find(<count>,'tabedit',<q-args>,<bang>0)
command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Vpedit
      \ :execute s:find(<count>,'pedit<bang>',<q-args>,0)
command! -bar -bang -range=1 -nargs=1 -complete=customlist,s:Complete Vread
      \ :execute s:find(<count>,'read',<q-args>,<bang>0)

" }}}1
" :Time {{{1

command! -count=1 -nargs=? -complete=command Time :exe s:time(<q-args>, <count>)

function! s:time(cmd, count)
  let time = reltime()
  try
    if a:count > 1
      let i = 0
      while i < a:count
        execute a:cmd
        let i += 1
      endwhile
    else
      execute a:cmd
    endif
  finally
    redraw
    echomsg matchstr(reltimestr(reltime(time)), '.*\..\{,3\}') . ' seconds to run :'.a:cmd
  endtry
  return ''
endfunction

" }}}1
" zS {{{1

function! scriptease#synnames(...) abort
  if a:0
    let [line, col] = [a:1, a:2]
  else
    let [line, col] = [line('.'), col('.')]
  endif
  return reverse(map(synstack(line, col), 'synIDattr(v:val,"name")'))
endfunction

function! s:zS(count)
  if a:count
    let name = get(scriptease#synnames(), a:count-1, '')
    if name !=# ''
      return 'syntax list '.name
    endif
  else
    echo join(scriptease#synnames(), ' ')
  endif
  return ''
endfunction

nnoremap <silent> <Plug>ScripteaseSynnames :<C-U>exe <SID>zS(v:count)<CR>
nmap zS <Plug>ScripteaseSynnames

" }}}1
" K {{{1

augroup scriptease_help
  autocmd!
  autocmd FileType vim nnoremap <silent><buffer> K :exe 'help '.<SID>helptopic()<CR>
augroup END

function! s:helptopic()
  let col = col('.') - 1
  while col && getline('.')[col] =~# '\k'
    let col -= 1
  endwhile
  let pre = col == 0 ? '' : getline('.')[0 : col]
  let col = col('.') - 1
  while col && getline('.')[col] =~# '\k'
    let col += 1
  endwhile
  let post = getline('.')[col : -1]
  let syn = get(scriptease#synnames(), 0, '')
  let cword = expand('<cword>')
  if syn ==# 'vimFuncName'
    return cword.'()'
  elseif syn ==# 'vimOption'
    return "'".cword."'"
  elseif syn ==# 'vimUserAttrbKey'
    return ':command-'.cword
  elseif pre =~# '^\s*:\=$'
    return ':'.cword
  elseif pre =~# '\<v:$'
    return 'v:'.cword
  elseif cword ==# 'v' && post =~# ':\w\+'
    return 'v'.matchstr(post, ':\w\+')
  else
    return cword
  endif
endfunction

function! s:build_path() abort
  let old_path = substitute(&g:path, '\v^\.,/%(usr|emx)/include,,,?', '', '')
  let new_path = escape(&runtimepath, ' ')
  return !empty(old_path) ? old_path.','.new_path : new_path
endfunction

" }}}1
" Settings {{{1

function! s:setup() abort
  setlocal suffixesadd=.vim keywordprg=:help
  let b:dispatch = ':Runtime'
  command! -bar -bang -buffer Console Runtime|PP
endfunction

augroup scriptease
  autocmd!
  autocmd FileType vim,help let &l:path = s:build_path()
  autocmd FileType help command! -bar -bang -buffer Console PP
  autocmd FileType vim call s:setup()
  " Recent versions of vim.vim set iskeyword to include ":", which breaks among
  " other things tags. :(
  autocmd FileType vim setlocal iskeyword-=:
  autocmd Syntax vim setlocal iskeyword-=:
augroup END

" }}}1
" Projectionist {{{1

function! s:projectionist_detect() abort
  let file = get(g:, 'projectionist_file', '')
  let path = s:sub(s:findinrtp(file)[0], '[\/]after$', '')
  if !empty(path)
    let reload = ":Runtime ./{open}autoload,plugin{close}/**/*.vim"
    call projectionist#append(path, {
          \ "*": {"start": reload},
          \ "*.vim": {"start": reload},
          \ "plugin/*.vim":   {"command": "plugin", "alternate": "autoload/{}.vim"},
          \ "autoload/*.vim": {"command": "autoload", "alternate": "plugin/{}.vim"},
          \ "compiler/*.vim": {"command": "compiler"},
          \ "ftdetect/*.vim": {"command": "ftdetect"},
          \ "syntax/*.vim":   {"command": "syntax", "alternate": ["ftplugin/{}.vim", "indent/{}.vim"]},
          \ "ftplugin/*.vim": {"command": "ftplugin", "alternate": ["indent/{}.vim", "syntax/{}.vim"]},
          \ "indent/*.vim":   {"command": "indent", "alternate": ["syntax/{}.vim", "ftplugin/{}.vim"]},
          \ "after/*.vim":    {"command": "after"},
          \ "doc/*.txt":      {"command": "doc", "start": reload}})
  endif
endfunction

augroup scriptease_projectionist
  autocmd!
  autocmd User ProjectionistDetect call s:projectionist_detect()
augroup END

" }}}1

" vim:set et sw=2: