-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.h
166 lines (150 loc) · 5.45 KB
/
helper.h
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
/***
* helper.h
*
* This header file contains all the related fucntion declarations, imports
* and macros for handing various escape sequences
*
* Author: Abhishek Shrivastava <[email protected]>
*/
// Header guard we don't want to deal with multiple import of same type
#ifndef HELPER
#define HELPER
// system header files required go here
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/ioctl.h>
#include <signal.h>
#include <ctype.h>
// macros for escape sequences go here
#define clrscr printf("\033c") //VT100 magic clear screen without scroll
#define gotopos(row,col) printf("\033[%d;%dH", (row), (col)) // gotopos will always be off by one (counts from 0)
// escape characters for keys
#define ESCAPE 0x001b
#define ENTER 0x000a
#define UP 0x0105
#define DOWN 0x0106
#define LEFT 0x0107
#define RIGHT 0x0108
#define BACKSPACE 0x7f
#define DELETE 126
#define LF 10
#define CR 13
// Color codes
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
#define RESET "\x1B[0m"
#define REDBG "\x1B[33m\x1B[41m" // escape code for a redbackround 41m
// application defaults
#define NORMAL_MODE "Normal"
#define INSERT_MODE "Insert"
#define REPLACE_MODE "Replace"
#define DEFAULT_FILE_NAME "[NoName]"
#define DEFAULT_MODE "Normal"
#define CMD_LEN 80
#define LINE_SIZE 80
#define TAB_LENGTH 4
#define LINE_NUMBERS 0 // set to 1 for line number on, experimental
/*
* A sequence will contain the metadata about the particular row
* Reference - https://www.cs.unm.edu/~crowley/papers/sds.pdf
* And the original Vi editor
*
* Linked line approach -
* The document is represented as linked list of different sequences.
* This is better than using a 2D array for document because line insertions
* are pretty easy and termination also, just have to use \0
*
* prev - the previous line
* sequence_row - the row number at which the sequence is supposed to come
* len - the length of sequence
* max_len - maximum length posssible of the seq (defined using malloc)
* data - a pointer to the stored string;
* next - the next line
*
* This type is defined here so that we can use sequence* in func decl
*/
typedef struct sequence
{
struct sequence* prev;
int seq_row;
int len;
int max_len;
char* data;
struct sequence* next;
} sequence;
// function declaration
void getControl(int fd); // to set terminal attributes
void readKey(FILE*); // master method for reading keys
void handleEscapeSequence(void); // handling escape seq
void handle_winresize(int sig); // singal for handling window resizee
void initscr(void); // initalize the editor config
void drawWindow(void); // draw the window
void setStatusLine(void); // set the status line and go back to your postion
void modify_seq_len(int); // modify the current sequence length
void modify_cur_pos(int, int, int); // modify the cursor position by some value
void insert(sequence*, int, int, char); // memmove for inserting
int readFile(char*); // read the files
void readKeyNormal(void); // this method is used to read charactes in normal mode
void replace(void); // replace the text under the cursor
void interpretCommand(void); // to handle the command mode
void exit_safely(void); // to safely exit the editor after freeing the memory
void writeFile(void); // method to write the file to disk
void executeBash(); // execute the bash command input
void updateRowNumber(sequence*, int); // update row numbers
void gHandler(int); // method to handle gg, G
/*
* config struct holds all the information related to the current state of
* editor
*
* w_row - number of rows supported by window, will change with window resize
* w_col - number of cols supported by window, will change with window resize
* cursor_row - the current row coordinate of cursor
* curosr_col - the current col coordinate of cursor
* is_mod - boolean to check whether the data in editor was modified or not
* mode - the mode the editor is opetating in
* filename - the name of file being currently edited
* openfile - filepointer to the open file
* act_rows - the number of active rows
* cmd_buf - the command buffer, which holds the text displated at bottom
* first_seq- pointer to the first sequence of doc
* current_seq- pointer to the sequence with the cursor on it
* o_term - old terminal props
* n_term - new terminal props
*/
typedef struct
{
int w_row;
int w_col;
int cursor_row;
int cursor_col;
int is_mod;
char* mode; // 0 is for NORMAL, 1 is for EDIT
char* filename;
FILE* openFile;
int act_rows; // number of active rows must match the number of sequecnes
char* cmd_buf;
sequence* first_seq;
sequence* current_seq;
int start_line;
int end_line;
int actual_row;
int actual_col;
struct termios o_term;
struct termios n_term;
} config;
/* extern variables go here
* global so we use suffix g_
* This only tells the other files that this variable exists
*/
extern config g_tavProps;
#endif