?? newpad.c
字號:
/* A Screen Editer Subsystem */
#define TURBOC
#include <stdio.h>
#include <dos.h>
#include <string.h>
#include <bios.h>
#include <conio.h>
#define BUF_SIZE 32000
#define LINE_LEN 79-2
#define MAX_LINES 24-1
#define KILL_BUF_SIZE 4*LINE_LEN
/***********************************************************/
char buf[BUF_SIZE];
char *curloc,*endloc;
int scrnx,scrny;
char killbuf[KILL_BUF_SIZE];
char *helpline="F1:save F2:load F3:find F4:replace ^K:kill line ^Y:Yank ^Z:qiut";
/*************************************************************/
void edit(char *fname),help(void);
void gotoxy(int x,int y),clrline(int y);
void edit_clr_col(int x,int y),clrscr(void);
void printline(char *p),delete_char(void);
void search(void),kill_line(void);
void scrolldn(int x,int y);
void scrollup(int topx,int topy,int endx,int endy);
void upline(void);
void downline(void),left(void),right(void);
void display_scrn(int x,int y,char *p);
void pagedown(void),pageup(void),replace(void);
void home(void),gotoend(void),yank(void);
int load(char *fname),save(char *fname);
void edit_gets(char *str);
void draw_border(int,int,int,int,int);
/***************************************************/
main(int argc,char *argv[])
{
union REGS r;
char fname[80];
if(argc<2)
{
draw_border(0,0,78,24,0x1f);
draw_border(20,12,60,14,0x2f);
gotoxy(20,11);
printf("\7FILE NAME:");
gotoxy(21,13);
gets(fname);
edit(fname);
}
if(argc==2)
{
strupr(argv[1]);
if(strstr(argv[1],".EXE")||strstr(argv[1],".COM")||\
strstr(argv[1],".OBJ")||strstr(argv[1],".LIB"))
{
printf("\7Can't edit file :%s",argv[1]);
exit(1);
}
else edit(argv[1]);
}
r.h.ah=6;
r.h.al=0;
r.h.ch=0;
r.h.cl=0;
r.h.dh=24;
r.h.dl=79;
r.h.bh=7;
int86(0x10,&r,&r);
gotoxy(1,1);
}
/******************************************************************/
void edit(char *fname)
{
union k
{
char ch[2];
unsigned i;
}key;
char name[80];
/* try to load the file */
if(!load(fname))curloc=endloc=buf;
strcpy(name,fname);
/* set initial values to X,Y coordinate vars */
scrnx=scrny=0;
display_scrn(0,0,curloc);
help();
gotoxy(1,1);
/* editer main loop . */
do{
#ifdef TURBOC
key.i=bioskey(0);
#endif
if(!key.ch[0])
{
switch(key.ch[1])
{
case 59: /*F1 :save file */
save(name);
break;
case 60: /*F2: load file */
clrline(MAX_LINES);
gotoxy(1,MAX_LINES);
printf("Enter filename :");
edit_gets(name);
strupr(name);
if(strstr(name,".EXE")||strstr(name,".COM")||\
strstr(name,".OBJ")||strstr(name,".LIB"))
{
gotoxy(1,MAX_LINES);
printf("\7Can't edit file: %s",name);
getch();
help();
break;
}
if( * name) load(name);
help();
display_scrn(0,0,curloc);
scrnx=scrny=0;
break;
case 61:
search();
break;
case 62:
replace();
break;
case 71:
home();
break;
case 79:
gotoend();
break;
case 75: /*left*/
left();
break;
case 77:
right();
break;
case 72:
upline();
break;
case 80:
downline();
break;
case 73:
pageup();
break;
case 81:
pagedown();
break;
case 83: /*Del*/
if(curloc<endloc) delete_char();
break;
}
if(curloc<buf)
{
scrnx=scrny=0;
curloc=buf;
}
gotoxy(scrnx+1,scrny+1); /*postion cursor */
}
else
{
switch(key.ch[0])
{
case '\r':
/*see if buffer is full */
if(endloc==buf+BUF_SIZE-2) break;
/* move contents of file below current location down
one byte to make room for the RETURN.*/
memmove(curloc+1,curloc,endloc-curloc+1);
*curloc=key.ch[0]; /*put RETURN in file */
curloc++;
/*clear rest of line */
edit_clr_col(scrnx,scrny);
scrnx=0;
scrny++;
/*move text on screen down */
if(scrny==MAX_LINES-1)
{
/*at bottom of page */
scrny=MAX_LINES-2;
scrollup(1,1,LINE_LEN,scrny+1);
}
else scrolldn(scrnx+1,scrny+1);
gotoxy(scrnx+1,scrny+1);
printline(curloc);
endloc++;
/* advance the end of file pointer */
break;
case '\b':
if(curloc==buf) break;
left();
delete_char();
break;
case 11:
kill_line();
break;
case 25:
yank();
break;
case 26:
clrline(MAX_LINES);
gotoxy(1,MAX_LINES);
printf("If saved file,press[Y]exit\7");
if(tolower(getch())=='y') goto end;
help();
break;
default: /* enter keystroke into file */
/*see if buf is full */
if(endloc==buf+BUF_SIZE-2) break;
/*can't type past end of line */
if(scrnx==LINE_LEN) break;
memmove(curloc+1,curloc,endloc-curloc+1);
*curloc=key.ch[0];
putch(*curloc);
scrnx++;
gotoxy(scrnx+1,scrny+1);
printline(curloc+1);
curloc++;
endloc++;
}
gotoxy(scrnx+1,scrny+1);
}
}while(1);
end:
printf("\n");
}
/****************************************************************************
Display a line pointed to by p.
****************************************************************************/
void printline(register char *p)
{
register int i;
i=scrnx+1;
while(*p!='\r'&&*p&&i<LINE_LEN)
{
putch(*p);
p++;
i++;
}
}
/****************************************************************************
Insert previously killled line.
****************************************************************************/
void yank(void)
{
char *p;
p=killbuf;
while(*p)
{
memmove(curloc+1,curloc,endloc-curloc+1);
*curloc=*p;
if(scrnx<LINE_LEN)
{
putch(*curloc);
scrnx++;
}
curloc++;
endloc++;
p++;
}
}
/*******************************************************/
/* Delete the line at the current location */
void kill_line(void)
{
register int i;
char *p,*killbufptr;
if(*curloc=='\r')
{
delete_char();
return;
}
edit_clr_col(scrnx,scrny); /*clear to CR */
/* find out hoe many characters are in the line */
p=curloc;
i=0;
killbufptr=killbuf;
while(*p!='r'&&p<endloc)
{
i++;
*killbufptr=*p;
p++;
if(killbufptr<killbuf+KILL_BUF_SIZE-2) killbufptr++;
}
*killbufptr='\0';
/* remove the line */
memmove(curloc,curloc+i,endloc-curloc);
endloc-=i;
}
/***********************************************************************
Global search and replace
***********************************************************************/
void replace(void)
{
register int len1;
char str1[80],str2[80];
char *p,*p2;
clrline(MAX_LINES);
gotoxy(1,MAX_LINES);
printf("enter string to replace :");
edit_gets(str1);
clrline(MAX_LINES);
gotoxy(1,MAX_LINES);
printf("enter replacement:");
edit_gets(str2);
p=curloc;
len1=strlen(str1);
while(*str1)
{
/*search for the string */
while(*p && strncmp(str1,p,len1)) p++;
if(!*p)
{
clrline(MAX_LINES);
gotoxy(1,MAX_LINES);
printf("No found string %s",str1);
getch();
help();
break;
} /* not found */
/* remove old string */
memmove(p,p+len1,endloc-p);
endloc-=len1;
/*insert new string */
p2=str2;
while(*p2)
{
memmove(p+1,p,endloc-p+1);
*p=*p2;
p++;
endloc++;
p2++;
}
}
clrscr();
/* find location of top screen */
p=curloc;
for(len1=scrny;len1>=0&&p<buf;)
{
p--;
if(*p=='r') len1--;
}
if(*p=='r') p++;
/* redisplay current screen */
display_scrn(0,0,p);
help();
}
/**************************************************************************
Delete character at the current location .
**************************************************************************/
void delete_char(void)
{
gotoxy(scrnx+1,scrny+1);
if(*curloc=='r')
{
scrollup(1,scrny+1+1,LINE_LEN,MAX_LINES-1);
memmove(curloc,curloc+1,endloc-curloc);
endloc--;
display_scrn(scrnx,scrny,curloc);
help();
}
else
{
memmove(curloc,curloc+1,endloc-curloc);
endloc--;
printline(curloc);
printf("");
}
}
/*************************************************************************
Display help line.
*************************************************************************/
void help(void)
{
gotoxy(1,MAX_LINES);
printf(helpline);
}
/*************************************************************************
Move cuuren location left
*************************************************************************/
void left(void)
{
if(curloc==buf) return;
scrnx--;
if(scrnx<0)
{
scrnx=0;
upline(); /*go up to next line */
/* find end of line */
while(*curloc!='r')
{
curloc++;
scrnx++;
}
}
else curloc--;
}
/**********************************************************************
Move current position right.
**********************************************************************/
void right(void)
{
if(curloc+1>endloc) return;
scrnx++;
/* if at the end of line,go to the next one. */
if(scrnx>LINE_LEN||*curloc=='\r')
{
scrnx=0;
scrny++;
if(scrny==MAX_LINES-1) /* at end of screen */
{
scrny=MAX_LINES-2;
downline();
/* move cursor and current loc to start of new line */
curloc--;
while(*curloc!='\r') curloc--;
curloc++;
scrnx=0;
}
else curloc++;
}
else curloc++;
}
/**********************************************************************/
void search(void)
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -