vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 6adb68e7339d3d548e2440c3fd3e083076eb5357
parent b0f2e43c423d036e35e90f39c273b90b5cd5f33a
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Wed, 24 Sep 2014 08:25:01 +0200

Implement :saveas command

Diffstat:
Mconfig.def.h | 1+
Mvis.c | 20+++++++++++++++++++-
2 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/config.def.h b/config.def.h @@ -53,6 +53,7 @@ static Command cmds[] = { { "^qa(ll)?!?$", cmd_qall }, { "^q(quit)?!?$", cmd_quit }, { "^r(ead)?$", cmd_read }, + { "^sav(as)?$", cmd_saveas }, { "^set$", cmd_set }, { "^sp(lit)?$", cmd_split }, { "^s(ubstitute)?$", cmd_substitute }, diff --git a/vis.c b/vis.c @@ -438,8 +438,14 @@ static bool cmd_split(const char *argv[]); static bool cmd_vsplit(const char *argv[]); /* save the file displayed in the current window and close it */ static bool cmd_wq(const char *argv[]); -/* save the file displayed in the current window to the name given */ +/* save the file displayed in the current window to the name given. + * do not change internal filname association. further :w commands + * without arguments will still write to the old filename */ static bool cmd_write(const char *argv[]); +/* save the file displayed in the current window to the name given, + * associate the new name with the buffer. further :w commands + * without arguments will write to the new filename */ +static bool cmd_saveas(const char *argv[]); static void action_reset(Action *a); static void switchmode_to(Mode *new_mode); @@ -1204,6 +1210,10 @@ static bool cmd_write(const char *argv[]) { Text *text = vis->win->text; if (!argv[1]) argv[1] = text_filename_get(text); + if (!argv[1]) { + editor_info_show(vis, "Filename expected"); + return false; + } for (const char **file = &argv[1]; *file; file++) { if (text_save(text, *file)) { editor_info_show(vis, "Can't write `%s'", *file); @@ -1213,6 +1223,14 @@ static bool cmd_write(const char *argv[]) { return true; } +static bool cmd_saveas(const char *argv[]) { + if (cmd_write(argv)) { + text_filename_set(vis->win->text, argv[1]); + return true; + } + return false; +} + static bool exec_command(char *cmdline) { static bool init = false; if (!init) {