Created
September 13, 2014 14:56
-
-
Save tyru/83b3c338e7358c366b06 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<job id="run-vimgrep"> | |
<script language="JavaScript"> | |
var fs = new ActiveXObject("Scripting.FileSystemObject"); | |
var sh = new ActiveXObject("WScript.Shell"); | |
// オープンモード | |
var FORREADING = 1; // 読み取り専用 | |
var FORWRITING = 2; // 書き込み専用 | |
var FORAPPENDING = 8; // 追加書き込み | |
// 開くファイルの形式 | |
var TRISTATE_TRUE = -1; // Unicode | |
var TRISTATE_FALSE = 0; // ASCII | |
var TRISTATE_USEDEFAULT = -2; // システムデフォルト | |
var DEBUG = false; | |
function createTempFile() { | |
var dir = fs.GetSpecialFolder(2); | |
var file = fs.GetTempName(); | |
return dir + "\\" + file; | |
} | |
function debug(msg) { | |
if (DEBUG) { | |
WScript.Echo(msg); | |
} | |
} | |
function getServerList() { | |
var serverListProc = sh.Exec('vim --serverlist'); | |
var serverList = []; | |
while (! serverListProc.StdOut.AtEndOfStream) { | |
serverList.push(serverListProc.StdOut.ReadLine()); | |
} | |
debug(serverList); | |
return serverList; | |
} | |
function createScriptFile(doTabedit) { | |
var scriptFile = createTempFile(); | |
debug(scriptFile); | |
var file = fs.OpenTextFile(scriptFile, FORWRITING, true, TRISTATE_FALSE); | |
if (doTabedit) { | |
file.WriteLine("tabedit"); | |
} | |
file.WriteLine("cd " + WScript.Arguments(0)); | |
// FIXME: 文字化けする | |
// file.WriteLine("setlocal fenc=cp932 ff=dos buftype=nofile"); | |
// file.WriteLine("call setline(1, ['以下のフォルダを検索します。検索語を入力してください。', getcwd()])"); | |
file.WriteLine("call feedkeys(\"\\<Esc>:vimgrep // **/*\\<Left>\\<Left>\\<Left>\\<Left>\\<Left>\\<Left>\")"); | |
file.Close(); | |
} | |
// =================== Main =================== // | |
if (WScript.Arguments.length === 0) { | |
WScript.Echo("エラー:引数を指定してください。"); | |
WScript.Quit(-1); | |
} | |
var serverList = getServerList(); | |
var doTabedit = serverList.length !== 0 | |
var scriptFile = createScriptFile(doTabedit); | |
var sendProc; | |
if (serverList.length !== 0) { | |
// すでにVimが起動していたらコマンド列を送る | |
sendProc = sh.Exec('gvim --servername ' + serverList[0] + ' --remote-send "<C-\\><C-g>:so ' + scriptFile + '<CR>"'); | |
} | |
else { | |
// 起動しているVimがいなかったら新規に起動する | |
sendProc = sh.Exec('gvim -S ' + scriptFile); | |
} | |
while (sendProc.Status === 0) { | |
WScript.Sleep(100); | |
} | |
fs.DeleteFile(scriptFile, true); | |
fs = sh = null; | |
debug("終了"); | |
</script> | |
</job> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment