#include    <stdio.h>
#include    <stdlib.h>
#include    <ctype.h>
#include    "defs.h"
#include    "key.h"
#include    "kkstr2.h"
/* ------------------ New file  91.10.12 Pumpkin ---------------- */
#include    <msdos.cf>
#include    <dos.h>
extern int  diskReady(int drive);
/* -------------------------------------------------------------- */
/* ------------------ SIDEWORK 92.01.31 Pumpkin -------------- */
extern	void	wink_sdwk_menu();
/* ---------------------------------------------------------- */

extern int  strlen();
extern void memset();

extern int  Get_key();
extern int  getch();
extern int  kbhit();
extern void KYB_clic();

extern void Set_vram();
extern void wrtstr();
extern void Dsp_vram();
extern void Dsp_vram_flash();
extern void Dsp_wrt_flg();
extern void Set_file_name();
extern void ChgCol_wind();

extern int  save();
extern int  File_sel();
extern BOOL iskan();

extern void Cut_buf();
extern void Ins_buf();
extern void Del_buf();
extern void Buf_init();
extern int  New_wind();
extern int  Del_wind();
extern void ReSize_wind();
extern LONG Line_ptr();
extern LONG Undo_get();
extern void Undo_put();

extern LONG Bak_line2();
extern LONG Nxt_line2();
extern LONG USerch();

extern void ASCII_code();
extern void KANJI_code();
extern void Printer_out();

extern char     *file_name;
extern UCHAR    vram[];
extern short    ReWrt_flg;
extern short    dlp_flg,Wind_flg;

       int      Line_no=0;
       short    Line_X=0,Line_Y=0;
       int      Wrt_mode=FALSE,TAB_mode=0;
       char     *SPCSTR="                                     ";
       char     *FILEMEI="ﾌｧｲﾙ名 ";
static int	Ins_flg=FALSE;
static int	Endof_edit=FALSE;
static UCHAR 	sctmp[160]={ 0 };
static UCHAR    rpsctmp[160]={ 0 };
static UCHAR    rpdstmp[160]={ 0 };

LONG	Top_line(ptr)
LONG	ptr;
{
    return Bak_line(Nxt_line(ptr));
}
char	*Dmy_form(p,i,ch1,ch2,ch3)
char	*p;
int	i,ch1,ch2,ch3;
{
    *(p++) = ch1; *(p++) = 0x1B;
    while ( i-- > 0 ) {
	*(p++) = ch2; *(p++) = 0x1B;
    }
    *(p++) = ch3; *(p++) = 0x1B; *p = '\0';
    return p;
}
int	Yes_no(ttl)
char	*ttl;
{
    int     len,x,ch,ec;
    char    tmp[160];

    len = strlen(ttl) + 4; x = 36 - len / 2;
    Dmy_form(tmp,len,0x98,0x95,0x99); 
    wrtstr(tmp,x,8,0x02);

    Dmy_form(tmp,len,0x96,0x20,0x96); 
    wrtstr(tmp,x,9,0x02);
    wrtstr(tmp,x,10,0x02);
    wrtstr(ttl,x+2,10,0x02);

    wrtstr(tmp,x,11,0x02);
    wrtstr(tmp,x,12,0x02);
    wrtstr("はい/Y  いいえ/N",28,12,0x02);

    wrtstr(tmp,x,13,0x02);
    Dmy_form(tmp,len,0x9A,0x95,0x9B); 
    wrtstr(tmp,x,14,0x02);

    /* --------------- FEP OFF   93.01.21 Pumpkin ------------- */
#ifdef	TOWNS
    KAN_disp(KAN_DISPOFF);
#endif
    /* ---------------------------------------------------------- */
    ch = Get_key(&ec);
#ifdef	TOWNS
    Dsp_vram_flash();
    /* --------------- FEP OFF   93.01.21 Pumpkin ------------- */
    KAN_disp(KAN_DISPON);
    /* ---------------------------------------------------------- */
#endif
    if ( ch == 'y' || ch == 'Y' || ec == 0x7300 )
	return TRUE;
    else
	return FALSE;
}
int	Sel_menu(menu,x,y,old_no)
char	*menu[];
int	x,y,*old_no;
{
    int     i,mx,ch,ec,no,fg;
    char    tmp[100];    /* RED2 Like  80 --> 100 に変更  HIKA */
    int     nul_no[8],nul=0;
    /* --------------- RED2 Like   90.10.18 HIKA ---------------- */
    /* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
     char    fststr[30];
    /* ---------------------------------------------------------- */

    /* --------------- FEP OFF   93.01.21 Pumpkin ------------- */
#ifdef	TOWNS
    KAN_disp(KAN_DISPOFF);
#endif
    /* ---------------------------------------------------------- */

    i = strlen(menu[0]);
    Dmy_form(tmp,i,0x98,0x95,0x99); 
    wrtstr(tmp,x,y++,0x07);
    no = *old_no;
    for ( mx = 0 ; menu[mx] != NULL ; mx++ ) {
	if ( menu[mx][0] == '\0' ) {
	    Dmy_form(tmp,i,0x93,0x95,0x92); 
	    nul_no[nul++] = mx;
	    if ( no >= mx ) no++;
	} else {
	    sprintf(tmp,"\x96\x1B%s\x96\x1B",menu[mx]);
	/* --------------- RED2 Like   90.10.18 HIKA ---------------- */
	    fststr[mx-nul] = menu[mx][0];
	/* ---------------------------------------------------------- */
	}
	wrtstr(tmp,x,y+mx,0x07);
    }
    Dmy_form(tmp,i,0x9A,0x95,0x9B); 
    wrtstr(tmp,x,y+mx,0x07);

    for ( ; ; ) {
	if ( no >= 0 )
	    wrtstr(menu[no],x+1,y+no,0x14);
	ch = Get_key(&ec);
	/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
	/* --------------- FEP OFF   93.01.21 Pumpkin ------------- */
        if ( ch == '\x1c' ) { /* → */
	    Dsp_vram_flash();
#ifdef	TOWNS
	    KAN_disp(KAN_DISPON);
#endif
	    return RIGHT;
	}
        if ( ch == '\x1d' ) { /* ← */
	    Dsp_vram_flash();
#ifdef	TOWNS
	    KAN_disp(KAN_DISPON);
#endif
	    return LEFT;
	}
        /* ---------------------------------------------------------- */
	/* --------------- RED2 Like   90.10.18 HIKA ---------------- */
        if ( ( ch >= 'A' && ch <= 'Z' ) || ( ch >= 'a' && ch <= 'z' ) ) {
	     for ( i = 0 ; i < mx-nul ; i++ ){
	         if ( ch == fststr[i] || ch == fststr[i] + 0x20 )
	             ch = 0x31 + i;
	     }
	}
	/* ---------------------------------------------------------- */

	if ( no >= 0 )
	    wrtstr(menu[no],x+1,y+no,0x07);
	if ( ch == ' ' || ch == '\x1F' || ec == 0x7200 ) {
	    if ( ++no >= mx )
		no = ERR;
	    else if ( menu[no][0] == '\0' && ++no >= mx )
		no = ERR;
	} else if ( ch == '\x08' || ch == '\x1E' ) {
	    if ( --no < ERR )
		no = mx - 1;
	    else if ( no >= 0 && menu[no][0] == '\0' && --no < ERR )
		no = mx - 1;
	} else if ( ch == '\x0D' || ec == 0x7300 ) {
	    fg = 0;
	    break;
	} else if ( ec == 0x7200 || ec == 0x0100 || 
			           (ec >= 0x5D00 && ec <= 0x6600) ) {
	    fg = 1;
	    break;
	} else if ( ch >= '1' && ch <= '9' && (ch - '1') < mx ) {
	    no = ch - '1';
	    fg = 2;
	    break;
	}
    }
    if ( fg != 2 ) {
	if ( no >= 0 )
	    wrtstr(menu[no],x+1,y+no,0x14);
	for ( i = 0,mx = no ; i < nul ; i++ ) {
	    if ( no >= nul_no[i] )
		mx--;
	}
	no = mx;
    }
#ifdef	TOWNS
    Dsp_vram_flash();
    /* --------------- FEP OFF   93.01.21 Pumpkin ------------- */
    KAN_disp(KAN_DISPON);
    /* ---------------------------------------------------------- */
#endif
    *old_no = no;
    if ( no == ERR || fg == 1 )
	return ERR;
    else
	return FALSE;
}
UCHAR	*kanji_chk(str,len)
UCHAR	*str;
int	len;
{
    while ( len > 0 ) {
	if ( iskanji(*str) && iskanji2(*(str+1)) ) {
	    str += 2;
	    len -= 2;
	} else {
	    str++;
	    len--;
	}
    }
    return str;
}

/* ------------------ RED2 like 92/12/28 Pumpkin ---------------- */

extern void MOJI_input1Char(UCHAR *moji_area
		,unsigned int ky,int max_moji,int *lcur_x);

BOOL	_Input(UCHAR *arg,char *ttl,int sw)
{
    int	    ch,ec,x,len;
    int     Bk_X,Bk_Y;
    BOOL    fg=FALSE;
    char    tmp[80],tmp2[3];
    int	    lcur_x;

    arg[35] = 0;
    sprintf(tmp,"%s%36s",ttl,"");
    x = strlen(ttl) + 15;
    Bk_X = Cur_X; Bk_Y = Cur_Y;
    len = lcur_x = strlen(arg);
    Cur_Y = (0 - OFF_Y);
    wrtstr(tmp,15,0,0x07);
    memset(arg+lcur_x,0,36-lcur_x);

    for ( ; ; ) {
	Cur_X = x + lcur_x; ReWrt_flg = ERR;
	strcpy(tmp,arg);
	memset(tmp+len,' ',36-len);
	tmp[36]=0;
	wrtstr(tmp,x,0,0x07);

	if (lcur_x == len) {
		tmp2[0] = 0xFE;
		tmp2[1] = 0x1B;
		tmp2[2] = 0;
		wrtstr(tmp2,Cur_X,0,0x07);
	}else{
		if (iskanji(arg[lcur_x])) {
			tmp2[0] = arg[lcur_x];
			tmp2[1] = arg[lcur_x+1];
			tmp2[2] = 0;
		}else{
			tmp2[0] = arg[lcur_x];
			tmp2[1] = 0;
		}
		wrtstr(tmp2,Cur_X,0,0x17);
	}

        ch = Get_key(&ec);

	MOJI_input1Char(arg,ch,36,&lcur_x);
	len = strlen(arg);
	if ( ch == '\x0D' || ec == 0x7300 )
	    break;
	else if ( ch == '\x1B' || ec == 0x7200 ) {
	    fg = ERR;
	    break;
	} else if ( sw == 1 && (ch == '\x1e' || ch == '\x1f') ) {
	    fg = ch;
	    break;
	} else if ( ec == 0x4f04) {
            lcur_x = 0;
	} else if ( ec == 0x5104) {
            lcur_x = strlen(arg);
	} else if ( ch == '\x0B' ) {
            arg[0] = '\0'; lcur_x = len = 0;
	}
    }
    arg[len] = '\0';
    memset(tmp,' ',51);
    tmp[51] = '\0';
    wrtstr(tmp,15,0,0x1F);
    Cur_X = Bk_X; Cur_Y = Bk_Y;

    return fg;
}

BOOL	Input(UCHAR *arg,char *ttl)
{
	return _Input(arg,ttl,0);
}
BOOL	Input2(UCHAR *arg,char *ttl)
{
	return _Input(arg,ttl,1);
}
/* -------------------------------------------------------------- */
/**************
BOOL	Input(arg,ttl)
UCHAR	*arg;
char	*ttl;
{
    int	    ch,ec,x,len;
    int     Bk_X,Bk_Y;
    BOOL    fg=FALSE;
    char    tmp[80];

    sprintf(tmp,"%s%36s",ttl,"");
    x = strlen(ttl) + 15;
    Bk_X = Cur_X; Bk_Y = Cur_Y;
    Cur_Y = (0 - OFF_Y);
    wrtstr(tmp,15,0,0x07);
    for ( len = strlen(arg) ; ; ) {
	arg[len] = 0xFE; arg[len+1] = 0x1B;
	memset(arg+len+2,' ',34-len);
	arg[34] = '\0';
	Cur_X = x + len; ReWrt_flg = ERR;
	wrtstr(arg,x,0,0x07);
        ch = Get_key(&ec);
	if ( ch != 0xFFFF && ch >= ' ' && len < 34 )
	    arg[len++] = ch;
	else if ( ch == '\x0D' || ec == 0x7300 )
	    break;
	else if ( ch == '\x1B' || ec == 0x7200 ) {
	    fg = ERR;
	    break;
	} else if ( ch == '\x08' && len > 0 ) {
	    len--;
	    if ( (arg + len) != kanji_chk(arg,len) )
		len--;
	}
    }
    arg[len] = '\0';
    memset(tmp,' ',50);
    tmp[50] = 0;
    wrtstr(tmp,15,0,0x1F);
    Cur_X = Bk_X; Cur_Y = Bk_Y;
    return fg;
}
*********/
void    Line_set()
{
    LONG    p;

    for ( Line_no = 0, p = 0 ; p < top_ptr ; Line_no++ )
        p = Nxt_line(p);
}
BOOL	Cut_buff(sp,ep)
LONG	sp,ep;
{
    Undo_put(sp,ep - sp);
    return FALSE;
}
void	ins_chr(str,len)
UCHAR	*str;
int	len;
{
    LONG    p;

    Ins_buf(ent_ptr,len,str);
    ent_ptr += len; btm_ptr += len;
    while ( (p = Nxt_line(lin_ptr)) <= ent_ptr ) {
	if ( (lin_ptr = p) == btm_ptr )
	    break;
    }
    Wrt_flg = Wrt_mode = TRUE;
}
void	set_chr(str,len)
UCHAR	*str;
int	len;
{
    LONG    p;

    if ( Ins_flg == FALSE || ent_ptr == btm_ptr ) {
	ins_chr(str,len);
	return;
    }

    p = Nxt_line(lin_ptr);
    while ( len > 0 ) {
	if ( ent_ptr == btm_ptr || *Cnv_ptr(ent_ptr) == '\x0D' ) {
	    ins_chr(str,len);
	    return;
	} else {
	   while ( len > 0 ) {
		if ( iskan(ent_ptr) != FALSE )
		    *Cnv_ptr(ent_ptr+1) = ' ';
		*Cnv_ptr(ent_ptr++) = *(str++);
		len--;
		if ( ent_ptr == btm_ptr )
		    break;
	   }
	}
	if ( ent_ptr >= p ) {
	    lin_ptr = p;
	    p = Nxt_line(p);
	}
    }
    Wrt_flg = Wrt_mode = TRUE;
}
LONG    Ent_pos()
{
    int	    x;
    LONG    p;

    for ( x = 0,p = lin_ptr ; x < Line_X && p < btm_ptr && 
					*Cnv_ptr(p) != '\x0D' ; ) {
        if ( iskan(p) != FALSE ) {
            x+=2; p+=2;
	} else if ( *Cnv_ptr(p) == '\t' ) {
            x = (((x + TAB) / TAB) * TAB);
	    p++;
        } else {
	    x++; p++;
	}
    }
    return p;
}
void	CR_put()
{
    if ( Ins_flg == FALSE || ent_ptr == btm_ptr ) {
	ins_chr((UCHAR *)"\x0D\x0A",2);
	return;
    }
    lin_ptr = ent_ptr = Nxt_line(lin_ptr);
}
void	TAB_put()
{
    int	    i,j,x;
    LONG    p;
    UCHAR   tmp[40];

    if ( TAB_mode == 0 ) {
	set_chr((UCHAR *)"\t",1);
	return;
    }

    for ( x = 0,p = lin_ptr ; p < ent_ptr ; ) {
        if ( iskan(p) != FALSE ) {
            x+=2; p+=2;
	} else if ( *Cnv_ptr(p) == '\t' ) {
            x = ((x + TAB) / TAB) * TAB;
	    p++;
        } else {
	    x++; p++;
	}
    }
    j = ((x + TAB) / TAB) * TAB - x;
    for ( i = 0 ; i < j ; i++ )
	tmp[i] = ' ';
    set_chr(tmp,j);
}
void	Word_cut()
{
    int     i,ch;
    LONG    p;
    UCHAR   *s;

    for ( p = ent_ptr ; p != btm_ptr && 
		((ch = *Cnv_ptr(p)) == ' ' || ch == '\t') ; p++ );
    s = sctmp; i = 0;
    while ( i < 160 && 
	    p != btm_ptr && ((ch = *Cnv_ptr(p)) >= '0' || ch == '_') ) {
	*(s++) = ch;
	p++;
    }
    *s = '\0';
}
void	Word_serch()
{
    LONG    p;

    if ( sctmp[0] == '\0' )
	return;
    if ( (p = USerch(FALSE,ent_ptr,sctmp)) >= btm_ptr )
	return;
    if ( p == ent_ptr ) {
	p += strlen(sctmp);
        if ( (p = USerch(FALSE,p,sctmp)) >= btm_ptr )
	    return;
    }
    ent_ptr = p;
    lin_ptr = Top_line(ent_ptr);
    return;
}
void	Cur_up()
{
    int     i;

    if ( lin_ptr == 0 )
	return;
    if ( lin_ptr == btm_ptr && *Cnv_ptr(lin_ptr-1) != 0x0A ) {
        lin_ptr = Bak_line(lin_ptr);
        lin_ptr = Bak_line(lin_ptr);
    } else 
        lin_ptr = Bak_line2(lin_ptr);
    ent_ptr = Ent_pos();
    if ( lin_ptr < top_ptr ) {
	for ( i = 5 ; top_ptr > 0 && i > 0 ; i-- ) {
	    top_ptr = Bak_line(top_ptr);
	    Line_no--;
	}
    }
}
void	Cur_down()
{
    if ( lin_ptr == btm_ptr )
	return;
    lin_ptr = Nxt_line2(lin_ptr);
    ent_ptr = Ent_pos();
}
void	Scr_up()
{
    if ( lin_ptr == btm_ptr )
	return;
    top_ptr = Nxt_line(top_ptr);
    lin_ptr = Nxt_line(lin_ptr);
    ent_ptr = Ent_pos();
    Line_no++;
}
void    Scr_down()
{
    if ( top_ptr == 0 ) {
	Cur_up();
        return;
    }
    top_ptr = Bak_line(top_ptr);
    lin_ptr = Bak_line(lin_ptr);
    ent_ptr = Ent_pos();
    Line_no--;
}
void	PScr_up()
{
    int     i;

    for ( i=10 ; i > 0 ; i-- ) 
	Scr_up();
}
void	PScr_down()
{
    int     i;

    for ( i=10 ; i > 0 ; i-- )
	Scr_down();
}
void	Cur_rit()
{
    LONG    p;

    if ( ent_ptr == btm_ptr )
	return;

    if ( iskan(ent_ptr) != FALSE ||
	(*Cnv_ptr(ent_ptr) == '\x0D' && *Cnv_ptr(ent_ptr+1) == '\x0A') )
	ent_ptr += 2;
    else
	ent_ptr++;
    if ( (p = Nxt_line(lin_ptr)) <= ent_ptr )
	lin_ptr = p;
}
void	Cur_left()
{
    LONG    p,s;

    if ( ent_ptr == 0 )
	return;
    if ( ent_ptr == lin_ptr ) {
	if ( lin_ptr == top_ptr ) {
	    top_ptr = Bak_line(top_ptr);
	    Line_no--;
	}
	lin_ptr = Bak_line(lin_ptr);
    }
    for ( p = s = lin_ptr ; p < ent_ptr ; ) {
        s = p;
	if ( iskan(p) != FALSE ||
	    (*Cnv_ptr(p) == '\x0D' && *Cnv_ptr(p+1) == '\x0A' ) )
	    p+=2;
	else
	    p++;
    }
    ent_ptr = s;
}
void	Cur_ltop()
{
    ent_ptr = lin_ptr;
}
void	Cur_lbtm()
{
    while ( ent_ptr != btm_ptr && *Cnv_ptr(ent_ptr) != '\x0D' )
	Cur_rit();
}
void	Rit_word()
{
    while ( ent_ptr != btm_ptr && *Cnv_ptr(ent_ptr) >= '0' )
	Cur_rit();
    while ( ent_ptr != btm_ptr && *Cnv_ptr(ent_ptr) < '0' )
	Cur_rit();
}
void	Left_word()
{
    if ( ent_ptr > 0 && *Cnv_ptr(ent_ptr) >= '0' && 
			 *Cnv_ptr(ent_ptr-1) >= '0' ) {
        while ( ent_ptr > 0 && *Cnv_ptr(ent_ptr) >= '0' )
	    Cur_left();
    } else {
        while ( ent_ptr > 0 && *Cnv_ptr(ent_ptr) >= '0' )
	    Cur_left();
        while ( ent_ptr > 0 && *Cnv_ptr(ent_ptr) < '0' )
	    Cur_left();
        while ( ent_ptr > 0 && *Cnv_ptr(ent_ptr) >= '0' )
	    Cur_left();
    }
    if ( ent_ptr > 0 && *Cnv_ptr(ent_ptr) < '0' )
	Cur_rit();
}
void    Del_chr()
{
    LONG    n;

    if ( ent_ptr == btm_ptr )
	return;

    if ( iskan(ent_ptr) != FALSE || 
	(*Cnv_ptr(ent_ptr) == '\x0D' && *Cnv_ptr(ent_ptr+1) == '\x0A' ) )
	n = 2;
    else
	n = 1;

    Del_buf(ent_ptr,n);
    btm_ptr -= n;
    Wrt_flg = Wrt_mode = TRUE;
}
void	Del_line()
{
    LONG    n,p;

    if ( (p = Nxt_line(lin_ptr)) == lin_ptr )
	return;
    n = p - lin_ptr;
    Cut_buff(lin_ptr,p);
    Del_buf(lin_ptr,n);
    btm_ptr -= n;
    ent_ptr = Ent_pos();
    Wrt_flg = Wrt_mode = TRUE;
}
void	Del_rit()
{
    LONG    n,p;

    if ( (p = Nxt_line(lin_ptr)) == lin_ptr )
	return;
    if ( (n = p - ent_ptr) <= 0 )
	return;
    Cut_buff(ent_ptr,p);
    Del_buf(ent_ptr,n);
    btm_ptr -= n;
    Wrt_flg = Wrt_mode = TRUE;
}
void	Del_left()
{
    LONG    n;

    if ( lin_ptr == ent_ptr )
	return;
    n = ent_ptr - lin_ptr;
    Cut_buff(lin_ptr,ent_ptr);
    Del_buf(lin_ptr,n);
    btm_ptr -= n;
    ent_ptr = lin_ptr;
    Wrt_flg = Wrt_mode = TRUE;
}
void	BS_chr()
{
    if ( ent_ptr == 0 )
	return;
    Cur_left();
    Del_chr();
}
void	Undo_set()
{
    LONG    n;

    if ( (n = Undo_get(ent_ptr)) > 0L ) {
        btm_ptr += n;
        Wrt_flg = Wrt_mode = TRUE;
    }
}
void	Del_dmy()
{
    LONG    p;

    if ( (p = Nxt_line(lin_ptr)) == lin_ptr )
	return;
    Cut_buff(lin_ptr,p);
    p = ent_ptr;
    ent_ptr = lin_ptr;
    Undo_set();
    ent_ptr = p;
}
void	Ins_line()
{
    Ins_buf(lin_ptr,2,"\x0D\x0A");
    ent_ptr = lin_ptr;
    btm_ptr += 2;
    Wrt_flg = Wrt_mode = TRUE;
}
void	Cnt_dsp()
{
    if ( ++CONTRL > 2 )
	CONTRL = 0;
}
void	Dsp_ins_mode()
{
    if ( Ins_flg == FALSE )
	wrtstr("挿入",5,0,0x17);
    else
	wrtstr("上書",5,0,0x17);
}
void	Ins_sw()
{
    Ins_flg = (Ins_flg == FALSE ? TRUE : FALSE );
    Dsp_ins_mode();
}
BOOL    Read_file(file)
char    *file;
{
    int     n;
    FILE    *fp;
    LONG    p;
    UCHAR   tmp[256];

    if ( (fp = fopen(file,"rb")) == NULL ) {
	wrtstr("(ファイルが見当たりません)",30,1,0x12);
        return ERR;
    }

    wrtstr("(ファイル読み込み中)",32,1,0x05);
    p = ent_ptr;
    while ( (n = fread(tmp,1,256,fp)) > 0 ) {
        if ( tmp[n-1] == '\x1A' ) n--;
	Ins_buf(p,n,tmp);
        btm_ptr += n;
        p += n;
        Wrt_flg = Wrt_mode = TRUE;
    }
    fclose(fp);
    wrtstr(SPCSTR,30,1,0x1F);
    return FALSE;
}
void	Wrt_file(sp,ep)
LONG	sp,ep;
{
    int     n;
    LONG    sz;
    FILE    *fp;
    UCHAR   tmp[256];

    wrtstr("(ファイルに抜き出します)",32,1,0x15);
    tmp[0] = '\0';
    if ( Input(tmp,FILEMEI) != FALSE )
	goto END;
    if ( (fp = fopen((char *)tmp,"wb")) == NULL ) {
        wrtstr("(ファイルが作成出来ません)",30,1,0x12);
	return;
    }
    for ( sz = ep - sp ; sz > 0 ; ) {
	if ( sz < 256 ) n = sz; else n = 256;
        Cut_buf(tmp,n,sp);
	fwrite(tmp,1,n,fp);
	sp += n;
	sz -= n;
    }
    tmp[0] = 0x1A;
    fwrite(tmp,1,1,fp);
    fclose(fp);

END:
    wrtstr(SPCSTR,30,1,0x1F);
}
void    Dsp_free()
{
    static int  Line_old=(-1);
    static LONG Free_buf=(-1);
    int    i;
    char   tmp[30];

    if ( (i = Line_no + Line_Y) != Line_old ) {
        Line_old = i;
        sprintf(tmp,"[%06d Line]",Line_old+1);
        wrtstr(tmp,114,0,0x1F);
    }
    if ( btm_ptr != Free_buf ) {
        Free_buf = btm_ptr;
        sprintf(tmp,"[%06ld Byte]",Free_buf);
        wrtstr(tmp,114,1,0x1F);
    }
}
void    Cur_top_file()
{
    lin_ptr = ent_ptr = top_ptr = 0;
    Line_no = 0;
}
void    Cur_btm_file()
{
    int     i;

/* ------------------ bug fix  93.02.18 Pumpkin ---------------- */
#ifdef TOWNS
    i = 0x7FFFFFFF;
#else
    i = 0x7FFF;
#endif
/* -------------------------------------------------------------- */
/**********
    i = 0x7FFF;
**********/
    top_ptr = lin_ptr = ent_ptr = Line_ptr(&i);
    Line_no = i;
    for ( i = 10 ; top_ptr > 0 && i > 0 ; i-- ) {
        top_ptr = Bak_line(top_ptr);
	Line_no--;
    }
}
void	Cmds_Q()
{
    int     ch;
    unsigned ec;

    ch = Get_key(&ec); ec &= 0xff00;
    switch(ec) {
	case 0x1400: Cur_top_file(); break;
	case 0x2C00: Cur_btm_file(); break;
	case 0x1E00: Cur_ltop(); break;
	case 0x2100: Cur_lbtm(); break;
    }
}
void    Cmds_PF1()
{
    int    no;
    UCHAR  *p;
    static int   old_no=ERR;
    static UCHAR tmp[160]={ 0 };
    /* --------------- RED2 Like   90.10.18 HIKA ---------------- */
     static char *menu[]={
         "S: Save           1.保存して終了する   ",
	 "A: save All touch 2.全て保存して終了   ",
	 "",
         "U: Up date        3.ﾌｧｲﾙを更新する     ",
         "C: Change name    4.ﾌｧｲﾙ名を変更する   ",
	 "",
         "R: Read file      5.別のﾌｧｲﾙを読み込む ",
         "Q: Quit           6.編集を放棄する     ",
	 "X: eXit all       7.全ての編集を放棄   ",
	 "",
         "O: Original file  8.再編集を行う       ",
         "N: New window     9.新規に編集を行う   ",
         NULL };
    /* ---------------------------------------------------------- */
    /***********************************************
    static char *menu[]={
        "1 保存して終了する  ",
	"2 全て保存して終了  ",
	"",
        "3 ﾌｧｲﾙを更新する    ",
        "4 ﾌｧｲﾙ名を変更する  ",
	"",
        "5 ﾌｧｲﾙを読み込む    ",
        "6 編集を放棄する    ",
	"7 全ての編集を放棄  ",
	"",
        "8 再編集を行う      ",
        "9 新規に編集を行う  ",
        NULL };
    ************************************************/
/* ------------------ New file  91.10.12 Pumpkin ---------------- */
    unsigned int att=0,stat=0;
/* -------------------------------------------------------------- */

    wrtstr(SPCSTR,30,1,0x1F);
/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
	switch (Sel_menu(menu,1,2,&old_no)) {
		case FALSE:
			break;
		case LEFT: 
			Dsp_vram(vram);
			CD_menu();
			return;
		case RIGHT:
                    	Dsp_vram(vram);
			Cmds_PF2();
			return;
		default:
			return;
	}
/*****
    if ( Sel_menu(menu,1,2,&old_no) != FALSE )
	return;
*****/
/* ---------------------------------------------------------- */

    no = old_no + 1;
    if ( no == 1 || no == 2 ) {
	do {
	    if ( Wrt_flg == FALSE ) {
	        wrtstr("(ファイルに変更がありません)",30,1,0x05);
	    } else {
                wrtstr("(書き込み中)",30,1,0x05);
                p = NULL;
                while ( save(p) != FALSE ) {
                    wrtstr("(セ−ブ出来ません)",30,1,0x12);
                    if ( Input(tmp,FILEMEI) != FALSE || 
			 File_sel(tmp) != FALSE ) {
                        wrtstr(SPCSTR,30,1,0x1F);
                        return;
                    }
                    p = tmp;
                }
                wrtstr("(書き込み終了)",30,1,0x05);
	    }
	} while ( no == 2 && Del_wind() == FALSE );
    } else if ( no == 3 ) {
        if ( save(NULL) != FALSE )
            wrtstr("(セ−ブ出来ません)",30,1,0x12);
        return;
    } else if ( no == 4 ) {
        if ( Input(tmp,FILEMEI) != FALSE || File_sel(tmp) != FALSE )
            return;
/* ------------------ New file  91.10.12 Pumpkin ---------------- */
	if (_dos_getfileattr((char *)tmp,&att) == 0 &&
	  Yes_no("同名のファイルが既にありますが　よろしいですか？") == FALSE )
                    return;
/* -------------------------------------------------------------- */
        Set_file_name(tmp);
/* ------------------ RED2 like 92/01/31 Pumpkin ----------------- */
        Wrt_flg = Wrt_mode = TRUE;
/* -------------------------------------------------------------- */
        return;
    } else if ( no == 5 ) {
        if ( Input(tmp,FILEMEI) != FALSE || File_sel(tmp) != FALSE )
            return;
        Read_file((char *)tmp);
        return;
   } else if ( no == 6 || no == 7 ) {
	do {
            if ( Wrt_flg != FALSE &&
	    Yes_no("ファイルに変更があります　編集を中断しますか？") == FALSE )
                    return;
            wrtstr("(編集を中断しました)",30,1,0x05);
	} while ( no == 7 && Del_wind() == FALSE );
    } else if ( no == 8 ) {
	if ( file_name == NULL ) {
	    wrtstr("(ファイルが設定されてません)",30,1,0x12);
	    return;
	}
	if ( Wrt_flg != FALSE &&
        Yes_no("ファイルに変更があります　編集を放棄しますか？") == FALSE )
                return;
	Del_buf(0l,btm_ptr);
	top_ptr = lin_ptr = ent_ptr = btm_ptr = 0;
	Line_no = 0;
	Wrt_flg = FALSE;
	Read_file(file_name);
	return;
    } else if ( no == 9 ) {
/* ------------------ New file  91.10.12 Pumpkin ---------------- */
/* ------------------ Debug  92.02.07 Pumpkin --------------- */
	if ( Input(tmp,FILEMEI) != FALSE ) return;

	att = 0;
	if (strlen(tmp) >= 2) {
		if (tmp[1] == 0x3a ) {
			if (tmp[0] >= 0x41 && tmp[0] <= 0x5a ) {
				att = (unsigned int)(tmp[0] - 0x40);
			}
			if (tmp[0] >= 0x61 && tmp[0] <= 0x7a) {
				att = (unsigned int)(tmp[0] - 0x60);
			}
		}
    		if (diskReady(att) == ERR) {
			wrtstr("ディスクの準備ができていません",30,1,0x12);
			return;
		}
	} 
	stat = File_sel(tmp);
	if (stat == 2) {
		wrtstr("マッチするファイルがありません",30,1,0x12);
		return;
	}
	if (stat != FALSE) return;

	if ( Wrt_flg == FALSE && file_name == NULL ) {
	   Buf_init(tmp);
	   Set_file_name(tmp);
 	   Cur_top_file();
	} else {
		if ( New_wind(tmp) != FALSE ) {
			wrtstr("新規編集に失敗しました",30,1,0x12);
		}else{
			Cur_top_file();
		}
	}
	return;

/**********
	if ( Input(tmp,FILEMEI) != FALSE || File_sel(tmp) != FALSE )
	    return;

	if ( Wrt_flg == FALSE && file_name == NULL ) {
	   Buf_init(tmp);
	   Set_file_name(tmp);
	} else if ( New_wind(tmp) != FALSE )
	    wrtstr("新規編集に失敗しました",30,1,0x12);
	return;
***********/
/* -------------------------------------------------------------- */

    }
    Endof_edit = TRUE;
    return;


}
LONG    Serch(ptr,str)
LONG    ptr;
UCHAR   *str;
{
    LONG    p;
    UCHAR   *s;

    while ( ptr < btm_ptr ) { 
	for ( p = ptr,s = str ; p < btm_ptr ; ) {
	    if ( *s == '\0' )
		return ptr;
	    if ( *Cnv_ptr(p++) != *(s++) )
		break;
	}
	ptr++;
    }
    return ptr;
}
LONG    USerch(sw,ptr,str)
int	sw;
LONG    ptr;
UCHAR   *str;
{
    int     cp,cs;
    LONG    p;
    UCHAR   *s;

    for ( ; ; ) {
	for ( p = ptr,s = str ; p < btm_ptr ; ) {
	    if ( *s == '\0' )
		return ptr;
	    cp = *Cnv_ptr(p++); cp = toupper(cp);
	    cs = *(s++); cs = toupper(cs);
	    if ( cp != cs )
		break;
	}
	if ( sw == FALSE ) {
	    if ( ++ptr >= btm_ptr )
	        return btm_ptr;
	} else if ( ptr-- <= 0 )
	    return btm_ptr;
    }
    return ptr;
}
void	Cmds_PF2()
{
    int     i,j,no,ch,fg;
    UCHAR   *d,tmp[160];
    LONG    p,s;
    static int   old_no=ERR;
/* --------------- RED2 Like   92.12.30 Pumpkin ------------- */
    int		df,ec;
/* ---------------------------------------------------------- */
   /***********************************************
    static char  *menu[]={
        "1 文字列の検索(大小区別無し)",
        "2 文字列の置き換え(確認有り)",
        "3 文字列の置き換え          ",
	"",
        "4 指定行番号に移動          ",
        "5 ファイルの先頭に移動      ",
        "6 ファイルの最後に移動      ",
        NULL };
   ************************************************/
    /* --------------- RED2 Like   90.10.18 HIKA ---------------- */
     static char  *menu[]={
         "S: Search         1.文字列の検索           ",
         "R: Replace        2.文字列の置換(確認有り) ",
         "A: replace All    3.文字列の置換           ",
	 "",
         "J: Jump           4.指定行番号に移動       ",
         "T: Top of file    5.ファイルの先頭に移動   ",
         "B: Bottom of file 6.ファイルの最後に移動   ",
	 "",
         "Z: SideWork Menu  7.サイドワークメニュー   ",
         NULL };
   /* --------------------------------------------------------- - */

    wrtstr(SPCSTR,30,1,0x1F);
/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
	switch (Sel_menu(menu,10,2,&old_no)) {
		case FALSE:
			break;
		case LEFT: 
                    	Dsp_vram(vram);
			Cmds_PF1();
			return;
		case RIGHT:
                    	Dsp_vram(vram);
			Cmds_PF3();
			return;
		default:
			return;
	}
/*****
    if ( Sel_menu(menu,10,2,&old_no) != FALSE )
	return;
*****/
/* ---------------------------------------------------------- */
    no = old_no + 1;

/* --------------- RED2 Like   92.12.30 Pumpkin ------------- */
    if ( no == 1 ) {
	df = Input2(sctmp,"検索文字列 ");
        if ( df == ERR || sctmp[0] == '\0' ) return;
	if ( df == FALSE || df == '\x1F' ) fg = FALSE;
	if ( df == '\x1E' ) fg = TRUE;
	for ( ; ; ) {
kensaku_loop:
	    p = USerch(fg,ent_ptr,sctmp);
	    if ( p >= btm_ptr ) {
		df = Input2(sctmp,"見つかりません ");
        	if ( df == ERR || sctmp[0] == '\0' ) return;
		if ( df == FALSE || df == '\x1F' ) {
			fg = FALSE;
			if ( ent_ptr < btm_ptr ) ent_ptr++;
		}
		if ( df == '\x1E' ) {
			fg = TRUE;
			if ( ent_ptr > 0 ) ent_ptr--;
		}
		goto kensaku_loop;

	    }

/*****
        if ( Input(sctmp,"検索文字列 ") != FALSE || sctmp[0] == '\0' )
	    return;
	for ( fg = FALSE ; ; ) {
	    if ( (p = USerch(fg,ent_ptr,sctmp)) >= btm_ptr ) {
		wrtstr("(見つかりません)",30,1,0x15);
		return;
	    }
*****/
/* ---------------------------------------------------------- */
	    chk_ptr = ent_ptr = p; p += strlen(sctmp); cke_ptr = p;
	    lin_ptr = Top_line(ent_ptr);
	    for ( s = lin_ptr,i = 10 ; i > 0 ; i-- )
		s = Bak_line(s);
	    while ( top_ptr < s ) {
		top_ptr = Nxt_line(top_ptr);
		Line_no++;
	    }
	    while ( top_ptr > s ) {
		top_ptr = Bak_line(top_ptr);
		Line_no--;
	    }
	    Set_vram();
	    Dsp_vram(vram);
	    Dsp_free();
	    chk_ptr = cke_ptr = ERR;
/* ------------------ RED2 like 92/12/28 Pumpkin ---------------- */
	    ch = Get_key(&ec);
	    if ( ch == '\x1B'  || ec == 0x7200 ) 
/* -------------------------------------------------------------- */
/**********
	    ch = getch();
	    if ( ch == '\x1B' )
**********/
		return;
	    else if ( ch == '\x0D' || ch == ' ' || ch == '\x1F' )
		fg = FALSE;
	    else if ( ch == '\x08' || ch == '\x1E' )
		fg = TRUE;

	    if ( fg == FALSE )
	        ent_ptr = p;
	    else if ( ent_ptr > 0 )
		ent_ptr--;
	}
    } else if ( no == 2 || no == 3 ) {
        if ( Input(rpsctmp,"現文字列 ") != FALSE )
	    return;
        if ( Input(rpdstmp,"新文字列 ") != FALSE )
	    return;
	if ( rpsctmp[0] == '\0' || rpdstmp[0] == '\0' )
	    return;
	while ( ent_ptr < btm_ptr ) {
	    if ( (p = Serch(ent_ptr,rpsctmp)) >= btm_ptr ) {
		wrtstr("(見つかりません)",30,1,0x15);
		return;
	    }
	    chk_ptr = ent_ptr = p; p += strlen(rpsctmp); cke_ptr = p;
	    lin_ptr = Top_line(ent_ptr);
	    for ( s = lin_ptr,i = 10 ; i > 0 ; i-- )
		s = Bak_line(s);
	    while ( top_ptr < s ) {
		top_ptr = Nxt_line(top_ptr);
		Line_no++;
	    }
	    Set_vram();
	    Dsp_vram(vram);
	    Dsp_free();
	    chk_ptr = cke_ptr = ERR;
	    if ( no == 2 ) {
		i = getch();
		if ( i == '\x1B' )
		    return;
		else if ( i == ' ' ) {
		    ent_ptr = p;
		    continue;
		}
	    }
	    if ( (i = strlen(rpsctmp)) > (j = strlen(rpdstmp)) ) {
		Del_buf(ent_ptr+j,(LONG)(i - j));
	        btm_ptr -= (i - j);
	    } else if ( i < j ) {
		Ins_buf(ent_ptr+i,(LONG)(j - i),rpdstmp);
	        btm_ptr += (j - i);
	    }
            for ( d = rpdstmp ; j-- > 0 ; )
		*Cnv_ptr(ent_ptr++) = *(d++);
	    Wrt_flg = Wrt_mode = TRUE;
	}
    } else if ( no == 4 ) {
	tmp[0] = '\0';
	if ( Input(tmp,"行番号 ") == FALSE ) {
	    i = atoi((char *)tmp) - 1;
	    top_ptr = Line_ptr(&i); Line_no = i;
	    lin_ptr = ent_ptr = top_ptr;
	    for ( i = 10 ; i > 0 && top_ptr > 0 ; i-- ) {
		top_ptr = Bak_line(top_ptr);
		Line_no--;
	    }
	}
    } else if ( no == 5 ) {
	Cur_top_file();
    } else if ( no == 6 ) {
	Cur_btm_file();
    } else if ( no == 7 ) {
#ifdef	FMR70
	cprintf("\x1B[24;1H");
	system("COMMAND.COM");
	Dsp_status();
	Dsp_free();
#endif
#ifdef	TOWNS
/* ------------------ SIDEWORK 92.01.31 Pumpkin -------------- */
	wink_sdwk_menu();
/******
        wrtstr("(実現不能？)",32,1,0x05);
******/
/* ---------------------------------------------------------- */
#endif
    }
}

void	Cmds_PF3()
{
    static int  no=ERR,clic=0;
    int     i;
    UCHAR   tmp[80];
    char    *menu[8];
    char    ttl[5][80]; /* RED2 Like  60 -> 80 に変更  HIKA */
   /***********************************************

    sprintf(ttl[0],"1 TABコ−ド長の設定(現在%d個) ",TAB);
    sprintf(ttl[1],"2 TABコ−ドの展開を%s%s",
		TAB_mode != 0 ? "行わない":"行う",
		"            ");
    sprintf(ttl[2],"3 %sｺ-ﾄﾞの表示を%s%s",
		CONTRL == 0 ? "CR" : "CR&TAB",
		CONTRL != 2 ? "行う" : "行わない",
		"            ");
    sprintf(ttl[3],"4 ｷ-ﾎﾞ-ﾄﾞ ｸﾘｯｸ音を%s%s",
		clic == 0 ? "鳴らす" : "消す",
		"             ");
    sprintf(ttl[4],"5 行番号表示を%s%s",
		dlp_flg == FALSE ? "する" : "しない",
		"             ");
   ***********************************************/
    /* --------------- RED2 Like   90.10.18 HIKA ---------------- */
     sprintf(ttl[0],"L: tab Length   1.TABｺｰﾄﾞ長の設定(現在%d個)  ",TAB);
     sprintf(ttl[1],"T: Tab code     2.TABコ−ドの展開を%s%s",
		 TAB_mode != 0 ? "行わない" : "行う",
		 "             ");
     sprintf(ttl[2],"R: cR&tab code  3.%sｺ-ﾄﾞの表示を%s%s",
		 CONTRL == 0 ? "CR" : "CR&TAB",
		 CONTRL != 2 ? "行う" : "行わない",
		 "             ");
     sprintf(ttl[3],"C: Click        4.ｷ-ﾎﾞ-ﾄﾞ ｸﾘｯｸ音を%s%s",
		 clic == 0 ? "鳴らす" : "消す",
		 "              ");
     sprintf(ttl[4],"N: line No.     5.行番号表示を%s%s",
		 dlp_flg == FALSE ? "する" : "しない",
		 "              ");
    /* ---------------------------------------------------------- */

    i = strlen(ttl[0]); 
    ttl[1][i] = ttl[2][i] = ttl[3][i] = ttl[4][i] = '\0';
    menu[0] = ttl[0]; menu[1] = ttl[1]; menu[2] = "";
    menu[3] = ttl[2]; menu[4] = ttl[3]; 
    menu[5] = "";     menu[6] = ttl[4]; menu[7] = NULL;
/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
	switch (Sel_menu(menu,20,2,&no)) {
		case FALSE:
			break;
		case LEFT: 
                    	Dsp_vram(vram);
			Cmds_PF2();
			return;
		case RIGHT:
                    	Dsp_vram(vram);
			Cmds_PF4();
			return;
		default:
			return;
	}
/*****
    if ( Sel_menu(menu,20,2,&no) != FALSE )
	return;
*****/
/* ---------------------------------------------------------- */

    if ( no == 0 ) {
	tmp[0] = '\0';
	if ( Input(tmp,"TAB長(1-40) ") != FALSE )
	    return;
	if ( (i = atoi((char *)tmp)) < 1 || i > 40 )
	    return;
	TAB = i;
    } else if ( no == 1 ) {
	TAB_mode = (TAB_mode == 0 ? 1 : 0);
    } else if ( no == 2 ) {
	if ( ++CONTRL > 2 )
	    CONTRL = 0;
    } else if ( no == 3 ) {
	KYB_clic(clic);
	clic = (clic == 0 ? 1 : 0);
    } else if ( no == 4 ) {
	dlp_flg = (dlp_flg == FALSE ? TRUE : FALSE);
	if ( dlp_flg != FALSE && Wind_flg == FALSE ) {
	    Wind_flg = TRUE;
	}
    }
}
void	Cmds_PF4()
{
    static int   no=ERR;

  /******************************************
    static char  *menu[]={
        "1 ウインドサイズの変更 ",
        "2 文字表示色の変更     ",
	"",
	"3 ASCIIコ−ドの挿入    ",
	"4 漢字コ−ドの挿入     ",
	"",
	"5 編集バッファの印刷   ",
        NULL };
  ******************************************/
    /* --------------- RED2 Like   90.10.18 HIKA ---------------- */
     static char  *menu[]={
         "W: Window size  1.ウインドサイズの変更 ",
         "C: Color        2.文字表示色の変更     ",
	 "",
	 "A: Ascii code   3.ASCIIコ−ドの挿入    ",
	 "K: Kanji code   4.漢字コ−ドの挿入     ",
	 "",
	 "P: Print out    5.編集バッファの印刷   ",
       NULL };
    /* ---------------------------------------------------------- */

    wrtstr(SPCSTR,30,1,0x1F);
/* --------------- RED2 Like   92.01.31 Pumpkin ------------- */
	switch (Sel_menu(menu,25,2,&no)) {
		case FALSE:
			break;
		case LEFT: 
                    	Dsp_vram(vram);
			Cmds_PF3();
			return;
		case RIGHT:
                    	Dsp_vram(vram);
			CD_menu();
			return;
		default:
			return;
	}
/*****
    if ( Sel_menu(menu,25,2,&no) != FALSE )
	return;
*****/
/* ---------------------------------------------------------- */

    if ( no == 0 )
	ReSize_wind();
    else if ( no == 1 )
	ChgCol_wind();
    else if ( no == 2 )
	ASCII_code();
    else if ( no == 3 )
	KANJI_code();
    else if ( no == 4 )
	Printer_out();
}
void	Cmds_PF6()
{
    int      ch,n,i,j,fg,no;
    unsigned ec;
    LONG     p,s,old_top;
    UCHAR    *d;

    chk_ptr = cke_ptr = ent_ptr; fg = FALSE;
    old_top = top_ptr; no = Line_no;
    wrtstr("範囲指定中",30,0,0x15);
    wrtstr(SPCSTR,30,1,0x1F);
    for ( ; ; ) {
        while ( kbhit() == FALSE );

        while ( kbhit() != FALSE ) {
	    ch = Get_key(&ec);
	    if ( ec == 0x0100 ) {
		goto ENDOF;
	    } else if ( ec == 0x6200 || ec == 0x2410 ) {
		chk_ptr = cke_ptr = ent_ptr; no = Line_no;
	    } else if ( ec == 0x6300 || ec == 0x1610 ) {
		if ( (n = cke_ptr - chk_ptr) > 0 ) {
                    if ( Cut_buff(chk_ptr,cke_ptr) != FALSE ) {
            if ( Yes_no("アンドゥ出来ません  削除を行いますか？") == FALSE )
                            goto ENDOF;
                    }
		    Del_buf(chk_ptr,(LONG)(cke_ptr - chk_ptr));
    		    btm_ptr -= n;
    		    ent_ptr = chk_ptr;
		    lin_ptr = Top_line(ent_ptr);
		    if ( lin_ptr <= top_ptr ) {
			top_ptr = old_top;
			Line_no = no;
		    }
		    Wrt_flg = Wrt_mode = TRUE;
		}
		goto ENDOF;
	    } else if ( ec == 0x6400 || ec == 0x2510 ) {
		if ( (cke_ptr - chk_ptr) > 0 )
		    Cut_buff(chk_ptr,cke_ptr);
		goto ENDOF;
	    } else if ( ec == 0x6500 ) {
		if ( (cke_ptr - chk_ptr) > 0 )
		    Wrt_file(chk_ptr,cke_ptr);
		goto ENDOF;
	    } else if ( ec == 0x5E00 ) {
		wrtstr("指定範囲内の文字列置き換え",30,1,0x15);
        	if ( Input(rpsctmp,"現文字列 ") != FALSE )
	    	    goto ENDOF;
                if ( Input(rpdstmp,"新文字列 ") != FALSE )
		    goto ENDOF;
		ent_ptr = chk_ptr;
		lin_ptr = Top_line(ent_ptr);
		if ( lin_ptr <= top_ptr ) {
		    top_ptr = old_top;
		    Line_no = no;
		}
		old_top = cke_ptr;
		while ( ent_ptr < old_top ) {
		    if ( (p = Serch(ent_ptr,rpsctmp)) >= old_top )
			goto ENDOF;
		    chk_ptr = ent_ptr = p; p += strlen(rpsctmp); cke_ptr = p;
	            lin_ptr = Top_line(ent_ptr);
	            for ( s = lin_ptr,i = 10 ; i > 0 ; i-- )
			s = Bak_line(s);
		    while ( top_ptr < s ) {
		        top_ptr = Nxt_line(top_ptr);
		        Line_no++;
	            }
	            Set_vram();
	            Dsp_vram(vram);
	            Dsp_free();
	            chk_ptr = cke_ptr = ERR;
		    i = getch();
		    if ( i == '\x1B' )
		        goto ENDOF;
		    else if ( i == ' ' ) {
		        ent_ptr = p;
		        continue;
		    }
	            if ( (i = strlen(rpsctmp)) > (j = strlen(rpdstmp)) ) {
		        Del_buf(ent_ptr+j,(LONG)(i - j));
	                btm_ptr -= (i - j);
	            } else if ( i < j ) {
		        Ins_buf(ent_ptr+i,(LONG)(j - i),rpdstmp);
	                btm_ptr += (j - i);
	            }
                    for ( d = rpdstmp ; j-- > 0 ; )
		        *Cnv_ptr(ent_ptr++) = *(d++);
		}
		goto ENDOF;
	    }
	    for ( i = 0 ; i < MOVECMDS && key_tbl[i].key_code != 0 ; i++ ) {
	        if ( key_tbl[i].key_code == ec ) {
		    (*(key_tbl[i].key_proc))();
		    break;
		}
	    }
	    if ( fg == FALSE ) {
		if ( ent_ptr < chk_ptr ) {
		    fg = TRUE;
		    chk_ptr = ent_ptr;
		    old_top = top_ptr;
		    no = Line_no;
		} else
		    cke_ptr = ent_ptr;
	    } else {
		if ( ent_ptr > cke_ptr ) {
		    fg = FALSE;
		    cke_ptr = ent_ptr;
		} else {
		    chk_ptr = ent_ptr;
		    old_top = top_ptr;
		    no = Line_no;
		}
	    }
	}
        Set_vram();
        Dsp_vram(vram);
	Dsp_free();
    }
ENDOF:
    wrtstr(SPCSTR,30,0,0x1F);
    wrtstr(SPCSTR,30,1,0x1F);
    chk_ptr = cke_ptr = ERR;
}
BOOL    edit()
{
    int	    i,n,ch,p;
    unsigned ec;
    UCHAR   tmp[40];    

    while ( kbhit() == 0 );

    Endof_edit = Wrt_mode = FALSE;
    for ( p = n = 0 ; Endof_edit == FALSE &&
		      kbhit() != 0 && n < 10 ; n++ ) {
	ch = Get_key(&ec);
	for ( i = 0 ; key_tbl[i].key_code != 0 ; i++ ) {
	    if ( key_tbl[i].key_code == ec ) {
		if ( p > 0 ) {
		    set_chr(tmp,p);
		    p = n = 0;
		}
		(*(key_tbl[i].key_proc))();
		break;
	    }
	}
	if ( ch != 0xFFFF && key_tbl[i].key_code == 0 ) {
	    tmp[p++] = ch;
	    if ( p >= 30 ) {
		set_chr(tmp,p);
	        p = 0;
	    }
	    n = 0;
	}
        Dsp_free();
    }
    if ( p > 0 )
	set_chr(tmp,p);
    Set_vram();
    Dsp_vram(vram);
    Dsp_wrt_flg();
    Dsp_free();
    if ( Endof_edit != FALSE )
	return TRUE;
    else
        return FALSE;
}
