From b22f20cbb3bf38a8cdaeb6685013764365f08c56 Mon Sep 17 00:00:00 2001
From: grirgz <grirgz@gmail.com>
Date: Tue, 7 Aug 2018 22:43:46 +0200
Subject: [PATCH 1/3] add .loadRelative support

---
 sc/SCVim.sc | 31 ++++++++++++++++++++++++++++++-
 1 file changed, 30 insertions(+), 1 deletion(-)

diff --git a/sc/SCVim.sc b/sc/SCVim.sc
index 29f9ac5..da9dbe7 100644
--- a/sc/SCVim.sc
+++ b/sc/SCVim.sc
@@ -23,8 +23,24 @@
 SCVim.generateTagsFile();
 */
 
+Document {
+    // needed for thisProcess.nowExecutingPath to work.. see Kernel::interpretCmdLine
+    var <path, <dataptr;
+    *new {|path, dataptr|
+        ^super.newCopyArgs(path, dataptr);
+    }
+    *current {
+        var path = SCVim.currentPath;
+        ^Document(path, true);
+    }
+	*dir {
+		^SCVim.currentPath
+	}
+}
+
+
 SCVim {
-	classvar nodes, <>vimPath;
+	classvar nodes, <>vimPath, <>vimServerName="scvim";
 
 	*initClass {
 		nodes = List[];
@@ -221,4 +237,17 @@ SCVim {
 		tagfile.close();
 		"finished generating tagsfile".postln;
 	}
+
+    *currentPath {
+        var cmd = "expand(\"%:p\")";
+        var path = "vim --servername % --remote-expr '%'".format(vimServerName, cmd).unixCmdGetStdOut;
+		if( path == "" ) {
+			//"can't get Vim current path".error; // also happen on unsaved files, but there is already an error message with .loadRelative
+			^nil
+		};
+        if (PathName(path).isAbsolutePath) {
+            ^path;
+        };
+        ^nil;
+    }
 } // end class

From c43bd7d328d034a868f0d8e5e811764f8588ed69 Mon Sep 17 00:00:00 2001
From: grirgz <grirgz@gmail.com>
Date: Wed, 16 Jan 2019 19:02:05 +0100
Subject: [PATCH 2/3] add check for vim executable, clientserver feature and
 available servers

---
 sc/SCVim.sc | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)

diff --git a/sc/SCVim.sc b/sc/SCVim.sc
index da9dbe7..65837e4 100644
--- a/sc/SCVim.sc
+++ b/sc/SCVim.sc
@@ -40,12 +40,13 @@ Document {
 
 
 SCVim {
-	classvar nodes, <>vimPath, <>vimServerName="scvim";
+	classvar nodes, <>vimPath, <>vimServerEnabled=false, <>vimServerName="SCVIM";
 
 	*initClass {
 		nodes = List[];
 
 		// TODO this has to be not so mac-centric
+		vimPath = "vim";
 		Platform.case(\osx) {
 
 			var whichVim = "which mvim".unixCmdGetStdOut;
@@ -57,6 +58,10 @@ SCVim {
 			vimPath = vimPath.replace("\n", "");
 		};
 
+		vimServerEnabled = "% --version".format(vimPath).unixCmdGetStdOut.contains("+clientserver") and: {
+			"% --serverlist".format(vimPath).unixCmdGetStdOut.contains(vimServerName.toUpper)
+		};
+
 		StartUp.add {
 			var classList, file, hugeString = "syn keyword scObject", basePath;
 			// search two folders deep below ~/.vim for a folder named "*scvim*"
@@ -240,14 +245,17 @@ SCVim {
 
     *currentPath {
         var cmd = "expand(\"%:p\")";
-        var path = "vim --servername % --remote-expr '%'".format(vimServerName, cmd).unixCmdGetStdOut;
-		if( path == "" ) {
-			//"can't get Vim current path".error; // also happen on unsaved files, but there is already an error message with .loadRelative
-			^nil
-		};
-        if (PathName(path).isAbsolutePath) {
-            ^path;
-        };
+        var path;
+		if(vimServerEnabled) {
+			path = "% --servername % --remote-expr '%'".format(vimPath, vimServerName, cmd).unixCmdGetStdOut;
+			if( path == "" ) {
+				//"can't get Vim current path".error; // also happen on unsaved files, but there is already an error message with .loadRelative
+				^nil
+			};
+			if (PathName(path).isAbsolutePath) {
+				^path;
+			};
+		}
         ^nil;
     }
 } // end class

From bd4f41308885e3c4f2a912cf878bc942cc60c26d Mon Sep 17 00:00:00 2001
From: grirgz <grirgz@gmail.com>
Date: Wed, 5 Jun 2019 17:21:48 +0200
Subject: [PATCH 3/3] server name can be changed, now warn if not found, add
 doc

---
 README.md   | 17 +++++++++++++++
 sc/SCVim.sc | 62 +++++++++++++++++++++++++++++++++--------------------
 2 files changed, 56 insertions(+), 23 deletions(-)

diff --git a/README.md b/README.md
index 84de8ba..57d2ef1 100644
--- a/README.md
+++ b/README.md
@@ -82,6 +82,22 @@ If for some reason vim can't find the path to the two launch scripts
     let g:sclangPipeApp     = "~/.vim/bundle/scvim/bin/start_pipe"
     let g:sclangDispatcher  = "~/.vim/bundle/scvim/bin/sc_dispatcher"
 
+### Document support
+
+If vim is compiled with +clientserver option, SuperCollider can retrieve the path of the current vim buffer and enable String:loadRelative.
+In debian based distributions, this option often comes with gui-enabled vim packages (vim-gtk for example).
+
+    "fileInSameFolder.scd".loadRelative;
+
+To use this feature, you also need to start vim in server mode:
+
+    vim --servername SCVIM 
+
+You can change the default server name used by SuperCollider to contact vim:
+
+    SCVim.vimServerName = "MYNAME";
+
+
 Usage
 -----
 To start open a file with the right extension :e foo.sc(d)
@@ -119,3 +135,4 @@ in normal/insert mode:
 * `F5` to execute a block of code scvim will attempt to find the outermost bracket
 * `F6` to execute the current line of code
 * `F12` is a hard stop
+
diff --git a/sc/SCVim.sc b/sc/SCVim.sc
index 65837e4..cc39121 100644
--- a/sc/SCVim.sc
+++ b/sc/SCVim.sc
@@ -24,15 +24,15 @@ SCVim.generateTagsFile();
 */
 
 Document {
-    // needed for thisProcess.nowExecutingPath to work.. see Kernel::interpretCmdLine
-    var <path, <dataptr;
-    *new {|path, dataptr|
-        ^super.newCopyArgs(path, dataptr);
-    }
-    *current {
-        var path = SCVim.currentPath;
-        ^Document(path, true);
-    }
+	// needed for thisProcess.nowExecutingPath to work.. see Kernel::interpretCmdLine
+	var <path, <dataptr;
+	*new {|path, dataptr|
+		^super.newCopyArgs(path, dataptr);
+	}
+	*current {
+		var path = SCVim.currentPath;
+		^Document(path, true);
+	}
 	*dir {
 		^SCVim.currentPath
 	}
@@ -40,7 +40,7 @@ Document {
 
 
 SCVim {
-	classvar nodes, <>vimPath, <>vimServerEnabled=false, <>vimServerName="SCVIM";
+	classvar nodes, <>vimPath, <>vimServerEnabled=false, <vimServerName;
 
 	*initClass {
 		nodes = List[];
@@ -58,9 +58,7 @@ SCVim {
 			vimPath = vimPath.replace("\n", "");
 		};
 
-		vimServerEnabled = "% --version".format(vimPath).unixCmdGetStdOut.contains("+clientserver") and: {
-			"% --serverlist".format(vimPath).unixCmdGetStdOut.contains(vimServerName.toUpper)
-		};
+		this.vimServerName = "SCVIM";
 
 		StartUp.add {
 			var classList, file, hugeString = "syn keyword scObject", basePath;
@@ -94,6 +92,24 @@ SCVim {
 		};
 	}
 
+	*vimServerName_ { arg name;
+		var clientServerEnabled, serverNameAvailable;
+		vimServerName = name;
+		clientServerEnabled = "% --version".format(vimPath).unixCmdGetStdOut.contains("+clientserver");
+		serverNameAvailable = "% --serverlist".format(vimPath).unixCmdGetStdOut.contains(vimServerName.toUpper);
+		if(clientServerEnabled) {
+			if(serverNameAvailable) {
+				vimServerEnabled = true;
+			} {
+				"No vim sesssion with server name % found: SCVim.currentPath will not work.".format(vimServerName.toUpper).warn;
+				vimServerEnabled = false;
+			}
+		} {
+			"vim is not compiled with +clientserver option: SCVim.currentPath will not work.".warn;
+			vimServerEnabled = false;
+		};
+	}
+
 	// DEPRECTATED in favor of tags system
 	*openClass{ |klass|
 		// TM version only
@@ -137,7 +153,7 @@ SCVim {
 
 					found = found + 1;
 					namestring = class.name ++ ":" ++ name;
-					out << "   " << namestring << " :     ";
+					out << "   " << namestring << " :	  ";
 					if (method.argNames.isNil or: { method.argNames.size == 1 }, {
 						out << "this." << name;
 						if (name.isSetter, { out << "(val)"; });
@@ -185,7 +201,7 @@ SCVim {
 
 		if (references.notNil, {
 			out << "References to '" << name << "' :\n";
-			references.do({ arg ref; out << "   " << ref.asString << "\n"; });
+			references.do({ arg ref; out << "	" << ref.asString << "\n"; });
 
 			if(openInVIM) {
 				fname = "/tmp/" ++ Date.seed ++ ".sc";
@@ -221,15 +237,15 @@ SCVim {
 			arg klass;
 			var klassName, klassFilename, klassSearchString;
 
-			klassName         = klass.asString;
-			klassFilename     = klass.filenameSymbol;
+			klassName		  = klass.asString;
+			klassFilename	  = klass.filenameSymbol;
 			klassSearchString = format("/^%/;\"", klassName);
 
 			tagfile.write(klassName ++ Char.tab ++ klassFilename ++ Char.tab ++ klassSearchString ++ Char.nl);
 
 			klass.methods.do{|meth|
 				var methName, methFilename, methSearchString;
-				methName     = meth.name;
+				methName	 = meth.name;
 				methFilename = meth.filenameSymbol;
 				// this strange fandango dance is necessary for sc to not complain
 				// when compiling 123 is the curly bracket
@@ -243,9 +259,9 @@ SCVim {
 		"finished generating tagsfile".postln;
 	}
 
-    *currentPath {
-        var cmd = "expand(\"%:p\")";
-        var path;
+	*currentPath {
+		var cmd = "expand(\"%:p\")";
+		var path;
 		if(vimServerEnabled) {
 			path = "% --servername % --remote-expr '%'".format(vimPath, vimServerName, cmd).unixCmdGetStdOut;
 			if( path == "" ) {
@@ -256,6 +272,6 @@ SCVim {
 				^path;
 			};
 		}
-        ^nil;
-    }
+		^nil;
+	}
 } // end class