diff --git a/src/main.cc b/src/main.cc index 58306671..2e91e345 100644 --- a/src/main.cc +++ b/src/main.cc @@ -11,12 +11,14 @@ using namespace Kakoune; void draw_window(Window& window) { - window.update_display_buffer(); - int max_x,max_y; getmaxyx(stdscr, max_y, max_x); max_y -= 1; + window.set_dimensions(LineAndColumn(max_y, max_x)); + window.update_display_buffer(); + + LineAndColumn position; for (const DisplayAtom& atom : window.display_buffer()) { diff --git a/src/window.cc b/src/window.cc index 09b7be7e..64c2b72d 100644 --- a/src/window.cc +++ b/src/window.cc @@ -170,4 +170,29 @@ void Window::set_dimensions(const LineAndColumn& dimensions) m_dimensions = dimensions; } +void Window::scroll_to_keep_cursor_visible_ifn() +{ + if (m_cursor.line < 0) + { + m_position.line = std::max(m_position.line + m_cursor.line, 0); + m_cursor.line = 0; + } + else if (m_cursor.line >= m_dimensions.line) + { + m_position.line += m_cursor.line - (m_dimensions.line - 1); + m_cursor.line = m_dimensions.line - 1; + } + + if (m_cursor.column < 0) + { + m_position.column = std::max(m_position.column + m_cursor.column, 0); + m_cursor.column = 0; + } + else if (m_cursor.column >= m_dimensions.column) + { + m_position.column += m_cursor.column - (m_dimensions.column - 1); + m_cursor.column = m_dimensions.column - 1; + } +} + } diff --git a/src/window.hh b/src/window.hh index 6fe86cef..926e1e64 100644 --- a/src/window.hh +++ b/src/window.hh @@ -57,6 +57,7 @@ public: void update_display_buffer(); private: + void scroll_to_keep_cursor_visible_ifn(); std::shared_ptr m_buffer; LineAndColumn m_position;