[BACK]Return to de.c CVS log [TXT][DIR] Up to [local] / OpenXM_contrib2 / asir2000 / gc5.3 / cord

Annotation of OpenXM_contrib2/asir2000/gc5.3/cord/de.c, Revision 1.1.1.1

1.1       noro        1: /*
                      2:  * Copyright (c) 1993-1994 by Xerox Corporation.  All rights reserved.
                      3:  *
                      4:  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
                      5:  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
                      6:  *
                      7:  * Permission is hereby granted to use or copy this program
                      8:  * for any purpose,  provided the above notices are retained on all copies.
                      9:  * Permission to modify the code and to distribute modified code is granted,
                     10:  * provided the above notices are retained, and a notice that the code was
                     11:  * modified is included with the above copyright notice.
                     12:  *
                     13:  * Author: Hans-J. Boehm (boehm@parc.xerox.com)
                     14:  */
                     15: /*
                     16:  * A really simple-minded text editor based on cords.
                     17:  * Things it does right:
                     18:  *     No size bounds.
                     19:  *     Inbounded undo.
                     20:  *     Shouldn't crash no matter what file you invoke it on (e.g. /vmunix)
                     21:  *             (Make sure /vmunix is not writable before you try this.)
                     22:  *     Scrolls horizontally.
                     23:  * Things it does wrong:
                     24:  *     It doesn't handle tabs reasonably (use "expand" first).
                     25:  *     The command set is MUCH too small.
                     26:  *     The redisplay algorithm doesn't let curses do the scrolling.
                     27:  *     The rule for moving the window over the file is suboptimal.
                     28:  */
                     29: /* Boehm, February 6, 1995 12:27 pm PST */
                     30:
                     31: /* Boehm, May 19, 1994 2:20 pm PDT */
                     32: #include <stdio.h>
                     33: #include "gc.h"
                     34: #include "cord.h"
                     35:
                     36: #ifdef THINK_C
                     37: #define MACINTOSH
                     38: #include <ctype.h>
                     39: #endif
                     40:
                     41: #if defined(__BORLANDC__) && !defined(WIN32)
                     42:     /* If this is DOS or win16, we'll fail anyway.     */
                     43:     /* Might as well assume win32.                     */
                     44: #   define WIN32
                     45: #endif
                     46:
                     47: #if defined(WIN32)
                     48: #  include <windows.h>
                     49: #  include "de_win.h"
                     50: #elif defined(MACINTOSH)
                     51: #      include <console.h>
                     52: /* curses emulation. */
                     53: #      define initscr()
                     54: #      define endwin()
                     55: #      define nonl()
                     56: #      define noecho() csetmode(C_NOECHO, stdout)
                     57: #      define cbreak() csetmode(C_CBREAK, stdout)
                     58: #      define refresh()
                     59: #      define addch(c) putchar(c)
                     60: #      define standout() cinverse(1, stdout)
                     61: #      define standend() cinverse(0, stdout)
                     62: #      define move(line,col) cgotoxy(col + 1, line + 1, stdout)
                     63: #      define clrtoeol() ccleol(stdout)
                     64: #      define de_error(s) { fprintf(stderr, s); getchar(); }
                     65: #      define LINES 25
                     66: #      define COLS 80
                     67: #else
                     68: #  include <curses.h>
                     69: #  define de_error(s) { fprintf(stderr, s); sleep(2); }
                     70: #endif
                     71: #include "de_cmds.h"
                     72:
                     73: /* List of line number to position mappings, in descending order. */
                     74: /* There may be holes.                                           */
                     75: typedef struct LineMapRep {
                     76:     int line;
                     77:     size_t pos;
                     78:     struct LineMapRep * previous;
                     79: } * line_map;
                     80:
                     81: /* List of file versions, one per edit operation */
                     82: typedef struct HistoryRep {
                     83:     CORD file_contents;
                     84:     struct HistoryRep * previous;
                     85:     line_map map;      /* Invalid for first record "now" */
                     86: } * history;
                     87:
                     88: history now = 0;
                     89: CORD current;          /* == now -> file_contents.     */
                     90: size_t current_len;    /* Current file length.         */
                     91: line_map current_map = 0;      /* Current line no. to pos. map  */
                     92: size_t current_map_size = 0;   /* Number of current_map entries.       */
                     93:                                /* Not always accurate, but reset       */
                     94:                                /* by prune_map.                        */
                     95: # define MAX_MAP_SIZE 3000
                     96:
                     97: /* Current display position */
                     98: int dis_line = 0;
                     99: int dis_col = 0;
                    100:
                    101: # define ALL -1
                    102: # define NONE - 2
                    103: int need_redisplay = 0;        /* Line that needs to be redisplayed.   */
                    104:
                    105:
                    106: /* Current cursor position. Always within file. */
                    107: int line = 0;
                    108: int col = 0;
                    109: size_t file_pos = 0;   /* Character position corresponding to cursor.  */
                    110:
                    111: /* Invalidate line map for lines > i */
                    112: void invalidate_map(int i)
                    113: {
                    114:     while(current_map -> line > i) {
                    115:         current_map = current_map -> previous;
                    116:         current_map_size--;
                    117:     }
                    118: }
                    119:
                    120: /* Reduce the number of map entries to save space for huge files. */
                    121: /* This also affects maps in histories.                                  */
                    122: void prune_map()
                    123: {
                    124:     line_map map = current_map;
                    125:     int start_line = map -> line;
                    126:
                    127:     current_map_size = 0;
                    128:     for(; map != 0; map = map -> previous) {
                    129:        current_map_size++;
                    130:        if (map -> line < start_line - LINES && map -> previous != 0) {
                    131:            map -> previous = map -> previous -> previous;
                    132:        }
                    133:     }
                    134: }
                    135: /* Add mapping entry */
                    136: void add_map(int line, size_t pos)
                    137: {
                    138:     line_map new_map = GC_NEW(struct LineMapRep);
                    139:
                    140:     if (current_map_size >= MAX_MAP_SIZE) prune_map();
                    141:     new_map -> line = line;
                    142:     new_map -> pos = pos;
                    143:     new_map -> previous = current_map;
                    144:     current_map = new_map;
                    145:     current_map_size++;
                    146: }
                    147:
                    148:
                    149:
                    150: /* Return position of column *c of ith line in   */
                    151: /* current file. Adjust *c to be within the line.*/
                    152: /* A 0 pointer is taken as 0 column.            */
                    153: /* Returns CORD_NOT_FOUND if i is too big.      */
                    154: /* Assumes i > dis_line.                        */
                    155: size_t line_pos(int i, int *c)
                    156: {
                    157:     int j;
                    158:     size_t cur;
                    159:     size_t next;
                    160:     line_map map = current_map;
                    161:
                    162:     while (map -> line > i) map = map -> previous;
                    163:     if (map -> line < i - 2) /* rebuild */ invalidate_map(i);
                    164:     for (j = map -> line, cur = map -> pos; j < i;) {
                    165:        cur = CORD_chr(current, cur, '\n');
                    166:         if (cur == current_len-1) return(CORD_NOT_FOUND);
                    167:         cur++;
                    168:         if (++j > current_map -> line) add_map(j, cur);
                    169:     }
                    170:     if (c != 0) {
                    171:         next = CORD_chr(current, cur, '\n');
                    172:         if (next == CORD_NOT_FOUND) next = current_len - 1;
                    173:         if (next < cur + *c) {
                    174:             *c = next - cur;
                    175:         }
                    176:         cur += *c;
                    177:     }
                    178:     return(cur);
                    179: }
                    180:
                    181: void add_hist(CORD s)
                    182: {
                    183:     history new_file = GC_NEW(struct HistoryRep);
                    184:
                    185:     new_file -> file_contents = current = s;
                    186:     current_len = CORD_len(s);
                    187:     new_file -> previous = now;
                    188:     if (now != 0) now -> map = current_map;
                    189:     now = new_file;
                    190: }
                    191:
                    192: void del_hist(void)
                    193: {
                    194:     now = now -> previous;
                    195:     current = now -> file_contents;
                    196:     current_map = now -> map;
                    197:     current_len = CORD_len(current);
                    198: }
                    199:
                    200: /* Current screen_contents; a dynamically allocated array of CORDs     */
                    201: CORD * screen = 0;
                    202: int screen_size = 0;
                    203:
                    204: # ifndef WIN32
                    205: /* Replace a line in the curses stdscr.        All control characters are      */
                    206: /* displayed as upper case characters in standout mode.  This isn't    */
                    207: /* terribly appropriate for tabs.                                                                      */
                    208: void replace_line(int i, CORD s)
                    209: {
                    210:     register int c;
                    211:     CORD_pos p;
                    212:     size_t len = CORD_len(s);
                    213:
                    214:     if (screen == 0 || LINES > screen_size) {
                    215:         screen_size = LINES;
                    216:        screen = (CORD *)GC_MALLOC(screen_size * sizeof(CORD));
                    217:     }
                    218: #   if !defined(MACINTOSH)
                    219:         /* A gross workaround for an apparent curses bug: */
                    220:         if (i == LINES-1 && len == COLS) {
                    221:             s = CORD_substr(s, 0, CORD_len(s) - 1);
                    222:         }
                    223: #   endif
                    224:     if (CORD_cmp(screen[i], s) != 0) {
                    225:         move(i, 0); clrtoeol(); move(i,0);
                    226:
                    227:         CORD_FOR (p, s) {
                    228:             c = CORD_pos_fetch(p) & 0x7f;
                    229:             if (iscntrl(c)) {
                    230:                standout(); addch(c + 0x40); standend();
                    231:             } else {
                    232:                addch(c);
                    233:            }
                    234:        }
                    235:        screen[i] = s;
                    236:     }
                    237: }
                    238: #else
                    239: # define replace_line(i,s) invalidate_line(i)
                    240: #endif
                    241:
                    242: /* Return up to COLS characters of the line of s starting at pos,      */
                    243: /* returning only characters after the given column.                   */
                    244: CORD retrieve_line(CORD s, size_t pos, unsigned column)
                    245: {
                    246:     CORD candidate = CORD_substr(s, pos, column + COLS);
                    247:                        /* avoids scanning very long lines      */
                    248:     int eol = CORD_chr(candidate, 0, '\n');
                    249:     int len;
                    250:
                    251:     if (eol == CORD_NOT_FOUND) eol = CORD_len(candidate);
                    252:     len = (int)eol - (int)column;
                    253:     if (len < 0) len = 0;
                    254:     return(CORD_substr(s, pos + column, len));
                    255: }
                    256:
                    257: # ifdef WIN32
                    258: #   define refresh();
                    259:
                    260:     CORD retrieve_screen_line(int i)
                    261:     {
                    262:        register size_t pos;
                    263:
                    264:        invalidate_map(dis_line + LINES);       /* Prune search */
                    265:        pos = line_pos(dis_line + i, 0);
                    266:        if (pos == CORD_NOT_FOUND) return(CORD_EMPTY);
                    267:        return(retrieve_line(current, pos, dis_col));
                    268:     }
                    269: # endif
                    270:
                    271: /* Display the visible section of the current file      */
                    272: void redisplay(void)
                    273: {
                    274:     register int i;
                    275:
                    276:     invalidate_map(dis_line + LINES);  /* Prune search */
                    277:     for (i = 0; i < LINES; i++) {
                    278:         if (need_redisplay == ALL || need_redisplay == i) {
                    279:             register size_t pos = line_pos(dis_line + i, 0);
                    280:
                    281:             if (pos == CORD_NOT_FOUND) break;
                    282:             replace_line(i, retrieve_line(current, pos, dis_col));
                    283:             if (need_redisplay == i) goto done;
                    284:         }
                    285:     }
                    286:     for (; i < LINES; i++) replace_line(i, CORD_EMPTY);
                    287: done:
                    288:     refresh();
                    289:     need_redisplay = NONE;
                    290: }
                    291:
                    292: int dis_granularity;
                    293:
                    294: /* Update dis_line, dis_col, and dis_pos to make cursor visible.       */
                    295: /* Assumes line, col, dis_line, dis_pos are in bounds.                 */
                    296: void normalize_display()
                    297: {
                    298:     int old_line = dis_line;
                    299:     int old_col = dis_col;
                    300:
                    301:     dis_granularity = 1;
                    302:     if (LINES > 15 && COLS > 15) dis_granularity = 2;
                    303:     while (dis_line > line) dis_line -= dis_granularity;
                    304:     while (dis_col > col) dis_col -= dis_granularity;
                    305:     while (line >= dis_line + LINES) dis_line += dis_granularity;
                    306:     while (col >= dis_col + COLS) dis_col += dis_granularity;
                    307:     if (old_line != dis_line || old_col != dis_col) {
                    308:         need_redisplay = ALL;
                    309:     }
                    310: }
                    311:
                    312: # if defined(WIN32)
                    313: # elif defined(MACINTOSH)
                    314: #              define move_cursor(x,y) cgotoxy(x + 1, y + 1, stdout)
                    315: # else
                    316: #              define move_cursor(x,y) move(y,x)
                    317: # endif
                    318:
                    319: /* Adjust display so that cursor is visible; move cursor into position */
                    320: /* Update screen if necessary.                                         */
                    321: void fix_cursor(void)
                    322: {
                    323:     normalize_display();
                    324:     if (need_redisplay != NONE) redisplay();
                    325:     move_cursor(col - dis_col, line - dis_line);
                    326:     refresh();
                    327: #   ifndef WIN32
                    328:       fflush(stdout);
                    329: #   endif
                    330: }
                    331:
                    332: /* Make sure line, col, and dis_pos are somewhere inside file. */
                    333: /* Recompute file_pos. Assumes dis_pos is accurate or past eof */
                    334: void fix_pos()
                    335: {
                    336:     int my_col = col;
                    337:
                    338:     if ((size_t)line > current_len) line = current_len;
                    339:     file_pos = line_pos(line, &my_col);
                    340:     if (file_pos == CORD_NOT_FOUND) {
                    341:         for (line = current_map -> line, file_pos = current_map -> pos;
                    342:              file_pos < current_len;
                    343:              line++, file_pos = CORD_chr(current, file_pos, '\n') + 1);
                    344:        line--;
                    345:         file_pos = line_pos(line, &col);
                    346:     } else {
                    347:        col = my_col;
                    348:     }
                    349: }
                    350:
                    351: #if defined(WIN32)
                    352: #  define beep() Beep(1000 /* Hz */, 300 /* msecs */)
                    353: #elif defined(MACINTOSH)
                    354: #      define beep() SysBeep(1)
                    355: #else
                    356: /*
                    357:  * beep() is part of some curses packages and not others.
                    358:  * We try to match the type of the builtin one, if any.
                    359:  */
                    360: #ifdef __STDC__
                    361:     int beep(void)
                    362: #else
                    363:     int beep()
                    364: #endif
                    365: {
                    366:     putc('\007', stderr);
                    367:     return(0);
                    368: }
                    369: #endif
                    370:
                    371: #   define NO_PREFIX -1
                    372: #   define BARE_PREFIX -2
                    373: int repeat_count = NO_PREFIX;  /* Current command prefix. */
                    374:
                    375: int locate_mode = 0;                   /* Currently between 2 ^Ls      */
                    376: CORD locate_string = CORD_EMPTY;       /* Current search string.       */
                    377:
                    378: char * arg_file_name;
                    379:
                    380: #ifdef WIN32
                    381: /* Change the current position to whatever is currently displayed at   */
                    382: /* the given SCREEN coordinates.                                       */
                    383: void set_position(int c, int l)
                    384: {
                    385:     line = l + dis_line;
                    386:     col = c + dis_col;
                    387:     fix_pos();
                    388:     move_cursor(col - dis_col, line - dis_line);
                    389: }
                    390: #endif /* WIN32 */
                    391:
                    392: /* Perform the command associated with character c.  C may be an       */
                    393: /* integer > 256 denoting a windows command, one of the above control  */
                    394: /* characters, or another ASCII character to be used as either a       */
                    395: /* character to be inserted, a repeat count, or a search string,       */
                    396: /* depending on the current state.                                     */
                    397: void do_command(int c)
                    398: {
                    399:     int i;
                    400:     int need_fix_pos;
                    401:     FILE * out;
                    402:
                    403:     if ( c == '\r') c = '\n';
                    404:     if (locate_mode) {
                    405:         size_t new_pos;
                    406:
                    407:         if (c == LOCATE) {
                    408:               locate_mode = 0;
                    409:               locate_string = CORD_EMPTY;
                    410:               return;
                    411:         }
                    412:         locate_string = CORD_cat_char(locate_string, (char)c);
                    413:         new_pos = CORD_str(current, file_pos - CORD_len(locate_string) + 1,
                    414:                           locate_string);
                    415:         if (new_pos != CORD_NOT_FOUND) {
                    416:             need_redisplay = ALL;
                    417:             new_pos += CORD_len(locate_string);
                    418:             for (;;) {
                    419:                file_pos = line_pos(line + 1, 0);
                    420:                if (file_pos > new_pos) break;
                    421:                line++;
                    422:             }
                    423:             col = new_pos - line_pos(line, 0);
                    424:             file_pos = new_pos;
                    425:             fix_cursor();
                    426:         } else {
                    427:             locate_string = CORD_substr(locate_string, 0,
                    428:                                        CORD_len(locate_string) - 1);
                    429:             beep();
                    430:         }
                    431:         return;
                    432:     }
                    433:     if (c == REPEAT) {
                    434:        repeat_count = BARE_PREFIX; return;
                    435:     } else if (c < 0x100 && isdigit(c)){
                    436:         if (repeat_count == BARE_PREFIX) {
                    437:           repeat_count = c - '0'; return;
                    438:         } else if (repeat_count != NO_PREFIX) {
                    439:           repeat_count = 10 * repeat_count + c - '0'; return;
                    440:         }
                    441:     }
                    442:     if (repeat_count == NO_PREFIX) repeat_count = 1;
                    443:     if (repeat_count == BARE_PREFIX && (c == UP || c == DOWN)) {
                    444:        repeat_count = LINES - dis_granularity;
                    445:     }
                    446:     if (repeat_count == BARE_PREFIX) repeat_count = 8;
                    447:     need_fix_pos = 0;
                    448:     for (i = 0; i < repeat_count; i++) {
                    449:         switch(c) {
                    450:           case LOCATE:
                    451:             locate_mode = 1;
                    452:             break;
                    453:           case TOP:
                    454:             line = col = file_pos = 0;
                    455:             break;
                    456:          case UP:
                    457:            if (line != 0) {
                    458:                line--;
                    459:                need_fix_pos = 1;
                    460:            }
                    461:            break;
                    462:          case DOWN:
                    463:            line++;
                    464:            need_fix_pos = 1;
                    465:            break;
                    466:          case LEFT:
                    467:            if (col != 0) {
                    468:                col--; file_pos--;
                    469:            }
                    470:            break;
                    471:          case RIGHT:
                    472:            if (CORD_fetch(current, file_pos) == '\n') break;
                    473:            col++; file_pos++;
                    474:            break;
                    475:          case UNDO:
                    476:            del_hist();
                    477:            need_redisplay = ALL; need_fix_pos = 1;
                    478:            break;
                    479:          case BS:
                    480:            if (col == 0) {
                    481:                beep();
                    482:                break;
                    483:            }
                    484:            col--; file_pos--;
                    485:            /* fall through: */
                    486:          case DEL:
                    487:            if (file_pos == current_len-1) break;
                    488:                /* Can't delete trailing newline */
                    489:            if (CORD_fetch(current, file_pos) == '\n') {
                    490:                need_redisplay = ALL; need_fix_pos = 1;
                    491:            } else {
                    492:                need_redisplay = line - dis_line;
                    493:            }
                    494:            add_hist(CORD_cat(
                    495:                        CORD_substr(current, 0, file_pos),
                    496:                        CORD_substr(current, file_pos+1, current_len)));
                    497:            invalidate_map(line);
                    498:            break;
                    499:          case WRITE:
                    500:            {
                    501:                CORD name = CORD_cat(CORD_from_char_star(arg_file_name),
                    502:                                     ".new");
                    503:
                    504:                if ((out = fopen(CORD_to_const_char_star(name), "wb")) == NULL
                    505:                    || CORD_put(current, out) == EOF) {
                    506:                    de_error("Write failed\n");
                    507:                    need_redisplay = ALL;
                    508:                 } else {
                    509:                     fclose(out);
                    510:                 }
                    511:            }
                    512:             break;
                    513:          default:
                    514:            {
                    515:                CORD left_part = CORD_substr(current, 0, file_pos);
                    516:                CORD right_part = CORD_substr(current, file_pos, current_len);
                    517:
                    518:                add_hist(CORD_cat(CORD_cat_char(left_part, (char)c),
                    519:                                  right_part));
                    520:                invalidate_map(line);
                    521:                if (c == '\n') {
                    522:                    col = 0; line++; file_pos++;
                    523:                    need_redisplay = ALL;
                    524:                } else {
                    525:                    col++; file_pos++;
                    526:                    need_redisplay = line - dis_line;
                    527:                }
                    528:                break;
                    529:            }
                    530:         }
                    531:     }
                    532:     if (need_fix_pos) fix_pos();
                    533:     fix_cursor();
                    534:     repeat_count = NO_PREFIX;
                    535: }
                    536:
                    537: /* OS independent initialization */
                    538:
                    539: void generic_init(void)
                    540: {
                    541:     FILE * f;
                    542:     CORD initial;
                    543:
                    544:     if ((f = fopen(arg_file_name, "rb")) == NULL) {
                    545:        initial = "\n";
                    546:     } else {
                    547:         initial = CORD_from_file(f);
                    548:         if (initial == CORD_EMPTY
                    549:             || CORD_fetch(initial, CORD_len(initial)-1) != '\n') {
                    550:             initial = CORD_cat(initial, "\n");
                    551:         }
                    552:     }
                    553:     add_map(0,0);
                    554:     add_hist(initial);
                    555:     now -> map = current_map;
                    556:     now -> previous = now;  /* Can't back up further: beginning of the world */
                    557:     need_redisplay = ALL;
                    558:     fix_cursor();
                    559: }
                    560:
                    561: #ifndef WIN32
                    562:
                    563: main(argc, argv)
                    564: int argc;
                    565: char ** argv;
                    566: {
                    567:     int c;
                    568:
                    569: #if defined(MACINTOSH)
                    570:        console_options.title = "\pDumb Editor";
                    571:        cshow(stdout);
                    572:        GC_init();
                    573:        argc = ccommand(&argv);
                    574: #endif
                    575:
                    576:     if (argc != 2) goto usage;
                    577:     arg_file_name = argv[1];
                    578:     setvbuf(stdout, GC_MALLOC_ATOMIC(8192), _IOFBF, 8192);
                    579:     initscr();
                    580:     noecho(); nonl(); cbreak();
                    581:     generic_init();
                    582:     while ((c = getchar()) != QUIT) {
                    583:                if (c == EOF) break;
                    584:            do_command(c);
                    585:     }
                    586: done:
                    587:     move(LINES-1, 0);
                    588:     clrtoeol();
                    589:     refresh();
                    590:     nl();
                    591:     echo();
                    592:     endwin();
                    593:     exit(0);
                    594: usage:
                    595:     fprintf(stderr, "Usage: %s file\n", argv[0]);
                    596:     fprintf(stderr, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n");
                    597:     fprintf(stderr, "Undo: ^U    Write to <file>.new: ^W");
                    598:     fprintf(stderr, "Quit:^D  Repeat count: ^R[n]\n");
                    599:     fprintf(stderr, "Top: ^T   Locate (search, find): ^L text ^L\n");
                    600:     exit(1);
                    601: }
                    602:
                    603: #endif  /* !WIN32 */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>