#include #include #include "keyscode.h" #include "menu.h" struct line{ int charnum; char *base; }; typedef struct line SequListElemType; #include "sequlist.h" #define MAXLINES 1000 int key; int cureditline=0; int curscrtopline=0; int cureditcol=0; int funckey; int editwinx=2; int editwiny=3; int editwinlines=20; int mainover=0; FILE *curfile; char filename[255]="test.txt"; SequList *Document; char ascii(int k); printdoc(int startfileline,int startscrline); readdoc(FILE *fp); editline(int curline,int maxcolnum) { char s[2000]; setscrcolor(BLUE,YELLOW); strcpy(s,Document->elem[curline].base); getstring(editwinx,editwiny+curline-curscrtopline,s,maxcolnum); free(Document->elem[curline].base); Document->elem[curline].base=(char *)malloc(strlen(s)+1); strcpy(Document->elem[curline].base,s); } printdoc(int startfileline,int startscrline) { int i; int j; cursor_hide(); i=startscrline; j=startfileline; setscrcolor(BLUE,YELLOW); gotoxy(editwinx,editwiny+startscrline); while(i<=editwinlines && ilength) { clearscrline(editwinx,editwiny+i,78); putsxy(editwinx,editwiny+i++,Document->elem[j++ -1].base); } while(i<=editwinlines) clearscrline(editwinx,editwiny+i++,78); cursor_show(); } getline(FILE *fp,char *l) { int i=0; while(!feof(fp)) if((l[i++]=fgetc(fp))=='\n') break; l[i-1]='\0'; } closedoc(FILE *fp) { int i; fclose(fp); for(i=0;ilength;i++) free(Document->elem[i].base); SequListDestroy(Document); } readdoc(FILE *fp) { char *newline; char s[20000]; int linenum=0; struct line curline; if(!fp) return; while(!feof(fp)) { getline(fp,s); linenum++; newline=(char *)malloc(strlen(s)+1); strcpy(newline,s); curline.base=newline; curline.charnum=strlen(newline); SequListInsert(Document,linenum,curline); } } char ascii(int k) { char c; c=(k<<8)>>8; if(c>=32&&c<127) return c; else return 0; } insertchar(char *s,char c,int pos) { int i; for(i=strlen(s)+1;i>pos;i--) s[i]=s[i-1]; s[i]=c; } deletechar(char *s,int pos) { int i; int j; j=strlen(s); for(i=pos;i<=j;i++) s[i]=s[i+1]; } clearscrline(int x,int y,int num) { int i; for(i=0;i0) { cureditline--; gotoxy(editwinx,editwiny+cureditline); editline(cureditline,78); } break; case KEYDown: if (cureditlinelength-1) { cureditline++; gotoxy(editwinx,editwiny+cureditline); editline(cureditline,78); } break; case KEYCtrl_y: deleteline(cureditline+1); if(Document->length==0) insertnewline(cureditline+1); if(cureditline>=Document->length) insertnewline(cureditline+1); printdoc(cureditline+1,cureditline-curscrtopline); gotoxy(editwinx,editwiny+cureditline); editline(cureditline,78); break; case KEYLeft: case KEYRight: break; case KEYCtrl_s: break; case ESC: cursor_hide(); dofunc(getmenuitem()); break; case KEYAlt_x: mainover=1; break; default: gotoxy(editwinx,editwiny+cureditline-curscrtopline); editline(cureditline,78); break; } } closedoc(curfile); setscrcolor(BLACK,WHITE); clrscr(); cursor_show(); }