#include<stdio.h> 
#include<string.h>
 
/* 




                 VV                   VV    EEEEEEEEEEEEEE   
                  VV                 VV     EE  
                   VV               VV      EE
                    VV             VV       EE
                     VV           VV        EE
                      VV         VV         EEEEEEEEEEEEEE
                       VV       VV          EE
                        VV     VV           EE
                         VV   VV            EE
                          VV VV             EE
                           VVV              EE
                            V               EEEEEEEEEEEEEE
                                                       
 
 
 
 
                       MODULE  : vedbase.c
 
                       VERSION : v3.5
 
                       REVISED : 12 Sep 2005
 
                       AUTHOR  : Michael A. Campbell
 
 
 
 
 
 


                          FUNCTIONAL DESCRIPTION
                          ----------------------
 
               This module contains the data base for the VE editor.
 
               This data base contains all of the variables that are
 
               shared across the editor, and the edit command table.
 

               WARNING:  THE VE COMMAND TABLE CONTAINED IN THIS FILE

                         MUST AGREE IN CONTENT AND ORDER WITH THE 

                         TABLE "COMMANDS' IN MODULE KBDIF.C, AND   

                         WITH THE COMMAND INDICES IN ../DEF/CMDIX.DEF





*/

/* .page */

/*****************
 * INCLUDE FILES *
 *****************/

#include <curses.h>

#include "../def/common.def"
#include "../def/cmdix.def"
#include "../def/undo.def"

#include "../str/vestruct.str"
#include "../str/blockutil.str"
#include "../str/stacks.str"

#include "../inc/termif.inc"
#include "../inc/kbdif.inc"

#include "../ext/blockcmd.ext"
#include "../ext/columncmd.ext"
#include "../ext/cursor.ext"
#include "../ext/delete.ext"
#include "../ext/editcmd.ext"
#include "../ext/findcmd.ext"
#include "../ext/jumpcmd.ext"
#include "../ext/macrocmd.ext"
#include "../ext/misccmd.ext"
#include "../ext/modeproc.ext"
#include "../ext/optcmd.ext"
#include "../ext/quitcmd.ext"
#include "../ext/wordcmd.ext"
#include "../ext/undo.ext"

/* .page */

/***********************
 * DATA BASE VARIABLES *
 ***********************/

char ve_masthead []  =  "MacOSX VE v3.5g"; /* must end with version num     */
                        /* ve_init uses last 4 chars to mark options block  */

BOOL ve_initializing;   /* Signal to various parts that it is still init    */
BOOL init_help_shown;   /* The initial help message is showing              */

WINDOW *ve_win;         /* ncurses window token for ve                      */
char   divider_char;    /* The character used to build the divider line     */

int ve_mode;            /* Mode of the editor: either INSERT or OVERWRITE   */
int last_ve_mode;       /* Mode of ve before this key was entered           */
int displayed_mode;     /* Mode displayed on status line                    */

int ve_exception;       /* Exception from current operation                 */
char *ve_msg;           /* Message to print when ve_exception = E_OK        */
char ve_msg_buf[82];    /* Buffer to use to construct dynamic ve_msg's      */

int MODE_X;             /* X coordinate for VE mode display                 */
int MODE_Y;             /* Y coordinate for VE mode display                 */            

int ERR_X;              /* X coordinate for error message displays          */
int ERR_Y;              /* Y coordinate for error message displays          */

int MSG_X;              /* X coordinate for ve_msg message displays         */
int MSG_Y;              /* Y coordinate for ve_msg message displays         */

int FN_FIELD_WIDTH;     /* Width of the filename display on status line     */

int rs_reps_left;       /* Repititions of state machine for read_char       */
int rs_ix;              /* Index within current read state macro            */
int read_state;         /* State of read_char state machine                 */

int undo_state;         /* Undo state - monitoring changes or undoing them  */

int rep_ix;             /* Index into the ASCII repitition buffer           */
int requested_reps;     /* The number of reps requested                     */
int curr_rep;           /* This repitition count                            */
int ext_cmd;            /* Is this an extended command sequence?            */

BOOL in_again_cmd_set[NUM_CMDS]; /* Is a cmd_idx in the set of again cmds?  */
BOOL undo_supported[NUM_CMDS];   /* Is undo supported for this command?     */

struct stack_type status_stack;   /* Status line stack                      */

char curr_cmd;          /* The currently entered command key                */
char last_cmd;          /* The last executed command key                    */
int  last_cmd_ix;       /* Index of the last command in table commands[]    */
int  last_reps;         /* The repetition count for the last command        */

int status;             /* General purpose status indicator for calls       */

FILE *curr_conn;        /* Connection to the currently editing file         */
char curr_filename [256];  /* Same size as file descriptor filename         */


/* Editor control blocks for the current file and screen, and the current   */
/* option set.                                                              */

struct file_ctl   curr_file;
struct screen_ctl curr_screen;
struct option_ctl options;


/* Editor control blocks for the current block and line_del buffers.        */

struct file_ctl   line_block;
struct file_ctl   line_del_block;
struct node       *block_start, *block_end, *block [2];


/* Global line pointers                                                     */

int               curr_line_num;
struct node       *curr_line, *new_line;
struct node       *cnode,     *pnode,    *nnode;

/* Global line buffers                                                      */

struct node       line_buf;        /* Buffer for the current line           */
struct node       filename_buf;    /* A buffer for reading filenames with   */
struct node       temp;            /* A temporary buffer for any use        */


/* Global ASCII buffers for holding the repitition count, find string, and  */
/* substitute string buffers.                                               */

struct asc_buf    rep_buf;         /* Buffer to hold repetition counts      */
struct asc_buf    find_buf;        /* Strings to find                       */
struct asc_buf    subs_buf;        /* String to use in substitution         */


/* Global variables for multi-file support */

int curr_file_ix;                  /* Index of descriptor for current file  */
int files_open;                    /* Number of files currently open        */ 

struct file_ctl     open_file_list; /* List of descriptors for open files   */
struct file_des_str *file_des;      /* Pointer to a descriptor              */
struct file_des_str *curr_file_des; /* Pointer to current descriptor        */


/* Global variables for Macro command support */

struct macro_str    *curr_macro;
struct file_ctl     macro_list;

/* Some global messages */

char    *mode_msg [] = {
        "      INS",                /* Was "INSERT"      */
        "      OVR",                /* Was "OVERWRITE"   */
        "      CMD"};               /* Was "COMMAND"     */

char    *err_msg  [] = {
        "ESC-h for Help",
        "Unknown command",
        "Insufficient memory",
        "Could not open requested file",
        "Filename too large",
        "Linked list error",
        "    End of list",
        "No current file",
        "Not a text file (ENTER to continue)"      };

char    *prompt_msg [] = {
        "File: ",
        "Find (F10 to clear): ",
        "Replace (F10 to clear): "};

char    *invalid_msg          = "Invalid menu selection";
char    *default_ve_msg       = "ESC-h for Help                          ";
char    *enter_to_edit_msg    = "ENTER (return to editing)";

char    *select_file_open     = " - Select/Type Filename to Open";
char    *select_file_read     = " - Select/Type Filename to Read";
char    *select_file_write    = " - Select/Type Filename to Write";

char    *no_file_select_open  = "No File Selected to Open";
char    *no_file_select_read  = "No File Selected to Read";
char    *no_file_select_write = "No File Selected to Write";

/* .page */

/* VE command table: Procedures to implement all VE commands are called     */
/* from this table.  This table must agree in order and content with the    */
/* commands table in the file TERMCTL.C *and* the command indices as        */
/* defined in ../def/cmdix.def. Note that NUM_CMDS in ../def/common.def     */
/* must properly match the number of entries in this table.                 */


void (*cmd_proc[NUM_CMDS]) () = {
    &insert_cmd,                /* INSERT command               */
    &overwrite_cmd,             /* OVERWRITE command            */

    &null_cmd,                  /* AGAIN command                */
    &block_cmd,                 /* BLOCK command                */
    &delete_left_cmd,           /* DELETE LEFT command          */
    &edit_cmd,                  /* EDIT command                 */
    &find_cmd,                  /* FIND command                 */
    &goto_file_cmd,             /* GOTO FILE command            */
    &help_cmd,                  /* HELP command                 */
    &info_cmd,                  /* INFO command                 */
    &jump_cmd,                  /* JUMP command                 */
    &delete_line_cmd,           /* DELETE LINE command          */
    &macro_cmd,                 /* MACRO command                */
    &next_file_cmd,             /* NEXT FILE command            */
    &option_cmd,                /* OPTION command               */
    &get_cmd,                   /* GET command                  */
    &quit_cmd,                  /* QUIT command                 */
    &subs_cmd,                  /* SUBSTITUTE command           */
    &status_cmd,                /* STATUS command               */
    &tags_cmd,                  /* TAGS command                 */
    &update_cmd,                /* UPDATE command               */
    &view_cmd,                  /* VIEW command                 */
    &wrap_paragraph_cmd,        /* WRAP PARAGRAPH command       */
    &xecute_cmd,                /* XECUTE command               */
    &pick_cmd,                  /* PICK command                 */

    &cursor_left_cmd,           /* cursor left                  */
    &cursor_right_cmd,          /* cursor right                 */
    &cursor_up_cmd,             /* cursor up                    */
    &cursor_down_cmd,           /* cursor down                  */
    &extended_motion_cmd,       /* HOME command                 */
    
    &word_right_cmd,            /* WORD RIGHT command           */
    &word_left_cmd,             /* WORD LEFT command            */
    &para_up_cmd,               /* PARAGRAPH UP command         */
    &para_down_cmd,             /* PARAGRAPH DOWN command       */
    
    &rubout_cmd,                /* RUBOUT command               */

    &delete_char_cmd,           /* DELETE CHAR command          */
    &delete_word_cmd,           /* DELETE WORD command          */
    &ext_home_cmd,              /* Start of Line command        */
    &ext_end_cmd,               /* End of Line command          */
    &ext_pgup_cmd,              /* Page Up command              */
    &top_of_file_cmd,           /* Top of File command          */
    &ext_pgdn_cmd,              /* Page Dn command              */
    &bottom_of_file_cmd,        /* Bottom of File command       */   

    &half_up_cmd,               /* Scroll half screen up         */
    &half_down_cmd,             /* Scroll half screen down       */
    &half_left_cmd,             /* Scroll half screen left       */
    &half_right_cmd,            /* Scroll half screen right      */

    &copy_line_del_buffer_cmd,  /* Copy Line Del Buffer command  */
    &cut_line_del_buffer_cmd,   /* Cut Line Del Buffer command   */
    &paste_line_del_buffer_cmd, /* Paste Line Del Buffer command */

    &cursor_top_of_page_cmd,    /* Cursor Top Of Page command    */
    &cursor_middle_of_page_cmd, /* Cursor Middle Of Page command */
    &cursor_bottom_of_page_cmd, /* Cursor Bottom Of Page command */

    &jump_to_tag_cmd,           /* JumpToTag command             */

    &delete_right_cmd,          /* DELETE RIGHT command          */
    &edge_cmd,                  /* Edge command                  */
    &column_cmd,                /* Column command                */
    &undo_cmd,                  /* Undo command                  */

    &mouse_cmd,                 /* Mouse event handler           */
    &about_ve_cmd };            /* About VE command              */

/* .page */

/*********************
 * PUBLIC PROCEDURES *
 *********************/

void init_ve_globals ()

/***************************************************************************
 * This function inits many of the VE global variables                     *
 * It is called from ve_init in main.c                                     *
 ***************************************************************************/

begin
   register int i;             /* Loop counter                             */

    /* Clear important control blocks used by VE. This nasty bit of code    */
    /* makes the implicit assumption that a NULL pointer is represented by  */
    /* all zeros. Hence, the setb operations below set all pointers in the  */
    /* control blocks being operated on to NULL. If NULL is ever changed to */
    /* a value other than zero by the compiler/OS, LOTS of stuff will break.*/
    /* This assumption (NULL == 0) is made ALL over VE.                     */

    setb (0, &curr_file,      sizeof (curr_file));
    setb (0, &curr_screen,    sizeof (curr_screen));
    setb (0, &line_block,     sizeof (line_block));
    setb (0, &line_del_block, sizeof (line_del_block));
    setb (0, &open_file_list, sizeof (open_file_list));
    setb (0, &macro_list,     sizeof (macro_list));

    setb (0, &line_buf,       sizeof (line_buf));
    setb (0, &temp,           sizeof (temp));
    setb (0, &rep_buf,        sizeof (rep_buf));

    setb (0, &find_buf,       sizeof (rep_buf));
    setb (0, &subs_buf,       sizeof (rep_buf));

    ve_exception = E_OK;

    strcpy ( ve_msg_buf, " " );
    ve_msg = ve_msg_buf;

    divider_char = '-'; /* Set char used to build divider line             */

    /* Set up the default operating options                                */

    i = strlen (ve_masthead);      /* Example masthead: "Linux VE v3.4a"   */
                                   /* Defined earlier in this module       */

    options.major_release          = ve_masthead[i-4];
    options.minor_release          = ve_masthead[i-2];
    options.subrelease             = ve_masthead[i-1];

    options.tab_stop               = 4;
    options.input_tab_translation  = TRUE;
    options.output_tab_translation = FALSE;

    options.auto_indent            = TRUE;

    options.wrap_text              = FALSE;
    options.left_margin            = -1;
    options.right_margin           = 72;

    options.right_justify          = TRUE;

    options.bg_color               = get_default_bg_color ();
    options.fg_color               = get_default_fg_color ();

    options.sol_match_char         = '^'; /* For replace start of line     */
    options.eol_match_char         = '$'; /* For replace end of line       */

    options.mouse_enabled          = TRUE;

    /* If the file ~/.ve.opt exists, load the user's saved options from it */

    status = load_ve_opt_file ();

    /* Set up (multi) file variables */

    files_open   = 0;            /* No files open                          */
    curr_file_ix = -1;           /* No current file index as a result      */
    curr_conn    = NULL;         /* ...and no current file connection      */

    /* Set up the read_char state machine */

    rs_reps_left = 0;
    rs_ix        = 0;
    curr_macro   = NULL;
    read_state   = RS_TRANSPARENT;

    /* Set up command processing variables */

    last_cmd    = commands[0];  /* INSERT command                           */
    last_cmd_ix = insert_ix;
    last_reps   = 1;            /* No repetition                            */
    rep_ix      = 0;            /* No current repetition count chars        */
    ext_cmd     = FALSE;        /* Not an extended command sequence         */

    /* Init the in_again_cmd_set array and the undo_supported array.          */
    
    for ( i = 0; i < NUM_CMDS; i++ )
    begin
       in_again_cmd_set[i] = FALSE;
       undo_supported[i]   = TRUE;
    end

    /* The in_again_cmd_set array is the set of commands that               */
    /* can be executed by the Again command. Generally Again execution is   */
    /* reserved for commands that change the file, although an exception is */
    /* made for specific commands like Find.                                */

    in_again_cmd_set[block_ix         ] = TRUE;
    in_again_cmd_set[del_right_ix     ] = TRUE;
    in_again_cmd_set[find_ix          ] = TRUE;
    in_again_cmd_set[del_line_ix      ] = TRUE;
    in_again_cmd_set[get_ix           ] = TRUE;
    in_again_cmd_set[substitute_ix    ] = TRUE;
    in_again_cmd_set[wrap_para_ix     ] = TRUE;
    in_again_cmd_set[xecute_ix        ] = TRUE;
    in_again_cmd_set[rubout_ix        ] = TRUE;
    in_again_cmd_set[del_char_ix      ] = TRUE;
    in_again_cmd_set[del_word_ix      ] = TRUE;
    in_again_cmd_set[copy_line_del_ix ] = TRUE;
    in_again_cmd_set[cut_line_del_ix  ] = TRUE;
    in_again_cmd_set[paste_line_del_ix] = TRUE;
    in_again_cmd_set[column_ix        ] = TRUE;
    in_again_cmd_set[undo_ix          ] = TRUE;

    /* The undo_supported array records per command whether or not that     */
    /* command supports undo. A command is deemed to support undo if it     */
    /* either (a) doesn't change the file (no undo needed) or (b) an undo   */
    /* action is available in undo.c for the change that it made.           */
    /* Since undo support is almost universal across VE, the array is       */
    /* initialized to TRUE, and only exceptions are dealt with below.       */

    undo_supported[edit_ix     ] = FALSE;
    undo_supported[goto_file_ix] = FALSE;
    undo_supported[next_file_ix] = FALSE;

    /* Miscellaneous items */

    curr_line_num = 0;

end /* init_ve_globals */

