/*
 * STEVIE - Simply Try this Editor for VI Enthusiasts
 *
 * Code Contributions By : Tim Thompson           twitch!tjt
 *                         Tony Andrews           onecom!wldrdg!tony 
 *                         G. R. (Fred) Walter    watmath!watcgl!grwalter 
 */

#include "env.h"

#include <stdio.h>
#ifndef ATARI
# ifndef UNIX
#   include <stdlib.h>
# endif
#endif
#include <ctype.h>
#ifndef MWC
# include <string.h>
#endif
#include "ascii.h"
#include "keymap.h"
#include "param.h"
#include "term.h"
#include "macros.h"

#ifdef AMIGA
/*
 * This won't be needed if you have a version of Lattice 4.01 without broken
 * break signal handling.
 */
#include <signal.h>
#endif

extern char    *strchr();

#define NORMAL			 0
#define CMDLINE			 1
#define INSERT			 2
#define APPEND			 3
#define UNDO			 4
#define REDO			 5
#define PUT			 6
#define FORWARD			 7
#define BACKWARD		 8
#define VALID			 9
#define NOT_VALID		10
#define VALID_TO_CURSCHAR	11

/*
 * Boolean type definition and constants 
 */
typedef int     bool_t;

#ifndef	TRUE
#define	FALSE	(0)
#define	TRUE	(1)
#endif
#define	SORTOF	(2)
#define YES      TRUE
#define NO       FALSE
#define MAYBE    SORTOF

/*
 * Maximum screen dimensions
 */
#define MAX_COLUMNS 140L

/*
 * Buffer sizes
 */
#define CMDBUFFSIZE MAX_COLUMNS	/* size of the command processing buffer */

#define LSIZE        512	/* max. size of a line in the tags file */

#define IOSIZE     (1024+1)	/* file i/o and sprintf buffer size */

#define YANKSIZE    5200	/* yank buffer size */
#define INSERT_SIZE 5300	/* insert, redo and undo buffer size must be
				 * bigger than YANKSIZE */
#define REDO_UNDO_SIZE 5400	/* redo, undo and (undo an undo) buffer size
				 * must be bigger than INSERT_SIZE */
#define READSIZE    5500	/* read buffer size must be bigger than
				 * YANKSIZE and REDO_UNDO_SIZE */

/*
 * SLOP is the amount of extra space we get for text on a line during editing
 * operations that need more space. This keeps us from calling alloc every
 * time we get a character during insert mode. No extra space is allocated
 * when the file is initially read. 
 */
#define	SLOP	10

/*
 * LINEINC is the gap we leave between the artificial line numbers. This
 * helps to avoid renumbering all the lines every time a new line is
 * inserted. 
 *
 * Since line numbers are stored in longs (32 bits), a LINEINC of 10000
 * lets us have > 200,000 lines and we won't have to renumber very often.
 */
#define	LINEINC	10000

#define CHANGED   { Changed = TRUE; }
#define UNCHANGED { Changed = FALSE; }

struct line {
    struct line    *next;	/* next line */
    struct line    *prev;	/* previous line */
    char           *s;		/* text for this line */
    int             size;	/* actual size of space at 's' */
    unsigned long   num;	/* line "number" */
};

#define	LINEOF(x)	((x)->linep->num)

struct lptr {
    struct line    *linep;	/* line we're referencing */
    int             index;	/* position within that line */
};

typedef struct line LINE;
typedef struct lptr LPtr;

struct charinfo {
    char            ch_size;
    char           *ch_str;
};

extern struct charinfo chars[];

extern int      State;
extern int      Rows;
extern int      Columns;
extern char    *Realscreen;
extern char    *Nextscreen;
extern int      NumLineSizes;
extern LINE   **LinePointers;
extern char    *LineSizes;
extern char    *Filename;
extern LPtr    *Filemem;
extern LPtr    *Filetop;
extern LPtr    *Fileend;
extern LPtr    *Topchar;
extern LPtr    *Botchar;
extern LPtr    *Curschar;
extern LPtr    *Insstart;
extern int      Cursrow, Curscol, Cursvcol, Curswant;
extern bool_t   set_want_col;
extern int      Prenum;
extern bool_t   Changed;
extern bool_t   RedrawingDisabled;
extern bool_t   MustRedrawLine;
extern bool_t   MustRedrawScreen;
extern bool_t   UndoInProgress;
extern bool_t   Binary;
extern char    *IObuff;
extern char    *Insbuffptr;
extern char    *Insbuff;
extern char    *Readbuffptr;
extern char    *Readbuff;
extern char    *Redobuffptr;
extern char    *Redobuff;
extern char    *Undobuffptr;
extern char    *Undobuff;
extern char    *UndoUndobuffptr;
extern char    *UndoUndobuff;
extern char    *Yankbuffptr;
extern char    *Yankbuff;
extern char     last_command;
extern char     last_command_char;

extern char    *strcpy();

/* alloc.c */
char  *alloc();
char  *strsave();
void   screenalloc(), filealloc(), freeall();
LINE  *newline();
bool_t canincrease();

/* cmdline.c */
void   readcmdline();
void   dotag();
void   msg(), emsg(), smsg();
void   gotocmdline();
void   wait_return();

/* dec.c */
int    dec();

/* edit.c */
void   edit(), insertchar(), getout(), scrollup(), scrolldown(), beginline();
bool_t oneright(), oneleft(), oneup(), onedown();

/* fileio.c */
void   filemess(), renum();
bool_t readfile(), writeit();

/* updateNs.c */
void   updateNextscreen();

/* updateRs.c */
void   updateRealscreen();

/* help.c */
bool_t help();

/* inc.c */
int    inc();

/* linefunc.c */
LPtr   *nextline(), *prevline(), *coladvance();

/* main.c */
void   stuffReadbuff();
void   stuffnumReadbuff();
char   vgetc();
char   vpeekc();

/* mark.c */
void   setpcmark(), clrall(), clrmark();
bool_t setmark();
LPtr  *getmark();

/* misccmds.c */
bool_t OpenForward();
bool_t OpenBackward();
void   fileinfo(), inschar(), insstr(), delline();
bool_t delchar();
int    cntllines(), plines();
LPtr  *gotoline();

/* normal.c */
void   normal();
void   ResetBuffers();
void   AppendToInsbuff();
void   AppendToRedobuff();
void   AppendNumberToRedobuff();
void   AppendToUndobuff();
void   AppendNumberToUndobuff();
void   AppendPositionToUndobuff();
void   AppendToUndoUndobuff();
void   AppendNumberToUndoUndobuff();
void   AppendPositionToUndoUndobuff();
bool_t linewhite();

/* mk.c */
char  *mkstr();
char  *mkline();

/* param.c */
void   doset();

/* screen.c */
void   nexttoscreen();
void   updateline();
void   redrawline();
void   screenclear();
void   cursupdate();
void   prt_line();
void   s_del();
void   s_ins();

/* search.c */
void   doglob();
void   dosub();
void   searchagain();
bool_t dosearch();
bool_t repsearch();
bool_t searchc(), crepsearch(), findfunc();
LPtr  *showmatch();
LPtr  *fwd_word(), *bck_word(), *end_word();

/*
 * Machine-dependent routines. 
 */
#ifdef AMIGA
# include "amiga.h"
#endif
#ifdef BSD
# include "bsd.h"
#endif
#ifdef UNIX
# include "unix.h"
#endif
#ifdef TOS
# include "tos.h"
#endif
#ifdef OS2
# include "os2.h"
#endif
#ifdef DOS
# include "dos.h"
#endif
