/*
 * 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 
 */

/*
 * One (and only 1) of the following defines should be uncommented. Most of
 * the code is pretty machine-independent. Machine dependent code goes in a
 * file like tos.c or unix.c. The only other place where machine dependent
 * code goes is term.h for escape sequences. 
 */

#ifndef AMIGA
/* #define	ATARI		Atari ST */
/* #define	UNIX		System V */
#define	BSD			BSD 4.3 */
/* #define	OS2		Microsoft OS/2 */
/* #define	AMIGA		Amiga */
#endif

/*
 * If ATARI is defined, one of the following compilers must be selected. 
 */
#ifdef	ATARI
#define	MEGAMAX			Megamax Compiler */
/* #define	ALCYON		Alcyon C compiler */
#endif

/*
 * If HELP is defined, the :help command shows a vi command summary. 
 */
#define	HELP			/* enable help command */

#include <stdio.h>
#ifndef ATARI
# ifndef UNIX
#  ifndef OS2
#   include <stdlib.h>
#  endif
# endif
#endif
#include <ctype.h>
#include <string.h>
#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 FORWARD  4
#define BACKWARD 5

/*
 * 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 malloc 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. 
 */
#define	LINEINC	10

#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 char    *Filename;
extern LPTR    *Filemem;
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 *malloc(), *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();

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

/* 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 opencmd();
void   fileinfo(), inschar(), insstr(), delline();
bool_t delchar();
int    cntllines(), plines();
LPTR  *gotoline();

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

/* normal.c */
void   normal();
void   ResetBuffers();
void   AppendToRedobuff();
void   AppendNumberToRedobuff();
void   AppendToUndobuff();
void   AppendNumberToUndobuff();
void   AppendPositionToUndobuff();
void   AppendToUndoUndobuff();
void   AppendNumberToUndoUndobuff();
void   AppendPositionToUndoUndobuff();
char  *mkstr();

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

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

/* search.c */
void   dosearch();
void   searchagain();
void   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
