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

📄 editzqw.c

📁 C语言不知道你学得怎么样呀
💻 C
📖 第 1 页 / 共 2 页
字号:
	}
	else curloc++;
    }
    else curloc++;
}

/* Find a string. */
void search(void)
{
    char str[80];
    register char *p;
    int len,i;

    clrline(MAX_LINES);   /* clear message line */
    gotoxy(0,MAX_LINES);
    printf("search string:");
    edit_gets(str);
    if(!*str) return;

    p=curloc;
    len=strlen(str);
    
	/* search for the string */
    while(*p && strncmp(str,p,len)) p++;
    if(!*p) return;    /* not found */
    
	/* back up to start of line */
    i=0;
    while(p>buf && *p!='\r') {
	p--;
	i++;
    }
    p++;
    i--;
    
	/* reposition current location to start of match */
    curloc=p+i;
    scrnx=i;
    scrny=0;
    
	/* display screen of text at locatin of match */
    clrscr();
    display_scrn(0,0,p);
    help();
}

/* Move up one line. If possible, keep scrnx same
   as in previous line.
*/
void upline(void)
{
    register int i;
    char *p;

    if(curloc==buf) return;   /* at first byte in buffer */

    p=curloc;

    if(*p=='\r') p--;   /* if at end of line, back up */

    /* back up locator to start of current line */
    for(;*p!='\r' && p>buf;p--);
    if(*p!='\r') return;    /* at first line, cannot go up */
    curloc=p;
    curloc--;   /* skip past CR */
    i=scrnx;    /* save X coordinate */
    
	/* find start of next line */
    while(*curloc!='\r' && curloc>=buf) curloc--;
    scrny--;scrnx=0;
    curloc++;

    /* at top of screen, must scroll up */
    if(scrny<0) {
	scrolldn(0,0);
	scrny=0;
	gotoxy(0,0);
	printline(curloc);
    }

    /* position the cursor and current location at 
       same scrnx position as previous line if possible */
    while(i && *curloc!='\r') {
	curloc++;
	scrnx++;
	i--;
    }
}

/* Move down one line. keep previous scrnx
   location if possible.
*/
void downline(void)
{
    register int i;
    char *p;
    i=scrnx;
    p=curloc;

    /* advance current location to start of next line */
    while(*p!='\r' && p<endloc) p++;
    if(p==endloc) return;   /* can't go down */
    p++;
    curloc=p;
    scrny++;scrnx=0;

    /* if moving down past the bottom of the screen */
    if(scrny==MAX_LINES) {
	scrny=MAX_LINES-1;
	scrollup(0,0,LINE_LEN,MAX_LINES-1);
	gotoxy(scrnx,scrny);
	printline(curloc);
    }

    /* advance to corresponding character in next line */
    while(i && *curloc!='\r' && curloc<endloc) {
	curloc++;
	scrnx++;
	i--;
    }
}

/* Display a screen full of text (up to 24 lines)
   starting at the specified location.
*/
void display_scrn(int x,int y,char *p)
{
    register int i;
    gotoxy(x,y);

    i=0;
    while(y<MAX_LINES && *p) {
	switch(*p) {
	    case '\r': printf("\n");
	        y++;
		i=0;
		break;
	    default: if(i<LINE_LEN) putch(*p);
	        i++;
	}
	p++;
    }
}

/* Page down MAX_LINES lines. */
void pagedown(void)
{
    register int i;

    clrscr();
    
    /* count down MAX_LINES lines */
    for(i=0;i<MAX_LINES && curloc<endloc;) {
	if(*curloc=='\r') i++;
	curloc++;
    }
    help();
    scrnx=0;scrny=0;
    display_scrn(0,0,curloc);
}

/* Page up MAX_LINES lines. */
void pageup(void)
{
    register int i;

    clrscr();
    /* if current lication points to a CR,
       move current location back on position */
    if(*curloc=='\r' && curloc>buf) curloc--;

    /* go back MAX_LINES in buffer */
    for(i=0;i<MAX_LINES+1 && curloc>buf;) {
	if(*curloc=='\r') i++;
	curloc--;
    }
    
    /* if not at the top line, increment the 
       current location pointer past the CR */
    if(i==MAX_LINES+1) curloc+=2;

    help();
    scrnx=0;scrny=0;
    display_scrn(0,0,curloc);
}

/* Go to the top of the file. */
void home(void)
{
    clrscr();
    curloc=buf;
    scrnx=scrny=0;
    display_scrn(0,0,curloc);
    help();
}

/* Go to the end of the file. */
void gotoend(void)
{
    clrscr();
    curloc=endloc;
    pageup();
}

/* Load a file. */
int load(char *fname)
{
    FILE *fp;
    char ch,*p;

    if((fp=fopen(fname,"rb"))==NULL)
	return 0;

    p=buf;
    while(!feof(fp) && p!=buf+BUF_SIZE-2) {
	ch=getc(fp);
	if(ch!='\n' && ch!=EOF) {
	    *p=ch;
	    p++;
	}
    }
    *p='\0';
    fclose(fp);
    curloc=buf;
    endloc=p;
    return 1;
}

/* Save a file. */
int save(char *fname)
{
    FILE *fp;
    char *p,name[80];

    if(!*fname) {
	printf("filename:");
	gets(name);
    }
    else strcpy(name,fname);

    if((fp=fopen(name,"wb"))==NULL)
	return 0;
    
    p=buf;
    while(p!=endloc) {
	if(*p!='\r')
	    putc(*p,fp);
	else {
	    putc('\r',fp);
	    putc('\r',fp);
	}
	p++;
    }
    fclose(fp);
    return 1;
}

/* Read a string form the keyboard, but do not 
   scroll the display when a RETURN is entered.
*/
void edit_gets(char *str)
{
    char *p;

    p=str;
    for(;;) {
	*str=getch();
	if(*str=='\r') {  /* return when RETURN entered */
	    *str='\0';    /* NULL terminate */
	    return;
	}

	if(*str=='\b') {  /* backspace */
	    if(str>p) {
		str--;
		putch('\b');
		putch(' ');
		putch('\b');
	    }
	}
	else {
	    putch(*str);
	    str++;
	}
    }
}

/* Read and save cursor coordinates. */
void cursor_pos(void)
{
    union REGS i,o;

    i.h.bh=0;
    i.h.ah=3;
    int86(16,&i,&o);
}

/* Send cursor to specified X,Y (0,0 is upper 
   left corner). */
void gotoxy(int x,int y)
{
    union REGS i;

    i.h.dh=y;
    i.h.dl=x;
    i.h.ah=2;
    i.h.bh=0;
    int86(16,&i,&i);
}

/* Clear entire line given its Y coordinate */
void clrline(int y)
{
    register int i;

    gotoxy(0,y);
    for(i=0;i<LINE_LEN;i++) putch(' ');
}

/* Clear to end of specified line. This function
   is for use with the editor only because it clears
   a line up to a carriage return. */
void edit_clr_eol(int x,int y)
{
    char *p;

    p=curloc;
    gotoxy(x,y);
    for(;x<LINE_LEN && *p!='\r' && *p;x++,p++) {
	printf(" ");
    }
}

/* Clear the screen. */
void clrscr(void)
{
    union REGS r;

    r.h.ah=6;
    r.h.al=0;
    r.h.ch=0;
    r.h.cl=0;
    r.h.dh=MAX_LINES;
    r.h.dl=LINE_LEN;
    r.h.bh=7;
    int86(0x10,&r,&r);
}

/* Scroll down the screen. This function scrolls
   all but the bottom line. */
void scrolldn(int x,int y)
{
    union REGS r;
    r.h.ah=7;
    r.h.al=1;
    r.h.ch=y;
    r.h.cl=x;
    r.h.dh=MAX_LINES-1;
    r.h.dl=LINE_LEN;
    r.h.bh=7;
    int86(0x10,&r,&r);
}

/* Scroll up the screen using the specified
   coordinates. */
void scrollup(int topx,int topy,int endx,int endy)
{
    union REGS r;

    r.h.ah=6;
    r.h.al=1;
    r.h.ch=topy;
    r.h.cl=topx;
    r.h.dh=endy;
    r.h.dl=endx;
    r.h.bh=7;
    int86(0x10,&r,&r);
}

⌨️ 快捷键说明

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