/*
phpman.vvs Ver 0.002 Copyright 2007 by Tsuda, Nobuhide
Created: 07/06/24
Author: Tsuda, Nobuhide
Description: 選択された文字列 or カーソル位置単語を PHP マニュアル検索

本プログラムのライセンスは MIT License (http://www.opensource.jp/licenses/mit-license.html) に従う。
MIT License とは、要約すると以下のとおりである。
   1. このソフトウェアを誰でも無償で無制限に扱って良い。但し、著作権表示および本許諾表示を、
      ソフトウェアのすべての複製または重要な部分に記載しなければならない。
   2. 作者または著作権者は、ソフトウェアに関してなんら責任を負わない。


Ver 0.002 log ファイルを残すよう修正 08/04/25

*/
//選択された文字列 or カーソル位置単語をGET
function getWord()
{
	if( !thisView.isSelected() )
		thisView.viCommand("vv");	//	非選択状態の場合はカーソル位置単語を選択
	var Range = thisView.getSelectedRange();
	var LineStr = thisView.getLineString(Range.line1);
	var SearchWord = LineStr.substring(Range.offset1,Range.offset2);
	if (thisView.isSelected())
		thisView.clearSelected();	//範囲選択を解除
	return SearchWord;
}

#include	"file.vvs"

//	検索ログファイルを更新
function updateLogFile(word)
{
	$vvsDir = globalSettings.getTextValue( "viviScriptDir" );
	$logFileName = $vvsDir + "/phpman.log";
	$sf = stdioFile();
	if( !$sf.open($logFileName, modeCreate |modeNoTruncate | modeWrite | typeText) ) {
		messageBox($logFileName + " をオープンできませんでした。");
		return;
	}
	$sf.seek(0, 2);		//	ファイル末尾に移動
	$sf.writeString(currentTime().strftime("%Y/%m/%d %H:%M:%S") + "\t" + word + "\n");
	$sf.close();
}

function uriEncode( str )
{
	var encoded = "";
	for ( var i = 0; i < str.length(); i++ ) {
		encoded += ( "%" + format("%02X", str.charCode(i)) );
	}
	return encoded;
}

function main()
{
	var word = getWord();
	if( word == "" ) return;
	updateLogFile(word);
	var phpSite = "http://php.s3.to/namazu/index.php";
	var URL = phpSite + "?query=" + uriEncode(word);
	thisView.viCommand(":openURL " + URL + "\n");
}