Commits

ZyX_I  committed dc59be5

ftplugin/aurumlog: Added support for command-t. First working revision
Fallbacks to old inputlist() thus tests do not fail. Should
do something smarter but simple here as inputlist() did not
become useful and trying to emulate Command-T does not make
much sense. Command-T support also needs moving to a separate
file and using in other ftplugins and in global mapping.

Ref #38

  • Participants
  • Parent commits 2c95e03
  • Branches command-t

Comments (0)

Files changed (4)

File ftplugin/aurumlog.vim

     endif
 endfunction
 "▶1 getfile
-function s:F.getfile(bvar, files, hex, more)
+function s:F.getfile(bvar, files, hex, more, cbargs)
     if empty(a:files)
         return 0
     endif
     endif
     let file=0
     if len(files)==1
-        let file=files[0]
+        return call(a:cbargs[0], [a:bvar, a:hex, files[0]]+a:cbargs[1:], {})
     else
-        return s:F.rbgetfile(files)
+        return s:F.promptuser(files, a:bvar, a:hex, a:cbargs)
+    endif
+endfunction
+"▶1 promptuser
+let s:initialized_ruby=((has('ruby'))?(0):(-1))
+function s:F.promptuser(files, bvar, hex, cbargs)
+    if !s:initialized_ruby
+        let s:initialized_ruby=1
+        try
+            execute 'rubyfile' fnameescape(s:_r.os.path.join(s:_frawor.runtimepath, 'ruby', 'aurum-command-t-rubyinit.rb'))
+        catch
+            let s:initialized_ruby=-1
+        endtry
+        lockvar s:initialized_ruby
+    endif
+    if s:initialized_ruby==1
+        let b:aurum_bvar=a:bvar
+        let b:aurum_hex=a:hex
+        let [b:aurum_callback_fun; b:aurum_addargs]=a:cbargs
+        ruby $comman_t_old = $command_t
+        ruby $command_t = $aurum_command_t
+        ruby $command_t.show_aurum_finder
+        autocmd BufWipeOut <buffer> ruby $command_t = $command_t_old
+    else
         let choice=inputlist(['Select file (0 to cancel):']+
-                    \        map(copy(files), '(v:key+1).". ".v:val'))
+                    \        map(copy(a:files), '(v:key+1).". ".v:val'))
         if choice
-            let file=files[choice-1]
+            return call(a:cbargs[0], [a:bvar, a:hex, a:files[choice-1]]+
+                        \            a:cbargs[1:], {})
         endif
     endif
-    return file
-endfunction
-function s:F.rbgetfile(files)
-ruby << EOF
-require 'command-t/aurum_controller'
-require 'command-t/vim'
-$aurum_command_t = CommandT::AurumController.new
-EOF
-ruby $aurum_command_t.show_aurum_finder
 endfunction
 "▶1 gethexfile
-function s:F.gethexfile(...)
+function s:F.gethexfile(more, cbargs)
     let bvar=s:_r.bufvars[bufnr('%')]
     let [blockstart, blockend, hex]=bvar.getblock(bvar)
     let spname=s:F.findCurSpecial(bvar, hex, blockstart[0])
     let cs=bvar.repo.changesets[hex]
     let file=0
+    let cbargs=copy(a:cbargs)
+    let cbargs[0]=s:F.callbacks[cbargs[0]]
     if spname=~#'\v^file\d+$'
         " XXX If fileN special exists, then files property was definitely added, 
         " so no need to use getcsprop()
-        let file=cs.files[str2nr(spname[4:])]
+        return call(cbargs[0], [bvar, hex, cs.files[str2nr(spname[4:])]]+
+                    \          cbargs[1:], {})
     else
         let file=s:F.getfile(bvar,
                     \        bvar.repo.functions.getcsprop(bvar.repo, cs,
-                    \                                      (((a:0 && !a:1))?
+                    \                                      ((a:more)?
                     \                                           ('allfiles'):
                     \                                           ('files'))),
-                    \        hex, (a:0 && !a:1))
-    endif
-    return [hex, file]
-endfunction
-"▶1 open
-function s:F.open(...)
-    let [hex, file]=call(s:F.gethexfile, a:000, {})
-    if file is 0
-        return 0
-    endif
-    let bvar=s:_r.bufvars[bufnr('%')]
-    call s:F.cwin(bvar)
-    call s:_r.mrun('silent edit', 'file', bvar.repo, hex, file)
-    return 1
-endfunction
-"▶1 annotate
-function s:F.annotate(...)
-    if call(s:F.open, a:000, {})
-        AuAnnotate
+                    \        hex, a:more, cbargs)
     endif
 endfunction
-"▶1 diff
-function s:F.diff(...)
-    let [hex, file]=s:F.gethexfile()
-    if file is 0
-        return ''
-    endif
-    let bvar=s:_r.bufvars[bufnr('%')]
-    call s:F.cwin(bvar)
+"▶1 Callbacks
+let s:F.callbacks={}
+"▶2 open
+function s:F.callbacks.open(bvar, hex, file)
+    call s:F.cwin(a:bvar)
+    call s:_r.mrun('silent edit', 'file', a:bvar.repo, a:hex, a:file)
+endfunction
+"▶2 annotate
+function s:F.callbacks.annotate(bvar, hex, file)
+    call s:F.callbacks.open(a:bvar, a:hex, a:file)
+    AuAnnotate
+endfunction
+"▶2 diff
+function s:F.callbacks.diff(bvar, hex, file, ...)
+    call s:F.cwin(a:bvar)
     if a:0 && a:1
-        call s:_r.mrun('silent edit', 'diff', bvar.repo, hex, '', [file], {})
+        call s:_r.mrun('silent edit', 'diff', a:bvar.repo,a:hex,'',[a:file],{})
     else
-        call s:_r.mrun('silent edit', 'diff', bvar.repo, '', hex, [file], {})
+        call s:_r.mrun('silent edit', 'diff', a:bvar.repo,'',a:hex,[a:file],{})
     endif
 endfunction
+"▶2 filehistory
+function s:F.callbacks.filehistory(bvar, hex, file)
+    let crrcond=((has_key(a:bvar.opts, 'crrestrict'))?
+                \   (string(a:bvar.opts.crrestrict).' isnot# v:key &&'):
+                \   (''))
+    let opts=filter(copy(a:bvar.opts), crrcond.'index(s:ignkeys, v:key)==-1')
+    call extend(opts, {'files': [s:_r.cmdutils.globescape(a:file)],
+                \ 'crrestrict': 'files'})
+    call s:_r.run('silent edit', 'log', a:bvar.repo, opts)
+endfunction
 "▶1 vimdiff
-function s:F.vimdiff(...)
-    let [hex, file]=s:F.gethexfile()
-    if file is 0
-        return
-    endif
-    let bvar=s:_r.bufvars[bufnr('%')]
-    let cs=bvar.repo.changesets[hex]
-    call s:F.cwin(bvar)
+function s:F.callbacks.vimdiff(bvar, hex, file, ...)
+    let cs=a:bvar.repo.changesets[a:hex]
+    call s:F.cwin(a:bvar)
     if a:0 && a:1
         execute 'silent edit'
-                    \ fnameescape(s:_r.os.path.join(bvar.repo.path, file))
-        call s:_r.vimdiff.split(s:_r.fname('file', bvar.repo, hex, file), 0)
+                    \ fnameescape(s:_r.os.path.join(a:bvar.repo.path, a:file))
+        call s:_r.vimdiff.split(s:_r.fname('file',a:bvar.repo,a:hex,a:file), 0)
     elseif !empty(cs.parents)
-        call s:_r.run('silent edit', 'file', bvar.repo, hex, file)
-        call s:_r.vimdiff.split(s:_r.fname('file', bvar.repo, cs.parents[0],
-                    \                      file), 0)
+        call s:_r.run('silent edit', 'file', a:bvar.repo, a:hex, a:file)
+        call s:_r.vimdiff.split(s:_r.fname('file', a:bvar.repo, cs.parents[0],
+                    \                      a:file), 0)
     else
-        call s:_f.throw('nopars', hex)
+        call s:_f.throw('nopars', a:hex)
     endif
 endfunction
 "▶1 findfirstvisible :: n → hex
 function s:F.prev()
     return s:F.findfirstvisible(v:count1)
 endfunction
-"▶1 filehistory
-function s:F.filehistory()
-    let [hex, file]=s:F.gethexfile()
-    if file is 0
-        return ''
-    endif
-    let bvar=s:_r.bufvars[bufnr('%')]
-    let crrcond=((has_key(bvar.opts, 'crrestrict'))?
-                \   (string(bvar.opts.crrestrict).' isnot# v:key &&'):
-                \   (''))
-    let opts=filter(copy(bvar.opts), crrcond.'index(s:ignkeys, v:key)==-1')
-    call extend(opts, {'files': [s:_r.cmdutils.globescape(file)],
-                \ 'crrestrict': 'files'})
-    return ':silent edit '.fnameescape(s:_r.fname('log', bvar.repo, opts))."\n"
-endfunction
 "▶1 update
 function s:F.update()
     let bvar=s:_r.bufvars[bufnr('%')]
     return ':<C-u>call <SNR>'.s:_sid.'_Eval(''s:F.'.a:f.
                 \                   '('.string(string(a:000))[2:-3].')'')<CR>'
 endfunction
+function s:c(f, m, ...)
+    return ':<C-u>call call(<SNR>'.s:_sid.'_Eval("s:F.gethexfile"), '.
+                \'['.a:m.', ["'.a:f.'", '.string(a:000)[1:-2].']], {})<CR>'
+endfunction
 call s:_f.mapgroup.add('AuLog', {
             \      'Enter': {'lhs': "\n", 'rhs': s:m('cr'),             },
-            \       'File': {'lhs': 'gF', 'rhs': s:F.filehistory        },
+            \       'File': {'lhs': 'gF', 'rhs': s:c('filehistory', 0)  },
             \       'User': {'lhs': 'gu', 'rhs': s:m('cr', 'user')      },
             \       'Date': {'lhs': 'gM', 'rhs': s:m('cr', 'time')      },
             \     'Branch': {'lhs': 'gb', 'rhs': s:m('cr', 'branch')    },
             \    'RFVdiff': {'lhs': 'gC', 'rhs': [1], 'func': s:F.fvdiff},
             \      'Fdiff': {'lhs': 'gd', 'rhs': s:m('cr', 'curdiff')   },
             \     'RFdiff': {'lhs': 'gc', 'rhs': s:m('cr', 'revdiff')   },
-            \       'Diff': {'lhs':  'd', 'rhs': s:m('diff', 1)         },
-            \      'Rdiff': {'lhs':  'c', 'rhs': s:m('diff')            },
-            \      'Vdiff': {'lhs':  'D', 'rhs': s:m('vimdiff', 1)      },
-            \     'RVdiff': {'lhs':  'C', 'rhs': s:m('vimdiff')         },
+            \       'Diff': {'lhs':  'd', 'rhs': s:c('diff', 0, 1)      },
+            \      'Rdiff': {'lhs':  'c', 'rhs': s:c('diff', 0)         },
+            \      'Vdiff': {'lhs':  'D', 'rhs': s:c('vimdiff', 0, 1)   },
+            \     'RVdiff': {'lhs':  'C', 'rhs': s:c('vimdiff', 0)      },
             \       'Next': {'lhs':  'K', 'rhs': s:F.next               },
             \       'Prev': {'lhs':  'J', 'rhs': s:F.prev               },
-            \       'Open': {'lhs':  'o', 'rhs': s:m('open')            },
-            \    'OpenAny': {'lhs':  'O', 'rhs': s:m('open', 0)         },
-            \   'Annotate': {'lhs':  'a', 'rhs': s:m('annotate')        },
-            \'AnnotateAny': {'lhs':  'A', 'rhs': s:m('annotate', 0)     },
+            \       'Open': {'lhs':  'o', 'rhs': s:c('open', 0)         },
+            \    'OpenAny': {'lhs':  'O', 'rhs': s:c('open', 1)         },
+            \   'Annotate': {'lhs':  'a', 'rhs': s:c('annotate', 0)     },
+            \'AnnotateAny': {'lhs':  'A', 'rhs': s:c('annotate', 1)     },
             \     'Update': {'lhs':  'U', 'rhs': s:F.update             },
             \       'Exit': {'lhs':  'X', 'rhs': ':<C-u>bwipeout<CR>'   },
             \}, {'func': s:F.cr, 'silent': 1, 'mode': 'n'})
 delfunction s:m
+delfunction s:c
 "▶1
-call frawor#Lockvar(s:, '_r')
+call frawor#Lockvar(s:, '_r,initialized_ruby')
 " vim: ft=vim ts=4 sts=4 et fmr=▶,▲

File ruby/aurum-command-t-rubyinit.rb

+require 'command-t/aurum_controller'
+require 'command-t/vim'
+$aurum_command_t = CommandT::AurumController.new

File ruby/command-t/finder/aurum_finder.rb

         end
 
         def open_selection command, selection, options
-            ::VIM::evaluate "return #{selection}"
+            selection = selection.gsub(/["\\\n]/) {|s|
+                {'"'  => "\\\"",
+                 "\n" => "\\n",
+                 "\\" => "\\\\"}[s]}
+            ::VIM::evaluate "call(b:aurum_callback_fun, [b:aurum_bvar, b:aurum_hex, \"#{selection}\"]+b:aurum_addargs, {})"
         end
     end
 end

File ruby/command-t/scanner/aurum_scanner.rb

 module CommandT
     class AurumScanner < Scanner
         def paths
-            ::VIM::evaluate "a:files"
+            @paths ||= (::VIM::evaluate "a:files").clone
         end
     end
 end