2001-05-05 15:01:42 +00:00
|
|
|
/**************************************************************************
|
2016-08-29 15:10:49 +00:00
|
|
|
* color.c -- This file is part of GNU nano. *
|
2001-05-05 15:01:42 +00:00
|
|
|
* *
|
2014-04-30 20:18:26 +00:00
|
|
|
* Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, *
|
2015-04-28 19:18:38 +00:00
|
|
|
* 2010, 2011, 2013, 2014, 2015 Free Software Foundation, Inc. *
|
2016-08-29 13:14:18 +00:00
|
|
|
* Copyright (C) 2014, 2015, 2016 Benno Schulenberg *
|
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano is free software: you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, *
|
|
|
|
* or (at your option) any later version. *
|
2001-05-05 15:01:42 +00:00
|
|
|
* *
|
2016-08-29 15:10:49 +00:00
|
|
|
* GNU nano 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 15:01:42 +00:00
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU General Public License *
|
2016-08-29 15:10:49 +00:00
|
|
|
* along with this program. If not, see http://www.gnu.org/licenses/. *
|
2001-05-05 15:01:42 +00:00
|
|
|
* *
|
|
|
|
**************************************************************************/
|
|
|
|
|
2005-12-08 02:47:10 +00:00
|
|
|
#include "proto.h"
|
2001-05-05 15:01:42 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2005-08-01 04:34:27 +00:00
|
|
|
#include <string.h>
|
2011-02-13 04:23:10 +00:00
|
|
|
#include <errno.h>
|
2015-12-22 16:51:00 +00:00
|
|
|
#include <time.h>
|
2015-04-28 19:18:38 +00:00
|
|
|
#include <unistd.h>
|
2011-02-13 04:23:10 +00:00
|
|
|
|
|
|
|
#ifdef HAVE_MAGIC_H
|
|
|
|
#include <magic.h>
|
|
|
|
#endif
|
2001-05-05 15:01:42 +00:00
|
|
|
|
2014-04-04 11:59:03 +00:00
|
|
|
#ifndef DISABLE_COLOR
|
2002-07-19 01:08:59 +00:00
|
|
|
|
2015-12-18 18:44:40 +00:00
|
|
|
/* Initialize the colors for nano's interface, and assign pair numbers
|
|
|
|
* for the colors in each syntax. */
|
2002-09-27 14:21:59 +00:00
|
|
|
void set_colorpairs(void)
|
2001-05-05 15:01:42 +00:00
|
|
|
{
|
2016-07-25 08:53:49 +00:00
|
|
|
const syntaxtype *sint;
|
2015-12-18 18:44:40 +00:00
|
|
|
bool using_defaults = FALSE;
|
|
|
|
short foreground, background;
|
2014-05-03 18:24:45 +00:00
|
|
|
size_t i;
|
|
|
|
|
2015-12-18 18:44:40 +00:00
|
|
|
/* Tell ncurses to enable colors. */
|
2014-05-03 18:24:45 +00:00
|
|
|
start_color();
|
|
|
|
|
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2015-12-18 18:44:40 +00:00
|
|
|
/* Allow using the default colors, if available. */
|
|
|
|
using_defaults = (use_default_colors() != ERR);
|
2014-05-03 18:24:45 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-18 18:44:40 +00:00
|
|
|
/* Initialize the color pairs for nano's interface elements. */
|
2014-05-03 18:24:45 +00:00
|
|
|
for (i = 0; i < NUMBER_OF_ELEMENTS; i++) {
|
2014-05-10 19:15:04 +00:00
|
|
|
bool bright = FALSE;
|
|
|
|
|
2015-12-18 18:44:40 +00:00
|
|
|
if (parse_color_names(specified_color_combo[i],
|
|
|
|
&foreground, &background, &bright)) {
|
|
|
|
if (foreground == -1 && !using_defaults)
|
|
|
|
foreground = COLOR_WHITE;
|
|
|
|
if (background == -1 && !using_defaults)
|
|
|
|
background = COLOR_BLACK;
|
|
|
|
init_pair(i + 1, foreground, background);
|
2016-07-12 07:35:48 +00:00
|
|
|
interface_color_pair[i] =
|
2016-07-11 20:25:56 +00:00
|
|
|
COLOR_PAIR(i + 1) | (bright ? A_BOLD : A_NORMAL);
|
2014-05-10 19:15:04 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (i != FUNCTION_TAG)
|
2016-07-12 07:35:48 +00:00
|
|
|
interface_color_pair[i] = hilite_attribute;
|
2014-05-10 19:15:04 +00:00
|
|
|
else
|
2016-07-12 07:35:48 +00:00
|
|
|
interface_color_pair[i] = A_NORMAL;
|
2014-05-03 18:24:45 +00:00
|
|
|
}
|
|
|
|
|
2015-06-14 19:14:41 +00:00
|
|
|
free(specified_color_combo[i]);
|
|
|
|
specified_color_combo[i] = NULL;
|
2014-05-03 18:24:45 +00:00
|
|
|
}
|
2002-09-27 14:21:59 +00:00
|
|
|
|
2015-12-18 18:44:40 +00:00
|
|
|
/* For each syntax, go through its list of colors and assign each
|
|
|
|
* its pair number, giving identical color pairs the same number. */
|
2016-07-25 08:53:49 +00:00
|
|
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
|
|
|
colortype *ink;
|
2016-07-25 08:57:33 +00:00
|
|
|
int new_number = NUMBER_OF_ELEMENTS + 1;
|
2002-09-27 14:21:59 +00:00
|
|
|
|
2016-07-25 08:53:49 +00:00
|
|
|
for (ink = sint->color; ink != NULL; ink = ink->next) {
|
|
|
|
const colortype *beforenow = sint->color;
|
2002-09-27 14:21:59 +00:00
|
|
|
|
2016-07-25 08:53:49 +00:00
|
|
|
while (beforenow != ink && (beforenow->fg != ink->fg ||
|
|
|
|
beforenow->bg != ink->bg ||
|
|
|
|
beforenow->bright != ink->bright))
|
2015-12-18 18:44:40 +00:00
|
|
|
beforenow = beforenow->next;
|
2002-09-27 14:21:59 +00:00
|
|
|
|
2016-07-25 08:53:49 +00:00
|
|
|
if (beforenow != ink)
|
|
|
|
ink->pairnum = beforenow->pairnum;
|
2016-07-25 08:57:33 +00:00
|
|
|
else
|
|
|
|
ink->pairnum = new_number++;
|
2016-07-17 13:29:07 +00:00
|
|
|
|
2016-07-25 08:53:49 +00:00
|
|
|
ink->attributes = COLOR_PAIR(ink->pairnum) |
|
|
|
|
(ink->bright ? A_BOLD : A_NORMAL);
|
2002-09-27 14:21:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2005-07-15 00:36:49 +00:00
|
|
|
/* Initialize the color information. */
|
2005-07-13 20:18:46 +00:00
|
|
|
void color_init(void)
|
2002-09-27 14:21:59 +00:00
|
|
|
{
|
2016-07-04 11:43:19 +00:00
|
|
|
const colortype *ink;
|
2015-12-18 10:41:09 +00:00
|
|
|
bool using_defaults = FALSE;
|
|
|
|
short foreground, background;
|
|
|
|
|
2005-07-13 20:18:46 +00:00
|
|
|
assert(openfile != NULL);
|
|
|
|
|
2015-12-18 10:41:09 +00:00
|
|
|
/* If the terminal is not capable of colors, forget it. */
|
|
|
|
if (!has_colors())
|
|
|
|
return;
|
|
|
|
|
2001-05-05 15:01:42 +00:00
|
|
|
#ifdef HAVE_USE_DEFAULT_COLORS
|
2015-12-18 10:41:09 +00:00
|
|
|
/* Allow using the default colors, if available. */
|
|
|
|
using_defaults = (use_default_colors() != ERR);
|
2001-05-05 15:01:42 +00:00
|
|
|
#endif
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2015-12-18 10:45:35 +00:00
|
|
|
/* For each coloring expression, initialize the color pair. */
|
2016-07-04 11:43:19 +00:00
|
|
|
for (ink = openfile->colorstrings; ink != NULL; ink = ink->next) {
|
|
|
|
foreground = ink->fg;
|
|
|
|
background = ink->bg;
|
2015-12-18 10:41:09 +00:00
|
|
|
|
2015-12-18 10:45:35 +00:00
|
|
|
if (foreground == -1 && !using_defaults)
|
|
|
|
foreground = COLOR_WHITE;
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2015-12-18 10:45:35 +00:00
|
|
|
if (background == -1 && !using_defaults)
|
|
|
|
background = COLOR_BLACK;
|
2001-11-29 02:42:27 +00:00
|
|
|
|
2016-07-04 11:43:19 +00:00
|
|
|
init_pair(ink->pairnum, foreground, background);
|
2001-12-12 01:45:01 +00:00
|
|
|
#ifdef DEBUG
|
2015-12-18 10:41:09 +00:00
|
|
|
fprintf(stderr, "init_pair(): fg = %hd, bg = %hd\n", foreground, background);
|
2001-12-12 01:45:01 +00:00
|
|
|
#endif
|
2015-12-18 10:45:35 +00:00
|
|
|
}
|
2001-05-05 15:01:42 +00:00
|
|
|
}
|
|
|
|
|
2016-02-26 16:37:23 +00:00
|
|
|
/* Try to match the given shibboleth string with one of the regexes in
|
|
|
|
* the list starting at head. Return TRUE upon success. */
|
|
|
|
bool found_in_list(regexlisttype *head, const char *shibboleth)
|
|
|
|
{
|
|
|
|
regexlisttype *item;
|
2016-03-04 14:53:45 +00:00
|
|
|
regex_t rgx;
|
2016-02-26 16:37:23 +00:00
|
|
|
|
|
|
|
for (item = head; item != NULL; item = item->next) {
|
2016-03-22 10:42:28 +00:00
|
|
|
regcomp(&rgx, fixbounds(item->full_regex), NANO_REG_EXTENDED);
|
2016-02-26 16:37:23 +00:00
|
|
|
|
2016-03-04 14:53:45 +00:00
|
|
|
if (regexec(&rgx, shibboleth, 0, NULL, 0) == 0) {
|
|
|
|
regfree(&rgx);
|
2016-02-26 16:37:23 +00:00
|
|
|
return TRUE;
|
2016-02-26 17:25:48 +00:00
|
|
|
}
|
2016-03-04 14:53:45 +00:00
|
|
|
|
|
|
|
regfree(&rgx);
|
2016-02-26 16:37:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-03-12 09:18:50 +00:00
|
|
|
/* Update the color information based on the current filename and content. */
|
2005-07-13 20:18:46 +00:00
|
|
|
void color_update(void)
|
2002-05-04 03:47:33 +00:00
|
|
|
{
|
2016-03-12 08:46:18 +00:00
|
|
|
syntaxtype *sint = NULL;
|
2016-03-04 20:50:38 +00:00
|
|
|
colortype *ink;
|
2011-02-13 04:23:10 +00:00
|
|
|
|
2005-07-13 20:18:46 +00:00
|
|
|
assert(openfile != NULL);
|
2002-05-04 03:47:33 +00:00
|
|
|
|
2014-06-30 17:47:07 +00:00
|
|
|
/* If the rcfiles were not read, or contained no syntaxes, get out. */
|
|
|
|
if (syntaxes == NULL)
|
|
|
|
return;
|
|
|
|
|
2016-03-12 09:18:50 +00:00
|
|
|
/* If we specified a syntax-override string, use it. */
|
2005-07-30 21:24:56 +00:00
|
|
|
if (syntaxstr != NULL) {
|
2016-03-12 09:18:50 +00:00
|
|
|
/* An override of "none" is like having no syntax at all. */
|
2005-08-01 04:59:34 +00:00
|
|
|
if (strcmp(syntaxstr, "none") == 0)
|
|
|
|
return;
|
|
|
|
|
2016-02-26 17:11:35 +00:00
|
|
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
2016-03-12 08:46:18 +00:00
|
|
|
if (strcmp(sint->name, syntaxstr) == 0)
|
2016-03-10 20:06:01 +00:00
|
|
|
break;
|
2002-05-04 03:47:33 +00:00
|
|
|
}
|
2015-11-24 11:42:19 +00:00
|
|
|
|
2016-03-12 08:46:18 +00:00
|
|
|
if (sint == NULL)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("Unknown syntax name: %s"), syntaxstr);
|
2002-05-04 03:47:33 +00:00
|
|
|
}
|
2002-05-04 04:23:30 +00:00
|
|
|
|
2016-03-12 09:18:50 +00:00
|
|
|
/* If no syntax-override string was specified, or it didn't match,
|
|
|
|
* try finding a syntax based on the filename (extension). */
|
2016-03-12 08:46:18 +00:00
|
|
|
if (sint == NULL) {
|
2016-06-04 10:02:49 +00:00
|
|
|
char *reserved = charalloc(PATH_MAX + 1);
|
|
|
|
char *currentdir = getcwd(reserved, PATH_MAX + 1);
|
2015-04-28 19:18:38 +00:00
|
|
|
char *joinednames = charalloc(PATH_MAX + 1);
|
|
|
|
char *fullname = NULL;
|
|
|
|
|
2016-06-04 10:02:49 +00:00
|
|
|
if (currentdir == NULL)
|
|
|
|
free(reserved);
|
|
|
|
else {
|
2015-04-28 19:18:38 +00:00
|
|
|
/* Concatenate the current working directory with the
|
|
|
|
* specified filename, and canonicalize the result. */
|
|
|
|
sprintf(joinednames, "%s/%s", currentdir, openfile->filename);
|
2016-02-29 16:56:54 +00:00
|
|
|
fullname = get_full_path(joinednames);
|
2015-04-28 19:18:38 +00:00
|
|
|
free(currentdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fullname == NULL)
|
|
|
|
fullname = mallocstrcpy(fullname, openfile->filename);
|
|
|
|
|
2016-02-26 17:11:35 +00:00
|
|
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
2016-03-12 08:46:18 +00:00
|
|
|
if (found_in_list(sint->extensions, fullname))
|
2016-03-10 20:06:01 +00:00
|
|
|
break;
|
2011-02-13 04:23:10 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 19:18:38 +00:00
|
|
|
free(joinednames);
|
|
|
|
free(fullname);
|
2016-03-12 09:18:50 +00:00
|
|
|
}
|
2015-04-28 19:18:38 +00:00
|
|
|
|
2016-03-12 09:18:50 +00:00
|
|
|
/* If the filename didn't match anything, try the first line. */
|
|
|
|
if (sint == NULL) {
|
2011-02-13 04:23:10 +00:00
|
|
|
#ifdef DEBUG
|
2016-03-12 09:18:50 +00:00
|
|
|
fprintf(stderr, "No result from file extension, trying headerline...\n");
|
2014-03-17 14:15:57 +00:00
|
|
|
#endif
|
2016-03-12 09:18:50 +00:00
|
|
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
|
|
|
if (found_in_list(sint->headers, openfile->fileage->data))
|
|
|
|
break;
|
2005-07-13 20:18:46 +00:00
|
|
|
}
|
2016-03-12 09:18:50 +00:00
|
|
|
}
|
2008-09-21 23:02:30 +00:00
|
|
|
|
2014-05-14 10:09:42 +00:00
|
|
|
#ifdef HAVE_LIBMAGIC
|
2016-03-12 09:18:50 +00:00
|
|
|
/* If we still don't have an answer, try using magic. */
|
|
|
|
if (sint == NULL) {
|
|
|
|
struct stat fileinfo;
|
|
|
|
magic_t cookie = NULL;
|
|
|
|
const char *magicstring = NULL;
|
2008-09-21 23:02:30 +00:00
|
|
|
#ifdef DEBUG
|
2016-03-12 09:18:50 +00:00
|
|
|
fprintf(stderr, "No result from headerline either, trying libmagic...\n");
|
2008-09-21 23:02:30 +00:00
|
|
|
#endif
|
2016-03-12 09:18:50 +00:00
|
|
|
if (stat(openfile->filename, &fileinfo) == 0) {
|
|
|
|
/* Open the magic database and get a diagnosis of the file. */
|
|
|
|
cookie = magic_open(MAGIC_SYMLINK |
|
2014-05-14 10:29:55 +00:00
|
|
|
#ifdef DEBUG
|
2015-03-27 13:46:50 +00:00
|
|
|
MAGIC_DEBUG | MAGIC_CHECK |
|
2014-05-14 10:29:55 +00:00
|
|
|
#endif
|
2015-03-27 13:46:50 +00:00
|
|
|
MAGIC_ERROR);
|
2016-03-12 09:18:50 +00:00
|
|
|
if (cookie == NULL || magic_load(cookie, NULL) < 0)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("magic_load() failed: %s"), strerror(errno));
|
2016-03-12 09:18:50 +00:00
|
|
|
else {
|
|
|
|
magicstring = magic_file(cookie, openfile->filename);
|
|
|
|
if (magicstring == NULL)
|
2016-04-30 15:31:43 +00:00
|
|
|
statusline(ALERT, _("magic_file(%s) failed: %s"),
|
2016-03-12 09:18:50 +00:00
|
|
|
openfile->filename, magic_error(cookie));
|
2014-05-14 10:29:55 +00:00
|
|
|
#ifdef DEBUG
|
2016-03-12 09:18:50 +00:00
|
|
|
fprintf(stderr, "Returned magic string is: %s\n", magicstring);
|
2014-05-14 10:29:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
2016-03-12 09:18:50 +00:00
|
|
|
}
|
2014-05-14 10:29:55 +00:00
|
|
|
|
2016-03-12 09:18:50 +00:00
|
|
|
/* Now try and find a syntax that matches the magic string. */
|
|
|
|
if (magicstring != NULL) {
|
|
|
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
2016-05-17 09:33:21 +00:00
|
|
|
if (found_in_list(sint->magics, magicstring))
|
2016-03-12 09:18:50 +00:00
|
|
|
break;
|
2008-09-21 23:02:30 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-12 09:18:50 +00:00
|
|
|
|
|
|
|
if (stat(openfile->filename, &fileinfo) == 0)
|
|
|
|
magic_close(cookie);
|
2005-07-13 20:18:46 +00:00
|
|
|
}
|
2016-03-12 09:18:50 +00:00
|
|
|
#endif /* HAVE_LIBMAGIC */
|
2005-07-13 20:18:46 +00:00
|
|
|
|
2016-03-12 09:18:50 +00:00
|
|
|
/* If nothing at all matched, see if there is a default syntax. */
|
2016-03-12 08:46:18 +00:00
|
|
|
if (sint == NULL) {
|
2016-02-26 17:11:35 +00:00
|
|
|
for (sint = syntaxes; sint != NULL; sint = sint->next) {
|
2016-03-12 08:46:18 +00:00
|
|
|
if (strcmp(sint->name, "default") == 0)
|
2016-03-10 20:06:01 +00:00
|
|
|
break;
|
2016-02-26 16:08:21 +00:00
|
|
|
}
|
2009-01-25 07:25:17 +00:00
|
|
|
}
|
2005-08-01 04:23:29 +00:00
|
|
|
|
2016-03-12 08:46:18 +00:00
|
|
|
openfile->syntax = sint;
|
|
|
|
openfile->colorstrings = (sint == NULL ? NULL : sint->color);
|
|
|
|
|
2016-03-12 09:18:50 +00:00
|
|
|
/* If a syntax was found, compile its specified regexes (which have
|
|
|
|
* already been checked for validity when they were read in). */
|
2016-03-14 16:01:44 +00:00
|
|
|
for (ink = openfile->colorstrings; ink != NULL; ink = ink->next) {
|
2016-03-04 20:50:38 +00:00
|
|
|
if (ink->start == NULL) {
|
|
|
|
ink->start = (regex_t *)nmalloc(sizeof(regex_t));
|
2016-03-13 19:37:21 +00:00
|
|
|
regcomp(ink->start, fixbounds(ink->start_regex), ink->rex_flags);
|
2005-07-13 20:18:46 +00:00
|
|
|
}
|
2005-07-14 18:33:51 +00:00
|
|
|
|
2016-03-04 20:50:38 +00:00
|
|
|
if (ink->end_regex != NULL && ink->end == NULL) {
|
|
|
|
ink->end = (regex_t *)nmalloc(sizeof(regex_t));
|
2016-03-13 19:37:21 +00:00
|
|
|
regcomp(ink->end, fixbounds(ink->end_regex), ink->rex_flags);
|
2002-05-04 04:23:30 +00:00
|
|
|
}
|
|
|
|
}
|
2002-05-04 03:47:33 +00:00
|
|
|
}
|
|
|
|
|
2015-12-04 20:54:34 +00:00
|
|
|
/* Reset the multiline coloring cache for one specific regex (given by
|
|
|
|
* index) for lines that need reevaluation. */
|
|
|
|
void reset_multis_for_id(filestruct *fileptr, int index)
|
2009-02-06 03:41:02 +00:00
|
|
|
{
|
2015-12-04 20:54:34 +00:00
|
|
|
filestruct *row;
|
|
|
|
|
|
|
|
/* Reset the cache of earlier lines, as far back as needed. */
|
|
|
|
for (row = fileptr->prev; row != NULL; row = row->prev) {
|
|
|
|
alloc_multidata_if_needed(row);
|
|
|
|
if (row->multidata[index] == CNONE)
|
2009-02-06 03:41:02 +00:00
|
|
|
break;
|
2015-12-04 20:54:34 +00:00
|
|
|
row->multidata[index] = -1;
|
2009-02-06 03:41:02 +00:00
|
|
|
}
|
2015-12-04 20:54:34 +00:00
|
|
|
for (; row != NULL; row = row->prev) {
|
|
|
|
alloc_multidata_if_needed(row);
|
|
|
|
if (row->multidata[index] != CNONE)
|
2009-02-08 18:00:44 +00:00
|
|
|
break;
|
2015-12-04 20:54:34 +00:00
|
|
|
row->multidata[index] = -1;
|
2009-02-08 18:00:44 +00:00
|
|
|
}
|
2009-02-06 03:41:02 +00:00
|
|
|
|
2015-12-04 20:54:34 +00:00
|
|
|
/* Reset the cache of the current line. */
|
|
|
|
fileptr->multidata[index] = -1;
|
|
|
|
|
|
|
|
/* Reset the cache of later lines, as far ahead as needed. */
|
|
|
|
for (row = fileptr->next; row != NULL; row = row->next) {
|
|
|
|
alloc_multidata_if_needed(row);
|
|
|
|
if (row->multidata[index] == CNONE)
|
2009-02-06 03:41:02 +00:00
|
|
|
break;
|
2015-12-04 20:54:34 +00:00
|
|
|
row->multidata[index] = -1;
|
2009-02-06 03:41:02 +00:00
|
|
|
}
|
2015-12-04 20:54:34 +00:00
|
|
|
for (; row != NULL; row = row->next) {
|
|
|
|
alloc_multidata_if_needed(row);
|
|
|
|
if (row->multidata[index] != CNONE)
|
2009-02-08 18:00:44 +00:00
|
|
|
break;
|
2015-12-04 20:54:34 +00:00
|
|
|
row->multidata[index] = -1;
|
2009-02-08 18:00:44 +00:00
|
|
|
}
|
2009-02-06 03:41:02 +00:00
|
|
|
|
2016-04-25 19:14:18 +00:00
|
|
|
refresh_needed = TRUE;
|
2009-02-16 21:04:00 +00:00
|
|
|
}
|
2009-02-06 03:41:02 +00:00
|
|
|
|
2015-11-29 10:43:10 +00:00
|
|
|
/* Reset multi-line strings around the filestruct fileptr, trying to be
|
|
|
|
* smart about stopping. Bool force means: reset everything regardless,
|
|
|
|
* useful when we don't know how much screen state has changed. */
|
2009-02-16 21:04:00 +00:00
|
|
|
void reset_multis(filestruct *fileptr, bool force)
|
2009-02-06 03:41:02 +00:00
|
|
|
{
|
2016-07-04 11:43:19 +00:00
|
|
|
const colortype *ink;
|
2016-07-22 13:48:06 +00:00
|
|
|
int nobegin = 0, noend = 0;
|
2009-02-06 03:41:02 +00:00
|
|
|
regmatch_t startmatch, endmatch;
|
2009-01-25 07:25:17 +00:00
|
|
|
|
2016-01-03 16:57:17 +00:00
|
|
|
/* If there is no syntax or no multiline regex, there is nothing to do. */
|
|
|
|
if (openfile->syntax == NULL || openfile->syntax->nmultis == 0)
|
2009-01-30 08:34:27 +00:00
|
|
|
return;
|
|
|
|
|
2016-07-04 11:43:19 +00:00
|
|
|
for (ink = openfile->colorstrings; ink != NULL; ink = ink->next) {
|
2014-04-04 09:01:21 +00:00
|
|
|
/* If it's not a multi-line regex, amscray. */
|
2016-07-04 11:43:19 +00:00
|
|
|
if (ink->end == NULL)
|
2009-02-06 03:41:02 +00:00
|
|
|
continue;
|
|
|
|
|
2009-02-07 14:48:30 +00:00
|
|
|
alloc_multidata_if_needed(fileptr);
|
2009-02-16 21:04:00 +00:00
|
|
|
|
2015-11-29 10:43:10 +00:00
|
|
|
if (force == FALSE) {
|
|
|
|
/* Check whether the multidata still matches the current situation. */
|
2016-07-04 11:43:19 +00:00
|
|
|
nobegin = regexec(ink->start, fileptr->data, 1, &startmatch, 0);
|
|
|
|
noend = regexec(ink->end, fileptr->data, 1, &endmatch, 0);
|
|
|
|
if ((fileptr->multidata[ink->id] == CWHOLELINE ||
|
|
|
|
fileptr->multidata[ink->id] == CNONE) &&
|
2015-11-29 10:43:10 +00:00
|
|
|
nobegin && noend)
|
2009-02-16 23:06:09 +00:00
|
|
|
continue;
|
2016-07-04 11:43:19 +00:00
|
|
|
else if (fileptr->multidata[ink->id] == CSTARTENDHERE &&
|
2015-11-29 10:14:25 +00:00
|
|
|
!nobegin && !noend && startmatch.rm_so < endmatch.rm_so)
|
2015-11-29 10:43:10 +00:00
|
|
|
continue;
|
2016-07-04 11:43:19 +00:00
|
|
|
else if (fileptr->multidata[ink->id] == CBEGINBEFORE &&
|
2015-11-29 10:43:10 +00:00
|
|
|
nobegin && !noend)
|
|
|
|
continue;
|
2016-07-04 11:43:19 +00:00
|
|
|
else if (fileptr->multidata[ink->id] == CENDAFTER &&
|
2015-11-29 10:14:25 +00:00
|
|
|
!nobegin && noend)
|
2015-11-29 10:43:10 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-02-06 03:41:02 +00:00
|
|
|
|
2015-11-29 10:43:10 +00:00
|
|
|
/* If we got here, things have changed. */
|
2016-07-04 11:43:19 +00:00
|
|
|
reset_multis_for_id(fileptr, ink->id);
|
2016-07-03 14:21:03 +00:00
|
|
|
|
|
|
|
/* If start and end are the same, push the resets further. */
|
|
|
|
if (force == FALSE && !nobegin && !noend &&
|
|
|
|
startmatch.rm_so == endmatch.rm_so)
|
2016-07-04 11:43:19 +00:00
|
|
|
reset_multis_for_id(fileptr, ink->id);
|
2009-01-25 07:25:17 +00:00
|
|
|
}
|
|
|
|
}
|
2014-04-04 11:59:03 +00:00
|
|
|
|
2015-12-22 16:51:00 +00:00
|
|
|
/* Allocate (for one line) the cache space for multiline color regexes. */
|
|
|
|
void alloc_multidata_if_needed(filestruct *fileptr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (fileptr->multidata == NULL) {
|
|
|
|
fileptr->multidata = (short *)nmalloc(openfile->syntax->nmultis * sizeof(short));
|
|
|
|
|
|
|
|
for (i = 0; i < openfile->syntax->nmultis; i++)
|
|
|
|
fileptr->multidata[i] = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Precalculate the multi-line start and end regex info so we can
|
|
|
|
* speed up rendering (with any hope at all...). */
|
|
|
|
void precalc_multicolorinfo(void)
|
|
|
|
{
|
2016-07-04 11:43:19 +00:00
|
|
|
const colortype *ink;
|
2015-12-22 16:51:00 +00:00
|
|
|
regmatch_t startmatch, endmatch;
|
|
|
|
filestruct *fileptr, *endptr;
|
|
|
|
|
|
|
|
if (openfile->colorstrings == NULL || ISSET(NO_COLOR_SYNTAX))
|
|
|
|
return;
|
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "Entering precalculation of multiline color info\n");
|
|
|
|
#endif
|
|
|
|
|
2016-07-04 11:43:19 +00:00
|
|
|
for (ink = openfile->colorstrings; ink != NULL; ink = ink->next) {
|
2015-12-22 16:51:00 +00:00
|
|
|
/* If this is not a multi-line regex, skip it. */
|
2016-07-04 11:43:19 +00:00
|
|
|
if (ink->end == NULL)
|
2015-12-22 16:51:00 +00:00
|
|
|
continue;
|
|
|
|
#ifdef DEBUG
|
2016-07-04 11:43:19 +00:00
|
|
|
fprintf(stderr, "Starting work on color id %d\n", ink->id);
|
2015-12-22 16:51:00 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
for (fileptr = openfile->fileage; fileptr != NULL; fileptr = fileptr->next) {
|
|
|
|
int startx = 0, nostart = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "working on lineno %ld... ", (long)fileptr->lineno);
|
|
|
|
#endif
|
|
|
|
alloc_multidata_if_needed(fileptr);
|
|
|
|
|
2016-07-04 11:43:19 +00:00
|
|
|
while ((nostart = regexec(ink->start, &fileptr->data[startx], 1,
|
|
|
|
&startmatch, (startx == 0) ? 0 : REG_NOTBOL)) == 0) {
|
2015-12-22 16:51:00 +00:00
|
|
|
/* Look for an end, and start marking how many lines are
|
|
|
|
* encompassed, which should speed up rendering later. */
|
|
|
|
startx += startmatch.rm_eo;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "start found at pos %lu... ", (unsigned long)startx);
|
|
|
|
#endif
|
|
|
|
/* Look first on this line for an end. */
|
2016-07-04 11:43:19 +00:00
|
|
|
if (regexec(ink->end, &fileptr->data[startx], 1,
|
2015-12-22 16:51:00 +00:00
|
|
|
&endmatch, (startx == 0) ? 0 : REG_NOTBOL) == 0) {
|
|
|
|
startx += endmatch.rm_eo;
|
2016-01-09 18:41:56 +00:00
|
|
|
/* Step ahead when both start and end are mere anchors. */
|
|
|
|
if (startmatch.rm_so == startmatch.rm_eo &&
|
|
|
|
endmatch.rm_so == endmatch.rm_eo)
|
|
|
|
startx += 1;
|
2016-07-04 11:43:19 +00:00
|
|
|
fileptr->multidata[ink->id] = CSTARTENDHERE;
|
2015-12-22 16:51:00 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "end found on this line\n");
|
|
|
|
#endif
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Nice, we didn't find the end regex on this line. Let's start looking for it. */
|
|
|
|
for (endptr = fileptr->next; endptr != NULL; endptr = endptr->next) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "\nadvancing to line %ld to find end... ", (long)endptr->lineno);
|
|
|
|
#endif
|
2016-07-04 11:43:19 +00:00
|
|
|
if (regexec(ink->end, endptr->data, 1, &endmatch, 0) == 0)
|
2015-12-22 16:51:00 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endptr == NULL) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "no end found, breaking out\n");
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "end found\n");
|
|
|
|
#endif
|
|
|
|
/* We found it, we found it, la la la la la. Mark all
|
|
|
|
* the lines in between and the end properly. */
|
2016-07-04 11:43:19 +00:00
|
|
|
fileptr->multidata[ink->id] = CENDAFTER;
|
2015-12-22 16:51:00 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "marking line %ld as CENDAFTER\n", (long)fileptr->lineno);
|
|
|
|
#endif
|
|
|
|
for (fileptr = fileptr->next; fileptr != endptr; fileptr = fileptr->next) {
|
|
|
|
alloc_multidata_if_needed(fileptr);
|
2016-07-04 11:43:19 +00:00
|
|
|
fileptr->multidata[ink->id] = CWHOLELINE;
|
2015-12-22 16:51:00 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "marking intermediary line %ld as CWHOLELINE\n", (long)fileptr->lineno);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
alloc_multidata_if_needed(endptr);
|
2016-07-04 11:43:19 +00:00
|
|
|
fileptr->multidata[ink->id] = CBEGINBEFORE;
|
2015-12-22 16:51:00 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "marking line %ld as CBEGINBEFORE\n", (long)fileptr->lineno);
|
|
|
|
#endif
|
|
|
|
/* Skip to the end point of the match. */
|
|
|
|
startx = endmatch.rm_eo;
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "jumping to line %ld pos %lu to continue\n", (long)fileptr->lineno, (unsigned long)startx);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nostart && startx == 0) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "no match\n");
|
|
|
|
#endif
|
2016-07-04 11:43:19 +00:00
|
|
|
fileptr->multidata[ink->id] = CNONE;
|
2015-12-22 16:51:00 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-04 11:59:03 +00:00
|
|
|
#endif /* !DISABLE_COLOR */
|