-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditor.c
387 lines (371 loc) · 7.79 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
#include "list.h"
#include<stdio.h>
#include<string.h>
#include<ncurses.h>
#include<stdlib.h>
#include<ctype.h>
#include<stdbool.h>
#include<string.h>
typedef struct mousevents{
int y,x;
}mousevents;
void edit(PAGE *page,char *filename);
int main(){
printf("WELCOME TO HYPER TEXT EDITOR\nThe following depict the basic operations:\n1.Save Ctrl+S\n2:Newline Enter\n3:To enable/disable autocomplete press F2\n4:To see auto complete suggestions press F3\n5:Press up and down arrow keys to scroll up and down\n6:Press Tab for identation\n7:Use backspace to delete characters\nPRESS ANY CHARACTER TO CONTINUE\nNote:Text editor only works with the command prompt in full screen\n");
getch();
printf("Enter filename to open\n");
char str[40];
fgets(str,35,stdin);
str[strlen(str)-1]='\0';
int count=0;
FILE *fp=fopen(str,"r");
char textStr[50];
//fgets(textStr,199,fp);
/*for(int i=0;i<PAGE_SIZE;i++){
text[i]=(char*)malloc(sizeof(char)*40);
}*/
//printw("%s\n",str);
//refresh();
//printf("%s",str);
char ele='i';
int x=0,y=0;
PAGE page;
LINE *line=page_initialize(&page);
mousevents store_coor;
if(fp!=NULL){
initscr();
//char text[200];
count=-1;
while(fgets(textStr,199,fp)!=NULL){
getyx(stdscr,x,y);
if(line==NULL)
line=insert_line(&page,line,count+1,0);
else line=insert_line(&page,line,count+1,line->count-1);
for(int i=0;textStr[i]!='\0';i++){
if(textStr[i]=='\t'){
int j=4;
while(j>0){ insert_At_End(line,' ');j--;}
}
else{
insert_At_End(line,textStr[i]);
}
//printw("%c",textStr[i]);
//getyx(stdscr,y,x);
//move(y,x+1);
}
count++;
//getyx(stdscr,y,x);
//move(y+1,0);
//refresh();
}
display_page(&page,0,43);
refresh();
fclose(fp);
edit(&page,str);
}
else{
char choice;
printf("Do you want to create a new file?\n");
scanf("%c",&choice);
if(toupper(choice)=='Y'){
edit(&page,str);
}
}
endwin();
return 0;
}
void edit(PAGE *page,char *filename){
initscr();
scrollok(stdscr,TRUE);
keypad(stdscr,TRUE);
cbreak();
int ch;
int x=0,y=0;
int flag=1;
raw();
cbreak();
//keypad(stdscr,TRUE);
noecho();
//idlok(stdscr,true);
//scrollok(stdscr,true);
getyx(stdscr,x,y);
mousemask(ALL_MOUSE_EVENTS|REPORT_MOUSE_POSITION ,NULL);
MEVENT m_event;
int beg=0,end=43;
LINE *curr=page->first_line;
move(0,0);
auto_tree sequence;
int check=0;
autocomplete_initialize(&sequence); //Autocomplete
//char buffer[50];
//search_autocomplete(sequence.first,"i",0,buffer);
//getch();
//delete_auto(&sequence);
int counter_num_spaces=0;
int auto_complete_flag=0;
int is_keyword=0;
char word[50];
character* buffer[50];
char temp_keyword[7];
int key_pointer=0;
if(page->first_line==NULL)printw("Enter the enter key before you start typing");
while(1)
{
ch=getch();
if(page->first_line==NULL&&ch!=10)continue;
if(strcmp(keyname(ch),"^S")==0)break;
else if(ch=='`')
{
x+=1;
y=0;
if(curr->next!=NULL)curr=curr->next;
move(x,y);
}
else if(ch==KEY_UP)
{
getyx(stdscr,x,y);
//x+=1;
if(x>0){
x-=1;
if(curr->prev!=NULL){
curr=curr->prev;
move(x,y);
}
}
else{
if(curr->prev!=NULL){
curr=curr->prev;
beg-=1;
end-=1;
clear();
move(0,0);
display_page(page,beg,end);
move(0,0);
refresh();
}
}
}
else if(ch==KEY_DOWN)
{
getyx(stdscr,x,y);
if(x<42){
x+=1;
if(curr->next!=NULL){
curr=curr->next;
move(x,y);
}
}
else{
if(curr->next!=NULL){
curr=curr->next;
beg+=1;
end+=1;
clear();
move(0,0);
display_page(page,beg,end);
refresh();
}
}
}
else if(ch==KEY_LEFT)
{getyx(stdscr,x,y);
y-=1;
move(x,y);
}
else if(ch==KEY_RIGHT)
{getyx(stdscr,x,y);
y+=1;
move(x,y);
}
else if(ch==127)//Backspace
{
getyx(stdscr,x,y);
y-=1;
move(x,y);
delete_At_Pos(curr,y);
delch();
}
else if(ch==KEY_HOME)
{
break;
}
else if(ch==KEY_MOUSE){
refresh();
if(getmouse(&m_event)==OK){
//refresh();
if(m_event.bstate){
//refresh();
switch(m_event.bstate){
case BUTTON1_CLICKED:
getyx(stdscr,x,y);
if(m_event.y>x&&m_event.y<page->num_lines){
for(int i=0;i<m_event.y-x;i++){
curr=curr->next;
if(curr==NULL){move(x,y);break;}
}
move(m_event.y,m_event.x);
}
else if(m_event.y<x){
for(int i=0;i<x-m_event.y;i++){
curr=curr->prev;
if(curr==NULL){move(x,y);break;}
}
move(m_event.y,m_event.x);
}
break;
}
}
else printw("Not working\n");
}
else printw("Not key_mouse");
}
else if(ch==10){ //Enter key
getyx(stdscr,x,y);
if(page->first_line==NULL||y<curr->count){
int offset;
if(curr==NULL)offset=x;
else offset=x+1;
curr=insert_line(page,curr,offset,y);
if((curr->prev!=NULL && curr->prev->tail==NULL)||(curr->prev!=NULL)&&(curr->prev->tail->ch!='\n'))insert_At_End(curr->prev,'\n');
if(curr->tail!=NULL&&curr->tail->ch!='\n')insert_At_End(curr,'\n');
if(curr->tail==NULL)insert_At_End(curr,'\n');
clear();
display_page(page,beg,end);
move(offset,0);
}
}
else if(ch==KEY_F(2)){
if(auto_complete_flag)auto_complete_flag=0;
else auto_complete_flag=1;
}
else if(ch==KEY_F(3)){
if(curr!=NULL&&auto_complete_flag&&y<curr->count){
getyx(stdscr,x,y);
int word_chosen=0;
//printw("%s",find_word_to_autocomplete(curr,y,word));
strcpy(word,find_word_to_autocomplete(curr,y,word));
move(x,y-strlen(word)+1);
int i=search_autocomplete(sequence.first,word,0,buffer,&word_chosen,&is_keyword,1);
//int i=0;
//move(x,y);
if(i!=-2&&i!=-1){ //if not a keyword
while(buffer[i]!=NULL){
y++;
insert_At_Pos(curr,buffer[i]->data,y);
i++;
}
//move(x,0);
clear();
//move(0,0);
display_page(page,beg,end);
move(x,y);
word[0]='\0';
buffer[0]=NULL;
}
else{
move(x,y);
}
}
}
else if(ch==9){//Tab key
if(curr){
getyx(stdscr,x,y);
for(int k=0;k<4;k++){
insert_At_Pos(curr,' ',y);
y=y+1;
}
move(x,0);
display_line_edit(curr,y-1,x);
//move(x,y+4);
}
}
else
{
getyx(stdscr,x,y);
move(x,y);
/*char buffer[50];
search_autocomplete(sequence.first,ch,0,buffer);*/
if(curr!=NULL){
if(insert_At_Pos(curr,ch,y)){
getyx(stdscr,x,y);
move(x,0);
display_line_edit(curr,y,x);
}
if(is_keyword&&(ch==' '||ch==';'||ch==','||ch=='('||ch==')'||ch=='{'||ch=='}'||ch=='['||ch==']'||ch=='=')){
counter_num_spaces++;
int word_chosen=0;
find_word_to_autocomplete(curr,y,word);
word[strlen(word)-1]='\0';
int status=0;
int check=search_autocomplete(sequence.first,word,0,buffer,&word_chosen,&status,0);
//printw("%d",check);
if(check!=-2){
if(strcmp(word," ")&&strcmp(word,";")&&strcmp(word,",")&&strcmp(word,"\n")){
is_keyword=0;
insert_autocomplete(&(sequence.first),word,&is_keyword);
counter_num_spaces=0;
}
}
word[0]='\0';
buffer[0]=NULL;
}
else if(!is_keyword){
if(isalpha(ch))
{ //printw("hello");
temp_keyword[key_pointer]=ch;
key_pointer++;
temp_keyword[key_pointer]='\0';
if(key_pointer>=3&&key_pointer<=6){
//printw("hello");
int word_chosen=0;
int status=0;
check=search_autocomplete(sequence.first,temp_keyword,0,buffer,&word_chosen,&status,0);
if(check==-2){
is_keyword=1;
temp_keyword[0]='\0';
key_pointer=0;
//buffer[0]=NULL;
}
}
else if(key_pointer>=7){
temp_keyword[0]='\0';
key_pointer=0;
//buffer[0]=NULL;
}
//printw("%s",temp_keyword);
}
else{
/*temp_keyword[0]='\0';
key_pointer=0; */
temp_keyword[0]='\0';
key_pointer=0;
}
buffer[0]=NULL;
}
else{
if(counter_num_spaces<1){//printw("Hi");
is_keyword=0;
}
}
refresh();
//addch(ch);
//y++;
}
}
}
refresh();
FILE *fp2=fopen(filename,"w");
curr=page->first_line;
while(curr!=NULL){
node *temp=curr->head;
for(int i=0;i<curr->count;i++){
fputc(temp->ch,fp2);
temp=temp->rlink;
}
curr=curr->next;
}
fclose(fp2);
//clear();
delete_auto(&sequence);
destroy_page(page);
}