diff --git a/src/assert.cc b/src/assert.cc index b193f595e..eeb4b36e0 100644 --- a/src/assert.cc +++ b/src/assert.cc @@ -38,7 +38,7 @@ bool notify_fatal_error(const String& msg) return true; } #elif defined(__linux__) - auto cmd = "xmessage -buttons 'quit:0,ignore:1' '" + msg + "'"; + auto cmd = format("xmessage -buttons 'quit:0,ignore:1' '{}'", msg); if (system(cmd.c_str()) == 1) return true; #endif diff --git a/src/client.cc b/src/client.cc index 71e8c29f3..a7d3b59aa 100644 --- a/src/client.cc +++ b/src/client.cc @@ -175,7 +175,7 @@ void Client::reload_buffer() kak_assert(buffer.flags() & Buffer::Flags::File); Buffer* buf = create_buffer_from_file(buffer.name()); kak_assert(buf == &buffer); - context().print_status({ "'" + buffer.display_name() + "' reloaded", + context().print_status({ format("'{}' reloaded", buffer.display_name()), get_face("Information") }); } @@ -189,12 +189,12 @@ void Client::on_buffer_reload_key(Key key) { // reread timestamp in case the file was modified again buffer.set_fs_timestamp(get_fs_timestamp(buffer.name())); - print_status({ "'" + buffer.display_name() + "' kept", + print_status({ format("'{}' kept", buffer.display_name()), get_face("Information") }); } else { - print_status({ "'" + key_to_str(key) + "' is not a valid choice", + print_status({ format("'{}' is not a valid choice", key_to_str(key)), get_face("Error") }); m_input_handler.on_next_key(KeymapMode::None, [this](Key key, Context&){ on_buffer_reload_key(key); }); return; @@ -233,9 +233,10 @@ void Client::check_if_buffer_needs_reloading() if (reload == Ask) { m_ui->info_show( - "reload '" + buffer.display_name() + "' ?", - "'" + buffer.display_name() + "' was modified externally\n" - "press or y to reload, or n to keep", + format("reload '{}' ?", buffer.display_name()), + format("'{}' was modified externally\n" + "press or y to reload, or n to keep", + buffer.display_name()), CharCoord{}, get_face("Information"), InfoStyle::Prompt); m_buffer_reload_dialog_opened = true; diff --git a/src/client_manager.cc b/src/client_manager.cc index ecade79c5..5e4978e62 100644 --- a/src/client_manager.cc +++ b/src/client_manager.cc @@ -110,8 +110,9 @@ void ClientManager::ensure_no_client_uses_buffer(Buffer& buffer) continue; if (client->context().is_editing()) - throw runtime_error("client '" + client->context().name() + "' is inserting in '" + - buffer.display_name() + "'"); + throw runtime_error(format("client '{}' is inserting in buffer '{}'", + client->context().name(), + buffer.display_name())); // change client context to edit the first buffer which is not the // specified one. As BufferManager stores buffer according to last @@ -150,7 +151,7 @@ Client& ClientManager::get_client(StringView name) { if (Client* client = get_client_ifp(name)) return *client; - throw runtime_error("no client named: " + name); + throw runtime_error(format("no client named '{}'", name)); } void ClientManager::redraw_clients() const diff --git a/src/commands.cc b/src/commands.cc index 65328f01d..e496124a4 100644 --- a/src/commands.cc +++ b/src/commands.cc @@ -45,7 +45,7 @@ Buffer* open_fifo(StringView name, StringView filename, bool scroll) int fd = open(parse_filename(filename).c_str(), O_RDONLY | O_NONBLOCK); fcntl(fd, F_SETFD, FD_CLOEXEC); if (fd < 0) - throw runtime_error("unable to open " + filename); + throw runtime_error(format("unable to open '{}'", filename)); return create_fifo_buffer(name.str(), fd, scroll); } @@ -109,7 +109,7 @@ Scope& get_scope(StringView scope, const Context& context) { if (auto s = get_scope_ifp(scope, context)) return *s; - throw runtime_error("error: no such scope " + scope); + throw runtime_error(format("error: no such scope '{}'", scope)); } struct CommandDesc @@ -158,9 +158,10 @@ void edit(const ParametersParser& parser, Context& context) if (not buffer) { if (parser.get_switch("existing")) - throw runtime_error("unable to open " + name); + throw runtime_error(format("unable to open '{}'", name)); - context.print_status({ "new file " + name, get_face("StatusLine") }); + context.print_status({ format("new file '{}'", name), + get_face("StatusLine") }); buffer = new Buffer(name, Buffer::Flags::File | Buffer::Flags::New); } } @@ -444,10 +445,10 @@ void delete_buffer(const ParametersParser& parser, Context& context) BufferManager& manager = BufferManager::instance(); Buffer& buffer = parser.positional_count() == 0 ? context.buffer() : manager.get_buffer(parser[0]); if (not force and (buffer.flags() & Buffer::Flags::File) and buffer.is_modified()) - throw runtime_error("buffer " + buffer.name() + " is modified"); + throw runtime_error(format("buffer '{}' is modified", buffer.name())); if (manager.count() == 1) - throw runtime_error("buffer " + buffer.name() + " is the last one"); + throw runtime_error(format("buffer '{}' is the last one", buffer.name())); manager.delete_buffer(buffer); } @@ -486,7 +487,7 @@ const CommandDesc namebuf_cmd = { [](const ParametersParser& parser, Context& context) { if (not context.buffer().set_name(parser[0])) - throw runtime_error("unable to change buffer name to " + parser[0]); + throw runtime_error(format("unable to change buffer name to '{}'", parser[0])); } }; @@ -573,7 +574,7 @@ const CommandDesc add_highlighter_cmd = { HighlighterRegistry& registry = HighlighterRegistry::instance(); auto it = registry.find(params[0]); if (it != registry.end()) - return params[0] + ":\n" + indent(it->second.docstring); + return format("{}:\n{}", params[0], indent(it->second.docstring)); } return ""; }, @@ -593,7 +594,7 @@ const CommandDesc add_highlighter_cmd = { : context.window().highlighters(); auto it = registry.find(name); if (it == registry.end()) - throw runtime_error("No such highlighter factory '" + name + "'"); + throw runtime_error(format("No such highlighter factory '{}'", name)); group.add_child(it->second.factory(highlighter_params)); } }; @@ -712,7 +713,7 @@ void define_command(const ParametersParser& parser, Context& context) auto& cm = CommandManager::instance(); if (cm.command_defined(cmd_name) and not parser.get_switch("allow-override")) - throw runtime_error("command '" + cmd_name + "' already defined"); + throw runtime_error(format("command '{}' already defined", cmd_name)); CommandFlags flags = CommandFlags::None; if (parser.get_switch("hidden")) @@ -978,7 +979,7 @@ const CommandDesc set_option_cmd = { OptionManager& options = get_scope(params[0], context).options(); const String& docstring = options[params[1]].docstring(); if (not docstring.empty()) - return params[1] + ": " + docstring; + return format("{}: {}", params[1], docstring); } catch (runtime_error&) {} return ""; @@ -1063,7 +1064,7 @@ const CommandDesc declare_option_cmd = { else if (parser[0] == "line-flag-list") opt = ®.declare_option>(parser[1], docstring, {}, flags); else - throw runtime_error("unknown type " + parser[0]); + throw runtime_error(format("unknown type {}", parser[0])); if (parser.positional_count() == 3) opt->set_from_string(parser[2]); @@ -1080,7 +1081,7 @@ KeymapMode parse_keymap_mode(const String& str) if (prefix_match("view", str)) return KeymapMode::View; if (prefix_match("user", str)) return KeymapMode::User; - throw runtime_error("unknown keymap mode '" + str + "'"); + throw runtime_error(format("unknown keymap mode '{}'", str)); } const CommandDesc map_key_cmd = { @@ -1282,9 +1283,7 @@ const CommandDesc eval_string_cmd = { [](const ParametersParser& parser, Context& context) { context_wrap(parser, context, [](const ParametersParser& parser, Context& context) { - String command; - for (auto& param : parser) - command += param + " "; + String command = join(parser, ' ', false); CommandManager::instance().execute(command, context); }); } @@ -1427,7 +1426,7 @@ const CommandDesc info_cmd = { else if (*placement == "below") style = InfoStyle::InlineBelow; else - throw runtime_error("invalid placement " + *placement); + throw runtime_error(format("invalid placement '{}'", *placement)); } } auto title = parser.get_switch("title").value_or(StringView{}); @@ -1502,7 +1501,7 @@ const CommandDesc set_client_name_cmd = { if (ClientManager::instance().validate_client_name(parser[0])) context.set_name(parser[0]); else if (context.name() != parser[0]) - throw runtime_error("client name '" + parser[0] + "' is not unique"); + throw runtime_error(format("client name '{}' is not unique", parser[0])); } }; @@ -1545,7 +1544,7 @@ const CommandDesc change_working_directory_cmd = { [](const ParametersParser& parser, Context&) { if (chdir(parse_filename(parser[0]).c_str()) != 0) - throw runtime_error("cannot change to directory " + parser[0]); + throw runtime_error(format("cannot change to directory '{}'", parser[0])); } }; diff --git a/src/face_registry.cc b/src/face_registry.cc index 8f2cfbea6..1a972852f 100644 --- a/src/face_registry.cc +++ b/src/face_registry.cc @@ -28,7 +28,7 @@ static Face parse_face(StringView facedesc) case 'b': res.attributes |= Attribute::Bold; break; case 'B': res.attributes |= Attribute::Blink; break; case 'd': res.attributes |= Attribute::Dim; break; - default: throw runtime_error("unknown face attribute '" + StringView(*attr_it) + "'"); + default: throw runtime_error(format("unknown face attribute '{}'", StringView{*attr_it})); } } } @@ -51,12 +51,12 @@ void FaceRegistry::register_alias(const String& name, const String& facedesc, bool override) { if (not override and m_aliases.find(name) != m_aliases.end()) - throw runtime_error("alias '" + name + "' already defined"); + throw runtime_error(format("alias '{}' already defined", name)); if (name.empty() or is_color_name(name) or std::any_of(name.begin(), name.end(), [](char c){ return not isalnum(c); })) - throw runtime_error("invalid alias name"); + throw runtime_error(format("invalid alias name: '{}'", name)); FaceOrAlias& alias = m_aliases[name]; auto it = m_aliases.find(facedesc); diff --git a/src/file.hh b/src/file.hh index 68e401c95..6f018e55d 100644 --- a/src/file.hh +++ b/src/file.hh @@ -14,7 +14,7 @@ struct file_access_error : runtime_error public: file_access_error(StringView filename, StringView error_desc) - : runtime_error(filename + ": " + error_desc) {} + : runtime_error(format("{}: {}", filename, error_desc)) {} }; struct file_not_found : file_access_error diff --git a/src/highlighter_group.cc b/src/highlighter_group.cc index 8a9d364f8..3224cc8a0 100644 --- a/src/highlighter_group.cc +++ b/src/highlighter_group.cc @@ -15,7 +15,7 @@ void HighlighterGroup::highlight(const Context& context, HighlightFlags flags, void HighlighterGroup::add_child(HighlighterAndId&& hl) { if (m_highlighters.contains(hl.first)) - throw runtime_error("duplicate id: " + hl.first); + throw runtime_error(format("duplicate id: '{}'", hl.first)); m_highlighters.append(std::move(hl)); } @@ -31,7 +31,7 @@ Highlighter& HighlighterGroup::get_child(StringView path) StringView id(path.begin(), sep_it); auto it = m_highlighters.find(id); if (it == m_highlighters.end()) - throw child_not_found("no such id: " + id); + throw child_not_found(format("no such id: '{}'", id)); if (sep_it == path.end()) return *it->second; else diff --git a/src/hook_manager.cc b/src/hook_manager.cc index d7879c614..da57a1dab 100644 --- a/src/hook_manager.cc +++ b/src/hook_manager.cc @@ -60,8 +60,8 @@ void HookManager::run_hook(StringView hook_name, } catch (runtime_error& err) { - write_debug("error running hook " + hook_name + "/" + - hook.first + ": " + err.what()); + write_debug(format("error running hook {}/{}: {}", + hook_name, hook.first, err.what())); } } } diff --git a/src/input_handler.cc b/src/input_handler.cc index f5e9440d0..0d4f74121 100644 --- a/src/input_handler.cc +++ b/src/input_handler.cc @@ -1010,7 +1010,7 @@ public: { auto num_sel = context().selections().size(); return {AtomList{ { "insert ", Face(Color::Green) }, - { to_string(num_sel) + " sel", Face(Color::Blue) } }}; + { format( "{} sel", num_sel), Face(Color::Blue) } }}; } KeymapMode keymap_mode() const override { return KeymapMode::Insert; } diff --git a/src/insert_completer.cc b/src/insert_completer.cc index 30be039cd..248636767 100644 --- a/src/insert_completer.cc +++ b/src/insert_completer.cc @@ -56,7 +56,7 @@ void option_from_string(StringView str, InsertCompleterDesc& opt) opt.param = Optional{}; return; } - throw runtime_error("invalid completer description: " + str); + throw runtime_error(format("invalid completer description: '{}'", str)); } namespace diff --git a/src/normal.cc b/src/normal.cc index ef4231bf9..f2b983dff 100644 --- a/src/normal.cc +++ b/src/normal.cc @@ -211,11 +211,11 @@ void goto_commands(Context& context, NormalParams params) String path = find_file(filename, paths); if (path.empty()) - throw runtime_error("unable to find file '" + filename + "'"); + throw runtime_error(format("unable to find file '{}'", filename)); Buffer* buffer = create_buffer_from_file(path); if (buffer == nullptr) - throw runtime_error("unable to open file '" + path + "'"); + throw runtime_error(format("unable to open file '{}'", path)); if (buffer != &context.buffer()) { diff --git a/src/option_manager.hh b/src/option_manager.hh index ec56cf107..23a801f3c 100644 --- a/src/option_manager.hh +++ b/src/option_manager.hh @@ -168,7 +168,7 @@ template const T& Option::get() const { auto* typed_opt = dynamic_cast*>(this); if (not typed_opt) - throw runtime_error("option " + name() + " is not of type " + typeid(T).name()); + throw runtime_error(format("option '{}' is not of type '{}'", name(), typeid(T).name())); return typed_opt->get(); } @@ -176,7 +176,7 @@ template void Option::set(const T& val) { auto* typed_opt = dynamic_cast*>(this); if (not typed_opt) - throw runtime_error("option " + name() + " is not of type " + typeid(T).name()); + throw runtime_error(format("option '{}' is not of type '{}'", name(), typeid(T).name())); return typed_opt->set(val); } @@ -208,7 +208,7 @@ public: { if ((*it)->is_of_type() and (*it)->flags() == flags) return **it; - throw runtime_error("option " + name + " already declared with different type or flags"); + throw runtime_error(format("option '{}' already declared with different type or flags", name)); } m_descs.emplace_back(new OptionDesc{name, docstring, flags}); opts.emplace_back(new TypedOption{m_global_manager, *m_descs.back(), value}); diff --git a/src/option_types.hh b/src/option_types.hh index 265d3c234..ae960261f 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -217,7 +217,7 @@ inline void option_from_string(StringView str, YesNoAsk& opt) else if (str == "ask") opt = Ask; else - throw runtime_error("invalid value '" + str + "', expected yes, no or ask"); + throw runtime_error(format("invalid value '{}', expected yes, no or ask", str)); } } diff --git a/src/parameters_parser.hh b/src/parameters_parser.hh index 827eacf9e..79f4f7022 100644 --- a/src/parameters_parser.hh +++ b/src/parameters_parser.hh @@ -21,13 +21,13 @@ struct parameter_error : public runtime_error struct unknown_option : public parameter_error { unknown_option(StringView name) - : parameter_error("unknown option '" + name + "'") {} + : parameter_error(format("unknown option '{}'", name)) {} }; struct missing_option_value: public parameter_error { missing_option_value(StringView name) - : parameter_error("missing value for option '" + name + "'") {} + : parameter_error(format("missing value for option '{}'", name)) {} }; struct wrong_argument_count : public parameter_error diff --git a/src/register_manager.cc b/src/register_manager.cc index 7990c13bd..27ca47bdb 100644 --- a/src/register_manager.cc +++ b/src/register_manager.cc @@ -68,7 +68,7 @@ Register& RegisterManager::operator[](StringView reg) }; auto it = reg_names.find(reg); if (it == reg_names.end()) - throw runtime_error("no such register: " + reg); + throw runtime_error(format("no such register: '{}'", reg)); return (*this)[it->second]; } diff --git a/src/remote.cc b/src/remote.cc index 05da041cc..207abfb16 100644 --- a/src/remote.cc +++ b/src/remote.cc @@ -287,14 +287,12 @@ RemoteUI::RemoteUI(int socket) m_input_callback(mode); }) { - write_debug("remote client connected: " + - to_string(m_socket_watcher.fd())); + write_debug(format("remote client connected: {}", m_socket_watcher.fd())); } RemoteUI::~RemoteUI() { - write_debug("remote client disconnected: " + - to_string(m_socket_watcher.fd())); + write_debug(format("remote client disconnected: {}", m_socket_watcher.fd())); m_socket_watcher.close_fd(); } @@ -421,7 +419,7 @@ static sockaddr_un session_addr(StringView session) { sockaddr_un addr; addr.sun_family = AF_UNIX; - strncpy(addr.sun_path, ("/tmp/kak-" + session).c_str(), + strncpy(addr.sun_path, format("/tmp/kak-{}", session).c_str(), sizeof(addr.sun_path) - 1); return addr; } @@ -585,8 +583,8 @@ private: } catch (runtime_error& e) { - write_debug("error running command '" + m_buffer + - "' : " + e.what()); + write_debug(format("error running command '{}': {}", + m_buffer, e.what())); } catch (client_removed&) {} close(socket); @@ -645,7 +643,7 @@ Server::Server(String session_name) void Server::close_session() { - unlink(("/tmp/kak-" + m_session).c_str()); + unlink(format("/tmp/kak-{}", m_session).c_str()); m_listener->close_fd(); m_listener.reset(); } diff --git a/src/remote.hh b/src/remote.hh index bb59270dd..60d616194 100644 --- a/src/remote.hh +++ b/src/remote.hh @@ -17,7 +17,7 @@ struct peer_disconnected {}; struct connection_failed : runtime_error { connection_failed(StringView filename) - : runtime_error{"connect to " + filename + " failed"} + : runtime_error{format("connect to {} failed", filename)} {} }; diff --git a/src/selectors.hh b/src/selectors.hh index f5cf8504a..a2810d035 100644 --- a/src/selectors.hh +++ b/src/selectors.hh @@ -259,7 +259,7 @@ Selection find_next_match(const Buffer& buffer, const Selection& sel, const Rege captures.emplace_back(match.first, match.second); } if (not found or begin == buffer.end()) - throw runtime_error("'" + regex.str() + "': no matches found"); + throw runtime_error(format("'{}': no matches found", regex.str())); end = (begin == end) ? end : utf8::previous(end, begin); if (direction == Backward)