/* inserts a character at the cursor */ void insertc(view_t *view,unsigned char c) { /* check readonly */ if(view->buffer->readonly) error_throw(ERR_READONLY); /* check valid character */ if((char)c<0) return; char *line=view->buffer->lines[view->cursor_line]; /* make sure the line is long enough */ view->buffer->lines[view->cursor_line]=strexpand(line,strlen(line)+2); line=view->buffer->lines[view->cursor_line]; /* make a copy of the line */ char *tln=malloc(strlen(line)+2); char *ln=tln; sprintf(ln,"%s",line); /* put the line back together */ /* copy the first part back */ line[0]='\0'; strncat(line,ln,view->cursor_x); /* copy the character */ line[strlen(line)+1]='\0'; line[strlen(line)]=c; /* copy the second part back */ ln+=view->cursor_x; strcat(line,ln); free(tln); /* free temporary buffer */ /* advance the cursor */ view->pref_x++; clean_cursor_x(view); /* we've been modified */ view->buffer->modified=1; }
/* inserts a line break at the cursor */ void insertlb(view_t *view) { buffer_t *b=view->buffer; if(b->readonly) error_throw(ERR_READONLY); /* enlarge the line count */ b->line_count++; /* allocate more space */ b->lines=realloc(b->lines,sizeof(char *)*b->line_count); int nln=view->cursor_line; /* the number of the new line -1 */ /* copy lines down */ int l; for(l=b->line_count-2;l>=nln;l--) b->lines[l+1]=b->lines[l]; /* create the new line */ b->lines[nln]=malloc(view->cursor_x+2); b->lines[nln][0]='\0'; /* copy the first part into the new line*/ strncat(b->lines[nln],b->lines[nln+1],view->cursor_x); /* copy the second part out of the next line */ char *sp=malloc(strsize(b->lines[nln+1]+view->cursor_x)); sp[0]='\0'; strcat(sp,b->lines[nln+1]+view->cursor_x); free(b->lines[nln+1]); /* free old stuff */ b->lines[nln+1]=sp; /* use new stuff */ view->pref_x=0; /* we're at the beginning of the line */ /* move the cursor down */ cursor_down(view); /* we've been modified */ b->modified=1; }
/* goto the given line in the given view */ void goto_line(view_t *view,int lno) { if(lno<=0) error_throw(ERR_BADINPUT); /* set the cursor line and refresh */ view->cursor_line=lno-1; /* move the screen */ while(view->topline>lno-1) view->topline--; while(view->cursor_line-view->topline>=view->height) view->topline++; /* clean the cursor X coordinate */ clean_cursor_x(view); }
int error_print(char *msg) { PUTERROR(msg); return (error_throw(FAIL)); }