1 2 /* 3 edx/emx (EDitor for X), (C) 2002, Terry Loveall 4 released into the public domain. 5 Based upon the original work ee.c of Yijun Ding, copyright 1991 which is 6 in the public domain. 7 This program comes with no warranties or binaries. Use at your own risk. 8 */ 9 10 /* This is the core editor engine */ 11 12 #define MAXVLINE 450 13 #define EOL '\0' /* end of line marker */ 14 #define BLNK ' ' /* blank */ 15 #define LF '\n' /* new line */ 16 #define NLEN 256 /* input buffer length */ 17 #define LMAX 10000 /* max line length */ 18 #define XINC 20 /* increment for x offset */ 19 20 #define CHG 0 /* file Modified: =0 file not changed, !=0 file changed */ 21 #define FIL 1 /* Fill */ 22 #define OVR 2 /* character insert=0, Overwrite=1 */ 23 #define CAS 3 /* Case sensitive: =0 no, !=0 yes */ 24 #define TAB 4 /* Tab expand */ 25 #define BLK 5 /* block mark active */ 26 27 #define IND 6 /* auto indent flag */ 28 #define REC 7 /* recording macro flag */ 29 #define ALL 8 /* replace all flag */ 30 #define EDT 9 /* edit flag */ 31 #define SHW 10 /* update & show entire screen */ 32 #define NEW 11 /* editing a new file */ 33 #define WIN 12 /* window: <0 same win, =0 load new */ 34 35 /* buffer sizes */ 36 37 #define AMAX 0x4000 /* main buffer initial size */ 38 #define BMAX 0x1000 /* block initial size */ 39 #define UMAX 0x2000 /* undo level */ 40 #define YTOP 0 /* first line */ 41 42 #define VOID_LINK (unsigned int)-1 43 44 #ifdef GREEK 45 /* Greek letters */ 46 static char *greek_letter[26] = { 47 "alpha", "!beta", "Chi", "Delta", "!epsilon", "!Phi", 48 "Gamma", "eta", "iota", "Psi", "kappa", "Lambda", "mu", "nu", 49 "omega", "Pi", "!Theta", "!rho", "!Sigma", "tau", "Upsilon", NULL, 50 "Omega", "Xi", "Psi", "zeta" 51 }; 52 int var_greek = 0; /* variant of Greek letters */ 53 #endif 54 55 /* character handler types */ 56 static enum { 57 MAIN, 58 DIALOG, 59 OPTIONS 60 } executive = MAIN; /* default character handler */ 61 62 /* current window struct */ 63 typedef struct { 64 char name[NLEN]; 65 int jump; 66 } MWIN; /* my window structure */ 67 68 MWIN ewin; /* current window */ 69 70 /* undo record struct */ 71 typedef struct { 72 unsigned int link; 73 int pos; 74 int length; 75 char str[0]; 76 } U_REC; 77 78 int x=0, screen_width; /* screen position 1 <= x <= screen_width */ 79 int y=1, screen_height; /* screen size 0 <= y <= screen_height */ 80 int eolx; /* coord of eol */ 81 int update_scr = 1; /* do screen updates flag */ 82 int search_mode = 0; /* was it a replace request ? */ 83 unsigned int amax, bmax, umax; /* main buffer, block size, undo level */ 84 85 char *cfdpath; 86 char Command[256]; 87 88 char *bstart, *bend; /* marked block start, end and char pointer */ 89 #ifndef MINIMAL 90 char *binding[26]; 91 #endif 92 93 char bbuf[NLEN]; /* temp file name */ 94 char sbuf[NLEN], rbuf[NLEN]; /* search buffer, replace buffer */ 95 char *s; /* pointer to search string */ 96 int rlen, slen; /* replace and search lengths */ 97 98 char *edbuf, *file_end; /* edit buffer, end of file */ 99 char *bb, *mk, *last_mk = NULL; /* block buffer, mark */ 100 unsigned blen; /* length of text in block buffer */ 101 char *cur_pos; 102 char *line_start; /* current pos, start of current line */ 103 char *screen_start; /* global screen start */ 104 unsigned int xlo; /* x offset for left display edge */ 105 char *last_pos; /* last last position visited */ 106 char *old_pos; /* last position visited */ 107 108 int x_offset; /* offset of xtru from line_start */ 109 int xtru = 0, ytru = 0; /* file position */ 110 int ytot = 0; /* 0 <= ytru <= ytot */ 111 112 int yl1, yl2; /* 1st, 2nd line of window */ 113 int tabsize=4; /* tab size */ 114 int doCtrlC = 0; /* decode next char from ^C function */ 115 int doCtrlK = 0; /* decode next char from ^K function */ 116 int doCtrlQ = 0; /* decode next char from ^Q function */ 117 int doCtrlX = 0; /* decode next char from ^X function */ 118 int doEscap = 0; /* decode next char from Esc function */ 119 int help_done = 0; 120 int literal = 0; 121 122 int col; /* width of dialog preset (if any) */ 123 int diastart; /* start of dialog input field */ 124 int first; /* first dialog char flag, 0=true */ 125 int dblen; /* current dialog buffer size */ 126 char *diabuf; /* dialog buffer pointer */ 127 void (*dialogCB) (); /* callback pointer */ 128 129 static char lbuf[16]; /* goto line number buffer */ 130 static char cbuf[16]; /* goto column number buffer */ 131 static char wbuf[8]="78"; /* reformat block right margin */ 132 static char twbuf[8]="4"; /* tab width buffer */ 133 134 static char mbuf[256]; /* macro record buffer */ 135 static char *pmbuf=mbuf; /* record pointer for macro buffer */ 136 struct stat ofstat; /* edit file stat structure */ 137 138 FILE *fi, *fo, *fb; /* file handles for input, output, block */ 139 static char fbuf[256]; /* current file name buffer */ 140 141 char flag[WIN+1]="\0\0\0C\0\0N\0\0E\0"; /* options flag presets */ 142 char fsym[]="MFOCTBNRAE? "; /* Modified,Fill,Overwrite,Case,Tab,Block marked,autoiNdent,Rec,All */ 143 144 /* word delimiter chars */ 145 char wdelims[] = "\t ,;+-*=^&|?:`()[]{}<>\"\'"; 146 char sdelims[] = "\t >]})"; 147 char s1delims[] = "\t "; 148 149 int undo_wrap = 0; /* undo buffer wrap flag */ 150 void * unbuf; /* undo stack */ 151 U_REC* undop = NULL; /* active undo/redo pointer */ 152 U_REC* undosp = NULL; /* top of undo stack pointer */ 153 int in_undo; /* dont reset undop to undosp if true */ 154 int undone=1; /* have undone all */ 155 156 /* function prototypes */ 157 158 int get_tru(char* pos); 159 char* get_cur(); 160 char * realloc_buffer(); 161 void init_undo(); 162 U_REC* new_undo(char *pos, int len); 163 void u_del(char *pos, int len); 164 void u_ins(char *pos, int len); 165 void ytot_ins(); 166 void undo(); 167 void redo(); 168 void sys_exit(int code); 169 void move_to(int newx, int newy); 170 void getx(); 171 void cursor_up(), cursor_down(), cursor_left(), cursor_right(); 172 void cursor_pageup(); 173 void cursor_pagedown(); 174 void word_left(); 175 void word_right(); 176 void word_mark(); 177 void drstr(char* disp, int i); 178 void show_rest(int len, char *s); 179 void show_scr(int fr, int to); 180 void scroll_up(); 181 void scroll_down(); 182 void scr_update(); 183 void show_sdn(int line); 184 void show_flag(int x, int g); 185 void dialog(int key); 186 void show_note(char *prp); 187 int show_gets(char *prp, char *buf, int blen, void *cb); 188 void options(int key); 189 void show_pos(); 190 void show_top(), show_help(), show_mode(); 191 void top(); 192 char *file_new(); 193 void file_read(); 194 char *file_ltab(char *s); 195 int file_write(FILE *fp, char *s, char *e); 196 int file_fout(); 197 void do_save(); 198 void set_title(); 199 int SYSTEM (char *cmd); 200 void gorun(); 201 void new_edit(char *nfile); 202 void do_cd(); 203 void chgdir(); 204 void do_open(); 205 #ifndef MINIMAL 206 void launch_edit(); 207 void req_edit(); 208 #endif 209 void file_save(int f_all, int f_win); 210 void file_resize(char *s, char *d); 211 void goto_x(int xx), goto_y(int yy); 212 void goto_ptr(char *s); 213 void goline(); 214 void goto_last(); 215 void goto_line(), goto_col(); 216 void gocol(); 217 void goto_col(); 218 void switch_marks(); 219 void find_match(); 220 int str_cmp(char *s); 221 void gofindforward(); 222 void gofindbackward(); 223 char *goto_find(char *s, int back); 224 void get_mk(char* dbuf); 225 char *goto_search(int back); 226 void do_sar(); 227 void ask_replace(); 228 void goto_replace(); 229 void gettab(); 230 void tab_size(); 231 void getmarg(); 232 void window_size(); 233 void mark_off(); 234 void block_put(), block_get(), block_mark(); 235 void remove_block(); 236 void block_remove_update(); 237 void chg_case(int upper); 238 void transpose(); 239 void block_copy(int delete); 240 void block_paste(), block_write(), block_line(); 241 void doblockr(); 242 void doblockw(); 243 void block_read(); 244 int block_fill(); 245 void block_format(); 246 void key_return(), key_deleol(char *s), key_delete(); 247 void key_backspace(), key_delword(int eol); 248 void key_tab(int tabi); 249 void key_normal(int key); 250 void key_macros(int record); 251 #ifdef GREEK 252 void key_greek(int key); 253 #endif 254 void ctrlc_key(int key); 255 void ctrlk_key(int key); 256 void ctrlq_key(int key); 257 void key_alt(int key); 258 void key_control(int key); 259 void key_escape(int key); 260 void key_func(int key); 261 void key_binding(int key); 262 void main_exec(int key); 263