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
master
David Lawrence Ramsey 2005-07-29 21:42:08 +00:00
parent 282a940745
commit 2385c1aa9b
6 changed files with 57 additions and 45 deletions

View File

@ -49,10 +49,11 @@ CVS code -
any variables used to hold them shorts. Changes to
do_colorinit() (renamed color_init()), color_to_int() (renamed
color_to_short()), and parse_colors(). (DLR)
- Change color handling to save only the regex strings
constantly, and to actually compile them on an as-needed
basis. Changes to update_color() and
thanks_for_all_the_fish(). (Brand Huntsman and DLR)
- Change color handling to save only the extension and color
regex strings constantly, and to actually compile them on an
as-needed basis. Changes to update_color(),
thanks_for_all_the_fish(), nregcomp(), parse_syntax(), and
parse_colors(). (Brand Huntsman and DLR)
- Various other color fixes. Handle unspecified foreground
colors properly, don't automatically reinitialize the
displayed colors every time we update the current buffer's

View File

@ -115,11 +115,19 @@ void color_update(void)
openfile->colorstrings = NULL;
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
tmpsyntax = tmpsyntax->next) {
const exttype *e;
exttype *e;
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. */
if (regexec(&e->ext, openfile->filename, 0, NULL, 0) == 0)
if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0)
openfile->colorstrings = tmpsyntax->color;
if (openfile->colorstrings != NULL)
@ -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;
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));
regcomp(tmpcolor->start, tmpcolor->start_regex,
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));
regcomp(tmpcolor->end, tmpcolor->end_regex,
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));

View File

@ -1219,15 +1219,16 @@ void thanks_for_all_the_fish(void)
exttype *bob = syntaxes->extensions;
syntaxes->extensions = bob->next;
regfree(&bob->ext);
free(bob->ext_regex);
regfree(bob->ext);
free(bob->ext);
free(bob);
}
while (syntaxes->color != NULL) {
colortype *bob = syntaxes->color;
syntaxes->color = bob->next;
if (bob->start_regex != NULL)
free(bob->start_regex);
free(bob->start_regex);
if (bob->start != NULL) {
regfree(bob->start);
free(bob->start);

View File

@ -195,7 +195,8 @@ typedef struct colortype {
} colortype;
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. */
struct exttype *next;
} exttype;

View File

@ -417,7 +417,7 @@ char *parse_argument(char *ptr);
#ifdef ENABLE_COLOR
short color_to_short(const char *colorname, bool *bright);
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_colors(char *ptr, bool icase);
#endif /* ENABLE_COLOR */

View File

@ -244,21 +244,24 @@ char *parse_next_regex(char *ptr)
return ptr;
}
/* Compile the regular expression regex to preg. Return TRUE on
* success, or FALSE if the expression is invalid. */
bool nregcomp(regex_t *preg, const char *regex, int eflags)
/* Compile the regular expression regex to see if it's valid. Return
* TRUE if it is, or FALSE otherwise. */
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) {
size_t len = regerror(rc, preg, NULL, 0);
size_t len = regerror(rc, &preg, NULL, 0);
char *str = charalloc(len);
regerror(rc, preg, str, len);
regerror(rc, &preg, str, len);
rcfile_error(N_("Bad regex \"%s\": %s"), regex, str);
free(str);
}
regfree(&preg);
return (rc == 0);
}
@ -299,11 +302,13 @@ void parse_syntax(char *ptr)
fprintf(stderr, "Adding new syntax after first one\n");
#endif
}
endsyntax->desc = mallocstrcpy(NULL, nameptr);
endsyntax->color = NULL;
endcolor = NULL;
endsyntax->extensions = NULL;
endsyntax->next = NULL;
#ifdef DEBUG
fprintf(stderr, "Starting a new syntax type: \"%s\"\n", nameptr);
#endif
@ -327,7 +332,12 @@ void parse_syntax(char *ptr)
break;
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)
endsyntax->extensions = newext;
else
@ -422,28 +432,27 @@ void parse_colors(char *ptr, bool icase)
ptr++;
newcolor = (colortype *)nmalloc(sizeof(colortype));
fgstr = ptr;
ptr = parse_next_regex(ptr);
if (ptr == NULL)
break;
newcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
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;
newcolor = (colortype *)nmalloc(sizeof(colortype));
/* 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->bg = bg;
newcolor->bright = bright;
newcolor->icase = icase;
newcolor->start_regex = mallocstrcpy(NULL, fgstr);
newcolor->start = NULL;
newcolor->end_regex = NULL;
newcolor->end = NULL;
newcolor->next = NULL;
if (endcolor == NULL) {
@ -457,9 +466,9 @@ void parse_colors(char *ptr, bool icase)
#endif
endcolor->next = newcolor;
}
endcolor = newcolor;
} else {
free(newcolor->start);
free(newcolor);
cancelled = TRUE;
}
@ -489,17 +498,9 @@ void parse_colors(char *ptr, bool icase)
if (cancelled)
continue;
newcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
if (nregcomp(newcolor->end, 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->end_regex = mallocstrcpy(NULL, fgstr);
regfree(newcolor->end);
}
free(newcolor->end);
newcolor->end = NULL;
/* Save the ending regex string if it's valid. */
newcolor->end_regex = (nregcomp(fgstr, icase ? REG_ICASE :
0)) ? mallocstrcpy(NULL, fgstr) : NULL;
}
}
}