Skip to content

Instantly share code, notes, and snippets.

@hironaeee
Last active December 7, 2020 12:39
Show Gist options
  • Save hironaeee/27dd083b17d16411d4c5ddb367a75021 to your computer and use it in GitHub Desktop.
Save hironaeee/27dd083b17d16411d4c5ddb367a75021 to your computer and use it in GitHub Desktop.
sakura-vim files
// Lib.js
(function() {	
	var Lib = {};
	var edt = Editor;
	var NORMAL = 'Normal';
	var VISUAL = 'Visual';
	var SELECT = 'Select';
	var INSERT = 'Insert';
	var CMDLINE = 'Cmdline';
	var EX = 'Ex';
	var TERMINAL = 'Terminal';
	
	Lib.getMode = function(mode_ch) {
		try {
			var url = "http://localhost:3991/mode/";
			if (typeof mode_ch !== 'undefined') {
				url += mode_ch;
			}
			var http = new ActiveXObject("Msxml2.ServerXMLHTTP");
			http.open("GET", url, false);
			http.send();
			return http.responseText;
		} catch (e) {
			return "Error(" + (e.number & 0xFFFF) + "):" + e.message;
		}
	}
	Lib.A = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.GoLineEnd(0);
				this.getMode("i");
				break;
			default:
				edt.Char(65);
		}
	}
	Lib.G = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.GoFileEnd( );
				break;
			default:
				edt.Char(71);
		}
	}
	Lib.N = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.SearchPrev();
				break;
			default:
				edt.Char(78);
		}
	}
	Lib.O = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.Up(0);
				edt.GoLineEnd(0);
				edt.Char(13);
				this.getMode("i");
				break;
			default:
				edt.Char(79);
		}
	}
	Lib.a = function(mode) {
		switch(mode) {
			case NORMAL:
				if (!isEnd()) edt.Right(0);
				this.getMode("i");
				break;
			default:
				edt.Char(97);
		}
	}
	Lib.b = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.WordLeft(0);
				break;
			default:
				edt.Char(98);
		}
	}
	Lib.g = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.GoFileTop( );
				break;
			default:
				edt.Char(103);
		}
	}
	Lib.h = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.Left(0);
				break;
			default:
				edt.Char(104);
		}
	}
	Lib.i = function(mode) {
		switch(mode) {
			case NORMAL:
				this.getMode("i");
				break;
			default:
				edt.Char(105);
		}
	}
	Lib.j = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.Down(0);
				break;
			default:
				edt.Char(106);
		}
	}
	Lib.k = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.Up(0);
				break;
			default:
				edt.Char(107);
		}
	}
	Lib.l = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.Right(0);
				break;
			default:
				edt.Char(108);
		}
	}
	Lib.n = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.SearchNext();
				break;
			default:
				edt.Char(110);
		}
	}
	Lib.o = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.GoLineEnd(0);
				edt.Char(13);
				this.getMode("i");
				break;
			default:
				edt.Char(111);
		}
	}
	Lib.w = function(mode) {
		switch(mode) {
			case NORMAL:
				edt.WordRight(0);
				break;
			default:
				edt.Char(119);
		}
	}
	Lib.esc = function(mode) {
		switch(mode) {
			case INSERT:
				this.getMode("n");
				break;
			default:
				edt.CancelMode(0);
		}
	}
	function isEnd() {
		edt.Right_Sel(0);
		var rightStr = edt.GetSelectedString(0);
		edt.CancelMode(0);
		edt.Left(0);
		return rightStr === '\r\n' || rightStr === '\n';
	}
	return Lib;
})();
// macro files
function include(filename) {
    var fso = new ActiveXObject('Scripting.FileSystemObject');
	// $I -> setting file path
	var app_dir = fso.GetParentFolderName(ExpandParameter('$I'));
	var filepath = app_dir + '\\' + filename;
    var src = fso.OpenTextFile(filepath, 1).ReadAll();
    return eval(src);
}

var lib = include('macro/Lib.js');

// $M -> executing macro filename
var macro_filename = ExpandParameter('$M').split('\\').slice(-1).toString();
var func = macro_filename.split('.')[0];
// remove UPPERCASE_'s underscore (workaround: windows cannot distinguish upper and lower cases)
if (func.match('^[A-Z]_')) {
	func = func.replace('_', '');
}

var mode = lib.getMode();
var exec_str = 'lib.' + func + '(mode);';
eval(exec_str);
// server
import System;

const listener = new System.Net.HttpListener();

// ②リスナー各種設定

listener.Prefixes.Clear();

listener.Prefixes.Add("http://*:3991/mode/");

listener.Realm = System.Net.Common.ListenerRealm;

const modes = {
	n: "Normal",
	v: "Visual",
	s: "Select",
	i: "Insert",
	c: "Cmdline",
	e: "Ex",
	t: "Terminal"
};
// Initialize mode to 'Normal'
var mode = modes["n"];

listener.Start();

while (true) {
	var context = listener.GetContext();
	var req = context.Request;
	var res = context.Response;
	
	var reqStr = req.RawUrl;
	if (!reqStr.match('mode/?$')) {
		var urlItems = reqStr.split('/');
		var key = urlItems.slice(-1);
		if (key in modes) {
			mode = modes[key];
		}
	}
	
	res.StatusCode = 200;
		
	var responseString = mode;
    var buffer = System.Text.Encoding.UTF8.GetBytes(responseString);
    res.ContentLength64 = buffer.Length;
    var output = res.OutputStream;
    output.Write(buffer, 0, buffer.Length);
    output.Close();
	
	res.Close();
}

listener.Close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment