summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.c8
-rw-r--r--menu.c49
-rw-r--r--ted.cfg2
-rw-r--r--ted.h2
-rw-r--r--ui.c43
-rw-r--r--util.c6
6 files changed, 78 insertions, 32 deletions
diff --git a/main.c b/main.c
index 709b23d..a7fbda0 100644
--- a/main.c
+++ b/main.c
@@ -1,5 +1,4 @@
// @TODO:
-// - popup to reload files and config on change
// - find & replace (with regex)
// - split
// - completion
@@ -508,7 +507,12 @@ int main(int argc, char **argv) {
{
TextBuffer *active_buffer = ted->active_buffer;
if (active_buffer && buffer_externally_changed(active_buffer)) {
- buffer_reload(active_buffer);
+ if (settings->auto_reload)
+ buffer_reload(active_buffer);
+ else {
+ strbuf_cpy(ted->ask_reload, buffer_get_filename(active_buffer));
+ menu_open(ted, MENU_ASK_RELOAD);
+ }
}
}
diff --git a/menu.c b/menu.c
index b9d3e66..d7af1e9 100644
--- a/menu.c
+++ b/menu.c
@@ -3,7 +3,6 @@ static void menu_open(Ted *ted, Menu menu) {
ted->prev_active_buffer = ted->active_buffer;
ted->active_buffer = NULL;
*ted->warn_overwrite = 0; // clear warn_overwrite
-
switch (menu) {
case MENU_NONE: assert(0); break;
case MENU_OPEN:
@@ -17,6 +16,9 @@ static void menu_open(Ted *ted, Menu menu) {
assert(ted->warn_unsaved);
assert(*ted->warn_unsaved_names);
break;
+ case MENU_ASK_RELOAD:
+ assert(*ted->ask_reload);
+ break;
}
}
@@ -34,6 +36,9 @@ static void menu_close(Ted *ted) {
ted->warn_unsaved = 0;
*ted->warn_unsaved_names = 0;
break;
+ case MENU_ASK_RELOAD:
+ *ted->ask_reload = 0;
+ break;
}
ted->menu = MENU_NONE;
}
@@ -70,7 +75,7 @@ static void menu_update(Ted *ted, Menu menu) {
case MENU_NONE: break;
case MENU_SAVE_AS: {
if (*ted->warn_overwrite) {
- switch (popup_update(ted)) {
+ switch (popup_update(ted, POPUP_YES_NO_CANCEL)) {
case POPUP_NONE:
// no option selected
break;
@@ -120,8 +125,25 @@ static void menu_update(Ted *ted, Menu menu) {
free(selected_file);
}
} break;
+ case MENU_ASK_RELOAD: {
+ TextBuffer *buffer = ted->prev_active_buffer;
+ switch (popup_update(ted, POPUP_YES_NO)) {
+ case POPUP_NONE: break;
+ case POPUP_YES:
+ menu_close(ted);
+ if (buffer)
+ buffer_reload(buffer);
+ break;
+ case POPUP_NO:
+ menu_close(ted);
+ if (buffer)
+ buffer->last_write_time = time_last_modified(buffer->filename);
+ break;
+ case POPUP_CANCEL: assert(0); break;
+ }
+ } break;
case MENU_WARN_UNSAVED:
- switch (popup_update(ted)) {
+ switch (popup_update(ted, POPUP_YES_NO_CANCEL)) {
case POPUP_NONE: break;
case POPUP_YES:
// save changes
@@ -176,18 +198,24 @@ static void menu_render(Ted *ted, Menu menu) {
char title[64] = {0}, body[1024] = {0};
strbuf_printf(title, "Overwrite %s?", filename);
strbuf_printf(body, "Are you sure you want to overwrite %s?", path);
- popup_render(ted, title, body);
+ popup_render(ted, POPUP_YES_NO_CANCEL, title, body);
return;
}
- if (menu == MENU_WARN_UNSAVED) {
+ switch (menu) {
+ case MENU_WARN_UNSAVED: {
char title[64] = {0}, body[1024] = {0};
strbuf_printf(title, "Save changes?");
strbuf_printf(body, "Do you want to save your changes to %s?", ted->warn_unsaved_names);
- popup_render(ted, title, body);
- return;
- }
-
- if (menu == MENU_OPEN || menu == MENU_SAVE_AS) {
+ popup_render(ted, POPUP_YES_NO_CANCEL, title, body);
+ } break;
+ case MENU_ASK_RELOAD: {
+ char title[64] = {0}, body[1024] = {0};
+ strbuf_printf(title, "Reload %s?", ted->ask_reload);
+ strbuf_printf(body, "%s has been changed by another program. Do you want to reload it?", ted->ask_reload);
+ popup_render(ted, POPUP_YES_NO, title, body);
+ } break;
+ case MENU_OPEN:
+ case MENU_SAVE_AS: {
float padding = settings->padding;
Rect rect = menu_rect(ted);
float menu_x1, menu_y1, menu_x2, menu_y2;
@@ -215,6 +243,7 @@ static void menu_render(Ted *ted, Menu menu) {
FileSelector *fs = &ted->file_selector;
fs->bounds = rect4(menu_x1, menu_y1, menu_x2, menu_y2);
file_selector_render(ted, fs);
+ } break;
}
}
diff --git a/ted.cfg b/ted.cfg
index 339274e..6ad3672 100644
--- a/ted.cfg
+++ b/ted.cfg
@@ -21,7 +21,7 @@ auto-indent = on
auto-add-newline = on
syntax-highlighting = on
line-numbers = on
-# Reload files when changed?
+# If set to "on", when a file is changed by another program, it will be reloaded by ted without asking you.
auto-reload = off
[keyboard]
diff --git a/ted.h b/ted.h
index 05283f7..7afc69c 100644
--- a/ted.h
+++ b/ted.h
@@ -154,6 +154,7 @@ ENUM_U16 {
MENU_OPEN,
MENU_SAVE_AS,
MENU_WARN_UNSAVED, // warn about unsaved changes
+ MENU_ASK_RELOAD, // prompt about whether to reload file which has ben changed by another program
} ENUM_U16_END(Menu);
// file entries for file selectors
@@ -216,6 +217,7 @@ typedef struct Ted {
Command warn_unsaved; // if non-zero, the user is trying to execute this command, but there are unsaved changes
char warn_unsaved_names[TED_PATH_MAX]; // comma-separated list of files with unsaved changes (only applicable if warn_unsaved != 0)
char warn_overwrite[TED_PATH_MAX]; // file name user is trying to overwrite
+ char ask_reload[TED_PATH_MAX]; // file name which we want to reload
char local_data_dir[TED_PATH_MAX];
char global_data_dir[TED_PATH_MAX];
char home[TED_PATH_MAX];
diff --git a/ui.c b/ui.c
index 68e464c..b74d75f 100644
--- a/ui.c
+++ b/ui.c
@@ -508,29 +508,40 @@ static bool button_update(Ted *ted, Rect button) {
typedef enum {
POPUP_NONE,
- POPUP_YES,
- POPUP_NO,
- POPUP_CANCEL
+ POPUP_YES = 1<<1,
+ POPUP_NO = 1<<2,
+ POPUP_CANCEL = 1<<3,
} PopupOption;
+#define POPUP_YES_NO (POPUP_YES | POPUP_NO)
+#define POPUP_YES_NO_CANCEL (POPUP_YES | POPUP_NO | POPUP_CANCEL)
-static void popup_get_rects(Ted const *ted, Rect *popup, Rect *button_yes, Rect *button_no, Rect *button_cancel) {
+static void popup_get_rects(Ted const *ted, u32 options, Rect *popup, Rect *button_yes, Rect *button_no, Rect *button_cancel) {
float window_width = ted->window_width, window_height = ted->window_height;
*popup = rect_centered(V2(window_width * 0.5f, window_height * 0.5f), V2(300, 200));
float button_height = 30;
- u16 nbuttons = 3;
+ u16 nbuttons = util_popcount(options);
float button_width = popup->size.x / nbuttons;
popup->size = v2_clamp(popup->size, v2_zero, V2(window_width, window_height));
- *button_yes = rect(V2(popup->pos.x, rect_y2(*popup) - button_height), V2(button_width, button_height));
- *button_no = rect_translate(*button_yes, V2(button_width, 0));
- *button_cancel = rect_translate(*button_no, V2(button_width, 0));
-
+ Rect r = rect(V2(popup->pos.x, rect_y2(*popup) - button_height), V2(button_width, button_height));
+ if (options & POPUP_YES) {
+ *button_yes = r;
+ r = rect_translate(r, V2(button_width, 0));
+ }
+ if (options & POPUP_NO) {
+ *button_no = r;
+ r = rect_translate(r, V2(button_width, 0));
+ }
+ if (options & POPUP_CANCEL) {
+ *button_cancel = r;
+ r = rect_translate(r, V2(button_width, 0));
+ }
}
-static PopupOption popup_update(Ted *ted) {
+static PopupOption popup_update(Ted *ted, u32 options) {
Rect r, button_yes, button_no, button_cancel;
- popup_get_rects(ted, &r, &button_yes, &button_no, &button_cancel);
+ popup_get_rects(ted, options, &r, &button_yes, &button_no, &button_cancel);
if (button_update(ted, button_yes))
return POPUP_YES;
if (button_update(ted, button_no))
@@ -540,7 +551,7 @@ static PopupOption popup_update(Ted *ted) {
return POPUP_NONE;
}
-static void popup_render(Ted *ted, char const *title, char const *body) {
+static void popup_render(Ted *ted, u32 options, char const *title, char const *body) {
float window_width = ted->window_width;
Font *font = ted->font;
Font *font_bold = ted->font_bold;
@@ -551,7 +562,7 @@ static void popup_render(Ted *ted, char const *title, char const *body) {
float const padding = settings->padding;
float const border_thickness = settings->border_thickness;
- popup_get_rects(ted, &r, &button_yes, &button_no, &button_cancel);
+ popup_get_rects(ted, options, &r, &button_yes, &button_no, &button_cancel);
float y = r.pos.y;
@@ -562,9 +573,9 @@ static void popup_render(Ted *ted, char const *title, char const *body) {
// line separating text from body
gl_geometry_rect(rect(V2(r.pos.x, y + char_height_bold), V2(r.size.x, border_thickness)), colors[COLOR_BORDER]);
- button_render(ted, button_yes, "Yes", colors[COLOR_YES]);
- button_render(ted, button_no, "No", colors[COLOR_NO]);
- button_render(ted, button_cancel, "Cancel", colors[COLOR_CANCEL]);
+ if (options & POPUP_YES) button_render(ted, button_yes, "Yes", colors[COLOR_YES]);
+ if (options & POPUP_NO) button_render(ted, button_no, "No", colors[COLOR_NO]);
+ if (options & POPUP_CANCEL) button_render(ted, button_cancel, "Cancel", colors[COLOR_CANCEL]);
// title text
v2 title_size = {0};
diff --git a/util.c b/util.c
index ed0b178..1584e0b 100644
--- a/util.c
+++ b/util.c
@@ -1,8 +1,8 @@
-static u32 util_popcount(u64 x) {
+static u8 util_popcount(u64 x) {
#ifdef __GNUC__
- return (u32)__builtin_popcountll(x);
+ return (u8)__builtin_popcountll(x);
#else
- u32 count = 0;
+ u8 count = 0;
while (x) {
x &= x-1;
++count;