From 917d7590d31670f020e86717d6f11bd8c4355992 Mon Sep 17 00:00:00 2001 From: Tim Edwards Date: Mon, 27 Jul 2020 12:10:08 -0400 Subject: [PATCH] Implemented a string truncation with ellipsis for the output to the caption line in the GUI window, which was causing problems with long filenames overrunning the string array dedicated to the caption line. Thanks to Sylvain Munaut for the patch. --- VERSION | 2 +- commands/CmdSubrs.c | 54 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/VERSION b/VERSION index 2997be56..2f376d3e 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -8.3.41 +8.3.42 diff --git a/commands/CmdSubrs.c b/commands/CmdSubrs.c index 842b026a..b2f2b954 100644 --- a/commands/CmdSubrs.c +++ b/commands/CmdSubrs.c @@ -774,6 +774,37 @@ again: return (returnname); } +/* + * ---------------------------------------------------------------------------- + * + * nameEllipsis --- + * + * Truncate a string an append an ellipsis ("...") to the end if the string + * will overflow a fixed array length. + * + * ---------------------------------------------------------------------------- + */ + +static char * +nameEllipsis(name, maxlen, prefix) + char *name; + int maxlen; + char **prefix; +{ + int l = strlen(name); + + if (l < maxlen) + { + *prefix = ""; + return name; + } + else + { + *prefix = "..."; + return &name[l - maxlen + 3]; + } +} + /* * ---------------------------------------------------------------------------- * @@ -804,12 +835,14 @@ cmdSaveWindSet(window, def) { char caption[200]; CellDef *rootDef; + char *name, *name_pfx; rootDef = ((CellUse *) window->w_surfaceID)->cu_def; if (rootDef != def) return 0; - (void) sprintf(caption, "%s [NOT BEING EDITED]", def->cd_name); + name = nameEllipsis(def->cd_name, 175, &name_pfx); + (void) snprintf(caption, sizeof(caption), "%s%s [NOT BEING EDITED]", name_pfx, name); (void) StrDup(&window->w_iconname, def->cd_name); WindCaption(window, caption); return 0; @@ -896,13 +929,22 @@ cmdWindSet(window) { char caption[200]; CellDef *wDef; + char *name[2], *name_pfx[2]; wDef = ((CellUse *) window->w_surfaceID)->cu_def; - if (wDef != newRootDef) - (void) sprintf(caption, "%s [NOT BEING EDITED]", wDef->cd_name); - else { - (void) sprintf(caption, "%s EDITING %s", wDef->cd_name, - newEditDef->cd_name); + + + + if (wDef != newRootDef) { + name[0] = nameEllipsis(wDef->cd_name, 175, &name_pfx[0]); + (void) snprintf(caption, sizeof(caption), "%s%s [NOT BEING EDITED]", + name_pfx[0], name[0]); + } else { + name[0] = nameEllipsis(wDef->cd_name, 90, &name_pfx[0]); + name[1] = nameEllipsis(newEditDef->cd_name, 90, &name_pfx[1]); + (void) snprintf(caption, sizeof(caption), "%s%s EDITING %s%s", + name_pfx[0], name[0], name_pfx[1], name[1]); + #ifdef SCHEME_INTERPRETER /* Add a binding to scheme variable "edit-cell" */ LispSetEdit (newEditDef->cd_name);