topfunky / PeepOpen-Issues

Bug reports and feature requests for the PeepOpen application
http://peepcode.com/products/peepopen
41 stars 1 forks source link

[PATCH] MacVim Tab support #259

Open borgand opened 12 years ago

borgand commented 12 years ago

Enable PeepOpen to use MacVim tabs even if MacVim is configured to open files from other apps in new windows. It first detects if the editor is MacVim and then executes the MacVim with --remote-tab-silent argument.

diff --git a/Classes/Controllers/FuzzyTableViewController.rb b/Classes/Controllers/FuzzyTableViewController.rb
index a8c015b..8b407f2 100755
--- a/Classes/Controllers/FuzzyTableViewController.rb
+++ b/Classes/Controllers/FuzzyTableViewController.rb
@@ -152,8 +152,15 @@ class FuzzyTableViewController
         return false
       end

-      NSWorkspace.sharedWorkspace.openFile(record.absFilePath,
+      workspace = NSWorkspace.sharedWorkspace
+      if editorApplicationName == "MacVim"
+        vim = File.join(workspace.fullPathForApplication("MacVim"), "Contents/MacOS/Vim")
+        system vim, "--remote-tab-silent", record.absFilePath
+      else
+        workspace.openFile(record.absFilePath,
                                            withApplication:editorApplicationName)
+      end
+      

       # Reset for next search
       searchForString("")
borgand commented 12 years ago

Unfortunately the above patch mixes up Vim windows (as it has no clue where PeepOpen was launched from). Fortunately Vim has a feature to overcome this using servernames. Each Vim process can be named (and is automatically) named uniquely and from cmd line the --servername <name> swhich can be used to select between these.

So the below patch now makes the following changes:

Altogether PeepOpen now is aware of current Vim window/server name and opens all files in the same window as new tabs.

The patch:

diff --git a/Classes/Controllers/AppDelegate.rb b/Classes/Controllers/AppDelegate.rb
index 014cc83..44218f4 100755
--- a/Classes/Controllers/AppDelegate.rb
+++ b/Classes/Controllers/AppDelegate.rb
@@ -34,11 +34,17 @@ class AppDelegate

       if customUrl.query
         # Don't try to gsub unless there is a query to work with.
-        editorName = customUrl.query.gsub('editor=', '')
+        
+        # Parse query for all params
+        queryParams = {}
+        customUrl.query.split(/&/).map{|p| k,v=p.split(/=/); queryParams[k] = v}
+        
+        editorName = queryParams.delete('editor')

         # Save the editor name to a SessionConfig object so we can pluck it out of the air later
         # (see FuzzyTableViewController.handleRowClick)
         @sessionConfig.editorName = editorName
+        @sessionConfig.editorParams = queryParams
       end

       application(nil, openFile:customUrl.path)
diff --git a/Classes/Controllers/FuzzyTableViewController.rb b/Classes/Controllers/FuzzyTableViewController.rb
index a8c015b..5685e60 100755
--- a/Classes/Controllers/FuzzyTableViewController.rb
+++ b/Classes/Controllers/FuzzyTableViewController.rb
@@ -142,6 +142,7 @@ class FuzzyTableViewController
       FuzzyRecord.storeRecentlyOpenedRecord(record)

       editorApplicationName = NSApp.delegate.sessionConfig.editorName
+      editorParams = NSApp.delegate.sessionConfig.editorParams
       if editorApplicationName.empty?
         editorApplicationName =
           NSUserDefaults.standardUserDefaults.stringForKey('editorApplicationName')
@@ -152,8 +153,28 @@ class FuzzyTableViewController
         return false
       end

-      NSWorkspace.sharedWorkspace.openFile(record.absFilePath,
+      workspace = NSWorkspace.sharedWorkspace
+      if editorApplicationName == "MacVim"
+        # Get the location of MacVim executable
+        vim_cmd = [ File.join(workspace.fullPathForApplication("MacVim"), "Contents/MacOS/Vim") ]
+        
+        # If given additional arguments, add them too
+        if editorParams
+          editorParams.each do |name,value|
+            vim_cmd << "--#{name}" << value
+          end
+        end
+        
+        # Add default arguments and remote-tab switch
+        vim_cmd << "-g" << "--remote-tab-silent" << record.absFilePath
+        
+        # Execute the command
+        system *vim_cmd
+      else
+        workspace.openFile(record.absFilePath,
                                            withApplication:editorApplicationName)
+      end
+      

       # Reset for next search
       searchForString("")
diff --git a/Classes/Helpers/SessionConfig.rb b/Classes/Helpers/SessionConfig.rb
index b3282ef..78583c0 100644
--- a/Classes/Helpers/SessionConfig.rb
+++ b/Classes/Helpers/SessionConfig.rb
@@ -8,6 +8,7 @@
 class SessionConfig

   attr_accessor :editorName
+  attr_accessor :editorParams

  def initialize(editorName)
     @editorName = editorName
diff --git a/Support/vim-peepopen/plugin/peepopen.vim b/Support/vim-peepopen/plugin/peepopen.vim
index 6ceab7c..d24fedc 100644
--- a/Support/vim-peepopen/plugin/peepopen.vim
+++ b/Support/vim-peepopen/plugin/peepopen.vim
@@ -26,7 +26,7 @@ set cpo&vim

 function s:LaunchPeepOpenViaVim()
   let cwd = getcwd()
-  silent exe "!open \"peepopen://" . shellescape(cwd) . "?editor=MacVim\""
+  silent exe "!open \"peepopen://" . shellescape(cwd) . "?editor=MacVim&servername=" . v:servername . "\""
 endfunction

 command! PeepOpen :call <SID>LaunchPeepOpenViaVim()