From: urosm <urosm@kompot.si>
This allows keeping files with unsaved modifications in the state
without having to have them displayed in a window.
vis_window_close no longer frees win->file, this is moved to vis_free.
After closing the last window cmd_quit and cmd_qall check for modified
files in vis->files and open a new window for the found modified file
if necessary.
---
vis-cmds.c | 40 +++++++++++++++++++++-------------------
vis.c | 3 ++-
2 files changed, 23 insertions(+), 20 deletions(-)
diff --git a/vis-cmds.c b/vis-cmds.c
index 4cfb3bd..f94c8b7 100644
--- a/vis-cmds.c
+++ b/vis-cmds.c
@@ -463,10 +463,6 @@ static bool cmd_edit(Vis *vis, Win *win, Command *cmd, const char *argv[], Selec
Win *oldwin = win;
if (!oldwin)
return false;
- if (cmd->flags != '!' && !vis_window_closable(oldwin)) {
- info_unsaved_changes(vis);
- return false;
- }
if (!argv[1]) {
if (oldwin->file->refcount > 1) {
vis_info_show(vis, "Can not reload file being opened multiple times");
@@ -511,30 +507,36 @@ static bool has_windows(Vis *vis) {
return false;
}
-static bool cmd_quit(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
- if (cmd->flags != '!' && !vis_window_closable(win)) {
- info_unsaved_changes(vis);
- return false;
+static bool find_modified_file(Vis *vis) {
+ for (File *file = vis->files; file; file = file->next) {
+ if (!file->internal && text_modified(file->text)) {
+ vis_window_new(vis, file->name);
+ info_unsaved_changes(vis);
+ return true;
+ }
}
+ return false;
+}
+
+static bool cmd_quit(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
vis_window_close(win);
- if (!has_windows(vis))
- vis_exit(vis, argv[1] ? atoi(argv[1]) : EXIT_SUCCESS);
+ if (has_windows(vis))
+ return true;
+ if (cmd->flags != '!' && find_modified_file(vis))
+ return false;
+ vis_exit(vis, argv[1] ? atoi(argv[1]) : EXIT_SUCCESS);
return true;
}
static bool cmd_qall(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
- for (Win *next, *win = vis->windows; win; win = next) {
- next = win->next;
- if (!win->file->internal && (!text_modified(win->file->text) || cmd->flags == '!'))
+ for (Win *win = vis->windows; win; win = win->next) {
+ if (!win->file->internal)
vis_window_close(win);
}
- if (!has_windows(vis)) {
- vis_exit(vis, argv[1] ? atoi(argv[1]) : EXIT_SUCCESS);
- return true;
- } else {
- info_unsaved_changes(vis);
+ if (cmd->flags != '!' && find_modified_file(vis))
return false;
- }
+ vis_exit(vis, argv[1] ? atoi(argv[1]) : EXIT_SUCCESS);
+ return true;
}
static bool cmd_split(Vis *vis, Win *win, Command *cmd, const char *argv[], Selection *sel, Filerange *range) {
diff --git a/vis.c b/vis.c
index 7d3dd6a..9841516 100644
--- a/vis.c
+++ b/vis.c
@@ -547,7 +547,6 @@ void vis_window_close(Win *win) {
return;
Vis *vis = win->vis;
vis_event_emit(vis, VIS_EVENT_WIN_CLOSE, win);
- file_free(vis, win->file);
if (win->prev)
win->prev->next = win->next;
if (win->next)
@@ -626,6 +625,8 @@ void vis_free(Vis *vis) {
file_free(vis, vis->command_file);
file_free(vis, vis->search_file);
file_free(vis, vis->error_file);
+ while (vis->files)
+ file_free(vis, vis->files);
for (int i = 0; i < LENGTH(vis->registers); i++)
register_release(&vis->registers[i]);
ui_terminal_free(&vis->ui);
--
2.45.2
~urosm <urosm@git.sr.ht> wrote:
> From: urosm <urosm@kompot.si>
>
> This allows keeping files with unsaved modifications in the state
> without having to have them displayed in a window.
>
> vis_window_close no longer frees win->file, this is moved to vis_free.
> After closing the last window cmd_quit and cmd_qall check for modified
> files in vis->files and open a new window for the found modified file
> if necessary.
> ---
> vis-cmds.c | 40 +++++++++++++++++++++-------------------
> vis.c | 3 ++-
> 2 files changed, 23 insertions(+), 20 deletions(-)
Sorry I didn't mean to overlook this one. A better motivation for
this is that this helps us move towards the Client-Server idea. It
should also help with improving support for multiple views of the
the same file.
I think all the changes look good to me but they introduce a bug.
After applying this modify a file and try to close it without
writing. The cursor will jump to position 0,0. I think the order
in which parts of the window are being destroyed was changed so
now the selection gets lost.
That will need to be fixed before I can apply this.
- Randy
--
https://rnpnr.xyz/
GPG Fingerprint: B8F0 CF4C B6E9 415C 1B27 A8C4 C8D2 F782 86DF 2DC5
I want to revisit this and address the bug. The cursor problem is
present also when switching between files that should already be in the
state. The cursor position is stored in View, View is tied to Win, but
files don't necessarily have a Win now, so they don't keep their cursor
position.
Any ideas or preferences on how I should address this? You have a better
idea about vis' structs.
--
u
I want to revisit this and address the bug. The cursor problem is
present also when switching between files that should already be in the
state. The cursor position is stored in View, View is tied to Win, but
files don't necessarily have a Win now, so they don't keep their cursor
position.
Any ideas or preferences on how I should address this? You have a better
idea about vis' structs.
--
u