-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.c
executable file
·661 lines (593 loc) · 14.9 KB
/
editor.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
/* My own vi-like editor for windows*/
/* Date: 07 Oct 2007 */
/* Author: Srinivas Nayak, Pradipta Kumar Satapathy */
/* Compile using Turbo C++ */
/* This code is distributed under the GPL License. */
/* For more info check: */
/* http://www.gnu.org/copyleft/gpl.html */
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<stdlib.h>
#include<string.h>
#include<dos.h>
#define TOTAL_DISPLAY_LINE_NUMBER 24
#define TOPMOST_DISPLAY_LINE_NUMBER 0
#define BOTTOMMOST_DISPLAY_LINE_NUMBER 23
#define TOTAL_DISPLAY_COLUMN_NUMBER 80
#define LEFTMOST_DISPLAY_COLUMN_NUMBER 0
#define RIGHTMOST_DISPLAY_COLUMN_NUMBER 79
#define NEWLINE 0xFF
#define TAB 0xEE
#define PADDING 0xDD
#define SPACE 0x20
/******************************************************/
struct line_node
{
int display_line_num;
int text_length;
char text[TOTAL_DISPLAY_COLUMN_NUMBER];//one extra for '\n' character.
struct line_node * prev;
struct line_node * next;
};
typedef struct line_node node;
enum
{
ESCAPE_MODE,
INSERT_MODE,
COLON_MODE
};
struct position
{
int row;
int col;
};
/********** GLOBAL VARIABLE DECLARATIONS **************/
FILE *sourcefile;//file pointers to source files.
char sourcefilename[20]; //array to hold source file name.
char statusbuffer[1][20]; //this holds the things
//that are displayed in the bottommost status line of editor
node * start_display_node;
node * current_node;
node *head= NULL, *tail = NULL;
int editor_mode = ESCAPE_MODE;
//***************END OF GLOBAL DECLARATIONS************************
//******************FUNCTION DECLARATIONS********************************************
// when the editor screen either first loaded or pressing escape in all the modes, the user will
// do all the operations in escape mode. e.g: deleting characters, deleting the current line..etc
void edit_in_escape_mode();
// After pressing the character 'i' in the escape mode will take into the insert mode. In this mode
// user can edit as many chacters into the file.
void edit_in_insert_mode();
// When pressing ESC followed by character ':' in the editor will take the editor into colon mode.
// In colon mode, user can save ('w') or quit (q) the file.
void edit_in_colon_mode();
// This function will be useful only in the escape mode. This function will be responsible for delete
// the current line. This functionality can be done by pressing 'dd'
void delete_current_line();
// This function will be useful only in the escape mode. This function will be responsible for delete
// the current line. This functionality can be done by pressing 'x'
void delete_current_character();
node * create_node();
void parse_from_file();
void editor_initialize();
void flush_on_screen();
void show_editor();
void show_cursor();
void reverse_parse_into_file();
void parse_from_file();
void save_file();
void insert(int,char *);
void delete_node();
void move_displayed_data_one_line_down();
void move_cursor_up();
void move_displayed_data_one_line_up();
void move_cursor_down();
void move_cursor_left();
void move_cursor_right();
void quit_file();
void stringcopy(char *, char *);
void create_empty_node();
// Each line is terminated with a CARRIAGE RETURN character,
// so we need to calculate how many characters in a node.
int get_string_length();
//****************END OF FUNCTION DECLARATIONS ******************************
int main(int argc,char *argv[])
{
int flag=1;
clrscr(); //before doing anything, clear the screen.
//***************file open operations**************
if(argc==2)// if user provides a source file name...
{
strncpy(sourcefilename,argv[1],19); //max sourcefilename assumed is 19.
// try to open that file to read and update.
sourcefile=fopen(sourcefilename,"r+");
if(sourcefile==NULL)// if we can't open it...
{
// try to create a file with the given name
sourcefile=fopen(sourcefilename,"w");
if(sourcefile==NULL)// if we couldn't create that file,
{
printf("disk full?"); //report user about the failure.
exit(0);
}
}
fclose(sourcefile);
}
else // if user desn't provide a file name or provide more file names,
{
printf(" provide a file name(only one)");
exit(0);
}
//*************end of file open operations**************
show_editor();
while(flag)
edit_in_escape_mode();
return 0;
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void show_editor()
{
parse_from_file();
editor_initialize();
flush_on_screen();
show_cursor();
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void save_into_file()
{
reverse_parse_into_file();
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void parse_from_file()
{
char text[80];
char ch;
int i = 0,j;
sourcefile=fopen(sourcefilename,"r");
ch = fgetc(sourcefile);
while(ch != EOF)
{
if(ch=='\t')
{
j = i + 7;
for(;i<=j;i++)
{
text[i]=TAB;
}
}
else if(ch == '\n')
{
text[i] = NEWLINE;
}
else
{
text[i] = ch;
}
i++;
if(i == 80 || ch == (char) NEWLINE)
{
insert(-1,text);
i = 0;
}
ch=fgetc(sourcefile);
}
if(i < 80)
{
text[i] = PADDING;
insert(-1,text);
}
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void editor_initialize()
{
start_display_node = current_node = head;
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void reverse_parse_into_file()
{
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void show_cursor()
{
// write this at last
}
/***************************************************
Function Name: edit_in_escape_mode
parameters:None
Description: In below cases, the editor will be in Escape mode.
1. When the editor will open for the first time, it will go into this mode.
2. Suppose editor is in the insert mode, pressing 'ESC' character will take the editing mode
to Escape mode.
Functionality: Below functions can be done in escape mode.
1. Pressing 'dd' will delete a line.
2. Pressing 'x' will delete the current character. (if there is any)
3. Pressing 'h','k','j','l' will move the cursor left,up,down and right respectively.
****************************************************/
void edit_in_escape_mode()
{
char ch;
editor_mode = ESCAPE_MODE;
ch = getch();
switch(ch)
{
case 'i':
edit_in_insert_mode();
break;
case ':':
edit_in_colon_mode();
break;
case 'd':
if(getch() == 'd')
{
delete_current_line();
flush_on_screen();
}
else
{
//invalid option..
}
break;
case 'x':
delete_current_character();
flush_on_screen();
break;
case 'k':
//up arrow
/*if(cursor_position.y == TOPMOST_DISPLAY_LINE_NUMBER)
move_displayed_data_one_line_down();
else
move_cursor_up();*/
break;
case 'j':
//down arrow
/*if(cursor_position.y == BOTTOMMOST_DISPLAY_LINE_NUMBER)
move_displayed_data_one_line_up();
else
move_cursor_down();*/
break;
case 'h':
//left arrow
move_cursor_left();
break;
case 'l':
//right arrow
move_cursor_right();
break;
}
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void edit_in_insert_mode()
{
char ch;
editor_mode = INSERT_MODE;
ch = getch();
while(ch!=27)//ESC
{
//insert
}
edit_in_escape_mode();
}
/***************************************************
Function Name: edit_in_colon_mode
parameters:None
Description: In colon mode,pressing the 'w', 'q' preceeded by a colon and followed by ENTER, will lead to
save and quit (without saving) the file respectively.
****************************************************/
void edit_in_colon_mode()
{
char ch;
editor_mode = COLON_MODE;
ch = getch();
switch(ch)
{
case 'w':
if(getch()==13)//ENTER or carriage return
{
save_file();//save
}
break;
case 'q':
if(getch()==13)//ENTER or carriage return
{
quit_file();//without save
}
break;
}
edit_in_escape_mode();
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void move_cursor_up()
{
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void move_cursor_down()
{
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void move_cursor_left()
{
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void move_cursor_right()
{
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
node * create_node(char *text)
{
node *new_node = (node*) malloc (sizeof(node));
if(new_node != NULL)
{
stringcopy(new_node->text,text);
new_node -> next = NULL;
new_node -> prev = NULL;
new_node -> text_length = get_string_length(text);
}
return new_node;
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void stringcopy(char* a, char* b)
{
int i=0;
while(i<=79)
{
a[i]=b[i];
i++;
}
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void insert(int insert_pos,char * text )
{
node *new_node,*ptr;
new_node = create_node(text);
if(head == NULL)
{
head = new_node;
tail = new_node;
}
else
{
if(insert_pos == -1)
{
new_node -> prev = tail;
tail -> next = new_node;
tail = new_node;
}
else
{
if(current_node == tail)
{
new_node -> prev = tail;
tail -> next = new_node;
tail = new_node;
}
else
{
new_node -> next = current_node -> next;
(current_node -> next) -> prev = new_node;
new_node -> prev = current_node;
current_node -> next = new_node;
}
}
}
}
/***************************************************
Function Name: delete_current_line
parameters:None
Description: It deletes the current line (provided the the line contains some data).e.g: suppose the cursor is @14 line number, pressing 'dd'
at line@14 will delete that perticular line.
****************************************************/
void delete_current_line()
{
node * temp = current_node -> next;
node * temp_start_display_node = start_display_node -> next;
delete_node();
if(current_node == head && current_node == tail)
{
// while deleting the last line of a file we need to create a
// empty node and assign the current_node and start_display_node as head.
create_empty_node();
current_node = start_display_node = head;
}
else if(current_node == head)
{
start_display_node = current_node = head;
}
else if(current_node == tail)
{
// start display line will not be changed..
// because we are not touching this when we are deleting the last line.
current_node = tail;
}
else
{
start_display_node = temp_start_display_node;
current_node = temp;
}
}
/***************************************************
Function Name: delete_current_character
parameters:None
Description: It deletes the current character which is under the cursor presently.
(provided that character is a valid data)
e.g: suppose the cursor is at 14th line and it is in 3rd character, pressing 'x'
will delte the 3rd character.
****************************************************/
void delete_current_character()
{
/*int i=cursor_pos.x;
while(i<=79 && current_node->text[i+1]!=NEWLINE)
{
current_node->text[i]=current_node->text[i+1];
}*/
}
/***************************************************
Function Name: delete_node
parameters:
Description:
****************************************************/
void delete_node()
{
node *temp;
if(current_node == head && current_node == tail)
{
head = tail = NULL;
free(current_node);
}
else if(current_node == head)
{
head = current_node -> next;
head -> prev = NULL;
free (current_node);
}
else if(current_node == tail)
{
tail = current_node -> prev;
tail -> next = NULL;
free(current_node);
}
else
{
(current_node -> prev) -> next = current_node -> next;
(current_node -> next) -> prev = current_node -> prev;
free (current_node);
}
}
/*************************************************
Function Name:
parameters:
Description:
*************************************************/
void create_empty_node()
{
char text[80];
text[0] = PADDING;
insert(-1,text);
}
/***************************************************
Function Name:
parameters:
Description:
****************************************************/
void flush_on_screen()
{
int i,j,k,p;
node *temp = start_display_node;
char far *videomemory = (char far*)0xB8000000;
clrscr();
for(j=TOPMOST_DISPLAY_LINE_NUMBER;
(j<=BOTTOMMOST_DISPLAY_LINE_NUMBER && temp != NULL);
j++,temp = temp -> next)
{
for(i = LEFTMOST_DISPLAY_COLUMN_NUMBER,p = 0;
(i <= RIGHTMOST_DISPLAY_COLUMN_NUMBER);
i++,p = p+2)
{
// Start FLUSHING the node text in to the video buffer here.
if(temp->text[i]==(char)TAB)
{
k = i + 7;
for(;p<=k;p+=2)
{
*(videomemory+(j*160+p))= SPACE;
}
}
else if(temp->text[i] == (char)NEWLINE
|| temp->text[i] == (char)PADDING)
{
for(;p<=79;p = p+2)
*(videomemory+(j*160+p))= SPACE;
break;
}
else
{
*(videomemory+(j*160+p))= temp->text[i];
}
}
}
}
/***************************************************
Function Name:
parameters:
Description:
***************************************************/
void quit_file()
{
exit(0);
}
/***************************************************
Function Name:
parameters:
Description:
***************************************************/
void save_file()
{
}
int get_string_length(char *string)
{
int i=0;
while(*string != (char)NEW_LINE || *string != (char)PADDING)
{
i++;
string++;
if(i == 80)
break;
}
return i;
}