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"
|
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"
|
|
|
|
#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"
|
2014-12-24 14:03:17 +01:00
|
|
|
#include "insert_completer.hh"
|
2016-03-06 02:37:20 +01:00
|
|
|
#include "json_ui.hh"
|
2019-11-16 11:27:06 +01:00
|
|
|
#include "terminal_ui.hh"
|
2017-03-16 10:57:39 +01:00
|
|
|
#include "option_types.hh"
|
2012-12-18 21:41:13 +01:00
|
|
|
#include "parameters_parser.hh"
|
2017-08-29 10:23:03 +02:00
|
|
|
#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"
|
2014-10-30 15:00:42 +01:00
|
|
|
#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"
|
2015-05-22 14:58:56 +02:00
|
|
|
#include "unit_tests.hh"
|
2013-04-18 14:28:53 +02:00
|
|
|
#include "window.hh"
|
2018-12-21 13:03:21 +01:00
|
|
|
#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>
|
2014-04-29 22:37:11 +02:00
|
|
|
#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>
|
2015-09-15 14:32:26 +02:00
|
|
|
#include <pwd.h>
|
2014-04-29 22:37:11 +02:00
|
|
|
|
2017-02-14 14:54:45 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
2011-09-02 18:51:20 +02:00
|
|
|
|
2018-04-09 09:04:51 +02:00
|
|
|
extern const char* version;
|
|
|
|
|
2018-06-04 01:39:32 +02:00
|
|
|
struct {
|
2018-07-20 12:49:36 +02:00
|
|
|
unsigned int version;
|
2020-01-04 01:41:32 +01:00
|
|
|
StringView notes;
|
2018-06-04 01:39:32 +02:00
|
|
|
} constexpr version_notes[] = { {
|
2022-10-30 23:05:58 +01:00
|
|
|
20221031,
|
2022-06-12 13:30:56 +02:00
|
|
|
"» {+b}<esc>{} does not end macro recording anymore, use {+b}Q{}\n"
|
2022-01-24 11:48:51 +01:00
|
|
|
"» pipe commands do not append final end-of-lines anymore\n"
|
2022-02-22 10:14:47 +01:00
|
|
|
"» {+u}complete-command{} to configure command completion\n"
|
2022-07-17 19:31:36 +02:00
|
|
|
"» {+b}p{}, {+b}P{}, {+b}!{} and {+b}<a-!>{} now select the inserted text\n"
|
2022-03-16 23:20:07 +01:00
|
|
|
"» {+b}x{} now uses {+b}<a-x>{} behaviour\n"
|
2022-04-15 00:14:17 +02:00
|
|
|
"» {+b}<space>{} and {+b},{} have been swapped\n"
|
2022-01-24 11:48:51 +01:00
|
|
|
}, {
|
2021-11-07 06:51:39 +01:00
|
|
|
20211107,
|
|
|
|
"» colored and curly underlines support (undocumented in 20210828)\n"
|
|
|
|
}, {
|
2021-10-28 00:13:34 +02:00
|
|
|
20211028,
|
|
|
|
"» {+b}g{} and {+b}v{} do not auto-convert to lowercase anymore\n"
|
|
|
|
}, {
|
2021-08-28 06:07:42 +02:00
|
|
|
20210828,
|
2021-07-20 14:45:33 +02:00
|
|
|
"» {+u}write <filename>{} will refuse to overwrite without {+u}-force{}\n"
|
2021-07-07 11:46:47 +02:00
|
|
|
"» {+u}$kak_command_fifo{} support\n"
|
2020-09-09 11:38:12 +02:00
|
|
|
"» {+u}set-option -remove{} support\n"
|
2020-10-20 11:55:42 +02:00
|
|
|
"» prompts auto select {+i}menu{} completions on space\n"
|
|
|
|
"» explicit completion support ({+b}<c-x>...{}) in prompts\n"
|
2020-10-27 10:20:18 +01:00
|
|
|
"» {+u}write -atomic{} was replaced with {+u}write -method <method>{}\n"
|
2020-09-09 11:38:12 +02:00
|
|
|
}, {
|
2020-09-01 12:37:20 +02:00
|
|
|
20200901,
|
2020-08-17 11:56:00 +02:00
|
|
|
"» daemon mode does not fork anymore\n"
|
|
|
|
}, {
|
2020-08-04 11:40:43 +02:00
|
|
|
20200804,
|
|
|
|
"» {+u}User{} hook support\n"
|
|
|
|
"» Removed {+i}bold{} and {+i}italic{} faces from colorschemes\n"
|
|
|
|
"» Read from stdin support for clients\n"
|
|
|
|
"» {+u}rgba:RRGGBBAA{} faces and alpha blending\n"
|
|
|
|
}, {
|
2020-01-16 10:52:01 +01:00
|
|
|
20200116,
|
2020-01-16 13:23:13 +01:00
|
|
|
"» {+u}InsertCompletionHide{} parameter is now the list of inserted ranges\n"
|
2020-01-16 10:52:01 +01:00
|
|
|
}, {
|
2019-12-10 11:27:48 +01:00
|
|
|
20191210,
|
2020-01-16 13:23:13 +01:00
|
|
|
"» {+u}ModeChange{} parameter has changed to contain push/pop\n"
|
2020-01-20 09:46:11 +01:00
|
|
|
"» {+ui}Mode{+u}Begin{}/{+ui}Mode{+u}End{} hooks were removed\n"
|
2019-10-16 11:19:43 +02:00
|
|
|
}, {
|
2019-07-01 14:07:29 +02:00
|
|
|
20190701,
|
2020-01-04 01:41:32 +01:00
|
|
|
"» {+u}%file\\{<filename>}{} expansions to read files\n"
|
|
|
|
"» {+u}echo -to-file <filename>{} to write to file\n"
|
|
|
|
"» completions option have an on select command instead of "
|
|
|
|
"a docstring\n"
|
2019-07-01 14:07:29 +02:00
|
|
|
"» Function key syntax do not accept lower case f anymore\n"
|
2020-01-04 01:41:32 +01:00
|
|
|
"» shell quoting of list options is now opt-in with "
|
|
|
|
"{+u}$kak_quoted_...{}\n"
|
2019-04-07 01:49:43 +02:00
|
|
|
}, {
|
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,
|
2020-01-04 01:41:32 +01:00
|
|
|
"» {+u}define-commands{} {+i}-shell-completion{} and {+i}-shell-candidates{} "
|
|
|
|
"has been renamed\n"
|
|
|
|
"» exclusive face attributes is replaced with final "
|
|
|
|
"(fg/bg/attr)\n"
|
|
|
|
"» {+b}<a-M>{} (merge consecutive) moved to {+b}<a-_>{} to make {+b}<a-M>{} "
|
|
|
|
"backward {+b}<a-m>{}\n"
|
|
|
|
"» {+u}remove-hooks{} now takes a regex parameter\n"
|
2018-10-06 02:55:26 +02:00
|
|
|
}, {
|
2018-09-04 00:03:54 +02:00
|
|
|
20180904,
|
2020-01-04 01:41:32 +01:00
|
|
|
"» Big breaking refactoring of various Kakoune features, "
|
|
|
|
"configuration might need to be updated see `:doc changelog` "
|
|
|
|
"for details\n"
|
|
|
|
"» {+u}define-command{} {+i}-allow-override{} switch has been renamed "
|
|
|
|
"{+i}-override{}\n"
|
2018-06-04 01:39:32 +02:00
|
|
|
}, {
|
|
|
|
20180413,
|
2020-01-04 01:41:32 +01:00
|
|
|
"» {+u}ModeChange{} hook has been introduced and is expected "
|
|
|
|
"to replace the various {+ui}Mode{+u}Begin{}/{+ui}Mode{+u}End{} hooks, "
|
|
|
|
"consider those deprecated.\n"
|
|
|
|
"» {+b}*{} Does not strip whitespaces anymore, use built-in "
|
|
|
|
"{+b}_{} to strip them\n"
|
|
|
|
"» {+b}l{} on eol will go to next line, {+b}h{} on first char will "
|
|
|
|
"go to previous\n"
|
|
|
|
"» selections merging behaviour is now a bit more complex "
|
|
|
|
"again\n"
|
|
|
|
"» {+b}x{} will only jump to next line if full line is already "
|
|
|
|
"selected\n"
|
|
|
|
"» {+i}WORD{} text object moved to {+b}<a-w>{} instead of {+b}W{} for "
|
|
|
|
"consistency\n"
|
|
|
|
"» rotate main selection moved to {+b}){}, rotate content to {+b}<a-)>{}, "
|
|
|
|
"{+b}({} for backward\n"
|
|
|
|
"» faces are now scoped, {+u}set-face{} command takes an additional "
|
|
|
|
"scope parameter\n"
|
|
|
|
"» {+b}<backtab>{} key is gone, use {+b}<s-tab>{} instead\n"
|
2018-06-04 01:39:32 +02:00
|
|
|
} };
|
|
|
|
|
|
|
|
void show_startup_info(Client* local_client, int last_version)
|
|
|
|
{
|
2020-01-04 01:41:32 +01:00
|
|
|
const Face version_face{Color::Default, Color::Default, Attribute::Bold};
|
|
|
|
DisplayLineList info;
|
2018-06-04 01:39:32 +02:00
|
|
|
for (auto note : version_notes)
|
|
|
|
{
|
2020-01-21 06:25:13 +01:00
|
|
|
if (note.version and note.version <= last_version)
|
2020-01-04 01:41:32 +01:00
|
|
|
continue;
|
|
|
|
|
2018-07-20 12:49:36 +02:00
|
|
|
if (not note.version)
|
2020-01-04 01:41:32 +01:00
|
|
|
info.push_back({"• Development version", version_face});
|
|
|
|
else
|
2018-06-04 01:39:32 +02:00
|
|
|
{
|
|
|
|
const auto year = note.version / 10000;
|
|
|
|
const auto month = (note.version / 100) % 100;
|
|
|
|
const auto day = note.version % 100;
|
2020-01-04 01:41:32 +01:00
|
|
|
info.push_back({format("• Kakoune v{}.{}{}.{}{}",
|
|
|
|
year, month < 10 ? "0" : "", month, day < 10 ? "0" : "", day),
|
|
|
|
version_face});
|
2018-06-04 01:39:32 +02:00
|
|
|
}
|
2020-01-04 01:41:32 +01:00
|
|
|
|
|
|
|
for (auto&& line : note.notes | split<StringView>('\n'))
|
|
|
|
info.push_back(parse_display_line(line, GlobalScope::instance().faces()));
|
2018-06-04 01:39:32 +02:00
|
|
|
}
|
|
|
|
if (not info.empty())
|
|
|
|
{
|
2020-01-04 01:41:32 +01:00
|
|
|
info.push_back({"See the `:doc options startup-info` to control this message",
|
|
|
|
Face{Color::Default, Color::Default, Attribute::Italic}});
|
|
|
|
local_client->info_show({format("Kakoune {}", version), version_face}, info, {}, InfoStyle::Prompt);
|
2018-06-04 01:39:32 +02:00
|
|
|
}
|
|
|
|
}
|
2017-02-19 14:59:44 +01:00
|
|
|
|
2019-09-08 14:53:06 +02:00
|
|
|
inline void write_stdout(StringView str) { try { write(STDOUT_FILENO, str); } catch (runtime_error&) {} }
|
|
|
|
inline void write_stderr(StringView str) { try { write(STDERR_FILENO, str); } catch (runtime_error&) {} }
|
2016-11-29 00:53:50 +01:00
|
|
|
|
2012-09-10 20:10:18 +02:00
|
|
|
String runtime_directory()
|
|
|
|
{
|
2020-08-04 17:48:34 +02:00
|
|
|
if (const char* runtime_directory = getenv("KAKOUNE_RUNTIME"))
|
|
|
|
return runtime_directory;
|
|
|
|
|
2015-09-02 23:28:41 +02:00
|
|
|
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";
|
2012-09-10 20:10:18 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 00:05:08 +01:00
|
|
|
String config_directory()
|
|
|
|
{
|
2019-10-10 11:26:27 +02:00
|
|
|
if (StringView kak_cfg_dir = getenv("KAKOUNE_CONFIG_DIR"); not kak_cfg_dir.empty())
|
|
|
|
return kak_cfg_dir.str();
|
|
|
|
if (StringView xdg_cfg_home = getenv("XDG_CONFIG_HOME"); not xdg_cfg_home.empty())
|
|
|
|
return format("{}/kak", xdg_cfg_home);
|
|
|
|
return format("{}/.config/kak", homedir());
|
2018-01-19 00:05:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-01 03:28:06 +01:00
|
|
|
static auto main_sel_first(const SelectionList& selections)
|
|
|
|
{
|
|
|
|
auto beg = &*selections.begin(), end = &*selections.end();
|
|
|
|
auto main = beg + selections.main_index();
|
|
|
|
using View = ConstArrayView<Selection>;
|
|
|
|
return concatenated(View{main, end}, View{beg, main});
|
|
|
|
}
|
|
|
|
|
2018-02-18 04:52:29 +01:00
|
|
|
static const EnvVarDesc builtin_env_vars[] = { {
|
|
|
|
"bufname", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {context.buffer().display_name()}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"buffile", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {context.buffer().name()}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"buflist", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return BufferManager::instance() | transform(&Buffer::display_name) | gather<Vector>(); }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"buf_line_count", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(context.buffer().line_count())}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"timestamp", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(context.buffer().timestamp())}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"history_id", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string((size_t)context.buffer().current_history_id())}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"selection", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
2018-02-18 04:52:29 +01:00
|
|
|
{ const Selection& sel = context.selections().main();
|
2020-03-01 03:28:06 +01:00
|
|
|
return {content(context.buffer(), sel)}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"selections", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return context.selections_content(); }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"runtime", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {runtime_directory()}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"config", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {config_directory()}; }
|
2018-04-09 09:04:51 +02:00
|
|
|
}, {
|
|
|
|
"version", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {version}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"opt_", true,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return context.options()[name.substr(4_byte)].get_as_strings(); }
|
2018-06-03 06:46:44 +02:00
|
|
|
}, {
|
|
|
|
"main_reg_", true,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {context.main_sel_register_value(name.substr(9_byte)).str()}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"reg_", true,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context)
|
|
|
|
{ return RegisterManager::instance()[name.substr(4_byte)].get(context) |
|
|
|
|
gather<Vector<String>>(); }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"client_env_", true,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {context.client().get_env_var(name.substr(11_byte)).str()}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"session", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {Server::instance().session()}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"client", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {context.name()}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"client_pid", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(context.client().pid())}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"client_list", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return ClientManager::instance() |
|
2018-02-18 04:52:29 +01:00
|
|
|
transform([](const std::unique_ptr<Client>& c) -> const String&
|
2020-03-01 03:28:06 +01:00
|
|
|
{ return c->context().name(); }) | gather<Vector>(); }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"modified", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {context.buffer().is_modified() ? "true" : "false"}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"cursor_line", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(context.selections().main().cursor().line + 1)}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"cursor_column", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(context.selections().main().cursor().column + 1)}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"cursor_char_value", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
2018-02-18 04:52:29 +01:00
|
|
|
{ auto coord = context.selections().main().cursor();
|
|
|
|
auto& buffer = context.buffer();
|
2020-03-01 03:28:06 +01:00
|
|
|
return {to_string((size_t)utf8::codepoint(buffer.iterator_at(coord), buffer.end()))}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"cursor_char_column", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
2018-02-18 04:52:29 +01:00
|
|
|
{ auto coord = context.selections().main().cursor();
|
2020-03-01 03:28:06 +01:00
|
|
|
return {to_string(context.buffer()[coord.line].char_count_to(coord.column) + 1)}; }
|
2019-03-23 01:32:32 +01:00
|
|
|
}, {
|
|
|
|
"cursor_display_column", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
2019-03-23 01:32:32 +01:00
|
|
|
{ auto coord = context.selections().main().cursor();
|
2020-03-01 03:28:06 +01:00
|
|
|
return {to_string(get_column(context.buffer(),
|
|
|
|
context.options()["tabstop"].get<int>(),
|
|
|
|
coord) + 1)}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"cursor_byte_offset", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
2018-02-18 04:52:29 +01:00
|
|
|
{ auto cursor = context.selections().main().cursor();
|
2020-03-01 03:28:06 +01:00
|
|
|
return {to_string(context.buffer().distance({0,0}, cursor))}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"selection_desc", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {selection_to_string(ColumnType::Byte, context.buffer(), context.selections().main())}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"selections_desc", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return main_sel_first(context.selections()) |
|
|
|
|
transform([&buffer=context.buffer()](const Selection& sel) {
|
|
|
|
return selection_to_string(ColumnType::Byte, buffer, sel);
|
|
|
|
}) | gather<Vector>(); }
|
2019-11-12 03:06:37 +01:00
|
|
|
}, {
|
|
|
|
"selections_char_desc", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return main_sel_first(context.selections()) |
|
|
|
|
transform([&buffer=context.buffer()](const Selection& sel) {
|
|
|
|
return selection_to_string(ColumnType::Codepoint, buffer, sel);
|
|
|
|
}) | gather<Vector>(); }
|
2019-11-12 11:56:45 +01:00
|
|
|
}, {
|
|
|
|
"selections_display_column_desc", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return main_sel_first(context.selections()) |
|
|
|
|
transform([&buffer=context.buffer(), tabstop=context.options()["tabstop"].get<int>()](const Selection& sel) {
|
|
|
|
return selection_to_string(ColumnType::DisplayColumn, buffer, sel, tabstop);
|
|
|
|
}) | gather<Vector>(); }
|
2018-10-20 12:36:41 +02:00
|
|
|
}, {
|
|
|
|
"selection_length", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(char_length(context.buffer(), context.selections().main()))}; }
|
2018-10-20 12:36:41 +02:00
|
|
|
}, {
|
|
|
|
"selections_length", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return context.selections() |
|
2020-03-17 07:00:29 +01:00
|
|
|
transform([&](const Selection& s) -> String {
|
2020-03-01 03:28:06 +01:00
|
|
|
return to_string(char_length(context.buffer(), s));
|
|
|
|
}) | gather<Vector<String>>(); }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"window_width", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(context.window().dimensions().column)}; }
|
2018-02-18 04:52:29 +01:00
|
|
|
}, {
|
|
|
|
"window_height", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return {to_string(context.window().dimensions().line)}; }
|
2018-03-02 07:45:04 +01:00
|
|
|
}, {
|
|
|
|
"user_modes", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return context.keymaps().user_modes(); }
|
2018-12-16 11:39:50 +01:00
|
|
|
}, {
|
|
|
|
"window_range", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
2018-12-16 11:39:50 +01:00
|
|
|
{
|
2019-12-16 04:09:20 +01:00
|
|
|
auto setup = context.window().compute_display_setup(context);
|
2022-07-10 02:04:15 +02:00
|
|
|
return {format("{} {} {} {}", setup.first_line, setup.first_column,
|
|
|
|
setup.line_count, 0)};
|
2018-12-16 11:39:50 +01:00
|
|
|
}
|
2018-12-31 18:40:49 +01:00
|
|
|
}, {
|
|
|
|
"history", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return history_as_strings(context.buffer().history()); }
|
2018-12-31 18:40:49 +01:00
|
|
|
}, {
|
|
|
|
"uncommitted_modifications", false,
|
2020-03-01 03:28:06 +01:00
|
|
|
[](StringView name, const Context& context) -> Vector<String>
|
|
|
|
{ return undo_group_as_strings(context.buffer().current_undo_group()); }
|
2018-02-18 04:52:29 +01:00
|
|
|
}
|
|
|
|
};
|
2012-10-23 22:56:24 +02:00
|
|
|
|
|
|
|
void register_registers()
|
|
|
|
{
|
2015-11-25 22:08:33 +01:00
|
|
|
RegisterManager& register_manager = RegisterManager::instance();
|
|
|
|
|
2020-07-19 04:56:55 +02:00
|
|
|
for (Codepoint c : StringView{"abcdefghijklmnopqrstuvwxyz\"^@"})
|
|
|
|
register_manager.add_register(c, std::make_unique<StaticRegister>(String{c}));
|
2015-11-25 22:08:33 +01:00
|
|
|
|
2020-07-19 04:56:55 +02:00
|
|
|
for (Codepoint c : StringView{"/|:\\"})
|
|
|
|
register_manager.add_register(c, std::make_unique<HistoryRegister>(String{c}));
|
2019-06-05 15:19:27 +02:00
|
|
|
|
2015-01-14 20:16:32 +01:00
|
|
|
using StringList = Vector<String, MemoryDomain::Registers>;
|
2016-03-31 10:17:02 +02:00
|
|
|
|
|
|
|
register_manager.add_register('%', make_dyn_reg(
|
2020-07-19 04:56:55 +02:00
|
|
|
"%",
|
2016-03-31 10:17:02 +02:00
|
|
|
[](const Context& context)
|
|
|
|
{ return StringList{{context.buffer().display_name()}}; }));
|
|
|
|
|
|
|
|
register_manager.add_register('.', make_dyn_reg(
|
2020-07-19 04:56:55 +02:00
|
|
|
".",
|
2016-03-31 10:17:02 +02:00
|
|
|
[](const Context& context) {
|
2015-01-14 20:16:32 +01:00
|
|
|
auto content = context.selections_content();
|
|
|
|
return StringList{content.begin(), content.end()};
|
2016-03-31 10:17:02 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
register_manager.add_register('#', make_dyn_reg(
|
2020-07-19 04:56:55 +02:00
|
|
|
"#",
|
2016-03-31 10:17:02 +02:00
|
|
|
[](const Context& context) {
|
2018-05-09 14:40:40 +02:00
|
|
|
const size_t count = context.selections().size();
|
2014-05-23 21:27:35 +02:00
|
|
|
StringList res;
|
2018-05-09 14:40:40 +02:00
|
|
|
res.reserve(count);
|
|
|
|
for (size_t i = 1; i < count+1; ++i)
|
2014-05-23 21:27:35 +02:00
|
|
|
res.push_back(to_string((int)i));
|
|
|
|
return res;
|
2016-03-31 10:17:02 +02:00
|
|
|
}));
|
2012-10-23 22:56:24 +02:00
|
|
|
|
|
|
|
for (size_t i = 0; i < 10; ++i)
|
|
|
|
{
|
2016-03-31 10:17:02 +02:00
|
|
|
register_manager.add_register('0'+i, make_dyn_reg(
|
2020-07-19 04:56:55 +02:00
|
|
|
String{Codepoint('0'+i)},
|
2013-01-04 18:39:13 +01:00
|
|
|
[i](const Context& context) {
|
2015-01-14 20:16:32 +01:00
|
|
|
StringList 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;
|
2017-02-14 01:02:01 +01:00
|
|
|
},
|
|
|
|
[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)];
|
|
|
|
}
|
2015-11-25 22:08:33 +01:00
|
|
|
}));
|
2012-10-23 22:56:24 +02:00
|
|
|
}
|
2015-11-25 22:08:33 +01:00
|
|
|
|
2016-03-12 16:27:54 +01:00
|
|
|
register_manager.add_register('_', std::make_unique<NullRegister>());
|
2012-10-23 22:56:24 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 11:40:38 +02:00
|
|
|
void register_keymaps()
|
|
|
|
{
|
|
|
|
auto& keymaps = GlobalScope::instance().keymaps();
|
|
|
|
keymaps.map_key(Key::Left, KeymapMode::Normal, {'h'}, "");
|
|
|
|
keymaps.map_key(Key::Right, KeymapMode::Normal, {'l'}, "");
|
|
|
|
keymaps.map_key(Key::Down, KeymapMode::Normal, {'j'}, "");
|
|
|
|
keymaps.map_key(Key::Up, KeymapMode::Normal, {'k'}, "");
|
|
|
|
|
|
|
|
keymaps.map_key(shift(Key::Left), KeymapMode::Normal, {'H'}, "");
|
|
|
|
keymaps.map_key(shift(Key::Right), KeymapMode::Normal, {'L'}, "");
|
|
|
|
keymaps.map_key(shift(Key::Down), KeymapMode::Normal, {'J'}, "");
|
|
|
|
keymaps.map_key(shift(Key::Up), KeymapMode::Normal, {'K'}, "");
|
|
|
|
|
|
|
|
keymaps.map_key(Key::End, KeymapMode::Normal, {alt('l')}, "");
|
|
|
|
keymaps.map_key(Key::Home, KeymapMode::Normal, {alt('h')}, "");
|
|
|
|
keymaps.map_key(shift(Key::End), KeymapMode::Normal, {alt('L')}, "");
|
|
|
|
keymaps.map_key(shift(Key::Home), KeymapMode::Normal, {alt('H')}, "");
|
|
|
|
}
|
|
|
|
|
2016-02-12 14:53:54 +01:00
|
|
|
static void check_tabstop(const int& val)
|
2016-02-11 23:07:18 +01:00
|
|
|
{
|
2016-02-12 14:53:54 +01:00
|
|
|
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"};
|
2016-02-11 23:07:18 +01:00
|
|
|
}
|
|
|
|
|
2016-09-22 21:36:26 +02:00
|
|
|
static void check_scrolloff(const DisplayCoord& so)
|
2016-02-11 23:07:18 +01:00
|
|
|
{
|
|
|
|
if (so.line < 0 or so.column < 0)
|
|
|
|
throw runtime_error{"scroll offset must be positive or zero"};
|
|
|
|
}
|
|
|
|
|
2016-09-18 14:47:22 +02:00
|
|
|
static void check_timeout(const int& timeout)
|
|
|
|
{
|
|
|
|
if (timeout < 50)
|
|
|
|
throw runtime_error{"the minimum acceptable timeout is 50 milliseconds"};
|
|
|
|
}
|
|
|
|
|
2017-06-26 16:28:41 +02:00
|
|
|
static void check_extra_word_chars(const Vector<Codepoint, MemoryDomain::Options>& extra_chars)
|
2017-02-20 20:19:26 +01:00
|
|
|
{
|
2018-03-25 07:47:19 +02:00
|
|
|
if (any_of(extra_chars, is_blank))
|
2017-02-20 20:19:26 +01:00
|
|
|
throw runtime_error{"blanks are not accepted for extra completion characters"};
|
|
|
|
}
|
|
|
|
|
2017-11-04 08:53:53 +01:00
|
|
|
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"};
|
2018-11-27 08:13:29 +01:00
|
|
|
if (not all_of(pairs, [](Codepoint cp) { return is_punctuation(cp); }))
|
2017-11-04 08:53:53 +01:00
|
|
|
throw runtime_error{"matching pairs can only be punctuation"};
|
|
|
|
}
|
|
|
|
|
2014-10-30 15:00:42 +01:00
|
|
|
void register_options()
|
|
|
|
{
|
|
|
|
OptionsRegistry& reg = GlobalScope::instance().option_registry();
|
|
|
|
|
2016-02-12 14:53:54 +01:00
|
|
|
reg.declare_option<int, check_tabstop>("tabstop", "size of a tab character", 8);
|
|
|
|
reg.declare_option<int, check_indentwidth>("indentwidth", "indentation width", 4);
|
2016-09-22 21:36:26 +02:00
|
|
|
reg.declare_option<DisplayCoord, check_scrolloff>(
|
2016-02-11 23:07:18 +01:00
|
|
|
"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",
|
2015-12-06 13:51:55 +01:00
|
|
|
ByteOrderMark::None);
|
2014-10-30 15:00:42 +01:00
|
|
|
reg.declare_option("incsearch",
|
2018-02-22 21:28:01 +01:00
|
|
|
"incrementally apply search/select/split regex",
|
2014-10-30 15:00:42 +01:00
|
|
|
true);
|
|
|
|
reg.declare_option("autoinfo",
|
|
|
|
"automatically display contextual help",
|
2015-11-19 00:43:51 +01:00
|
|
|
AutoInfo::Command | AutoInfo::OnKey);
|
2018-07-19 10:10:10 +02:00
|
|
|
reg.declare_option("autocomplete",
|
2018-07-15 01:45:17 +02:00
|
|
|
"automatically display possible completions",
|
|
|
|
AutoComplete::Insert | AutoComplete::Prompt);
|
2014-10-30 15:00:42 +01:00
|
|
|
reg.declare_option("aligntab",
|
2018-02-22 21:28:01 +01:00
|
|
|
"use tab characters when possible for alignment",
|
2014-10-30 15:00:42 +01:00
|
|
|
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",
|
2018-03-22 22:22:34 +01:00
|
|
|
Vector<String, MemoryDomain::Options>({ "./", "%/", "/usr/include" }));
|
2014-10-30 15:00:42 +01:00
|
|
|
reg.declare_option("completers", "insert mode completers to execute.",
|
2015-01-12 14:24:30 +01:00
|
|
|
InsertCompleterDescList({
|
2017-01-29 14:49:45 +01:00
|
|
|
InsertCompleterDesc{ InsertCompleterDesc::Filename, {} },
|
2014-10-30 15:00:42 +01:00
|
|
|
InsertCompleterDesc{ InsertCompleterDesc::Word, "all"_str }
|
|
|
|
}), OptionFlags::None);
|
2015-12-27 09:49:15 +01:00
|
|
|
reg.declare_option("static_words", "list of words to always consider for insert word completion",
|
|
|
|
Vector<String, MemoryDomain::Options>{});
|
2014-10-30 15:00:42 +01:00
|
|
|
reg.declare_option("autoreload",
|
|
|
|
"autoreload buffer when a filesystem modification is detected",
|
2015-11-20 09:50:53 +01:00
|
|
|
Autoreload::Ask);
|
2019-02-12 10:54:37 +01:00
|
|
|
reg.declare_option("writemethod",
|
|
|
|
"how to write buffer to files",
|
|
|
|
WriteMethod::Overwrite);
|
2016-09-18 14:47:22 +02:00
|
|
|
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);
|
2014-11-11 00:29:16 +01:00
|
|
|
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"
|
2019-11-16 11:27:06 +01:00
|
|
|
"The terminal ui supports the following options:\n"
|
2017-06-29 10:31:02 +02:00
|
|
|
" <key>: <value>:\n"
|
2019-11-16 11:27:06 +01:00
|
|
|
" terminal_assistant clippy|cat|dilbert|none|off\n"
|
|
|
|
" terminal_status_on_top bool\n"
|
|
|
|
" terminal_set_title bool\n"
|
|
|
|
" terminal_enable_mouse bool\n"
|
2021-09-26 02:45:23 +02:00
|
|
|
" terminal_synchronized bool\n"
|
2019-11-16 11:27:06 +01:00
|
|
|
" terminal_wheel_scroll_amount int\n"
|
2021-09-26 02:45:23 +02:00
|
|
|
" terminal_shift_function_key int\n"
|
|
|
|
" terminal_padding_char codepoint\n"
|
|
|
|
" terminal_padding_fill bool\n",
|
2015-04-16 12:35:52 +02:00
|
|
|
UserInterface::Options{});
|
2015-09-19 13:43:39 +02:00
|
|
|
reg.declare_option("modelinefmt", "format string used to generate the modeline",
|
2017-03-10 10:06:37 +01:00
|
|
|
"%val{bufname} %val{cursor_line}:%val{cursor_char_column} {{context_info}} {{mode_info}} - %val{client}@[%val{session}]"_str);
|
|
|
|
|
2015-11-19 22:58:26 +01:00
|
|
|
reg.declare_option("debug", "various debug flags", DebugFlags::None);
|
2016-07-20 19:45:50 +02:00
|
|
|
reg.declare_option("readonly", "prevent buffers from being modified", false);
|
2017-06-26 16:28:41 +02:00
|
|
|
reg.declare_option<Vector<Codepoint, MemoryDomain::Options>, check_extra_word_chars>(
|
|
|
|
"extra_word_chars",
|
2017-02-20 20:19:26 +01:00
|
|
|
"Additional characters to be considered as words for insert completion",
|
2018-11-27 08:13:29 +01:00
|
|
|
{ '_' });
|
2017-11-04 08:53:53 +01:00
|
|
|
reg.declare_option<Vector<Codepoint, MemoryDomain::Options>, check_matching_pairs>(
|
|
|
|
"matching_pairs",
|
|
|
|
"set of pair of characters to be considered as matching pairs",
|
|
|
|
{ '(', ')', '{', '}', '[', ']', '<', '>' });
|
2018-06-04 01:39:32 +02:00
|
|
|
reg.declare_option<int>("startup_info_version", "version up to which startup info changes should be hidden", 0);
|
2014-10-30 15:00:42 +01:00
|
|
|
}
|
|
|
|
|
2017-02-14 14:54:45 +01:00
|
|
|
static Client* local_client = nullptr;
|
2017-08-23 08:22:23 +02:00
|
|
|
static int local_client_exit = 0;
|
2017-02-14 14:54:45 +01:00
|
|
|
static bool convert_to_client_pending = false;
|
2015-10-08 21:05:47 +02:00
|
|
|
|
2016-03-07 21:12:21 +01:00
|
|
|
enum class UIType
|
|
|
|
{
|
2019-11-16 11:27:06 +01:00
|
|
|
Terminal,
|
2016-03-07 21:12:21 +01:00
|
|
|
Json,
|
|
|
|
Dummy,
|
|
|
|
};
|
|
|
|
|
2017-02-14 14:54:45 +01:00
|
|
|
UIType parse_ui_type(StringView ui_name)
|
2015-10-08 21:05:47 +02:00
|
|
|
{
|
2019-11-16 11:27:06 +01:00
|
|
|
if (ui_name == "terminal") return UIType::Terminal;
|
2017-02-14 14:54:45 +01:00
|
|
|
if (ui_name == "json") return UIType::Json;
|
|
|
|
if (ui_name == "dummy") return UIType::Dummy;
|
2015-10-08 21:05:47 +02:00
|
|
|
|
2017-02-14 14:54:45 +01:00
|
|
|
throw parameter_error(format("error: unknown ui type: '{}'", ui_name));
|
2015-10-08 21:05:47 +02:00
|
|
|
}
|
|
|
|
|
2016-03-07 21:12:21 +01:00
|
|
|
std::unique_ptr<UserInterface> make_ui(UIType ui_type)
|
2012-10-17 17:49:34 +02:00
|
|
|
{
|
2015-08-26 20:33:52 +02:00
|
|
|
struct DummyUI : UserInterface
|
|
|
|
{
|
2016-03-15 01:24:42 +01:00
|
|
|
DummyUI() { set_signal_handler(SIGINT, SIG_DFL); }
|
2018-04-29 14:27:28 +02:00
|
|
|
bool is_ok() const override { return true; }
|
2016-09-22 21:36:26 +02:00
|
|
|
void menu_show(ConstArrayView<DisplayLine>, DisplayCoord,
|
2015-10-05 02:25:23 +02:00
|
|
|
Face, Face, MenuStyle) override {}
|
2015-08-26 20:33:52 +02:00
|
|
|
void menu_select(int) override {}
|
|
|
|
void menu_hide() override {}
|
|
|
|
|
2019-11-22 11:48:26 +01:00
|
|
|
void info_show(const DisplayLine&, const DisplayLineList&, DisplayCoord, Face, InfoStyle) override {}
|
2015-08-26 20:33:52 +02:00
|
|
|
void info_hide() override {}
|
|
|
|
|
2016-02-17 14:32:05 +01:00
|
|
|
void draw(const DisplayBuffer&, const Face&, const Face&) override {}
|
2015-08-26 20:33:52 +02:00
|
|
|
void draw_status(const DisplayLine&, const DisplayLine&, const Face&) override {}
|
2016-09-22 21:36:26 +02:00
|
|
|
DisplayCoord dimensions() override { return {24,80}; }
|
2017-04-12 11:39:17 +02:00
|
|
|
void set_cursor(CursorMode, DisplayCoord) override {}
|
2016-03-07 14:54:20 +01:00
|
|
|
void refresh(bool) override {}
|
2017-02-14 14:54:45 +01:00
|
|
|
void set_on_key(OnKeyCallback) override {}
|
2015-08-26 20:33:52 +02:00
|
|
|
void set_ui_options(const Options&) override {}
|
|
|
|
};
|
|
|
|
|
2016-03-07 21:12:21 +01:00
|
|
|
switch (ui_type)
|
|
|
|
{
|
2019-11-16 11:27:06 +01:00
|
|
|
case UIType::Terminal: return std::make_unique<TerminalUI>();
|
2016-03-12 16:27:54 +01:00
|
|
|
case UIType::Json: return std::make_unique<JsonUI>();
|
|
|
|
case UIType::Dummy: return std::make_unique<DummyUI>();
|
2016-03-07 21:12:21 +01:00
|
|
|
}
|
|
|
|
throw logic_error{};
|
|
|
|
}
|
|
|
|
|
2017-02-14 14:54:45 +01:00
|
|
|
pid_t fork_server_to_background()
|
|
|
|
{
|
|
|
|
if (pid_t pid = fork())
|
|
|
|
return pid;
|
|
|
|
|
2019-11-17 12:44:49 +01:00
|
|
|
setsid();
|
2017-02-14 14:54:45 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-03-07 21:12:21 +01:00
|
|
|
std::unique_ptr<UserInterface> create_local_ui(UIType ui_type)
|
|
|
|
{
|
2019-11-16 11:27:06 +01:00
|
|
|
if (ui_type != UIType::Terminal)
|
2016-03-07 21:12:21 +01:00
|
|
|
return make_ui(ui_type);
|
2015-08-26 20:33:52 +02:00
|
|
|
|
2019-11-16 11:27:06 +01:00
|
|
|
struct LocalUI : TerminalUI
|
2012-11-29 18:56:08 +01:00
|
|
|
{
|
2015-10-08 21:05:47 +02:00
|
|
|
LocalUI()
|
|
|
|
{
|
2019-09-09 12:11:04 +02:00
|
|
|
set_signal_handler(SIGTSTP, [](int) {
|
2015-10-08 21:05:47 +02:00
|
|
|
if (ClientManager::instance().count() == 1 and
|
|
|
|
*ClientManager::instance().begin() == local_client)
|
2019-11-16 11:27:06 +01:00
|
|
|
TerminalUI::instance().suspend();
|
2015-10-08 21:05:47 +02:00
|
|
|
else
|
|
|
|
convert_to_client_pending = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-08 23:30:15 +01:00
|
|
|
~LocalUI() override
|
2012-11-29 18:56:08 +01:00
|
|
|
{
|
2015-10-08 21:05:47 +02:00
|
|
|
local_client = nullptr;
|
2019-09-09 12:11:04 +02:00
|
|
|
if (convert_to_client_pending or
|
|
|
|
ClientManager::instance().empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (fork_server_to_background())
|
2012-11-29 18:56:08 +01:00
|
|
|
{
|
2019-11-16 11:27:06 +01:00
|
|
|
this->TerminalUI::~TerminalUI();
|
2019-09-09 12:11:04 +02:00
|
|
|
exit(local_client_exit);
|
2012-11-29 18:56:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-08-26 20:33:52 +02:00
|
|
|
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);
|
2017-03-08 20:33:25 +01:00
|
|
|
create_fifo_buffer("*stdin*", fd, Buffer::Flags::None);
|
2014-04-29 22:37:11 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 16:27:54 +01:00
|
|
|
return std::make_unique<LocalUI>();
|
2015-08-26 20:33:52 +02:00
|
|
|
}
|
|
|
|
|
2018-03-22 21:36:18 +01:00
|
|
|
int run_client(StringView session, StringView name, StringView client_init,
|
2017-11-12 15:28:13 +01:00
|
|
|
Optional<BufferCoord> init_coord, UIType ui_type,
|
|
|
|
bool suspend)
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2020-05-10 07:21:49 +02:00
|
|
|
Optional<int> stdin_fd;
|
2021-11-14 22:14:18 +01:00
|
|
|
// json-ui (or dummy) is not intended to be user interactive.
|
|
|
|
// So only worry about making the tty your stdin if:
|
|
|
|
// (a) ui_type is Terminal, *and*
|
|
|
|
// (b) fd 0 is not interactive.
|
|
|
|
if (ui_type == UIType::Terminal && not isatty(0))
|
2020-05-10 07:21:49 +02:00
|
|
|
{
|
|
|
|
// move stdin to another fd, and restore tty as stdin
|
|
|
|
stdin_fd = dup(0);
|
|
|
|
int tty = open("/dev/tty", O_RDONLY);
|
|
|
|
dup2(tty, 0);
|
|
|
|
close(tty);
|
|
|
|
}
|
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
EventManager event_manager;
|
2018-03-22 21:36:18 +01:00
|
|
|
RemoteClient client{session, name, make_ui(ui_type), getpid(), get_env_vars(),
|
2020-05-10 07:21:49 +02:00
|
|
|
client_init, std::move(init_coord), stdin_fd};
|
|
|
|
stdin_fd.map(close);
|
|
|
|
|
2017-11-12 15:28:13 +01:00
|
|
|
if (suspend)
|
|
|
|
raise(SIGTSTP);
|
2018-05-23 00:14:56 +02:00
|
|
|
while (not client.exit_status() and client.is_ui_ok())
|
2014-11-25 02:00:18 +01:00
|
|
|
event_manager.handle_next_events(EventMode::Normal);
|
2018-07-30 11:32:38 +02:00
|
|
|
return client.exit_status().value_or(-1);
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2016-12-20 11:34:48 +01:00
|
|
|
catch (disconnected& e)
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
2017-08-23 08:22:23 +02:00
|
|
|
write_stderr(format("{}\ndisconnecting\n", e.what()));
|
|
|
|
return -1;
|
2014-03-21 14:42:37 +01:00
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
|
|
|
|
2017-02-14 14:54:45 +01:00
|
|
|
struct convert_to_client_mode
|
|
|
|
{
|
|
|
|
String session;
|
2018-03-22 21:36:18 +01:00
|
|
|
String client_name;
|
2017-02-14 14:54:45 +01:00
|
|
|
String buffer_name;
|
|
|
|
String selections;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum class ServerFlags
|
|
|
|
{
|
2017-02-19 14:59:44 +01:00
|
|
|
None = 0,
|
2017-02-14 14:54:45 +01:00
|
|
|
IgnoreKakrc = 1 << 0,
|
2017-02-19 14:59:44 +01:00
|
|
|
Daemon = 1 << 1,
|
|
|
|
ReadOnly = 1 << 2,
|
|
|
|
StartupInfo = 1 << 3,
|
2017-02-14 14:54:45 +01:00
|
|
|
};
|
2017-03-15 18:55:34 +01:00
|
|
|
constexpr bool with_bit_ops(Meta::Type<ServerFlags>) { return true; }
|
2017-02-14 14:54:45 +01:00
|
|
|
|
2017-05-13 12:05:09 +02:00
|
|
|
int run_server(StringView session, StringView server_init,
|
|
|
|
StringView client_init, Optional<BufferCoord> init_coord,
|
2018-07-19 10:36:49 +02:00
|
|
|
ServerFlags flags, UIType ui_type, DebugFlags debug_flags,
|
2016-12-01 21:40:50 +01:00
|
|
|
ConstArrayView<StringView> files)
|
2012-10-21 13:02:24 +02:00
|
|
|
{
|
2013-09-23 20:28:15 +02:00
|
|
|
static bool terminate = false;
|
2020-07-07 12:41:40 +02:00
|
|
|
set_signal_handler(SIGTERM, [](int) { terminate = true; });
|
2021-07-12 02:08:17 +02:00
|
|
|
set_signal_handler(SIGINT, [](int) { terminate = true; });
|
2020-08-17 11:56:00 +02:00
|
|
|
if ((flags & ServerFlags::Daemon) and session.empty())
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
2020-08-17 11:56:00 +02:00
|
|
|
write_stderr("-d needs a session name to be specified with -s\n");
|
|
|
|
return -1;
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2013-09-19 21:09:53 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
EventManager event_manager;
|
2020-10-19 11:39:25 +02:00
|
|
|
Server server{session.empty() ? to_string(getpid()) : session.str(),
|
|
|
|
(bool)(flags & ServerFlags::Daemon)};
|
2017-10-10 02:04:19 +02:00
|
|
|
|
|
|
|
StringRegistry string_registry;
|
2014-10-30 15:00:42 +01:00
|
|
|
GlobalScope global_scope;
|
2018-02-18 04:52:29 +01:00
|
|
|
ShellManager shell_manager{builtin_env_vars};
|
2013-09-23 20:28:15 +02:00
|
|
|
CommandManager command_manager;
|
|
|
|
RegisterManager register_manager;
|
|
|
|
HighlighterRegistry highlighter_registry;
|
2020-08-08 03:19:02 +02:00
|
|
|
SharedHighlighters defined_highlighters;
|
2016-03-02 21:27:47 +01:00
|
|
|
ClientManager client_manager;
|
2016-03-03 14:55:35 +01:00
|
|
|
BufferManager buffer_manager;
|
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_registers();
|
2019-09-15 11:40:38 +02:00
|
|
|
register_keymaps();
|
2013-09-23 20:28:15 +02:00
|
|
|
register_commands();
|
|
|
|
register_highlighters();
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2018-07-19 10:36:49 +02:00
|
|
|
global_scope.options()["debug"].set(debug_flags);
|
|
|
|
|
2018-12-21 13:03:21 +01:00
|
|
|
write_to_debug_buffer("*** This is the debug buffer, where debug info will be written ***");
|
|
|
|
|
2021-07-26 08:57:07 +02:00
|
|
|
#ifdef KAK_DEBUG
|
2018-12-21 13:03:21 +01:00
|
|
|
const auto start_time = Clock::now();
|
2015-10-16 14:52:14 +02:00
|
|
|
UnitTest::run_all_tests();
|
|
|
|
|
2018-12-21 13:03:21 +01:00
|
|
|
if (debug_flags & DebugFlags::Profile)
|
2018-12-27 20:27:35 +01:00
|
|
|
{
|
|
|
|
using namespace std::chrono;
|
2018-12-21 13:03:21 +01:00
|
|
|
write_to_debug_buffer(format("running the unit tests took {} ms",
|
2018-12-27 20:27:35 +01:00
|
|
|
duration_cast<milliseconds>(Clock::now() - start_time).count()));
|
|
|
|
}
|
2021-07-26 08:57:07 +02:00
|
|
|
#endif
|
2012-10-23 22:56:24 +02:00
|
|
|
|
2015-07-08 14:43:40 +02:00
|
|
|
bool startup_error = false;
|
2017-02-14 14:54:45 +01:00
|
|
|
if (not (flags & ServerFlags::IgnoreKakrc)) try
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
2017-05-13 12:05:09 +02:00
|
|
|
Context init_context{Context::EmptyContextFlag{}};
|
2015-06-01 20:06:35 +02:00
|
|
|
command_manager.execute(format("source {}/kakrc", runtime_directory()),
|
2017-05-13 12:05:09 +02:00
|
|
|
init_context);
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2017-02-14 14:54:45 +01:00
|
|
|
catch (runtime_error& error)
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
2015-07-08 14:43:40 +02:00
|
|
|
startup_error = true;
|
2016-10-17 19:37:05 +02:00
|
|
|
write_to_debug_buffer(format("error while parsing kakrc:\n"
|
|
|
|
" {}", error.what()));
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2012-10-16 17:15:09 +02:00
|
|
|
|
2017-05-13 12:05: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()));
|
|
|
|
}
|
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
2015-04-19 19:47:52 +02:00
|
|
|
Context empty_context{Context::EmptyContextFlag{}};
|
2018-10-22 23:15:53 +02:00
|
|
|
global_scope.hooks().run_hook(Hook::KakBegin, session, 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.
|
2016-03-08 22:35:56 +01:00
|
|
|
for (auto& file : files | reverse())
|
2013-04-29 13:50:13 +02:00
|
|
|
{
|
2015-07-08 14:43:40 +02:00
|
|
|
try
|
|
|
|
{
|
2016-07-20 19:45:50 +02:00
|
|
|
Buffer *buffer = open_or_create_file_buffer(file);
|
2017-02-14 14:54:45 +01:00
|
|
|
if (flags & ServerFlags::ReadOnly)
|
2021-08-22 14:59:16 +02:00
|
|
|
{
|
2016-07-20 19:45:50 +02:00
|
|
|
buffer->flags() |= Buffer::Flags::ReadOnly;
|
2021-08-22 14:59:16 +02:00
|
|
|
buffer->options().get_local_option("readonly").set(true);
|
|
|
|
}
|
2015-07-08 14:43:40 +02:00
|
|
|
}
|
2017-02-14 14:54:45 +01:00
|
|
|
catch (runtime_error& error)
|
2015-07-08 14:43:40 +02:00
|
|
|
{
|
|
|
|
startup_error = true;
|
2016-10-17 19:37:05 +02:00
|
|
|
write_to_debug_buffer(format("error while opening file '{}':\n"
|
|
|
|
" {}", file, error.what()));
|
2015-07-08 14:43:40 +02:00
|
|
|
}
|
2013-04-29 13:50:13 +02:00
|
|
|
}
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2017-02-14 14:54:45 +01:00
|
|
|
catch (runtime_error& error)
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
2015-06-06 12:54:48 +02:00
|
|
|
write_to_debug_buffer(format("error while opening command line files: {}", error.what()));
|
2013-09-23 20:28:15 +02:00
|
|
|
}
|
2012-12-28 13:51:14 +01:00
|
|
|
|
2016-09-06 23:46:27 +02:00
|
|
|
try
|
2015-12-05 11:14:04 +01:00
|
|
|
{
|
2020-10-19 11:39:25 +02:00
|
|
|
if (not server.is_daemon())
|
2017-02-19 14:59:44 +01:00
|
|
|
{
|
2016-12-01 21:40:50 +01:00
|
|
|
local_client = client_manager.create_client(
|
2018-03-22 21:36:18 +01:00
|
|
|
create_local_ui(ui_type), getpid(), {}, get_env_vars(), client_init, std::move(init_coord),
|
2017-08-23 08:22:23 +02:00
|
|
|
[](int status) { local_client_exit = status; });
|
2016-12-01 21:40:50 +01:00
|
|
|
|
2018-04-18 23:56:37 +02:00
|
|
|
if (startup_error and local_client)
|
2017-02-19 14:59:44 +01:00
|
|
|
local_client->print_status({
|
2021-04-27 12:41:27 +02:00
|
|
|
"error during startup, see `:buffer *debug*` for details",
|
2018-04-07 07:36:39 +02:00
|
|
|
local_client->context().faces()["Error"]
|
2017-02-19 14:59:44 +01:00
|
|
|
});
|
|
|
|
|
2018-04-18 23:56:37 +02:00
|
|
|
if (flags & ServerFlags::StartupInfo and local_client)
|
2018-06-04 01:39:32 +02:00
|
|
|
show_startup_info(local_client, global_scope.options()["startup_info_version"].get<int>());
|
2017-02-19 14:59:44 +01:00
|
|
|
}
|
2012-10-30 14:00:44 +01:00
|
|
|
|
2017-11-12 15:28:13 +01:00
|
|
|
while (not terminate and
|
2020-10-19 11:39:25 +02:00
|
|
|
(not client_manager.empty() or server.negotiating() or server.is_daemon()))
|
2015-08-26 20:34:19 +02:00
|
|
|
{
|
|
|
|
client_manager.redraw_clients();
|
2018-05-15 13:37:41 +02:00
|
|
|
|
2018-05-17 14:50:11 +02:00
|
|
|
// Loop so that eventual inputs happening during the processing are handled as
|
|
|
|
// well, avoiding unneeded redraws.
|
2018-05-15 13:37:41 +02:00
|
|
|
bool allow_blocking = not client_manager.has_pending_inputs();
|
|
|
|
while (event_manager.handle_next_events(EventMode::Normal, nullptr, allow_blocking))
|
|
|
|
{
|
2021-03-10 23:08:35 +01:00
|
|
|
if (client_manager.process_pending_inputs())
|
|
|
|
break;
|
2018-05-15 13:37:41 +02:00
|
|
|
allow_blocking = false;
|
|
|
|
}
|
2018-05-17 14:50:11 +02:00
|
|
|
client_manager.process_pending_inputs();
|
|
|
|
|
2016-09-04 18:54:07 +02:00
|
|
|
client_manager.clear_client_trash();
|
2016-05-13 21:32:53 +02:00
|
|
|
client_manager.clear_window_trash();
|
2015-08-26 20:34:19 +02:00
|
|
|
buffer_manager.clear_buffer_trash();
|
2019-01-20 12:46:40 +01:00
|
|
|
global_scope.option_registry().clear_option_trash();
|
2015-10-08 21:05:47 +02:00
|
|
|
|
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())
|
2017-09-11 08:21:07 +02:00
|
|
|
{
|
2018-04-29 14:27:28 +02:00
|
|
|
ClientManager::instance().remove_client(*local_client, false, -1);
|
2018-04-29 14:58:37 +02:00
|
|
|
local_client = nullptr;
|
2017-09-11 08:21:07 +02:00
|
|
|
if (not client_manager.empty() and fork_server_to_background())
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else if (convert_to_client_pending)
|
2015-10-08 21:05:47 +02:00
|
|
|
{
|
2018-04-18 23:56:37 +02:00
|
|
|
kak_assert(local_client);
|
2019-11-12 11:56:45 +01:00
|
|
|
auto& local_context = local_client->context();
|
|
|
|
const String client_name = local_context.name();
|
|
|
|
const String buffer_name = local_context.buffer().name();
|
|
|
|
const String selections = selection_list_to_string(ColumnType::Byte, local_context.selections());
|
2015-11-16 14:52:33 +01:00
|
|
|
|
2017-08-23 08:22:23 +02:00
|
|
|
ClientManager::instance().remove_client(*local_client, true, 0);
|
2016-10-07 00:35:32 +02:00
|
|
|
client_manager.clear_client_trash();
|
2015-10-08 21:05:47 +02:00
|
|
|
convert_to_client_pending = false;
|
|
|
|
|
|
|
|
if (fork_server_to_background())
|
|
|
|
{
|
2019-04-12 01:02:59 +02:00
|
|
|
ClientManager::instance().clear(false);
|
2015-10-08 21:05:47 +02:00
|
|
|
String session = server.session();
|
|
|
|
server.close_session(false);
|
2018-03-22 21:36:18 +01:00
|
|
|
throw convert_to_client_mode{ std::move(session), std::move(client_name), std::move(buffer_name), std::move(selections) };
|
2015-10-08 21:05:47 +02:00
|
|
|
}
|
|
|
|
}
|
2015-08-26 20:34:19 +02:00
|
|
|
}
|
2014-08-12 20:24:09 +02:00
|
|
|
}
|
2018-07-19 11:27:56 +02:00
|
|
|
catch (const kill_session& kill)
|
|
|
|
{
|
|
|
|
local_client_exit = kill.exit_status;
|
|
|
|
}
|
2013-04-19 13:45:44 +02:00
|
|
|
|
2013-09-23 20:28:15 +02:00
|
|
|
{
|
2015-04-19 19:47:52 +02:00
|
|
|
Context empty_context{Context::EmptyContextFlag{}};
|
2018-10-22 23:15:53 +02:00
|
|
|
global_scope.hooks().run_hook(Hook::KakEnd, "", empty_context);
|
2013-09-19 20:53:04 +02:00
|
|
|
}
|
2014-08-14 21:37:36 +02:00
|
|
|
|
2017-08-23 08:22:23 +02:00
|
|
|
return local_client_exit;
|
2013-09-19 20:53:04 +02:00
|
|
|
}
|
|
|
|
|
2018-02-18 04:54:21 +01:00
|
|
|
int run_filter(StringView keystr, ConstArrayView<StringView> files, bool quiet, StringView suffix_backup)
|
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;
|
2015-12-08 01:12:50 +01:00
|
|
|
EventManager event_manager;
|
2018-02-18 04:52:29 +01:00
|
|
|
ShellManager shell_manager{builtin_env_vars};
|
2014-10-26 21:21:01 +01:00
|
|
|
RegisterManager register_manager;
|
2016-03-03 14:55:35 +01:00
|
|
|
BufferManager buffer_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_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
|
|
|
|
2015-12-03 14:48:30 +01:00
|
|
|
auto apply_to_buffer = [&](Buffer& buffer)
|
2014-08-17 16:19:04 +02:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2014-12-19 00:12:58 +01:00
|
|
|
InputHandler input_handler{
|
2015-02-02 19:46:55 +01:00
|
|
|
{ buffer, Selection{{0,0}, buffer.back_coord()} },
|
2018-05-13 09:59:01 +02:00
|
|
|
Context::Flags::Draft
|
2014-12-19 00:12:58 +01:00
|
|
|
};
|
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);
|
|
|
|
}
|
2017-02-14 14:54:45 +01:00
|
|
|
catch (runtime_error& err)
|
2014-08-17 16:19:04 +02:00
|
|
|
{
|
2014-11-12 00:40:07 +01:00
|
|
|
if (not quiet)
|
2015-04-01 14:44:04 +02:00
|
|
|
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-15 00:51:24 +02:00
|
|
|
|
2014-08-17 16:19:04 +02:00
|
|
|
for (auto& file : files)
|
|
|
|
{
|
2018-02-18 04:48:24 +01:00
|
|
|
Buffer* buffer = open_file_buffer(file, Buffer::Flags::NoHooks);
|
2017-03-22 14:47:25 +01:00
|
|
|
if (not suffix_backup.empty())
|
2019-02-12 10:18:34 +01:00
|
|
|
write_buffer_to_file(*buffer, buffer->name() + suffix_backup,
|
2019-02-12 10:54:37 +01:00
|
|
|
WriteMethod::Overwrite, WriteFlags::None);
|
2015-12-03 14:48:30 +01:00
|
|
|
apply_to_buffer(*buffer);
|
2019-02-12 10:18:34 +01:00
|
|
|
write_buffer_to_file(*buffer, buffer->name(),
|
2019-02-12 10:54:37 +01:00
|
|
|
WriteMethod::Overwrite, WriteFlags::None);
|
2014-08-17 16:19:04 +02:00
|
|
|
buffer_manager.delete_buffer(*buffer);
|
|
|
|
}
|
|
|
|
if (not isatty(0))
|
|
|
|
{
|
2021-05-28 09:03:06 +02:00
|
|
|
Buffer& buffer = *create_buffer_from_string(
|
|
|
|
"*stdin*", Buffer::Flags::NoHooks, read_fd(0));
|
2016-07-10 17:01:33 +02:00
|
|
|
apply_to_buffer(buffer);
|
|
|
|
write_buffer_to_fd(buffer, 1);
|
|
|
|
buffer_manager.delete_buffer(buffer);
|
2014-08-17 16:19:04 +02:00
|
|
|
}
|
2014-08-15 00:51:24 +02:00
|
|
|
}
|
2017-02-14 14:54:45 +01:00
|
|
|
catch (runtime_error& err)
|
2014-08-15 14:21:54 +02:00
|
|
|
{
|
2015-04-01 14:44:04 +02:00
|
|
|
write_stderr(format("error: {}\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)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2017-06-12 06:21:34 +02:00
|
|
|
send_command(session, read_fd(0));
|
2014-08-14 21:37:36 +02:00
|
|
|
}
|
2016-12-20 11:34:48 +01:00
|
|
|
catch (disconnected& e)
|
2014-08-14 21:37:36 +02:00
|
|
|
{
|
2016-11-29 20:12:56 +01:00
|
|
|
write_stderr(format("{}\ndisconnecting\n", e.what()));
|
2017-08-23 08:22:23 +02:00
|
|
|
return -1;
|
2014-08-14 21:37:36 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-02-14 14:54:45 +01:00
|
|
|
void signal_handler(int signal)
|
2016-06-11 14:22:24 +02:00
|
|
|
{
|
2019-11-16 11:27:06 +01:00
|
|
|
TerminalUI::restore_terminal();
|
2017-02-14 14:54:45 +01:00
|
|
|
const char* text = nullptr;
|
|
|
|
switch (signal)
|
|
|
|
{
|
|
|
|
case SIGSEGV: text = "SIGSEGV"; break;
|
|
|
|
case SIGFPE: text = "SIGFPE"; break;
|
|
|
|
case SIGQUIT: text = "SIGQUIT"; break;
|
|
|
|
case SIGPIPE: text = "SIGPIPE"; break;
|
|
|
|
}
|
2020-07-07 12:41:40 +02:00
|
|
|
auto msg = format("Received {}, exiting.\nPid: {}\nCallstack:\n{}",
|
|
|
|
text, getpid(), Backtrace{}.desc());
|
|
|
|
write_stderr(msg);
|
|
|
|
notify_fatal_error(msg);
|
2017-02-14 14:54:45 +01:00
|
|
|
|
|
|
|
if (Server::has_instance())
|
|
|
|
Server::instance().close_session();
|
|
|
|
if (BufferManager::has_instance())
|
|
|
|
BufferManager::instance().backup_modified_buffers();
|
|
|
|
|
2020-07-07 12:41:40 +02:00
|
|
|
if (signal == SIGSEGV)
|
2018-12-13 11:29:33 +01:00
|
|
|
{
|
|
|
|
// generate core dump
|
|
|
|
::signal(SIGSEGV, SIG_DFL);
|
2019-01-19 00:34:12 +01:00
|
|
|
::kill(getpid(), SIGSEGV);
|
2018-12-13 11:29:33 +01:00
|
|
|
}
|
2017-02-14 14:54:45 +01:00
|
|
|
else
|
|
|
|
abort();
|
|
|
|
}
|
2016-06-11 14:22:24 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-09-19 20:53:04 +02:00
|
|
|
int main(int argc, char* argv[])
|
|
|
|
{
|
2017-02-14 14:54:45 +01:00
|
|
|
using namespace Kakoune;
|
|
|
|
|
2016-05-19 22:45:23 +02:00
|
|
|
setlocale(LC_ALL, "");
|
2013-09-19 20:53:04 +02:00
|
|
|
|
2016-01-10 21:46:15 +01:00
|
|
|
set_signal_handler(SIGSEGV, signal_handler);
|
|
|
|
set_signal_handler(SIGFPE, signal_handler);
|
|
|
|
set_signal_handler(SIGQUIT, signal_handler);
|
|
|
|
set_signal_handler(SIGTERM, signal_handler);
|
2018-12-06 10:33:13 +01:00
|
|
|
set_signal_handler(SIGPIPE, [](int){});
|
2016-01-10 21:46:15 +01:00
|
|
|
set_signal_handler(SIGINT, [](int){});
|
|
|
|
set_signal_handler(SIGCHLD, [](int){});
|
2019-11-18 10:57:49 +01:00
|
|
|
set_signal_handler(SIGTTOU, SIG_IGN);
|
2013-09-19 20:53:04 +02:00
|
|
|
|
2014-02-11 23:16:17 +01:00
|
|
|
const ParameterDesc param_desc{
|
2015-03-14 18:30:34 +01:00
|
|
|
SwitchMap{ { "c", { true, "connect to given session" } },
|
2017-05-13 12:05:09 +02:00
|
|
|
{ "e", { true, "execute argument on client initialisation" } },
|
|
|
|
{ "E", { true, "execute argument on server initialisation" } },
|
2014-02-11 23:16:17 +01:00
|
|
|
{ "n", { false, "do not source kakrc files on startup" } },
|
2015-03-14 18:30:34 +01:00
|
|
|
{ "s", { true, "set session name" } },
|
2014-03-02 03:01:09 +01:00
|
|
|
{ "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" } },
|
2018-09-20 15:08:55 +02:00
|
|
|
{ "f", { true, "filter: for each file, select the entire buffer and execute the given keys" } },
|
2017-03-22 14:47:25 +01:00
|
|
|
{ "i", { true, "backup the files on which a filter is applied using the given suffix" } },
|
2015-03-24 14:14:02 +01:00
|
|
|
{ "q", { false, "in filter mode, be quiet about errors applying keys" } },
|
2019-11-16 11:27:06 +01:00
|
|
|
{ "ui", { true, "set the type of user interface to use (terminal, dummy, or json)" } },
|
2016-06-06 20:28:56 +02:00
|
|
|
{ "l", { false, "list existing sessions" } },
|
2016-07-20 19:45:50 +02:00
|
|
|
{ "clear", { false, "clear dead sessions" } },
|
2018-07-19 10:36:49 +02:00
|
|
|
{ "debug", { true, "initial debug option value" } },
|
2018-08-09 11:24:11 +02:00
|
|
|
{ "version", { false, "display kakoune version and exit" } },
|
2017-03-07 16:11:29 +01:00
|
|
|
{ "ro", { false, "readonly mode" } },
|
|
|
|
{ "help", { false, "display a help message and quit" } } }
|
2014-02-11 23:16:17 +01:00
|
|
|
};
|
2017-03-07 16:11:29 +01:00
|
|
|
|
2014-02-11 23:16:17 +01:00
|
|
|
try
|
|
|
|
{
|
2017-03-07 16:11:29 +01:00
|
|
|
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))
|
2017-03-07 16:11:29 +01:00
|
|
|
return show_usage();
|
|
|
|
|
2017-06-12 07:12:10 +02:00
|
|
|
ParametersParser parser{params, param_desc};
|
2014-08-15 00:57:13 +02:00
|
|
|
|
2017-03-07 16:11:29 +01:00
|
|
|
const bool show_help_message = (bool)parser.get_switch("help");
|
|
|
|
if (show_help_message)
|
|
|
|
return show_usage();
|
|
|
|
|
2018-08-09 11:24:11 +02:00
|
|
|
if (parser.get_switch("version"))
|
|
|
|
{
|
|
|
|
write_stdout(format("Kakoune {}\n", Kakoune::version));
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-06-06 20:28:56 +02:00
|
|
|
const bool list_sessions = (bool)parser.get_switch("l");
|
|
|
|
const bool clear_sessions = (bool)parser.get_switch("clear");
|
|
|
|
if (list_sessions or clear_sessions)
|
2015-08-23 15:22:23 +02:00
|
|
|
{
|
2019-08-20 22:13:21 +02:00
|
|
|
for (auto& session : list_files(session_directory()))
|
2016-06-06 20:28:56 +02:00
|
|
|
{
|
|
|
|
const bool valid = check_session(session);
|
|
|
|
if (list_sessions)
|
|
|
|
write_stdout(format("{}{}\n", session, valid ? "" : " (dead)"));
|
|
|
|
if (not valid and clear_sessions)
|
2019-08-20 22:13:21 +02:00
|
|
|
unlink(session_path(session).c_str());
|
2016-06-06 20:28:56 +02:00
|
|
|
}
|
2015-08-23 15:22:23 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2016-06-06 20:28:56 +02:00
|
|
|
|
2015-03-14 20:16:46 +01:00
|
|
|
if (auto session = parser.get_switch("p"))
|
2014-08-15 00:57:13 +02:00
|
|
|
{
|
2017-05-13 12:05:09 +02:00
|
|
|
for (auto opt : { "c", "n", "s", "d", "e", "E", "ro" })
|
2014-08-15 00:57:13 +02:00
|
|
|
{
|
2015-03-14 20:16:46 +01:00
|
|
|
if (parser.get_switch(opt))
|
2014-08-15 00:57:13 +02:00
|
|
|
{
|
2016-07-20 20:11:36 +02:00
|
|
|
write_stderr(format("error: -{} is incompatible with -p\n", opt));
|
2014-08-15 00:57:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-03-14 20:16:46 +01:00
|
|
|
return run_pipe(*session);
|
2014-08-15 00:57:13 +02:00
|
|
|
}
|
2015-12-03 14:48:30 +01:00
|
|
|
|
2017-05-13 12:05:09 +02:00
|
|
|
auto client_init = parser.get_switch("e").value_or(StringView{});
|
|
|
|
auto server_init = parser.get_switch("E").value_or(StringView{});
|
2019-11-16 11:27:06 +01:00
|
|
|
const UIType ui_type = parse_ui_type(parser.get_switch("ui").value_or("terminal"));
|
2015-12-03 14:48:30 +01:00
|
|
|
|
|
|
|
if (auto keys = parser.get_switch("f"))
|
2014-08-15 00:57:13 +02:00
|
|
|
{
|
2016-07-20 20:10:13 +02:00
|
|
|
if (parser.get_switch("ro"))
|
|
|
|
{
|
2016-07-20 20:11:36 +02:00
|
|
|
write_stderr("error: -ro is incompatible with -f\n");
|
2016-07-20 20:10:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
Vector<StringView> files;
|
2014-08-15 00:57:13 +02:00
|
|
|
for (size_t i = 0; i < parser.positional_count(); ++i)
|
|
|
|
files.emplace_back(parser[i]);
|
|
|
|
|
2018-02-18 04:54:21 +01:00
|
|
|
return run_filter(*keys, files, (bool)parser.get_switch("q"),
|
2017-03-22 14:47:25 +01:00
|
|
|
parser.get_switch("i").value_or(StringView{}));
|
2014-08-15 00:57:13 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 13:14:44 +01:00
|
|
|
Vector<StringView> files;
|
|
|
|
Optional<BufferCoord> init_coord;
|
|
|
|
for (auto& name : parser)
|
|
|
|
{
|
|
|
|
if (not name.empty() and name[0_byte] == '+')
|
|
|
|
{
|
2017-01-31 19:44:27 +01:00
|
|
|
if (name == "+" or name == "+:")
|
|
|
|
{
|
2017-05-13 12:05:09 +02:00
|
|
|
client_init = client_init + "; exec gj";
|
2017-01-31 19:44:27 +01:00
|
|
|
continue;
|
|
|
|
}
|
2017-01-21 13:14:44 +01:00
|
|
|
auto colon = find(name, ':');
|
|
|
|
if (auto line = str_to_int_ifp({name.begin()+1, colon}))
|
|
|
|
{
|
2019-10-03 13:18:51 +02:00
|
|
|
init_coord = std::max<BufferCoord>({0,0}, {
|
2017-01-21 13:14:44 +01:00
|
|
|
*line - 1,
|
|
|
|
colon != name.end() ?
|
|
|
|
str_to_int_ifp({colon+1, name.end()}).value_or(1) - 1
|
|
|
|
: 0
|
2019-10-03 13:18:51 +02:00
|
|
|
});
|
2017-01-21 13:14:44 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
files.emplace_back(name);
|
|
|
|
}
|
|
|
|
|
2015-03-14 20:16:46 +01:00
|
|
|
if (auto server_session = parser.get_switch("c"))
|
2014-08-15 00:57:13 +02:00
|
|
|
{
|
2017-05-13 12:05:09 +02:00
|
|
|
for (auto opt : { "n", "s", "d", "E", "ro" })
|
2014-08-15 00:57:13 +02:00
|
|
|
{
|
2015-03-14 20:16:46 +01:00
|
|
|
if (parser.get_switch(opt))
|
2014-08-15 00:57:13 +02:00
|
|
|
{
|
2016-07-20 20:11:36 +02:00
|
|
|
write_stderr(format("error: -{} is incompatible with -c\n", opt));
|
2014-08-15 00:57:13 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 12:48:02 +01:00
|
|
|
String new_files;
|
2020-04-30 22:48:32 +02:00
|
|
|
for (auto name : files) {
|
|
|
|
new_files += format("edit '{}'", escape(real_path(name), "'", '\\'));
|
|
|
|
if (init_coord) {
|
|
|
|
new_files += format(" {} {}", init_coord->line + 1, init_coord->column + 1);
|
|
|
|
init_coord.reset();
|
|
|
|
}
|
|
|
|
new_files += ";";
|
|
|
|
}
|
2015-11-22 12:48:02 +01:00
|
|
|
|
2018-03-22 21:36:18 +01:00
|
|
|
return run_client(*server_session, {}, new_files + client_init, init_coord, ui_type, false);
|
2014-08-15 00:57:13 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2015-03-14 20:16:46 +01:00
|
|
|
StringView session = parser.get_switch("s").value_or(StringView{});
|
2015-10-08 21:05:47 +02:00
|
|
|
try
|
|
|
|
{
|
2020-01-25 09:33:37 +01:00
|
|
|
auto ignore_kakrc = (bool)parser.get_switch("n");
|
2020-02-07 12:27:54 +01:00
|
|
|
auto flags = (ignore_kakrc ? ServerFlags::IgnoreKakrc : ServerFlags::None) |
|
|
|
|
(parser.get_switch("d") ? ServerFlags::Daemon : ServerFlags::None) |
|
|
|
|
(parser.get_switch("ro") ? ServerFlags::ReadOnly : ServerFlags::None) |
|
|
|
|
((argc == 1 or (ignore_kakrc and argc == 2))
|
|
|
|
and isatty(0) ? ServerFlags::StartupInfo : ServerFlags::None);
|
2018-07-19 10:36:49 +02:00
|
|
|
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);
|
2015-10-08 21:05:47 +02:00
|
|
|
}
|
|
|
|
catch (convert_to_client_mode& convert)
|
|
|
|
{
|
2018-03-22 21:36:18 +01:00
|
|
|
return run_client(convert.session, convert.client_name,
|
2016-08-05 09:16:43 +02:00
|
|
|
format("try %^buffer '{}'; select '{}'^; echo converted to client only mode",
|
2017-11-12 15:28:13 +01:00
|
|
|
escape(convert.buffer_name, "'^", '\\'), convert.selections), {}, ui_type, true);
|
2015-10-08 21:05:47 +02:00
|
|
|
}
|
2014-08-15 00:57:13 +02:00
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
}
|
2017-02-14 14:54:45 +01:00
|
|
|
catch (parameter_error& error)
|
2014-01-23 20:36:07 +01:00
|
|
|
{
|
2015-08-18 01:28:04 +02:00
|
|
|
write_stderr(format("Error while parsing parameters: {}\n"
|
2015-04-01 14:44:04 +02:00
|
|
|
"Valid switches:\n"
|
|
|
|
"{}", error.what(),
|
|
|
|
generate_switches_doc(param_desc.switches)));
|
2014-01-23 20:36:07 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2011-09-09 21:24:18 +02:00
|
|
|
catch (Kakoune::exception& error)
|
|
|
|
{
|
2019-11-08 22:19:45 +01:00
|
|
|
write_stderr(format("Fatal error: {}\n", error.what()));
|
2013-02-22 18:45:59 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-03-22 14:29:22 +01:00
|
|
|
catch (std::exception& error)
|
|
|
|
{
|
2016-12-20 13:49:05 +01:00
|
|
|
write_stderr(format("uncaught exception ({}):\n{}\n", typeid(error).name(), error.what()));
|
2013-03-22 14:29:22 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-02-22 18:45:59 +01:00
|
|
|
catch (...)
|
|
|
|
{
|
2016-03-03 14:56:42 +01:00
|
|
|
write_stderr("uncaught exception");
|
2011-09-09 21:24:18 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-09-02 18:51:20 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2019-01-23 10:27:16 +01:00
|
|
|
|
2019-01-23 10:59:32 +01:00
|
|
|
#if defined(__ELF__)
|
2021-10-25 15:12:42 +02:00
|
|
|
#ifdef __arm__
|
|
|
|
# define PROGBITS "%progbits"
|
|
|
|
#else
|
|
|
|
# define PROGBITS "@progbits"
|
|
|
|
#endif
|
|
|
|
asm(".pushsection \".debug_gdb_scripts\", \"MS\"," PROGBITS ",1" R"(
|
2019-01-23 10:27:16 +01:00
|
|
|
.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"
|
2019-05-29 14:40:16 +02:00
|
|
|
.ascii "gdb.printing.register_pretty_printer(gdb.current_objfile(), kakoune.build_pretty_printer())\n\0"
|
2019-01-23 10:27:16 +01:00
|
|
|
.popsection
|
|
|
|
)");
|
2019-01-23 10:59:32 +01:00
|
|
|
#endif
|