-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrawMode.c
More file actions
129 lines (124 loc) · 2.86 KB
/
rawMode.c
File metadata and controls
129 lines (124 loc) · 2.86 KB
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
/*
Author:Yashasvi Goel
Code Refactored on 21/3/20
*/
#include "rawMode.h"
void die(const char *s)//editorUtil
{
moveCursor(KILLED);
// E.cx=0;
clear(0);
perror(s);
exit(1);
}
void disableRawMode()
{
if(tcsetattr(STDIN_FILENO,TCSAFLUSH,&E.orig_termios)==-1)
die("tcsetattr");
}
void enableRawMode()
{
if(tcgetattr(STDIN_FILENO,&E.orig_termios)==-1)//fetch the terminal attr
die("tcgetattr");
atexit(disableRawMode);
struct termios raw=E.orig_termios;
raw.c_lflag &= ~(ECHO|ICANON|ISIG|IEXTEN);//local flags
//to disable echo; no text appears on screen
//ISIG diables ctl+C ctrl-z
//IEXTEN disables ctl+v
//ICANON disables canonical mode; input isn't altered anymore
raw.c_iflag&=~(IXON|ICRNL|INPCK|ISTRIP|BRKINT);//input flags
//IXON disables ctl+s ctl+q
raw.c_oflag&= ~(OPOST);//OPOST disables all post-processing including carriage operator
//Following are to edit config of read()
raw.c_cc[VMIN]=0;//min chars after which read returns
raw.c_cc[VTIME]=1;//max time read() waits before returning
if(tcsetattr(STDIN_FILENO, TCSAFLUSH, &raw)==-1)//apply the attrs(file,delay,which one)
die("tcsetattr");
}
void initEditor(){
E.cx=0;
E.cy=0;
E.rx=0;
E.rowOffset=0;
E.colOffset=0;
E.numRows=0;
E.row=NULL;
E.file=NULL;
E.status[0]='\0';
E.statusTime=0;
E.syntax=NULL;
E.dirty=0;
if(getWindowSize(&E.screenRows,&E.screenColumns)==-1)
die("Window");
E.screenRows-=2;
}
int readKey()//reads input character-by-character
{
int nread=0;
char c='\0';
while((nread=read(STDIN_FILENO,&c,1))!=1){
if(nread==-1)
die("read");
}
if(c == '\x1b'){
char seq[3];
if (read(STDIN_FILENO, &seq[0], 1) != 1) return '\x1b';
if (read(STDIN_FILENO, &seq[1], 1) != 1) return '\x1b';
if (seq[0] == '[')
{
if (seq[1] >= '0' && seq[1] <= '9')
{
if (read(STDIN_FILENO, &seq[2], 1) != 1)
return '\x1b';
if (seq[2] == '~')
{
switch (seq[1]){
case '1': return HOME_KEY;
case '3': return DEL_KEY;
case '4': return END_KEY;
case '5': return PAGE_UP;
case '6': return PAGE_DOWN;
case '7': return HOME_KEY;
case '8': return END_KEY;
}
}
}
else{
switch (seq[1]){
case 'A': return ARROW_UP;
case 'B': return ARROW_DOWN;
case 'C': return ARROW_RIGHT;
case 'D': return ARROW_LEFT;
case 'H': return HOME_KEY;
case 'F': return END_KEY;
}
}
}
else if (seq[0] == 'O'){
switch (seq[1]){
case 'H': return HOME_KEY;
case 'F': return END_KEY;
}
}
return '\x1b';
}
else{
return c;
}
}
void clear()
{
editorScroll();
strBuffer ab=str_INIT;
bufAppend(&ab,"\x1b[?25l",6);
E.cx=E.cy=0;
bufAppend(&ab,"\x1b[2J",4);
bufAppend(&ab,"\x1b[H",3);
char buf[32];
snprintf(buf,sizeof(buf),"\x1b[%d;%dH",E.cy -E.rowOffset+1,E.rx-E.colOffset+1);
bufAppend(&ab,buf,strlen(buf));
bufAppend(&ab,"\x1b[?25h",6);
write(STDOUT_FILENO,ab.buffer,ab.len);
bufFree(&ab);
}