-
Notifications
You must be signed in to change notification settings - Fork 0
/
ee.c
5341 lines (4965 loc) · 120 KB
/
ee.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
| ee (easy editor)
|
| An easy to use, simple screen oriented editor.
|
| written by Hugh Mahon
|
|
| Copyright (c) 2009, Hugh Mahon
| All rights reserved.
|
| Redistribution and use in source and binary forms, with or without
| modification, are permitted provided that the following conditions
| are met:
|
| * Redistributions of source code must retain the above copyright
| notice, this list of conditions and the following disclaimer.
| * Redistributions in binary form must reproduce the above
| copyright notice, this list of conditions and the following
| disclaimer in the documentation and/or other materials provided
| with the distribution.
|
| THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
| "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
| LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
| FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
| COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
| INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
| BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
| LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
| CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
| LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
| ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
| POSSIBILITY OF SUCH DAMAGE.
|
| -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
|
| This editor was purposely developed to be simple, both in
| interface and implementation. This editor was developed to
| address a specific audience: the user who is new to computers
| (especially UNIX).
|
| ee is not aimed at technical users; for that reason more
| complex features were intentionally left out. In addition,
| ee is intended to be compiled by people with little computer
| experience, which means that it needs to be small, relatively
| simple in implementation, and portable.
|
| This software and documentation contains
| proprietary information which is protected by
| copyright. All rights are reserved.
|
| $Header: /home/hugh/sources/old_ae/RCS/ee.c,v 1.104 2010/06/04 01:55:31 hugh Exp hugh $
|
*/
char *ee_copyright_message =
"Copyright (c) 1986, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 2009 Hugh Mahon ";
#include "ee_version.h"
char *version = "@(#) ee, version " EE_VERSION " $Revision: 1.104 $";
#ifdef NCURSE
#include "new_curse.h"
#elif HAS_NCURSES
#include <ncurses.h>
#else
#include <curses.h>
#endif
#include <ctype.h>
#include <signal.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <errno.h>
#include <string.h>
#include <pwd.h>
#include <locale.h>
#ifdef HAS_SYS_WAIT
#include <sys/wait.h>
#endif
#ifdef HAS_STDLIB
#include <stdlib.h>
#endif
#ifdef HAS_STDARG
#include <stdarg.h>
#endif
#ifdef HAS_UNISTD
#include <unistd.h>
#endif
#ifndef NO_CATGETS
#include <nl_types.h>
nl_catd catalog;
#else
#define catgetlocal(a, b) (b)
#endif /* NO_CATGETS */
#ifndef SIGCHLD
#define SIGCHLD SIGCLD
#endif
enum {
TAB = 9
};
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
/*
| defines for type of data to show in info window
*/
enum {
CONTROL_KEYS = 1,
COMMANDS = 2
};
struct text {
unsigned char *line; /* line of characters */
int line_number; /* line number */
int line_length; /* actual number of characters in the line */
int max_length; /* maximum number of characters the line handles */
struct text *next_line; /* next line of text */
struct text *prev_line; /* previous line of text */
};
struct text *first_line; /* first line of current buffer */
struct text *dlt_line; /* structure for info on deleted line */
struct text *curr_line; /* current line cursor is on */
struct text *tmp_line; /* temporary line pointer */
struct text *srch_line; /* temporary pointer for search routine */
struct files { /* structure to store names of files to be edited*/
unsigned char *name; /* name of file */
struct files *next_name;
};
struct files *top_of_stack = NULL;
int d_wrd_len; /* length of deleted word */
int position; /* offset in bytes from begin of line */
int scr_pos; /* horizontal position */
int scr_vert; /* vertical position on screen */
int scr_horz; /* horizontal position on screen */
int absolute_lin; /* number of lines from top */
int tmp_vert, tmp_horz;
int input_file; /* indicate to read input file */
int recv_file; /* indicate reading a file */
int edit; /* continue executing while true */
int gold; /* 'gold' function key pressed */
int fildes; /* file descriptor */
int case_sen; /* case sensitive search flag */
int last_line; /* last line for text display */
int last_col; /* last column for text display */
int horiz_offset = 0; /* offset from left edge of text */
int clear_com_win; /* flag to indicate com_win needs clearing */
int text_changes = FALSE; /* indicate changes have been made to text */
int get_fd; /* file descriptor for reading a file */
int info_window = TRUE; /* flag to indicate if help window visible */
int info_type = CONTROL_KEYS; /* flag to indicate type of info to display */
int expand_tabs = TRUE; /* flag for expanding tabs */
int right_margin = 0; /* the right margin */
int observ_margins = TRUE; /* flag for whether margins are observed */
int shell_fork;
int temp_stdin; /* temporary storage for stdin */
int temp_stdout; /* temp storage for stdout descriptor */
int temp_stderr; /* temp storage for stderr descriptor */
int pipe_out[2]; /* pipe file desc for output */
int pipe_in[2]; /* pipe file descriptors for input */
int out_pipe; /* flag that info is piped out */
int in_pipe; /* flag that info is piped in */
int formatted = FALSE; /* flag indicating paragraph formatted */
int auto_format = FALSE; /* flag for auto_format mode */
int restricted = FALSE; /* flag to indicate restricted mode */
int nohighlight = FALSE; /* turns off highlighting */
int eightbit = TRUE; /* eight bit character flag */
int local_LINES = 0; /* copy of LINES, to detect when win resizes */
int local_COLS = 0; /* copy of COLS, to detect when win resizes */
int curses_initialized = FALSE; /* flag indicating if curses has been started*/
int emacs_keys_mode = FALSE; /* mode for if emacs key binings are used */
int ee_chinese = FALSE; /* allows handling of multi-byte characters */
/* by checking for high bit in a byte the */
/* code recognizes a two-byte character */
/* sequence */
unsigned char *point; /* points to current position in line */
unsigned char *srch_str; /* pointer for search string */
unsigned char *u_srch_str; /* pointer to non-case sensitive search */
unsigned char *srch_1; /* pointer to start of suspect string */
unsigned char *srch_2; /* pointer to next character of string */
unsigned char *srch_3;
unsigned char *in_file_name = NULL; /* name of input file */
char *tmp_file; /* temporary file name */
unsigned char *d_char; /* deleted character */
unsigned char *d_word; /* deleted word */
unsigned char *d_line; /* deleted line */
char in_string[513]; /* buffer for reading a file */
unsigned char *print_command = (unsigned char *)"lpr"; /* string to use for the print command */
unsigned char *start_at_line = NULL; /* move to this line at start of session*/
int in; /* input character */
FILE *temp_fp; /* temporary file pointer */
FILE *bit_bucket; /* file pointer to /dev/null */
char *table[] = {
"^@", "^A", "^B", "^C", "^D", "^E", "^F", "^G", "^H", "\t", "^J",
"^K", "^L", "^M", "^N", "^O", "^P", "^Q", "^R", "^S", "^T", "^U",
"^V", "^W", "^X", "^Y", "^Z", "^[", "^\\", "^]", "^^", "^_"
};
WINDOW *com_win;
WINDOW *text_win;
WINDOW *help_win;
WINDOW *info_win;
/*
| The following structure allows menu items to be flexibly declared.
| The first item is the string describing the selection, the second
| is the address of the procedure to call when the item is selected,
| and the third is the argument for the procedure.
|
| For those systems with i18n, the string should be accompanied by a
| catalog number. The 'int *' should be replaced with 'void *' on
| systems with that type.
|
| The first menu item will be the title of the menu, with NULL
| parameters for the procedure and argument, followed by the menu items.
|
| If the procedure value is NULL, the menu item is displayed, but no
| procedure is called when the item is selected. The number of the
| item will be returned. If the third (argument) parameter is -1, no
| argument is given to the procedure when it is called.
*/
struct menu_entries {
char *item_string;
int (*procedure)(struct menu_entries *);
struct menu_entries *ptr_argument;
int (*iprocedure)(int);
void (*nprocedure)(void);
int argument;
};
unsigned char *resiz_line(int factor, struct text *rline, int rpos);
void insert(int character);
void delete(int disp);
void scanline(unsigned char *pos);
int tabshift(int temp_int);
int out_char(WINDOW *window, int character, int column);
int len_char(int character, int column);
void draw_line(int vertical, int horiz, unsigned char *ptr, int t_pos, int length);
void insert_line(int disp);
struct text *txtalloc(void);
struct files *name_alloc(void);
unsigned char *next_word(unsigned char *string);
void prev_word(void);
void control(void);
void emacs_control(void);
void bottom(void);
void top(void);
void nextline(void);
void prevline(void);
void left(int disp);
void right(int disp);
void find_pos(void);
void up(void);
void down(void);
void function_key(void);
void print_buffer(void);
void command_prompt(void);
void command(char *cmd_str1);
int scan(char *line, int offset, int column);
char *get_string(char *prompt, int advance);
int compare(char *string1, char *string2, int sensitive);
void goto_line(char *cmd_str);
void midscreen(int line, unsigned char *pnt);
void get_options(int numargs, char *arguments[]);
void check_fp(void);
void get_file(char *file_name);
void get_line(int length, unsigned char *in_string, int *append);
void draw_screen(void);
void finish(void);
int quit(int noverify);
void edit_abort(int arg);
void delete_text(void);
int write_file(char *file_name, int warn_if_exists);
int search(int display_message);
void search_prompt(void);
void del_char(void);
void undel_char(void);
void del_word(void);
void undel_word(void);
void del_line(void);
void undel_line(void);
void adv_word(void);
void move_rel(int direction, int lines);
void eol(void);
void bol(void);
void adv_line(void);
void sh_command(char *string);
void set_up_term(void);
void resize_check(void);
int menu_op(struct menu_entries *);
void paint_menu(struct menu_entries menu_list[], int max_width, int max_height, int list_size, int top_offset, WINDOW *menu_win, int off_start, int vert_size);
void help(void);
void paint_info_win(void);
void no_info_window(void);
void create_info_window(void);
int file_op(int arg);
void shell_op(void);
void leave_op(void);
void redraw(void);
int Blank_Line(struct text *test_line);
void Format(void);
void ee_init(void);
void dump_ee_conf(void);
void echo_string(char *string);
void spell_op(void);
void ispell_op(void);
int first_word_len(struct text *test_line);
void Auto_Format(void);
void modes_op(void);
char *is_in_string(char *string, char *substring);
char *resolve_name(char *name);
int restrict_mode(void);
int unique_test(char *string, char *list[]);
void strings_init(void);
#undef P_
/*
| allocate space here for the strings that will be in the menu
*/
struct menu_entries modes_menu[] = {
{"", NULL, NULL, NULL, NULL, 0}, /* title */
{"", NULL, NULL, NULL, NULL, -1}, /* 1. tabs to spaces */
{"", NULL, NULL, NULL, NULL, -1}, /* 2. case sensitive search*/
{"", NULL, NULL, NULL, NULL, -1}, /* 3. margins observed */
{"", NULL, NULL, NULL, NULL, -1}, /* 4. auto-paragraph */
{"", NULL, NULL, NULL, NULL, -1}, /* 5. eightbit characters*/
{"", NULL, NULL, NULL, NULL, -1}, /* 6. info window */
{"", NULL, NULL, NULL, NULL, -1}, /* 7. emacs key bindings*/
{"", NULL, NULL, NULL, NULL, -1}, /* 8. right margin */
{"", NULL, NULL, NULL, NULL, -1}, /* 9. chinese text */
{"", NULL, NULL, NULL, dump_ee_conf, -1}, /* 10. save editor config */
{NULL, NULL, NULL, NULL, NULL, -1} /* terminator */
};
char *mode_strings[11];
#define NUM_MODES_ITEMS 10
struct menu_entries config_dump_menu[] = {
{"", NULL, NULL, NULL, NULL, 0},
{"", NULL, NULL, NULL, NULL, -1},
{"", NULL, NULL, NULL, NULL, -1},
{NULL, NULL, NULL, NULL, NULL, -1}
};
struct menu_entries leave_menu[] = {
{"", NULL, NULL, NULL, NULL, -1},
{"", NULL, NULL, NULL, finish, -1},
{"", NULL, NULL, quit, NULL, TRUE},
{NULL, NULL, NULL, NULL, NULL, -1}
};
#define READ_FILE 1
#define WRITE_FILE 2
#define SAVE_FILE 3
struct menu_entries file_menu[] = {
{"", NULL, NULL, NULL, NULL, -1},
{"", NULL, NULL, file_op, NULL, READ_FILE},
{"", NULL, NULL, file_op, NULL, WRITE_FILE},
{"", NULL, NULL, file_op, NULL, SAVE_FILE},
{"", NULL, NULL, NULL, print_buffer, -1},
{NULL, NULL, NULL, NULL, NULL, -1}
};
struct menu_entries search_menu[] = {
{"", NULL, NULL, NULL, NULL, 0},
{"", NULL, NULL, NULL, search_prompt, -1},
{"", NULL, NULL, search, NULL, TRUE},
{NULL, NULL, NULL, NULL, NULL, -1}
};
struct menu_entries spell_menu[] = {
{"", NULL, NULL, NULL, NULL, -1},
{"", NULL, NULL, NULL, spell_op, -1},
{"", NULL, NULL, NULL, ispell_op, -1},
{NULL, NULL, NULL, NULL, NULL, -1}
};
struct menu_entries misc_menu[] = {
{"", NULL, NULL, NULL, NULL, -1},
{"", NULL, NULL, NULL, Format, -1},
{"", NULL, NULL, NULL, shell_op, -1},
{"", menu_op, spell_menu, NULL, NULL, -1},
{NULL, NULL, NULL, NULL, NULL, -1}
};
struct menu_entries main_menu[] = {
{"", NULL, NULL, NULL, NULL, -1},
{"", NULL, NULL, NULL, leave_op, -1},
{"", NULL, NULL, NULL, help, -1},
{"", menu_op, file_menu, NULL, NULL, -1},
{"", NULL, NULL, NULL, redraw, -1},
{"", NULL, NULL, NULL, modes_op, -1},
{"", menu_op, search_menu, NULL, NULL, -1},
{"", menu_op, misc_menu, NULL, NULL, -1},
{NULL, NULL, NULL, NULL, NULL, -1}
};
char *help_text[23];
char *control_keys[5];
char *emacs_help_text[22];
char *emacs_control_keys[5];
char *command_strings[5];
char *commands[32];
char *init_strings[22];
#define MENU_WARN 1
#define max_alpha_char 36
/*
| Declarations for strings for localization
*/
char *com_win_message; /* to be shown in com_win if no info window */
char *no_file_string;
char *ascii_code_str;
char *printer_msg_str;
char *command_str;
char *file_write_prompt_str;
char *file_read_prompt_str;
char *char_str;
char *unkn_cmd_str;
char *non_unique_cmd_msg;
char *line_num_str;
char *line_len_str;
char *current_file_str;
char *usage0;
char *usage1;
char *usage2;
char *usage3;
char *usage4;
char *file_is_dir_msg;
char *new_file_msg;
char *cant_open_msg;
char *open_file_msg;
char *file_read_fin_msg;
char *reading_file_msg;
char *read_only_msg;
char *file_read_lines_msg;
char *save_file_name_prompt;
char *file_not_saved_msg;
char *changes_made_prompt;
char *yes_char;
char *file_exists_prompt;
char *create_file_fail_msg;
char *writing_file_msg;
char *file_written_msg;
char *searching_msg;
char *str_not_found_msg;
char *search_prompt_str;
char *exec_err_msg;
char *continue_msg;
char *menu_cancel_msg;
char *menu_size_err_msg;
char *press_any_key_msg;
char *shell_prompt;
char *formatting_msg;
char *shell_echo_msg;
char *spell_in_prog_msg;
char *margin_prompt;
char *restricted_msg;
char *ON;
char *OFF;
char *HELP;
char *WRITE;
char *READ;
char *LINE;
char *FILE_str;
char *CHARACTER;
char *REDRAW;
char *RESEQUENCE;
char *AUTHOR;
char *VERSION;
char *CASE;
char *NOCASE;
char *EXPAND;
char *NOEXPAND;
char *Exit_string;
char *QUIT_string;
char *INFO;
char *NOINFO;
char *MARGINS;
char *NOMARGINS;
char *AUTOFORMAT;
char *NOAUTOFORMAT;
char *Echo;
char *PRINTCOMMAND;
char *RIGHTMARGIN;
char *HIGHLIGHT;
char *NOHIGHLIGHT;
char *EIGHTBIT;
char *NOEIGHTBIT;
char *EMACS_string;
char *NOEMACS_string;
char *conf_dump_err_msg;
char *conf_dump_success_msg;
char *conf_not_saved_msg;
char *ree_no_file_msg;
char *cancel_string;
char *menu_too_lrg_msg;
char *more_above_str, *more_below_str;
char *separator = "===============================================================================";
char *chinese_cmd, *nochinese_cmd;
#ifndef __STDC__
#ifndef HAS_STDLIB
extern char *malloc();
extern char *realloc();
extern char *getenv();
FILE *fopen(); /* declaration for open function */
#endif /* HAS_STDLIB */
#endif /* __STDC__ */
/* beginning of main program */
int
main(int argc, char *argv[])
{
int counter;
for (counter = 1; counter < 24; counter++)
signal(counter, SIG_IGN);
/* Always read from (and write to) a terminal. */
if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO)) {
fprintf(stderr,
"ee's standard input and output must be a terminal\n");
exit(1);
}
signal(SIGCHLD, SIG_DFL);
signal(SIGSEGV, SIG_DFL);
signal(SIGINT, edit_abort);
d_char = malloc(3); /* provide a buffer for multi-byte chars */
d_word = malloc(150);
*d_word = '\0';
d_line = NULL;
dlt_line = txtalloc();
dlt_line->line = d_line;
dlt_line->line_length = 0;
curr_line = first_line = txtalloc();
curr_line->line = point = malloc(10);
curr_line->line_length = 1;
curr_line->max_length = 10;
curr_line->prev_line = NULL;
curr_line->next_line = NULL;
curr_line->line_number = 1;
srch_str = NULL;
u_srch_str = NULL;
position = 1;
scr_pos =0;
scr_vert = 0;
scr_horz = 0;
absolute_lin = 1;
bit_bucket = fopen("/dev/null", "w");
edit = TRUE;
gold = case_sen = FALSE;
shell_fork = TRUE;
strings_init();
ee_init();
if (argc > 0 )
get_options(argc, argv);
set_up_term();
if (right_margin == 0)
right_margin = COLS - 1;
if (top_of_stack == NULL)
{
if (restrict_mode())
{
wmove(com_win, 0, 0);
werase(com_win);
wprintw(com_win, "%s", ree_no_file_msg);
wrefresh(com_win);
edit_abort(0);
}
wprintw(com_win, "%s", no_file_string);
wrefresh(com_win);
}
else
check_fp();
clear_com_win = TRUE;
counter = 0;
while(edit)
{
/*
| display line and column information
*/
if (info_window)
{
if (!nohighlight)
wstandout(info_win);
wmove(info_win, 5, 0);
wprintw(info_win, "%s", separator);
wmove(info_win, 5, 5);
wprintw(info_win, "line %d col %d lines from top %d ",
curr_line->line_number, scr_horz, absolute_lin);
wstandend(info_win);
wrefresh(info_win);
}
wrefresh(text_win);
in = wgetch(text_win);
if (in == -1)
exit(0); /* without this exit ee will go into an
infinite loop if the network
session detaches */
resize_check();
if (clear_com_win)
{
clear_com_win = FALSE;
wmove(com_win, 0, 0);
werase(com_win);
if (!info_window)
{
wprintw(com_win, "%s", com_win_message);
}
wrefresh(com_win);
}
if (in > 255)
function_key();
else if ((in == '\10') || (in == 127))
{
in = 8; /* make sure key is set to backspace */
delete(TRUE);
}
else if ((in > 31) || (in == 9))
insert(in);
else if ((in >= 0) && (in <= 31))
{
if (emacs_keys_mode)
emacs_control();
else
control();
}
}
return(0);
}
/* resize the line to length + factor*/
unsigned char *
resiz_line(int factor, struct text *rline, int rpos)
{
unsigned char *rpoint;
int resiz_var;
rline->max_length += factor;
rpoint = rline->line = realloc(rline->line, rline->max_length );
for (resiz_var = 1 ; (resiz_var < rpos) ; resiz_var++)
rpoint++;
return(rpoint);
}
/* insert character into line */
void
insert(int character)
{
int counter;
int value;
unsigned char *temp; /* temporary pointer */
unsigned char *temp2; /* temporary pointer */
if ((character == '\011') && (expand_tabs))
{
counter = len_char('\011', scr_horz);
for (; counter > 0; counter--)
insert(' ');
if (auto_format)
Auto_Format();
return;
}
text_changes = TRUE;
if ((curr_line->max_length - curr_line->line_length) < 5)
point = resiz_line(10, curr_line, position);
curr_line->line_length++;
temp = point;
counter = position;
while (counter < curr_line->line_length) /* find end of line */
{
counter++;
temp++;
}
temp++; /* increase length of line by one */
while (point < temp)
{
temp2=temp - 1;
*temp= *temp2; /* shift characters over by one */
temp--;
}
*point = character; /* insert new character */
wclrtoeol(text_win);
if (!isprint((unsigned char)character)) /* check for TAB character*/
{
scr_pos = scr_horz += out_char(text_win, character, scr_horz);
point++;
position++;
}
else
{
waddch(text_win, (unsigned char)character);
scr_pos = ++scr_horz;
point++;
position ++;
}
if ((observ_margins) && (right_margin < scr_pos))
{
counter = position;
while (scr_pos > right_margin)
prev_word();
if (scr_pos == 0)
{
while (position < counter)
right(TRUE);
}
else
{
counter -= position;
insert_line(TRUE);
for (value = 0; value < counter; value++)
right(TRUE);
}
}
if ((scr_horz - horiz_offset) > last_col)
{
horiz_offset += 8;
midscreen(scr_vert, point);
}
if ((auto_format) && (character == ' ') && (!formatted))
Auto_Format();
else if ((character != ' ') && (character != '\t'))
formatted = FALSE;
draw_line(scr_vert, scr_horz, point, position, curr_line->line_length);
}
/* delete character */
void
delete(int disp)
{
unsigned char *tp;
unsigned char *temp2;
struct text *temp_buff;
int temp_vert;
int temp_pos;
int del_width = 1;
if (point != curr_line->line) /* if not at beginning of line */
{
text_changes = TRUE;
temp2 = tp = point;
if ((ee_chinese) && (position >= 2) && (*(point - 2) > 127))
{
del_width = 2;
}
tp -= del_width;
point -= del_width;
position -= del_width;
temp_pos = position;
curr_line->line_length -= del_width;
if ((*tp < ' ') || (*tp >= 127)) /* check for TAB */
scanline(tp);
else
scr_horz -= del_width;
scr_pos = scr_horz;
if (in == 8)
{
if (del_width == 1)
*d_char = *point; /* save deleted character */
else
{
d_char[0] = *point;
d_char[1] = *(point + 1);
}
d_char[del_width] = '\0';
}
while (temp_pos <= curr_line->line_length)
{
temp_pos++;
*tp = *temp2;
tp++;
temp2++;
}
if ((scr_horz < horiz_offset) && (horiz_offset > 0))
{
horiz_offset -= 8;
midscreen(scr_vert, point);
}
}
else if (curr_line->prev_line != NULL)
{
text_changes = TRUE;
left(disp); /* go to previous line */
temp_buff = curr_line->next_line;
point = resiz_line(temp_buff->line_length, curr_line, position);
if (temp_buff->next_line != NULL)
temp_buff->next_line->prev_line = curr_line;
curr_line->next_line = temp_buff->next_line;
temp2 = temp_buff->line;
if (in == 8)
{
d_char[0] = '\n';
d_char[1] = '\0';
}
tp = point;
temp_pos = 1;
while (temp_pos < temp_buff->line_length)
{
curr_line->line_length++;
temp_pos++;
*tp = *temp2;
tp++;
temp2++;
}
*tp = '\0';
free(temp_buff->line);
free(temp_buff);
temp_buff = curr_line;
temp_vert = scr_vert;
scr_pos = scr_horz;
if (scr_vert < last_line)
{
wmove(text_win, scr_vert + 1, 0);
wdeleteln(text_win);
}
while ((temp_buff != NULL) && (temp_vert < last_line))
{
temp_buff = temp_buff->next_line;
temp_vert++;
}
if ((temp_vert == last_line) && (temp_buff != NULL))
{
tp = temp_buff->line;
wmove(text_win, last_line,0);
wclrtobot(text_win);
draw_line(last_line, 0, tp, 1, temp_buff->line_length);
wmove(text_win, scr_vert, (scr_horz - horiz_offset));
}
}
draw_line(scr_vert, scr_horz, point, position, curr_line->line_length);
formatted = FALSE;
}
/* find the proper horizontal position for the pointer */
void
scanline(unsigned char *pos)
{
int temp;
unsigned char *ptr;
ptr = curr_line->line;
temp = 0;
while (ptr < pos)
{
if (*ptr <= 8)
temp += 2;
else if (*ptr == 9)
temp += tabshift(temp);
else if ((*ptr >= 10) && (*ptr <= 31))
temp += 2;
else if ((*ptr >= 32) && (*ptr < 127))
temp++;
else if (*ptr == 127)
temp += 2;
else if (!eightbit)
temp += 5;
else
temp++;
ptr++;
}
scr_horz = temp;
if ((scr_horz - horiz_offset) > last_col)
{
horiz_offset = (scr_horz - (scr_horz % 8)) - (COLS - 8);
midscreen(scr_vert, point);
}
else if (scr_horz < horiz_offset)
{
horiz_offset = max(0, (scr_horz - (scr_horz % 8)));
midscreen(scr_vert, point);
}
}
/* give the number of spaces to shift */
int
tabshift(int temp_int)
{
int leftover;
leftover = ((temp_int + 1) % 8);
if (leftover == 0)
return (1);
else
return (9 - leftover);
}
/* output non-printing character */
int
out_char(WINDOW *window, int character, int column)
{
int i1, i2;
char *string;
char string2[8];
if (character == TAB)
{
i1 = tabshift(column);
for (i2 = 0;
(i2 < i1) && (((column+i2+1)-horiz_offset) < last_col); i2++)
{
waddch(window, ' ');
}
return(i1);
}
else if ((character >= '\0') && (character < ' '))
{
string = table[(int) character];
}
else if ((character < 0) || (character >= 127))
{
if (character == 127)
string = "^?";
else if (!eightbit)
{
sprintf(string2, "<%d>", (character < 0) ? (character + 256) : character);
string = string2;
}
else
{
waddch(window, (unsigned char)character );
return(1);
}
}
else
{
waddch(window, (unsigned char)character);
return(1);
}
for (i2 = 0; (string[i2] != '\0') && (((column+i2+1)-horiz_offset) < last_col); i2++)
waddch(window, (unsigned char)string[i2]);
return(strlen(string));
}
/* return the length of the character */
int
len_char(int character, int column)
{
int length;
if (character == '\t')
length = tabshift(column);
else if ((character >= 0) && (character < 32))
length = 2;
else if ((character >= 32) && (character <= 126))
length = 1;
else if (character == 127)
length = 2;
else if (((character > 126) || (character < 0)) && (!eightbit))
length = 5;
else
length = 1;
return(length);
}
/* redraw line from current position */
void