<job>
<!-- SciTE: (file must be saved)
file.patterns.jush=*.htm;*.html;*.php;*.sql;*.js;*.css;php.ini;*.conf;.htaccess;my.ini
command.1.$(file.patterns.jush)=WScript jush-help.wsf "$(FilePath)" $(SelectionStartLine) $(SelectionStartColumn) $(tabsize) "$(CurrentWord)"
command.name.1.$(file.patterns.jush)=JUSH help
command.subsystem.1.$(file.patterns.jush)=2
command.save.before.1.$(file.patterns.jush)=1
-->
<script language="JScript" src="jush.js"></script>
<script language="JScript">
function readFile(filename) {
	var fso = new ActiveXObject('Scripting.FileSystemObject');
	var file = fso.OpenTextFile(filename, 1);
	return file.ReadAll();
}

function openUrl(url) {
	var shell = new ActiveXObject('WScript.Shell');
	var command = shell.RegRead('HKCR\\http\\shell\\open\\command\\'); // `start` is not available
	shell.Exec(command.replace(/%1/g, url));
}

if (WScript.Arguments.length < 3) {
	WScript.Echo('Usage: WScript jush-help.wsf filename line column [tabsize] [word]\nPurpose: Open URL with help on specified position in file');
	WScript.Quit(1);
}
var filename = WScript.Arguments(0);
var line = WScript.Arguments(1);
var column = WScript.Arguments(2);
var lang = 'htm';
var match = /\.(js|sql|xml|css)$/.exec(filename);
if (match) {
	lang = match[1];
} else if (/php\.ini$/.test(filename)) {
	lang = 'phpini';
} else if (/my\.ini$/.test(filename)) {
	lang = 'sqlset';
} else if (/(\.conf|\\\.htaccess)$/.test(filename)) {
	lang = 'cnf';
}
var file = (new RegExp('^(.*\n){' + (line - 1) + '}.*', '')).exec(readFile(filename).replace(/\r/g, ''))[0]; // highlight only first lines of file (performance)
var s = /(.*)$/.exec(jush.highlight(lang, file))[1]; // get last line of output
if (WScript.Arguments.length >= 3) {
	var tab = '';
	for (var i = WScript.Arguments(3); i--; ) {
		tab += ' ';
	}
	s = s.replace(/\t/g, tab);
}
s = s.replace(/&[^;]+;/g, '&');
var href = '';
var pos = 1;
var re = /<a href="([^"]+)" class="jush-help">|(<\/a>)|<[^>]+>|([^<]+)/g;
while (match = re.exec(s)) {
	if (match[1]) {
		href = match[1];
	} else if (match[2]) {
		if (pos == column) { // last character of link
			break;
		}
		href = '';
	} else if (match[3]) {
		pos += match[3].length;
		if (pos > column) {
			break;
		}
	}
}
openUrl(href ? href : 'https://www.google.com/search?q=' + encodeURIComponent(WScript.Arguments.length >= 4 ? WScript.Arguments(4) : (match ? match[3] : '')));
</script>
</job>
