home/src/main.cc

1156 lines
43 KiB
C++
Raw Normal View History

2013-04-09 20:05:40 +02:00
#include "assert.hh"
2016-12-03 14:18:11 +01:00
#include "backtrace.hh"
2017-03-16 10:57:39 +01:00
#include "buffer.hh"
#include "buffer_manager.hh"
#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"
#include "event_manager.hh"
#include "face_registry.hh"
2013-04-09 20:05:40 +02:00
#include "file.hh"
#include "highlighters.hh"
2014-12-24 14:03:17 +01:00
#include "insert_completer.hh"
#include "json_ui.hh"
2017-03-16 10:57:39 +01:00
#include "ncurses_ui.hh"
#include "option_types.hh"
#include "parameters_parser.hh"
#include "ranges.hh"
2017-03-16 10:57:39 +01:00
#include "regex.hh"
2013-04-09 20:05:40 +02:00
#include "register_manager.hh"
#include "remote.hh"
#include "scope.hh"
2017-03-16 10:57:39 +01:00
#include "shared_string.hh"
2014-12-24 14:03:17 +01:00
#include "shell_manager.hh"
2013-04-09 20:05:40 +02:00
#include "string.hh"
#include "unit_tests.hh"
2013-04-18 14:28:53 +02:00
#include "window.hh"
#include "clock.hh"
2011-09-02 18:51:20 +02:00
2014-12-24 14:03:17 +01:00
#include <fcntl.h>
2019-01-24 13:04:24 +01:00
#include <locale.h>
#include <sys/stat.h>
2014-12-24 14:03:17 +01:00
#include <sys/types.h>
2014-04-30 20:08:06 +02:00
#include <unistd.h>
#include <pwd.h>
2017-02-14 14:54:45 +01:00
namespace Kakoune
{
2011-09-02 18:51:20 +02:00
extern const char* version;
struct {
unsigned int version;
const char* notes;
} constexpr version_notes[] = { {
2019-01-20 12:52:01 +01:00
20190120,
"» named capture groups in regex\n"
"» auto_complete option renamed to autocomplete\n"
}, {
2018-10-27 00:26:50 +02:00
20181027,
"» define-commands -shell-completion and -shell-candidates has been renamed\n"
"» exclusive face attributes is replaced with final (fg/bg/attr)\n"
"» <a-M> (merge consecutive) moved to <a-_> to make <a-M> backward <a-m>\n"
"» remove-hooks now takes a regex parameter\n"
}, {
2018-09-04 00:03:54 +02:00
20180904,
"» Big breaking refactoring of various Kakoune features,\n"
" configuration might need to be updated see `:doc changelog` for details\n"
"» define-command -allow-override switch has been renamed -override\n"
}, {
20180413,
"» ModeChange hook has been introduced and is expected to replace\n"
" the various ${MODE}Begin/${MODE}End hooks, consider those deprecated.\n"
"» '*' Does not strip whitespaces anymore, use built-in '_' to strip them\n"
"» 'l' on eol will go to next line, 'h' on first char will go to previous\n"
"» selections merging behaviour is now a bit more complex again\n"
"» 'x' will only jump to next line if full line is already selected\n"
"» WORD text object moved to <a-w> instead of W for consistency\n"
"» rotate main selection moved to ), rotate content to <a-)>, ( for backward\n"
"» faces are now scoped, set-face command takes an additional scope parameter\n"
"» <backtab> key is gone, use <s-tab> instead\n"
} };
void show_startup_info(Client* local_client, int last_version)
{
String info;
for (auto note : version_notes)
{
if (not note.version)
info += format("• Development version\n{}\n", note.notes);
else if (note.version > last_version)
{
const auto year = note.version / 10000;
const auto month = (note.version / 100) % 100;
const auto day = note.version % 100;
info += format("• Kakoune v{}.{}{}.{}{}\n{}\n",
year, month < 10 ? "0" : "", month, day < 10 ? "0" : "", day, note.notes);
}
}
if (not info.empty())
{
info += "See the `:doc options startup-info` to control this message\n";
local_client->info_show(format("Kakoune {}", version), info, {}, InfoStyle::Prompt);
}
}
2017-02-14 14:54:45 +01:00
struct startup_error : runtime_error
{
2017-02-14 14:54:45 +01:00
using runtime_error::runtime_error;
};
inline void write_stdout(StringView str) { try { write(1, str); } catch (runtime_error&) {} }
inline void write_stderr(StringView str) { try { write(2, str); } catch (runtime_error&) {} }
2016-11-29 00:53:50 +01:00
String runtime_directory()
{
char relpath[PATH_MAX+1];
format_to(relpath, "{}../share/kak", split_path(get_kak_binary_path()).first);
struct stat st;
if (stat(relpath, &st) == 0 and S_ISDIR(st.st_mode))
return real_path(relpath);
return "/usr/share/kak";
}
String config_directory()
{
StringView config_home = getenv("XDG_CONFIG_HOME");
if (config_home.empty())
return format("{}/.config/kak", homedir());
return format("{}/kak", config_home);
}
static const EnvVarDesc builtin_env_vars[] = { {
"bufname", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.buffer().display_name(); }
}, {
"buffile", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.buffer().name(); }
}, {
"buflist", false,
[](StringView name, const Context& context, Quoting quoting)
{ return join(BufferManager::instance() |
transform(&Buffer::display_name) | transform(quoter(quoting)), ' ', false); }
}, {
"buf_line_count", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.buffer().line_count()); }
}, {
"timestamp", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.buffer().timestamp()); }
}, {
"history_id", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string((size_t)context.buffer().current_history_id()); }
}, {
"selection", false,
[](StringView name, const Context& context, Quoting quoting)
{ const Selection& sel = context.selections().main();
return content(context.buffer(), sel); }
}, {
"selections", false,
[](StringView name, const Context& context, Quoting quoting)
{ return join(context.selections_content() | transform(quoter(quoting)), ' ', false); }
}, {
"runtime", false,
[](StringView name, const Context& context, Quoting quoting)
{ return runtime_directory(); }
}, {
"config", false,
[](StringView name, const Context& context, Quoting quoting)
{ return config_directory(); }
}, {
"version", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return version; }
}, {
"opt_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return context.options()[name.substr(4_byte)].get_as_string(quoting); }
}, {
"main_reg_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return context.main_sel_register_value(name.substr(9_byte)).str(); }
}, {
"reg_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return join(RegisterManager::instance()[name.substr(4_byte)].get(context) |
transform(quoter(quoting)), ' ', false); }
}, {
"client_env_", true,
[](StringView name, const Context& context, Quoting quoting)
{ return context.client().get_env_var(name.substr(11_byte)).str(); }
}, {
"session", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return Server::instance().session(); }
}, {
"client", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.name(); }
}, {
"client_pid", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.client().pid()); }
}, {
"client_list", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return join(ClientManager::instance() |
transform([](const std::unique_ptr<Client>& c) -> const String&
{ return c->context().name(); }), ' ', false); }
}, {
"modified", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return context.buffer().is_modified() ? "true" : "false"; }
}, {
"cursor_line", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.selections().main().cursor().line + 1); }
}, {
"cursor_column", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.selections().main().cursor().column + 1); }
}, {
"cursor_char_value", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ auto coord = context.selections().main().cursor();
auto& buffer = context.buffer();
return to_string((size_t)utf8::codepoint(buffer.iterator_at(coord), buffer.end())); }
}, {
"cursor_char_column", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ auto coord = context.selections().main().cursor();
return to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1); }
}, {
"cursor_byte_offset", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ auto cursor = context.selections().main().cursor();
return to_string(context.buffer().distance({0,0}, cursor)); }
}, {
"selection_desc", false,
[](StringView name, const Context& context, Quoting quoting)
{ return selection_to_string(context.selections().main()); }
}, {
"selections_desc", false,
[](StringView name, const Context& context, Quoting quoting)
{ return selection_list_to_string(context.selections()); }
}, {
"selection_length", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(char_length(context.buffer(), context.selections().main())); }
}, {
"selections_length", false,
[](StringView name, const Context& context, Quoting quoting)
{ return join(context.selections() |
transform([&](const Selection& s)
{ return to_string(char_length(context.buffer(), s)); }), ' ', false); }
}, {
"window_width", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.window().dimensions().column); }
}, {
"window_height", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return to_string(context.window().dimensions().line); }
}, {
"user_modes", false,
[](StringView name, const Context& context, Quoting quoting) -> String
{ return join(context.keymaps().user_modes(), ' ', false); }
}
};
void register_registers()
{
RegisterManager& register_manager = RegisterManager::instance();
for (auto c : "abcdefghijklmnopqrstuvwxyz/\"|^@:")
2016-03-12 16:27:54 +01:00
register_manager.add_register(c, std::make_unique<StaticRegister>());
2015-01-14 20:16:32 +01:00
using StringList = Vector<String, MemoryDomain::Registers>;
register_manager.add_register('%', make_dyn_reg(
[](const Context& context)
{ return StringList{{context.buffer().display_name()}}; }));
register_manager.add_register('.', make_dyn_reg(
[](const Context& context) {
2015-01-14 20:16:32 +01:00
auto content = context.selections_content();
return StringList{content.begin(), content.end()};
}));
register_manager.add_register('#', make_dyn_reg(
[](const Context& context) {
const size_t count = context.selections().size();
StringList res;
res.reserve(count);
for (size_t i = 1; i < count+1; ++i)
res.push_back(to_string((int)i));
return res;
}));
for (size_t i = 0; i < 10; ++i)
{
register_manager.add_register('0'+i, make_dyn_reg(
2013-01-04 18:39:13 +01:00
[i](const Context& context) {
2015-01-14 20:16:32 +01:00
StringList result;
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;
},
[i](Context& context, ConstArrayView<String> values) {
if (values.empty())
return;
auto& sels = context.selections();
for (size_t sel_index = 0; sel_index < sels.size(); ++sel_index)
{
auto& sel = sels[sel_index];
if (sel.captures().size() < i+1)
sel.captures().resize(i+1);
sel.captures()[i] = values[std::min(sel_index, values.size()-1)];
}
}));
}
2016-03-12 16:27:54 +01:00
register_manager.add_register('_', std::make_unique<NullRegister>());
}
static void check_tabstop(const int& val)
{
if (val < 1) throw runtime_error{"tabstop should be strictly positive"};
}
static void check_indentwidth(const int& val)
{
if (val < 0) throw runtime_error{"indentwidth should be positive or zero"};
}
static void check_scrolloff(const DisplayCoord& so)
{
if (so.line < 0 or so.column < 0)
throw runtime_error{"scroll offset must be positive or zero"};
}
static void check_timeout(const int& timeout)
{
if (timeout < 50)
throw runtime_error{"the minimum acceptable timeout is 50 milliseconds"};
}
static void check_extra_word_chars(const Vector<Codepoint, MemoryDomain::Options>& extra_chars)
{
if (any_of(extra_chars, is_blank))
throw runtime_error{"blanks are not accepted for extra completion characters"};
}
static void check_matching_pairs(const Vector<Codepoint, MemoryDomain::Options>& pairs)
{
if ((pairs.size() % 2) != 0)
throw runtime_error{"matching pairs should have a pair number of element"};
if (not all_of(pairs, [](Codepoint cp) { return is_punctuation(cp); }))
throw runtime_error{"matching pairs can only be punctuation"};
}
void register_options()
{
OptionsRegistry& reg = GlobalScope::instance().option_registry();
reg.declare_option<int, check_tabstop>("tabstop", "size of a tab character", 8);
reg.declare_option<int, check_indentwidth>("indentwidth", "indentation width", 4);
reg.declare_option<DisplayCoord, check_scrolloff>(
"scrolloff", "number of lines and columns to keep visible main cursor when scrolling",
{0,0});
2016-08-06 10:05:50 +02:00
reg.declare_option("eolformat", "end of line format", EolFormat::Lf);
reg.declare_option("BOM", "byte order mark to use when writing buffer",
ByteOrderMark::None);
reg.declare_option("incsearch",
"incrementally apply search/select/split regex",
true);
reg.declare_option("autoinfo",
"automatically display contextual help",
AutoInfo::Command | AutoInfo::OnKey);
reg.declare_option("autocomplete",
"automatically display possible completions",
AutoComplete::Insert | AutoComplete::Prompt);
reg.declare_option("aligntab",
"use tab characters when possible for alignment",
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",
Vector<String, MemoryDomain::Options>({ "./", "%/", "/usr/include" }));
reg.declare_option("completers", "insert mode completers to execute.",
2015-01-12 14:24:30 +01:00
InsertCompleterDescList({
InsertCompleterDesc{ InsertCompleterDesc::Filename, {} },
InsertCompleterDesc{ InsertCompleterDesc::Word, "all"_str }
}), OptionFlags::None);
reg.declare_option("static_words", "list of words to always consider for insert word completion",
Vector<String, MemoryDomain::Options>{});
reg.declare_option("autoreload",
"autoreload buffer when a filesystem modification is detected",
Autoreload::Ask);
reg.declare_option<int, check_timeout>(
"idle_timeout", "timeout, in milliseconds, before idle hooks are triggered", 50);
reg.declare_option<int, check_timeout>(
"fs_check_timeout", "timeout, in milliseconds, between file system buffer modification checks",
500);
reg.declare_option("ui_options",
2018-07-13 22:46:24 +02:00
"space separated list of <key>=<value> options that are "
2015-10-02 14:56:19 +02:00
"passed to and interpreted by the user interface\n"
"\n"
"The ncurses ui supports the following options:\n"
2017-06-29 10:31:02 +02:00
" <key>: <value>:\n"
2017-03-10 13:09:09 +01:00
" ncurses_assistant clippy|cat|dilbert|none|off\n"
" ncurses_status_on_top bool\n"
2015-10-02 14:56:19 +02:00
" ncurses_set_title bool\n"
" ncurses_enable_mouse bool\n"
" ncurses_change_colors bool\n"
" ncurses_wheel_up_button int\n"
" ncurses_wheel_down_button int\n"
" ncurses_shift_function_key int\n"
" ncurses_builtin_key_parser bool\n",
2015-04-16 12:35:52 +02:00
UserInterface::Options{});
reg.declare_option("modelinefmt", "format string used to generate the modeline",
"%val{bufname} %val{cursor_line}:%val{cursor_char_column} {{context_info}} {{mode_info}} - %val{client}@[%val{session}]"_str);
reg.declare_option("debug", "various debug flags", DebugFlags::None);
reg.declare_option("readonly", "prevent buffers from being modified", false);
reg.declare_option<Vector<Codepoint, MemoryDomain::Options>, check_extra_word_chars>(
"extra_word_chars",
"Additional characters to be considered as words for insert completion",
{ '_' });
reg.declare_option<Vector<Codepoint, MemoryDomain::Options>, check_matching_pairs>(
"matching_pairs",
"set of pair of characters to be considered as matching pairs",
{ '(', ')', '{', '}', '[', ']', '<', '>' });
reg.declare_option<int>("startup_info_version", "version up to which startup info changes should be hidden", 0);
}
2017-02-14 14:54:45 +01:00
static Client* local_client = nullptr;
static int local_client_exit = 0;
2017-02-14 14:54:45 +01:00
static UserInterface* local_ui = nullptr;
static bool convert_to_client_pending = false;
enum class UIType
{
NCurses,
Json,
Dummy,
};
2017-02-14 14:54:45 +01:00
UIType parse_ui_type(StringView ui_name)
{
2017-02-14 14:54:45 +01:00
if (ui_name == "ncurses") return UIType::NCurses;
if (ui_name == "json") return UIType::Json;
if (ui_name == "dummy") return UIType::Dummy;
2017-02-14 14:54:45 +01:00
throw parameter_error(format("error: unknown ui type: '{}'", ui_name));
}
std::unique_ptr<UserInterface> make_ui(UIType ui_type)
2012-10-17 17:49:34 +02:00
{
struct DummyUI : UserInterface
{
2016-03-15 01:24:42 +01:00
DummyUI() { set_signal_handler(SIGINT, SIG_DFL); }
bool is_ok() const override { return true; }
void menu_show(ConstArrayView<DisplayLine>, DisplayCoord,
2015-10-05 02:25:23 +02:00
Face, Face, MenuStyle) override {}
void menu_select(int) override {}
void menu_hide() override {}
void info_show(StringView, StringView, DisplayCoord, Face, InfoStyle) override {}
void info_hide() override {}
void draw(const DisplayBuffer&, const Face&, const Face&) override {}
void draw_status(const DisplayLine&, const DisplayLine&, const Face&) override {}
DisplayCoord dimensions() override { return {24,80}; }
void set_cursor(CursorMode, DisplayCoord) override {}
void refresh(bool) override {}
2017-02-14 14:54:45 +01:00
void set_on_key(OnKeyCallback) override {}
void set_ui_options(const Options&) override {}
};
switch (ui_type)
{
2016-03-12 16:27:54 +01:00
case UIType::NCurses: return std::make_unique<NCursesUI>();
case UIType::Json: return std::make_unique<JsonUI>();
case UIType::Dummy: return std::make_unique<DummyUI>();
}
throw logic_error{};
}
2017-02-14 14:54:45 +01:00
pid_t fork_server_to_background()
{
if (pid_t pid = fork())
return pid;
if (fork()) // double fork to orphan the server
exit(0);
write_stderr(format("Kakoune forked server to background ({}), for session '{}'\n",
getpid(), Server::instance().session()));
return 0;
}
std::unique_ptr<UserInterface> create_local_ui(UIType ui_type)
{
if (ui_type != UIType::NCurses)
return make_ui(ui_type);
struct LocalUI : NCursesUI
{
LocalUI()
{
kak_assert(not local_ui);
local_ui = this;
m_old_sigtstp = set_signal_handler(SIGTSTP, [](int) {
if (ClientManager::instance().count() == 1 and
*ClientManager::instance().begin() == local_client)
{
// Suspend normally if we are the only client
auto current = set_signal_handler(SIGTSTP, static_cast<LocalUI*>(local_ui)->m_old_sigtstp);
sigset_t unblock_sigtstp, old_mask;
sigemptyset(&unblock_sigtstp);
sigaddset(&unblock_sigtstp, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &unblock_sigtstp, &old_mask);
raise(SIGTSTP);
set_signal_handler(SIGTSTP, current);
sigprocmask(SIG_SETMASK, &old_mask, nullptr);
}
else
convert_to_client_pending = true;
});
}
~LocalUI() override
{
set_signal_handler(SIGTSTP, m_old_sigtstp);
local_client = nullptr;
local_ui = nullptr;
if (not convert_to_client_pending and
not ClientManager::instance().empty())
{
if (fork_server_to_background())
2015-10-14 14:21:27 +02:00
{
this->NCursesUI::~NCursesUI();
exit(local_client_exit);
2015-10-14 14:21:27 +02:00
}
}
}
private:
2015-10-09 14:41:28 +02:00
using SigHandler = void (*)(int);
SigHandler m_old_sigtstp;
};
if (not isatty(1))
throw startup_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, Buffer::Flags::None);
}
2016-03-12 16:27:54 +01:00
return std::make_unique<LocalUI>();
}
int run_client(StringView session, StringView name, StringView client_init,
Optional<BufferCoord> init_coord, UIType ui_type,
bool suspend)
{
try
{
EventManager event_manager;
RemoteClient client{session, name, make_ui(ui_type), getpid(), get_env_vars(),
client_init, std::move(init_coord)};
if (suspend)
raise(SIGTSTP);
while (not client.exit_status() and client.is_ui_ok())
event_manager.handle_next_events(EventMode::Normal);
return client.exit_status().value_or(-1);
}
catch (disconnected& e)
{
write_stderr(format("{}\ndisconnecting\n", e.what()));
return -1;
}
}
2017-02-14 14:54:45 +01:00
struct convert_to_client_mode
{
String session;
String client_name;
2017-02-14 14:54:45 +01:00
String buffer_name;
String selections;
};
enum class ServerFlags
{
None = 0,
2017-02-14 14:54:45 +01:00
IgnoreKakrc = 1 << 0,
Daemon = 1 << 1,
ReadOnly = 1 << 2,
StartupInfo = 1 << 3,
2017-02-14 14:54:45 +01:00
};
constexpr bool with_bit_ops(Meta::Type<ServerFlags>) { return true; }
2017-02-14 14:54:45 +01:00
int run_server(StringView session, StringView server_init,
StringView client_init, Optional<BufferCoord> init_coord,
ServerFlags flags, UIType ui_type, DebugFlags debug_flags,
ConstArrayView<StringView> files)
{
static bool terminate = false;
2017-02-14 14:54:45 +01:00
if (flags & ServerFlags::Daemon)
{
if (session.empty())
{
write_stderr("-d needs a session name to be specified with -s\n");
return -1;
}
if (pid_t child = fork())
{
write_stderr(format("Kakoune forked to background, for session '{}'\n"
"send SIGTERM to process {} for closing the session\n",
session, child));
exit(0);
}
set_signal_handler(SIGTERM, [](int) { terminate = true; });
}
EventManager event_manager;
Server server{session.empty() ? to_string(getpid()) : session.str()};
StringRegistry string_registry;
GlobalScope global_scope;
ShellManager shell_manager{builtin_env_vars};
CommandManager command_manager;
RegisterManager register_manager;
HighlighterRegistry highlighter_registry;
DefinedHighlighters defined_highlighters;
ClientManager client_manager;
BufferManager buffer_manager;
register_options();
register_registers();
register_commands();
register_highlighters();
global_scope.options()["debug"].set(debug_flags);
write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***");
const auto start_time = Clock::now();
UnitTest::run_all_tests();
if (debug_flags & DebugFlags::Profile)
{
using namespace std::chrono;
write_to_debug_buffer(format("running the unit tests took {} ms",
duration_cast<milliseconds>(Clock::now() - start_time).count()));
}
2017-02-14 14:54:45 +01:00
GlobalScope::instance().options().get_local_option("readonly").set<bool>(flags & ServerFlags::ReadOnly);
bool startup_error = false;
2017-02-14 14:54:45 +01:00
if (not (flags & ServerFlags::IgnoreKakrc)) try
{
Context init_context{Context::EmptyContextFlag{}};
command_manager.execute(format("source {}/kakrc", runtime_directory()),
init_context);
}
2017-02-14 14:54:45 +01:00
catch (runtime_error& error)
{
startup_error = true;
2016-10-17 19:37:05 +02:00
write_to_debug_buffer(format("error while parsing kakrc:\n"
" {}", error.what()));
}
2012-10-16 17:15:09 +02:00
if (not server_init.empty()) try
{
Context init_context{Context::EmptyContextFlag{}};
command_manager.execute(server_init, init_context);
}
catch (runtime_error& error)
{
startup_error = true;
write_to_debug_buffer(format("error while running server init commands:\n"
" {}", error.what()));
}
{
Context empty_context{Context::EmptyContextFlag{}};
global_scope.hooks().run_hook(Hook::KakBegin, session, empty_context);
}
2013-04-19 13:45:44 +02:00
if (not files.empty()) try
{
// create buffers in reverse order so that the first given buffer
// is the most recently created one.
for (auto& file : files | reverse())
{
try
{
Buffer *buffer = open_or_create_file_buffer(file);
2017-02-14 14:54:45 +01:00
if (flags & ServerFlags::ReadOnly)
buffer->flags() |= Buffer::Flags::ReadOnly;
}
2017-02-14 14:54:45 +01:00
catch (runtime_error& error)
{
startup_error = true;
2016-10-17 19:37:05 +02:00
write_to_debug_buffer(format("error while opening file '{}':\n"
" {}", file, error.what()));
}
}
}
2017-02-14 14:54:45 +01:00
catch (runtime_error& error)
{
write_to_debug_buffer(format("error while opening command line files: {}", error.what()));
}
try
2015-12-05 11:14:04 +01:00
{
2017-02-14 14:54:45 +01:00
if (not (flags & ServerFlags::Daemon))
{
local_client = client_manager.create_client(
create_local_ui(ui_type), getpid(), {}, get_env_vars(), client_init, std::move(init_coord),
[](int status) { local_client_exit = status; });
if (startup_error and local_client)
local_client->print_status({
"error during startup, see *debug* buffer for details",
local_client->context().faces()["Error"]
});
if (flags & ServerFlags::StartupInfo and local_client)
show_startup_info(local_client, global_scope.options()["startup_info_version"].get<int>());
}
while (not terminate and
(not client_manager.empty() or server.negotiating() or
(flags & ServerFlags::Daemon)))
{
client_manager.redraw_clients();
2018-05-17 14:50:11 +02:00
// Loop so that eventual inputs happening during the processing are handled as
// well, avoiding unneeded redraws.
bool allow_blocking = not client_manager.has_pending_inputs();
while (event_manager.handle_next_events(EventMode::Normal, nullptr, allow_blocking))
{
client_manager.process_pending_inputs();
allow_blocking = false;
}
2018-05-17 14:50:11 +02:00
client_manager.process_pending_inputs();
client_manager.clear_client_trash();
client_manager.clear_window_trash();
buffer_manager.clear_buffer_trash();
global_scope.option_registry().clear_option_trash();
2018-04-29 14:58:37 +02:00
if (local_client and not contains(client_manager, local_client))
local_client = nullptr;
else if (local_client and not local_client->is_ui_ok())
{
ClientManager::instance().remove_client(*local_client, false, -1);
2018-04-29 14:58:37 +02:00
local_client = nullptr;
if (not client_manager.empty() and fork_server_to_background())
return 0;
}
else if (convert_to_client_pending)
{
kak_assert(local_client);
const String client_name = local_client->context().name();
const String buffer_name = local_client->context().buffer().name();
const String selections = selection_list_to_string(local_client->context().selections());
ClientManager::instance().remove_client(*local_client, true, 0);
client_manager.clear_client_trash();
convert_to_client_pending = false;
if (fork_server_to_background())
{
String session = server.session();
server.close_session(false);
throw convert_to_client_mode{ std::move(session), std::move(client_name), std::move(buffer_name), std::move(selections) };
}
}
}
}
catch (const kill_session& kill)
{
local_client_exit = kill.exit_status;
}
2013-04-19 13:45:44 +02:00
{
Context empty_context{Context::EmptyContextFlag{}};
global_scope.hooks().run_hook(Hook::KakEnd, "", empty_context);
}
return local_client_exit;
}
int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet, StringView suffix_backup)
{
2014-10-27 18:54:20 +01:00
StringRegistry string_registry;
GlobalScope global_scope;
EventManager event_manager;
ShellManager shell_manager{builtin_env_vars};
2014-10-26 21:21:01 +01:00
RegisterManager register_manager;
BufferManager buffer_manager;
register_options();
register_registers();
2014-08-17 16:19:04 +02:00
try
{
2014-08-17 16:19:04 +02:00
auto keys = parse_keys(keystr);
auto apply_to_buffer = [&](Buffer& buffer)
2014-08-17 16:19:04 +02:00
{
try
{
InputHandler input_handler{
2015-02-02 19:46:55 +01:00
{ buffer, Selection{{0,0}, buffer.back_coord()} },
Context::Flags::Draft
};
2014-08-17 16:19:04 +02:00
for (auto& key : keys)
input_handler.handle_key(key);
}
2017-02-14 14:54:45 +01:00
catch (runtime_error& err)
2014-08-17 16:19:04 +02:00
{
if (not quiet)
write_stderr(format("error while applying keys to buffer '{}': {}\n",
buffer.display_name(), err.what()));
2014-08-17 16:19:04 +02:00
}
};
2014-08-17 16:19:04 +02:00
for (auto& file : files)
{
Buffer* buffer = open_file_buffer(file, Buffer::Flags::NoHooks);
if (not suffix_backup.empty())
write_buffer_to_file(*buffer, buffer->name() + suffix_backup);
apply_to_buffer(*buffer);
write_buffer_to_file(*buffer, buffer->name());
2014-08-17 16:19:04 +02:00
buffer_manager.delete_buffer(*buffer);
}
if (not isatty(0))
{
Buffer& buffer = *buffer_manager.create_buffer(
"*stdin*", Buffer::Flags::NoHooks, read_fd(0), InvalidTime);
apply_to_buffer(buffer);
write_buffer_to_fd(buffer, 1);
buffer_manager.delete_buffer(buffer);
2014-08-17 16:19:04 +02:00
}
}
2017-02-14 14:54:45 +01:00
catch (runtime_error& err)
{
write_stderr(format("error: {}\n", err.what()));
}
buffer_manager.clear_buffer_trash();
return 0;
}
int run_pipe(StringView session)
{
try
{
send_command(session, read_fd(0));
}
catch (disconnected& e)
{
2016-11-29 20:12:56 +01:00
write_stderr(format("{}\ndisconnecting\n", e.what()));
return -1;
}
return 0;
}
2017-02-14 14:54:45 +01:00
void signal_handler(int signal)
{
2017-02-14 14:54:45 +01:00
NCursesUI::abort();
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;
case SIGPIPE: text = "SIGPIPE"; break;
}
if (signal != SIGTERM)
{
auto msg = format("Received {}, exiting.\nPid: {}\nCallstack:\n{}",
text, getpid(), Backtrace{}.desc());
write_stderr(msg);
notify_fatal_error(msg);
}
if (Server::has_instance())
Server::instance().close_session();
if (BufferManager::has_instance())
BufferManager::instance().backup_modified_buffers();
if (signal == SIGTERM)
exit(-1);
else if (signal == SIGSEGV)
{
// generate core dump
::signal(SIGSEGV, SIG_DFL);
2019-01-19 00:34:12 +01:00
::kill(getpid(), SIGSEGV);
}
2017-02-14 14:54:45 +01:00
else
abort();
}
}
int main(int argc, char* argv[])
{
2017-02-14 14:54:45 +01:00
using namespace Kakoune;
setlocale(LC_ALL, "");
set_signal_handler(SIGSEGV, signal_handler);
set_signal_handler(SIGFPE, signal_handler);
set_signal_handler(SIGQUIT, signal_handler);
set_signal_handler(SIGTERM, signal_handler);
set_signal_handler(SIGPIPE, [](int){});
set_signal_handler(SIGINT, [](int){});
set_signal_handler(SIGCHLD, [](int){});
const ParameterDesc param_desc{
2015-03-14 18:30:34 +01:00
SwitchMap{ { "c", { true, "connect to given session" } },
{ "e", { true, "execute argument on client initialisation" } },
{ "E", { true, "execute argument on server initialisation" } },
{ "n", { false, "do not source kakrc files on startup" } },
2015-03-14 18:30:34 +01:00
{ "s", { true, "set session name" } },
{ "d", { false, "run as a headless session (requires -s)" } },
2015-03-14 18:30:34 +01:00
{ "p", { true, "just send stdin as commands to the given session" } },
{ "f", { true, "filter: for each file, select the entire buffer and execute the given keys" } },
{ "i", { true, "backup the files on which a filter is applied using the given suffix" } },
{ "q", { false, "in filter mode, be quiet about errors applying keys" } },
{ "ui", { true, "set the type of user interface to use (ncurses, dummy, or json)" } },
{ "l", { false, "list existing sessions" } },
{ "clear", { false, "clear dead sessions" } },
{ "debug", { true, "initial debug option value" } },
{ "version", { false, "display kakoune version and exit" } },
{ "ro", { false, "readonly mode" } },
{ "help", { false, "display a help message and quit" } } }
};
try
{
auto show_usage = [&]()
{
write_stdout(format("Usage: {} [options] [file]... [+<line>[:<col>]|+:]\n\n"
"Options:\n"
"{}\n"
"Prefixing a positional argument with a plus (`+`) sign will place the\n"
"cursor at a given set of coordinates, or the end of the buffer if the plus\n"
"sign is followed only by a colon (`:`)\n",
argv[0], generate_switches_doc(param_desc.switches)));
return 0;
};
2018-07-26 13:04:02 +02:00
const auto params = ArrayView<char*>{argv+1, argv + argc}
| transform([](auto* s) { return String{s}; })
| gather<Vector<String>>();
if (contains(params, "--help"_sv))
return show_usage();
2017-06-12 07:12:10 +02:00
ParametersParser parser{params, param_desc};
const bool show_help_message = (bool)parser.get_switch("help");
if (show_help_message)
return show_usage();
if (parser.get_switch("version"))
{
write_stdout(format("Kakoune {}\n", Kakoune::version));
return 0;
}
const bool list_sessions = (bool)parser.get_switch("l");
const bool clear_sessions = (bool)parser.get_switch("clear");
if (list_sessions or clear_sessions)
{
2018-08-24 16:47:11 +02:00
const String username = get_user_name();
const StringView tmp_dir = tmpdir();
for (auto& session : list_files(format("{}/kakoune/{}/", tmp_dir,
username)))
{
const bool valid = check_session(session);
if (list_sessions)
write_stdout(format("{}{}\n", session, valid ? "" : " (dead)"));
if (not valid and clear_sessions)
{
char socket_file[128];
format_to(socket_file, "{}/kakoune/{}/{}", tmp_dir,
username, session);
unlink(socket_file);
}
}
return 0;
}
if (auto session = parser.get_switch("p"))
{
for (auto opt : { "c", "n", "s", "d", "e", "E", "ro" })
{
if (parser.get_switch(opt))
{
write_stderr(format("error: -{} is incompatible with -p\n", opt));
return -1;
}
}
return run_pipe(*session);
}
auto client_init = parser.get_switch("e").value_or(StringView{});
auto server_init = parser.get_switch("E").value_or(StringView{});
const UIType ui_type = parse_ui_type(parser.get_switch("ui").value_or("ncurses"));
if (auto keys = parser.get_switch("f"))
{
if (parser.get_switch("ro"))
{
write_stderr("error: -ro is incompatible with -f\n");
return -1;
}
2015-01-12 14:58:41 +01:00
Vector<StringView> files;
for (size_t i = 0; i < parser.positional_count(); ++i)
files.emplace_back(parser[i]);
return run_filter(*keys, files, (bool)parser.get_switch("q"),
parser.get_switch("i").value_or(StringView{}));
}
Vector<StringView> files;
Optional<BufferCoord> init_coord;
for (auto& name : parser)
{
if (not name.empty() and name[0_byte] == '+')
{
if (name == "+" or name == "+:")
{
client_init = client_init + "; exec gj";
continue;
}
auto colon = find(name, ':');
if (auto line = str_to_int_ifp({name.begin()+1, colon}))
{
init_coord = BufferCoord{
*line - 1,
colon != name.end() ?
str_to_int_ifp({colon+1, name.end()}).value_or(1) - 1
: 0
};
continue;
}
}
files.emplace_back(name);
}
if (auto server_session = parser.get_switch("c"))
{
for (auto opt : { "n", "s", "d", "E", "ro" })
{
if (parser.get_switch(opt))
{
write_stderr(format("error: -{} is incompatible with -c\n", opt));
return -1;
}
}
String new_files;
for (auto name : files)
new_files += format("edit '{}';", escape(real_path(name), "'", '\\'));
return run_client(*server_session, {}, new_files + client_init, init_coord, ui_type, false);
}
else
{
StringView session = parser.get_switch("s").value_or(StringView{});
try
{
2017-02-14 14:54:45 +01:00
auto flags = (parser.get_switch("n") ? ServerFlags::IgnoreKakrc : ServerFlags::None) |
(parser.get_switch("d") ? ServerFlags::Daemon : ServerFlags::None) |
(parser.get_switch("ro") ? ServerFlags::ReadOnly : ServerFlags::None) |
(argc == 1 and isatty(0) ? ServerFlags::StartupInfo : ServerFlags::None);
auto debug_flags = option_from_string(Meta::Type<DebugFlags>{}, parser.get_switch("debug").value_or(""));
return run_server(session, server_init, client_init, init_coord, flags, ui_type, debug_flags, files);
}
catch (convert_to_client_mode& convert)
{
return run_client(convert.session, convert.client_name,
format("try %^buffer '{}'; select '{}'^; echo converted to client only mode",
escape(convert.buffer_name, "'^", '\\'), convert.selections), {}, ui_type, true);
}
}
2011-09-02 18:51:20 +02:00
}
2017-02-14 14:54:45 +01:00
catch (parameter_error& error)
{
write_stderr(format("Error while parsing parameters: {}\n"
"Valid switches:\n"
"{}", error.what(),
generate_switches_doc(param_desc.switches)));
return -1;
}
catch (startup_error& error)
{
write_stderr(format("Could not start kakoune: {}\n", error.what()));
return -1;
}
2011-09-09 21:24:18 +02:00
catch (Kakoune::exception& error)
{
write_stderr(format("uncaught exception ({}):\n{}\n", typeid(error).name(), error.what()));
2013-02-22 18:45:59 +01:00
return -1;
}
catch (std::exception& error)
{
write_stderr(format("uncaught exception ({}):\n{}\n", typeid(error).name(), error.what()));
return -1;
}
2013-02-22 18:45:59 +01:00
catch (...)
{
write_stderr("uncaught exception");
2011-09-09 21:24:18 +02:00
return -1;
}
2011-09-02 18:51:20 +02:00
return 0;
}
#if defined(__ELF__)
asm(R"(
.pushsection ".debug_gdb_scripts", "MS",@progbits,1
.byte 4
.ascii "kakoune-inline-gdb.py\n"
.ascii "import os.path\n"
.ascii "sys.path.insert(0, os.path.dirname(gdb.current_objfile().filename) + '/../share/kak/gdb/')\n"
.ascii "import gdb.printing\n"
.ascii "import kakoune\n"
.ascii "gdb.printing.register_pretty_printer(gdb.current_objfile(), kakoune.build_pretty_printer())\n"
.popsection
)");
#endif