summaryrefslogtreecommitdiff
path: root/src/window.cc
blob: c7eaf686a096bb7e1c7409a1448e2d2a1c533a7b (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#include "window.hh"

#include "assert.hh"
#include "context.hh"
#include "highlighter.hh"
#include "hook_manager.hh"
#include "client.hh"

#include <algorithm>
#include <sstream>

namespace Kakoune
{

// Implementation in highlighters.cc
void highlight_selections(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer);
void expand_tabulations(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer);
void expand_unprintable(const Context& context, HighlightFlags flags, DisplayBuffer& display_buffer);

Window::Window(Buffer& buffer)
    : Scope(buffer),
      m_buffer(&buffer)
{
    InputHandler hook_handler{{ *m_buffer, Selection{} }};
    hook_handler.context().set_window(*this);
    hooks().run_hook("WinCreate", buffer.name(), hook_handler.context());
    options().register_watcher(*this);

    m_builtin_highlighters.add_child({"tabulations"_str, make_simple_highlighter(expand_tabulations)});
    m_builtin_highlighters.add_child({"unprintable"_str, make_simple_highlighter(expand_unprintable)});
    m_builtin_highlighters.add_child({"selections"_str,  make_simple_highlighter(highlight_selections)});

    for (auto& option : options().flatten_options())
        on_option_changed(*option);
}

Window::~Window()
{
    InputHandler hook_handler{{ *m_buffer, Selection{} }};
    hook_handler.context().set_window(*this);
    hooks().run_hook("WinClose", buffer().name(), hook_handler.context());
    options().unregister_watcher(*this);
}

void Window::display_line_at(LineCount buffer_line, LineCount display_line)
{
    if (display_line >= 0 or display_line < m_dimensions.line)
        m_position.line = std::max(0_line, buffer_line - display_line);
}

void Window::center_line(LineCount buffer_line)
{
    display_line_at(buffer_line, m_dimensions.line/2_line);
}

void Window::scroll(LineCount offset)
{
    m_position.line = std::max(0_line, m_position.line + offset);
}

void Window::scroll(CharCount offset)
{
    m_position.column = std::max(0_char, m_position.column + offset);
}

void Window::update_display_buffer(const Context& context)
{
    kak_assert(&buffer() == &context.buffer());
    scroll_to_keep_selection_visible_ifn(context);

    DisplayBuffer::LineList& lines = m_display_buffer.lines();
    lines.clear();

    for (LineCount line = 0; line < m_dimensions.line; ++line)
    {
        LineCount buffer_line = m_position.line + line;
        if (buffer_line >= buffer().line_count())
            break;
        lines.emplace_back(AtomList{ {buffer(), buffer_line, buffer_line+1} });
    }

    m_display_buffer.compute_range();
    m_highlighters.highlight(context, HighlightFlags::Highlight, m_display_buffer);
    m_builtin_highlighters.highlight(context, HighlightFlags::Highlight, m_display_buffer);

    // cut the start of the line before m_position.column
    for (auto& line : lines)
        line.trim(m_position.column, m_dimensions.column);
    m_display_buffer.optimize();

    m_timestamp = buffer().timestamp();
}

void Window::set_position(CharCoord position)
{
    m_position.line = std::max(0_line, position.line);
    m_position.column = std::max(0_char, position.column);
}

void Window::set_dimensions(CharCoord dimensions)
{
    m_dimensions = dimensions;
}

static LineCount adapt_view_pos(LineCount line, LineCount offset,
                                LineCount view_pos, LineCount view_size,
                                LineCount buffer_size)
{
    if (line - offset < view_pos)
        return std::max(0_line, line - offset);
    else if (line + offset >= view_pos + view_size)
        return std::min(buffer_size - view_size,
                        line + offset - (view_size - 1));
    return view_pos;
}

static CharCount adapt_view_pos(const DisplayBuffer& display_buffer, CharCount offset,
                                ByteCoord pos, CharCount view_pos, CharCount view_size)
{
    CharCount buffer_column = 0;
    CharCount non_buffer_column = 0;
    for (auto& line : display_buffer.lines())
    {
        for (auto& atom : line)
        {
            if (atom.has_buffer_range())
            {
                if (atom.begin() <= pos and atom.end() > pos)
                {
                    CharCount pos_beg, pos_end;
                    if (atom.type() == DisplayAtom::BufferRange)
                    {
                        auto& buf = atom.buffer();
                        pos_beg = buffer_column
                                + utf8::distance(buf.iterator_at(atom.begin()),
                                                 buf.iterator_at(pos));
                        pos_end = pos_beg+1;
                    }
                    else
                    {
                        pos_beg = buffer_column;
                        pos_end = pos_beg + atom.length();
                    }

                    if (pos_beg - offset < view_pos)
                        return std::max(0_char, pos_beg - offset);

                    if (pos_end + offset >= view_pos + view_size - non_buffer_column)
                        return pos_end + offset - view_size + non_buffer_column;
                }
                buffer_column += atom.length();
            }
            else
                non_buffer_column += atom.length();
        }
    }
    return view_pos;
}

void Window::scroll_to_keep_selection_visible_ifn(const Context& context)
{
    auto& selection = context.selections().main();
    const auto& anchor = selection.anchor();
    const auto& cursor  = selection.cursor();

    const CharCoord max_offset{(m_dimensions.line - 1)/2,
                               (m_dimensions.column - 1)/2};
    const CharCoord offset = std::min(options()["scrolloff"].get<CharCoord>(),
                                      max_offset);

    // scroll lines if needed, try to get as much of the selection visible as possible
    m_position.line = adapt_view_pos(anchor.line, offset.line, m_position.line,
                                     m_dimensions.line, buffer().line_count());
    m_position.line = adapt_view_pos(cursor.line,  offset.line, m_position.line,
                                     m_dimensions.line, buffer().line_count());

    // highlight only the line containing the cursor
    DisplayBuffer display_buffer;
    DisplayBuffer::LineList& lines = display_buffer.lines();
    lines.emplace_back(AtomList{ {buffer(), cursor.line, cursor.line+1} });

    display_buffer.compute_range();
    m_highlighters.highlight(context, HighlightFlags::MoveOnly, display_buffer);
    m_builtin_highlighters.highlight(context, HighlightFlags::MoveOnly, display_buffer);

    // now we can compute where the cursor is in display columns
    // (this is only valid if highlighting one line and multiple lines put
    // the cursor in the same position, however I do not find any sane example
    // of highlighters not doing that)
    m_position.column = adapt_view_pos(display_buffer, offset.column,
                                       anchor.line == cursor.line ? anchor : cursor.line,
                                       m_position.column, m_dimensions.column);
    m_position.column = adapt_view_pos(display_buffer, offset.column, cursor,
                                       m_position.column, m_dimensions.column);
}

namespace
{
CharCount find_display_column(const DisplayLine& line, const Buffer& buffer,
                              ByteCoord coord)
{
    CharCount column = 0;
    for (auto& atom : line)
    {
        if (atom.has_buffer_range() and
            coord >= atom.begin() and coord < atom.end())
        {
            if (atom.type() == DisplayAtom::BufferRange)
                column += utf8::distance(buffer.iterator_at(atom.begin()),
                                         buffer.iterator_at(coord));
            return column;
        }
        column += atom.length();
    }
    return column;
}

ByteCoord find_buffer_coord(const DisplayLine& line, const Buffer& buffer,
                            CharCount column)
{
    auto& range = line.range();
    for (auto& atom : line)
    {
        CharCount len = atom.length();
        if (atom.has_buffer_range() and column < len)
        {
            if (atom.type() == DisplayAtom::BufferRange)
                return utf8::advance(buffer.iterator_at(atom.begin()), buffer.iterator_at(range.second),
                                     std::max(0_char, column)).coord();
             return atom.begin();
        }
        column -= len;
    }
    return buffer.clamp(buffer.prev(range.second));
}
}

CharCoord Window::display_position(ByteCoord coord)
{
    LineCount l = 0;
    for (auto& line : m_display_buffer.lines())
    {
        auto& range = line.range();
        if (range.first <= coord and coord < range.second)
            return {l, find_display_column(line, buffer(), coord)};
        ++l;
    }
    return { 0, 0 };
}

ByteCoord Window::offset_coord(ByteCoord coord, CharCount offset)
{
    return buffer().offset_coord(coord, offset);
}

ByteCoordAndTarget Window::offset_coord(ByteCoordAndTarget coord, LineCount offset)
{
    auto line = clamp(coord.line + offset, 0_line, buffer().line_count()-1);
    DisplayBuffer display_buffer;
    DisplayBuffer::LineList& lines = display_buffer.lines();
    lines.emplace_back(AtomList{ {buffer(), coord.line, coord.line+1} });
    lines.emplace_back(AtomList{ {buffer(), line, line+1} });
    display_buffer.compute_range();

    InputHandler hook_handler{{ *m_buffer, Selection{} } };
    hook_handler.context().set_window(*this);
    m_highlighters.highlight(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer);
    m_builtin_highlighters.highlight(hook_handler.context(), HighlightFlags::MoveOnly, display_buffer);

    CharCount column = coord.target == -1 ? find_display_column(lines[0], buffer(), coord) : coord.target;
    return { find_buffer_coord(lines[1], buffer(), column), column };
}

void Window::on_option_changed(const Option& option)
{
    String desc = option.name() + "=" + option.get_as_string();
    InputHandler hook_handler{{ *m_buffer, Selection{} }};
    hook_handler.context().set_window(*this);
    hooks().run_hook("WinSetOption", desc, hook_handler.context());

    // an highlighter might depend on the option, so we need to redraw
    forget_timestamp();
}

}