Advertisement
Guest User

edit.c

a guest
May 3rd, 2019
194
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.28 KB | None | 0 0
  1.  
  2.  
  3. #define BUFFER_SIZE (32 * 1024)
  4. #define COLOR 1 /* 0 for gray highlighting */
  5.  
  6. #if 0
  7.    ----- crude text editor (doesn't read or write to disk yet) -----
  8.  
  9.   * inserts or deletes one character at a time into fixed-width buffer
  10.   * internally a simple buffer, but user interface is line oriented...
  11.   * displays text intermittantly, not a problem but limited screen space is
  12.   * many improvements required to be usable... good luck!
  13.  
  14. #endif
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h> /* exit() */
  18. #include <ctype.h>  /* toupper(), tolower() */
  19.  
  20. char a[BUFFER_SIZE], b[BUFFER_SIZE];
  21. char *buff, *scratch;
  22. int pos, len;
  23. int cmd_mode;
  24.  
  25. #define DIM  "\033[2m"
  26. #define NORM "\033[0m"
  27.  
  28. #if COLOR
  29.  #define HIGH "\033[1;31m" // bold red, that should do
  30.  // #define HIGH "\033[35m" // try magenta...
  31.  // #define HIGH "\033[30;41m" // black on red, stands out better
  32.  // #define HIGH "\033[31m" // red
  33.  #define CTRL "\033[34m" // blue
  34.  #define LINE "\033[33m" // yellow
  35. #else
  36.  #define HIGH DIM
  37.  #define CTRL DIM
  38.  #define LINE DIM
  39. #endif
  40.  
  41. #define RUB (-1)
  42.  
  43. /* needs up/down line, goto line, string search-find. some of those need input entry */
  44. /* needs to read and save to file! input entry for filename might be needed          */
  45.  
  46. enum {
  47.    cQuit=(int)'Q',
  48.    cBack=(int)'B',
  49.    cForward=(int)'F',
  50.    cLit=(int)'V',
  51.    cNewline=(int)'L',
  52.    cRub=(int)'X',
  53.    CMD=(int)'.'
  54. };
  55.  
  56. void fBack();
  57. void fFoward();
  58. void fLit();
  59. void fEsc();
  60. void insert(int c); /* default action */
  61.  
  62. void fBack()   { if(pos > 0)   pos--; return; }
  63. void fForward() { if(pos < len) pos++; return; }
  64. void fLit(int *c) { *c = getc(stdin); if(*c != EOF) insert(*c); return; }
  65.  
  66. /* we swap buffers, with one char inserted (or one char skipped)                   */
  67. /* it might be better to shift text in-place within one buffer, without swapping   */
  68. /* it would be good to collect input text in a small buffer and insert all at once */
  69. /* or port to a language with builtin string functions and use those to insert     */
  70.  
  71. void insert(int c) {
  72.    char *tmp;
  73.    int i, x, y;
  74.  
  75.    if(c == RUB && (len-pos) == 0) return;
  76.    if(c != RUB && len == BUFFER_SIZE) return;
  77.  
  78.    x = 0;
  79.    for(i = 0; i < pos; i++) scratch[x++] = buff[i];
  80.  
  81.    if(c != RUB) { scratch[x++] = c; y = pos; }
  82.    else { y = pos + 1; }
  83.  
  84.    for(i = y; i < len; i++) scratch[x++] = buff[i];
  85.  
  86.    len = x;
  87.    if(c != RUB) pos++;
  88.  
  89.    tmp = buff; buff = scratch; scratch = tmp;
  90.    return;
  91. }
  92.  
  93. void p_item(int c, char *txt) {
  94.    int i;
  95.    for(i = 0; txt[i] != '\0'; i++) {
  96.        if(txt[i] == '~') printf("%s%c%s", CTRL, c, NORM);
  97.        else printf("%c", txt[i]);
  98.    }
  99.    return;
  100. }
  101.  
  102. /* dumps whole buffer, better to display lines new cursor when or else overrun screen */
  103. /* when reasonable no. of lines fixed, maybe show extra info above + below text view  */
  104.  
  105. void display() {
  106.    int i, c, prev, lineno;
  107.    char *cchars = "@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
  108.  
  109.    c = '\n';
  110.    lineno = 0;
  111.  
  112.    for(i = 0; i < len; i++) {
  113.  
  114.        prev = c; c = buff[i];
  115.  
  116.        if(prev == '\n') {
  117.            lineno++;
  118.            printf("%s%4d%s ", LINE, lineno, NORM);
  119.        }
  120.  
  121.        if(i == pos) printf("%s^%s", HIGH, NORM);
  122.  
  123.        if(c == '\n') printf("%s%c%s\n", CTRL, '$', NORM);
  124.        else if(c < 32) printf("%s^%c%s", CTRL, cchars[c], NORM);
  125.        else if(c >= 0x7f) printf("%s\\x%02x%s", CTRL, c, NORM);
  126.        else printf("%c", c);
  127.    }
  128.  
  129.    if(pos == len) {
  130.        if(c == '\n') printf("%s%4d %s^%s", CTRL, lineno+1, HIGH, NORM);
  131.        else printf("%s%c%s", HIGH, '^', NORM);
  132.    }
  133.  
  134.    printf("\n");
  135.  
  136.    /* context sensitive help should be own function... */
  137.  
  138.    if(cmd_mode) {
  139.  
  140.        printf("[%d/%d] (cmd) ", pos, len);
  141.  
  142.        p_item(cQuit, "~uit ");
  143.        p_item(cBack, "~ack "); p_item(cForward, "~orward ");
  144.        p_item(cLit, "~=literal "); p_item(cNewline, "new~ine ");
  145.        p_item(cRub, "~=delete ");
  146.  
  147.        p_item(CMD, "| '~' return to edit\n");
  148.  
  149.    }
  150.    else {
  151.        printf("[%d/%d] (edit) ", pos, len);
  152.        p_item(CMD, "press '~' to enter cmd mode, ");
  153.        printf("'%s%c%c%s' for a '%c', ", CTRL, CMD, CMD, NORM, CMD);
  154.        printf("'%s%c%c%s' for newline\n", CTRL, CMD, cNewline, NORM);
  155.    }
  156.    return;
  157. }
  158.  
  159. int main() {
  160.    int c, n;
  161.  
  162.    buff = a; scratch = b;
  163.    pos = 0; len = 0;
  164.    cmd_mode = 0;
  165.  
  166.    display();
  167.    c = getc(stdin);
  168.    n = 0;
  169.  
  170.    while(c != EOF) {
  171.  
  172.        if(cmd_mode == 0) {
  173.            if(c == CMD) { cmd_mode = 1; n = 0; }
  174.            else if(c == '\n') display();
  175.            else { insert(c); }
  176.        }
  177.  
  178.        else {
  179.            c = toupper(c);
  180.            n++;
  181.            switch(c) {
  182.                case cQuit:    exit(0); break;
  183.                case cBack:    fBack(); break;
  184.                case cForward: fForward(); break;
  185.                case cLit:     fLit(&c); if(c == '\n') display(); break;
  186.                case cNewline: insert('\n'); if(n == 1) cmd_mode = 0; break;
  187.                case cRub:     insert(RUB); break;
  188.                case CMD:      if(n == 1) insert(CMD); cmd_mode = 0; break;
  189.  
  190.                case '\n': display(); break;
  191.                default: /* ignore */ ;
  192.            };
  193.        }
  194.  
  195.        c = getc(stdin);
  196.    }
  197.    return 0;
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement