diff --git a/src/main.cc b/src/main.cc index 93e7080c..cbff8402 100644 --- a/src/main.cc +++ b/src/main.cc @@ -236,6 +236,32 @@ void do_join(Context& context) editor.move_selections(-1_char); } +void do_indent(Context& context) +{ + const char* spaces = " "; + int width = std::min(context.option_manager()["indentwidth"].as_int(), 16); + String indent(spaces, spaces + width); + + Editor& editor = context.editor(); + SelectionAndCapturesList sels = editor.selections(); + auto restore_sels = on_scope_end([&]{ editor.select(std::move(sels)); }); + editor.select(select_whole_lines); + editor.multi_select(std::bind(select_all_matches, _1, "^[^\n]")); + editor.insert(indent, InsertMode::Insert); +} + +void do_deindent(Context& context) +{ + int width = context.option_manager()["indentwidth"].as_int(); + Editor& editor = context.editor(); + SelectionAndCapturesList sels = editor.selections(); + auto restore_sels = on_scope_end([&]{ editor.select(std::move(sels)); }); + editor.select(select_whole_lines); + editor.multi_select(std::bind(select_all_matches, _1, + "^\\h{1," + int_to_str(width) + "}")); + editor.erase(); +} + template void do_select_object(Context& context) { @@ -462,6 +488,9 @@ std::unordered_map> keymap = { { Key::Modifiers::Alt, 'j' }, do_join }, + { { Key::Modifiers::None, '<' }, do_deindent }, + { { Key::Modifiers::None, '>' }, do_indent }, + { { Key::Modifiers::Alt, 'x' }, [](Context& context) { context.editor().select(select_whole_lines); } }, { { Key::Modifiers::Alt, 'c' }, [](Context& context) { if (context.has_window()) context.window().center_selection(); } }, diff --git a/src/option_manager.cc b/src/option_manager.cc index b2123cfe..b3e6c5a4 100644 --- a/src/option_manager.cc +++ b/src/option_manager.cc @@ -94,6 +94,7 @@ GlobalOptionManager::GlobalOptionManager() : OptionManager() { set_option("tabstop", Option(8)); + set_option("indentwidth", Option(4)); set_option("eolformat", Option("lf")); set_option("BOM", Option("no")); }