Begin to use CommandMap to evaluate command-mode inputs

This commit is contained in:
Josh Holtrop 2017-11-05 09:42:20 -05:00
parent e58d8a4500
commit ac316193fb
3 changed files with 199 additions and 121 deletions

View File

@ -22,21 +22,33 @@ public:
YANK_MOTION, YANK_MOTION,
YANK_LINE, YANK_LINE,
UNDO, UNDO,
REDO,
ENTER_INSERT_MODE, ENTER_INSERT_MODE,
ENTER_INSERT_MODE_NEXT_LINE, ENTER_INSERT_MODE_NEXT_LINE,
ENTER_INSERT_MODE_AFTER_CHAR, ENTER_INSERT_MODE_AFTER_CHAR,
ENTER_INSERT_MODE_AFTER_LINE,
ENTER_INSERT_MODE_START_OF_LINE,
ENTER_INSERT_MODE_NEW_LINE_BEFORE,
ENTER_INSERT_MODE_NEW_LINE_AFTER,
PUT, PUT,
PUT_BEFORE, PUT_BEFORE,
CHANGE_CHAR, CHANGE_CHAR,
GO_TO_LINE, GO_TO_LINE,
GO_TO_LAST_LINE,
GO_LEFT, GO_LEFT,
GO_DOWN, GO_DOWN,
GO_UP, GO_UP,
GO_RIGHT, GO_RIGHT,
NEXT,
PREV,
GO_START_OF_LINE, GO_START_OF_LINE,
GO_END_OF_LINE, GO_END_OF_LINE,
NEXT,
PREV,
SCROLL_WINDOW_UP_ONE_LINE,
SCROLL_WINDOW_DOWN_ONE_LINE,
SCROLL_WINDOW_UP_HALF_SCREEN,
SCROLL_WINDOW_DOWN_HALF_SCREEN,
SCROLL_WINDOW_UP_WHOLE_SCREEN,
SCROLL_WINDOW_DOWN_WHOLE_SCREN,
COMMAND_COUNT, COMMAND_COUNT,
}; };

View File

@ -6,6 +6,7 @@
#include "jes_icon-32x32.h" #include "jes_icon-32x32.h"
#include <unistd.h> #include <unistd.h>
#include <unordered_map> #include <unordered_map>
#include "DefaultCommandMap.h"
#define INITIAL_WIDTH 800 #define INITIAL_WIDTH 800
#define INITIAL_HEIGHT 800 #define INITIAL_HEIGHT 800
@ -215,150 +216,210 @@ void Window::handle_event(Jtk_Event & event)
void Window::handle_keypress(uint32_t keyval) void Window::handle_keypress(uint32_t keyval)
{ {
uint32_t ctrl_keyval = keyval & (JTK_KEY_KEYCODE_MASK | JTK_KEY_MODS_CTRL);
uint32_t keycode = keyval & JTK_KEY_KEYCODE_MASK; uint32_t keycode = keyval & JTK_KEY_KEYCODE_MASK;
switch (keyval & (JTK_KEY_KEYCODE_MASK | JTK_KEY_MODS_CTRL)) if (m_focused_buffer_pane->insert_mode())
{ {
case JTK_KEY_HOME: if (keycode == '\033')
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SOL);
break;
case JTK_KEY_END:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::EOL);
break;
case JTK_KEY_RIGHT:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::RIGHT);
break;
case JTK_KEY_LEFT:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LEFT);
break;
case JTK_KEY_DOWN:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::DOWN);
break;
case JTK_KEY_UP:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::UP);
break;
case JTK_KEY_PAGE_UP:
m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
break;
case JTK_KEY_PAGE_DOWN:
m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
break;
default:
if (m_focused_buffer_pane->insert_mode())
{ {
if (keycode == '\033') if (m_focused_buffer_pane == m_command_buffer_pane)
{ {
if (m_focused_buffer_pane == m_command_buffer_pane) m_command_buffer_pane->clear();
{ m_command_buffer_screen_rows = 1;
m_command_buffer_pane->clear(); change_focus(m_buffer_pane);
m_command_buffer_screen_rows = 1;
change_focus(m_buffer_pane);
}
else
{
m_focused_buffer_pane->exit_insert_mode();
}
} }
else if (keycode < 0xFFu) else
{ {
if ((keycode == '\n') && (m_focused_buffer_pane == m_command_buffer_pane)) m_focused_buffer_pane->exit_insert_mode();
{
EncodedString command = m_command_buffer->get_string();
m_command_buffer_pane->clear();
m_command_buffer_screen_rows = 1;
change_focus(m_buffer_pane);
handle_command(command);
}
else
{
m_focused_buffer_pane->insert_code_point(keycode);
}
} }
} }
else if ((keycode == '\n') && (m_focused_buffer_pane == m_command_buffer_pane))
{
EncodedString command = m_command_buffer->get_string();
m_command_buffer_pane->clear();
m_command_buffer_screen_rows = 1;
change_focus(m_buffer_pane);
handle_command(command);
}
else if (ctrl_keyval <= 0xFFu)
{
m_focused_buffer_pane->insert_code_point(ctrl_keyval);
}
else else
{ {
switch (keyval & (JTK_KEY_KEYCODE_MASK | JTK_KEY_MODS_CTRL)) switch (ctrl_keyval)
{ {
case '0': case JTK_KEY_HOME:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SOL); m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SOL);
break; break;
case '$': case JTK_KEY_END:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::EOL); m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::EOL);
break; break;
case ':': case JTK_KEY_RIGHT:
m_command_buffer_pane->clear();
change_focus(m_command_buffer_pane);
break;
case 'A':
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::END_OF_LINE);
break;
case 'G':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LAST_LINE);
break;
case 'I':
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::START_OF_LINE);
break;
case 'J':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SCREEN_ROW_DOWN);
break;
case 'K':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SCREEN_ROW_UP);
break;
case 'O':
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::NEW_LINE_BEFORE);
break;
case 'a':
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::END_OF_CHAR);
break;
case 'g':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::FIRST_LINE);
break;
case 'h':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LEFT);
break;
case 'i':
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::START_OF_CHAR);
break;
case 'j':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::DOWN);
break;
case 'k':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::UP);
break;
case 'l':
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::RIGHT); m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::RIGHT);
break; break;
case 'o': case JTK_KEY_LEFT:
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::NEW_LINE_AFTER); m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LEFT);
break; break;
case 'r': case JTK_KEY_DOWN:
m_focused_buffer_pane->redo(); m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::DOWN);
break; break;
case 'u': case JTK_KEY_UP:
m_focused_buffer_pane->undo(); m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::UP);
break; break;
case 'x': case JTK_KEY_PAGE_UP:
m_focused_buffer_pane->kill_character_at_cursor();
break;
case JTK_KEY_MODS_CTRL + 'b':
m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN); m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
break; break;
case JTK_KEY_MODS_CTRL + 'd': case JTK_KEY_PAGE_DOWN:
m_focused_buffer_pane->scroll_window_down(ScrollMode::HALF_SCREEN);
break;
case JTK_KEY_MODS_CTRL + 'e':
m_focused_buffer_pane->scroll_window_down(ScrollMode::ONE_LINE);
break;
case JTK_KEY_MODS_CTRL + 'f':
m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN); m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
break; break;
case JTK_KEY_MODS_CTRL + 'u':
m_focused_buffer_pane->scroll_window_up(ScrollMode::HALF_SCREEN);
break;
case JTK_KEY_MODS_CTRL + 'y':
m_focused_buffer_pane->scroll_window_up(ScrollMode::ONE_LINE);
break;
} }
} }
}
else
{
if ((keycode == '\033') || (keycode == '\b'))
{
m_command_input.clear();
m_command_invalid = false;
}
else if (keycode == ':')
{
m_command_buffer_pane->clear();
change_focus(m_command_buffer_pane);
}
else
{
m_command_input.push_back(ctrl_keyval);
evaluate_command_input();
}
}
}
void Window::evaluate_command_input()
{
auto cm = DefaultCommandMap::get();
Command command;
switch (cm->lookup_command(&m_command_input[0], m_command_input.size(), command))
{
case CommandMap::COMMAND_INVALID:
m_command_invalid = true;
break;
case CommandMap::COMMAND_INCOMPLETE:
break;
case CommandMap::COMMAND_COMPLETE:
execute_command(command);
m_command_input.clear();
break;
}
}
void Window::execute_command(const Command & command)
{
switch (command.main.id)
{
case Command::GO_FORWARD_UP_TO_CHAR:
break;
case Command::GO_FORWARD_ON_TO_CHAR:
break;
case Command::GO_BACK_UP_TO_CHAR:
break;
case Command::GO_BACK_ON_TO_CHAR:
break;
case Command::DELETE_MOTION:
break;
case Command::DELETE_LINE:
break;
case Command::DELETE_CHAR:
m_focused_buffer_pane->kill_character_at_cursor();
break;
case Command::DELETE_CHAR_BACK:
break;
case Command::CHANGE_MOTION:
break;
case Command::CHANGE_LINE:
break;
case Command::YANK_MOTION:
break;
case Command::YANK_LINE:
break;
case Command::UNDO:
m_focused_buffer_pane->undo();
break;
case Command::REDO:
m_focused_buffer_pane->redo();
break;
case Command::ENTER_INSERT_MODE:
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::START_OF_CHAR);
break;
case Command::ENTER_INSERT_MODE_NEXT_LINE:
break;
case Command::ENTER_INSERT_MODE_AFTER_CHAR:
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::END_OF_CHAR);
break;
case Command::ENTER_INSERT_MODE_AFTER_LINE:
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::END_OF_LINE);
break;
case Command::ENTER_INSERT_MODE_START_OF_LINE:
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::START_OF_LINE);
break;
case Command::ENTER_INSERT_MODE_NEW_LINE_BEFORE:
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::NEW_LINE_BEFORE);
break;
case Command::ENTER_INSERT_MODE_NEW_LINE_AFTER:
m_focused_buffer_pane->enter_insert_mode(EnterInsertModeMode::NEW_LINE_AFTER);
break;
case Command::PUT:
break;
case Command::PUT_BEFORE:
break;
case Command::CHANGE_CHAR:
break;
case Command::GO_TO_LINE:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::FIRST_LINE);
break;
case Command::GO_TO_LAST_LINE:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LAST_LINE);
break;
case Command::GO_LEFT:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::LEFT);
break;
case Command::GO_DOWN:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::DOWN);
break;
case Command::GO_UP:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::UP);
break;
case Command::GO_RIGHT:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::RIGHT);
break;
case Command::GO_START_OF_LINE:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::SOL);
break;
case Command::GO_END_OF_LINE:
m_focused_buffer_pane->cursor_move(BufferPane::CursorMovement::EOL);
break;
case Command::NEXT:
break;
case Command::PREV:
break;
case Command::SCROLL_WINDOW_UP_ONE_LINE:
m_focused_buffer_pane->scroll_window_up(ScrollMode::ONE_LINE);
break;
case Command::SCROLL_WINDOW_DOWN_ONE_LINE:
m_focused_buffer_pane->scroll_window_down(ScrollMode::ONE_LINE);
break;
case Command::SCROLL_WINDOW_UP_HALF_SCREEN:
m_focused_buffer_pane->scroll_window_up(ScrollMode::HALF_SCREEN);
break;
case Command::SCROLL_WINDOW_DOWN_HALF_SCREEN:
m_focused_buffer_pane->scroll_window_down(ScrollMode::HALF_SCREEN);
break;
case Command::SCROLL_WINDOW_UP_WHOLE_SCREEN:
m_focused_buffer_pane->scroll_window_up(ScrollMode::WHOLE_SCREEN);
break;
case Command::SCROLL_WINDOW_DOWN_WHOLE_SCREN:
m_focused_buffer_pane->scroll_window_down(ScrollMode::WHOLE_SCREEN);
break; break;
} }
} }

View File

@ -3,12 +3,14 @@
#include <memory> #include <memory>
#include <utility> #include <utility>
#include <vector>
#include "Font.h" #include "Font.h"
#include "Buffer.h" #include "Buffer.h"
#include "GL.h" #include "GL.h"
#include "EncodedString.h" #include "EncodedString.h"
#include "CommandParser.h" #include "CommandParser.h"
#include "Jtk.h" #include "Jtk.h"
#include "Command.h"
class BufferPane; class BufferPane;
@ -40,7 +42,6 @@ public:
std::shared_ptr<GL> gl() const { return m_gl; } std::shared_ptr<GL> gl() const { return m_gl; }
protected: protected:
void resize(size_t width, size_t height); void resize(size_t width, size_t height);
void redraw(); void redraw();
void handle_event(Jtk_Event & event); void handle_event(Jtk_Event & event);
@ -48,6 +49,8 @@ protected:
void change_focus(std::shared_ptr<BufferPane> buffer_pane); void change_focus(std::shared_ptr<BufferPane> buffer_pane);
void set_window_icon(); void set_window_icon();
void handle_command(const EncodedString & command); void handle_command(const EncodedString & command);
void evaluate_command_input();
void execute_command(const Command & command);
void command_write_file(const CommandParser & cp); void command_write_file(const CommandParser & cp);
void command_quit(const CommandParser & cp); void command_quit(const CommandParser & cp);
@ -68,6 +71,8 @@ protected:
std::shared_ptr<BufferPane> m_focused_buffer_pane; std::shared_ptr<BufferPane> m_focused_buffer_pane;
std::shared_ptr<Buffer> m_command_buffer; std::shared_ptr<Buffer> m_command_buffer;
std::shared_ptr<BufferPane> m_command_buffer_pane; std::shared_ptr<BufferPane> m_command_buffer_pane;
std::vector<uint32_t> m_command_input;
bool m_command_invalid;
}; };
#endif #endif