📄 editor.c
字号:
#include <conio.h>
#include<process.h>
#include<stdlib.h>
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#define BUF_SIZE 10000
#define LINE_LEN 80
#define MAX_LINES 23
#define up 0
#define down 1
/************************************************************//*预定义字符串*/
char buf[BUF_SIZE];
char buf2[BUF_SIZE];/*新加的*/
int mousex,mousey;/*新加的*/
char *curloc,*endloc;
char *headline,*tailline;/*头行和尾行指针*/
int scrnx,scrny;
int linelenth; /*新加的*/
char *helpline="help-> F1: save Esc: quit ";
/************************************************************//*函数声明*/
void draw_border(int,int,int,int,int);
int load(char *fname);
void help(void);
void display_scrn(int x,int y,char *p);
void moveline(int);/*上下移动光标*/
void editnew(void);
void opennew(void);
int linelong(void);
int high(void);
void save(char *);
/************************************************************/
void main(void)
{
char fname[80];
char ch;
int esc=0;
headline=buf;
{ /*显示选择边框*/
textbackground(1);
clrscr();
draw_border(10,12,70,14,0x2f);
gotoxy(11,13);
printf("open (enter o)or edit(enter e) file? ");
}
for(;;)
{/*分支————>打开或者编辑*/
ch=getch();
if(ch=='o')
{
opennew();
break;
}
else
if(ch=='e')
{
editnew();
break;
}
}
}
void draw_border(int beginx,int beginy,int endx,int endy,int attr)
{
int i;
for(i=beginx;i<endx;i++)
{ gotoxy(i,beginy);
putchar(196);
gotoxy(i,endy);
putchar(196);
}
for(i=beginy;i<=endy;i++)
{
gotoxy(beginx,i);
putchar(179);
gotoxy(endx,i);
putchar(179);
}
gotoxy(beginx,beginy);
putchar(218);
gotoxy(beginx,endy);
putchar(192);
gotoxy(endx,beginy);
putchar(191);
gotoxy(endx,endy);
putchar(217);
}
int load(char *fname)
{
/*定义变量*/
FILE *fp;
char ch,*p;
int i;
/*画边框*/
draw_border(0,0,78,24,0x2f);
clrscr();
/*打开文件*/
if((fp=fopen(fname,"rb"))==NULL) return 0;/*如果打开文件失败,返回0*/
p=buf;
while(!feof(fp)&&p!=buf+BUF_SIZE-2)/*文件没有结束,并且数组没有越界时,读文件,存到buf数阻里*/
{
ch=getc(fp);
if(ch=='\x09') /*制表键*/
for(i=0;i<8;i++,p++) *p='\x20';/*这是做什么呢???*/
else if(ch!='\n'&&ch!=EOF)
{
*p=ch;
p++;
}
}
*p='\0';
fclose(fp);
clrscr();
/*help();*/
curloc=endloc=p;
curloc=buf;
/*scrnx=scrny=0;*/
}
void help(void)
{
window(1,24,80,25);
textcolor(4);
textbackground(3);
clrscr();
printf(helpline);
}
void display_scrn(int x,int y,char *p)
{
register int i,j;
window(1,1,80,23);
gotoxy(x,y);
textcolor(WHITE);
textbackground(BLUE);
i=0;
while(y<MAX_LINES+1&&*p)
{
if(y==23)
tailline=p;/*指向显示的最后一行的指针*/
switch(*p)
{
case '\r':/*回车*/
{
{/*清理字符结尾部分*/
j=i+1;
gotoxy(j,y);
clreol();
}
printf("\n");/*打印回车换行*/
y++;
gotoxy(1,y);
i=0;
break;
}
case '\x09':/*空格*/
{
printf(" ");
i++;
break;
}
case '\0': /*自定义结束符*/
{
break;
}
default:
if(i<LINE_LEN) putch(*p);
i++;
}
if(*p=='\0')
break;
p++;
}
/*gotoxy(1,1);*/
}
void moveline(int key)/*上下移动光标后的显示*/
{
char *p;
int x,y;
switch(key)
{
case 0 : /*向上移动line指针*/
{
p=headline;
if(p<=buf)
{
headline=buf;
display_scrn(1,1,headline);
break;
}
else
{
p--;
p--;
while(*p!='\r')
p--;
p++;
/*
if(p<buf)
p=buf;
*/
headline=p;
display_scrn(1,1,headline);
if(high()>=23)
{
p=tailline;
p--;
p--;
while(*p!='\r')
p--;
p++;
tailline=p;
}
}
}
break;
case 1 :/*向下移动line指针*/
{
if(high()>=23)
{
{ /*移动tailline指针*/
p=tailline;
p++;
while(*p!='\r')
{
if(*p=='\0')
break;
else
p++;
}
if(*p=='\0')/*如果是因为文件结束跳出的while循环*/
{
break;
}
else
p++; /*如果是因为遇到换行符退出循环*/
if(*p=='\0')
{
break;
}
else
tailline=p;
}
{/*移动headline指针*/
p=headline;
p++;
while(*p!='\r')
p++;
p++;
headline=p;
display_scrn(1,1,headline);
}
}
else
display_scrn(1,1,headline);
break;
}
}
}
void opennew(void)/*打开新文件*/
{
char fname[80];
unsigned m;
int esc=0;
{ /*显示那个边框*/
textbackground(1);
clrscr();
draw_border(20,12,60,14,0x2f);
gotoxy(21,13);
printf("enter file name:");
}
{ /*加载文件,显示文件内容*/
gets(fname);
load(fname); /*加载文件*/
help(); /*显示帮助栏*/
display_scrn(1,1,curloc);/*显示文本*/
}
for(;;)
{
switch(bioskey(0))
{
case 19200 :/*left 左键*/
{
if(wherex()<=1)
gotoxy(1,wherey());
else
gotoxy(wherex()-1,wherey());
break;
}
case 18432 :/*upper上键*/
{
if(wherey()<=1)
{
moveline(up);
gotoxy(wherex(),1);
}
else
{
gotoxy(wherex(),wherey()-1);
}
break;
}
case 19712 :/*right右键*/
{
if(wherex()>=linelong())
gotoxy(linelong(),wherey());
else
gotoxy(wherex()+1,wherey());
break;
}
case 20480 :/*down下键*/
{
if(wherey()>=23)
{
moveline(down);
gotoxy(wherex(),23);
}
else
gotoxy(wherex(),wherey()+1);
break;
}
case 0x3b00:/*F1键保存*/
{
{
window(1,24,80,25);
textcolor(128);
textbackground(2);
clrscr();
printf("name of the file to save:");
gets(fname);
}
save(fname);
break;
}
case 0x011b: /*esc 退出*/
{
{
window(1,24,80,25);
textcolor(128);
textbackground(2);
clrscr();
printf("save file (y/n)");
}
for(;;)
{
m=bioskey(0);
if(m==0x1579)
{
window(1,24,80,25);
textcolor(128);
textbackground(2);
clrscr();
printf("name of the file to save:");
gets(fname);
save(fname);
esc=1;
break;
}
else
if(m==0x316e)
{
esc=1;
break;
}
}
if(esc==1)
break;
}
/*
case 0x3c00:
{
{
window(1,24,80,25);
textcolor(128);
textbackground(2);
clrscr();
printf("what to search:");
}
gets(fname);
find(fname);
}
*/
default:/*未完待续*/
{
break;
}
}
if(esc==1)
break;
}
}
int linelong()/*求光标所在行的长度*/
{
char *p;
int i,j=1;
p=headline;
for(;;)/*第一个for把指针移动到光标位置所对应的数组位置*/
{
if(j==wherey())
{
break;
}
if(*p=='\r')
{
j++;
p++;
}
else
p++;
}
for(i=1;;i++)/*第二个for计算光标所在行的长度*/
{
if(*p=='\r')
break;
p++;
}
return i;
}
int high()/*求文件行数*/
{
char *p=buf;
int y=0;
for(;;p++)
{
if(*p=='\0')
break;
else
if(*p=='\r')
y++;
if(y>=23)
break;
}
return y;
}
void editnew()/*编辑新文件*/
{
char ch1[100];
unsigned m;
int i;
int HIGH,x,y;
char *p=buf,*q;/*指针,先放着,一会可能用到*/
char fname[80];
int esc=0;
{/*显示帮助和编辑窗口*/
help();
window(1,1,80,23);
gotoxy(1,1);
textcolor(WHITE);
textbackground(BLUE);
clrscr();
}
*p='\0';/*字符串赋初值*/
for(i=0;;i++)
{
m=bioskey(0);
switch(m)
{
case 0x1c0d:/*如果是回车键*/
{
*p='\r';
p++;
*p='\0';
display_scrn( 1, 1,buf);
break;
}
case 0x0e08:/*如果是退格键*/
{
p--;
if(p<=buf)
{
p=buf;
*p='\0';
}
else
{
*p='\0';
clrscr();
display_scrn( 1, 1,buf);
}
break;
}
case 19200 :/*left 左键*/
{
if(wherex()<=1)
{
gotoxy(1,wherey());
}
else
gotoxy(wherex()-1,wherey());
break;
}
case 18432 :/*upper上键*/
{
if(wherey()<=1)
{
moveline(up);
gotoxy(wherex(),1);
}
else
{
gotoxy(wherex(),wherey()-1);
}
break;
}
case 19712 :/*right右键*/
{
if(wherex()>=linelong())
gotoxy(linelong(),wherey());
else
gotoxy(wherex()+1,wherey());
break;
}
case 20480 :/*down下键*/
{
if(wherey()>=23)
{
moveline(down);
gotoxy(wherex(),23);
}
else
gotoxy(wherex(),wherey()+1);
break;
}
case 0x3b00:/*F1键保存*/
{
{
window(1,24,80,25);
textcolor(128);
textbackground(2);
clrscr();
printf("name of the file to save:");
gets(fname);
/*
gotoxy(1,MAX_LINES);
clreol();
printf("name of the file to save:");
gets(fname);
*/
}
save(fname);
break;
}
/*
case 0x3c00:
{
}
*/
case 0x011b: /*esc 退出*/
{
{
window(1,24,80,25);
textcolor(128);
textbackground(2);
clrscr();
printf("save file ? (y/n)");
/*
gotoxy(1,MAX_LINES);
clreol();
printf("save file (y/n)");
*/
}
for(;;)
{
m=bioskey(0);
if(m==0x1579)
{
window(1,24,80,25);
textcolor(128);
textbackground(2);
clrscr();
printf("name of the file to save:");
gets(fname);
save(fname);
esc=1;
break;
}
else
if(m==0x316e)
{
esc=1;
break;
}
}
if(esc==1)
break;
}
default:/*其他情况*/
{
*p=m;
p++;
*p='\0';
display_scrn( 1, 1,buf);
}
}
if(esc==1)
break;
}
}
void save(char *fname)/*保存文件*/
{
int i;
FILE *fp;
char *p;
/*
if(!*fname)
{
gotoxy(1,MAX_LINES);
clreol();
printf("filename:");
gets(name);
}
else strcpy(name,fname);
*/
if((fp=fopen(fname,"wb"))==NULL) return 0;
p=buf;
while(p!=endloc)
{
if(*p!='\r')
putc(*p,fp);
else
{
putc('\r',fp);
putc('\n',fp);/*有必要吗??*/
}
p++;
}
fclose(fp);
printf("file %s already saved",fname);
getch();
help();
}
/*
void find(char *fname)
{
int i;
char *p=buf;
char *q=p;
for(;buf[i]==fname[i];i++)
p++;
if()
}
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -