-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.c
112 lines (90 loc) · 2.2 KB
/
base.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
#define _GNU_SOURCE
#pragma once
#include "appendBuffer.c"
#include <stdint.h>
#include <termios.h>
#include <unistd.h>
/*** defines ***/
/// It sets the upper 3 bits of the character to 0, like the Ctrl key.
#define CTRL_KEY(k) ((k)&0x1f)
#define TAB_STOP 4
#define STATUS_MSG_TIMEOUT 5
#define QUIT_TIMES 2
typedef enum editorKey {
BACKSPACE = 127,
ESC = '\x1b',
ENTER = '\r',
ARROW_LEFT = 1000,
ARROW_RIGHT,
ARROW_UP,
ARROW_DOWN,
HOME_KEY,
END_KEY,
PAGE_UP,
PAGE_DOWN
} editorKey;
typedef enum editorHighlight {
HL_NORMAL = 0,
HL_NUMBER,
HL_MATCH
} editorHighlight;
typedef enum Mode { NORMAL, INSERT } Mode;
/*** data ***/
typedef struct row {
appendBuffer chars;
appendBuffer render;
uint8_t *hl; // Highlight information. TODO use a bitset.
} row;
row new_row() {
row r = {0};
r.chars = newAppendBuffer();
r.render = newAppendBuffer();
return r;
}
/// Holds all the state of the editor.
struct editorConfig {
struct termios orig_termios;
// Current screen buffer.
appendBuffer screen;
// Size of the terminal
uint_fast32_t screen_cols;
uint_fast32_t screen_rows;
// Left margin width
uint_fast32_t left_margin;
// Cursor Position
uint_fast32_t cx;
uint_fast32_t cy;
// Cursor Render Position
uint_fast32_t rx;
// File contents, line by line
uint_fast32_t num_rows;
row *rows;
// Current view posiiton
int_fast32_t row_offset;
int_fast32_t col_offset;
// Status bar stuff
char *filename;
appendBuffer status_msg;
time_t status_msg_time;
// State Flags
uint_fast8_t dirty;
Mode mode;
};
struct editorConfig E = {0};
uint_fast32_t getCy() { return (E.cy - E.row_offset); }
uint_fast32_t getCx() { return (E.rx - E.col_offset); }
/*** prototypes ***/
char *editorPrompt(char *prompt, void (*callback)(char *, size_t));
void editorDelChar();
void editorFind();
void editorInsertChar(size_t c);
void editorInsertNewline();
void editorRefreshScreen();
void editorSave();
void moveCursor(uint64_t key);
void rowDelChar(row *row, size_t at);
void rowInsertChar(row *row, size_t at, size_t c);
void setStatusMessage(const char *fmt, ...);
void updateRow(row *r);
void editorRowAppendString(row *src, row *dst);
void editorDelRow(size_t at);