summaryrefslogtreecommitdiff
path: root/examples/dte/editor.h
blob: 86a8103c3132fda1c08bf375c1b418f12ba37254 (plain)
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
#ifndef EDITOR_H
#define EDITOR_H

#include <stdbool.h>
#include <stddef.h>
#include "buffer.h"
#include "cmdline.h"
#include "command/macro.h"
#include "command/run.h"
#include "commands.h"
#include "copy.h"
#include "file-history.h"
#include "frame.h"
#include "history.h"
#include "msg.h"
#include "options.h"
#include "regexp.h"
#include "search.h"
#include "syntax/color.h"
#include "tag.h"
#include "terminal/cursor.h"
#include "terminal/terminal.h"
#include "util/debug.h"
#include "util/hashmap.h"
#include "util/intmap.h"
#include "util/macros.h"
#include "util/ptr-array.h"
#include "util/string-view.h"
#include "view.h"

typedef enum {
    EDITOR_INITIALIZING = -2,
    EDITOR_RUNNING = -1,
    // Values 0-125 are exit codes
    EDITOR_EXIT_OK = 0,
    EDITOR_EXIT_MAX = 125,
} EditorStatus;

typedef enum {
    INPUT_NORMAL,
    INPUT_COMMAND,
    INPUT_SEARCH,
} InputMode;

typedef struct {
    const CommandSet *cmds;
    IntMap key_bindings;
} ModeHandler;

typedef struct EditorState {
    EditorStatus status;
    InputMode input_mode;
    CommandLine cmdline;
    SearchState search;
    GlobalOptions options;
    Terminal terminal;
    StringView home_dir;
    const char *user_config_dir;
    bool child_controls_terminal;
    bool everything_changed;
    bool cursor_style_changed;
    bool session_leader;
    size_t cmdline_x;
    ModeHandler modes[3];
    Clipboard clipboard;
    TagFile tagfile;
    HashMap aliases;
    HashMap compilers;
    HashMap syntaxes;
    ColorScheme colors;
    CommandMacroState macro;
    TermCursorStyle cursor_styles[NR_CURSOR_MODES];
    Frame *root_frame;
    struct Window *window;
    View *view;
    Buffer *buffer;
    PointerArray buffers;
    PointerArray filetypes;
    PointerArray file_options;
    PointerArray bookmarks;
    MessageArray messages;
    FileHistory file_history;
    History search_history;
    History command_history;
    RegexpWordBoundaryTokens regexp_word_tokens;
    const char *version;
} EditorState;

static inline void mark_everything_changed(EditorState *e)
{
    e->everything_changed = true;
}

static inline void set_input_mode(EditorState *e, InputMode mode)
{
    e->cursor_style_changed = true;
    e->input_mode = mode;
}

static inline CommandRunner cmdrunner_for_mode(EditorState *e, InputMode mode, bool allow_recording)
{
    BUG_ON(mode >= ARRAYLEN(e->modes));
    CommandRunner runner = {
        .cmds = e->modes[mode].cmds,
        .lookup_alias = (mode == INPUT_NORMAL) ? find_normal_alias : NULL,
        .home_dir = &e->home_dir,
        .allow_recording = allow_recording,
        .userdata = e,
    };
    return runner;
}

EditorState *init_editor_state(void) RETURNS_NONNULL;
void free_editor_state(EditorState *e) NONNULL_ARGS;
void any_key(Terminal *term, unsigned int esc_timeout) NONNULL_ARGS;
int main_loop(EditorState *e) NONNULL_ARGS WARN_UNUSED_RESULT;
void ui_start(EditorState *e) NONNULL_ARGS;
void ui_end(EditorState *e) NONNULL_ARGS;
void ui_resize(EditorState *e) NONNULL_ARGS;

#endif