From 6f208a42ce93dfac6f16cb16fdc7a4e6d37e4e7d Mon Sep 17 00:00:00 2001 From: pommicket Date: Tue, 27 Dec 2022 15:05:18 -0500 Subject: configurable max buffer size + max view-only buffer size --- buffer.c | 11 ++++++++++- config.c | 31 +++++++++++++++++++++++++++++++ main.c | 1 - ted.cfg | 9 +++++++++ ted.h | 2 ++ 5 files changed, 52 insertions(+), 2 deletions(-) diff --git a/buffer.c b/buffer.c index 49d63a4..7b569bd 100644 --- a/buffer.c +++ b/buffer.c @@ -2176,10 +2176,14 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) { long file_pos = ftell(fp); size_t file_size = (size_t)file_pos; fseek(fp, 0, SEEK_SET); + const Settings *default_settings = buffer->ted->default_settings; + u32 max_file_size_editable = default_settings->max_file_size; + u32 max_file_size_view_only = default_settings->max_file_size_view_only; + if (file_pos == -1 || file_pos == LONG_MAX) { buffer_seterr(buffer, "Couldn't get file position. There is something wrong with the file '%s'.", filename); success = false; - } else if (file_size > 10L<<20) { + } else if (file_size > max_file_size_editable && file_size > max_file_size_view_only) { buffer_seterr(buffer, "File too big (size: %zu).", file_size); success = false; } else { @@ -2254,6 +2258,11 @@ Status buffer_load_file(TextBuffer *buffer, char const *filename) { buffer->view_only = true; } + if (file_size > max_file_size_editable) { + // file very large; open in view-only mode. + buffer->view_only = true; + } + // this will send a didOpen request if needed buffer_lsp(buffer); } diff --git a/config.c b/config.c index c399ad6..0b9c396 100644 --- a/config.c +++ b/config.c @@ -210,6 +210,12 @@ typedef struct { u16 min, max; bool per_language; } OptionU16; +typedef struct { + char const *name; + const u32 *control; + u32 min, max; + bool per_language; +} OptionU32; typedef struct { char const *name; const char *control; @@ -221,6 +227,7 @@ typedef enum { OPTION_BOOL = 1, OPTION_U8, OPTION_U16, + OPTION_U32, OPTION_FLOAT, OPTION_STRING } OptionType; @@ -232,6 +239,7 @@ typedef struct { OptionU8 _u8; OptionBool _bool; OptionU16 _u16; + OptionU32 _u32; OptionFloat _float; OptionString _string; } u; @@ -267,6 +275,10 @@ static OptionU16 const options_u16[] = { {"max-menu-width", &options_zero.max_menu_width, 10, U16_MAX, false}, {"error-display-time", &options_zero.error_display_time, 0, U16_MAX, false}, }; +static OptionU32 const options_u32[] = { + {"max-file-size", &options_zero.max_file_size, 100, 2000000000, false}, + {"max-file-size-view-only", &options_zero.max_file_size_view_only, 100, 2000000000, false}, +}; static OptionFloat const options_float[] = { {"cursor-blink-time-on", &options_zero.cursor_blink_time_on, 0, 1000, true}, {"cursor-blink-time-off", &options_zero.cursor_blink_time_off, 0, 1000, true}, @@ -290,6 +302,10 @@ static void option_u16_set(Settings *settings, const OptionU16 *opt, u16 value) if (value >= opt->min && value <= opt->max) *(u16 *)((char *)settings + ((char*)opt->control - (char*)&options_zero)) = value; } +static void option_u32_set(Settings *settings, const OptionU32 *opt, u32 value) { + if (value >= opt->min && value <= opt->max) + *(u32 *)((char *)settings + ((char*)opt->control - (char*)&options_zero)) = value; +} static void option_float_set(Settings *settings, const OptionFloat *opt, float value) { if (value >= opt->min && value <= opt->max) *(float *)((char *)settings + ((char*)opt->control - (char*)&options_zero)) = value; @@ -389,6 +405,13 @@ static void config_init_options(void) { opt->u._u16 = options_u16[i]; ++opt; } + for (size_t i = 0; i < arr_count(options_u32); ++i) { + opt->type = OPTION_U32; + opt->name = options_u32[i].name; + opt->per_language = options_u32[i].per_language; + opt->u._u32 = options_u32[i]; + ++opt; + } for (size_t i = 0; i < arr_count(options_float); ++i) { opt->type = OPTION_FLOAT; opt->name = options_float[i].name; @@ -843,6 +866,14 @@ static void config_parse_line(ConfigReader *cfg, Settings *settings, const Confi else config_err(cfg, "Invalid %s: %s. This should be an integer from %u to %u.", option->name, value, option->min, option->max); } break; + case OPTION_U32: { + OptionU32 const *option = &any->u._u32; + if (is_integer && integer >= option->min && integer <= option->max) + option_u32_set(settings, option, (u32)integer); + else + config_err(cfg, "Invalid %s: %s. This should be an integer from %" PRIu32 " to %" PRIu32 ".", + option->name, value, option->min, option->max); + } break; case OPTION_FLOAT: { OptionFloat const *option = &any->u._float; if (is_floating && floating >= option->min && floating <= option->max) diff --git a/main.c b/main.c index 1de9985..dab67d9 100644 --- a/main.c +++ b/main.c @@ -32,7 +32,6 @@ FUTURE FEATURES: - comment-start + comment-end settings - robust find (results shouldn't move around when you type things) - multiple files with command line arguments -- configurable max buffer size + max view-only buffer size - :set-build-command - add numlock as a key modifier? (but make sure "Ctrl+S" handles both "No NumLock+Ctrl+S" and "NumLock+Ctrl+S" - better undo chaining (dechain on backspace?) diff --git a/ted.cfg b/ted.cfg index 8a23968..027730e 100644 --- a/ted.cfg +++ b/ted.cfg @@ -36,6 +36,15 @@ identifier-trigger-characters = off # enable LSP support (for autocompletion, etc.) # you can also set `lsp = ""` but this is a quick way to disable LSP servers for all langauges lsp-enabled = yes +# maximum editable file size. +# ted will set the buffer to view-only if a file larger than this is loaded. +# NOTE: ted is not really meant for absolutely massive files. +# it should handle anything up to 100,000 lines just fine (maybe small hiccups in some cases for >20,000) +# files up to 1,000,000 lines should be just fine in view-only mode (although they might take a bit of time to load) +max-file-size = 20000000 +# absolute maximum file size. +# ted will produce an error if a file larger than this is loaded. +max-file-size-view-only = 100000000 # search depth for files to generate tags for. # if set to 0, tag generation/regeneration will do nothing diff --git a/ted.h b/ted.h index ae02fac..8d5d40c 100644 --- a/ted.h +++ b/ted.h @@ -175,6 +175,8 @@ typedef struct { SettingsContext context; float cursor_blink_time_on, cursor_blink_time_off; u32 colors[COLOR_COUNT]; + u32 max_file_size; + u32 max_file_size_view_only; u16 text_size; u16 max_menu_width; u16 error_display_time; -- cgit v1.2.3