コンピュータや音楽の事書いてます

メモ:秀丸でincludeファイル単語検索

  • カーソル行にある単語を、includeしてるファイルから検索してその場所にジャンプする
//includeされているファイルから検索する
seterrormode 1,0;
$search_path[0] = ".";
$search_path[1] = ".\\include";
$search_path[2] = "..\\include";
$search_path[3] = "..\..\include";
$search_path[4] = "..";
#thisdoc = hidemaruhandle(0);
#cursorx = x;
#cursory = y;

searchup "[^a-zA-Z0-9_]", regular; //単語の先頭の手前の文字
right;
#startx = x;
#starty = y;

searchdown "[^a-zA-Z0-9_]", regular; //単語の末尾の次の文字
#endx = x;
#endy = y;

$wordFind = gettext(#startx, #starty, #endx, #endy);



call search_include;

goto end_macro;

search_include:
	gofiletop; ##i=0;
	while(1){
		searchdown "#include";
		if(!result)break; //includeリストアップ終了
		searchdown "[\"<]", regular;
		right;
		##startx = x;
		##starty = y;
		searchdown "[\">]", regular;
		##endx = x;
		##endy = y;
		call replace_slash gettext(##startx, ##starty, ##endx, ##endy);
		$headerlist[##i] = $$return;
		##i=##i+1;
	}
	#max_count = ##i;
	moveto #cursorx, #cursory;
	call search_headers;
	return;

search_headers:
	##i=0;
	while(##i < #max_count){
		//まずカレントディレクトリ
		if(existfile($headerlist[##i])){
			readonlyopenfile $headerlist[##i];
			gofiletop;
			call search_word;
			call close_doc;
		} else {
			//その他のディレクトリ
			call search_dir $headerlist[##i];
			if($$return != "") {
				readonlyopenfile $$return;
				if(result){ 
					gofiletop; 
					call search_word; 
					call close_doc;
				}
			}
		}
		##i=##i+1;
	}
	//******* ここからあやしい ******* 
	##i=0;
	while(##i < #max_count){
		//まずカレントディレクトリ
		if(existfile($headerlist[##i])){
			readonlyopenfile $headerlist[##i];
			gofiletop;
			call search_include; //再帰//
			call close_doc;
		} else {
			//その他のディレクトリ
			call search_dir $headerlist[##i];
			if($$return != "") {
				readonlyopenfile $$return;
				if(result){ 
					gofiletop; 
					call search_include; //再帰//
					call close_doc;
				}
			}
		}
		##i=##i+1;
	}
	
	return;

search_word:
	searchdown $wordFind;
	if(result){ endmacro; } //成功したら終わり
	return;

search_dir: //配下を全部探すのやめた方が良い???
	if($$1 == "")return "";
	##i = 0;
	while($search_path[##i] != ""){
		$thedir = $search_path[##i];
		if(strstr($thedir, ".h") != -1)return $thedir; //ファイル名の場合はそのまま
		runex "cmd /c dir /s /b " + $thedir + "\\" + $$1, 1, 0, "", 4;
		if(y > 0){ //何かでた
			gofiletop;
			$$filename = gettext2(0, lineno, linelen2, lineno); //最初に見つかったフルパスファイル名
			call close_doc;
			break;
		}
		call close_doc;
		##i=##i+1;
	}

	return $$filename;
replace_slash:
	while(1){
		##fx = strstr($$1, "/");
		if(##fx == -1)break;
		$$1 = leftstr($$1, ##fx) + "\\" + midstr($$1, ##fx + 2);
		//message $$1;
	}
	return $$1;

close_doc:
	##closedoc = hidemaruhandle(0);
	setactivehidemaru #thisdoc;
	closehidemaru ##closedoc;
	return;

end_macro:
setactivehidemaru #thisdoc;


endmacro;