diff --git a/src/main.c b/src/main.c index 005da84..08d3eb2 100644 --- a/src/main.c +++ b/src/main.c @@ -85,7 +85,8 @@ static void update_fkeys(void) } } else if(app.fkmenu == FK_EDITOR_SOURCE) { - fkeys_sprintf(app.fkeys, "@SAVE;%s;;;;", + fkeys_sprintf(app.fkeys, "%s;%s;;;;", + !app.source->no_origin ? "@SAVE" : "", app.source->intf->cap & SOURCE_SAVEAS ? "@SAVE AS" : ""); } else if(app.fkmenu == FK_EDITOR_OPEN) { @@ -239,6 +240,19 @@ static void render_stats_histogram(int x, int y) } } +static void open_source(source_t *source, bool insert) +{ + app.source = source; + app.fkmenu = FK_EDITOR; + app.insert = insert; + if(app.source) + source_load(app.source, 0, 2048); + heditor_set_source(app.editor, app.source); + heditor_set_insert_mode(app.editor, app.insert); + update_fkeys(); + update_status(); +} + void hex_view(void) { memset(&app, 0, sizeof app); @@ -378,25 +392,20 @@ void hex_view(void) if(browser_mode == OPEN_LF || browser_mode == OPEN_LZ) { if(app.source) source_free(app.source); - app.source = (browser_mode == OPEN_LZ) + open_source(browser_mode == OPEN_LZ ? source_lazy_file_open(file) - : source_loaded_file_open(file); - app.fkmenu = FK_EDITOR; - app.insert = false; - source_load(app.source, 0, 2048); - heditor_set_source(mem, app.source); - heditor_set_insert_mode(mem, app.insert); - update_fkeys(); + : source_loaded_file_open(file), false); + jscene_show_and_focus(scene, mem); } else if(browser_mode == SAVE_AS) { if(heditor_save(mem, file)) { app.fkmenu = FK_EDITOR; update_fkeys(); } + jscene_show_and_focus(scene, mem); + update_status(); } } - jscene_show_and_focus(scene, mem); - update_status(); } if(e.type == HEDITOR_CHANGED) { update_status(); @@ -448,7 +457,7 @@ void hex_view(void) } } else if(app.fkmenu == FK_EDITOR_SOURCE) { - if(key == KEY_F1 && app.source) { + if(key == KEY_F1 && app.source && !app.source->no_origin) { if(heditor_save(mem, NULL)) { app.fkmenu = FK_EDITOR; update_fkeys(); @@ -490,7 +499,12 @@ void hex_view(void) /* TODO: Memory browser */ } else if(key == KEY_F4) { - /* TODO: New empty source */ + if(!has_unsaved_changes() || confirm_discard()) { + if(app.source) + source_free(app.source); + open_source(source_loaded_file_new(), true); + jscene_show_and_focus(scene, mem); + } } else if(key == KEY_EXIT) { app.fkmenu = FK_EDITOR; diff --git a/src/source-loaded-file.c b/src/source-loaded-file.c index 15ad317..4362cec 100644 --- a/src/source-loaded-file.c +++ b/src/source-loaded-file.c @@ -136,3 +136,28 @@ fail: lf_free(lf); return NULL; } + +source_t *source_loaded_file_new(void) +{ + source_t *source = NULL; + loaded_file_t *lf = NULL; + + lf = calloc(1, sizeof *lf); + if(!lf) goto fail; + + lf->original_size = 0; + lf->buf = buffer_create(4096, 4096); + if(!lf->buf) goto fail; + lf->buf->data_size = 0; + + source = source_open(&lf_intf, lf, ""); + if(!source) goto fail; + + source->no_origin = true; + return source; + +fail: + free(source); + lf_free(lf); + return NULL; +} diff --git a/src/source-loaded-file.h b/src/source-loaded-file.h index e39854c..c2ac813 100644 --- a/src/source-loaded-file.h +++ b/src/source-loaded-file.h @@ -23,3 +23,5 @@ typedef struct { } loaded_file_t; source_t *source_loaded_file_open(char const *path); + +source_t *source_loaded_file_new(void); diff --git a/src/source.c b/src/source.c index ea50855..f79fee6 100644 --- a/src/source.c +++ b/src/source.c @@ -9,6 +9,7 @@ source_t *source_open(source_intf_t *intf, void *cookie, char const *origin) if(!s) goto fail; s->origin = strdup(origin); + s->no_origin = false; if(!s->origin) goto fail; s->buf = buffer_create(0, 1024); diff --git a/src/source.h b/src/source.h index 6c9912e..8f46464 100644 --- a/src/source.h +++ b/src/source.h @@ -53,8 +53,11 @@ typedef struct { /* Interface identifier, and opaque interface data */ source_intf_t *intf; void *cookie; - /* Source name. Can be any string: file path, "memory at X", etc */ + /* Source name. Can be any string: file path, "memory at X", etc. */ char *origin; + /* If set, there is no origin and the "SAVE" button is disabled (only + useful if "SAVE AS" is enabled) */ + bool no_origin; /* Front buffer for edition */ buffer_t *buf; /* Address of the front buffer in the source */