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"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "client_manager.hh"
|
|
|
|
#include "color_registry.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"
|
|
|
|
#include "file.hh"
|
|
|
|
#include "highlighters.hh"
|
2012-04-03 14:01:01 +02:00
|
|
|
#include "hook_manager.hh"
|
2012-02-16 15:25:16 +01:00
|
|
|
#include "ncurses.hh"
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "option_manager.hh"
|
2013-10-25 01:01:17 +02:00
|
|
|
#include "keymap_manager.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"
|
|
|
|
#include "string.hh"
|
2013-04-18 14:28:53 +02:00
|
|
|
#include "window.hh"
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2012-10-02 10:36:28 +02:00
|
|
|
#if defined(__APPLE__)
|
|
|
|
#include <mach-o/dyld.h>
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
|
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()
|
|
|
|
{
|
|
|
|
char buffer[2048];
|
2013-06-18 22:13:40 +02:00
|
|
|
#if defined(__linux__) || defined(__CYGWIN__)
|
2012-09-10 20:10:18 +02:00
|
|
|
ssize_t res = readlink("/proc/self/exe", buffer, 2048);
|
2013-04-09 20:04:11 +02:00
|
|
|
kak_assert(res != -1);
|
2012-09-10 20:10:18 +02:00
|
|
|
buffer[res] = '\0';
|
|
|
|
#elif defined(__APPLE__)
|
|
|
|
uint32_t bufsize = 2048;
|
|
|
|
_NSGetExecutablePath(buffer, &bufsize);
|
2013-07-28 16:40:02 +02:00
|
|
|
char* canonical_path = realpath(buffer, nullptr);
|
2012-09-10 20:10:18 +02:00
|
|
|
strncpy(buffer, canonical_path, 2048);
|
|
|
|
free(canonical_path);
|
|
|
|
#else
|
|
|
|
# error "finding executable path is not implemented on this platform"
|
|
|
|
#endif
|
|
|
|
char* ptr = strrchr(buffer, '/');
|
|
|
|
if (not ptr)
|
2012-11-08 13:38:02 +01:00
|
|
|
throw runtime_error("unable to determine runtime directory");
|
2013-12-23 22:26:07 +01:00
|
|
|
return String(buffer, ptr) + "/../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;
|
|
|
|
String (*func)(const String&, const Context&);
|
|
|
|
} env_vars[] = { {
|
2013-10-01 19:47:37 +02:00
|
|
|
"bufname",
|
|
|
|
[](const String& name, const Context& context)
|
|
|
|
{ return context.buffer().display_name(); }
|
|
|
|
}, {
|
|
|
|
"timestamp",
|
|
|
|
[](const String& name, const Context& context)
|
|
|
|
{ return to_string(context.buffer().timestamp()); }
|
|
|
|
}, {
|
|
|
|
"selection",
|
|
|
|
[](const String& name, const Context& context)
|
2013-12-15 15:25:23 +01:00
|
|
|
{ const Range& sel = context.selections().main();
|
2013-10-01 19:47:37 +02:00
|
|
|
return content(context.buffer(), sel); }
|
|
|
|
}, {
|
|
|
|
"selections",
|
|
|
|
[](const String& 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",
|
|
|
|
[](const String& name, const Context& context)
|
|
|
|
{ return runtime_directory(); }
|
|
|
|
}, {
|
|
|
|
"opt_.+",
|
|
|
|
[](const String& name, const Context& context)
|
|
|
|
{ return context.options()[name.substr(4_byte)].get_as_string(); }
|
|
|
|
}, {
|
|
|
|
"reg_.+",
|
2014-01-27 21:28:38 +01:00
|
|
|
[](const String& name, const Context& context) -> String
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return RegisterManager::instance()[name[4]].values(context)[0]; }
|
|
|
|
}, {
|
|
|
|
"session",
|
2014-01-27 21:28:38 +01:00
|
|
|
[](const String& name, const Context& context) -> String
|
2013-10-01 19:47:37 +02:00
|
|
|
{ return Server::instance().session(); }
|
|
|
|
}, {
|
|
|
|
"client",
|
2014-01-27 21:28:38 +01:00
|
|
|
[](const String& 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",
|
|
|
|
[](const String& 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",
|
|
|
|
[](const String& 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",
|
|
|
|
[](const String& 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",
|
|
|
|
[](const String& 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();
|
|
|
|
return to_string(beg.line + 1) + ':' + to_string(beg.column + 1) + '+' +
|
|
|
|
to_string((int)context.buffer().distance(beg, sel.max())+1); }
|
|
|
|
}, {
|
|
|
|
"window_width",
|
|
|
|
[](const String& name, const Context& context)
|
|
|
|
{ return to_string(context.window().dimensions().column); }
|
|
|
|
}, {
|
|
|
|
"window_height",
|
|
|
|
[](const String& name, const Context& context)
|
|
|
|
{ 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>;
|
|
|
|
struct DynRegDesc { char name; StringList (*func)(const Context&); };
|
|
|
|
static const DynRegDesc dyn_regs[] = {
|
|
|
|
{ '%', [](const Context& context) { return StringList{{context.buffer().display_name()}}; } },
|
2013-12-15 21:37:07 +01:00
|
|
|
{ '.', [](const Context& context) { return context.selections_content(); } },
|
2013-12-15 15:25:23 +01:00
|
|
|
{ '#', [](const Context& context) { return StringList{{to_string((int)context.selections().size())}}; } },
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
UserInterface* ui = new LocalNCursesUI{};
|
2013-09-12 23:47:23 +02:00
|
|
|
static Client* client = ClientManager::instance().create_client(
|
2013-01-11 19:17:21 +01:00
|
|
|
std::unique_ptr<UserInterface>{ui}, 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;
|
|
|
|
}
|
|
|
|
on_assert_failed(text);
|
2014-01-27 20:53:17 +01:00
|
|
|
if (Server::has_instance())
|
|
|
|
Server::instance().close_session();
|
2013-01-23 13:46:18 +01:00
|
|
|
abort();
|
|
|
|
}
|
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
int run_client(const String& session, const String& init_command)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
EventManager event_manager;
|
|
|
|
auto client = connect_to(session,
|
|
|
|
std::unique_ptr<UserInterface>{new NCursesUI{}},
|
|
|
|
init_command);
|
|
|
|
while (true)
|
|
|
|
event_manager.handle_next_events();
|
|
|
|
}
|
|
|
|
catch (peer_disconnected&)
|
|
|
|
{
|
|
|
|
fputs("disconnected from server\n", stderr);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:53:04 +02:00
|
|
|
int kakoune(memoryview<String> params)
|
2012-10-21 13:02:24 +02:00
|
|
|
{
|
2013-09-19 20:53:04 +02:00
|
|
|
ParametersParser parser(params, { { "c", true },
|
|
|
|
{ "e", true },
|
2013-09-19 20:54:14 +02:00
|
|
|
{ "n", false },
|
2013-09-19 21:09:53 +02:00
|
|
|
{ "s", true },
|
|
|
|
{ "d", false } });
|
2013-09-19 20:53:04 +02:00
|
|
|
String init_command;
|
|
|
|
if (parser.has_option("e"))
|
|
|
|
init_command = parser.option_value("e");
|
2013-02-26 14:13:37 +01:00
|
|
|
|
2013-09-19 20:53:04 +02:00
|
|
|
if (parser.has_option("c"))
|
|
|
|
{
|
2013-09-19 21:09:53 +02:00
|
|
|
for (auto opt : { "n", "s", "d" })
|
2012-10-23 22:56:24 +02:00
|
|
|
{
|
2013-09-19 20:53:04 +02:00
|
|
|
if (parser.has_option(opt))
|
2012-10-30 14:00:44 +01:00
|
|
|
{
|
2013-09-21 17:10:37 +02:00
|
|
|
fprintf(stderr, "error: -%s makes not sense with -c\n", opt);
|
2013-09-19 20:53:04 +02:00
|
|
|
return -1;
|
2012-10-30 14:00:44 +01:00
|
|
|
}
|
2012-10-23 22:56:24 +02:00
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
return run_client(parser.option_value("c"), init_command);
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool daemon = parser.has_option("d");
|
|
|
|
static bool terminate = false;
|
|
|
|
if (daemon)
|
|
|
|
{
|
|
|
|
if (not parser.has_option("s"))
|
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",
|
|
|
|
parser.option_value("s").c_str(), child);
|
|
|
|
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
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
EventManager event_manager;
|
|
|
|
GlobalOptions global_options;
|
|
|
|
GlobalHooks global_hooks;
|
2013-10-25 01:01:17 +02:00
|
|
|
GlobalKeymaps global_keymaps;
|
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;
|
2013-09-23 20:28:15 +02:00
|
|
|
ColorRegistry color_registry;
|
|
|
|
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
|
|
|
|
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 ***");
|
|
|
|
write_debug("pid: " + to_string(getpid()));
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
Server server(parser.has_option("s") ? parser.option_value("s") : to_string(getpid()));
|
2013-11-16 00:47:44 +01:00
|
|
|
write_debug("session: " + server.session());
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
if (not parser.has_option("n")) try
|
|
|
|
{
|
|
|
|
Context initialisation_context;
|
|
|
|
command_manager.execute("source " + runtime_directory() + "/kakrc",
|
|
|
|
initialisation_context);
|
|
|
|
}
|
|
|
|
catch (Kakoune::runtime_error& error)
|
|
|
|
{
|
2013-10-26 20:23:00 +02:00
|
|
|
write_debug("error while parsing kakrc: "_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;
|
|
|
|
global_hooks.run_hook("KakBegin", "", empty_context);
|
|
|
|
}
|
2013-04-19 13:45:44 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
if (parser.positional_count() != 0) try
|
|
|
|
{
|
|
|
|
// create buffers in reverse order so that the first given buffer
|
|
|
|
// is the most recently created one.
|
|
|
|
for (int i = parser.positional_count() - 1; i >= 0; --i)
|
2013-04-29 13:50:13 +02:00
|
|
|
{
|
2013-09-23 20:28:15 +02:00
|
|
|
const String& file = parser[i];
|
|
|
|
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))
|
|
|
|
event_manager.handle_next_events();
|
2013-04-19 13:45:44 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
|
|
|
Context empty_context;
|
|
|
|
global_hooks.run_hook("KakEnd", "", empty_context);
|
2013-09-19 20:53:04 +02:00
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
return 0;
|
2013-09-19 20:53:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
setlocale(LC_ALL, "");
|
|
|
|
|
|
|
|
signal(SIGSEGV, signal_handler);
|
|
|
|
signal(SIGFPE, signal_handler);
|
|
|
|
signal(SIGQUIT, signal_handler);
|
|
|
|
signal(SIGTERM, signal_handler);
|
|
|
|
|
|
|
|
std::vector<String> params;
|
|
|
|
for (size_t i = 1; i < argc; ++i)
|
2013-12-03 20:48:53 +01:00
|
|
|
params.push_back(argv[i]);
|
2013-09-19 20:53:04 +02:00
|
|
|
|
|
|
|
kakoune(params);
|
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"
|
|
|
|
"Valid options:\n"
|
|
|
|
" -e <commands>: execute commands on initialisation\n"
|
|
|
|
" -c <session>: connect to the given session\n"
|
|
|
|
" -s <session>: set session name\n"
|
|
|
|
" -d: run as a headless session (requires -s)\n"
|
|
|
|
" -n: do not source kakrc files on startup\n",
|
|
|
|
error.what());
|
|
|
|
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;
|
|
|
|
}
|