Move selectors function to their own file
This commit is contained in:
parent
3c06846aa3
commit
3af66276f0
67
src/main.cc
67
src/main.cc
|
@ -4,6 +4,7 @@
|
||||||
#include "regex_selector.hh"
|
#include "regex_selector.hh"
|
||||||
#include "command_manager.hh"
|
#include "command_manager.hh"
|
||||||
#include "buffer_manager.hh"
|
#include "buffer_manager.hh"
|
||||||
|
#include "selectors.hh"
|
||||||
#include "assert.hh"
|
#include "assert.hh"
|
||||||
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
@ -314,58 +315,6 @@ void do_command()
|
||||||
catch (prompt_aborted&) {}
|
catch (prompt_aborted&) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool is_blank(char c)
|
|
||||||
{
|
|
||||||
return c == ' ' or c == '\t' or c == '\n';
|
|
||||||
}
|
|
||||||
|
|
||||||
bool is_word(char c)
|
|
||||||
{
|
|
||||||
if (c >= '0' and c <= '9')
|
|
||||||
return true;
|
|
||||||
if (c >= 'a' and c <= 'z')
|
|
||||||
return true;
|
|
||||||
if (c >= 'A' and c <= 'Z')
|
|
||||||
return true;
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
Selection select_to_next_word(const BufferIterator& cursor)
|
|
||||||
{
|
|
||||||
BufferIterator end = cursor;
|
|
||||||
while (not end.is_end() and is_word(*end))
|
|
||||||
++end;
|
|
||||||
|
|
||||||
while (not end.is_end() and not is_word(*end))
|
|
||||||
++end;
|
|
||||||
|
|
||||||
return Selection(cursor, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
Selection select_to_next_word_end(const BufferIterator& cursor)
|
|
||||||
{
|
|
||||||
BufferIterator end = cursor;
|
|
||||||
while (not end.is_end() and not is_word(*end))
|
|
||||||
++end;
|
|
||||||
|
|
||||||
while (not end.is_end() and is_word(*end))
|
|
||||||
++end;
|
|
||||||
|
|
||||||
return Selection(cursor, end);
|
|
||||||
}
|
|
||||||
|
|
||||||
Selection select_line(const BufferIterator& cursor)
|
|
||||||
{
|
|
||||||
BufferIterator begin = cursor;
|
|
||||||
while (not begin.is_begin() and *(begin -1) != '\n')
|
|
||||||
--begin;
|
|
||||||
|
|
||||||
BufferIterator end = cursor;
|
|
||||||
while (not end.is_end() and *end != '\n')
|
|
||||||
++end;
|
|
||||||
return Selection(begin, end + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
void do_search(Window& window)
|
void do_search(Window& window)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -376,14 +325,6 @@ void do_search(Window& window)
|
||||||
catch (prompt_aborted&) {}
|
catch (prompt_aborted&) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
Selection move_select(Window& window, const BufferIterator& cursor, const WindowCoord& offset)
|
|
||||||
{
|
|
||||||
WindowCoord cursor_pos = window.line_and_column_at(cursor);
|
|
||||||
WindowCoord new_pos = cursor_pos + offset;
|
|
||||||
|
|
||||||
return Selection(cursor, window.iterator_at(new_pos));
|
|
||||||
}
|
|
||||||
|
|
||||||
std::unordered_map<char, std::function<void (Window& window, int count)>> keymap =
|
std::unordered_map<char, std::function<void (Window& window, int count)>> keymap =
|
||||||
{
|
{
|
||||||
{ 'h', [](Window& window, int count) { window.move_cursor(WindowCoord(0, -std::max(count,1))); window.empty_selections(); } },
|
{ 'h', [](Window& window, int count) { window.move_cursor(WindowCoord(0, -std::max(count,1))); window.empty_selections(); } },
|
||||||
|
@ -410,13 +351,15 @@ std::unordered_map<char, std::function<void (Window& window, int count)>> keymap
|
||||||
{ 'W', [](Window& window, int count) { do { window.select(true, select_to_next_word); } while(--count > 0); } },
|
{ 'W', [](Window& window, int count) { do { window.select(true, select_to_next_word); } while(--count > 0); } },
|
||||||
{ 'e', [](Window& window, int count) { do { window.select(false, select_to_next_word_end); } while(--count > 0); } },
|
{ 'e', [](Window& window, int count) { do { window.select(false, select_to_next_word_end); } while(--count > 0); } },
|
||||||
{ 'E', [](Window& window, int count) { do { window.select(true, select_to_next_word_end); } while(--count > 0); } },
|
{ 'E', [](Window& window, int count) { do { window.select(true, select_to_next_word_end); } while(--count > 0); } },
|
||||||
|
{ 'b', [](Window& window, int count) { do { window.select(false, select_to_previous_word); } while(--count > 0); } },
|
||||||
|
{ 'B', [](Window& window, int count) { do { window.select(true, select_to_previous_word); } while(--count > 0); } },
|
||||||
{ '.', [](Window& window, int count) { do { window.select(false, select_line); } while(--count > 0); } },
|
{ '.', [](Window& window, int count) { do { window.select(false, select_line); } while(--count > 0); } },
|
||||||
{ '/', [](Window& window, int count) { do_search(window); } },
|
{ '/', [](Window& window, int count) { do_search(window); } },
|
||||||
{ 'u', [](Window& window, int count) { do { if (not window.undo()) { print_status("nothing left to undo"); break; } } while(--count > 0); } },
|
{ 'u', [](Window& window, int count) { do { if (not window.undo()) { print_status("nothing left to undo"); break; } } while(--count > 0); } },
|
||||||
{ 'U', [](Window& window, int count) { do { if (not window.redo()) { print_status("nothing left to redo"); break; } } while(--count > 0); } },
|
{ 'U', [](Window& window, int count) { do { if (not window.redo()) { print_status("nothing left to redo"); break; } } while(--count > 0); } },
|
||||||
};
|
};
|
||||||
|
|
||||||
int main()
|
int main(int argc, char* argv[])
|
||||||
{
|
{
|
||||||
init_ncurses();
|
init_ncurses();
|
||||||
command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit,
|
command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit,
|
||||||
|
@ -428,7 +371,7 @@ int main()
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
auto buffer = new Buffer("<scratch>");
|
auto buffer = (argc > 1) ? create_buffer_from_file(argv[1]) : new Buffer("<scratch>");
|
||||||
current_window = buffer->get_or_create_window();
|
current_window = buffer->get_or_create_window();
|
||||||
|
|
||||||
draw_window(*current_window);
|
draw_window(*current_window);
|
||||||
|
|
78
src/selectors.cc
Normal file
78
src/selectors.cc
Normal file
|
@ -0,0 +1,78 @@
|
||||||
|
#include "selectors.hh"
|
||||||
|
|
||||||
|
namespace Kakoune
|
||||||
|
{
|
||||||
|
|
||||||
|
static bool is_blank(char c)
|
||||||
|
{
|
||||||
|
return c == ' ' or c == '\t' or c == '\n';
|
||||||
|
}
|
||||||
|
|
||||||
|
static bool is_word(char c)
|
||||||
|
{
|
||||||
|
if (c >= '0' and c <= '9')
|
||||||
|
return true;
|
||||||
|
if (c >= 'a' and c <= 'z')
|
||||||
|
return true;
|
||||||
|
if (c >= 'A' and c <= 'Z')
|
||||||
|
return true;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Selection select_to_next_word(const BufferIterator& cursor)
|
||||||
|
{
|
||||||
|
BufferIterator end = cursor;
|
||||||
|
while (not end.is_end() and is_word(*end))
|
||||||
|
++end;
|
||||||
|
|
||||||
|
while (not end.is_end() and not is_word(*end))
|
||||||
|
++end;
|
||||||
|
|
||||||
|
return Selection(cursor, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
Selection select_to_next_word_end(const BufferIterator& cursor)
|
||||||
|
{
|
||||||
|
BufferIterator end = cursor;
|
||||||
|
while (not end.is_end() and not is_word(*end))
|
||||||
|
++end;
|
||||||
|
|
||||||
|
while (not end.is_end() and is_word(*end))
|
||||||
|
++end;
|
||||||
|
|
||||||
|
return Selection(cursor, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
Selection select_to_previous_word(const BufferIterator& cursor)
|
||||||
|
{
|
||||||
|
BufferIterator end = cursor;
|
||||||
|
while (not end.is_end() and not is_word(*end))
|
||||||
|
--end;
|
||||||
|
|
||||||
|
while (not end.is_end() and is_word(*end))
|
||||||
|
--end;
|
||||||
|
|
||||||
|
return Selection(cursor, end);
|
||||||
|
}
|
||||||
|
|
||||||
|
Selection select_line(const BufferIterator& cursor)
|
||||||
|
{
|
||||||
|
BufferIterator begin = cursor;
|
||||||
|
while (not begin.is_begin() and *(begin -1) != '\n')
|
||||||
|
--begin;
|
||||||
|
|
||||||
|
BufferIterator end = cursor;
|
||||||
|
while (not end.is_end() and *end != '\n')
|
||||||
|
++end;
|
||||||
|
return Selection(begin, end + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
Selection move_select(Window& window, const BufferIterator& cursor, const WindowCoord& offset)
|
||||||
|
{
|
||||||
|
WindowCoord cursor_pos = window.line_and_column_at(cursor);
|
||||||
|
WindowCoord new_pos = cursor_pos + offset;
|
||||||
|
|
||||||
|
return Selection(cursor, window.iterator_at(new_pos));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
17
src/selectors.hh
Normal file
17
src/selectors.hh
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
#ifndef selectors_hh_INCLUDED
|
||||||
|
#define selectors_hh_INCLUDED
|
||||||
|
|
||||||
|
#include "window.hh"
|
||||||
|
|
||||||
|
namespace Kakoune
|
||||||
|
{
|
||||||
|
|
||||||
|
Selection select_to_next_word(const BufferIterator& cursor);
|
||||||
|
Selection select_to_next_word_end(const BufferIterator& cursor);
|
||||||
|
Selection select_to_previous_word(const BufferIterator& cursor);
|
||||||
|
Selection select_line(const BufferIterator& cursor);
|
||||||
|
Selection move_select(Window& window, const BufferIterator& cursor, const WindowCoord& offset);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // selectors_hh_INCLUDED
|
Loading…
Reference in New Issue
Block a user