/*************************************************************************
*	ワードヘルプ
*************************************************************************/

char	WdHlpPath[128];
char	WdHlpFn[128];
char	WdHlpIdxFn[128];

#define	WDHLP_KEY_LEN	(16)


int		WdHelp( char *name )
{
	int		ch, i, ret;
	char	*fp, *idxFp;
	char	*s, buf[BUFSIZ];
	char	key[WDHLP_KEY_LEN+4];
	long	fpos;
	int		x0, y0;
	char	*dlg;
	char	*txtPtr;

	x0 = y0 = DLGPOS_CENTER_OF_SCN;
	txtPtr = NULL;
	DSP_writePage(1);
	if ( WdHlpPath[0] == '\0' )
		strcpy( WdHlpPath, AplGetStartPath() );
	sprintf( WdHlpFn   , "%sEdiHelp.whf", WdHlpPath );
	sprintf( WdHlpIdxFn, "%sEdiHelp.whi", WdHlpPath );

	if ( (fp = fopen(WdHlpFn,"rb")) == NULL )
	{
		DLG_tmpMsgTime( DLGPOS_MOS_SET_CENTER, DLGPOS_MOS_SET_CENTER,
		    C_MBLACK, C_DLGBASE, COLMIX(C_ERROR,C_GRAY),
		    3, " %s : \n  File open error!! ", WdHlpFn );
		return (ERR);
	}

	if ( (idxFp = fopen(WdHlpIdxFn,"rb")) == NULL )
	{
		/* インデックファイルの作成	*/

		dlg = DLG_msgOpen(x0,y0, 8*40,16*8, C_MBLACK,C_DLGBASE,C_HGREEN,
		    "MAKE INDEX FILE for WORD HELP" );
		if ( dlg == NULL )
		{
			fclose(fp);
			return (ERR);
		}
		DLG_msgClear( dlg, -1 );
		DLG_msgPrintf(dlg,"インデックスファイルを作成します。\r\n" );

		if ( (idxFp = fopen(WdHlpIdxFn,"wb")) == NULL )
		{
			DLG_msgPrintf(dlg,"File open errro!! (%s)\r\n", WdHlpIdxFn );
			DLG_msgClose(dlg);
			fclose(fp);
			return (ERR);
		}

		for (;;)
		{
			fpos = ftell(fp);
			if ( fgets(buf,BUFSIZ,fp) == NULL )
				break;
			s = buf;
			if ( *s != '.' )
				continue;
			++s;
			while ( isspace(*s) )
				++s;
			memset( key, '\0', 17 );
			for ( i = 0; i < WDHLP_KEY_LEN; ++i )
			{
				ch = *s;	++s;
				if ( isdigit(ch) == 0 && isalpha(ch) == 0 && ch != '_' )
					break;
				key[i] = ch;
			}
			if ( key[0] == '\0' )
				continue;
			DLG_msgPrintf(dlg,"fpos = %6ld : %s\r\n", fpos, key );
			fwrite(&fpos,            4,1,idxFp);
			fwrite(  key,WDHLP_KEY_LEN,1,idxFp);
		}
		DLG_msgClose(dlg);
		fclose(idxFp);
		if ( (idxFp = fopen(WdHlpIdxFn,"rb")) == NULL )
		{
			fclose(fp);
			return (ERR);
		}
	}

	ret = 0;
	for(;;)
	{
		if ( fread(&fpos,4,1,idxFp) < 1 )	/* ファイル位置情報	*/
		{
			ret = -1;
			break;
		}
		if ( fread(key,WDHLP_KEY_LEN,1,idxFp) < 1 )	/* キーネーム	*/
		{
			ret = -1;
			break;
		}
		if ( strncmp( name, key, WDHLP_KEY_LEN) == 0 )
			break;
	}
	fclose(idxFp);
	if ( ret )
	{	/* 指定の検索パターンはない	*/
		DLG_tmpMsgTime( DLGPOS_MOS_SET_CENTER, DLGPOS_MOS_SET_CENTER,
		    C_MBLACK, C_DLGBASE, COLMIX(C_ERROR,C_GRAY),
		    3, "  %s : \n  Pattern not found!! ", name );
		fclose(fp);
		return (ERR);
	}
	fseek(fp,fpos,SEEK_SET);
	fgets(buf,BUFSIZ,fp);	/* read header line */
	while ( fgets(buf,BUFSIZ,fp) != NULL )
	{
		if ( buf[0] == '.' )
			break;
		txtPtr = DLG_txtSetLinBuf( txtPtr, 8, 80, "%s", buf );
	}
	fclose(fp);

	if ( (dlg = DLG_txtOpen(x0,y0,80,20,txtPtr,0)) == NULL )
	{
		DLG_txtFreeLinBuf( txtPtr );
		return (ERR);
	}
	DLG_txtSetTitle( dlg, "EDI WORD HELP" );
	DLG_txtSetGuideMsg( dlg, "Function : %s", name );
	DLG_txtStart( dlg );
	DLG_txtClose( dlg );
	DLG_txtFreeLinBuf( txtPtr );

	return (NORMAL);
}

/*************************************************************************
*	カーソル位置の単語の説明を見る
*************************************************************************/

int		TxdUsr_word_help( char *txd )
{
	int		ret, ch, len;
	char	word[WDHLP_KEY_LEN+4];
	size_t	curLnL, curOfs;
	char	*dlg;

	if ( TxdGet_selFlag(txd) != 0 )		/* 選択中なら処理しない	*/
		return (0);

	/* 現在カーソル位置を取得	*/
	curLnL = TxdGet_curPosLnL(txd);
	curOfs = TxdGet_curPosOfs(txd);

	if ( (ch = TXD_getch(txd)) > 0 && ch < 256 && (isalpha(ch) || ch == '_') )
	{
		len = 0;
		while( len < WDHLP_KEY_LEN )
		{
			ch = TXD_getch(txd);
			TXD_cmdJump( txd, TXD_CUR_RIGHT );
			if ( isalpha(ch) || ch == '_' )
			{
				word[len++] = ch;
			} else
				break;
		}
		word[len] = '\0';
		TXD_jumpPos( txd, curLnL, curOfs );
		if ( len == 0 )
			return (NORMAL);
	} else
		word[0] = '\0';

	DSP_writePage(1);
	dlg = DLG_getsOpen( DLGPOS_MOS_SET_HOME, DLGPOS_MOS_SET_HOME, 320,
	    "検索パターンを入力してください。" );
	DLG_getsSetTitle( dlg, "PATTERN" );
	ret = DLG_getsStart( dlg, WDHLP_KEY_LEN, word );
	DLG_getsClose(dlg);
	if ( ret <= 0 )
		return (0);

	WdHelp( word );

	return (0);
}

#undef	WDHLP_KEY_LEN
