Support horizontal centering in view commands (using m)

Fixes #442
This commit is contained in:
Maxime Coste 2015-11-02 19:49:58 +00:00
parent 17eef4a7dc
commit c8d2417646
3 changed files with 30 additions and 11 deletions

View File

@ -239,19 +239,23 @@ void view_commands(Context& context, NormalParams params)
if (not cp or not context.has_window()) if (not cp or not context.has_window())
return; return;
LineCount cursor_line = context.selections().main().cursor().line; const ByteCoord cursor = context.selections().main().cursor();
Window& window = context.window(); Window& window = context.window();
switch (tolower(*cp)) switch (tolower(*cp))
{ {
case 'v': case 'v':
case 'c': case 'c':
context.window().center_line(cursor_line); context.window().center_line(cursor.line);
break;
case 'm':
context.window().center_column(
context.buffer()[cursor.line].char_count_to(cursor.column));
break; break;
case 't': case 't':
context.window().display_line_at(cursor_line, 0); context.window().display_line_at(cursor.line, 0);
break; break;
case 'b': case 'b':
context.window().display_line_at(cursor_line, window.dimensions().line-1); context.window().display_line_at(cursor.line, window.dimensions().line-1);
break; break;
case 'h': case 'h':
context.window().scroll(-std::max<CharCount>(1, params.count)); context.window().scroll(-std::max<CharCount>(1, params.count));
@ -267,7 +271,8 @@ void view_commands(Context& context, NormalParams params)
break; break;
} }
}, "view", }, "view",
"v,c: center cursor \n" "v,c: center cursor (vertically)\n"
"m: center cursor (horzontally)\n"
"t: cursor on top \n" "t: cursor on top \n"
"b: cursor on bottom\n" "b: cursor on bottom\n"
"h: scroll left \n" "h: scroll left \n"

View File

@ -40,6 +40,11 @@ Window::~Window()
options().unregister_watcher(*this); options().unregister_watcher(*this);
} }
void Window::scroll(LineCount offset)
{
m_position.line = std::max(0_line, m_position.line + offset);
}
void Window::display_line_at(LineCount buffer_line, LineCount display_line) void Window::display_line_at(LineCount buffer_line, LineCount display_line)
{ {
if (display_line >= 0 or display_line < m_dimensions.line) if (display_line >= 0 or display_line < m_dimensions.line)
@ -51,16 +56,22 @@ void Window::center_line(LineCount buffer_line)
display_line_at(buffer_line, m_dimensions.line/2_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) void Window::scroll(CharCount offset)
{ {
m_position.column = std::max(0_char, m_position.column + offset); m_position.column = std::max(0_char, m_position.column + offset);
} }
void Window::display_column_at(CharCount buffer_column, CharCount display_column)
{
if (display_column >= 0 or display_column < m_dimensions.column)
m_position.column = std::max(0_char, buffer_column - display_column);
}
void Window::center_column(CharCount buffer_column)
{
display_column_at(buffer_column, m_dimensions.column/2_char);
}
size_t Window::compute_hash(const Context& context) const size_t Window::compute_hash(const Context& context) const
{ {
size_t res = hash_values(m_position, context.ui().dimensions(), context.buffer().timestamp()); size_t res = hash_values(m_position, context.ui().dimensions(), context.buffer().timestamp());

View File

@ -23,10 +23,13 @@ public:
const CharCoord& dimensions() const { return m_dimensions; } const CharCoord& dimensions() const { return m_dimensions; }
void set_dimensions(CharCoord dimensions); void set_dimensions(CharCoord dimensions);
void scroll(LineCount offset);
void center_line(LineCount buffer_line); void center_line(LineCount buffer_line);
void display_line_at(LineCount buffer_line, LineCount display_line); void display_line_at(LineCount buffer_line, LineCount display_line);
void scroll(LineCount offset);
void scroll(CharCount offset); void scroll(CharCount offset);
void center_column(CharCount buffer_column);
void display_column_at(CharCount buffer_column, CharCount display_column);
const DisplayBuffer& update_display_buffer(const Context& context); const DisplayBuffer& update_display_buffer(const Context& context);