+/Femto

toy text editor

git clone https://git.julia.blue/+/Femto

Joseph RyanFixup973c5b9

master
2.3 KiB121 linesraw
1#pragma once
2
3#include <stdarg.h>
4#include <string.h>
5#include <time.h>
6
7#include "display.h"
8#include "fileio.h"
9#include "terminal.h"
10
11/**
12 * Encapsulates a row of text including its position in the file and syntax
13 * highlighting data.
14 */
15struct erow {
16    int index;
17    char* text;
18    int size;
19    char* hl;
20    int unclosed;
21};
22
23typedef struct erow erow;
24
25/**
26 * Global object containing the state of the editor.
27 */
28struct editorConfig {
29    int cx, oldcx, cy;
30    int width, height;
31    int scrollRow, scrollCol;
32    erow* row;
33    int numrows;
34    int dirty;
35    char* filename;
36    int tabSize;
37    char statusmsg[80];
38    time_t messageTime;
39    struct editorSyntax* syntax;
40    struct termios orig_termios;
41};
42
43extern struct editorConfig E;
44
45enum keys {
46    BACKSPACE = 127,
47    ARROW_UP = 1024,
48    ARROW_DOWN,
49    ARROW_LEFT,
50    ARROW_RIGHT,
51    PAGE_UP,
52    PAGE_DOWN,
53    HOME_KEY,
54    END_KEY,
55    DEL_KEY
56};
57
58/**
59 * Initializes global editor object, destroying an old one of it existed.
60 */
61void initEditor();
62
63/**
64 * Displays a string to the user with printf style fomatting flags.
65 */
66void setStatusMessage(const char* fmt, ...);
67
68/**
69 * Displays a string to the user and lets them input a response, with an
70 * optional callback function firing after each keypress.
71 */
72char* prompt(char* prompt, void (*callback)(char*, int));
73
74/**
75 * Callback function for the find operation. Handles moving between selections
76 * with arrow keys and canceling or finishing the find operation.
77 * @param query current search string
78 * @param key   latest key pressed
79 */
80void findcb(char* query, int key);
81
82/**
83 * Prompts the user to search for a string in the file.
84 */
85void find();
86
87/**
88 * Creates a new row in the editor.
89 * @param s   seed string for new row
90 * @param len seed string length
91 */
92void insertRow(int at, char* s, size_t len);
93
94void deleteRow(int at);
95
96void freeRow(erow* row);
97
98/**
99 * Handles arrow key presses.
100 */
101void moveCursor(int key);
102
103void rowInsertChar(erow* row, int at, char c);
104
105void rowDeleteChar(erow* row, int at);
106
107void rowAppendStr(erow* row, char* s, size_t len);
108
109void insertNewline();
110
111void insertChar(int c);
112
113void deleteChar();
114
115int countTabs(char* s, size_t len);
116
117/**
118 * Contains the high level logic for handling different kinds of keys
119 * (blocking).
120 */
121void processKeypress();