Commit 2385c1aa authored by David Lawrence Ramsey's avatar David Lawrence Ramsey
Browse files

port over more of Brand Huntsman's old patch (with a few tweaks):

compile the file extension regexes on an as-needed basis too


git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@2948 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
No related merge requests found
Showing with 57 additions and 45 deletions
+57 -45
...@@ -49,10 +49,11 @@ CVS code - ...@@ -49,10 +49,11 @@ CVS code -
any variables used to hold them shorts. Changes to any variables used to hold them shorts. Changes to
do_colorinit() (renamed color_init()), color_to_int() (renamed do_colorinit() (renamed color_init()), color_to_int() (renamed
color_to_short()), and parse_colors(). (DLR) color_to_short()), and parse_colors(). (DLR)
- Change color handling to save only the regex strings - Change color handling to save only the extension and color
constantly, and to actually compile them on an as-needed regex strings constantly, and to actually compile them on an
basis. Changes to update_color() and as-needed basis. Changes to update_color(),
thanks_for_all_the_fish(). (Brand Huntsman and DLR) thanks_for_all_the_fish(), nregcomp(), parse_syntax(), and
parse_colors(). (Brand Huntsman and DLR)
- Various other color fixes. Handle unspecified foreground - Various other color fixes. Handle unspecified foreground
colors properly, don't automatically reinitialize the colors properly, don't automatically reinitialize the
displayed colors every time we update the current buffer's displayed colors every time we update the current buffer's
......
...@@ -115,11 +115,19 @@ void color_update(void) ...@@ -115,11 +115,19 @@ void color_update(void)
openfile->colorstrings = NULL; openfile->colorstrings = NULL;
for (tmpsyntax = syntaxes; tmpsyntax != NULL; for (tmpsyntax = syntaxes; tmpsyntax != NULL;
tmpsyntax = tmpsyntax->next) { tmpsyntax = tmpsyntax->next) {
const exttype *e; exttype *e;
for (e = tmpsyntax->extensions; e != NULL; e = e->next) { for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
/* e->ext_regex has already been checked for validity
* elsewhere. Compile its specified regex if we haven't
* already. */
if (e->ext == NULL) {
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(e->ext, e->ext_regex, REG_EXTENDED);
}
/* Set colorstrings if we matched the extension regex. */ /* Set colorstrings if we matched the extension regex. */
if (regexec(&e->ext, openfile->filename, 0, NULL, 0) == 0) if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0)
openfile->colorstrings = tmpsyntax->color; openfile->colorstrings = tmpsyntax->color;
if (openfile->colorstrings != NULL) if (openfile->colorstrings != NULL)
...@@ -139,18 +147,18 @@ void color_update(void) ...@@ -139,18 +147,18 @@ void color_update(void)
} }
} }
/* tmpcolor->start_regex and tmpcolor->end_regex have already been
* checked for validity elsewhere. Compile their associated regexes
* if we haven't already. */
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL; for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
tmpcolor = tmpcolor->next) { tmpcolor = tmpcolor->next) {
if (tmpcolor->start_regex != NULL) { /* tmpcolor->start_regex and tmpcolor->end_regex have already
* been checked for validity elsewhere. Compile their specified
* regexes if we haven't already. */
if (tmpcolor->start == NULL) {
tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t)); tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(tmpcolor->start, tmpcolor->start_regex, regcomp(tmpcolor->start, tmpcolor->start_regex,
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0)); REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
} }
if (tmpcolor->end_regex != NULL) { if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) {
tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t)); tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(tmpcolor->end, tmpcolor->end_regex, regcomp(tmpcolor->end, tmpcolor->end_regex,
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0)); REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
......
...@@ -1219,14 +1219,15 @@ void thanks_for_all_the_fish(void) ...@@ -1219,14 +1219,15 @@ void thanks_for_all_the_fish(void)
exttype *bob = syntaxes->extensions; exttype *bob = syntaxes->extensions;
syntaxes->extensions = bob->next; syntaxes->extensions = bob->next;
regfree(&bob->ext); free(bob->ext_regex);
regfree(bob->ext);
free(bob->ext);
free(bob); free(bob);
} }
while (syntaxes->color != NULL) { while (syntaxes->color != NULL) {
colortype *bob = syntaxes->color; colortype *bob = syntaxes->color;
syntaxes->color = bob->next; syntaxes->color = bob->next;
if (bob->start_regex != NULL)
free(bob->start_regex); free(bob->start_regex);
if (bob->start != NULL) { if (bob->start != NULL) {
regfree(bob->start); regfree(bob->start);
......
...@@ -195,7 +195,8 @@ typedef struct colortype { ...@@ -195,7 +195,8 @@ typedef struct colortype {
} colortype; } colortype;
typedef struct exttype { typedef struct exttype {
regex_t ext; /* The extensions that match this char *ext_regex; /* Extensions that match this syntax. */
regex_t *ext; /* Compiled extensions that match this
* syntax. */ * syntax. */
struct exttype *next; struct exttype *next;
} exttype; } exttype;
......
...@@ -417,7 +417,7 @@ char *parse_argument(char *ptr); ...@@ -417,7 +417,7 @@ char *parse_argument(char *ptr);
#ifdef ENABLE_COLOR #ifdef ENABLE_COLOR
short color_to_short(const char *colorname, bool *bright); short color_to_short(const char *colorname, bool *bright);
char *parse_next_regex(char *ptr); char *parse_next_regex(char *ptr);
bool nregcomp(regex_t *preg, const char *regex, int eflags); bool nregcomp(const char *regex, int eflags);
void parse_syntax(char *ptr); void parse_syntax(char *ptr);
void parse_colors(char *ptr, bool icase); void parse_colors(char *ptr, bool icase);
#endif /* ENABLE_COLOR */ #endif /* ENABLE_COLOR */
......
...@@ -244,21 +244,24 @@ char *parse_next_regex(char *ptr) ...@@ -244,21 +244,24 @@ char *parse_next_regex(char *ptr)
return ptr; return ptr;
} }
/* Compile the regular expression regex to preg. Return TRUE on /* Compile the regular expression regex to see if it's valid. Return
* success, or FALSE if the expression is invalid. */ * TRUE if it is, or FALSE otherwise. */
bool nregcomp(regex_t *preg, const char *regex, int eflags) bool nregcomp(const char *regex, int eflags)
{ {
int rc = regcomp(preg, regex, REG_EXTENDED | eflags); regex_t preg;
int rc = regcomp(&preg, regex, REG_EXTENDED | eflags);
if (rc != 0) { if (rc != 0) {
size_t len = regerror(rc, preg, NULL, 0); size_t len = regerror(rc, &preg, NULL, 0);
char *str = charalloc(len); char *str = charalloc(len);
regerror(rc, preg, str, len); regerror(rc, &preg, str, len);
rcfile_error(N_("Bad regex \"%s\": %s"), regex, str); rcfile_error(N_("Bad regex \"%s\": %s"), regex, str);
free(str); free(str);
} }
regfree(&preg);
return (rc == 0); return (rc == 0);
} }
...@@ -299,11 +302,13 @@ void parse_syntax(char *ptr) ...@@ -299,11 +302,13 @@ void parse_syntax(char *ptr)
fprintf(stderr, "Adding new syntax after first one\n"); fprintf(stderr, "Adding new syntax after first one\n");
#endif #endif
} }
endsyntax->desc = mallocstrcpy(NULL, nameptr); endsyntax->desc = mallocstrcpy(NULL, nameptr);
endsyntax->color = NULL; endsyntax->color = NULL;
endcolor = NULL; endcolor = NULL;
endsyntax->extensions = NULL; endsyntax->extensions = NULL;
endsyntax->next = NULL; endsyntax->next = NULL;
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr); fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
#endif #endif
...@@ -327,7 +332,12 @@ void parse_syntax(char *ptr) ...@@ -327,7 +332,12 @@ void parse_syntax(char *ptr)
break; break;
newext = (exttype *)nmalloc(sizeof(exttype)); newext = (exttype *)nmalloc(sizeof(exttype));
if (nregcomp(&newext->ext, fileregptr, REG_NOSUB)) {
/* Save the extension regex if it's valid. */
if (nregcomp(fileregptr, REG_NOSUB)) {
newext->ext_regex = mallocstrcpy(NULL, fileregptr);
newext->ext = NULL;
if (endext == NULL) if (endext == NULL)
endsyntax->extensions = newext; endsyntax->extensions = newext;
else else
...@@ -422,28 +432,27 @@ void parse_colors(char *ptr, bool icase) ...@@ -422,28 +432,27 @@ void parse_colors(char *ptr, bool icase)
ptr++; ptr++;
newcolor = (colortype *)nmalloc(sizeof(colortype));
fgstr = ptr; fgstr = ptr;
ptr = parse_next_regex(ptr); ptr = parse_next_regex(ptr);
if (ptr == NULL) if (ptr == NULL)
break; break;
newcolor->start = (regex_t *)nmalloc(sizeof(regex_t)); newcolor = (colortype *)nmalloc(sizeof(colortype));
if (nregcomp(newcolor->start, fgstr, icase ? REG_ICASE : 0)) {
/* Free this regex, now that we know it's valid, and save
* the original string, so that we can recompile this regex
* later as needed. */
newcolor->start_regex = mallocstrcpy(NULL, fgstr);
regfree(newcolor->start);
free(newcolor->start);
newcolor->start = NULL;
/* Save the starting regex string if it's valid, and set up the
* color information. */
if (nregcomp(fgstr, icase ? REG_ICASE : 0)) {
newcolor->fg = fg; newcolor->fg = fg;
newcolor->bg = bg; newcolor->bg = bg;
newcolor->bright = bright; newcolor->bright = bright;
newcolor->icase = icase; newcolor->icase = icase;
newcolor->start_regex = mallocstrcpy(NULL, fgstr);
newcolor->start = NULL;
newcolor->end_regex = NULL; newcolor->end_regex = NULL;
newcolor->end = NULL; newcolor->end = NULL;
newcolor->next = NULL; newcolor->next = NULL;
if (endcolor == NULL) { if (endcolor == NULL) {
...@@ -457,9 +466,9 @@ void parse_colors(char *ptr, bool icase) ...@@ -457,9 +466,9 @@ void parse_colors(char *ptr, bool icase)
#endif #endif
endcolor->next = newcolor; endcolor->next = newcolor;
} }
endcolor = newcolor; endcolor = newcolor;
} else { } else {
free(newcolor->start);
free(newcolor); free(newcolor);
cancelled = TRUE; cancelled = TRUE;
} }
...@@ -489,17 +498,9 @@ void parse_colors(char *ptr, bool icase) ...@@ -489,17 +498,9 @@ void parse_colors(char *ptr, bool icase)
if (cancelled) if (cancelled)
continue; continue;
newcolor->end = (regex_t *)nmalloc(sizeof(regex_t)); /* Save the ending regex string if it's valid. */
if (nregcomp(newcolor->end, fgstr, icase ? REG_ICASE : 0)) { newcolor->end_regex = (nregcomp(fgstr, icase ? REG_ICASE :
/* Free this regex, now that we know it's valid, and 0)) ? mallocstrcpy(NULL, fgstr) : NULL;
* save the original string, so that we can recompile
* this regex later as needed. */
newcolor->end_regex = mallocstrcpy(NULL, fgstr);
regfree(newcolor->end);
}
free(newcolor->end);
newcolor->end = NULL;
} }
} }
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment