Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BUFFER_SIZE (32 * 1024)
- #define COLOR 1 /* 0 for gray highlighting */
- #if 0
- ----- crude text editor (doesn't read or write to disk yet) -----
- * inserts or deletes one character at a time into fixed-width buffer
- * internally a simple buffer, but user interface is line oriented...
- * displays text intermittantly, not a problem but limited screen space is
- * many improvements required to be usable... good luck!
- #endif
- #include <stdio.h>
- #include <stdlib.h> /* exit() */
- #include <ctype.h> /* toupper(), tolower() */
- char a[BUFFER_SIZE], b[BUFFER_SIZE];
- char *buff, *scratch;
- int pos, len;
- int cmd_mode;
- #define DIM "\033[2m"
- #define NORM "\033[0m"
- #if COLOR
- #define HIGH "\033[1;31m" // bold red, that should do
- // #define HIGH "\033[35m" // try magenta...
- // #define HIGH "\033[30;41m" // black on red, stands out better
- // #define HIGH "\033[31m" // red
- #define CTRL "\033[34m" // blue
- #define LINE "\033[33m" // yellow
- #else
- #define HIGH DIM
- #define CTRL DIM
- #define LINE DIM
- #endif
- #define RUB (-1)
- /* needs up/down line, goto line, string search-find. some of those need input entry */
- /* needs to read and save to file! input entry for filename might be needed */
- enum {
- cQuit=(int)'Q',
- cBack=(int)'B',
- cForward=(int)'F',
- cLit=(int)'V',
- cNewline=(int)'L',
- cRub=(int)'X',
- CMD=(int)'.'
- };
- void fBack();
- void fFoward();
- void fLit();
- void fEsc();
- void insert(int c); /* default action */
- void fBack() { if(pos > 0) pos--; return; }
- void fForward() { if(pos < len) pos++; return; }
- void fLit(int *c) { *c = getc(stdin); if(*c != EOF) insert(*c); return; }
- /* we swap buffers, with one char inserted (or one char skipped) */
- /* it might be better to shift text in-place within one buffer, without swapping */
- /* it would be good to collect input text in a small buffer and insert all at once */
- /* or port to a language with builtin string functions and use those to insert */
- void insert(int c) {
- char *tmp;
- int i, x, y;
- if(c == RUB && (len-pos) == 0) return;
- if(c != RUB && len == BUFFER_SIZE) return;
- x = 0;
- for(i = 0; i < pos; i++) scratch[x++] = buff[i];
- if(c != RUB) { scratch[x++] = c; y = pos; }
- else { y = pos + 1; }
- for(i = y; i < len; i++) scratch[x++] = buff[i];
- len = x;
- if(c != RUB) pos++;
- tmp = buff; buff = scratch; scratch = tmp;
- return;
- }
- void p_item(int c, char *txt) {
- int i;
- for(i = 0; txt[i] != '\0'; i++) {
- if(txt[i] == '~') printf("%s%c%s", CTRL, c, NORM);
- else printf("%c", txt[i]);
- }
- return;
- }
- /* dumps whole buffer, better to display lines new cursor when or else overrun screen */
- /* when reasonable no. of lines fixed, maybe show extra info above + below text view */
- void display() {
- int i, c, prev, lineno;
- char *cchars = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
- c = '\n';
- lineno = 0;
- for(i = 0; i < len; i++) {
- prev = c; c = buff[i];
- if(prev == '\n') {
- lineno++;
- printf("%s%4d%s ", LINE, lineno, NORM);
- }
- if(i == pos) printf("%s^%s", HIGH, NORM);
- if(c == '\n') printf("%s%c%s\n", CTRL, '$', NORM);
- else if(c < 32) printf("%s^%c%s", CTRL, cchars[c], NORM);
- else if(c >= 0x7f) printf("%s\\x%02x%s", CTRL, c, NORM);
- else printf("%c", c);
- }
- if(pos == len) {
- if(c == '\n') printf("%s%4d %s^%s", CTRL, lineno+1, HIGH, NORM);
- else printf("%s%c%s", HIGH, '^', NORM);
- }
- printf("\n");
- /* context sensitive help should be own function... */
- if(cmd_mode) {
- printf("[%d/%d] (cmd) ", pos, len);
- p_item(cQuit, "~uit ");
- p_item(cBack, "~ack "); p_item(cForward, "~orward ");
- p_item(cLit, "~=literal "); p_item(cNewline, "new~ine ");
- p_item(cRub, "~=delete ");
- p_item(CMD, "| '~' return to edit\n");
- }
- else {
- printf("[%d/%d] (edit) ", pos, len);
- p_item(CMD, "press '~' to enter cmd mode, ");
- printf("'%s%c%c%s' for a '%c', ", CTRL, CMD, CMD, NORM, CMD);
- printf("'%s%c%c%s' for newline\n", CTRL, CMD, cNewline, NORM);
- }
- return;
- }
- int main() {
- int c, n;
- buff = a; scratch = b;
- pos = 0; len = 0;
- cmd_mode = 0;
- display();
- c = getc(stdin);
- n = 0;
- while(c != EOF) {
- if(cmd_mode == 0) {
- if(c == CMD) { cmd_mode = 1; n = 0; }
- else if(c == '\n') display();
- else { insert(c); }
- }
- else {
- c = toupper(c);
- n++;
- switch(c) {
- case cQuit: exit(0); break;
- case cBack: fBack(); break;
- case cForward: fForward(); break;
- case cLit: fLit(&c); if(c == '\n') display(); break;
- case cNewline: insert('\n'); if(n == 1) cmd_mode = 0; break;
- case cRub: insert(RUB); break;
- case CMD: if(n == 1) insert(CMD); cmd_mode = 0; break;
- case '\n': display(); break;
- default: /* ignore */ ;
- };
- }
- c = getc(stdin);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement