summaryrefslogtreecommitdiff
path: root/src/client.cc
blob: 09d5f89b45383feac3f3e19fa55d8b714520e6b2 (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
122
123
124
125
126
127
128
#include "client.hh"

#include "color_registry.hh"
#include "context.hh"
#include "editor.hh"
#include "buffer_manager.hh"
#include "user_interface.hh"
#include "file.hh"
#include "remote.hh"
#include "client_manager.hh"
#include "window.hh"

namespace Kakoune
{

Client::Client(std::unique_ptr<UserInterface>&& ui, Editor& editor, String name)
    : m_input_handler(editor, std::move(name)), m_ui(std::move(ui))
{
    context().set_client(*this);
}

Client::~Client()
{
}

void Client::handle_available_input()
{
    while (m_ui->is_key_available())
    {
        m_input_handler.handle_key(m_ui->get_key());
        m_input_handler.clear_mode_trash();
    }
    context().window().forget_timestamp();
}

void Client::print_status(DisplayLine status_line)
{
    m_status_line = std::move(status_line);
    context().window().forget_timestamp();
}

DisplayLine Client::generate_mode_line() const
{
    auto pos = context().editor().main_selection().last();
    auto col = context().buffer()[pos.line].char_count_to(pos.column);

    std::ostringstream oss;
    oss << context().buffer().display_name()
        << " " << (int)pos.line+1 << ":" << (int)col+1;
    if (context().buffer().is_modified())
        oss << " [+]";
    if (m_input_handler.is_recording())
       oss << " [recording (" << m_input_handler.recording_reg() << ")]";
    if (context().buffer().flags() & Buffer::Flags::New)
        oss << " [new file]";
    oss << " [" << m_input_handler.mode_string() << "]" << " - "
        << context().name() << "@[" << Server::instance().session() << "]";
    return { oss.str(), get_color("StatusLine") };
}

void Client::redraw_ifn()
{
    if (context().window().timestamp() != context().buffer().timestamp())
    {
        DisplayCoord dimensions = context().ui().dimensions();
        if (dimensions == DisplayCoord{0,0})
            return;
        context().window().set_dimensions(dimensions);
        context().window().update_display_buffer();;

        context().ui().draw(context().window().display_buffer(),
                            m_status_line, generate_mode_line());
    }
}

static void reload_buffer(Context& context, const String& filename)
{
    DisplayCoord view_pos = context.window().position();
    BufferCoord cursor_pos = context.editor().main_selection().last();
    Buffer* buf = create_buffer_from_file(filename);
    if (not buf)
        return;
    Window& win = ClientManager::instance().get_unused_window_for_buffer(*buf);
    win.select(cursor_pos);
    win.set_position(view_pos);
    context.change_editor(win);
    context.print_status({ "'" + buf->display_name() + "' reloaded",
                           get_color("Information") });
}

void Client::check_buffer_fs_timestamp()
{
    Buffer& buffer = context().buffer();
    auto reload = context().options()["autoreload"].get<YesNoAsk>();
    if (not (buffer.flags() & Buffer::Flags::File) or reload == No)
        return;

    const String& filename = buffer.name();
    time_t ts = get_fs_timestamp(filename);
    if (ts == buffer.fs_timestamp())
        return;
    if (reload == Ask)
    {
        print_status({"'" + buffer.display_name() + "' was modified externally, press r or y to reload, k or n to keep", get_color("Prompt")});
        m_input_handler.on_next_key([this, ts, filename](Key key, Context& context) {
            Buffer* buf = BufferManager::instance().get_buffer_ifp(filename);
            // buffer got deleted while waiting for the key, do nothing
            if (not buf)
            {
                print_status({});
                return;
            }
            if (key == 'r' or key == 'y')
                reload_buffer(context, filename);
            if (key == 'k' or key == 'n')
            {
                buf->set_fs_timestamp(ts);
                print_status({"'" + buf->display_name() + "' kept", get_color("Information") });
            }
            else
                check_buffer_fs_timestamp();
        });
    }
    else
        reload_buffer(context(), filename);
}

}