Back to index

plt-scheme  4.2.1
Classes | Defines | Typedefs | Functions | Variables
de.c File Reference
#include <stdio.h>
#include "gc.h"
#include "cord.h"
#include <curses.h>
#include "de_cmds.h"

Go to the source code of this file.

Classes

struct  LineMapRep
struct  HistoryRep

Defines

#define de_error(s)   { fprintf(stderr, s); sleep(2); }
#define MAX_MAP_SIZE   3000
#define ALL   -1
#define NONE   - 2
#define move_cursor(x, y)   move(y,x)
#define NO_PREFIX   -1
#define BARE_PREFIX   -2

Typedefs

typedef struct LineMapRepline_map
typedef struct HistoryRephistory

Functions

void invalidate_map (int i)
void prune_map ()
void add_map (int line, size_t pos)
size_t line_pos (int i, int *c)
void add_hist (CORD s)
void del_hist (void)
void replace_line (int i, CORD s)
CORD retrieve_line (CORD s, size_t pos, unsigned column)
void redisplay (void)
void normalize_display ()
void fix_cursor (void)
void fix_pos ()
int beep ()
void do_command (int c)
void generic_init (void)
 main (int argc, char **argv)

Variables

history now = 0
CORD current
size_t current_len
line_map current_map = 0
size_t current_map_size = 0
int dis_line = 0
int dis_col = 0
int need_redisplay = 0
int line = 0
int col = 0
size_t file_pos = 0
CORDscreen = 0
int screen_size = 0
int dis_granularity
int repeat_count = NO_PREFIX
int locate_mode = 0
CORD locate_string = CORD_EMPTY
char * arg_file_name

Class Documentation

struct LineMapRep

Definition at line 75 of file de.c.

Collaboration diagram for LineMapRep:
Class Members
int line
size_t pos
struct LineMapRep * previous
struct HistoryRep

Definition at line 82 of file de.c.

Collaboration diagram for HistoryRep:
Class Members
CORD file_contents
line_map map
struct HistoryRep * previous

Define Documentation

#define ALL   -1

Definition at line 101 of file de.c.

#define BARE_PREFIX   -2

Definition at line 372 of file de.c.

#define de_error (   s)    { fprintf(stderr, s); sleep(2); }

Definition at line 69 of file de.c.

#define MAX_MAP_SIZE   3000

Definition at line 95 of file de.c.

#define move_cursor (   x,
  y 
)    move(y,x)

Definition at line 316 of file de.c.

#define NO_PREFIX   -1

Definition at line 371 of file de.c.

#define NONE   - 2

Definition at line 102 of file de.c.


Typedef Documentation


Function Documentation

void add_hist ( CORD  s)

Definition at line 181 of file de.c.

{
    history new_file = GC_NEW(struct HistoryRep);
    
    new_file -> file_contents = current = s;
    current_len = CORD_len(s);
    new_file -> previous = now;
    if (now != 0) now -> map = current_map;
    now = new_file;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void add_map ( int  line,
size_t  pos 
)

Definition at line 136 of file de.c.

{
    line_map new_map = GC_NEW(struct LineMapRep);
    
    if (current_map_size >= MAX_MAP_SIZE) prune_map();
    new_map -> line = line;
    new_map -> pos = pos;
    new_map -> previous = current_map;
    current_map = new_map;
    current_map_size++;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int beep ( )

Definition at line 363 of file de.c.

{
    putc('\007', stderr);
    return(0);
}

Here is the caller graph for this function:

Definition at line 192 of file de.c.

{
    now = now -> previous;
    current = now -> file_contents;
    current_map = now -> map;
    current_len = CORD_len(current);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 397 of file de.c.

{
    int i;
    int need_fix_pos;
    FILE * out;
    
    if ( c == '\r') c = '\n';
    if (locate_mode) {
        size_t new_pos;
          
        if (c == LOCATE) {
              locate_mode = 0;
              locate_string = CORD_EMPTY;
              return;
        }
        locate_string = CORD_cat_char(locate_string, (char)c);
        new_pos = CORD_str(current, file_pos - CORD_len(locate_string) + 1,
                        locate_string);
        if (new_pos != CORD_NOT_FOUND) {
            need_redisplay = ALL;
            new_pos += CORD_len(locate_string);
            for (;;) {
                     file_pos = line_pos(line + 1, 0);
                     if (file_pos > new_pos) break;
                     line++;
            }
            col = new_pos - line_pos(line, 0);
            file_pos = new_pos;
            fix_cursor();
        } else {
            locate_string = CORD_substr(locate_string, 0,
                                          CORD_len(locate_string) - 1);
            beep();
        }
        return;
    }
    if (c == REPEAT) {
       repeat_count = BARE_PREFIX; return;
    } else if (c < 0x100 && isdigit(c)){
        if (repeat_count == BARE_PREFIX) {
          repeat_count = c - '0'; return;
        } else if (repeat_count != NO_PREFIX) {
          repeat_count = 10 * repeat_count + c - '0'; return;
        }
    }
    if (repeat_count == NO_PREFIX) repeat_count = 1;
    if (repeat_count == BARE_PREFIX && (c == UP || c == DOWN)) {
       repeat_count = LINES - dis_granularity;
    }
    if (repeat_count == BARE_PREFIX) repeat_count = 8;
    need_fix_pos = 0;
    for (i = 0; i < repeat_count; i++) {
        switch(c) {
          case LOCATE:
            locate_mode = 1;
            break;
          case TOP:
            line = col = file_pos = 0;
            break;
         case UP:
           if (line != 0) {
               line--;
               need_fix_pos = 1;
           }
           break;
         case DOWN:
           line++;
           need_fix_pos = 1;
           break;
         case LEFT:
           if (col != 0) {
               col--; file_pos--;
           }
           break;
         case RIGHT:
           if (CORD_fetch(current, file_pos) == '\n') break;
           col++; file_pos++;
           break;
         case UNDO:
           del_hist();
           need_redisplay = ALL; need_fix_pos = 1;
           break;
         case BS:
           if (col == 0) {
               beep();
               break;
           }
           col--; file_pos--;
           /* fall through: */
         case DEL:
           if (file_pos == current_len-1) break;
              /* Can't delete trailing newline */
           if (CORD_fetch(current, file_pos) == '\n') {
               need_redisplay = ALL; need_fix_pos = 1;
           } else {
               need_redisplay = line - dis_line;
           }
           add_hist(CORD_cat(
                     CORD_substr(current, 0, file_pos),
                     CORD_substr(current, file_pos+1, current_len)));
           invalidate_map(line);
           break;
         case WRITE:
           {
              CORD name = CORD_cat(CORD_from_char_star(arg_file_name),
                                 ".new");

               if ((out = fopen(CORD_to_const_char_star(name), "wb")) == NULL
                   || CORD_put(current, out) == EOF) {
                  de_error("Write failed\n");
                  need_redisplay = ALL;
                } else {
                    fclose(out);
                }
           }
            break;
         default:
           {
               CORD left_part = CORD_substr(current, 0, file_pos);
               CORD right_part = CORD_substr(current, file_pos, current_len);
               
               add_hist(CORD_cat(CORD_cat_char(left_part, (char)c),
                              right_part));
               invalidate_map(line);
               if (c == '\n') {
                   col = 0; line++; file_pos++;
                   need_redisplay = ALL;
               } else {
                   col++; file_pos++;
                   need_redisplay = line - dis_line;
              }
               break;
           }
        }
    }
    if (need_fix_pos) fix_pos();
    fix_cursor();
    repeat_count = NO_PREFIX;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 321 of file de.c.

{
    normalize_display();
    if (need_redisplay != NONE) redisplay();
    move_cursor(col - dis_col, line - dis_line);
    refresh();
#   ifndef WIN32
      fflush(stdout);
#   endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 334 of file de.c.

{
    int my_col = col;
    
    if ((size_t)line > current_len) line = current_len;
    file_pos = line_pos(line, &my_col);
    if (file_pos == CORD_NOT_FOUND) {
        for (line = current_map -> line, file_pos = current_map -> pos;
             file_pos < current_len;
             line++, file_pos = CORD_chr(current, file_pos, '\n') + 1);
       line--;
        file_pos = line_pos(line, &col);
    } else {
       col = my_col;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 539 of file de.c.

{
    FILE * f;
    CORD initial;
    
    if ((f = fopen(arg_file_name, "rb")) == NULL) {
       initial = "\n";
    } else {
        initial = CORD_from_file(f);
        if (initial == CORD_EMPTY
            || CORD_fetch(initial, CORD_len(initial)-1) != '\n') {
            initial = CORD_cat(initial, "\n");
        }
    }
    add_map(0,0);
    add_hist(initial);
    now -> map = current_map;
    now -> previous = now;  /* Can't back up further: beginning of the world */
    need_redisplay = ALL;
    fix_cursor();
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 112 of file de.c.

{
    while(current_map -> line > i) {
        current_map = current_map -> previous;
        current_map_size--;
    }
}

Here is the caller graph for this function:

size_t line_pos ( int  i,
int c 
)

Definition at line 155 of file de.c.

{
    int j;
    size_t cur;
    size_t next;
    line_map map = current_map;
    
    while (map -> line > i) map = map -> previous;
    if (map -> line < i - 2) /* rebuild */ invalidate_map(i);
    for (j = map -> line, cur = map -> pos; j < i;) {
       cur = CORD_chr(current, cur, '\n');
        if (cur == current_len-1) return(CORD_NOT_FOUND);
        cur++;
        if (++j > current_map -> line) add_map(j, cur);
    }
    if (c != 0) {
        next = CORD_chr(current, cur, '\n');
        if (next == CORD_NOT_FOUND) next = current_len - 1;
        if (next < cur + *c) {
            *c = next - cur;
        }
        cur += *c;
    }
    return(cur);
}

Here is the call graph for this function:

Here is the caller graph for this function:

main ( int  argc,
char **  argv 
)

Definition at line 563 of file de.c.

{
    int c;

#if defined(MACINTOSH)
       console_options.title = "\pDumb Editor";
       cshow(stdout);
       argc = ccommand(&argv);
#endif
    GC_INIT();
    
    if (argc != 2) goto usage;
    arg_file_name = argv[1];
    setvbuf(stdout, GC_MALLOC_ATOMIC(8192), _IOFBF, 8192);
    initscr();
    noecho(); nonl(); cbreak();
    generic_init();
    while ((c = getchar()) != QUIT) {
              if (c == EOF) break;
           do_command(c);
    }
done:
    move(LINES-1, 0);
    clrtoeol();
    refresh();
    nl();
    echo();
    endwin();
    exit(0);
usage:
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    fprintf(stderr, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n");
    fprintf(stderr, "Undo: ^U    Write to <file>.new: ^W");
    fprintf(stderr, "Quit:^D  Repeat count: ^R[n]\n");
    fprintf(stderr, "Top: ^T   Locate (search, find): ^L text ^L\n");
    exit(1);
}

Here is the call graph for this function:

Definition at line 296 of file de.c.

{
    int old_line = dis_line;
    int old_col = dis_col;
    
    dis_granularity = 1;
    if (LINES > 15 && COLS > 15) dis_granularity = 2;
    while (dis_line > line) dis_line -= dis_granularity;
    while (dis_col > col) dis_col -= dis_granularity;
    while (line >= dis_line + LINES) dis_line += dis_granularity;
    while (col >= dis_col + COLS) dis_col += dis_granularity;
    if (old_line != dis_line || old_col != dis_col) {
        need_redisplay = ALL;
    }
}

Here is the caller graph for this function:

Definition at line 122 of file de.c.

{
    line_map map = current_map;
    int start_line = map -> line;
    
    current_map_size = 0;
    for(; map != 0; map = map -> previous) {
       current_map_size++;
       if (map -> line < start_line - LINES && map -> previous != 0) {
           map -> previous = map -> previous -> previous;
       }
    }
}

Here is the caller graph for this function:

Definition at line 272 of file de.c.

{
    register int i;
    
    invalidate_map(dis_line + LINES);     /* Prune search */
    for (i = 0; i < LINES; i++) {
        if (need_redisplay == ALL || need_redisplay == i) {
            register size_t pos = line_pos(dis_line + i, 0);
            
            if (pos == CORD_NOT_FOUND) break;
            replace_line(i, retrieve_line(current, pos, dis_col));
            if (need_redisplay == i) goto done;
        }
    }
    for (; i < LINES; i++) replace_line(i, CORD_EMPTY);
done:
    refresh();
    need_redisplay = NONE;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void replace_line ( int  i,
CORD  s 
)

Definition at line 208 of file de.c.

{
    register int c;
    CORD_pos p;
    size_t len = CORD_len(s);
    
    if (screen == 0 || LINES > screen_size) {
        screen_size = LINES;
       screen = (CORD *)GC_MALLOC(screen_size * sizeof(CORD));
    }
#   if !defined(MACINTOSH)
        /* A gross workaround for an apparent curses bug: */
        if (i == LINES-1 && len == COLS) {
            s = CORD_substr(s, 0, CORD_len(s) - 1);
        }
#   endif
    if (CORD_cmp(screen[i], s) != 0) {
        move(i, 0); clrtoeol(); move(i,0);

        CORD_FOR (p, s) {
            c = CORD_pos_fetch(p) & 0x7f;
            if (iscntrl(c)) {
              standout(); addch(c + 0x40); standend();
            } else {
               addch(c);
           }
       }
       screen[i] = s;
    }
}

Here is the call graph for this function:

Here is the caller graph for this function:

CORD retrieve_line ( CORD  s,
size_t  pos,
unsigned  column 
)

Definition at line 244 of file de.c.

{
    CORD candidate = CORD_substr(s, pos, column + COLS);
                     /* avoids scanning very long lines */
    int eol = CORD_chr(candidate, 0, '\n');
    int len;
    
    if (eol == CORD_NOT_FOUND) eol = CORD_len(candidate);
    len = (int)eol - (int)column;
    if (len < 0) len = 0;
    return(CORD_substr(s, pos + column, len));
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 378 of file de.c.

int col = 0

Definition at line 108 of file de.c.

Definition at line 89 of file de.c.

size_t current_len

Definition at line 90 of file de.c.

Definition at line 91 of file de.c.

size_t current_map_size = 0

Definition at line 92 of file de.c.

int dis_col = 0

Definition at line 99 of file de.c.

Definition at line 292 of file de.c.

Definition at line 98 of file de.c.

size_t file_pos = 0

Definition at line 109 of file de.c.

int line = 0

Definition at line 107 of file de.c.

Definition at line 375 of file de.c.

Definition at line 376 of file de.c.

Definition at line 103 of file de.c.

history now = 0

Definition at line 88 of file de.c.

Definition at line 373 of file de.c.

CORD* screen = 0

Definition at line 201 of file de.c.

Definition at line 202 of file de.c.