2013-04-09 20:05:40 +02:00
|
|
|
#include "assert.hh"
|
2011-09-02 18:51:20 +02:00
|
|
|
#include "buffer.hh"
|
2011-09-08 16:30:36 +02:00
|
|
|
#include "buffer_manager.hh"
|
2014-04-28 20:48:23 +02:00
|
|
|
#include "buffer_utils.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "client_manager.hh"
|
|
|
|
#include "command_manager.hh"
|
|
|
|
#include "commands.hh"
|
|
|
|
#include "context.hh"
|
2011-10-07 16:16:38 +02:00
|
|
|
#include "debug.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "event_manager.hh"
|
2014-07-11 01:27:04 +02:00
|
|
|
#include "face_registry.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "file.hh"
|
|
|
|
#include "highlighters.hh"
|
2012-02-16 15:25:16 +01:00
|
|
|
#include "ncurses.hh"
|
2012-12-18 21:41:13 +01:00
|
|
|
#include "parameters_parser.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "register_manager.hh"
|
|
|
|
#include "remote.hh"
|
|
|
|
#include "shell_manager.hh"
|
2014-10-30 15:00:42 +01:00
|
|
|
#include "scope.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "string.hh"
|
2014-10-30 15:00:42 +01:00
|
|
|
#include "insert_completer.hh"
|
2014-10-01 01:20:12 +02:00
|
|
|
#include "interned_string.hh"
|
2013-04-18 14:28:53 +02:00
|
|
|
#include "window.hh"
|
2011-09-02 18:51:20 +02:00
|
|
|
|
|
|
|
#include <unordered_map>
|
2013-02-26 14:12:21 +01:00
|
|
|
#include <locale>
|
2013-03-13 19:59:39 +01:00
|
|
|
#include <signal.h>
|
2013-02-26 14:12:21 +01:00
|
|
|
|
2014-04-29 22:37:11 +02:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <fcntl.h>
|
2014-04-30 20:08:06 +02:00
|
|
|
#include <unistd.h>
|
2014-04-29 22:37:11 +02:00
|
|
|
|
2011-09-02 18:51:20 +02:00
|
|
|
using namespace Kakoune;
|
|
|
|
|
2013-04-12 14:28:13 +02:00
|
|
|
void run_unit_tests();
|
2012-11-12 19:59:25 +01:00
|
|
|
|
2012-09-10 20:10:18 +02:00
|
|
|
String runtime_directory()
|
|
|
|
{
|
2014-10-30 01:50:40 +01:00
|
|
|
String bin_path = get_kak_binary_path();
|
|
|
|
size_t last_slash = bin_path.find_last_of('/');
|
|
|
|
if (last_slash == String::npos)
|
2012-11-08 13:38:02 +01:00
|
|
|
throw runtime_error("unable to determine runtime directory");
|
2014-10-30 01:50:40 +01:00
|
|
|
return bin_path.substr(0_byte, (int)last_slash) + "/../share/kak";
|
2012-09-10 20:10:18 +02:00
|
|
|
}
|
|
|
|
|
2012-10-23 22:56:24 +02:00
|
|
|
void register_env_vars()
|
|
|
|
{
|
2014-01-12 22:25:21 +01:00
|
|
|
static const struct {
|
|
|
|
const char* name;
|
2014-04-20 13:15:31 +02:00
|
|
|
String (*func)(StringView, const Context&);
|
2014-01-12 22:25:21 +01:00
|
|
|
} env_vars[] = { {
|
2013-10-01 19:47:37 +02:00
|
|
|
"bufname",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return context.buffer().display_name(); }
|
2014-02-27 07:43:21 +01:00
|
|
|
}, {
|
|
|
|
"buffile",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context) -> String
|
2014-02-27 07:43:21 +01:00
|
|
|
{ return context.buffer().name(); }
|
2013-10-01 19:47:37 +02:00
|
|
|
}, {
|
|
|
|
"timestamp",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return to_string(context.buffer().timestamp()); }
|
|
|
|
}, {
|
|
|
|
"selection",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2014-03-29 09:55:45 +01:00
|
|
|
{ const Selection& sel = context.selections().main();
|
2013-10-01 19:47:37 +02:00
|
|
|
return content(context.buffer(), sel); }
|
|
|
|
}, {
|
|
|
|
"selections",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-12-15 21:37:07 +01:00
|
|
|
{ auto sels = context.selections_content();
|
2013-10-01 19:47:37 +02:00
|
|
|
String res;
|
|
|
|
for (size_t i = 0; i < sels.size(); ++i)
|
|
|
|
{
|
|
|
|
res += escape(sels[i], ':', '\\');
|
|
|
|
if (i != sels.size() - 1)
|
|
|
|
res += ':';
|
|
|
|
}
|
|
|
|
return res; }
|
|
|
|
}, {
|
|
|
|
"runtime",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return runtime_directory(); }
|
|
|
|
}, {
|
|
|
|
"opt_.+",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return context.options()[name.substr(4_byte)].get_as_string(); }
|
|
|
|
}, {
|
|
|
|
"reg_.+",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context) -> String
|
2014-06-21 12:31:08 +02:00
|
|
|
{ return context.main_sel_register_value(name.substr(4_byte)); }
|
2014-04-07 22:25:44 +02:00
|
|
|
}, {
|
|
|
|
"client_env_.+",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context) -> String
|
2014-04-07 22:25:44 +02:00
|
|
|
{ return context.client().get_env_var(name.substr(11_byte)); }
|
2013-10-01 19:47:37 +02:00
|
|
|
}, {
|
|
|
|
"session",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context) -> String
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return Server::instance().session(); }
|
|
|
|
}, {
|
|
|
|
"client",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context) -> String
|
2013-11-14 21:51:25 +01:00
|
|
|
{ return context.name(); }
|
2013-10-01 19:47:37 +02:00
|
|
|
}, {
|
|
|
|
"cursor_line",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2014-01-28 20:05:49 +01:00
|
|
|
{ return to_string(context.selections().main().cursor().line + 1); }
|
2013-10-01 19:47:37 +02:00
|
|
|
}, {
|
|
|
|
"cursor_column",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2014-01-28 20:05:49 +01:00
|
|
|
{ return to_string(context.selections().main().cursor().column + 1); }
|
2013-12-11 14:46:33 +01:00
|
|
|
}, {
|
|
|
|
"cursor_char_column",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2014-01-28 20:05:49 +01:00
|
|
|
{ auto coord = context.selections().main().cursor();
|
2013-12-11 14:46:33 +01:00
|
|
|
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
|
2013-10-01 19:47:37 +02:00
|
|
|
}, {
|
|
|
|
"selection_desc",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-12-15 15:25:23 +01:00
|
|
|
{ auto& sel = context.selections().main();
|
2013-10-01 19:47:37 +02:00
|
|
|
auto beg = sel.min();
|
2014-05-25 19:26:31 +02:00
|
|
|
return to_string(beg.line + 1) + '.' + to_string(beg.column + 1) + '+' +
|
2013-10-01 19:47:37 +02:00
|
|
|
to_string((int)context.buffer().distance(beg, sel.max())+1); }
|
2014-05-25 19:26:31 +02:00
|
|
|
}, {
|
|
|
|
"selections_desc",
|
|
|
|
[](StringView name, const Context& context)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (auto& sel : context.selections())
|
|
|
|
{
|
|
|
|
auto beg = sel.min();
|
|
|
|
if (not res.empty())
|
|
|
|
res += ':';
|
|
|
|
res += to_string(beg.line + 1) + '.' + to_string(beg.column + 1) + '+' +
|
|
|
|
to_string((int)context.buffer().distance(beg, sel.max())+1);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
2013-10-01 19:47:37 +02:00
|
|
|
}, {
|
|
|
|
"window_width",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return to_string(context.window().dimensions().column); }
|
|
|
|
}, {
|
|
|
|
"window_height",
|
2014-04-20 13:15:31 +02:00
|
|
|
[](StringView name, const Context& context)
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return to_string(context.window().dimensions().line); }
|
|
|
|
} };
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2013-10-01 19:47:37 +02:00
|
|
|
ShellManager& shell_manager = ShellManager::instance();
|
|
|
|
for (auto& env_var : env_vars)
|
|
|
|
shell_manager.register_env_var(env_var.name, env_var.func);
|
2012-10-23 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void register_registers()
|
|
|
|
{
|
2013-10-01 19:52:19 +02:00
|
|
|
using StringList = std::vector<String>;
|
2014-03-22 22:00:24 +01:00
|
|
|
static const struct {
|
|
|
|
char name;
|
|
|
|
StringList (*func)(const Context&);
|
|
|
|
} dyn_regs[] = {
|
2013-10-01 19:52:19 +02:00
|
|
|
{ '%', [](const Context& context) { return StringList{{context.buffer().display_name()}}; } },
|
2013-12-15 21:37:07 +01:00
|
|
|
{ '.', [](const Context& context) { return context.selections_content(); } },
|
2014-05-23 21:27:35 +02:00
|
|
|
{ '#', [](const Context& context) {
|
|
|
|
StringList res;
|
2014-07-14 20:00:54 +02:00
|
|
|
for (size_t i = 1; i < context.selections().size()+1; ++i)
|
2014-05-23 21:27:35 +02:00
|
|
|
res.push_back(to_string((int)i));
|
|
|
|
return res;
|
|
|
|
} }
|
2013-10-01 19:52:19 +02:00
|
|
|
};
|
|
|
|
|
2012-10-23 22:56:24 +02:00
|
|
|
RegisterManager& register_manager = RegisterManager::instance();
|
2013-10-01 19:52:19 +02:00
|
|
|
for (auto& dyn_reg : dyn_regs)
|
|
|
|
register_manager.register_dynamic_register(dyn_reg.name, dyn_reg.func);
|
2012-10-23 22:56:24 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
{
|
2013-01-04 18:39:13 +01:00
|
|
|
register_manager.register_dynamic_register('0'+i,
|
|
|
|
[i](const Context& context) {
|
|
|
|
std::vector<String> result;
|
2013-12-15 15:25:23 +01:00
|
|
|
for (auto& sel : context.selections())
|
2013-01-04 18:39:13 +01:00
|
|
|
result.emplace_back(i < sel.captures().size() ? sel.captures()[i] : "");
|
|
|
|
return result;
|
|
|
|
});
|
2012-10-23 22:56:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-30 15:00:42 +01:00
|
|
|
void register_options()
|
|
|
|
{
|
|
|
|
OptionsRegistry& reg = GlobalScope::instance().option_registry();
|
|
|
|
|
|
|
|
reg.declare_option("tabstop", "size of a tab character", 8);
|
|
|
|
reg.declare_option("indentwidth", "indentation width", 4);
|
|
|
|
reg.declare_option("scrolloff",
|
|
|
|
"number of lines and columns to keep visible main cursor when scrolling",
|
|
|
|
CharCoord{0,0});
|
|
|
|
reg.declare_option("eolformat", "end of line format: 'crlf' or 'lf'", "lf"_str);
|
|
|
|
reg.declare_option("BOM", "insert a byte order mark when writing buffer",
|
|
|
|
"no"_str);
|
|
|
|
reg.declare_option("complete_prefix",
|
|
|
|
"complete up to common prefix in tab completion",
|
|
|
|
true);
|
|
|
|
reg.declare_option("incsearch",
|
|
|
|
"incrementaly apply search/select/split regex",
|
|
|
|
true);
|
|
|
|
reg.declare_option("autoinfo",
|
|
|
|
"automatically display contextual help",
|
|
|
|
1);
|
|
|
|
reg.declare_option("autoshowcompl",
|
|
|
|
"automatically display possible completions for prompts",
|
|
|
|
true);
|
|
|
|
reg.declare_option("aligntab",
|
|
|
|
"use tab characters when possible for alignement",
|
|
|
|
false);
|
|
|
|
reg.declare_option("ignored_files",
|
|
|
|
"patterns to ignore when completing filenames",
|
|
|
|
Regex{R"(^(\..*|.*\.(o|so|a))$)"});
|
|
|
|
reg.declare_option("disabled_hooks",
|
|
|
|
"patterns to disable hooks whose group is matched",
|
|
|
|
Regex{});
|
|
|
|
reg.declare_option("filetype", "buffer filetype", ""_str);
|
|
|
|
reg.declare_option("path", "path to consider when trying to find a file",
|
|
|
|
std::vector<String>({ "./", "/usr/include" }));
|
|
|
|
reg.declare_option("completers", "insert mode completers to execute.",
|
|
|
|
std::vector<InsertCompleterDesc>({
|
|
|
|
InsertCompleterDesc{ InsertCompleterDesc::Filename },
|
|
|
|
InsertCompleterDesc{ InsertCompleterDesc::Word, "all"_str }
|
|
|
|
}), OptionFlags::None);
|
|
|
|
reg.declare_option("autoreload",
|
|
|
|
"autoreload buffer when a filesystem modification is detected",
|
|
|
|
Ask);
|
2014-11-11 00:29:16 +01:00
|
|
|
reg.declare_option("ui_options",
|
|
|
|
"options passed to UI as a string map",
|
|
|
|
UserInterface::Options());
|
2014-10-30 15:00:42 +01:00
|
|
|
}
|
|
|
|
|
2012-12-28 13:51:14 +01:00
|
|
|
void create_local_client(const String& init_command)
|
2012-10-17 17:49:34 +02:00
|
|
|
{
|
2012-11-29 18:56:08 +01:00
|
|
|
class LocalNCursesUI : public NCursesUI
|
|
|
|
{
|
|
|
|
~LocalNCursesUI()
|
|
|
|
{
|
|
|
|
if (not ClientManager::instance().empty() and fork())
|
|
|
|
{
|
2013-04-03 18:51:40 +02:00
|
|
|
this->NCursesUI::~NCursesUI();
|
2012-11-29 18:56:08 +01:00
|
|
|
puts("detached from terminal\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-04-29 22:37:11 +02:00
|
|
|
if (not isatty(1))
|
|
|
|
throw runtime_error("stdout is not a tty");
|
|
|
|
|
|
|
|
if (not isatty(0))
|
|
|
|
{
|
|
|
|
// move stdin to another fd, and restore tty as stdin
|
|
|
|
int fd = dup(0);
|
|
|
|
int tty = open("/dev/tty", O_RDONLY);
|
|
|
|
dup2(tty, 0);
|
|
|
|
close(tty);
|
|
|
|
create_fifo_buffer("*stdin*", fd);
|
|
|
|
}
|
|
|
|
|
2012-11-29 18:56:08 +01:00
|
|
|
UserInterface* ui = new LocalNCursesUI{};
|
2013-09-12 23:47:23 +02:00
|
|
|
static Client* client = ClientManager::instance().create_client(
|
2014-04-07 22:25:44 +02:00
|
|
|
std::unique_ptr<UserInterface>{ui}, get_env_vars(), init_command);
|
2013-04-15 18:50:45 +02:00
|
|
|
signal(SIGHUP, [](int) {
|
|
|
|
if (client)
|
|
|
|
ClientManager::instance().remove_client(*client);
|
|
|
|
client = nullptr;
|
|
|
|
});
|
2012-10-17 17:49:34 +02:00
|
|
|
}
|
|
|
|
|
2013-02-26 14:13:37 +01:00
|
|
|
void signal_handler(int signal)
|
2013-01-23 13:46:18 +01:00
|
|
|
{
|
2013-04-12 01:28:22 +02:00
|
|
|
NCursesUI::abort();
|
2013-02-26 14:13:37 +01:00
|
|
|
const char* text = nullptr;
|
|
|
|
switch (signal)
|
|
|
|
{
|
|
|
|
case SIGSEGV: text = "SIGSEGV"; break;
|
|
|
|
case SIGFPE: text = "SIGFPE"; break;
|
|
|
|
case SIGQUIT: text = "SIGQUIT"; break;
|
|
|
|
case SIGTERM: text = "SIGTERM"; break;
|
|
|
|
}
|
2014-10-13 14:38:28 +02:00
|
|
|
if (signal != SIGTERM)
|
|
|
|
on_assert_failed(text);
|
|
|
|
|
2014-01-27 20:53:17 +01:00
|
|
|
if (Server::has_instance())
|
|
|
|
Server::instance().close_session();
|
2014-10-13 14:38:28 +02:00
|
|
|
if (BufferManager::has_instance())
|
|
|
|
BufferManager::instance().backup_modified_buffers();
|
|
|
|
|
|
|
|
if (signal == SIGTERM)
|
|
|
|
exit(-1);
|
|
|
|
else
|
|
|
|
abort();
|
2013-01-23 13:46:18 +01:00
|
|
|
}
|
|
|
|
|
2014-08-14 21:37:36 +02:00
|
|
|
int run_client(StringView session, StringView init_command)
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
EventManager event_manager;
|
2014-11-05 14:57:12 +01:00
|
|
|
RemoteClient client{session, make_unique<NCursesUI>(),
|
|
|
|
get_env_vars(), init_command};
|
2013-09-23 20:28:15 +02:00
|
|
|
while (true)
|
|
|
|
event_manager.handle_next_events();
|
|
|
|
}
|
|
|
|
catch (peer_disconnected&)
|
|
|
|
{
|
|
|
|
fputs("disconnected from server\n", stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-03-21 14:42:37 +01:00
|
|
|
catch (connection_failed& e)
|
|
|
|
{
|
|
|
|
fputs(e.what(), stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:37:36 +02:00
|
|
|
int run_server(StringView session, StringView init_command,
|
|
|
|
bool ignore_kakrc, bool daemon, memoryview<StringView> files)
|
2012-10-21 13:02:24 +02:00
|
|
|
{
|
2013-09-23 20:28:15 +02:00
|
|
|
static bool terminate = false;
|
|
|
|
if (daemon)
|
|
|
|
{
|
2014-08-14 21:37:36 +02:00
|
|
|
if (session.empty())
|
2013-09-19 20:53:04 +02:00
|
|
|
{
|
2013-09-23 20:28:15 +02:00
|
|
|
fputs("-d needs a session name to be specified with -s\n", stderr);
|
2013-09-19 20:53:04 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
if (pid_t child = fork())
|
2013-09-19 21:09:53 +02:00
|
|
|
{
|
2013-09-23 20:28:15 +02:00
|
|
|
printf("Kakoune forked to background, for session '%s'\n"
|
|
|
|
"send SIGTERM to process %d for closing the session\n",
|
2014-08-14 21:37:36 +02:00
|
|
|
(const char*)session.zstr(), child);
|
2013-09-23 20:28:15 +02:00
|
|
|
exit(0);
|
2013-09-19 21:09:53 +02:00
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
signal(SIGTERM, [](int) { terminate = true; });
|
|
|
|
}
|
2013-09-19 21:09:53 +02:00
|
|
|
|
2014-10-01 01:20:12 +02:00
|
|
|
StringRegistry string_registry;
|
2013-09-23 20:28:15 +02:00
|
|
|
EventManager event_manager;
|
2014-10-30 15:00:42 +01:00
|
|
|
GlobalScope global_scope;
|
2013-09-23 20:28:15 +02:00
|
|
|
ShellManager shell_manager;
|
|
|
|
CommandManager command_manager;
|
|
|
|
BufferManager buffer_manager;
|
|
|
|
RegisterManager register_manager;
|
|
|
|
HighlighterRegistry highlighter_registry;
|
2013-12-03 23:03:10 +01:00
|
|
|
DefinedHighlighters defined_highlighters;
|
2014-07-11 01:27:04 +02:00
|
|
|
FaceRegistry face_registry;
|
2013-09-23 20:28:15 +02:00
|
|
|
ClientManager client_manager;
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
run_unit_tests();
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2014-10-30 15:00:42 +01:00
|
|
|
register_options();
|
2013-09-23 20:28:15 +02:00
|
|
|
register_env_vars();
|
|
|
|
register_registers();
|
|
|
|
register_commands();
|
|
|
|
register_highlighters();
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
write_debug("*** This is the debug buffer, where debug info will be written ***");
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2014-08-14 21:37:36 +02:00
|
|
|
Server server(session.empty() ? to_string(getpid()) : String{session});
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2014-08-14 21:37:36 +02:00
|
|
|
if (not ignore_kakrc) try
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
|
|
|
Context initialisation_context;
|
|
|
|
command_manager.execute("source " + runtime_directory() + "/kakrc",
|
|
|
|
initialisation_context);
|
|
|
|
}
|
|
|
|
catch (Kakoune::runtime_error& error)
|
|
|
|
{
|
2014-05-07 21:39:59 +02:00
|
|
|
write_debug("error while parsing kakrc:\n "_str + error.what());
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
|
|
|
catch (Kakoune::client_removed&)
|
|
|
|
{
|
2013-10-26 20:23:00 +02:00
|
|
|
write_debug("error while parsing kakrc: asked to quit");
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2012-10-16 17:15:09 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
|
|
|
Context empty_context;
|
2014-10-30 15:00:42 +01:00
|
|
|
global_scope.hooks().run_hook("KakBegin", "", empty_context);
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2013-04-19 13:45:44 +02:00
|
|
|
|
2014-08-14 21:37:36 +02:00
|
|
|
if (not files.empty()) try
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
|
|
|
// create buffers in reverse order so that the first given buffer
|
|
|
|
// is the most recently created one.
|
2014-08-14 21:37:36 +02:00
|
|
|
for (auto& file : reversed(files))
|
2013-04-29 13:50:13 +02:00
|
|
|
{
|
2013-09-23 20:28:15 +02:00
|
|
|
if (not create_buffer_from_file(file))
|
|
|
|
new Buffer(file, Buffer::Flags::New | Buffer::Flags::File);
|
2013-04-29 13:50:13 +02:00
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
|
|
|
catch (Kakoune::runtime_error& error)
|
|
|
|
{
|
|
|
|
write_debug("error while opening command line files: "_str + error.what());
|
|
|
|
}
|
|
|
|
else
|
|
|
|
new Buffer("*scratch*", Buffer::Flags::None);
|
2012-12-28 13:51:14 +01:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
if (not daemon)
|
|
|
|
create_local_client(init_command);
|
2012-10-30 14:00:44 +01:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
while (not terminate and (not client_manager.empty() or daemon))
|
2014-08-12 20:24:09 +02:00
|
|
|
{
|
2013-09-23 20:28:15 +02:00
|
|
|
event_manager.handle_next_events();
|
2014-08-12 20:24:09 +02:00
|
|
|
client_manager.clear_mode_trashes();
|
|
|
|
buffer_manager.clear_buffer_trash();
|
|
|
|
client_manager.redraw_clients();
|
|
|
|
}
|
2013-04-19 13:45:44 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
|
|
|
Context empty_context;
|
2014-10-30 15:00:42 +01:00
|
|
|
global_scope.hooks().run_hook("KakEnd", "", empty_context);
|
2013-09-19 20:53:04 +02:00
|
|
|
}
|
2014-08-14 21:37:36 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
return 0;
|
2013-09-19 20:53:04 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 00:40:07 +01:00
|
|
|
int run_filter(StringView keystr, memoryview<StringView> files, bool quiet)
|
2014-08-15 00:51:24 +02:00
|
|
|
{
|
2014-10-27 18:54:20 +01:00
|
|
|
StringRegistry string_registry;
|
2014-10-30 15:00:42 +01:00
|
|
|
GlobalScope global_scope;
|
2014-10-26 21:21:01 +01:00
|
|
|
ShellManager shell_manager;
|
|
|
|
BufferManager buffer_manager;
|
|
|
|
RegisterManager register_manager;
|
2014-08-15 00:51:24 +02:00
|
|
|
|
2014-10-30 15:00:42 +01:00
|
|
|
register_options();
|
2014-08-15 00:51:24 +02:00
|
|
|
register_env_vars();
|
|
|
|
register_registers();
|
|
|
|
|
2014-08-17 16:19:04 +02:00
|
|
|
try
|
2014-08-15 00:51:24 +02:00
|
|
|
{
|
2014-08-17 16:19:04 +02:00
|
|
|
auto keys = parse_keys(keystr);
|
2014-08-15 00:51:24 +02:00
|
|
|
|
2014-08-17 16:19:04 +02:00
|
|
|
auto apply_keys_to_buffer = [&](Buffer& buffer)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-09-29 00:58:34 +02:00
|
|
|
InputHandler input_handler{{ buffer, Selection{{0,0}, buffer.back_coord()} }};
|
2014-08-15 00:51:24 +02:00
|
|
|
|
2014-08-17 16:19:04 +02:00
|
|
|
for (auto& key : keys)
|
|
|
|
input_handler.handle_key(key);
|
|
|
|
}
|
|
|
|
catch (Kakoune::runtime_error& err)
|
|
|
|
{
|
2014-11-12 00:40:07 +01:00
|
|
|
if (not quiet)
|
|
|
|
fprintf(stderr, "error while applying keys to buffer '%s': %s\n",
|
|
|
|
buffer.display_name().c_str(), err.what());
|
2014-08-17 16:19:04 +02:00
|
|
|
}
|
|
|
|
};
|
2014-08-15 00:51:24 +02:00
|
|
|
|
2014-08-17 16:19:04 +02:00
|
|
|
for (auto& file : files)
|
|
|
|
{
|
|
|
|
Buffer* buffer = create_buffer_from_file(file);
|
2014-11-12 00:40:07 +01:00
|
|
|
write_buffer_to_file(*buffer, file + ".kak-bak");
|
2014-08-17 16:19:04 +02:00
|
|
|
apply_keys_to_buffer(*buffer);
|
2014-11-12 00:40:07 +01:00
|
|
|
write_buffer_to_file(*buffer, file);
|
2014-08-17 16:19:04 +02:00
|
|
|
buffer_manager.delete_buffer(*buffer);
|
|
|
|
}
|
|
|
|
if (not isatty(0))
|
|
|
|
{
|
|
|
|
Buffer* buffer = create_buffer_from_data(read_fd(0), "*stdin*",
|
|
|
|
Buffer::Flags::None);
|
|
|
|
apply_keys_to_buffer(*buffer);
|
|
|
|
write_buffer_to_fd(*buffer, 1);
|
|
|
|
buffer_manager.delete_buffer(*buffer);
|
|
|
|
}
|
2014-08-15 00:51:24 +02:00
|
|
|
}
|
2014-08-17 16:19:04 +02:00
|
|
|
catch (Kakoune::runtime_error& err)
|
2014-08-15 14:21:54 +02:00
|
|
|
{
|
2014-08-17 16:19:04 +02:00
|
|
|
fprintf(stderr, "error: %s\n", err.what());
|
2014-08-15 14:21:54 +02:00
|
|
|
}
|
|
|
|
|
2014-08-15 00:51:24 +02:00
|
|
|
buffer_manager.clear_buffer_trash();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-14 21:37:36 +02:00
|
|
|
int run_pipe(StringView session)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
String command;
|
|
|
|
while (ssize_t count = read(0, buf, 512))
|
|
|
|
{
|
|
|
|
if (count < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error while reading stdin\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
command += String{buf, buf + count};
|
|
|
|
}
|
|
|
|
try
|
|
|
|
{
|
|
|
|
send_command(session, command);
|
|
|
|
}
|
|
|
|
catch (connection_failed& e)
|
|
|
|
{
|
|
|
|
fputs(e.what(), stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:53:04 +02:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2014-02-11 23:16:17 +01:00
|
|
|
setlocale(LC_ALL, "");
|
2013-09-19 20:53:04 +02:00
|
|
|
|
2014-02-11 23:16:17 +01:00
|
|
|
signal(SIGSEGV, signal_handler);
|
|
|
|
signal(SIGFPE, signal_handler);
|
|
|
|
signal(SIGQUIT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
2013-09-19 20:53:04 +02:00
|
|
|
|
2014-02-11 23:16:17 +01:00
|
|
|
std::vector<String> params;
|
|
|
|
for (size_t i = 1; i < argc; ++i)
|
|
|
|
params.push_back(argv[i]);
|
2013-09-19 20:53:04 +02:00
|
|
|
|
2014-02-11 23:16:17 +01:00
|
|
|
const ParameterDesc param_desc{
|
2014-02-11 23:23:44 +01:00
|
|
|
SwitchMap{ { "c", { true, "connect to given session" } },
|
2014-02-11 23:16:17 +01:00
|
|
|
{ "e", { true, "execute argument on initialisation" } },
|
|
|
|
{ "n", { false, "do not source kakrc files on startup" } },
|
|
|
|
{ "s", { true, "set session name" } },
|
2014-03-02 03:01:09 +01:00
|
|
|
{ "d", { false, "run as a headless session (requires -s)" } },
|
2014-08-15 00:51:24 +02:00
|
|
|
{ "p", { true, "just send stdin as commands to the given session" } },
|
2014-11-12 00:40:07 +01:00
|
|
|
{ "f", { true, "act as a filter, executing given keys on given files" } },
|
|
|
|
{ "q", { false, "in filter mode, be quiet about errors applying keys" } } }
|
2014-02-11 23:16:17 +01:00
|
|
|
};
|
|
|
|
try
|
|
|
|
{
|
2014-08-15 00:57:13 +02:00
|
|
|
ParametersParser parser(params, param_desc);
|
|
|
|
|
|
|
|
if (parser.has_option("p"))
|
|
|
|
{
|
|
|
|
for (auto opt : { "c", "n", "s", "d", "e" })
|
|
|
|
{
|
|
|
|
if (parser.has_option(opt))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: -%s makes not sense with -p\n", opt);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return run_pipe(parser.option_value("p"));
|
|
|
|
}
|
|
|
|
else if (parser.has_option("f"))
|
|
|
|
{
|
|
|
|
std::vector<StringView> files;
|
|
|
|
for (size_t i = 0; i < parser.positional_count(); ++i)
|
|
|
|
files.emplace_back(parser[i]);
|
|
|
|
|
2014-11-12 00:40:07 +01:00
|
|
|
return run_filter(parser.option_value("f"), files,
|
|
|
|
parser.has_option("q"));
|
2014-08-15 00:57:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
String init_command;
|
|
|
|
if (parser.has_option("e"))
|
|
|
|
init_command = parser.option_value("e");
|
|
|
|
|
|
|
|
if (parser.has_option("c"))
|
|
|
|
{
|
|
|
|
for (auto opt : { "n", "s", "d" })
|
|
|
|
{
|
|
|
|
if (parser.has_option(opt))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "error: -%s makes not sense with -c\n", opt);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return run_client(parser.option_value("c"), init_command);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
std::vector<StringView> files;
|
|
|
|
for (size_t i = 0; i < parser.positional_count(); ++i)
|
|
|
|
files.emplace_back(parser[i]);
|
|
|
|
StringView session;
|
|
|
|
if (parser.has_option("s"))
|
|
|
|
session = parser.option_value("s");
|
|
|
|
|
|
|
|
return run_server(session, init_command,
|
|
|
|
parser.has_option("n"),
|
2014-09-18 19:43:42 +02:00
|
|
|
parser.has_option("d"),
|
2014-08-15 00:57:13 +02:00
|
|
|
files);
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
2014-01-23 20:36:07 +01:00
|
|
|
catch (Kakoune::parameter_error& error)
|
|
|
|
{
|
|
|
|
printf("Error: %s\n"
|
2014-02-11 23:23:44 +01:00
|
|
|
"Valid switches:\n"
|
2014-02-11 23:16:17 +01:00
|
|
|
"%s",
|
2014-08-15 00:57:13 +02:00
|
|
|
error.what(),
|
|
|
|
generate_switches_doc(param_desc.switches).c_str());
|
2014-01-23 20:36:07 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-09-09 21:24:18 +02:00
|
|
|
catch (Kakoune::exception& error)
|
|
|
|
{
|
2013-04-11 13:57:35 +02:00
|
|
|
on_assert_failed(("uncaught exception:\n"_str + error.what()).c_str());
|
2013-02-22 18:45:59 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-03-22 14:29:22 +01:00
|
|
|
catch (std::exception& error)
|
|
|
|
{
|
|
|
|
on_assert_failed(("uncaught exception:\n"_str + error.what()).c_str());
|
|
|
|
return -1;
|
|
|
|
}
|
2013-02-22 18:45:59 +01:00
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
on_assert_failed("uncaught exception");
|
2011-09-09 21:24:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|