580 lines
18 KiB
C++
580 lines
18 KiB
C++
#include "BufferPane.h"
|
|
|
|
BufferPane::BufferPane(Window * window, std::shared_ptr<Buffer> buffer)
|
|
: m_window(window), m_buffer(buffer)
|
|
{
|
|
m_cursor_screen_row = 0;
|
|
m_scroll_offset = 5;
|
|
m_iterator = buffer->add_iterator();
|
|
m_target_column = 0;
|
|
m_cursor_virtual_column = 0;
|
|
}
|
|
|
|
void BufferPane::resize(int width, int height)
|
|
{
|
|
Pane::resize(width, height);
|
|
m_columns = std::max(1, m_width / m_window->font()->get_advance());
|
|
m_rows = std::max(1, (m_height - m_window->font()->get_line_height() - 1) / m_window->font()->get_line_height());
|
|
}
|
|
|
|
void BufferPane::walk_line(const Buffer::Iterator & start_of_line, std::function<void(int, int, int, int, const Buffer::Iterator &)> callback)
|
|
{
|
|
int row_offset = 0;
|
|
int screen_column = 0;
|
|
int virtual_column = 0;
|
|
Buffer::Iterator i = start_of_line;
|
|
for (;;)
|
|
{
|
|
uint32_t code_point = *i;
|
|
if ((code_point == '\n') || (!i.valid()))
|
|
{
|
|
callback(row_offset, screen_column, virtual_column, 0, i);
|
|
break;
|
|
}
|
|
int c_width;
|
|
if (code_point == '\t')
|
|
{
|
|
uint8_t tabstop = m_buffer->tabstop();
|
|
c_width = tabstop - screen_column % tabstop;
|
|
}
|
|
else
|
|
{
|
|
c_width = character_width(code_point);
|
|
}
|
|
if (((screen_column + c_width) > m_columns) &&
|
|
(screen_column > 0))
|
|
{
|
|
row_offset++;
|
|
screen_column = 0;
|
|
}
|
|
callback(row_offset, screen_column, virtual_column, c_width, i);
|
|
virtual_column += c_width;
|
|
if (code_point == '\t')
|
|
{
|
|
screen_column += c_width;
|
|
while (screen_column >= m_columns)
|
|
{
|
|
screen_column -= m_columns;
|
|
row_offset++;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if ((screen_column + c_width) >= m_columns)
|
|
{
|
|
row_offset++;
|
|
screen_column = 0;
|
|
}
|
|
else
|
|
{
|
|
screen_column += c_width;
|
|
}
|
|
}
|
|
i.go_forward();
|
|
}
|
|
}
|
|
|
|
int BufferPane::rows_in_line(const Buffer::Iterator & start_of_line)
|
|
{
|
|
int saved_row_offset = 0;
|
|
walk_line(start_of_line, [&saved_row_offset](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) {
|
|
uint32_t code_point = *i;
|
|
if ((code_point != '\n') && (code_point != Buffer::Iterator::INVALID_CODE_POINT))
|
|
{
|
|
saved_row_offset = row_offset;
|
|
}
|
|
});
|
|
return saved_row_offset + 1;
|
|
}
|
|
|
|
int BufferPane::rows_in_line_with_iterator_offset(const Buffer::Iterator & start_of_line, const Buffer::Iterator & reference, int * iterator_row_offset)
|
|
{
|
|
int saved_row_offset = 0;
|
|
walk_line(start_of_line, [this, &saved_row_offset, &reference, &iterator_row_offset](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) {
|
|
uint32_t code_point = *i;
|
|
if (i == reference)
|
|
{
|
|
m_cursor_virtual_column = virtual_column;
|
|
*iterator_row_offset = row_offset;
|
|
}
|
|
if ((code_point != '\n') && (code_point != Buffer::Iterator::INVALID_CODE_POINT))
|
|
{
|
|
saved_row_offset = row_offset;
|
|
}
|
|
});
|
|
return saved_row_offset + 1;
|
|
}
|
|
|
|
int BufferPane::screen_rows_below_line(const Buffer::Iterator & line)
|
|
{
|
|
Buffer::Iterator i = line;
|
|
int rows = 0;
|
|
while ((rows < m_rows) && i.go_next_line())
|
|
{
|
|
rows += rows_in_line(i);
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
int BufferPane::screen_rows_above_line(const Buffer::Iterator & line, std::list<std::pair<int, Buffer::Iterator>> & backward_lines)
|
|
{
|
|
Buffer::Iterator i = line;
|
|
int rows = 0;
|
|
while ((rows < m_rows) && i.go_previous_line())
|
|
{
|
|
int rows_in_this_line = rows_in_line(i);
|
|
rows += rows_in_this_line;
|
|
backward_lines.push_back(std::pair<int, Buffer::Iterator>(rows_in_this_line, i));
|
|
}
|
|
return rows;
|
|
}
|
|
|
|
int BufferPane::update_cursor_row(std::list<std::pair<int, Buffer::Iterator>> & backward_lines)
|
|
{
|
|
Buffer::Iterator start_of_line = *m_iterator;
|
|
start_of_line.go_start_of_line();
|
|
int cursor_row_offset;
|
|
int rows_in_cursor_line = rows_in_line_with_iterator_offset(start_of_line, *m_iterator, &cursor_row_offset);
|
|
int so = effective_scroll_offset();
|
|
int rows_above = screen_rows_above_line(*m_iterator, backward_lines) + cursor_row_offset;
|
|
int rows_below = screen_rows_below_line(*m_iterator) + std::max(0, rows_in_cursor_line - cursor_row_offset - 1);
|
|
int min_rows_to_leave_above = std::min(rows_above, so);
|
|
int min_rows_to_leave_below = std::min(rows_below, so);
|
|
m_cursor_screen_row = std::min(m_rows - min_rows_to_leave_below - 1, m_cursor_screen_row);
|
|
m_cursor_screen_row = std::max(min_rows_to_leave_above, m_cursor_screen_row);
|
|
m_cursor_screen_row = std::max(m_rows - rows_below - 1, m_cursor_screen_row);
|
|
m_cursor_screen_row = std::min(rows_above, m_cursor_screen_row);
|
|
return cursor_row_offset;
|
|
}
|
|
|
|
int BufferPane::determine_new_cursor_screen_row()
|
|
{
|
|
if (m_screen_lines.size() > 0)
|
|
{
|
|
if (m_iterator->line() < m_screen_lines.begin()->second.line())
|
|
{
|
|
return 0;
|
|
}
|
|
if (m_iterator->line() > m_screen_lines.rbegin()->second.line())
|
|
{
|
|
return m_rows;
|
|
}
|
|
for (auto screen_line : m_screen_lines)
|
|
{
|
|
if (screen_line.second.line() == m_iterator->line())
|
|
{
|
|
int row_offset;
|
|
rows_in_line_with_iterator_offset(screen_line.second, *m_iterator, &row_offset);
|
|
return screen_line.first + row_offset;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void BufferPane::draw()
|
|
{
|
|
m_screen_lines.clear();
|
|
if (m_iterator->valid())
|
|
{
|
|
std::list<std::pair<int, Buffer::Iterator>> backward_lines;
|
|
int cursor_row_offset_in_current_line = update_cursor_row(backward_lines);
|
|
int screen_row = m_cursor_screen_row - cursor_row_offset_in_current_line;
|
|
Buffer::Iterator i = *m_iterator;
|
|
if (screen_row <= 0)
|
|
{
|
|
i.go_start_of_line();
|
|
}
|
|
else for (auto rows_iterator_pair : backward_lines)
|
|
{
|
|
screen_row -= rows_iterator_pair.first;
|
|
i = rows_iterator_pair.second;
|
|
if (screen_row <= 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
while (screen_row <= m_rows)
|
|
{
|
|
m_screen_lines.push_back(std::pair<int, Buffer::Iterator>(screen_row, i));
|
|
screen_row += draw_buffer_line(screen_row, i);
|
|
if (!i.go_next_line())
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
draw_cursor(col_x(0), row_y(0), m_buffer->insert_mode());
|
|
}
|
|
draw_status_bar();
|
|
}
|
|
|
|
int BufferPane::draw_buffer_line(int screen_row, const Buffer::Iterator & start_of_line)
|
|
{
|
|
int saved_row_offset = 0;
|
|
walk_line(start_of_line, [this, &saved_row_offset, &screen_row](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) {
|
|
uint32_t code_point = *i;
|
|
int draw_row = screen_row + row_offset;
|
|
int x = col_x(screen_column);
|
|
int y = row_y(draw_row);
|
|
if ((draw_row >= 0) && (draw_row <= m_rows))
|
|
{
|
|
if (i == *m_iterator)
|
|
{
|
|
/* TODO: highlight multi-column characters */
|
|
draw_cursor(x, y, m_buffer->insert_mode());
|
|
}
|
|
if ((code_point == '\n') || (code_point == Buffer::Iterator::INVALID_CODE_POINT))
|
|
{
|
|
}
|
|
else
|
|
{
|
|
saved_row_offset = row_offset;
|
|
if ((code_point != '\t') && (code_point != ' '))
|
|
{
|
|
m_window->gl()->draw_character(win_x(x), win_y(y), code_point, *m_window->font());
|
|
}
|
|
}
|
|
}
|
|
});
|
|
return saved_row_offset + 1;
|
|
}
|
|
|
|
int BufferPane::character_width(uint32_t character)
|
|
{
|
|
if (character < 0x20u)
|
|
{
|
|
return 2;
|
|
}
|
|
else if (character < 0x80u)
|
|
{
|
|
return 1u;
|
|
}
|
|
else
|
|
{
|
|
return std::max(1, m_window->font()->get_glyph(character)->get_advance() / m_window->font()->get_advance());
|
|
}
|
|
}
|
|
|
|
void BufferPane::draw_buffer_character(int screen_column, int screen_row, uint32_t character)
|
|
{
|
|
if (character < 0x20u)
|
|
{
|
|
m_window->gl()->draw_character(win_x(col_x(screen_column)), win_y(row_y(screen_row)),
|
|
'^', *m_window->font());
|
|
m_window->gl()->draw_character(win_x(col_x(screen_column + 1)), win_y(row_y(screen_row)),
|
|
(character - 0x20u + 'A'), *m_window->font());
|
|
}
|
|
else
|
|
{
|
|
m_window->gl()->draw_character(win_x(col_x(screen_column)), win_y(row_y(screen_row)),
|
|
character, *m_window->font());
|
|
}
|
|
}
|
|
|
|
void BufferPane::draw_cursor(int x, int y, bool insert_mode)
|
|
{
|
|
int width = m_window->font()->get_advance();
|
|
int height = m_window->font()->get_line_height();
|
|
if (insert_mode)
|
|
{
|
|
m_window->gl()->draw_rect(win_x(x), win_y(y), 1, height, 1.0, 0.2, 1.0, 1.0);
|
|
m_window->gl()->draw_rect(win_x(x + 1), win_y(y + height - 1), 2, 1, 1.0, 0.2, 1.0, 1.0);
|
|
m_window->gl()->draw_rect(win_x(x + 1), win_y(y), 2, 1, 1.0, 0.2, 1.0, 1.0);
|
|
}
|
|
else
|
|
{
|
|
m_window->gl()->draw_rect(win_x(x), win_y(y), width, height, 1.0, 0.2, 1.0, 1.0);
|
|
}
|
|
}
|
|
|
|
void BufferPane::handle_key(uint32_t keyval)
|
|
{
|
|
if (m_buffer->insert_mode())
|
|
{
|
|
if (keyval == '\033')
|
|
{
|
|
m_buffer->exit_insert_mode();
|
|
if (!m_iterator->is_start_of_line())
|
|
{
|
|
m_iterator->go_back();
|
|
}
|
|
}
|
|
else if (keyval == SDLK_BACKSPACE)
|
|
{
|
|
Buffer::Iterator i = *m_iterator;
|
|
i.go_back();
|
|
if (i.valid())
|
|
{
|
|
m_buffer->erase_code_point(i);
|
|
m_window->request_redraw();
|
|
}
|
|
}
|
|
else if (keyval <= 0xFFu)
|
|
{
|
|
if (keyval == SDLK_RETURN)
|
|
{
|
|
keyval = '\n';
|
|
m_cursor_screen_row++;
|
|
}
|
|
m_buffer->insert_code_point(keyval);
|
|
}
|
|
m_window->request_redraw();
|
|
}
|
|
else
|
|
{
|
|
switch (keyval)
|
|
{
|
|
case '0':
|
|
cursor_move(CursorMovement::SOL);
|
|
break;
|
|
case '$':
|
|
cursor_move(CursorMovement::EOL);
|
|
break;
|
|
case 'A':
|
|
enter_insert_mode(EnterInsertModeMode::END_OF_LINE);
|
|
break;
|
|
case 'G':
|
|
cursor_move(CursorMovement::LAST_LINE);
|
|
break;
|
|
case 'I':
|
|
enter_insert_mode(EnterInsertModeMode::START_OF_LINE);
|
|
break;
|
|
case 'O':
|
|
enter_insert_mode(EnterInsertModeMode::NEW_LINE_BEFORE);
|
|
break;
|
|
case 'a':
|
|
enter_insert_mode(EnterInsertModeMode::END_OF_CHAR);
|
|
break;
|
|
case 'g':
|
|
cursor_move(CursorMovement::FIRST_LINE);
|
|
break;
|
|
case 'h':
|
|
cursor_move(CursorMovement::LEFT);
|
|
break;
|
|
case 'i':
|
|
enter_insert_mode(EnterInsertModeMode::START_OF_CHAR);
|
|
break;
|
|
case 'j':
|
|
cursor_move(CursorMovement::DOWN);
|
|
break;
|
|
case 'k':
|
|
cursor_move(CursorMovement::UP);
|
|
break;
|
|
case 'l':
|
|
cursor_move(CursorMovement::RIGHT);
|
|
break;
|
|
case 'o':
|
|
enter_insert_mode(EnterInsertModeMode::NEW_LINE_AFTER);
|
|
break;
|
|
case 'x':
|
|
if (**m_iterator != '\n')
|
|
{
|
|
m_buffer->erase_code_point(*m_iterator);
|
|
if (**m_iterator == '\n')
|
|
{
|
|
m_iterator->go_left_in_line();
|
|
}
|
|
m_window->request_redraw();
|
|
}
|
|
break;
|
|
case Window::KEYMOD_CTRL + 'w':
|
|
if (m_buffer->filename() != "")
|
|
{
|
|
m_buffer->write_to_file(m_buffer->filename().c_str());
|
|
}
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t BufferPane::display_column() const
|
|
{
|
|
if (!m_iterator->valid())
|
|
{
|
|
return 0u;
|
|
}
|
|
if ((**m_iterator == '\n') && (!m_buffer->insert_mode()))
|
|
{
|
|
Buffer::Iterator i = *m_iterator;
|
|
if (!i.go_left_in_line())
|
|
{
|
|
return 0u;
|
|
}
|
|
}
|
|
return m_cursor_virtual_column + 1u;
|
|
}
|
|
|
|
void BufferPane::cursor_move(CursorMovement which)
|
|
{
|
|
bool moved = false;
|
|
|
|
switch (which)
|
|
{
|
|
case CursorMovement::LEFT:
|
|
moved = m_iterator->go_left_in_line();
|
|
break;
|
|
case CursorMovement::RIGHT:
|
|
moved = m_iterator->go_right_in_line(m_buffer->insert_mode());
|
|
break;
|
|
case CursorMovement::UP:
|
|
moved = m_iterator->go_previous_line();
|
|
break;
|
|
case CursorMovement::DOWN:
|
|
moved = m_iterator->go_next_line();
|
|
break;
|
|
case CursorMovement::SOL:
|
|
moved = m_iterator->go_start_of_line();
|
|
break;
|
|
case CursorMovement::EOL:
|
|
moved = m_iterator->go_end_of_line(m_buffer->insert_mode());
|
|
break;
|
|
case CursorMovement::FIRST_LINE:
|
|
{
|
|
auto it = m_buffer->begin();
|
|
if (it != *m_iterator)
|
|
{
|
|
*m_iterator = it;
|
|
moved = true;
|
|
}
|
|
}
|
|
break;
|
|
case CursorMovement::LAST_LINE:
|
|
{
|
|
auto it = m_buffer->end();
|
|
it.go_back();
|
|
it.go_start_of_line();
|
|
if (it != *m_iterator)
|
|
{
|
|
*m_iterator = it;
|
|
moved = true;
|
|
}
|
|
}
|
|
break;
|
|
}
|
|
if (moved)
|
|
{
|
|
switch (which)
|
|
{
|
|
case CursorMovement::LEFT:
|
|
case CursorMovement::RIGHT:
|
|
{
|
|
int cursor_row_offset;
|
|
Buffer::Iterator start_of_line = *m_iterator;
|
|
start_of_line.go_start_of_line();
|
|
rows_in_line_with_iterator_offset(start_of_line, *m_iterator, &cursor_row_offset);
|
|
m_target_column = m_cursor_virtual_column;
|
|
}
|
|
break;
|
|
case CursorMovement::UP:
|
|
forward_to_column(m_target_column, m_buffer->insert_mode());
|
|
break;
|
|
case CursorMovement::DOWN:
|
|
forward_to_column(m_target_column, m_buffer->insert_mode());
|
|
break;
|
|
case CursorMovement::SOL:
|
|
m_target_column = 0;
|
|
break;
|
|
case CursorMovement::EOL:
|
|
m_target_column = INT_MAX;
|
|
break;
|
|
case CursorMovement::FIRST_LINE:
|
|
m_cursor_screen_row = 0;
|
|
break;
|
|
case CursorMovement::LAST_LINE:
|
|
m_cursor_screen_row = m_rows;
|
|
break;
|
|
}
|
|
m_cursor_screen_row = determine_new_cursor_screen_row();
|
|
m_window->request_redraw();
|
|
}
|
|
}
|
|
|
|
void BufferPane::enter_insert_mode(EnterInsertModeMode which)
|
|
{
|
|
if (m_buffer->insert_mode())
|
|
return;
|
|
|
|
switch (which)
|
|
{
|
|
case EnterInsertModeMode::START_OF_CHAR:
|
|
m_buffer->enter_insert_mode(*m_iterator);
|
|
break;
|
|
case EnterInsertModeMode::END_OF_CHAR:
|
|
if (**m_iterator != '\n')
|
|
{
|
|
m_iterator->go_forward();
|
|
}
|
|
m_buffer->enter_insert_mode(*m_iterator);
|
|
break;
|
|
case EnterInsertModeMode::START_OF_LINE:
|
|
m_iterator->go_start_of_line();
|
|
enter_insert_mode(EnterInsertModeMode::START_OF_CHAR);
|
|
break;
|
|
case EnterInsertModeMode::END_OF_LINE:
|
|
m_iterator->go_end_of_line(false);
|
|
enter_insert_mode(EnterInsertModeMode::END_OF_CHAR);
|
|
break;
|
|
case EnterInsertModeMode::NEW_LINE_BEFORE:
|
|
m_iterator->go_start_of_line();
|
|
m_buffer->enter_insert_mode(*m_iterator);
|
|
m_buffer->insert_code_point('\n');
|
|
m_buffer->exit_insert_mode();
|
|
cursor_move(CursorMovement::UP);
|
|
enter_insert_mode(EnterInsertModeMode::START_OF_CHAR);
|
|
break;
|
|
case EnterInsertModeMode::NEW_LINE_AFTER:
|
|
m_iterator->go_end_of_line(true);
|
|
m_buffer->enter_insert_mode(*m_iterator);
|
|
m_buffer->insert_code_point('\n');
|
|
break;
|
|
}
|
|
|
|
m_window->request_redraw();
|
|
}
|
|
|
|
void BufferPane::forward_to_column(int column, bool allow_eol)
|
|
{
|
|
Buffer::Iterator start_of_line = *m_iterator;
|
|
start_of_line.go_start_of_line();
|
|
walk_line(start_of_line, [this, &column, &allow_eol](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & i) {
|
|
uint32_t code_point = *i;
|
|
if ((code_point != '\n') || allow_eol)
|
|
{
|
|
if (virtual_column <= column)
|
|
{
|
|
*m_iterator = i;
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void BufferPane::draw_status_bar()
|
|
{
|
|
m_window->gl()->draw_rect(win_x(0), win_y(m_window->font()->get_line_height()), m_width, 1, 0.5, 0.5, 0.5, 1.0);
|
|
m_window->gl()->draw_rect(win_x(0), win_y(0), m_width, m_window->font()->get_line_height(), 0.0, 0.0, 0.0, 1.0);
|
|
char cursor_position[20];
|
|
sprintf(cursor_position, "%zu, %zu", display_line(), display_column());
|
|
int cursor_position_length = strlen(cursor_position);
|
|
int x = m_width - m_window->font()->get_advance() * cursor_position_length;
|
|
m_window->gl()->draw_rect(win_x(x - 2), win_y(0), 1, m_window->font()->get_line_height(), 0.5, 0.5, 0.5, 1.0);
|
|
std::string filename = m_buffer->filename();
|
|
if (filename == "")
|
|
{
|
|
filename = "[No Name]";
|
|
}
|
|
int filename_x = std::min(0, x - 3 - (int)filename.size() * m_window->font()->get_advance());
|
|
for (int i = 0; i < cursor_position_length; i++)
|
|
{
|
|
m_window->gl()->draw_character(win_x(x), win_y(0), cursor_position[i], *m_window->font());
|
|
x += m_window->font()->get_advance();
|
|
}
|
|
for (size_t i = 0; i < filename.size(); i++)
|
|
{
|
|
m_window->gl()->draw_character(win_x(filename_x), win_y(0), filename[i], *m_window->font());
|
|
filename_x += m_window->font()->get_advance();
|
|
}
|
|
}
|