@@ -22,7 +22,7 @@ class FileDiffMenuCommand(sublime_plugin.TextCommand):
2222 def settings (self ):
2323 return sublime .load_settings ('FileDiffs.sublime-settings' )
2424
25- def run (self , edit ):
25+ def run (self , edit , cmd = None ):
2626 menu_items = self .FILE_DIFFS [:]
2727 saved = self .SAVED
2828 non_empty_regions = [region for region in self .view .sel () if not region .empty ()]
@@ -40,15 +40,15 @@ def on_done(index):
4040 if index == - 1 :
4141 return
4242 elif restored_menu_items [index ] == self .CLIPBOARD :
43- self .view .run_command ('file_diff_clipboard' )
43+ self .view .run_command ('file_diff_clipboard' , { 'cmd' : cmd } )
4444 elif restored_menu_items [index ] == self .SELECTIONS :
45- self .view .run_command ('file_diff_selections' )
45+ self .view .run_command ('file_diff_selections' , { 'cmd' : cmd } )
4646 elif restored_menu_items [index ] == self .SAVED :
47- self .view .run_command ('file_diff_saved' )
47+ self .view .run_command ('file_diff_saved' , { 'cmd' : cmd } )
4848 elif restored_menu_items [index ] == self .FILE :
49- self .view .run_command ('file_diff_file' )
49+ self .view .run_command ('file_diff_file' , { 'cmd' : cmd } )
5050 elif restored_menu_items [index ] == self .TAB :
51- self .view .run_command ('file_diff_tab' )
51+ self .view .run_command ('file_diff_tab' , { 'cmd' : cmd } )
5252 self .view .window ().show_quick_panel (menu_items , on_done )
5353
5454
@@ -68,7 +68,7 @@ def diff_content(self):
6868 content = self .view .substr (sublime .Region (0 , self .view .size ()))
6969 return content
7070
71- def run_diff (self , a , b , from_file = None , to_file = None ):
71+ def run_diff (self , a , b , from_file , to_file , external_diff_tool ):
7272 from_content = a
7373 to_content = b
7474
@@ -109,28 +109,38 @@ def run_diff(self, a, b, from_file=None, to_file=None):
109109 if not diffs :
110110 sublime .status_message ('No Difference' )
111111 else :
112- external_command = self .settings ().get ('cmd' )
112+ external_command = external_diff_tool or self .settings ().get ('cmd' )
113113 open_in_sublime = self .settings ().get ('open_in_sublime' , not external_command )
114114
115115 if external_command :
116- self .diff_with_external (a , b , from_file , to_file )
116+ self .diff_with_external (external_command , a , b , from_file , to_file )
117117
118118 if open_in_sublime :
119119 # fix diffs
120120 diffs = map (lambda line : (line and line [- 1 ] == "\n " ) and line or line + "\n " , diffs )
121121 self .diff_in_sublime (diffs )
122122
123- def diff_with_external (self , a , b , from_file = None , to_file = None ):
123+ def diff_with_external (self , external_command , a , b , from_file = None , to_file = None ):
124124 try :
125- if not os .path .exists (from_file ):
125+ try :
126+ from_file_exists = os .path .exists (from_file )
127+ except ValueError :
128+ from_file_exists = False
129+
130+ try :
131+ to_file_exists = os .path .exists (to_file )
132+ except ValueError :
133+ to_file_exists = False
134+
135+ if not from_file_exists :
126136 tmp_file = tempfile .NamedTemporaryFile (delete = False )
127137 from_file = tmp_file .name
128138 tmp_file .close ()
129139
130140 with codecs .open (from_file , encoding = 'utf-8' , mode = 'w+' ) as tmp_file :
131141 tmp_file .write (a )
132142
133- if not os . path . exists ( to_file ) :
143+ if not to_file_exists :
134144 tmp_file = tempfile .NamedTemporaryFile (delete = False )
135145 to_file = tmp_file .name
136146 tmp_file .close ()
@@ -139,11 +149,9 @@ def diff_with_external(self, a, b, from_file=None, to_file=None):
139149 tmp_file .write (b )
140150
141151 if os .path .exists (from_file ):
142- command = self .settings ().get ('cmd' )
143- if command is not None :
144- command = [c .replace (u'$file1' , from_file ) for c in command ]
145- command = [c .replace (u'$file2' , to_file ) for c in command ]
146- self .view .window ().run_command ("exec" , {"cmd" : command })
152+ external_command = [c .replace (u'$file1' , from_file ) for c in external_command ]
153+ external_command = [c .replace (u'$file2' , to_file ) for c in external_command ]
154+ self .view .window ().run_command ("exec" , {"cmd" : external_command })
147155 except Exception as e :
148156 # some basic logging here, since we are cluttering the /tmp folder
149157 sublime .status_message (str (e ))
@@ -166,7 +174,8 @@ def run(self, edit, **kwargs):
166174 current = sublime .get_clipboard ()
167175 self .run_diff (self .diff_content (), current ,
168176 from_file = self .view .file_name (),
169- to_file = '(clipboard)' )
177+ to_file = '(clipboard)' ,
178+ external_diff_tool = kwargs .get ('cmd' , None ))
170179
171180
172181class FileDiffSelectionsCommand (FileDiffCommand ):
@@ -210,7 +219,8 @@ def run(self, edit, **kwargs):
210219
211220 self .run_diff (current , diff ,
212221 from_file = 'first selection' ,
213- to_file = 'second selection' )
222+ to_file = 'second selection' ,
223+ external_diff_tool = kwargs .get ('cmd' , None ))
214224
215225
216226class FileDiffSavedCommand (FileDiffCommand ):
@@ -225,7 +235,8 @@ def run(self, edit, **kwargs):
225235
226236 self .run_diff (self .view .file_name (), content ,
227237 from_file = self .view .file_name (),
228- to_file = self .view .file_name () + u' (Unsaved)' )
238+ to_file = self .view .file_name () + u' (Unsaved)' ,
239+ external_diff_tool = kwargs .get ('cmd' , None ))
229240
230241
231242class FileDiffFileCommand (FileDiffCommand ):
@@ -251,7 +262,9 @@ def run(self, edit, **kwargs):
251262 def on_done (index ):
252263 if index > - 1 :
253264 self .run_diff (self .diff_content (), files [index ],
254- from_file = self .view .file_name ())
265+ from_file = self .view .file_name (),
266+ to_file = files [index ],
267+ external_diff_tool = kwargs .get ('cmd' , None ))
255268 sublime .set_timeout (lambda : self .view .window ().show_quick_panel (file_picker , on_done ), 1 )
256269
257270 def find_files (self , folders ):
@@ -302,7 +315,8 @@ def on_done(index):
302315 if index > - 1 :
303316 self .run_diff (self .diff_content (), contents [index ],
304317 from_file = self .view .file_name (),
305- to_file = files [index ])
318+ to_file = files [index ],
319+ external_diff_tool = kwargs .get ('cmd' , None ))
306320
307321 if len (files ) == 1 :
308322 on_done (0 )
0 commit comments