summaryrefslogtreecommitdiff
path: root/Editor/Editor.h
blob: ea8106ed5b9493313bf7a4e6c1e2fe110aaccf7d (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
#pragma once

#include "OwnPtr.h"
#include "Position.h"
#include "UndoStack.h"
#include <string>

class Document;
class Line;

class Editor {
public:
    Editor();
    ~Editor();

    void set_document(OwnPtr<Document>&&);
    void redraw();

    int exec();

    enum Mode {
        Idle,
        EditingCommand,
        EditingDocument,
    };

    void set_mode(Mode);
    Mode mode() const { return m_mode; } 
    bool is_editing_document() const { return m_mode == EditingDocument; }
    bool is_editing_command() const { return m_mode == EditingCommand; }
    bool is_idle() const { return m_mode == Idle; }

    void set_status_text(const std::string&);
    void set_status_text(const char* fmt, ...);

    bool insert_text_at_cursor(const std::string&);
    bool remove_text_at_cursor(const std::string&);

    void run(OwnPtr<Operation>&&);

private:
    Line& current_line();
    const Line& current_line() const;

    void move_left();
    void move_down();
    void move_up();
    void move_right();
    void move_to_end_of_line();
    void move_to_start_of_line();
    void erase_left();
    void erase_right();

    size_t max_line() const;
    size_t max_column() const;

    void update_scroll_position_if_needed();

    void draw_status_bar();
    void draw_cursor();

    void handle_document_key_press(int ch);
    void handle_command_key_press(int ch);

    void insert_at_cursor(int ch);

    void exec_command();
    void coalesce_current_line();
    void write_to_file();

    OwnPtr<Document> m_document;

    UndoStack m_undo_stack;

    // Document relative
    Position m_scroll_position;
    Position m_cursor;

    std::string m_command;
    std::string m_status_text;

    bool m_should_quit { false };
    size_t m_ruler_width { 0 };
    Mode m_mode { Idle };
};