Back to index

plt-scheme  4.2.1
de.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1993-1994 by Xerox Corporation.  All rights reserved.
00003  *
00004  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00005  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00006  *
00007  * Permission is hereby granted to use or copy this program
00008  * for any purpose,  provided the above notices are retained on all copies.
00009  * Permission to modify the code and to distribute modified code is granted,
00010  * provided the above notices are retained, and a notice that the code was
00011  * modified is included with the above copyright notice.
00012  *
00013  * Author: Hans-J. Boehm (boehm@parc.xerox.com)
00014  */
00015 /*
00016  * A really simple-minded text editor based on cords.
00017  * Things it does right:
00018  *     No size bounds.
00019  *     Inbounded undo.
00020  *     Shouldn't crash no matter what file you invoke it on (e.g. /vmunix)
00021  *            (Make sure /vmunix is not writable before you try this.)
00022  *     Scrolls horizontally.
00023  * Things it does wrong:
00024  *     It doesn't handle tabs reasonably (use "expand" first).
00025  *     The command set is MUCH too small.
00026  *     The redisplay algorithm doesn't let curses do the scrolling.
00027  *     The rule for moving the window over the file is suboptimal.
00028  */
00029 /* Boehm, February 6, 1995 12:27 pm PST */
00030 
00031 /* Boehm, May 19, 1994 2:20 pm PDT */
00032 #include <stdio.h>
00033 #include "gc.h"
00034 #include "cord.h"
00035 
00036 #ifdef THINK_C
00037 #define MACINTOSH
00038 #include <ctype.h>
00039 #endif
00040 
00041 #if defined(__BORLANDC__) && !defined(WIN32)
00042     /* If this is DOS or win16, we'll fail anyway.      */
00043     /* Might as well assume win32.               */
00044 #   define WIN32
00045 #endif
00046 
00047 #if defined(WIN32)
00048 #  include <windows.h>
00049 #  include "de_win.h"
00050 #elif defined(MACINTOSH)
00051 #      include <console.h>
00052 /* curses emulation. */
00053 #      define initscr()
00054 #      define endwin()
00055 #      define nonl()
00056 #      define noecho() csetmode(C_NOECHO, stdout)
00057 #      define cbreak() csetmode(C_CBREAK, stdout)
00058 #      define refresh()
00059 #      define addch(c) putchar(c)
00060 #      define standout() cinverse(1, stdout)
00061 #      define standend() cinverse(0, stdout)
00062 #      define move(line,col) cgotoxy(col + 1, line + 1, stdout)
00063 #      define clrtoeol() ccleol(stdout)
00064 #      define de_error(s) { fprintf(stderr, s); getchar(); }
00065 #      define LINES 25
00066 #      define COLS 80
00067 #else
00068 #  include <curses.h>
00069 #  define de_error(s) { fprintf(stderr, s); sleep(2); }
00070 #endif
00071 #include "de_cmds.h"
00072 
00073 /* List of line number to position mappings, in descending order. */
00074 /* There may be holes.                                           */
00075 typedef struct LineMapRep {
00076     int line;
00077     size_t pos;
00078     struct LineMapRep * previous;
00079 } * line_map;
00080 
00081 /* List of file versions, one per edit operation */
00082 typedef struct HistoryRep {
00083     CORD file_contents;
00084     struct HistoryRep * previous;
00085     line_map map;    /* Invalid for first record "now" */
00086 } * history;
00087 
00088 history now = 0;
00089 CORD current;        /* == now -> file_contents. */
00090 size_t current_len;  /* Current file length.            */
00091 line_map current_map = 0;   /* Current line no. to pos. map     */
00092 size_t current_map_size = 0;       /* Number of current_map entries.  */
00093                             /* Not always accurate, but reset  */
00094                             /* by prune_map.                   */
00095 # define MAX_MAP_SIZE 3000
00096 
00097 /* Current display position */
00098 int dis_line = 0;
00099 int dis_col = 0;
00100 
00101 # define ALL -1
00102 # define NONE - 2
00103 int need_redisplay = 0;     /* Line that needs to be redisplayed.     */
00104 
00105 
00106 /* Current cursor position. Always within file. */
00107 int line = 0; 
00108 int col = 0;
00109 size_t file_pos = 0; /* Character position corresponding to cursor.   */
00110 
00111 /* Invalidate line map for lines > i */
00112 void invalidate_map(int i)
00113 {
00114     while(current_map -> line > i) {
00115         current_map = current_map -> previous;
00116         current_map_size--;
00117     }
00118 }
00119 
00120 /* Reduce the number of map entries to save space for huge files. */
00121 /* This also affects maps in histories.                          */
00122 void prune_map()
00123 {
00124     line_map map = current_map;
00125     int start_line = map -> line;
00126     
00127     current_map_size = 0;
00128     for(; map != 0; map = map -> previous) {
00129        current_map_size++;
00130        if (map -> line < start_line - LINES && map -> previous != 0) {
00131            map -> previous = map -> previous -> previous;
00132        }
00133     }
00134 }
00135 /* Add mapping entry */
00136 void add_map(int line, size_t pos)
00137 {
00138     line_map new_map = GC_NEW(struct LineMapRep);
00139     
00140     if (current_map_size >= MAX_MAP_SIZE) prune_map();
00141     new_map -> line = line;
00142     new_map -> pos = pos;
00143     new_map -> previous = current_map;
00144     current_map = new_map;
00145     current_map_size++;
00146 }
00147 
00148 
00149 
00150 /* Return position of column *c of ith line in   */
00151 /* current file. Adjust *c to be within the line.*/
00152 /* A 0 pointer is taken as 0 column.              */
00153 /* Returns CORD_NOT_FOUND if i is too big.        */
00154 /* Assumes i > dis_line.                   */
00155 size_t line_pos(int i, int *c)
00156 {
00157     int j;
00158     size_t cur;
00159     size_t next;
00160     line_map map = current_map;
00161     
00162     while (map -> line > i) map = map -> previous;
00163     if (map -> line < i - 2) /* rebuild */ invalidate_map(i);
00164     for (j = map -> line, cur = map -> pos; j < i;) {
00165        cur = CORD_chr(current, cur, '\n');
00166         if (cur == current_len-1) return(CORD_NOT_FOUND);
00167         cur++;
00168         if (++j > current_map -> line) add_map(j, cur);
00169     }
00170     if (c != 0) {
00171         next = CORD_chr(current, cur, '\n');
00172         if (next == CORD_NOT_FOUND) next = current_len - 1;
00173         if (next < cur + *c) {
00174             *c = next - cur;
00175         }
00176         cur += *c;
00177     }
00178     return(cur);
00179 }
00180 
00181 void add_hist(CORD s)
00182 {
00183     history new_file = GC_NEW(struct HistoryRep);
00184     
00185     new_file -> file_contents = current = s;
00186     current_len = CORD_len(s);
00187     new_file -> previous = now;
00188     if (now != 0) now -> map = current_map;
00189     now = new_file;
00190 }
00191 
00192 void del_hist(void)
00193 {
00194     now = now -> previous;
00195     current = now -> file_contents;
00196     current_map = now -> map;
00197     current_len = CORD_len(current);
00198 }
00199 
00200 /* Current screen_contents; a dynamically allocated array of CORDs    */
00201 CORD * screen = 0;
00202 int screen_size = 0;
00203 
00204 # ifndef WIN32
00205 /* Replace a line in the curses stdscr.   All control characters are  */
00206 /* displayed as upper case characters in standout mode.  This isn't   */
00207 /* terribly appropriate for tabs.                                                          */
00208 void replace_line(int i, CORD s)
00209 {
00210     register int c;
00211     CORD_pos p;
00212     size_t len = CORD_len(s);
00213     
00214     if (screen == 0 || LINES > screen_size) {
00215         screen_size = LINES;
00216        screen = (CORD *)GC_MALLOC(screen_size * sizeof(CORD));
00217     }
00218 #   if !defined(MACINTOSH)
00219         /* A gross workaround for an apparent curses bug: */
00220         if (i == LINES-1 && len == COLS) {
00221             s = CORD_substr(s, 0, CORD_len(s) - 1);
00222         }
00223 #   endif
00224     if (CORD_cmp(screen[i], s) != 0) {
00225         move(i, 0); clrtoeol(); move(i,0);
00226 
00227         CORD_FOR (p, s) {
00228             c = CORD_pos_fetch(p) & 0x7f;
00229             if (iscntrl(c)) {
00230               standout(); addch(c + 0x40); standend();
00231             } else {
00232                addch(c);
00233            }
00234        }
00235        screen[i] = s;
00236     }
00237 }
00238 #else
00239 # define replace_line(i,s) invalidate_line(i)
00240 #endif
00241 
00242 /* Return up to COLS characters of the line of s starting at pos,     */
00243 /* returning only characters after the given column.                  */
00244 CORD retrieve_line(CORD s, size_t pos, unsigned column)
00245 {
00246     CORD candidate = CORD_substr(s, pos, column + COLS);
00247                      /* avoids scanning very long lines */
00248     int eol = CORD_chr(candidate, 0, '\n');
00249     int len;
00250     
00251     if (eol == CORD_NOT_FOUND) eol = CORD_len(candidate);
00252     len = (int)eol - (int)column;
00253     if (len < 0) len = 0;
00254     return(CORD_substr(s, pos + column, len));
00255 }
00256 
00257 # ifdef WIN32
00258 #   define refresh();
00259 
00260     CORD retrieve_screen_line(int i)
00261     {
00262        register size_t pos;
00263        
00264        invalidate_map(dis_line + LINES);  /* Prune search */
00265        pos = line_pos(dis_line + i, 0);
00266        if (pos == CORD_NOT_FOUND) return(CORD_EMPTY);
00267        return(retrieve_line(current, pos, dis_col));
00268     }
00269 # endif
00270 
00271 /* Display the visible section of the current file       */
00272 void redisplay(void)
00273 {
00274     register int i;
00275     
00276     invalidate_map(dis_line + LINES);     /* Prune search */
00277     for (i = 0; i < LINES; i++) {
00278         if (need_redisplay == ALL || need_redisplay == i) {
00279             register size_t pos = line_pos(dis_line + i, 0);
00280             
00281             if (pos == CORD_NOT_FOUND) break;
00282             replace_line(i, retrieve_line(current, pos, dis_col));
00283             if (need_redisplay == i) goto done;
00284         }
00285     }
00286     for (; i < LINES; i++) replace_line(i, CORD_EMPTY);
00287 done:
00288     refresh();
00289     need_redisplay = NONE;
00290 }
00291 
00292 int dis_granularity;
00293 
00294 /* Update dis_line, dis_col, and dis_pos to make cursor visible.      */
00295 /* Assumes line, col, dis_line, dis_pos are in bounds.                */
00296 void normalize_display()
00297 {
00298     int old_line = dis_line;
00299     int old_col = dis_col;
00300     
00301     dis_granularity = 1;
00302     if (LINES > 15 && COLS > 15) dis_granularity = 2;
00303     while (dis_line > line) dis_line -= dis_granularity;
00304     while (dis_col > col) dis_col -= dis_granularity;
00305     while (line >= dis_line + LINES) dis_line += dis_granularity;
00306     while (col >= dis_col + COLS) dis_col += dis_granularity;
00307     if (old_line != dis_line || old_col != dis_col) {
00308         need_redisplay = ALL;
00309     }
00310 }
00311 
00312 # if defined(WIN32)
00313 # elif defined(MACINTOSH)
00314 #             define move_cursor(x,y) cgotoxy(x + 1, y + 1, stdout)
00315 # else
00316 #             define move_cursor(x,y) move(y,x)
00317 # endif
00318 
00319 /* Adjust display so that cursor is visible; move cursor into position       */
00320 /* Update screen if necessary.                                        */
00321 void fix_cursor(void)
00322 {
00323     normalize_display();
00324     if (need_redisplay != NONE) redisplay();
00325     move_cursor(col - dis_col, line - dis_line);
00326     refresh();
00327 #   ifndef WIN32
00328       fflush(stdout);
00329 #   endif
00330 }
00331 
00332 /* Make sure line, col, and dis_pos are somewhere inside file. */
00333 /* Recompute file_pos.      Assumes dis_pos is accurate or past eof   */
00334 void fix_pos()
00335 {
00336     int my_col = col;
00337     
00338     if ((size_t)line > current_len) line = current_len;
00339     file_pos = line_pos(line, &my_col);
00340     if (file_pos == CORD_NOT_FOUND) {
00341         for (line = current_map -> line, file_pos = current_map -> pos;
00342              file_pos < current_len;
00343              line++, file_pos = CORD_chr(current, file_pos, '\n') + 1);
00344        line--;
00345         file_pos = line_pos(line, &col);
00346     } else {
00347        col = my_col;
00348     }
00349 }
00350 
00351 #if defined(WIN32)
00352 #  define beep() Beep(1000 /* Hz */, 300 /* msecs */) 
00353 #elif defined(MACINTOSH)
00354 #      define beep() SysBeep(1)
00355 #else
00356 /*
00357  * beep() is part of some curses packages and not others.
00358  * We try to match the type of the builtin one, if any.
00359  */
00360 #ifdef __STDC__
00361     int beep(void)
00362 #else
00363     int beep()
00364 #endif
00365 {
00366     putc('\007', stderr);
00367     return(0);
00368 }
00369 #endif
00370 
00371 #   define NO_PREFIX -1
00372 #   define BARE_PREFIX -2
00373 int repeat_count = NO_PREFIX;      /* Current command prefix. */
00374 
00375 int locate_mode = 0;               /* Currently between 2 ^Ls  */
00376 CORD locate_string = CORD_EMPTY;   /* Current search string.   */
00377 
00378 char * arg_file_name;
00379 
00380 #ifdef WIN32
00381 /* Change the current position to whatever is currently displayed at  */
00382 /* the given SCREEN coordinates.                               */
00383 void set_position(int c, int l)
00384 {
00385     line = l + dis_line;
00386     col = c + dis_col;
00387     fix_pos();
00388     move_cursor(col - dis_col, line - dis_line);
00389 }
00390 #endif /* WIN32 */
00391 
00392 /* Perform the command associated with character c.  C may be an      */
00393 /* integer > 256 denoting a windows command, one of the above control */
00394 /* characters, or another ASCII character to be used as either a      */
00395 /* character to be inserted, a repeat count, or a search string,      */
00396 /* depending on the current state.                             */
00397 void do_command(int c)
00398 {
00399     int i;
00400     int need_fix_pos;
00401     FILE * out;
00402     
00403     if ( c == '\r') c = '\n';
00404     if (locate_mode) {
00405         size_t new_pos;
00406           
00407         if (c == LOCATE) {
00408               locate_mode = 0;
00409               locate_string = CORD_EMPTY;
00410               return;
00411         }
00412         locate_string = CORD_cat_char(locate_string, (char)c);
00413         new_pos = CORD_str(current, file_pos - CORD_len(locate_string) + 1,
00414                         locate_string);
00415         if (new_pos != CORD_NOT_FOUND) {
00416             need_redisplay = ALL;
00417             new_pos += CORD_len(locate_string);
00418             for (;;) {
00419                      file_pos = line_pos(line + 1, 0);
00420                      if (file_pos > new_pos) break;
00421                      line++;
00422             }
00423             col = new_pos - line_pos(line, 0);
00424             file_pos = new_pos;
00425             fix_cursor();
00426         } else {
00427             locate_string = CORD_substr(locate_string, 0,
00428                                           CORD_len(locate_string) - 1);
00429             beep();
00430         }
00431         return;
00432     }
00433     if (c == REPEAT) {
00434        repeat_count = BARE_PREFIX; return;
00435     } else if (c < 0x100 && isdigit(c)){
00436         if (repeat_count == BARE_PREFIX) {
00437           repeat_count = c - '0'; return;
00438         } else if (repeat_count != NO_PREFIX) {
00439           repeat_count = 10 * repeat_count + c - '0'; return;
00440         }
00441     }
00442     if (repeat_count == NO_PREFIX) repeat_count = 1;
00443     if (repeat_count == BARE_PREFIX && (c == UP || c == DOWN)) {
00444        repeat_count = LINES - dis_granularity;
00445     }
00446     if (repeat_count == BARE_PREFIX) repeat_count = 8;
00447     need_fix_pos = 0;
00448     for (i = 0; i < repeat_count; i++) {
00449         switch(c) {
00450           case LOCATE:
00451             locate_mode = 1;
00452             break;
00453           case TOP:
00454             line = col = file_pos = 0;
00455             break;
00456          case UP:
00457            if (line != 0) {
00458                line--;
00459                need_fix_pos = 1;
00460            }
00461            break;
00462          case DOWN:
00463            line++;
00464            need_fix_pos = 1;
00465            break;
00466          case LEFT:
00467            if (col != 0) {
00468                col--; file_pos--;
00469            }
00470            break;
00471          case RIGHT:
00472            if (CORD_fetch(current, file_pos) == '\n') break;
00473            col++; file_pos++;
00474            break;
00475          case UNDO:
00476            del_hist();
00477            need_redisplay = ALL; need_fix_pos = 1;
00478            break;
00479          case BS:
00480            if (col == 0) {
00481                beep();
00482                break;
00483            }
00484            col--; file_pos--;
00485            /* fall through: */
00486          case DEL:
00487            if (file_pos == current_len-1) break;
00488               /* Can't delete trailing newline */
00489            if (CORD_fetch(current, file_pos) == '\n') {
00490                need_redisplay = ALL; need_fix_pos = 1;
00491            } else {
00492                need_redisplay = line - dis_line;
00493            }
00494            add_hist(CORD_cat(
00495                      CORD_substr(current, 0, file_pos),
00496                      CORD_substr(current, file_pos+1, current_len)));
00497            invalidate_map(line);
00498            break;
00499          case WRITE:
00500            {
00501               CORD name = CORD_cat(CORD_from_char_star(arg_file_name),
00502                                  ".new");
00503 
00504                if ((out = fopen(CORD_to_const_char_star(name), "wb")) == NULL
00505                    || CORD_put(current, out) == EOF) {
00506                   de_error("Write failed\n");
00507                   need_redisplay = ALL;
00508                 } else {
00509                     fclose(out);
00510                 }
00511            }
00512             break;
00513          default:
00514            {
00515                CORD left_part = CORD_substr(current, 0, file_pos);
00516                CORD right_part = CORD_substr(current, file_pos, current_len);
00517                
00518                add_hist(CORD_cat(CORD_cat_char(left_part, (char)c),
00519                               right_part));
00520                invalidate_map(line);
00521                if (c == '\n') {
00522                    col = 0; line++; file_pos++;
00523                    need_redisplay = ALL;
00524                } else {
00525                    col++; file_pos++;
00526                    need_redisplay = line - dis_line;
00527               }
00528                break;
00529            }
00530         }
00531     }
00532     if (need_fix_pos) fix_pos();
00533     fix_cursor();
00534     repeat_count = NO_PREFIX;
00535 }
00536 
00537 /* OS independent initialization */
00538 
00539 void generic_init(void)
00540 {
00541     FILE * f;
00542     CORD initial;
00543     
00544     if ((f = fopen(arg_file_name, "rb")) == NULL) {
00545        initial = "\n";
00546     } else {
00547         initial = CORD_from_file(f);
00548         if (initial == CORD_EMPTY
00549             || CORD_fetch(initial, CORD_len(initial)-1) != '\n') {
00550             initial = CORD_cat(initial, "\n");
00551         }
00552     }
00553     add_map(0,0);
00554     add_hist(initial);
00555     now -> map = current_map;
00556     now -> previous = now;  /* Can't back up further: beginning of the world */
00557     need_redisplay = ALL;
00558     fix_cursor();
00559 }
00560 
00561 #ifndef WIN32
00562 
00563 main(argc, argv)
00564 int argc;
00565 char ** argv;
00566 {
00567     int c;
00568 
00569 #if defined(MACINTOSH)
00570        console_options.title = "\pDumb Editor";
00571        cshow(stdout);
00572        argc = ccommand(&argv);
00573 #endif
00574     GC_INIT();
00575     
00576     if (argc != 2) goto usage;
00577     arg_file_name = argv[1];
00578     setvbuf(stdout, GC_MALLOC_ATOMIC(8192), _IOFBF, 8192);
00579     initscr();
00580     noecho(); nonl(); cbreak();
00581     generic_init();
00582     while ((c = getchar()) != QUIT) {
00583               if (c == EOF) break;
00584            do_command(c);
00585     }
00586 done:
00587     move(LINES-1, 0);
00588     clrtoeol();
00589     refresh();
00590     nl();
00591     echo();
00592     endwin();
00593     exit(0);
00594 usage:
00595     fprintf(stderr, "Usage: %s file\n", argv[0]);
00596     fprintf(stderr, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n");
00597     fprintf(stderr, "Undo: ^U    Write to <file>.new: ^W");
00598     fprintf(stderr, "Quit:^D  Repeat count: ^R[n]\n");
00599     fprintf(stderr, "Top: ^T   Locate (search, find): ^L text ^L\n");
00600     exit(1);
00601 }
00602 
00603 #endif  /* !WIN32 */