⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 codearea.c

📁 linux 下类似于TC功能的编译器,但还没有完成,希望大家共同研究
💻 C
📖 第 1 页 / 共 3 页
字号:
#include "codearea.h"#define DEBUGPAGE \{\	PageList p;\	int k=0;\	p=pagelist;\	endwin();\	do\	{\		fprintf(stderr,"index:%d ptr:%x\n\r",k++,p);\		p=p->next;\	}while (p!=pagelist);\	fprintf(stderr,"begin:%x\n\r",beginpage);\	fprintf(stderr,"totalrow:%d  curpage:%d  currow:%d\n\r",totalrow,curpage,currow);\	exit(0);\}#define DEBUGBLOCK \{\	endwin();\	fprintf(stderr,"starty:%d startx:%d endy:%d endx:%d\n\r",seltext.begline,seltext.begcol,seltext.endline,seltext.endcol);\	exit(0);\}#define DEBUGMSG \{\	int fd=open("/dev/console",O_WRONLY);\	write(fd,"Hello world!",12);\	close(fd);\}int CodeArea::InitCodeArea(int Y,int X,int Height,int Width,int Color,int FontStyle,int WinType){	int ret=FALSE;	if(InitBaseWindow(Y,X+1,Height,Width-1,Color,0,FontStyle,WinType,"")==FALSE)		goto failure;	keypad(basewin,TRUE);	draw_box(0,0,Height,Width, COLOR_PAIR(Color)|' ',COLOR_PAIR(Color));	wmove(basewin,0,0);	curs_set(1);	wrefresh(basewin);	if(InitLocalVar()==FALSE)		goto failure;	ret=TRUE;failure:	return ret;}BOOL CodeArea::InitLocalVar(){	int ret=FALSE;	insertmode=TRUE;	currow=0;	curcol=0;	offline=0;	offcol=0;	totalrow=1;			//in order to process untitled file	totalcol=0;	maxcol=0;	curpage=0;	totalpage=0;	pagelist=NULL;	beginpage=NULL;	multiremark=FALSE;	sym=SYM_NULL;	modified=FALSE;	seltext.selected=FALSE;		//the selected code area	/*alloc the first page*/	if ((pagelist=AllocPage())==NULL)		goto failure;	pagelist->next=pagelist;	pagelist->prev=pagelist;	beginpage=pagelist;	memset(filenm,0,sizeof(char)*MAXFIELDLEN);	wmove(basewin,0,0);	ret=TRUE;failure:	return ret;}int CodeArea::CodeAreaDriver(int key){	int ret=0;	int i=0;	int tempy,tempx;	tempy=tempx=0; 	switch(key)        {		case KEY_LEFT:			if(curcol>0)			{				getyx(basewin,tempy,tempx);				curcol--;				if(tempx<=0 && offcol>0)				{					offcol--;					ShowPage(offcol*TABSIZE);					wmove(basewin,tempy,tempx+TABSIZE-1);				}				else				{					wmove(basewin,tempy,tempx-1);				}				ret = UPDATE_STATUS;			}			break;		case KEY_RIGHT:			if(curcol<totalcol)			{				getyx(basewin,tempy,tempx);				curcol++;				if(tempx+1>=GetWidth())				{					offcol++;					ShowPage(offcol*TABSIZE);					wmove(basewin,tempy,tempx-TABSIZE+1);				}				else				{					wmove(basewin,tempy,tempx+1);				}				ret = UPDATE_STATUS;			}			break;		case KEY_UP:			if(currow>0)			{				getyx(basewin,tempy,tempx);				currow--;				tempy--;				totalcol=GetCurColWidth(NULL,currow);				if( tempy < 0 )				{					tempy++;					if( (int)((currow-tempy)/GetHeight()) !=curpage)					{						if (beginpage->prev!=pagelist->prev)						{							beginpage=beginpage->prev;							curpage--;						}					}					if(offcol>0)					{						curcol=0;						offcol=0;						tempx=curcol;					}					else					{						if (curcol>totalcol)						{							tempx=totalcol;							curcol=totalcol;						}					}					ShowPage();				}				else				{					if(offcol>0)					{						currow++;						curcol=0;						offcol=0;						ShowPage();						currow--;						tempx=curcol;					}					else					{						if (curcol>totalcol)						{							tempx=totalcol;							curcol=totalcol;						}					}				}				wmove(basewin,tempy,tempx);									ret = UPDATE_STATUS;			}			break;		case KEY_DOWN:			if(currow+1<totalrow)			{				getyx(basewin,tempy,tempx);				currow++;				tempy++;				totalcol=GetCurColWidth(NULL,currow);				if(tempy>=GetHeight())				{					tempy--;					if( (int)((currow-tempy)/GetHeight()) !=curpage)					{						if (beginpage->next!=pagelist)						{							beginpage=beginpage->next;							curpage++;						}					}					if(offcol>0)					{						curcol=0;						offcol=0;						tempx=curcol;					}					else					{						if (curcol>totalcol)						{							tempx=totalcol;							curcol=totalcol;						}					}					ShowPage();				}				else				{					if(offcol>0)					{						currow--;						curcol=0;						offcol=0;						ShowPage();						currow++;						tempx=curcol;					}					else					{						if (curcol>totalcol)						{							tempx=totalcol;							curcol=totalcol;						}					}				}				wmove(basewin,tempy,tempx);				ret = UPDATE_STATUS;			}			break;		case KEY_HOME:			curcol=0;			getyx(basewin,tempy,tempx);			if(offcol>0)			{				offcol=0;				ShowPage();			}			wmove(basewin,tempy,curcol);			ret =UPDATE_STATUS;			break;		case KEY_END:			getyx(basewin,tempy,tempx);			curcol=totalcol;			if(totalcol>=GetWidth())			{				offcol=(int)((totalcol-GetWidth())/TABSIZE)+1;				ShowPage(offcol*TABSIZE);				wmove(basewin,tempy,totalcol-offcol*TABSIZE);			}			else			{				if(offcol>0)				{					offcol=0;					ShowPage(0);				}				wmove(basewin,tempy,curcol);			}			ret = UPDATE_STATUS;			break;		case 10://KEY_ENTER:			getyx(basewin,tempy,tempx);			if(totalrow%GetHeight()==0)		//need a new page			{				PageList newp=NULL;				PageList dptr=pagelist->prev;				newp=AllocPage();				InsertPage(dptr,newp);			}			ShiftLinesDown(LocatePage(NULL,currow),currow%CODE_QSET,1);			CopyLineDown(NULL,offcol*TABSIZE+tempx,totalcol-offcol*TABSIZE-tempx,NULL,0,1,TRUE);			offcol=0;			if(tempy+1>=CODE_QSET)			{				currow++;				if( (int)((currow-tempy)/GetHeight()) !=curpage)				{					if (beginpage->next!=pagelist)					{						beginpage=beginpage->next;						curpage++;					}				}				ShowPage();			}			else			{				ShowPage();				currow++;				tempy=tempy+1;			}			curcol=0;			totalrow=totalrow+1;			totalcol=GetCurColWidth(NULL,currow);			wmove(basewin,tempy,curcol);			ret = UPDATE_STATUS;			modified=TRUE;			break;		case KEY_F(1):			break;		case KEY_F(2):			LostActive();			ret = ACTIVE_TOPMENU;			break;		case KEY_F(3)://open file			LostActive();			msg.fileobj.type=DIALOG_FILEOPEN;			ret=UPDATE_CODEAREA|FILE_DIALOG|UPDATE_STATUS;			break;		case KEY_F(4)://save file			LostActive();			msg.fileobj.type=DIALOG_FILESAVE;			ret=UPDATE_CODEAREA|FILE_DIALOG|UPDATE_STATUS;			break;		case KEY_F(5):			break;		case KEY_F(6):			break;		case KEY_F(7):			LostActive();			msg.fileobj.type=DIALOG_EDITFIND;			ret=UPDATE_CODEAREA|EDIT_DIALOG|UPDATE_STATUS;			break;		case KEY_F(8):			LostActive();			msg.fileobj.type=DIALOG_EDITREPLACE;			ret=UPDATE_CODEAREA|EDIT_DIALOG|UPDATE_STATUS;			break;		case KEY_BACKSPACE:		{			int ln;			PageList pg;			getyx(basewin,tempy,tempx);			pg=LocatePage(NULL,currow);			ln=currow % CODE_QSET;			ret = UPDATE_STATUS;			if(tempx>0)			{				for(i=0;i<totalcol-curcol;i++)				{					pg->data[ln][curcol-1+i]=pg->data[ln][curcol+i];				}				pg->data[ln][totalcol-1]='\0';				totalcol--;/***Strictly speaking,maxcol should be set according to the maxium columns of the whole file.**However, in order to get exact value after "backspace" a character,width comparision action**must be taken among all lines in the file. It is time consuming very much. I just ignore it.**The result of doing so may lays that the scale of hscroll is rough in some circumstances.*///				maxcol=(maxcol>totalcol)?(maxcol):(totalcol);				tempx--;				curcol--;				ShowLine(pg->data[ln],curcol,GetWidth()-tempx,tempy,tempx);				wmove(basewin,tempy,tempx);				modified=TRUE;			}			else if(offcol>0)			{				for(i=0;i<totalcol-curcol;i++)				{					pg->data[ln][curcol-1+i]=pg->data[ln][curcol+i];				}				pg->data[ln][totalcol-1]='\0';				totalcol--;				curcol--;				offcol--;				ShowPage(offcol*TABSIZE);				wmove(basewin,tempy,tempx+TABSIZE-1);				modified=TRUE;			}			else if(currow>0)			{				int t=GetCurColWidth(NULL,currow-1);				offcol=(t<width)?(0):(  (int)((t-width)/TABSIZE)+1  );				CopyLineUp(NULL,tempx,totalcol-tempx,NULL,t,1,TRUE);				ShiftLinesUp(LocatePage(NULL,currow+1),currow+1,1);				if(tempy<=0)				{					currow--;//after shiftlinesup,beginpage has already pointed to the correct page//					if( (int)((currow-tempy)/GetHeight()) !=curpage)//					{//						if (beginpage->prev!=pagelist->prev)//						{//							beginpage=beginpage->prev;//							curpage--;//						}//					}					ShowPage(offcol*TABSIZE);				}				else				{					ShowPage(offcol*TABSIZE);					currow--;					tempy=tempy-1;				}				//shiftlinesup may free page,so need recalculate curpage				curpage=(int)((currow-tempy)/CODE_QSET);				curcol=t;				totalrow=totalrow-1;				totalcol=GetCurColWidth(NULL,currow);				maxcol=(maxcol>totalcol)?(maxcol):(totalcol);				wmove(basewin,tempy,t-offcol*TABSIZE);				modified=TRUE;			}			else			{				ret = NO_TRANSPORT;			}			break;		}		case KEY_DC:		{			int ln;			PageList pg,ppg;			getyx(basewin,tempy,tempx);			pg=LocatePage(NULL,currow);			ppg=pg;			ln=currow % CODE_QSET;			ret = UPDATE_STATUS;			if(curcol<totalcol)			{				for(i=curcol+1;i<totalcol;i++)				{					pg->data[ln][i-1]=pg->data[ln][i];				}				pg->data[ln][totalcol-1]='\0';				totalcol--;//				maxcol=(maxcol>totalcol)?(maxcol):(totalcol);				ShowLine(pg->data[ln],curcol,GetWidth()-tempx,tempy,tempx);				wmove(basewin,tempy,tempx);				modified=TRUE;			}			else if(currow+1<totalrow)//at end of line & not end line of file			{				int end=GetCurColWidth(NULL,currow+1);				ppg=(ln+1>=CODE_QSET)?(ppg->next):(ppg);				CopyLineUp(ppg->data[(ln+1)%CODE_QSET],0,end,pg->data[ln],curcol,1,TRUE);				if(currow+2<totalrow)				{					//shiftlinesup may free page,so need recalculate curpage					ShiftLinesUp(LocatePage(NULL,currow+2),currow+2,1);					curpage=(int)((currow-tempy)/CODE_QSET);				}				ShowPage(offcol*TABSIZE);				totalrow=totalrow-1;				totalcol=(end+curcol>=CODE_QUANTUM)?(CODE_QUANTUM-1):(end+curcol);				maxcol=(maxcol>totalcol)?(maxcol):(totalcol);				wmove(basewin,tempy,tempx);				modified=TRUE;			}			else			{				ret = NO_TRANSPORT;			}			break;		}		case KEY_PPAGE:						if(beginpage->prev!=pagelist->prev)			{				getyx(basewin,tempy,tempx);				currow=currow-GetHeight();				if(currow<0)					currow=0;				beginpage=beginpage->prev;				curpage--;				offcol=0;				curcol=0;				totalcol=GetCurColWidth(NULL,currow);				ShowPage();				wmove(basewin,tempy,curcol);				ret = UPDATE_STATUS;			}			break;		case KEY_NPAGE:			getyx(basewin,tempy,tempx);			if(beginpage->next!=pagelist && (currow-tempy+GetHeight())<totalrow)			{				int endline=currow-tempy+GetHeight()*2-1;				int nextpgl=currow-tempy+GetHeight();				currow=currow+GetHeight();				beginpage=beginpage->next;				curpage++;				offcol=0;				curcol=0;				ShowPage();				if(endline<totalrow || currow<totalrow)				{					wmove(basewin,tempy,curcol);				}				else				{					currow=totalrow-1;					wmove (basewin, totalrow-nextpgl-1, curcol);				}				totalcol=GetCurColWidth(NULL,currow);				ret = UPDATE_STATUS;			}			break;		case KEY_TAB:		{			int ln;			PageList pg;			getyx(basewin,tempy,tempx);			pg=LocatePage(NULL,currow);			ln=currow % CODE_QSET;			ret = NO_TRANSPORT;			if(totalcol+TABSIZE<CODE_QUANTUM)			{				for(i=totalcol-1;i>=curcol;i--)					pg->data[ln][i+TABSIZE]=pg->data[ln][i];				for(i=0;i<TABSIZE;i++)					pg->data[ln][curcol+i]=' '|COLOR_PAIR(color);				pg->data[ln][totalcol+TABSIZE]='\0';				totalcol+=TABSIZE;				ShowLine(pg->data[ln],curcol,GetWidth()-tempx,tempy,tempx);				curcol+=TABSIZE;				wmove(basewin,tempy,tempx+TABSIZE);				modified=TRUE;				ret = UPDATE_STATUS;			}			break;		}		case KEY_INSERT:		{			if (insertmode)			{				curs_set(2);				insertmode=FALSE;				msg.statusobj.type=STATUS_REPLACE;				ret=UPDATE_STATUS;			}			else			{				curs_set(1);				insertmode=TRUE;				msg.statusobj.type=STATUS_INSERT;				ret=UPDATE_STATUS;			}			break;		}		case 13:endwin();fprintf(stderr,"ctrl+s");exit(0);			break;		default:			int line;			PageList page;			getyx(basewin,tempy,tempx);			page=LocatePage(NULL,currow);			line=currow % CODE_QSET;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -