Skip to content

Instantly share code, notes, and snippets.

@hovsater
Last active March 30, 2023 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hovsater/691aa7ed3a66abdb53b3cc1f5a35ff91 to your computer and use it in GitHub Desktop.
Save hovsater/691aa7ed3a66abdb53b3cc1f5a35ff91 to your computer and use it in GitHub Desktop.
diff --git a/src/commands.c b/src/commands.c
index 63cb3606..61abb0c3 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -1113,10 +1113,12 @@ static bool cmd_open(EditorState *e, const CommandArgs *a)
paths = globbuf.gl_pathv;
}
+ bool replace_view = has_flag(a, 'R');
+
View *first_opened;
if (!paths[1]) {
// Previous view is remembered when opening single file
- first_opened = window_open_file(e->window, paths[0], &encoding);
+ first_opened = window_open_file(e->window, paths[0], &encoding, replace_view);
} else {
// It makes no sense to remember previous view when opening multiple files
first_opened = window_open_files(e->window, paths, &encoding);
@@ -2405,7 +2407,7 @@ static const Command cmds[] = {
{"msg", "np", false, 0, 1, cmd_msg},
{"new-line", "a", false, 0, 0, cmd_new_line},
{"next", "", false, 0, 0, cmd_next},
- {"open", "e=gt", false, 0, -1, cmd_open},
+ {"open", "e=gtR", false, 0, -1, cmd_open},
{"option", "-r", true, 3, -1, cmd_option},
{"paste", "acm", false, 0, 0, cmd_paste},
{"pgdown", "cl", false, 0, 0, cmd_pgdown},
diff --git a/src/window.c b/src/window.c
index 8d49eca9..f88c14bc 100644
--- a/src/window.c
+++ b/src/window.c
@@ -315,14 +315,14 @@ static bool is_useless_empty_view(const View *v)
return v && v->window->views.count == 1 && buffer_is_empty_and_untouched(v->buffer);
}
-View *window_open_file(Window *window, const char *filename, const Encoding *encoding)
+View *window_open_file(Window *window, const char *filename, const Encoding *encoding, bool replace_view)
{
View *prev = window->view;
bool useless = is_useless_empty_view(prev);
View *view = window_open_buffer(window, filename, false, encoding);
if (view) {
set_view(view);
- if (useless) {
+ if (useless || replace_view) {
remove_view(prev);
} else {
window->prev_view = prev;
diff --git a/src/window.h b/src/window.h
index 026a1b50..fc24a990 100644
--- a/src/window.h
+++ b/src/window.h
@@ -52,7 +52,7 @@ void window_free(Window *window);
void window_close(Window *window);
void window_close_current_view(Window *window);
View *window_open_new_file(Window *window);
-View *window_open_file(Window *window, const char *filename, const Encoding *encoding);
+View *window_open_file(Window *window, const char *filename, const Encoding *encoding, bool replace_view);
View *window_open_files(Window *window, char **filenames, const Encoding *encoding);
void window_calculate_line_numbers(Window *window);
void window_set_coordinates(Window *window, int x, int y);
diff --git a/test/command.c b/test/command.c
index 365ead6f..7d6b443d 100644
--- a/test/command.c
+++ b/test/command.c
@@ -521,7 +521,7 @@ static void test_command_struct_layout(TestContext *ctx)
{
const Command *cmd = find_normal_command("open");
EXPECT_STREQ(cmd->name, "open");
- EXPECT_STREQ(cmd->flags, "e=gt");
+ EXPECT_STREQ(cmd->flags, "e=gtR");
EXPECT_FALSE(cmd->allow_in_rc);
EXPECT_UINT_EQ(cmd->min_args, 0);
EXPECT_UINT_EQ(cmd->max_args, 0xFF);
diff --git a/test/syntax.c b/test/syntax.c
index 2d2c4fa6..2e9aaaa7 100644
--- a/test/syntax.c
+++ b/test/syntax.c
@@ -86,7 +86,7 @@ static void test_hl_line(TestContext *ctx)
EditorState *e = ctx->userdata;
Window *window = e->window;
ASSERT_NONNULL(window);
- View *view = window_open_file(window, "test/data/test.c", &utf8);
+ View *view = window_open_file(window, "test/data/test.c", &utf8, false);
ASSERT_NONNULL(view);
Buffer *buffer = view->buffer;
ASSERT_NONNULL(buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment