2001-05-05 19:01:42 +04:00
|
|
|
/* $Id$ */
|
|
|
|
/**************************************************************************
|
|
|
|
* color.c *
|
|
|
|
* *
|
2009-12-02 06:36:22 +03:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 *
|
2007-10-11 09:01:32 +04:00
|
|
|
* Free Software Foundation, Inc. *
|
2001-05-05 19:01:42 +04:00
|
|
|
* This program is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published by *
|
2007-08-11 09:17:36 +04:00
|
|
|
* the Free Software Foundation; either version 3, or (at your option) *
|
2001-05-05 19:01:42 +04:00
|
|
|
* any later version. *
|
|
|
|
* *
|
2005-05-15 23:57:17 +04:00
|
|
|
* This program is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
|
|
|
|
* General Public License for more details. *
|
2001-05-05 19:01:42 +04:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
|
|
|
* along with this program; if not, write to the Free Software *
|
2005-05-15 23:57:17 +04:00
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
|
|
|
|
* 02110-1301, USA. *
|
2001-05-05 19:01:42 +04:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 05:47:10 +03:00
|
|
|
#include "proto.h"
|
2001-05-05 19:01:42 +04:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-08-01 08:34:27 +04:00
|
|
|
#include <string.h>
|
2011-02-13 07:23:10 +03:00
|
|
|
#include <errno.h>
|
|
|
|
|
|
|
|
#ifdef HAVE_MAGIC_H
|
|
|
|
#include <magic.h>
|
|
|
|
#endif
|
2001-05-05 19:01:42 +04:00
|
|
|
|
2002-07-19 05:08:59 +04:00
|
|
|
#ifdef ENABLE_COLOR
|
|
|
|
|
2005-11-16 11:14:16 +03:00
|
|
|
/* For each syntax list entry, go through the list of colors and assign
|
2007-01-01 08:15:32 +03:00
|
|
|
* the color pairs. */
|
2002-09-27 18:21:59 +04:00
|
|
|
void set_colorpairs(void)
|
2001-05-05 19:01:42 +04:00
|
|
|
{
|
2002-09-27 18:21:59 +04:00
|
|
|
const syntaxtype *this_syntax = syntaxes;
|
|
|
|
|
2003-11-06 01:04:08 +03:00
|
|
|
for (; this_syntax != NULL; this_syntax = this_syntax->next) {
|
2002-09-27 18:21:59 +04:00
|
|
|
colortype *this_color = this_syntax->color;
|
|
|
|
int color_pair = 1;
|
|
|
|
|
2003-11-06 01:04:08 +03:00
|
|
|
for (; this_color != NULL; this_color = this_color->next) {
|
2002-09-27 18:21:59 +04:00
|
|
|
const colortype *beforenow = this_syntax->color;
|
|
|
|
|
2005-03-10 23:55:11 +03:00
|
|
|
for (; beforenow != this_color &&
|
|
|
|
(beforenow->fg != this_color->fg ||
|
|
|
|
beforenow->bg != this_color->bg ||
|
|
|
|
beforenow->bright != this_color->bright);
|
|
|
|
beforenow = beforenow->next)
|
2002-09-27 18:21:59 +04:00
|
|
|
;
|
|
|
|
|
2005-03-10 23:55:11 +03:00
|
|
|
if (beforenow != this_color)
|
2002-09-27 18:21:59 +04:00
|
|
|
this_color->pairnum = beforenow->pairnum;
|
|
|
|
else {
|
|
|
|
this_color->pairnum = color_pair;
|
|
|
|
color_pair++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2005-07-15 04:36:49 +04:00
|
|
|
/* Initialize the color information. */
|
2005-07-14 00:18:46 +04:00
|
|
|
void color_init(void)
|
2002-09-27 18:21:59 +04:00
|
|
|
{
|
2005-07-14 00:18:46 +04:00
|
|
|
assert(openfile != NULL);
|
|
|
|
|
2005-07-27 07:12:22 +04:00
|
|
|
if (has_colors()) {
|
2005-07-14 00:18:46 +04:00
|
|
|
const colortype *tmpcolor;
|
2002-09-27 18:21:59 +04:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2005-03-10 23:55:11 +03:00
|
|
|
bool defok;
|
2002-09-27 18:21:59 +04:00
|
|
|
#endif
|
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
start_color();
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2001-05-05 19:01:42 +04:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2006-01-02 20:09:35 +03:00
|
|
|
/* Use the default colors, if available. */
|
2005-03-10 23:55:11 +03:00
|
|
|
defok = (use_default_colors() != ERR);
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
|
2001-11-29 05:42:27 +03:00
|
|
|
tmpcolor = tmpcolor->next) {
|
2005-07-15 04:17:13 +04:00
|
|
|
short foreground = tmpcolor->fg, background = tmpcolor->bg;
|
|
|
|
if (foreground == -1) {
|
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
|
|
|
if (!defok)
|
|
|
|
#endif
|
|
|
|
foreground = COLOR_WHITE;
|
|
|
|
}
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
if (background == -1) {
|
2002-09-27 18:21:59 +04:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2003-01-13 04:35:15 +03:00
|
|
|
if (!defok)
|
2002-09-27 18:21:59 +04:00
|
|
|
#endif
|
|
|
|
background = COLOR_BLACK;
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
2001-12-02 07:55:44 +03:00
|
|
|
|
2005-07-15 04:17:13 +04:00
|
|
|
init_pair(tmpcolor->pairnum, foreground, background);
|
2001-11-29 05:42:27 +03:00
|
|
|
|
2001-12-12 04:45:01 +03:00
|
|
|
#ifdef DEBUG
|
2005-08-01 09:54:11 +04:00
|
|
|
fprintf(stderr, "init_pair(): fg = %hd, bg = %hd\n", tmpcolor->fg, tmpcolor->bg);
|
2001-12-12 04:45:01 +03:00
|
|
|
#endif
|
2001-11-29 05:42:27 +03:00
|
|
|
}
|
|
|
|
}
|
2001-05-05 19:01:42 +04:00
|
|
|
}
|
|
|
|
|
2011-02-13 07:23:10 +03:00
|
|
|
/* Cleanup a regex we previously compiled */
|
2011-02-23 06:09:23 +03:00
|
|
|
void nfreeregex(regex_t **r)
|
2011-02-13 07:23:10 +03:00
|
|
|
{
|
|
|
|
assert(r != NULL);
|
|
|
|
|
2011-02-23 06:09:23 +03:00
|
|
|
regfree(*r);
|
|
|
|
free(*r);
|
|
|
|
*r = NULL;
|
2011-02-13 07:23:10 +03:00
|
|
|
}
|
|
|
|
|
2003-01-13 04:35:15 +03:00
|
|
|
/* Update the color information based on the current filename. */
|
2005-07-14 00:18:46 +04:00
|
|
|
void color_update(void)
|
2002-05-04 07:47:33 +04:00
|
|
|
{
|
2009-01-25 10:25:17 +03:00
|
|
|
syntaxtype *tmpsyntax;
|
|
|
|
syntaxtype *defsyntax = NULL;
|
2005-08-01 08:23:29 +04:00
|
|
|
colortype *tmpcolor, *defcolor = NULL;
|
2011-02-13 07:23:10 +03:00
|
|
|
exttype *e;
|
|
|
|
|
|
|
|
/* libmagic structures */
|
|
|
|
/* magicstring will be NULL if we fail to get magic result */
|
|
|
|
#ifdef HAVE_LIBMAGIC
|
|
|
|
const char *magicstring = NULL;
|
|
|
|
const char *magicerr = NULL;
|
|
|
|
magic_t m;
|
2011-03-05 08:01:13 +03:00
|
|
|
struct stat fileinfo;
|
2011-02-13 07:23:10 +03:00
|
|
|
#endif /* HAVE_LIBMAGIC */
|
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
|
|
|
|
assert(openfile != NULL);
|
2002-05-04 07:47:33 +04:00
|
|
|
|
2009-01-25 10:25:17 +03:00
|
|
|
openfile->syntax = NULL;
|
2005-07-14 00:18:46 +04:00
|
|
|
openfile->colorstrings = NULL;
|
2005-07-30 01:42:08 +04:00
|
|
|
|
2005-07-31 01:24:56 +04:00
|
|
|
/* If we specified a syntax override string, use it. */
|
|
|
|
if (syntaxstr != NULL) {
|
2005-08-01 08:59:34 +04:00
|
|
|
/* If the syntax override is "none", it's the same as not having
|
|
|
|
* a syntax at all, so get out. */
|
|
|
|
if (strcmp(syntaxstr, "none") == 0)
|
|
|
|
return;
|
|
|
|
|
2005-07-31 01:24:56 +04:00
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
2009-01-25 10:25:17 +03:00
|
|
|
if (strcmp(tmpsyntax->desc, syntaxstr) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2005-07-14 00:18:46 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
2002-05-13 00:54:16 +04:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
if (openfile->colorstrings != NULL)
|
2002-09-27 18:21:59 +04:00
|
|
|
break;
|
2002-05-04 07:47:33 +04:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 08:23:30 +04:00
|
|
|
|
2011-02-13 07:23:10 +03:00
|
|
|
#ifdef HAVE_LIBMAGIC
|
2014-03-01 14:20:57 +04:00
|
|
|
if (stat(openfile->filename, &fileinfo) == 0) {
|
2011-02-13 07:23:10 +03:00
|
|
|
m = magic_open(MAGIC_SYMLINK |
|
|
|
|
#ifdef DEBUG
|
|
|
|
MAGIC_DEBUG | MAGIC_CHECK |
|
2014-03-01 14:20:57 +04:00
|
|
|
#endif
|
2011-02-13 07:23:10 +03:00
|
|
|
MAGIC_ERROR);
|
|
|
|
if (m == NULL || magic_load(m, NULL) < 0)
|
2014-03-01 14:20:57 +04:00
|
|
|
statusbar(_("magic_load() failed: %s"), strerror(errno));
|
2011-02-13 07:23:10 +03:00
|
|
|
else {
|
|
|
|
magicstring = magic_file(m,openfile->filename);
|
|
|
|
if (magicstring == NULL) {
|
|
|
|
magicerr = magic_error(m);
|
2014-03-01 14:20:57 +04:00
|
|
|
statusbar(_("magic_file(%s) failed: %s"), openfile->filename, magicerr);
|
2011-02-13 07:23:10 +03:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
2014-03-01 14:20:57 +04:00
|
|
|
fprintf(stderr, "magic string returned: %s\n", magicstring);
|
|
|
|
#endif
|
2011-02-13 07:23:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* HAVE_LIBMAGIC */
|
|
|
|
|
2005-07-31 01:24:56 +04:00
|
|
|
/* 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
|
2008-09-22 03:02:30 +04:00
|
|
|
* file extension, and then look in the header. */
|
2005-07-31 01:24:56 +04:00
|
|
|
if (openfile->colorstrings == NULL) {
|
2004-05-27 22:39:16 +04:00
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
2005-03-10 23:55:11 +03:00
|
|
|
tmpsyntax = tmpsyntax->next) {
|
2005-07-31 01:24:56 +04:00
|
|
|
|
2005-08-01 08:23:29 +04:00
|
|
|
/* If this is the default syntax, it has no associated
|
2006-07-30 01:40:32 +04:00
|
|
|
* extensions, which we've checked for elsewhere. Skip over
|
|
|
|
* it here, but keep track of its color regexes. */
|
2005-08-01 08:34:27 +04:00
|
|
|
if (strcmp(tmpsyntax->desc, "default") == 0) {
|
2009-01-25 10:25:17 +03:00
|
|
|
defsyntax = tmpsyntax;
|
2006-05-28 22:51:48 +04:00
|
|
|
defcolor = tmpsyntax->color;
|
2005-08-01 08:23:29 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2005-07-31 01:24:56 +04:00
|
|
|
for (e = tmpsyntax->extensions; e != NULL; e = e->next) {
|
|
|
|
bool not_compiled = (e->ext == NULL);
|
|
|
|
|
|
|
|
/* e->ext_regex has already been checked for validity
|
|
|
|
* elsewhere. Compile its specified regex if we haven't
|
|
|
|
* already. */
|
|
|
|
if (not_compiled) {
|
|
|
|
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
|
2008-08-30 09:16:20 +04:00
|
|
|
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
|
2005-07-31 01:24:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Set colorstrings if we matched the extension
|
|
|
|
* regex. */
|
2011-02-13 07:23:10 +03:00
|
|
|
if (regexec(e->ext, openfile->filename, 0, NULL, 0) == 0) {
|
2009-01-25 10:25:17 +03:00
|
|
|
openfile->syntax = tmpsyntax;
|
2005-07-31 01:24:56 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
|
|
|
break;
|
2011-02-13 07:23:10 +03:00
|
|
|
}
|
2005-07-31 01:24:56 +04:00
|
|
|
|
|
|
|
/* Decompile e->ext_regex's specified regex if we aren't
|
|
|
|
* going to use it. */
|
2011-02-13 07:23:10 +03:00
|
|
|
if (not_compiled)
|
2011-02-23 06:09:23 +03:00
|
|
|
nfreeregex(&e->ext);
|
2011-02-13 07:23:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check magic if we don't yet have an answer */
|
|
|
|
#ifdef HAVE_LIBMAGIC
|
|
|
|
if (openfile->colorstrings == NULL) {
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "No match using extension, trying libmagic...\n");
|
2014-03-17 18:15:57 +04:00
|
|
|
#endif
|
2011-02-13 07:23:10 +03:00
|
|
|
|
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
|
|
|
for (e = tmpsyntax->magics; e != NULL; e = e->next) {
|
|
|
|
bool not_compiled = (e->ext == NULL);
|
|
|
|
if (not_compiled) {
|
|
|
|
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
|
|
|
|
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr,"Matching regex \"%s\" against \"%s\"\n",e->ext_regex, magicstring);
|
2014-03-17 18:15:57 +04:00
|
|
|
#endif
|
2011-02-13 07:23:10 +03:00
|
|
|
|
|
|
|
if (magicstring && regexec(e->ext, magicstring, 0, NULL, 0) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (not_compiled)
|
2011-02-23 06:09:23 +03:00
|
|
|
nfreeregex(&e->ext);
|
2005-07-31 01:24:56 +04:00
|
|
|
}
|
|
|
|
}
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
2011-02-13 07:23:10 +03:00
|
|
|
#endif /* HAVE_LIBMAGIC */
|
2008-09-22 03:02:30 +04:00
|
|
|
|
|
|
|
/* If we haven't matched anything yet, try the headers */
|
|
|
|
if (openfile->colorstrings == NULL) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "No match for file extensions, looking at headers...\n");
|
|
|
|
#endif
|
|
|
|
for (tmpsyntax = syntaxes; tmpsyntax != NULL;
|
|
|
|
tmpsyntax = tmpsyntax->next) {
|
|
|
|
|
|
|
|
for (e = tmpsyntax->headers; e != NULL; e = e->next) {
|
|
|
|
bool not_compiled = (e->ext == NULL);
|
|
|
|
|
|
|
|
/* e->ext_regex has already been checked for validity
|
|
|
|
* elsewhere. Compile its specified regex if we haven't
|
|
|
|
* already. */
|
|
|
|
if (not_compiled) {
|
|
|
|
e->ext = (regex_t *)nmalloc(sizeof(regex_t));
|
|
|
|
regcomp(e->ext, fixbounds(e->ext_regex), REG_EXTENDED);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set colorstrings if we matched the extension
|
|
|
|
* regex. */
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Comparing header regex \"%s\" to fileage \"%s\"...\n", e->ext_regex, openfile->fileage->data);
|
|
|
|
#endif
|
2009-01-25 10:25:17 +03:00
|
|
|
if (regexec(e->ext, openfile->fileage->data, 0, NULL, 0) == 0) {
|
|
|
|
openfile->syntax = tmpsyntax;
|
2008-09-22 03:02:30 +04:00
|
|
|
openfile->colorstrings = tmpsyntax->color;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
2008-09-22 03:02:30 +04:00
|
|
|
|
|
|
|
if (openfile->colorstrings != NULL)
|
|
|
|
break;
|
|
|
|
|
|
|
|
/* Decompile e->ext_regex's specified regex if we aren't
|
|
|
|
* going to use it. */
|
2011-02-13 07:23:10 +03:00
|
|
|
if (not_compiled)
|
2011-02-23 06:09:23 +03:00
|
|
|
nfreeregex(&e->ext);
|
2008-09-22 03:02:30 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
|
|
|
|
2008-09-22 03:02:30 +04:00
|
|
|
|
2005-08-01 08:23:29 +04:00
|
|
|
/* If we didn't get a syntax based on the file extension, and we
|
|
|
|
* have a default syntax, use it. */
|
2009-01-25 10:25:17 +03:00
|
|
|
if (openfile->colorstrings == NULL && defcolor != NULL) {
|
|
|
|
openfile->syntax = defsyntax;
|
2005-08-01 08:23:29 +04:00
|
|
|
openfile->colorstrings = defcolor;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
2005-08-01 08:23:29 +04:00
|
|
|
|
2005-07-14 00:18:46 +04:00
|
|
|
for (tmpcolor = openfile->colorstrings; tmpcolor != NULL;
|
|
|
|
tmpcolor = tmpcolor->next) {
|
2005-07-30 01:42:08 +04:00
|
|
|
/* 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) {
|
2005-07-14 00:18:46 +04:00
|
|
|
tmpcolor->start = (regex_t *)nmalloc(sizeof(regex_t));
|
2008-08-30 09:16:20 +04:00
|
|
|
regcomp(tmpcolor->start, fixbounds(tmpcolor->start_regex),
|
2005-07-14 22:33:51 +04:00
|
|
|
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
|
2005-07-14 00:18:46 +04:00
|
|
|
}
|
2005-07-14 22:33:51 +04:00
|
|
|
|
2005-07-30 01:42:08 +04:00
|
|
|
if (tmpcolor->end_regex != NULL && tmpcolor->end == NULL) {
|
2005-07-14 00:18:46 +04:00
|
|
|
tmpcolor->end = (regex_t *)nmalloc(sizeof(regex_t));
|
2008-08-30 09:16:20 +04:00
|
|
|
regcomp(tmpcolor->end, fixbounds(tmpcolor->end_regex),
|
2005-07-14 22:33:51 +04:00
|
|
|
REG_EXTENDED | (tmpcolor->icase ? REG_ICASE : 0));
|
2002-05-04 08:23:30 +04:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 07:47:33 +04:00
|
|
|
}
|
|
|
|
|
2009-02-06 06:41:02 +03:00
|
|
|
/* Reset the multicolor info cache for records for any lines which need
|
|
|
|
to be recalculated */
|
|
|
|
void reset_multis_after(filestruct *fileptr, int mindex)
|
|
|
|
{
|
|
|
|
filestruct *oof;
|
|
|
|
for (oof = fileptr->next; oof != NULL; oof = oof->next) {
|
2009-02-07 17:48:30 +03:00
|
|
|
alloc_multidata_if_needed(oof);
|
2009-02-06 06:41:02 +03:00
|
|
|
if (oof->multidata == NULL)
|
|
|
|
continue;
|
2009-02-07 17:48:30 +03:00
|
|
|
if (oof->multidata[mindex] != CNONE)
|
2009-02-06 06:41:02 +03:00
|
|
|
oof->multidata[mindex] = -1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2009-02-08 21:00:44 +03:00
|
|
|
for (; oof != NULL; oof = oof->next) {
|
|
|
|
alloc_multidata_if_needed(oof);
|
|
|
|
if (oof->multidata == NULL)
|
|
|
|
continue;
|
|
|
|
if (oof->multidata[mindex] == CNONE)
|
|
|
|
oof->multidata[mindex] = -1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
edit_refresh_needed = TRUE;
|
2009-02-06 06:41:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_multis_before(filestruct *fileptr, int mindex)
|
2009-01-25 10:25:17 +03:00
|
|
|
{
|
|
|
|
filestruct *oof;
|
2009-02-06 06:41:02 +03:00
|
|
|
for (oof = fileptr->prev; oof != NULL; oof = oof->prev) {
|
2009-02-07 17:48:30 +03:00
|
|
|
alloc_multidata_if_needed(oof);
|
2009-02-06 06:41:02 +03:00
|
|
|
if (oof->multidata == NULL)
|
|
|
|
continue;
|
2009-02-07 17:48:30 +03:00
|
|
|
if (oof->multidata[mindex] != CNONE)
|
2009-02-06 06:41:02 +03:00
|
|
|
oof->multidata[mindex] = -1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2009-02-08 21:00:44 +03:00
|
|
|
for (; oof != NULL; oof = oof->prev) {
|
|
|
|
alloc_multidata_if_needed(oof);
|
|
|
|
if (oof->multidata == NULL)
|
|
|
|
continue;
|
|
|
|
if (oof->multidata[mindex] == CNONE)
|
|
|
|
oof->multidata[mindex] = -1;
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
edit_refresh_needed = TRUE;
|
2009-02-06 06:41:02 +03:00
|
|
|
}
|
|
|
|
|
2009-02-17 00:04:00 +03:00
|
|
|
/* Reset one multiline regex info */
|
|
|
|
void reset_multis_for_id(filestruct *fileptr, int num)
|
|
|
|
{
|
|
|
|
reset_multis_before(fileptr, num);
|
|
|
|
reset_multis_after(fileptr, num);
|
|
|
|
fileptr->multidata[num] = -1;
|
|
|
|
}
|
2009-02-06 06:41:02 +03:00
|
|
|
|
2009-02-17 00:04:00 +03:00
|
|
|
/* Reset multi line strings around a filestruct ptr, trying to be smart about stopping
|
|
|
|
force = reset everything regardless, useful when we don't know how much screen state
|
|
|
|
has changed */
|
|
|
|
void reset_multis(filestruct *fileptr, bool force)
|
2009-02-06 06:41:02 +03:00
|
|
|
{
|
|
|
|
int nobegin, noend;
|
|
|
|
regmatch_t startmatch, endmatch;
|
|
|
|
const colortype *tmpcolor = openfile->colorstrings;
|
2009-01-25 10:25:17 +03:00
|
|
|
|
2009-01-30 11:34:27 +03:00
|
|
|
if (!openfile->syntax)
|
|
|
|
return;
|
|
|
|
|
2009-02-06 06:41:02 +03:00
|
|
|
for (; tmpcolor != NULL; tmpcolor = tmpcolor->next) {
|
|
|
|
|
|
|
|
/* If it's not a multi-line regex, amscray */
|
|
|
|
if (tmpcolor->end == NULL)
|
|
|
|
continue;
|
|
|
|
|
2009-02-07 17:48:30 +03:00
|
|
|
alloc_multidata_if_needed(fileptr);
|
2009-02-17 00:04:00 +03:00
|
|
|
if (force == TRUE) {
|
|
|
|
reset_multis_for_id(fileptr, tmpcolor->id);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2009-02-06 06:41:02 +03:00
|
|
|
/* Figure out where the first begin and end are to determine if
|
|
|
|
things changed drastically for the precalculated multi values */
|
|
|
|
nobegin = regexec(tmpcolor->start, fileptr->data, 1, &startmatch, 0);
|
|
|
|
noend = regexec(tmpcolor->end, fileptr->data, 1, &endmatch, 0);
|
|
|
|
if (fileptr->multidata[tmpcolor->id] == CWHOLELINE) {
|
|
|
|
if (nobegin && noend)
|
2009-01-25 10:25:17 +03:00
|
|
|
continue;
|
2009-02-17 02:06:09 +03:00
|
|
|
} else if (fileptr->multidata[tmpcolor->id] == CNONE) {
|
|
|
|
if (nobegin && noend)
|
|
|
|
continue;
|
|
|
|
} else if (fileptr->multidata[tmpcolor->id] & CBEGINBEFORE && !noend
|
2009-02-06 06:41:02 +03:00
|
|
|
&& (nobegin || endmatch.rm_eo > startmatch.rm_eo)) {
|
|
|
|
reset_multis_after(fileptr, tmpcolor->id);
|
|
|
|
continue;
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
2009-02-06 06:41:02 +03:00
|
|
|
|
|
|
|
/* If we got here assume the worst */
|
2009-02-17 00:04:00 +03:00
|
|
|
reset_multis_for_id(fileptr, tmpcolor->id);
|
2009-01-25 10:25:17 +03:00
|
|
|
}
|
|
|
|
}
|
2001-05-05 19:01:42 +04:00
|
|
|
#endif /* ENABLE_COLOR */
|