2011-09-02 18:51:20 +02:00
|
|
|
#include "window.hh"
|
|
|
|
#include "buffer.hh"
|
|
|
|
#include "file.hh"
|
|
|
|
#include "regex_selector.hh"
|
2011-09-07 20:16:56 +02:00
|
|
|
#include "command_manager.hh"
|
2011-09-08 16:30:36 +02:00
|
|
|
#include "buffer_manager.hh"
|
2011-09-23 16:31:57 +02:00
|
|
|
#include "register_manager.hh"
|
2011-09-21 16:37:09 +02:00
|
|
|
#include "selectors.hh"
|
2011-09-09 21:24:18 +02:00
|
|
|
#include "assert.hh"
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
#include <unordered_map>
|
2011-09-09 21:24:18 +02:00
|
|
|
#include <ncurses.h>
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
using namespace Kakoune;
|
2011-09-23 16:27:34 +02:00
|
|
|
using namespace std::placeholders;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2011-09-26 01:51:12 +02:00
|
|
|
void set_attribute(int attribute, bool on)
|
|
|
|
{
|
|
|
|
if (on)
|
|
|
|
attron(attribute);
|
|
|
|
else
|
|
|
|
attroff(attribute);
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
void draw_window(Window& window)
|
|
|
|
{
|
|
|
|
int max_x,max_y;
|
|
|
|
getmaxyx(stdscr, max_y, max_x);
|
|
|
|
max_y -= 1;
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
window.set_dimensions(WindowCoord(max_y, max_x));
|
2011-09-05 20:55:31 +02:00
|
|
|
window.update_display_buffer();
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
WindowCoord position;
|
2011-09-02 18:51:20 +02:00
|
|
|
for (const DisplayAtom& atom : window.display_buffer())
|
|
|
|
{
|
|
|
|
const std::string& content = atom.content;
|
|
|
|
|
2011-09-26 01:51:12 +02:00
|
|
|
set_attribute(A_UNDERLINE, atom.attribute & Underline);
|
|
|
|
set_attribute(A_REVERSE, atom.attribute & Reverse);
|
|
|
|
set_attribute(A_BLINK, atom.attribute & Blink);
|
|
|
|
set_attribute(A_BOLD, atom.attribute & Bold);
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
size_t pos = 0;
|
|
|
|
size_t end;
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
move(position.line, position.column);
|
|
|
|
clrtoeol();
|
|
|
|
end = content.find_first_of('\n', pos);
|
|
|
|
std::string line = content.substr(pos, end - pos);
|
|
|
|
addstr(line.c_str());
|
|
|
|
|
|
|
|
if (end != std::string::npos)
|
|
|
|
{
|
|
|
|
position.line = position.line + 1;
|
|
|
|
position.column = 0;
|
|
|
|
pos = end + 1;
|
|
|
|
|
|
|
|
if (position.line >= max_y)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
position.column += line.length();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (position.line >= max_y)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
while (++position.line < max_y)
|
|
|
|
{
|
|
|
|
move(position.line, 0);
|
|
|
|
clrtoeol();
|
|
|
|
addch('~');
|
|
|
|
}
|
|
|
|
|
2011-09-05 21:06:31 +02:00
|
|
|
const WindowCoord& cursor_position = window.cursor_position();
|
2011-09-02 18:51:20 +02:00
|
|
|
move(cursor_position.line, cursor_position.column);
|
|
|
|
}
|
|
|
|
|
|
|
|
void init_ncurses()
|
|
|
|
{
|
|
|
|
initscr();
|
|
|
|
cbreak();
|
|
|
|
noecho();
|
|
|
|
nonl();
|
|
|
|
intrflush(stdscr, false);
|
|
|
|
keypad(stdscr, true);
|
|
|
|
curs_set(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deinit_ncurses()
|
|
|
|
{
|
|
|
|
endwin();
|
|
|
|
}
|
|
|
|
|
2011-09-02 20:02:29 +02:00
|
|
|
struct prompt_aborted {};
|
|
|
|
|
2011-09-13 23:16:48 +02:00
|
|
|
struct NullCompletion
|
|
|
|
{
|
|
|
|
Completions operator() (const std::string&, size_t cursor_pos)
|
|
|
|
{
|
|
|
|
return Completions(cursor_pos, cursor_pos);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::string prompt(const std::string& text,
|
|
|
|
std::function<Completions (const std::string&, size_t)> completer = NullCompletion())
|
2011-09-02 20:02:29 +02:00
|
|
|
{
|
|
|
|
int max_x, max_y;
|
|
|
|
getmaxyx(stdscr, max_y, max_x);
|
|
|
|
move(max_y-1, 0);
|
|
|
|
addstr(text.c_str());
|
|
|
|
clrtoeol();
|
|
|
|
|
|
|
|
std::string result;
|
2011-09-14 21:15:09 +02:00
|
|
|
size_t cursor_pos = 0;
|
|
|
|
|
|
|
|
Completions completions;
|
|
|
|
int current_completion = -1;
|
|
|
|
std::string text_before_completion;
|
|
|
|
|
2011-09-02 20:02:29 +02:00
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
char c = getch();
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case '\r':
|
|
|
|
return result;
|
2011-09-14 21:15:09 +02:00
|
|
|
case 4:
|
|
|
|
if (cursor_pos > 0)
|
|
|
|
--cursor_pos;
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
if (cursor_pos < result.length())
|
|
|
|
++cursor_pos;
|
|
|
|
break;
|
2011-09-02 20:02:29 +02:00
|
|
|
case 7:
|
2011-09-14 21:15:09 +02:00
|
|
|
if (cursor_pos != 0)
|
2011-09-02 20:02:29 +02:00
|
|
|
{
|
2011-09-14 21:15:09 +02:00
|
|
|
result = result.substr(0, cursor_pos - 1)
|
|
|
|
+ result.substr(cursor_pos, std::string::npos);
|
|
|
|
|
|
|
|
--cursor_pos;
|
2011-09-02 20:02:29 +02:00
|
|
|
}
|
2011-09-14 21:15:09 +02:00
|
|
|
|
|
|
|
current_completion = -1;
|
2011-09-02 20:02:29 +02:00
|
|
|
break;
|
|
|
|
case 27:
|
|
|
|
throw prompt_aborted();
|
2011-09-13 23:16:48 +02:00
|
|
|
case '\t':
|
|
|
|
{
|
2011-09-14 21:15:09 +02:00
|
|
|
if (current_completion == -1)
|
2011-09-13 23:16:48 +02:00
|
|
|
{
|
2011-09-16 11:20:01 +02:00
|
|
|
completions = completer(result, cursor_pos);
|
2011-09-14 21:15:09 +02:00
|
|
|
if (completions.candidates.empty())
|
|
|
|
break;
|
|
|
|
|
|
|
|
text_before_completion = result.substr(completions.start,
|
|
|
|
completions.end - completions.start);
|
2011-09-13 23:16:48 +02:00
|
|
|
}
|
2011-09-14 21:15:09 +02:00
|
|
|
++current_completion;
|
|
|
|
|
|
|
|
std::string completion;
|
|
|
|
if (current_completion >= completions.candidates.size())
|
|
|
|
{
|
|
|
|
if (current_completion == completions.candidates.size() and
|
|
|
|
std::find(completions.candidates.begin(), completions.candidates.end(), text_before_completion) == completions.candidates.end())
|
|
|
|
completion = text_before_completion;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current_completion = 0;
|
|
|
|
completion = completions.candidates[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
completion = completions.candidates[current_completion];
|
|
|
|
|
|
|
|
move(max_y-1, text.length());
|
|
|
|
result = result.substr(0, completions.start) + completion;
|
|
|
|
cursor_pos = completions.start + completion.length();
|
2011-09-13 23:16:48 +02:00
|
|
|
break;
|
|
|
|
}
|
2011-09-02 20:02:29 +02:00
|
|
|
default:
|
2011-09-14 21:15:09 +02:00
|
|
|
current_completion = -1;
|
|
|
|
result = result.substr(0, cursor_pos) + c + result.substr(cursor_pos, std::string::npos);
|
|
|
|
++cursor_pos;
|
2011-09-02 20:02:29 +02:00
|
|
|
}
|
2011-09-14 21:15:09 +02:00
|
|
|
|
|
|
|
move(max_y - 1, text.length());
|
|
|
|
clrtoeol();
|
|
|
|
addstr(result.c_str());
|
|
|
|
move(max_y - 1, text.length() + cursor_pos);
|
|
|
|
refresh();
|
2011-09-02 20:02:29 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_status(const std::string& status)
|
|
|
|
{
|
|
|
|
int x,y;
|
|
|
|
getmaxyx(stdscr, y, x);
|
|
|
|
move(y-1, 0);
|
|
|
|
clrtoeol();
|
|
|
|
addstr(status.c_str());
|
|
|
|
}
|
|
|
|
|
2011-09-20 00:00:29 +02:00
|
|
|
struct scoped_status
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-20 00:00:29 +02:00
|
|
|
scoped_status(const std::string& status)
|
|
|
|
{
|
|
|
|
print_status(status);
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
~scoped_status()
|
|
|
|
{
|
|
|
|
print_status("");
|
|
|
|
refresh();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void do_insert(Window& window, bool append = false)
|
|
|
|
{
|
|
|
|
scoped_status("-- INSERT --");
|
|
|
|
Kakoune::IncrementalInserter inserter(window, append);
|
2011-09-22 11:24:16 +02:00
|
|
|
draw_window(window);
|
2011-09-02 18:51:20 +02:00
|
|
|
while(true)
|
|
|
|
{
|
2011-09-20 00:00:29 +02:00
|
|
|
const WindowCoord& pos = inserter.cursors().back();
|
|
|
|
move(pos.line, pos.column);
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
char c = getch();
|
2011-09-20 00:00:29 +02:00
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 27:
|
|
|
|
return;
|
|
|
|
|
|
|
|
case 4:
|
|
|
|
inserter.move_cursor({0, -1});
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
inserter.move_cursor({0, 1});
|
2011-09-02 18:51:20 +02:00
|
|
|
break;
|
|
|
|
|
2011-09-20 00:00:29 +02:00
|
|
|
case 7:
|
|
|
|
inserter.erase();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '\r':
|
|
|
|
c = '\n';
|
|
|
|
default:
|
|
|
|
inserter.insert(std::string() + c);
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
draw_window(window);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-22 16:02:07 +02:00
|
|
|
void do_go(Window& window, int count)
|
|
|
|
{
|
2011-09-24 15:08:04 +02:00
|
|
|
BufferIterator target;
|
2011-09-22 16:02:07 +02:00
|
|
|
if (count != 0)
|
2011-09-24 15:08:04 +02:00
|
|
|
target = window.buffer().iterator_at({count, 0});
|
2011-09-22 16:02:07 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
char c = getch();
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'g':
|
|
|
|
case 't':
|
2011-09-24 15:08:04 +02:00
|
|
|
target = window.buffer().iterator_at({0,0});
|
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
target = window.iterator_at(window.cursor_position());
|
|
|
|
while (not target.is_end() and *target != '\n')
|
|
|
|
++target;
|
|
|
|
--target;
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
target = window.iterator_at(window.cursor_position());
|
|
|
|
while (not target.is_begin() and *target != '\n')
|
|
|
|
--target;
|
|
|
|
++target;
|
2011-09-22 16:02:07 +02:00
|
|
|
break;
|
|
|
|
case 'b':
|
2011-09-24 15:08:04 +02:00
|
|
|
target = window.buffer().iterator_at(
|
|
|
|
{window.buffer().line_count() - 1, 0});
|
|
|
|
break;
|
2011-09-22 16:02:07 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-09-24 15:08:04 +02:00
|
|
|
window.move_cursor_to(window.line_and_column_at(target));
|
2011-09-22 16:02:07 +02:00
|
|
|
}
|
|
|
|
|
2011-09-08 02:13:19 +02:00
|
|
|
Window* current_window;
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2011-09-07 20:16:56 +02:00
|
|
|
void edit(const CommandParameters& params)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-07 20:16:56 +02:00
|
|
|
if (params.size() != 1)
|
|
|
|
throw wrong_argument_count();
|
|
|
|
|
|
|
|
std::string filename = params[0];
|
2011-09-08 16:30:36 +02:00
|
|
|
Buffer* buffer = NULL;
|
2011-09-02 18:51:20 +02:00
|
|
|
try
|
|
|
|
{
|
2011-09-08 16:30:36 +02:00
|
|
|
buffer = create_buffer_from_file(filename);
|
2011-09-02 20:02:29 +02:00
|
|
|
}
|
|
|
|
catch (file_not_found& what)
|
|
|
|
{
|
2011-09-07 20:16:56 +02:00
|
|
|
print_status("new file " + filename);
|
2011-09-08 16:30:36 +02:00
|
|
|
buffer = new Buffer(filename);
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
2011-09-08 16:30:36 +02:00
|
|
|
current_window = buffer->get_or_create_window();
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
2011-09-07 20:16:56 +02:00
|
|
|
void write_buffer(const CommandParameters& params)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-07 20:16:56 +02:00
|
|
|
if (params.size() > 1)
|
|
|
|
throw wrong_argument_count();
|
|
|
|
|
2011-09-08 02:13:19 +02:00
|
|
|
Buffer& buffer = current_window->buffer();
|
2011-09-07 20:16:56 +02:00
|
|
|
std::string filename = params.empty() ? buffer.name() : params[0];
|
|
|
|
|
|
|
|
write_buffer_to_file(buffer, filename);
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool quit_requested = false;
|
|
|
|
|
2011-09-07 20:16:56 +02:00
|
|
|
void quit(const CommandParameters& params)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-07 20:16:56 +02:00
|
|
|
if (params.size() != 0)
|
|
|
|
throw wrong_argument_count();
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
quit_requested = true;
|
|
|
|
}
|
|
|
|
|
2011-09-08 16:32:36 +02:00
|
|
|
void show_buffer(const CommandParameters& params)
|
|
|
|
{
|
|
|
|
if (params.size() != 1)
|
|
|
|
throw wrong_argument_count();
|
|
|
|
|
|
|
|
Buffer* buffer = BufferManager::instance().get_buffer(params[0]);
|
|
|
|
if (not buffer)
|
|
|
|
print_status("buffer " + params[0] + " does not exists");
|
|
|
|
else
|
|
|
|
current_window = buffer->get_or_create_window();
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
void do_command()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-09-23 16:29:42 +02:00
|
|
|
auto cmdline = prompt(":", std::bind(&CommandManager::complete,
|
|
|
|
&CommandManager::instance(),
|
|
|
|
_1, _2));
|
|
|
|
|
|
|
|
CommandManager::instance().execute(cmdline);
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
catch (prompt_aborted&) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_search(Window& window)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
std::string ex = prompt("/");
|
2011-09-24 14:48:58 +02:00
|
|
|
if (ex.empty())
|
|
|
|
ex = RegisterManager::instance()['/'];
|
|
|
|
else
|
|
|
|
RegisterManager::instance()['/'] = ex;
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
window.select(false, RegexSelector(ex));
|
|
|
|
}
|
|
|
|
catch (prompt_aborted&) {}
|
|
|
|
}
|
|
|
|
|
2011-09-24 14:48:58 +02:00
|
|
|
void do_search_next(Window& window)
|
|
|
|
{
|
|
|
|
std::string& ex = RegisterManager::instance()['/'];
|
|
|
|
if (not ex.empty())
|
|
|
|
window.select(false, RegexSelector(ex));
|
|
|
|
else
|
|
|
|
print_status("no search pattern");
|
|
|
|
}
|
|
|
|
|
2011-09-23 16:31:57 +02:00
|
|
|
void do_yank(Window& window, int count)
|
|
|
|
{
|
|
|
|
RegisterManager::instance()['"'] = window.selection_content();
|
|
|
|
}
|
|
|
|
|
2011-09-26 10:59:32 +02:00
|
|
|
void do_erase(Window& window, int count)
|
|
|
|
{
|
|
|
|
do_yank(window, 0);
|
|
|
|
window.erase();
|
|
|
|
window.empty_selections();
|
|
|
|
}
|
|
|
|
|
|
|
|
void do_change(Window& window, int count)
|
|
|
|
{
|
|
|
|
do_yank(window, 0);
|
|
|
|
window.erase();
|
|
|
|
do_insert(window);
|
|
|
|
}
|
|
|
|
|
2011-09-23 16:31:57 +02:00
|
|
|
void do_paste(Window& window, int count)
|
|
|
|
{
|
|
|
|
window.append(RegisterManager::instance()['"']);
|
|
|
|
}
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
std::unordered_map<char, std::function<void (Window& window, int count)>> keymap =
|
|
|
|
{
|
2011-09-17 16:28:23 +02:00
|
|
|
{ 'h', [](Window& window, int count) { window.move_cursor(WindowCoord(0, -std::max(count,1))); window.empty_selections(); } },
|
|
|
|
{ 'j', [](Window& window, int count) { window.move_cursor(WindowCoord( std::max(count,1), 0)); window.empty_selections(); } },
|
|
|
|
{ 'k', [](Window& window, int count) { window.move_cursor(WindowCoord(-std::max(count,1), 0)); window.empty_selections(); } },
|
|
|
|
{ 'l', [](Window& window, int count) { window.move_cursor(WindowCoord(0, std::max(count,1))); window.empty_selections(); } },
|
|
|
|
|
2011-09-23 16:27:34 +02:00
|
|
|
{ 'H', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), _1,
|
2011-09-17 16:28:23 +02:00
|
|
|
WindowCoord(0, -std::max(count,1)))); } },
|
2011-09-23 16:27:34 +02:00
|
|
|
{ 'J', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), _1,
|
2011-09-17 16:28:23 +02:00
|
|
|
WindowCoord( std::max(count,1), 0))); } },
|
2011-09-23 16:27:34 +02:00
|
|
|
{ 'K', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), _1,
|
2011-09-17 16:28:23 +02:00
|
|
|
WindowCoord(-std::max(count,1), 0))); } },
|
2011-09-23 16:27:34 +02:00
|
|
|
{ 'L', [](Window& window, int count) { window.select(true, std::bind(move_select, std::ref(window), _1,
|
2011-09-17 16:28:23 +02:00
|
|
|
WindowCoord(0, std::max(count,1)))); } },
|
|
|
|
|
2011-09-23 16:27:34 +02:00
|
|
|
{ 't', [](Window& window, int count) { window.select(false, std::bind(select_to, _1, getch(), false)); } },
|
|
|
|
{ 'f', [](Window& window, int count) { window.select(false, std::bind(select_to, _1, getch(), true)); } },
|
2011-09-22 16:35:28 +02:00
|
|
|
|
2011-09-26 10:59:32 +02:00
|
|
|
{ 'd', do_erase },
|
|
|
|
{ 'c', do_change },
|
2011-09-02 18:51:20 +02:00
|
|
|
{ 'i', [](Window& window, int count) { do_insert(window); } },
|
2011-09-20 00:00:29 +02:00
|
|
|
{ 'a', [](Window& window, int count) { do_insert(window, true); } },
|
2011-09-22 11:24:16 +02:00
|
|
|
{ 'o', [](Window& window, int count) { window.select(true, select_line); window.append("\n"); do_insert(window, true); } },
|
|
|
|
|
2011-09-22 16:02:07 +02:00
|
|
|
{ 'g', do_go },
|
2011-09-22 11:24:16 +02:00
|
|
|
|
2011-09-23 16:31:57 +02:00
|
|
|
{ 'y', do_yank },
|
|
|
|
{ 'p', do_paste },
|
|
|
|
|
2011-09-24 15:45:25 +02:00
|
|
|
{ '%', [](Window& window, int count) { window.select(false, [](const BufferIterator& cursor)
|
|
|
|
{ return Selection(cursor.buffer().begin(), cursor.buffer().end()-1); }); } },
|
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
{ ':', [](Window& window, int count) { do_command(); } },
|
|
|
|
{ ' ', [](Window& window, int count) { window.empty_selections(); } },
|
|
|
|
{ 'w', [](Window& window, int count) { do { window.select(false, 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(true, select_to_next_word_end); } while(--count > 0); } },
|
2011-09-21 16:37:09 +02:00
|
|
|
{ '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); } },
|
2011-09-02 18:51:20 +02:00
|
|
|
{ '.', [](Window& window, int count) { do { window.select(false, select_line); } while(--count > 0); } },
|
2011-09-21 21:09:49 +02:00
|
|
|
{ 'm', [](Window& window, int count) { window.select(false, select_matching); } },
|
|
|
|
{ 'M', [](Window& window, int count) { window.select(true, select_matching); } },
|
2011-09-02 18:51:20 +02:00
|
|
|
{ '/', [](Window& window, int count) { do_search(window); } },
|
2011-09-24 14:48:58 +02:00
|
|
|
{ 'n', [](Window& window, int count) { do_search_next(window); } },
|
2011-09-06 20:53:12 +02:00
|
|
|
{ '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); } },
|
2011-09-02 18:51:20 +02:00
|
|
|
};
|
|
|
|
|
2011-09-21 16:37:09 +02:00
|
|
|
int main(int argc, char* argv[])
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
init_ncurses();
|
2011-09-23 16:29:42 +02:00
|
|
|
|
|
|
|
CommandManager command_manager;
|
|
|
|
BufferManager buffer_manager;
|
2011-09-23 16:31:57 +02:00
|
|
|
RegisterManager register_manager;
|
2011-09-23 16:29:42 +02:00
|
|
|
|
2011-09-16 11:20:01 +02:00
|
|
|
command_manager.register_command(std::vector<std::string>{ "e", "edit" }, edit,
|
|
|
|
PerArgumentCommandCompleter{ complete_filename });
|
2011-09-07 20:16:56 +02:00
|
|
|
command_manager.register_command(std::vector<std::string>{ "q", "quit" }, quit);
|
2011-09-16 11:20:01 +02:00
|
|
|
command_manager.register_command(std::vector<std::string>{ "w", "write" }, write_buffer,
|
|
|
|
PerArgumentCommandCompleter{ complete_filename });
|
2011-09-22 20:55:45 +02:00
|
|
|
command_manager.register_command(std::vector<std::string>{ "b", "buffer" }, show_buffer,
|
|
|
|
PerArgumentCommandCompleter { complete_buffername });
|
2011-09-07 20:16:56 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
try
|
|
|
|
{
|
2011-09-21 16:37:09 +02:00
|
|
|
auto buffer = (argc > 1) ? create_buffer_from_file(argv[1]) : new Buffer("<scratch>");
|
2011-09-08 16:30:36 +02:00
|
|
|
current_window = buffer->get_or_create_window();
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
draw_window(*current_window);
|
|
|
|
int count = 0;
|
|
|
|
while(not quit_requested)
|
|
|
|
{
|
2011-09-09 20:40:59 +02:00
|
|
|
try
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-09 20:40:59 +02:00
|
|
|
char c = getch();
|
|
|
|
|
|
|
|
if (isdigit(c))
|
|
|
|
count = count * 10 + c - '0';
|
|
|
|
else
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
2011-09-09 20:40:59 +02:00
|
|
|
if (keymap.find(c) != keymap.end())
|
|
|
|
{
|
|
|
|
keymap[c](*current_window, count);
|
|
|
|
draw_window(*current_window);
|
|
|
|
}
|
|
|
|
count = 0;
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
2011-09-09 20:40:59 +02:00
|
|
|
}
|
|
|
|
catch (Kakoune::runtime_error& error)
|
|
|
|
{
|
|
|
|
print_status(error.description());
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
deinit_ncurses();
|
|
|
|
}
|
2011-09-09 21:24:18 +02:00
|
|
|
catch (Kakoune::exception& error)
|
|
|
|
{
|
|
|
|
deinit_ncurses();
|
|
|
|
puts("uncaught exception:\n");
|
|
|
|
puts(error.description().c_str());
|
|
|
|
return -1;
|
|
|
|
}
|
2011-09-02 19:05:44 +02:00
|
|
|
catch (...)
|
2011-09-02 18:51:20 +02:00
|
|
|
{
|
|
|
|
deinit_ncurses();
|
2011-09-02 19:05:44 +02:00
|
|
|
throw;
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|