Setting the syntax and regex pointers just once, in a single place.

git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@5723 35c25a1d-7b9e-4130-9fde-d3aeb78583b8
master
Benno Schulenberg 2016-03-12 08:46:18 +00:00
parent 1786484486
commit c5fedaa043
2 changed files with 19 additions and 30 deletions

View File

@ -1,3 +1,7 @@
2016-03-12 Benno Schulenberg <bensberg@justemail.net>
* src/color.c (color_update): Set the syntax and regex pointers
just once, in a single place.
2016-03-11 Benno Schulenberg <bensberg@justemail.net> 2016-03-11 Benno Schulenberg <bensberg@justemail.net>
* src/browser.c (do_browser): Fix compilation when configured with * src/browser.c (do_browser): Fix compilation when configured with
--enable-tiny plus --enable-browser. --enable-tiny plus --enable-browser.

View File

@ -163,14 +163,11 @@ bool found_in_list(regexlisttype *head, const char *shibboleth)
/* Update the color information based on the current filename. */ /* Update the color information based on the current filename. */
void color_update(void) void color_update(void)
{ {
syntaxtype *sint; syntaxtype *sint = NULL;
colortype *ink; colortype *ink;
assert(openfile != NULL); assert(openfile != NULL);
openfile->syntax = NULL;
openfile->colorstrings = NULL;
/* If the rcfiles were not read, or contained no syntaxes, get out. */ /* If the rcfiles were not read, or contained no syntaxes, get out. */
if (syntaxes == NULL) if (syntaxes == NULL)
return; return;
@ -183,21 +180,18 @@ void color_update(void)
return; return;
for (sint = syntaxes; sint != NULL; sint = sint->next) { for (sint = syntaxes; sint != NULL; sint = sint->next) {
if (strcmp(sint->name, syntaxstr) == 0) { if (strcmp(sint->name, syntaxstr) == 0)
openfile->syntax = sint;
openfile->colorstrings = sint->color;
break; break;
} }
}
if (openfile->syntax == NULL) if (sint == NULL)
statusbar(_("Unknown syntax name: %s"), syntaxstr); statusbar(_("Unknown syntax name: %s"), syntaxstr);
} }
/* If we didn't specify a syntax override string, or if we did and /* If we didn't specify a syntax override string, or if we did and
* there was no syntax by that name, get the syntax based on the * there was no syntax by that name, get the syntax based on the
* file extension, then try the headerline, and then try magic. */ * file extension, then try the headerline, and then try magic. */
if (openfile->syntax == NULL) { if (sint == NULL) {
char *currentdir = getcwd(NULL, PATH_MAX + 1); char *currentdir = getcwd(NULL, PATH_MAX + 1);
char *joinednames = charalloc(PATH_MAX + 1); char *joinednames = charalloc(PATH_MAX + 1);
char *fullname = NULL; char *fullname = NULL;
@ -214,33 +208,27 @@ void color_update(void)
fullname = mallocstrcpy(fullname, openfile->filename); fullname = mallocstrcpy(fullname, openfile->filename);
for (sint = syntaxes; sint != NULL; sint = sint->next) { for (sint = syntaxes; sint != NULL; sint = sint->next) {
if (found_in_list(sint->extensions, fullname)) { if (found_in_list(sint->extensions, fullname))
openfile->syntax = sint;
openfile->colorstrings = sint->color;
break; break;
} }
}
free(joinednames); free(joinednames);
free(fullname); free(fullname);
/* Check the headerline if the extension didn't match anything. */ /* Check the headerline if the extension didn't match anything. */
if (openfile->syntax == NULL) { if (sint == NULL) {
#ifdef DEBUG #ifdef DEBUG
fprintf(stderr, "No result from file extension, trying headerline...\n"); fprintf(stderr, "No result from file extension, trying headerline...\n");
#endif #endif
for (sint = syntaxes; sint != NULL; sint = sint->next) { for (sint = syntaxes; sint != NULL; sint = sint->next) {
if (found_in_list(sint->headers, openfile->fileage->data)) { if (found_in_list(sint->headers, openfile->fileage->data))
openfile->syntax = sint;
openfile->colorstrings = sint->color;
break; break;
} }
} }
}
#ifdef HAVE_LIBMAGIC #ifdef HAVE_LIBMAGIC
/* Check magic if we don't have an answer yet. */ /* Check magic if we don't have an answer yet. */
if (openfile->syntax == NULL) { if (sint == NULL) {
struct stat fileinfo; struct stat fileinfo;
magic_t cookie = NULL; magic_t cookie = NULL;
const char *magicstring = NULL; const char *magicstring = NULL;
@ -271,13 +259,10 @@ void color_update(void)
/* Now try and find a syntax that matches the magicstring. */ /* Now try and find a syntax that matches the magicstring. */
if (magicstring != NULL) { if (magicstring != NULL) {
for (sint = syntaxes; sint != NULL; sint = sint->next) { for (sint = syntaxes; sint != NULL; sint = sint->next) {
if (found_in_list(sint->magics, magicstring)) { if (found_in_list(sint->magics, magicstring))
openfile->syntax = sint;
openfile->colorstrings = sint->color;
break; break;
} }
} }
}
if (stat(openfile->filename, &fileinfo) == 0) if (stat(openfile->filename, &fileinfo) == 0)
magic_close(cookie); magic_close(cookie);
@ -286,19 +271,19 @@ void color_update(void)
} }
/* If we didn't find any syntax yet, see if there is a default one. */ /* If we didn't find any syntax yet, see if there is a default one. */
if (openfile->syntax == NULL) { if (sint == NULL) {
for (sint = syntaxes; sint != NULL; sint = sint->next) { for (sint = syntaxes; sint != NULL; sint = sint->next) {
if (strcmp(sint->name, "default") == 0) { if (strcmp(sint->name, "default") == 0)
openfile->syntax = sint;
openfile->colorstrings = sint->color;
break; break;
} }
} }
}
openfile->syntax = sint;
openfile->colorstrings = (sint == NULL ? NULL : sint->color);
/* If a syntax was found, compile its specified regexes, which have /* If a syntax was found, compile its specified regexes, which have
* already been checked for validity when they were read in. */ * already been checked for validity when they were read in. */
for (ink = openfile->colorstrings; ink != NULL; ink = ink->next) { for (ink = sint->color; ink != NULL; ink = ink->next) {
if (ink->start == NULL) { if (ink->start == NULL) {
ink->start = (regex_t *)nmalloc(sizeof(regex_t)); ink->start = (regex_t *)nmalloc(sizeof(regex_t));
regcomp(ink->start, fixbounds(ink->start_regex), regcomp(ink->start, fixbounds(ink->start_regex),