OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-dev/] [fsf-gcc-snapshot-1-mar-12/] [or1k-gcc/] [boehm-gc/] [cord/] [de.c] - Diff between revs 721 and 783

Only display areas with differences | Details | Blame | View Log

Rev 721 Rev 783
/*
/*
 * Copyright (c) 1993-1994 by Xerox Corporation.  All rights reserved.
 * Copyright (c) 1993-1994 by Xerox Corporation.  All rights reserved.
 *
 *
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
 * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
 *
 *
 * Permission is hereby granted to use or copy this program
 * Permission is hereby granted to use or copy this program
 * for any purpose,  provided the above notices are retained on all copies.
 * for any purpose,  provided the above notices are retained on all copies.
 * Permission to modify the code and to distribute modified code is granted,
 * Permission to modify the code and to distribute modified code is granted,
 * provided the above notices are retained, and a notice that the code was
 * provided the above notices are retained, and a notice that the code was
 * modified is included with the above copyright notice.
 * modified is included with the above copyright notice.
 *
 *
 * Author: Hans-J. Boehm (boehm@parc.xerox.com)
 * Author: Hans-J. Boehm (boehm@parc.xerox.com)
 */
 */
/*
/*
 * A really simple-minded text editor based on cords.
 * A really simple-minded text editor based on cords.
 * Things it does right:
 * Things it does right:
 *      No size bounds.
 *      No size bounds.
 *      Inbounded undo.
 *      Inbounded undo.
 *      Shouldn't crash no matter what file you invoke it on (e.g. /vmunix)
 *      Shouldn't crash no matter what file you invoke it on (e.g. /vmunix)
 *              (Make sure /vmunix is not writable before you try this.)
 *              (Make sure /vmunix is not writable before you try this.)
 *      Scrolls horizontally.
 *      Scrolls horizontally.
 * Things it does wrong:
 * Things it does wrong:
 *      It doesn't handle tabs reasonably (use "expand" first).
 *      It doesn't handle tabs reasonably (use "expand" first).
 *      The command set is MUCH too small.
 *      The command set is MUCH too small.
 *      The redisplay algorithm doesn't let curses do the scrolling.
 *      The redisplay algorithm doesn't let curses do the scrolling.
 *      The rule for moving the window over the file is suboptimal.
 *      The rule for moving the window over the file is suboptimal.
 */
 */
/* Boehm, February 6, 1995 12:27 pm PST */
/* Boehm, February 6, 1995 12:27 pm PST */
 
 
/* Boehm, May 19, 1994 2:20 pm PDT */
/* Boehm, May 19, 1994 2:20 pm PDT */
#include <stdio.h>
#include <stdio.h>
#include "gc.h"
#include "gc.h"
#include "cord.h"
#include "cord.h"
 
 
#ifdef THINK_C
#ifdef THINK_C
#define MACINTOSH
#define MACINTOSH
#include <ctype.h>
#include <ctype.h>
#endif
#endif
 
 
#if defined(__BORLANDC__) && !defined(WIN32)
#if defined(__BORLANDC__) && !defined(WIN32)
    /* If this is DOS or win16, we'll fail anyway.      */
    /* If this is DOS or win16, we'll fail anyway.      */
    /* Might as well assume win32.                      */
    /* Might as well assume win32.                      */
#   define WIN32
#   define WIN32
#endif
#endif
 
 
#if defined(WIN32)
#if defined(WIN32)
#  include <windows.h>
#  include <windows.h>
#  include "de_win.h"
#  include "de_win.h"
#elif defined(MACINTOSH)
#elif defined(MACINTOSH)
#       include <console.h>
#       include <console.h>
/* curses emulation. */
/* curses emulation. */
#       define initscr()
#       define initscr()
#       define endwin()
#       define endwin()
#       define nonl()
#       define nonl()
#       define noecho() csetmode(C_NOECHO, stdout)
#       define noecho() csetmode(C_NOECHO, stdout)
#       define cbreak() csetmode(C_CBREAK, stdout)
#       define cbreak() csetmode(C_CBREAK, stdout)
#       define refresh()
#       define refresh()
#       define addch(c) putchar(c)
#       define addch(c) putchar(c)
#       define standout() cinverse(1, stdout)
#       define standout() cinverse(1, stdout)
#       define standend() cinverse(0, stdout)
#       define standend() cinverse(0, stdout)
#       define move(line,col) cgotoxy(col + 1, line + 1, stdout)
#       define move(line,col) cgotoxy(col + 1, line + 1, stdout)
#       define clrtoeol() ccleol(stdout)
#       define clrtoeol() ccleol(stdout)
#       define de_error(s) { fprintf(stderr, s); getchar(); }
#       define de_error(s) { fprintf(stderr, s); getchar(); }
#       define LINES 25
#       define LINES 25
#       define COLS 80
#       define COLS 80
#else
#else
#  include <curses.h>
#  include <curses.h>
#  define de_error(s) { fprintf(stderr, s); sleep(2); }
#  define de_error(s) { fprintf(stderr, s); sleep(2); }
#endif
#endif
#include "de_cmds.h"
#include "de_cmds.h"
 
 
/* List of line number to position mappings, in descending order. */
/* List of line number to position mappings, in descending order. */
/* There may be holes.                                            */
/* There may be holes.                                            */
typedef struct LineMapRep {
typedef struct LineMapRep {
    int line;
    int line;
    size_t pos;
    size_t pos;
    struct LineMapRep * previous;
    struct LineMapRep * previous;
} * line_map;
} * line_map;
 
 
/* List of file versions, one per edit operation */
/* List of file versions, one per edit operation */
typedef struct HistoryRep {
typedef struct HistoryRep {
    CORD file_contents;
    CORD file_contents;
    struct HistoryRep * previous;
    struct HistoryRep * previous;
    line_map map;       /* Invalid for first record "now" */
    line_map map;       /* Invalid for first record "now" */
} * history;
} * history;
 
 
history now = 0;
history now = 0;
CORD current;           /* == now -> file_contents.     */
CORD current;           /* == now -> file_contents.     */
size_t current_len;     /* Current file length.         */
size_t current_len;     /* Current file length.         */
line_map current_map = 0;        /* Current line no. to pos. map  */
line_map current_map = 0;        /* Current line no. to pos. map  */
size_t current_map_size = 0;     /* Number of current_map entries.       */
size_t current_map_size = 0;     /* Number of current_map entries.       */
                                /* Not always accurate, but reset       */
                                /* Not always accurate, but reset       */
                                /* by prune_map.                        */
                                /* by prune_map.                        */
# define MAX_MAP_SIZE 3000
# define MAX_MAP_SIZE 3000
 
 
/* Current display position */
/* Current display position */
int dis_line = 0;
int dis_line = 0;
int dis_col = 0;
int dis_col = 0;
 
 
# define ALL -1
# define ALL -1
# define NONE - 2
# define NONE - 2
int need_redisplay = 0;  /* Line that needs to be redisplayed.   */
int need_redisplay = 0;  /* Line that needs to be redisplayed.   */
 
 
 
 
/* Current cursor position. Always within file. */
/* Current cursor position. Always within file. */
int line = 0;
int line = 0;
int col = 0;
int col = 0;
size_t file_pos = 0;     /* Character position corresponding to cursor.  */
size_t file_pos = 0;     /* Character position corresponding to cursor.  */
 
 
/* Invalidate line map for lines > i */
/* Invalidate line map for lines > i */
void invalidate_map(int i)
void invalidate_map(int i)
{
{
    while(current_map -> line > i) {
    while(current_map -> line > i) {
        current_map = current_map -> previous;
        current_map = current_map -> previous;
        current_map_size--;
        current_map_size--;
    }
    }
}
}
 
 
/* Reduce the number of map entries to save space for huge files. */
/* Reduce the number of map entries to save space for huge files. */
/* This also affects maps in histories.                           */
/* This also affects maps in histories.                           */
void prune_map()
void prune_map()
{
{
    line_map map = current_map;
    line_map map = current_map;
    int start_line = map -> line;
    int start_line = map -> line;
 
 
    current_map_size = 0;
    current_map_size = 0;
    for(; map != 0; map = map -> previous) {
    for(; map != 0; map = map -> previous) {
        current_map_size++;
        current_map_size++;
        if (map -> line < start_line - LINES && map -> previous != 0) {
        if (map -> line < start_line - LINES && map -> previous != 0) {
            map -> previous = map -> previous -> previous;
            map -> previous = map -> previous -> previous;
        }
        }
    }
    }
}
}
/* Add mapping entry */
/* Add mapping entry */
void add_map(int line, size_t pos)
void add_map(int line, size_t pos)
{
{
    line_map new_map = GC_NEW(struct LineMapRep);
    line_map new_map = GC_NEW(struct LineMapRep);
 
 
    if (current_map_size >= MAX_MAP_SIZE) prune_map();
    if (current_map_size >= MAX_MAP_SIZE) prune_map();
    new_map -> line = line;
    new_map -> line = line;
    new_map -> pos = pos;
    new_map -> pos = pos;
    new_map -> previous = current_map;
    new_map -> previous = current_map;
    current_map = new_map;
    current_map = new_map;
    current_map_size++;
    current_map_size++;
}
}
 
 
 
 
 
 
/* Return position of column *c of ith line in   */
/* Return position of column *c of ith line in   */
/* current file. Adjust *c to be within the line.*/
/* current file. Adjust *c to be within the line.*/
/* A 0 pointer is taken as 0 column.             */
/* A 0 pointer is taken as 0 column.             */
/* Returns CORD_NOT_FOUND if i is too big.       */
/* Returns CORD_NOT_FOUND if i is too big.       */
/* Assumes i > dis_line.                         */
/* Assumes i > dis_line.                         */
size_t line_pos(int i, int *c)
size_t line_pos(int i, int *c)
{
{
    int j;
    int j;
    size_t cur;
    size_t cur;
    size_t next;
    size_t next;
    line_map map = current_map;
    line_map map = current_map;
 
 
    while (map -> line > i) map = map -> previous;
    while (map -> line > i) map = map -> previous;
    if (map -> line < i - 2) /* rebuild */ invalidate_map(i);
    if (map -> line < i - 2) /* rebuild */ invalidate_map(i);
    for (j = map -> line, cur = map -> pos; j < i;) {
    for (j = map -> line, cur = map -> pos; j < i;) {
        cur = CORD_chr(current, cur, '\n');
        cur = CORD_chr(current, cur, '\n');
        if (cur == current_len-1) return(CORD_NOT_FOUND);
        if (cur == current_len-1) return(CORD_NOT_FOUND);
        cur++;
        cur++;
        if (++j > current_map -> line) add_map(j, cur);
        if (++j > current_map -> line) add_map(j, cur);
    }
    }
    if (c != 0) {
    if (c != 0) {
        next = CORD_chr(current, cur, '\n');
        next = CORD_chr(current, cur, '\n');
        if (next == CORD_NOT_FOUND) next = current_len - 1;
        if (next == CORD_NOT_FOUND) next = current_len - 1;
        if (next < cur + *c) {
        if (next < cur + *c) {
            *c = next - cur;
            *c = next - cur;
        }
        }
        cur += *c;
        cur += *c;
    }
    }
    return(cur);
    return(cur);
}
}
 
 
void add_hist(CORD s)
void add_hist(CORD s)
{
{
    history new_file = GC_NEW(struct HistoryRep);
    history new_file = GC_NEW(struct HistoryRep);
 
 
    new_file -> file_contents = current = s;
    new_file -> file_contents = current = s;
    current_len = CORD_len(s);
    current_len = CORD_len(s);
    new_file -> previous = now;
    new_file -> previous = now;
    if (now != 0) now -> map = current_map;
    if (now != 0) now -> map = current_map;
    now = new_file;
    now = new_file;
}
}
 
 
void del_hist(void)
void del_hist(void)
{
{
    now = now -> previous;
    now = now -> previous;
    current = now -> file_contents;
    current = now -> file_contents;
    current_map = now -> map;
    current_map = now -> map;
    current_len = CORD_len(current);
    current_len = CORD_len(current);
}
}
 
 
/* Current screen_contents; a dynamically allocated array of CORDs      */
/* Current screen_contents; a dynamically allocated array of CORDs      */
CORD * screen = 0;
CORD * screen = 0;
int screen_size = 0;
int screen_size = 0;
 
 
# ifndef WIN32
# ifndef WIN32
/* Replace a line in the curses stdscr. All control characters are      */
/* Replace a line in the curses stdscr. All control characters are      */
/* displayed as upper case characters in standout mode.  This isn't     */
/* displayed as upper case characters in standout mode.  This isn't     */
/* terribly appropriate for tabs.                                                                       */
/* terribly appropriate for tabs.                                                                       */
void replace_line(int i, CORD s)
void replace_line(int i, CORD s)
{
{
    register int c;
    register int c;
    CORD_pos p;
    CORD_pos p;
    size_t len = CORD_len(s);
    size_t len = CORD_len(s);
 
 
    if (screen == 0 || LINES > screen_size) {
    if (screen == 0 || LINES > screen_size) {
        screen_size = LINES;
        screen_size = LINES;
        screen = (CORD *)GC_MALLOC(screen_size * sizeof(CORD));
        screen = (CORD *)GC_MALLOC(screen_size * sizeof(CORD));
    }
    }
#   if !defined(MACINTOSH)
#   if !defined(MACINTOSH)
        /* A gross workaround for an apparent curses bug: */
        /* A gross workaround for an apparent curses bug: */
        if (i == LINES-1 && len == COLS) {
        if (i == LINES-1 && len == COLS) {
            s = CORD_substr(s, 0, CORD_len(s) - 1);
            s = CORD_substr(s, 0, CORD_len(s) - 1);
        }
        }
#   endif
#   endif
    if (CORD_cmp(screen[i], s) != 0) {
    if (CORD_cmp(screen[i], s) != 0) {
        move(i, 0); clrtoeol(); move(i,0);
        move(i, 0); clrtoeol(); move(i,0);
 
 
        CORD_FOR (p, s) {
        CORD_FOR (p, s) {
            c = CORD_pos_fetch(p) & 0x7f;
            c = CORD_pos_fetch(p) & 0x7f;
            if (iscntrl(c)) {
            if (iscntrl(c)) {
                standout(); addch(c + 0x40); standend();
                standout(); addch(c + 0x40); standend();
            } else {
            } else {
                addch(c);
                addch(c);
            }
            }
        }
        }
        screen[i] = s;
        screen[i] = s;
    }
    }
}
}
#else
#else
# define replace_line(i,s) invalidate_line(i)
# define replace_line(i,s) invalidate_line(i)
#endif
#endif
 
 
/* Return up to COLS characters of the line of s starting at pos,       */
/* Return up to COLS characters of the line of s starting at pos,       */
/* returning only characters after the given column.                    */
/* returning only characters after the given column.                    */
CORD retrieve_line(CORD s, size_t pos, unsigned column)
CORD retrieve_line(CORD s, size_t pos, unsigned column)
{
{
    CORD candidate = CORD_substr(s, pos, column + COLS);
    CORD candidate = CORD_substr(s, pos, column + COLS);
                        /* avoids scanning very long lines      */
                        /* avoids scanning very long lines      */
    int eol = CORD_chr(candidate, 0, '\n');
    int eol = CORD_chr(candidate, 0, '\n');
    int len;
    int len;
 
 
    if (eol == CORD_NOT_FOUND) eol = CORD_len(candidate);
    if (eol == CORD_NOT_FOUND) eol = CORD_len(candidate);
    len = (int)eol - (int)column;
    len = (int)eol - (int)column;
    if (len < 0) len = 0;
    if (len < 0) len = 0;
    return(CORD_substr(s, pos + column, len));
    return(CORD_substr(s, pos + column, len));
}
}
 
 
# ifdef WIN32
# ifdef WIN32
#   define refresh();
#   define refresh();
 
 
    CORD retrieve_screen_line(int i)
    CORD retrieve_screen_line(int i)
    {
    {
        register size_t pos;
        register size_t pos;
 
 
        invalidate_map(dis_line + LINES);       /* Prune search */
        invalidate_map(dis_line + LINES);       /* Prune search */
        pos = line_pos(dis_line + i, 0);
        pos = line_pos(dis_line + i, 0);
        if (pos == CORD_NOT_FOUND) return(CORD_EMPTY);
        if (pos == CORD_NOT_FOUND) return(CORD_EMPTY);
        return(retrieve_line(current, pos, dis_col));
        return(retrieve_line(current, pos, dis_col));
    }
    }
# endif
# endif
 
 
/* Display the visible section of the current file       */
/* Display the visible section of the current file       */
void redisplay(void)
void redisplay(void)
{
{
    register int i;
    register int i;
 
 
    invalidate_map(dis_line + LINES);   /* Prune search */
    invalidate_map(dis_line + LINES);   /* Prune search */
    for (i = 0; i < LINES; i++) {
    for (i = 0; i < LINES; i++) {
        if (need_redisplay == ALL || need_redisplay == i) {
        if (need_redisplay == ALL || need_redisplay == i) {
            register size_t pos = line_pos(dis_line + i, 0);
            register size_t pos = line_pos(dis_line + i, 0);
 
 
            if (pos == CORD_NOT_FOUND) break;
            if (pos == CORD_NOT_FOUND) break;
            replace_line(i, retrieve_line(current, pos, dis_col));
            replace_line(i, retrieve_line(current, pos, dis_col));
            if (need_redisplay == i) goto done;
            if (need_redisplay == i) goto done;
        }
        }
    }
    }
    for (; i < LINES; i++) replace_line(i, CORD_EMPTY);
    for (; i < LINES; i++) replace_line(i, CORD_EMPTY);
done:
done:
    refresh();
    refresh();
    need_redisplay = NONE;
    need_redisplay = NONE;
}
}
 
 
int dis_granularity;
int dis_granularity;
 
 
/* Update dis_line, dis_col, and dis_pos to make cursor visible.        */
/* Update dis_line, dis_col, and dis_pos to make cursor visible.        */
/* Assumes line, col, dis_line, dis_pos are in bounds.                  */
/* Assumes line, col, dis_line, dis_pos are in bounds.                  */
void normalize_display()
void normalize_display()
{
{
    int old_line = dis_line;
    int old_line = dis_line;
    int old_col = dis_col;
    int old_col = dis_col;
 
 
    dis_granularity = 1;
    dis_granularity = 1;
    if (LINES > 15 && COLS > 15) dis_granularity = 2;
    if (LINES > 15 && COLS > 15) dis_granularity = 2;
    while (dis_line > line) dis_line -= dis_granularity;
    while (dis_line > line) dis_line -= dis_granularity;
    while (dis_col > col) dis_col -= dis_granularity;
    while (dis_col > col) dis_col -= dis_granularity;
    while (line >= dis_line + LINES) dis_line += dis_granularity;
    while (line >= dis_line + LINES) dis_line += dis_granularity;
    while (col >= dis_col + COLS) dis_col += dis_granularity;
    while (col >= dis_col + COLS) dis_col += dis_granularity;
    if (old_line != dis_line || old_col != dis_col) {
    if (old_line != dis_line || old_col != dis_col) {
        need_redisplay = ALL;
        need_redisplay = ALL;
    }
    }
}
}
 
 
# if defined(WIN32)
# if defined(WIN32)
# elif defined(MACINTOSH)
# elif defined(MACINTOSH)
#               define move_cursor(x,y) cgotoxy(x + 1, y + 1, stdout)
#               define move_cursor(x,y) cgotoxy(x + 1, y + 1, stdout)
# else
# else
#               define move_cursor(x,y) move(y,x)
#               define move_cursor(x,y) move(y,x)
# endif
# endif
 
 
/* Adjust display so that cursor is visible; move cursor into position  */
/* Adjust display so that cursor is visible; move cursor into position  */
/* Update screen if necessary.                                          */
/* Update screen if necessary.                                          */
void fix_cursor(void)
void fix_cursor(void)
{
{
    normalize_display();
    normalize_display();
    if (need_redisplay != NONE) redisplay();
    if (need_redisplay != NONE) redisplay();
    move_cursor(col - dis_col, line - dis_line);
    move_cursor(col - dis_col, line - dis_line);
    refresh();
    refresh();
#   ifndef WIN32
#   ifndef WIN32
      fflush(stdout);
      fflush(stdout);
#   endif
#   endif
}
}
 
 
/* Make sure line, col, and dis_pos are somewhere inside file.  */
/* Make sure line, col, and dis_pos are somewhere inside file.  */
/* Recompute file_pos.  Assumes dis_pos is accurate or past eof */
/* Recompute file_pos.  Assumes dis_pos is accurate or past eof */
void fix_pos()
void fix_pos()
{
{
    int my_col = col;
    int my_col = col;
 
 
    if ((size_t)line > current_len) line = current_len;
    if ((size_t)line > current_len) line = current_len;
    file_pos = line_pos(line, &my_col);
    file_pos = line_pos(line, &my_col);
    if (file_pos == CORD_NOT_FOUND) {
    if (file_pos == CORD_NOT_FOUND) {
        for (line = current_map -> line, file_pos = current_map -> pos;
        for (line = current_map -> line, file_pos = current_map -> pos;
             file_pos < current_len;
             file_pos < current_len;
             line++, file_pos = CORD_chr(current, file_pos, '\n') + 1);
             line++, file_pos = CORD_chr(current, file_pos, '\n') + 1);
        line--;
        line--;
        file_pos = line_pos(line, &col);
        file_pos = line_pos(line, &col);
    } else {
    } else {
        col = my_col;
        col = my_col;
    }
    }
}
}
 
 
#if defined(WIN32)
#if defined(WIN32)
#  define beep() Beep(1000 /* Hz */, 300 /* msecs */) 
#  define beep() Beep(1000 /* Hz */, 300 /* msecs */) 
#elif defined(MACINTOSH)
#elif defined(MACINTOSH)
#       define beep() SysBeep(1)
#       define beep() SysBeep(1)
#else
#else
/*
/*
 * beep() is part of some curses packages and not others.
 * beep() is part of some curses packages and not others.
 * We try to match the type of the builtin one, if any.
 * We try to match the type of the builtin one, if any.
 */
 */
#ifdef __STDC__
#ifdef __STDC__
    int beep(void)
    int beep(void)
#else
#else
    int beep()
    int beep()
#endif
#endif
{
{
    putc('\007', stderr);
    putc('\007', stderr);
    return(0);
    return(0);
}
}
#endif
#endif
 
 
#   define NO_PREFIX -1
#   define NO_PREFIX -1
#   define BARE_PREFIX -2
#   define BARE_PREFIX -2
int repeat_count = NO_PREFIX;   /* Current command prefix. */
int repeat_count = NO_PREFIX;   /* Current command prefix. */
 
 
int locate_mode = 0;                     /* Currently between 2 ^Ls      */
int locate_mode = 0;                     /* Currently between 2 ^Ls      */
CORD locate_string = CORD_EMPTY;        /* Current search string.       */
CORD locate_string = CORD_EMPTY;        /* Current search string.       */
 
 
char * arg_file_name;
char * arg_file_name;
 
 
#ifdef WIN32
#ifdef WIN32
/* Change the current position to whatever is currently displayed at    */
/* Change the current position to whatever is currently displayed at    */
/* the given SCREEN coordinates.                                        */
/* the given SCREEN coordinates.                                        */
void set_position(int c, int l)
void set_position(int c, int l)
{
{
    line = l + dis_line;
    line = l + dis_line;
    col = c + dis_col;
    col = c + dis_col;
    fix_pos();
    fix_pos();
    move_cursor(col - dis_col, line - dis_line);
    move_cursor(col - dis_col, line - dis_line);
}
}
#endif /* WIN32 */
#endif /* WIN32 */
 
 
/* Perform the command associated with character c.  C may be an        */
/* Perform the command associated with character c.  C may be an        */
/* integer > 256 denoting a windows command, one of the above control   */
/* integer > 256 denoting a windows command, one of the above control   */
/* characters, or another ASCII character to be used as either a        */
/* characters, or another ASCII character to be used as either a        */
/* character to be inserted, a repeat count, or a search string,        */
/* character to be inserted, a repeat count, or a search string,        */
/* depending on the current state.                                      */
/* depending on the current state.                                      */
void do_command(int c)
void do_command(int c)
{
{
    int i;
    int i;
    int need_fix_pos;
    int need_fix_pos;
    FILE * out;
    FILE * out;
 
 
    if ( c == '\r') c = '\n';
    if ( c == '\r') c = '\n';
    if (locate_mode) {
    if (locate_mode) {
        size_t new_pos;
        size_t new_pos;
 
 
        if (c == LOCATE) {
        if (c == LOCATE) {
              locate_mode = 0;
              locate_mode = 0;
              locate_string = CORD_EMPTY;
              locate_string = CORD_EMPTY;
              return;
              return;
        }
        }
        locate_string = CORD_cat_char(locate_string, (char)c);
        locate_string = CORD_cat_char(locate_string, (char)c);
        new_pos = CORD_str(current, file_pos - CORD_len(locate_string) + 1,
        new_pos = CORD_str(current, file_pos - CORD_len(locate_string) + 1,
                           locate_string);
                           locate_string);
        if (new_pos != CORD_NOT_FOUND) {
        if (new_pos != CORD_NOT_FOUND) {
            need_redisplay = ALL;
            need_redisplay = ALL;
            new_pos += CORD_len(locate_string);
            new_pos += CORD_len(locate_string);
            for (;;) {
            for (;;) {
                file_pos = line_pos(line + 1, 0);
                file_pos = line_pos(line + 1, 0);
                if (file_pos > new_pos) break;
                if (file_pos > new_pos) break;
                line++;
                line++;
            }
            }
            col = new_pos - line_pos(line, 0);
            col = new_pos - line_pos(line, 0);
            file_pos = new_pos;
            file_pos = new_pos;
            fix_cursor();
            fix_cursor();
        } else {
        } else {
            locate_string = CORD_substr(locate_string, 0,
            locate_string = CORD_substr(locate_string, 0,
                                        CORD_len(locate_string) - 1);
                                        CORD_len(locate_string) - 1);
            beep();
            beep();
        }
        }
        return;
        return;
    }
    }
    if (c == REPEAT) {
    if (c == REPEAT) {
        repeat_count = BARE_PREFIX; return;
        repeat_count = BARE_PREFIX; return;
    } else if (c < 0x100 && isdigit(c)){
    } else if (c < 0x100 && isdigit(c)){
        if (repeat_count == BARE_PREFIX) {
        if (repeat_count == BARE_PREFIX) {
          repeat_count = c - '0'; return;
          repeat_count = c - '0'; return;
        } else if (repeat_count != NO_PREFIX) {
        } else if (repeat_count != NO_PREFIX) {
          repeat_count = 10 * repeat_count + c - '0'; return;
          repeat_count = 10 * repeat_count + c - '0'; return;
        }
        }
    }
    }
    if (repeat_count == NO_PREFIX) repeat_count = 1;
    if (repeat_count == NO_PREFIX) repeat_count = 1;
    if (repeat_count == BARE_PREFIX && (c == UP || c == DOWN)) {
    if (repeat_count == BARE_PREFIX && (c == UP || c == DOWN)) {
        repeat_count = LINES - dis_granularity;
        repeat_count = LINES - dis_granularity;
    }
    }
    if (repeat_count == BARE_PREFIX) repeat_count = 8;
    if (repeat_count == BARE_PREFIX) repeat_count = 8;
    need_fix_pos = 0;
    need_fix_pos = 0;
    for (i = 0; i < repeat_count; i++) {
    for (i = 0; i < repeat_count; i++) {
        switch(c) {
        switch(c) {
          case LOCATE:
          case LOCATE:
            locate_mode = 1;
            locate_mode = 1;
            break;
            break;
          case TOP:
          case TOP:
            line = col = file_pos = 0;
            line = col = file_pos = 0;
            break;
            break;
          case UP:
          case UP:
            if (line != 0) {
            if (line != 0) {
                line--;
                line--;
                need_fix_pos = 1;
                need_fix_pos = 1;
            }
            }
            break;
            break;
          case DOWN:
          case DOWN:
            line++;
            line++;
            need_fix_pos = 1;
            need_fix_pos = 1;
            break;
            break;
          case LEFT:
          case LEFT:
            if (col != 0) {
            if (col != 0) {
                col--; file_pos--;
                col--; file_pos--;
            }
            }
            break;
            break;
          case RIGHT:
          case RIGHT:
            if (CORD_fetch(current, file_pos) == '\n') break;
            if (CORD_fetch(current, file_pos) == '\n') break;
            col++; file_pos++;
            col++; file_pos++;
            break;
            break;
          case UNDO:
          case UNDO:
            del_hist();
            del_hist();
            need_redisplay = ALL; need_fix_pos = 1;
            need_redisplay = ALL; need_fix_pos = 1;
            break;
            break;
          case BS:
          case BS:
            if (col == 0) {
            if (col == 0) {
                beep();
                beep();
                break;
                break;
            }
            }
            col--; file_pos--;
            col--; file_pos--;
            /* fall through: */
            /* fall through: */
          case DEL:
          case DEL:
            if (file_pos == current_len-1) break;
            if (file_pos == current_len-1) break;
                /* Can't delete trailing newline */
                /* Can't delete trailing newline */
            if (CORD_fetch(current, file_pos) == '\n') {
            if (CORD_fetch(current, file_pos) == '\n') {
                need_redisplay = ALL; need_fix_pos = 1;
                need_redisplay = ALL; need_fix_pos = 1;
            } else {
            } else {
                need_redisplay = line - dis_line;
                need_redisplay = line - dis_line;
            }
            }
            add_hist(CORD_cat(
            add_hist(CORD_cat(
                        CORD_substr(current, 0, file_pos),
                        CORD_substr(current, 0, file_pos),
                        CORD_substr(current, file_pos+1, current_len)));
                        CORD_substr(current, file_pos+1, current_len)));
            invalidate_map(line);
            invalidate_map(line);
            break;
            break;
          case WRITE:
          case WRITE:
            {
            {
                CORD name = CORD_cat(CORD_from_char_star(arg_file_name),
                CORD name = CORD_cat(CORD_from_char_star(arg_file_name),
                                     ".new");
                                     ".new");
 
 
                if ((out = fopen(CORD_to_const_char_star(name), "wb")) == NULL
                if ((out = fopen(CORD_to_const_char_star(name), "wb")) == NULL
                    || CORD_put(current, out) == EOF) {
                    || CORD_put(current, out) == EOF) {
                    de_error("Write failed\n");
                    de_error("Write failed\n");
                    need_redisplay = ALL;
                    need_redisplay = ALL;
                } else {
                } else {
                    fclose(out);
                    fclose(out);
                }
                }
            }
            }
            break;
            break;
          default:
          default:
            {
            {
                CORD left_part = CORD_substr(current, 0, file_pos);
                CORD left_part = CORD_substr(current, 0, file_pos);
                CORD right_part = CORD_substr(current, file_pos, current_len);
                CORD right_part = CORD_substr(current, file_pos, current_len);
 
 
                add_hist(CORD_cat(CORD_cat_char(left_part, (char)c),
                add_hist(CORD_cat(CORD_cat_char(left_part, (char)c),
                                  right_part));
                                  right_part));
                invalidate_map(line);
                invalidate_map(line);
                if (c == '\n') {
                if (c == '\n') {
                    col = 0; line++; file_pos++;
                    col = 0; line++; file_pos++;
                    need_redisplay = ALL;
                    need_redisplay = ALL;
                } else {
                } else {
                    col++; file_pos++;
                    col++; file_pos++;
                    need_redisplay = line - dis_line;
                    need_redisplay = line - dis_line;
                }
                }
                break;
                break;
            }
            }
        }
        }
    }
    }
    if (need_fix_pos) fix_pos();
    if (need_fix_pos) fix_pos();
    fix_cursor();
    fix_cursor();
    repeat_count = NO_PREFIX;
    repeat_count = NO_PREFIX;
}
}
 
 
/* OS independent initialization */
/* OS independent initialization */
 
 
void generic_init(void)
void generic_init(void)
{
{
    FILE * f;
    FILE * f;
    CORD initial;
    CORD initial;
 
 
    if ((f = fopen(arg_file_name, "rb")) == NULL) {
    if ((f = fopen(arg_file_name, "rb")) == NULL) {
        initial = "\n";
        initial = "\n";
    } else {
    } else {
        initial = CORD_from_file(f);
        initial = CORD_from_file(f);
        if (initial == CORD_EMPTY
        if (initial == CORD_EMPTY
            || CORD_fetch(initial, CORD_len(initial)-1) != '\n') {
            || CORD_fetch(initial, CORD_len(initial)-1) != '\n') {
            initial = CORD_cat(initial, "\n");
            initial = CORD_cat(initial, "\n");
        }
        }
    }
    }
    add_map(0,0);
    add_map(0,0);
    add_hist(initial);
    add_hist(initial);
    now -> map = current_map;
    now -> map = current_map;
    now -> previous = now;  /* Can't back up further: beginning of the world */
    now -> previous = now;  /* Can't back up further: beginning of the world */
    need_redisplay = ALL;
    need_redisplay = ALL;
    fix_cursor();
    fix_cursor();
}
}
 
 
#ifndef WIN32
#ifndef WIN32
 
 
main(argc, argv)
main(argc, argv)
int argc;
int argc;
char ** argv;
char ** argv;
{
{
    int c;
    int c;
 
 
#if defined(MACINTOSH)
#if defined(MACINTOSH)
        console_options.title = "\pDumb Editor";
        console_options.title = "\pDumb Editor";
        cshow(stdout);
        cshow(stdout);
        argc = ccommand(&argv);
        argc = ccommand(&argv);
#endif
#endif
    GC_INIT();
    GC_INIT();
 
 
    if (argc != 2) goto usage;
    if (argc != 2) goto usage;
    arg_file_name = argv[1];
    arg_file_name = argv[1];
    setvbuf(stdout, GC_MALLOC_ATOMIC(8192), _IOFBF, 8192);
    setvbuf(stdout, GC_MALLOC_ATOMIC(8192), _IOFBF, 8192);
    initscr();
    initscr();
    noecho(); nonl(); cbreak();
    noecho(); nonl(); cbreak();
    generic_init();
    generic_init();
    while ((c = getchar()) != QUIT) {
    while ((c = getchar()) != QUIT) {
                if (c == EOF) break;
                if (c == EOF) break;
            do_command(c);
            do_command(c);
    }
    }
done:
done:
    move(LINES-1, 0);
    move(LINES-1, 0);
    clrtoeol();
    clrtoeol();
    refresh();
    refresh();
    nl();
    nl();
    echo();
    echo();
    endwin();
    endwin();
    exit(0);
    exit(0);
usage:
usage:
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    fprintf(stderr, "Usage: %s file\n", argv[0]);
    fprintf(stderr, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n");
    fprintf(stderr, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n");
    fprintf(stderr, "Undo: ^U    Write to <file>.new: ^W");
    fprintf(stderr, "Undo: ^U    Write to <file>.new: ^W");
    fprintf(stderr, "Quit:^D  Repeat count: ^R[n]\n");
    fprintf(stderr, "Quit:^D  Repeat count: ^R[n]\n");
    fprintf(stderr, "Top: ^T   Locate (search, find): ^L text ^L\n");
    fprintf(stderr, "Top: ^T   Locate (search, find): ^L text ^L\n");
    exit(1);
    exit(1);
}
}
 
 
#endif  /* !WIN32 */
#endif  /* !WIN32 */
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.