11"""ropevim, a vim mode for using rope refactoring library"""
2+ import glob
23import os
34import tempfile
45import re
910
1011import vim
1112
13+ # Gobal var to be able to shutup output
14+ _rope_quiet = False
15+
1216
1317class VimUtils (environment .Environment ):
1418
@@ -21,8 +25,8 @@ def ask(self, prompt, default=None, starting=None):
2125if starting is None :
2226starting = ''
2327if default is not None :
24- prompt = prompt + ( '[%s ] '% default )
25- result = call ('input("%s ", "%s ")' % (prompt ,starting ))
28+ prompt = prompt + '[{0} ] '. format ( default )
29+ result = call ('input("{0} ", "{1} ")' . format (prompt ,starting ))
2630if default is not None and result == '' :
2731return default
2832return result
@@ -32,11 +36,14 @@ def ask_values(self, prompt, values, default=None,
3236if show_values or (show_values is None and len (values )< 14 ):
3337self ._print_values (values )
3438if default is not None :
35- prompt = prompt + ( '[%s ] '% default )
39+ prompt = prompt + '[{0} ] '. format ( default )
3640starting = starting or ''
3741_completer .values = values
38- answer = call ('input("%s", "%s", "customlist,RopeValueCompleter")' %
39- (prompt ,starting ))
42+ answer = call (
43+ 'input("{0}", "{1}", "customlist,RopeValueCompleter")' .format (
44+ prompt ,starting
45+ )
46+ )
4047if answer is None :
4148if 'cancel' in values :
4249return 'cancel'
@@ -54,14 +61,14 @@ def _print_values(self, values):
5461echo ('\n ' .join (numbered )+ '\n ' )
5562
5663def ask_directory (self ,prompt ,default = None ,starting = None ):
57- return call ('input("%s ", ".", "dir")' % prompt )
64+ return call ('input("{0} ", ".", "dir")' . format ( prompt ) )
5865
5966def _update_proposals (self ,values ):
6067self .completeopt = vim .eval ('&completeopt' )
6168self .preview = 'preview' in self .completeopt
6269
6370if not self .get ('extended_complete' ):
64- return u',' .join (u"'%s'" % self ._completion_text (proposal )
71+ return u',' .join (u"'{0}'" . format ( self ._completion_text (proposal ) )
6572for proposal in values )
6673
6774return u',' .join (self ._extended_completion (proposal )
@@ -78,7 +85,7 @@ def ask_completion(self, prompt, values, starting=None):
7885col = int (call ('col(".")' ))
7986if starting :
8087col -= len (starting )
81- self ._command (u'call complete(%s , [%s ])' % (col ,proposals ),
88+ self ._command (u'call complete({0} , [{1} ])' . format (col ,proposals ),
8289encode = True )
8390return None
8491
@@ -95,8 +102,8 @@ def y_or_n(self, prompt):
95102return self .yes_or_no (prompt )
96103
97104def get (self ,name ,default = None ):
98- vimname = 'g:pymode_rope_%s' % name
99- if str (vim .eval ('exists("%s ")' % vimname ))== '0' :
105+ vimname = 'g:pymode_rope_{0}' . format ( name )
106+ if str (vim .eval ('exists("{0} ")' . format ( vimname ) ))== '0' :
100107return default
101108result = vim .eval (vimname )
102109if isinstance (result ,str )and result .isdigit ():
@@ -261,8 +268,9 @@ def _writedefs(self, locations, filename):
261268
262269def show_doc (self ,docs ,altview = False ):
263270if docs :
264- cmd = 'call pymode#ShowStr("%s")' % str (docs .replace ('"' ,'\\ "' ))
265- vim .command (cmd )
271+ vim .command (
272+ 'call pymode#ShowStr("{0}")' .format (docs .replace ('"' ,'\\ "' ))
273+ )
266274
267275def preview_changes (self ,diffs ):
268276echo (diffs )
@@ -281,23 +289,33 @@ def add_hook(self, name, callback, hook):
281289'after_save' :'FileWritePost,BufWritePost' ,
282290'exit' :'VimLeave' }
283291self ._add_function (name ,callback )
284- vim .command ('autocmd %s *.py call %s()' %
285- (mapping [hook ],_vim_name (name )))
292+ vim .command (
293+ 'autocmd {0} *.py call {1}()' .format (
294+ mapping [hook ],_vim_name (name )
295+ )
296+ )
286297
287298def _add_command (self ,name ,callback ,key ,prefix ,prekey ):
288299self ._add_function (name ,callback ,prefix )
289- vim .command ('command! -range %s call %s()' %
290- (_vim_name (name ),_vim_name (name )))
300+ vim .command (
301+ 'command! -range {0} call {1}()' .format (
302+ _vim_name (name ),_vim_name (name )
303+ )
304+ )
291305if key is not None :
292306key = prekey + key .replace (' ' ,'' )
293- vim .command ('noremap %s :call %s()<cr>' % (key ,_vim_name (name )))
307+ vim .command (
308+ 'noremap {0} :call {1}()<cr>' .format (key ,_vim_name (name ))
309+ )
294310
295311def _add_function (self ,name ,callback ,prefix = False ):
296312globals ()[name ]= callback
297313arg = 'None' if prefix else ''
298- vim .command ('function! %s()\n ' % _vim_name (name )+
299- 'python ropevim.%s(%s)\n ' % (name ,arg )+
300- 'endfunction\n ' )
314+ vim .command (
315+ 'function! {0}()\n '
316+ 'python ropevim.{1}({2})\n '
317+ 'endfunction\n ' .format (_vim_name (name ),name ,arg )
318+ )
301319
302320def _completion_data (self ,proposal ):
303321return proposal
@@ -317,7 +335,7 @@ def _extended_completion(self, proposal):
317335
318336if proposal .scope == 'parameter_keyword' :
319337default = proposal .get_default ()
320- ci ["menu" ]+= '*' if default is None else '=%s' % default
338+ ci ["menu" ]+= '*' if default is None else '={0}' . format ( default )
321339
322340if self .preview and not ci ['menu' ]:
323341doc = proposal .get_doc ()
@@ -328,9 +346,9 @@ def _extended_completion(self, proposal):
328346def _conv (self ,obj ):
329347if isinstance (obj ,dict ):
330348return u'{' + u',' .join ([
331- u"%s:%s" % (self ._conv (key ),self ._conv (value ))
349+ u"{0}:{1}" . format (self ._conv (key ),self ._conv (value ))
332350for key ,value in obj .iteritems ()])+ u'}'
333- return u'"%s"' % str (obj ).replace (u'"' ,u'\\ "' )
351+ return u'"{0}"' . format ( str (obj ).replace (u'"' ,u'\\ "' ) )
334352
335353
336354def _vim_name (name ):
@@ -344,31 +362,38 @@ class VimProgress(object):
344362def __init__ (self ,name ):
345363self .name = name
346364self .last = 0
347- status ('%s ... ' % self .name )
365+ status ('{0} ... ' . format ( self .name ) )
348366
349367def update (self ,percent ):
350368try :
351369vim .eval ('getchar(0)' )
352370except vim .error :
353- raise KeyboardInterrupt ('Task %s was interrupted!' % self .name )
371+ raise KeyboardInterrupt (
372+ 'Task {0} was interrupted!' .format (self .name )
373+ )
354374if percent > self .last + 4 :
355- status ('%s ...%s%%%%' % (self .name ,percent ))
375+ status ('{0} ...{1}%' . format (self .name ,percent ))
356376self .last = percent
357377
358378def done (self ):
359- status ('%s ... done' % self .name )
379+ status ('{0} ... done' . format ( self .name ) )
360380
361381
362382def echo (message ):
383+ if _rope_quiet :
384+ return
363385if isinstance (message ,unicode ):
364386message = message .encode (vim .eval ('&encoding' ))
365387print message
366388
367389
368390def status (message ):
391+ if _rope_quiet :
392+ return
393+
369394if isinstance (message ,unicode ):
370395message = message .encode (vim .eval ('&encoding' ))
371- vim .command ('redraw | echon "%s"' % message )
396+ vim .command ('redraw | echon "{0}"' . format ( message ) )
372397
373398
374399def call (command ):
@@ -395,13 +420,37 @@ def __call__(self, arg_lead, cmd_line, cursor_pos):
395420else :
396421result = [proposal for proposal in self .values
397422if proposal .startswith (arg_lead )]
398- vim .command ('let s:completions = %s' % result )
423+ vim .command ('let s:completions = {0}' .format (result ))
424+
425+
426+ class RopeMode (interface .RopeMode ):
427+ @decorators .global_command ('o' )
428+ def open_project (self ,root = None ,quiet = False ):
429+ global _rope_quiet
430+ _rope_quiet = quiet
431+
432+ super (RopeMode ,self ).open_project (root = root )
433+ rope_project_dir = os .path .join (self .project .address ,'.ropeproject' )
434+ vimfiles = glob .glob (os .path .join (rope_project_dir ,'*.vim' ))
435+
436+ if not vimfiles :
437+ return
438+
439+ txt = 'Sourcing vim files under\' .ropeproject/\' '
440+ progress = self .env .create_progress (txt )
441+ for idx ,vimfile in enumerate (sorted (vimfiles )):
442+ progress .name = txt + ' ({0})' .format (os .path .basename (vimfile ))
443+ vim .command (':silent source {0}' .format (vimfile ))
444+ progress .update (idx * 100 / len (vimfiles ))
399445
446+ progress .name = txt
447+ progress .done ()
448+ echo ('Project opened!' )
400449
401450decorators .logger .message = echo
402451decorators .logger .only_short = True
403452
404453_completer = _ValueCompleter ()
405454
406455_env = VimUtils ()
407- _interface = interface . RopeMode (env = _env )
456+ _interface = RopeMode (env = _env )