Skip to content

add .loadRelative support #48

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,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)
Expand Down
65 changes: 59 additions & 6 deletions sc/SCVim.sc
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,30 @@
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, <>vimServerEnabled=false, <vimServerName;

*initClass {
nodes = List[];

// TODO this has to be not so mac-centric
vimPath = "vim";
Platform.case(\osx) {

var whichVim = "which mvim".unixCmdGetStdOut;
Expand All @@ -41,6 +58,8 @@ SCVim {
vimPath = vimPath.replace("\n", "");
};

this.vimServerName = "SCVIM";

StartUp.add {
var classList, file, hugeString = "syn keyword scObject", basePath;

Expand Down Expand Up @@ -83,6 +102,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
Expand Down Expand Up @@ -126,7 +163,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)"; });
Expand Down Expand Up @@ -174,7 +211,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";
Expand Down Expand Up @@ -210,15 +247,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
Expand All @@ -231,4 +268,20 @@ SCVim {
tagfile.close();
"finished generating tagsfile".postln;
}

*currentPath {
var cmd = "expand(\"%:p\")";
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