-
Notifications
You must be signed in to change notification settings - Fork 0
/
InsertMode.c
190 lines (160 loc) · 9.44 KB
/
InsertMode.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
// Krishna Dwypayan Kota
// 20172076
#include "editor2076.h"
//READ INPUT IN INSERT MODE
char readKeyInsertMode( int argc , char* argv[ ])
{
int readKey;
char ch = ' ';
struct winsize ws;
ioctl( STDOUT_FILENO , TIOCGWINSZ , &ws );
int row_num = curse.cursor_x;
int col_num = curse.cursor_y;
col_num = curse.cursor_y;
row_num = curse.cursor_x;
//OPENING THE FILE HERE
if( argc > 1 )
{
FILE* file = fopen( argv[ 1 ] , "a");
//row = NULL;
if( file == 0 )
{
printerror( "No such file exists " );
exit(0);
}
else
{
while( 1 )
{
readKey = read(STDIN_FILENO, &ch, 1);
if(readKey == -1 && errno != EAGAIN)
{
printerror("ERROR IN READING");
}
if( ch == 27 )
return ch;
//ENTER KEY PRESSED
if( ch == 13 )
{
if( curse.cursor_x != ws.ws_row )
{
curse.cursor_x++;
curse.cursor_y = 1;
row_num = curse.cursor_x ;
//INSERTION INTO THE LINKED LIST
rows[ row_num ] = Insert_in_LL(rows[ row_num ] , '\n');
//SYSTEM DISPLAY
screenClear_NormalMode();
append( config.terminal_rows - 2, 3 );
write( STDOUT_FILENO , "INSERT" , 6);
append( 1 , 1 );
print_window_buffer( row_init , row_last );
norm_curse.norm_x = row_last;
norm_curse.norm_y = 1;
append( config.terminal_rows - 2 , config.terminal_cols - 10 );
char* cursor_pos_g = printCoordinates( curse.cursor_x , curse.cursor_y );
write( STDOUT_FILENO , cursor_pos_g , 6 );
append( curse.cursor_x , curse.cursor_y );
// Insert_at_posX( rows[ row_num ] , '\r' , col_num );
// Insert_at_posX( rows[ row_num ] , '\n' , col_num );
//printCharLL( rows[ row_num ] );
row_num++;
fputc( '\n' , file );
}
else
{
curse.cursor_x = ws.ws_row;
}
char* cursor_pos_enter = append( config.terminal_rows -2 , config.terminal_cols - 10 );
write( STDOUT_FILENO , cursor_pos_enter , strlen( cursor_pos_enter ) );
write( STDOUT_FILENO , "\x1b[K" , 3 );
append( config.terminal_rows -2 , config.terminal_cols - 10 );
char* coordinates_enter = printCoordinates( curse.cursor_x , curse.cursor_y );
write( STDOUT_FILENO , coordinates_enter , strlen( coordinates_enter ) );
append( curse.cursor_x , curse.cursor_y );
}
//ESCAPE KEY
if( ch == 127 )
{
write( STDOUT_FILENO , "\b", 1 );
//write( STDOUT_FILENO , " " , 1 );
rows[ row_init + row_num - 1 ] = deleteAtPosX( rows[ row_init + row_num - 1 ] , norm_curse.norm_y ) ;
if( norm_curse.norm_y != 1)
norm_curse.norm_y--;
else
{
if( norm_curse.norm_x != 1)
{
norm_curse.norm_x --;
}
else
{
norm_curse.norm_x = 1;
}
}
for( int i = 1; i <= config.terminal_rows - 4; i++)
{
append( i , 1 );
write( STDOUT_FILENO, "\x1b[K", 3 );
}
append( 1 , 1 );
print_window_buffer( row_init , row_last );
//write( STDOUT_FILENO , "NORMAL" , 6);
append( config.terminal_rows - 2 , 11 );
write( STDOUT_FILENO, "\x1b[K", 3 );
//append( config.terminal_rows - 2 , config.terminal_cols - 10 );
char* cursor_pos_x = printCoordinates( norm_curse.norm_x , norm_curse.norm_y );
//append( config.terminal_rows - 2, 1 );
//write( STDOUT_FILENO, "\x1b[K", 3 );
append( config.terminal_rows -2 , config.terminal_cols - 10 );
write( STDOUT_FILENO , cursor_pos_x , strlen( cursor_pos_x ) );
append( norm_curse.norm_x , norm_curse.norm_y );
//append( 1 , 1 );
}
//INSERT THE CHAR INSIDE THE LINKED LIST
else
{
if( rows[ row_init + row_num - 1 ] == NULL )
{
rows[ row_init + row_num - 1 ] = Insert_in_LL( rows[ row_init + row_num - 1 ] , ch );
}
else
rows[ row_init + row_num - 1 ] = Insert_at_posX( rows[ row_init + row_num - 1 ] , ch , norm_curse.norm_y );
for( int i = 1; i <= config.terminal_rows - 4; i++)
{
append( i , 1 );
write( STDOUT_FILENO, "\x1b[K", 3 );
}
if( norm_curse.norm_y == config.terminal_cols )
{
write( STDOUT_FILENO , "\n\r" , 2 );
}
append( norm_curse.norm_x , norm_curse.norm_y );
write( STDOUT_FILENO, &ch, 1);
if( norm_curse.norm_y != ws.ws_col )
{
norm_curse.norm_y++;
}
else
{
norm_curse.norm_y = 1;
if( norm_curse.norm_x != ws.ws_row )
norm_curse.norm_x++;
}
append( config.terminal_rows - 2, 3 );
write( STDOUT_FILENO , "INSERT" , 6);
append( 1 , 1 );
print_window_buffer( row_init , row_last );
// norm_curse.norm_x = row_last;
// norm_curse.norm_y = 1;
append( config.terminal_rows - 2 , config.terminal_cols - 10 );
char* cursor_pos_g = printCoordinates( norm_curse.norm_x, norm_curse.norm_y );
write( STDOUT_FILENO , cursor_pos_g , 6 );
append( norm_curse.norm_x , norm_curse.norm_y );
}
}
}
fclose( file );
}
return ch;
}