From: Chris Allegretta Date: Sat, 4 May 2002 03:47:33 +0000 (+0000) Subject: - Added syntax command to .nanorc file, to allow multiple syntaxes. New function... X-Git-Tag: v1.1.9~14 X-Git-Url: https://git.wh0rd.org/?a=commitdiff_plain;h=b6c5dc29efde88f9d79b61d709595bfbb4ac09ae;p=nano.git - Added syntax command to .nanorc file, to allow multiple syntaxes. New function color.c:update_color(), calls in various files.c places, syntaxtype struct, global variables syntaxes, syntaxfile_regexp and synfilematches git-svn-id: svn://svn.savannah.gnu.org/nano/trunk/nano@1197 35c25a1d-7b9e-4130-9fde-d3aeb78583b8 --- diff --git a/ChangeLog b/ChangeLog index 8de8c0f4..18c307c3 100644 --- a/ChangeLog +++ b/ChangeLog @@ -18,6 +18,10 @@ CVS code - - More cleanups with DISABLE flags, better free_shortcutage and free_toggle, and get rid of unnecessary variable decls with NANO_SMALL in shortcut_init() by David Benbennick. + - Added "syntax" command to .nanorc file, to allow multiple + syntaxes. New function color.c:update_color(), calls in various + files.c places, syntaxtype struct, global variables syntaxes, + syntaxfile_regexp and synfilematches. - configure.ac: - Define NDEBUG to silence asserts (David Benbennick). - files.c: diff --git a/color.c b/color.c index e9f6a9b6..7135a395 100644 --- a/color.c +++ b/color.c @@ -177,5 +177,25 @@ int do_colorinit(void) return 0; } +/* Update the color information based on the current filename */ +void update_color(void) +{ + syntaxtype *tmpsyntax; + + colorstrings = NULL; + for (tmpsyntax = syntaxes; tmpsyntax != NULL; tmpsyntax = tmpsyntax->next) { + exttype *e; + for (e = tmpsyntax->extensions; e != NULL; e = e->next) { + regcomp(&syntaxfile_regexp, e->val, 0); + + /* Set colorstrings if we matched the extension regex */ + if (!regexec(&syntaxfile_regexp, filename, 1, synfilematches, 0)) + colorstrings = tmpsyntax->color; + } + } + do_colorinit(); + edit_refresh(); +} + #endif /* ENABLE_COLOR */ diff --git a/files.c b/files.c index ecf53680..9f864c87 100644 --- a/files.c +++ b/files.c @@ -61,6 +61,10 @@ void load_file(int quiet) add_open_file(quiet); #endif +#ifdef ENABLE_COLOR + update_color(); +#endif + wmove(edit, current_y, current_x); } @@ -487,6 +491,10 @@ int do_insertfile(int loading_file) else edit_refresh(); +#ifdef ENABLE_COLOR + update_color(); +#endif + UNSET(KEEP_CUTBUFFER); display_main_list(); return i; @@ -612,6 +620,7 @@ int open_file_change_name(void) */ int load_open_file(void) { + if (!open_files) return 1; @@ -650,6 +659,10 @@ int load_open_file(void) if (ISSET(CONSTUPDATE)) do_cursorpos(0); +#ifdef ENABLE_COLOR + update_color(); +#endif + /* now we're done */ return 0; } diff --git a/global.c b/global.c index 09080adf..501af2e5 100644 --- a/global.c +++ b/global.c @@ -110,6 +110,7 @@ shortcut *browser_list = NULL; #ifdef ENABLE_COLOR colorstruct colors[NUM_NCOLORS]; colortype *colorstrings = NULL; + syntaxtype *syntaxes = NULL; #endif #if !defined(DISABLE_BROWSER) || !defined(DISABLE_MOUSE) || !defined (DISABLE_HELP) @@ -129,6 +130,9 @@ regmatch_t regmatches[10]; /* Match positions for parenthetical #ifdef ENABLE_COLOR regex_t color_regexp; /* Global to store compiled search regexp */ regmatch_t colormatches[1]; /* Match positions for parenthetical */ + +regex_t syntaxfile_regexp; /* Global to store compiled search regexp */ +regmatch_t synfilematches[1]; /* Match positions for parenthetical */ #endif /* ENABLE_COLOR */ #endif diff --git a/nano.h b/nano.h index c11ba051..799729c9 100644 --- a/nano.h +++ b/nano.h @@ -137,6 +137,18 @@ typedef struct colortype { struct colortype *next; } colortype; +typedef struct exttype { + char *val; + struct exttype *next; +} exttype; + +typedef struct syntaxtype { + char *desc; /* Name of this syntax type */ + struct exttype *extensions; /* List of extensions that this applies to */ + colortype *color; /* color struct for this syntax */ + struct syntaxtype *next; +} syntaxtype; + #endif /* ENABLE_COLOR */ diff --git a/nanorc.sample b/nanorc.sample index 4b1681e9..be611711 100644 --- a/nanorc.sample +++ b/nanorc.sample @@ -73,7 +73,9 @@ # # Color setup -# Format: color foreground,background "regex" ["regex"...] +# Format: +# syntax "short description" ["filename regex" ...] +# color foreground,background "regex" ["regex"...] # # Legal colors are: white, black, red, blue, green, yellow, purple, cyan # You may use the prefix "bright" to mean a stronger color highlight @@ -84,6 +86,7 @@ # color will use a transparent color. If you don't want this, be sure # to set the background color to black or white. # +#syntax "c-file" ".*\.c" ".*\.h" #color brightred "float " "char " "int " "void " "NULL" "[A-Z_]\{2,\}" #color brightred "static" "const" "[\ ]struct" "^struct" "if " "while[\ \n\(]" #color brightred "do[\ \n\(]" "else[\ \n]" "case " "switch " "break;" diff --git a/proto.h b/proto.h index 6f97abf5..ffdbec3a 100644 --- a/proto.h +++ b/proto.h @@ -66,6 +66,7 @@ extern openfilestruct *open_files; #ifdef ENABLE_COLOR extern colortype *colorstrings; +extern syntaxtype *syntaxes; #endif extern shortcut *shortcut_list; @@ -90,6 +91,9 @@ extern regmatch_t regmatches[10]; #ifdef ENABLE_COLOR extern regex_t color_regexp; extern regmatch_t colormatches[1]; + +extern regex_t syntaxfile_regexp; +extern regmatch_t synfilematches[1]; #endif /* ENABLE_COLOR */ #endif @@ -279,6 +283,7 @@ char *do_browse_from(char *inpath); int do_colorinit(void); void color_on(WINDOW *win, int whatever); void color_off(WINDOW *win, int whatever); +void update_color(void); extern colorstruct colors[NUM_NCOLORS]; #endif /* ENABLE_COLOR */ diff --git a/rcfile.c b/rcfile.c index 8531b7ce..9a0f066a 100644 --- a/rcfile.c +++ b/rcfile.c @@ -183,6 +183,93 @@ int colortoint(char *colorname, int *bright) #ifdef ENABLE_COLOR +void parse_syntax(FILE * rcstream, char *buf, char *ptr) +{ + syntaxtype *tmpsyntax = NULL; + char *fileregptr = NULL, *nameptr = NULL; + exttype *exttmp = NULL; + + while (*ptr == ' ') + ptr++; + + if (*ptr == '\n' || *ptr == '\0') + return; + + if (*ptr != '"') { + rcfile_error(_("regex strings must begin and end with a \" character\n")); + exit(1); + } + ptr++; + + nameptr = ptr; + ptr = parse_next_regex(ptr); + + if (ptr == NULL) { + rcfile_error(_("Missing syntax name")); + exit(1); + } + + if (syntaxes == NULL) { + syntaxes = nmalloc(sizeof(syntaxtype)); + syntaxes->desc = NULL; + syntaxes->desc = mallocstrcpy(syntaxes->desc, nameptr); + syntaxes->color = NULL; + syntaxes->extensions = NULL; + syntaxes->next = NULL; + tmpsyntax = syntaxes; +#ifdef DEBUG + fprintf(stderr, + "Starting a new syntax type\n"); + fprintf(stderr, "string val=%s\n", tmp); +#endif + + } else { + for (tmpsyntax = syntaxes; + tmpsyntax->next != NULL; tmpsyntax = tmpsyntax->next); +#ifdef DEBUG + fprintf(stderr, "Adding new syntax after 1st\n"); +#endif + + tmpsyntax->next = nmalloc(sizeof(syntaxtype)); + tmpsyntax->next->desc = NULL; + tmpsyntax->next->desc = mallocstrcpy(tmpsyntax->next->desc, nameptr); + tmpsyntax->next->color = NULL; + tmpsyntax->next->extensions = NULL; + tmpsyntax->next->next = NULL; + tmpsyntax = tmpsyntax->next; + } + + /* Now load in the extensions to their part of the struct */ + while (*ptr != '\n' && *ptr != '\0') { + + while (*ptr != '"' && *ptr != '\n' && *ptr != '\0') + ptr++; + + if (*ptr == '\n' || *ptr == '\0') + return; + ptr++; + + fileregptr = ptr; + ptr = parse_next_regex(ptr); + + if (tmpsyntax->extensions == NULL) { + tmpsyntax->extensions = nmalloc(sizeof(exttype)); + tmpsyntax->extensions->val = NULL; + tmpsyntax->extensions->val = mallocstrcpy(tmpsyntax->extensions->val, fileregptr); + tmpsyntax->extensions->next = NULL; + } + else { + for (exttmp = tmpsyntax->extensions; exttmp->next != NULL; + exttmp = exttmp->next); + exttmp->next = nmalloc(sizeof(exttype)); + exttmp->next->val = NULL; + exttmp->next->val = mallocstrcpy(exttmp->next->val, fileregptr); + exttmp->next->next = NULL; + } + } + +} + /* Parse the color stuff into the colorstrings array */ void parse_colors(FILE * rcstream, char *buf, char *ptr) { @@ -190,6 +277,7 @@ void parse_colors(FILE * rcstream, char *buf, char *ptr) int expectend = 0; /* Do we expect an end= line? */ char *tmp = NULL, *beginning, *fgstr, *bgstr; colortype *tmpcolor = NULL; + syntaxtype *tmpsyntax = NULL; fgstr = ptr; ptr = parse_next_word(ptr); @@ -208,6 +296,15 @@ void parse_colors(FILE * rcstream, char *buf, char *ptr) fg = colortoint(fgstr, &bright); bg = colortoint(bgstr, &bright); + if (syntaxes == NULL) { + rcfile_error(_("Cannot add a color directive without a syntax line")); + exit(1); + } + + for (tmpsyntax = syntaxes; tmpsyntax->next != NULL; + tmpsyntax = tmpsyntax->next) + ; + /* Now the fun part, start adding regexps to individual strings in the colorstrings array, woo! */ @@ -236,14 +333,14 @@ void parse_colors(FILE * rcstream, char *buf, char *ptr) tmp = NULL; tmp = mallocstrcpy(tmp, beginning); - if (colorstrings == NULL) { - colorstrings = nmalloc(sizeof(colortype)); - colorstrings->fg = fg; - colorstrings->bg = bg; - colorstrings->bright = bright; - colorstrings->start = tmp; - colorstrings->next = NULL; - tmpcolor = colorstrings; + if (tmpsyntax->color == NULL) { + tmpsyntax->color = nmalloc(sizeof(colortype)); + tmpsyntax->color->fg = fg; + tmpsyntax->color->bg = bg; + tmpsyntax->color->bright = bright; + tmpsyntax->color->start = tmp; + tmpsyntax->color->next = NULL; + tmpcolor = tmpsyntax->color; #ifdef DEBUG fprintf(stderr, "Starting a new colorstring for fg %d bg %d\n", @@ -252,7 +349,7 @@ void parse_colors(FILE * rcstream, char *buf, char *ptr) #endif } else { - for (tmpcolor = colorstrings; + for (tmpcolor = tmpsyntax->color; tmpcolor->next != NULL; tmpcolor = tmpcolor->next); #ifdef DEBUG fprintf(stderr, "Adding new entry for fg %d bg %d\n", fg, bg); @@ -338,6 +435,8 @@ void parse_rcfile(FILE * rcstream) else if (!strcasecmp(keyword, "unset")) set = -1; #ifdef ENABLE_COLOR + else if (!strcasecmp(keyword, "syntax")) + parse_syntax(rcstream, buf, ptr); else if (!strcasecmp(keyword, "color")) parse_colors(rcstream, buf, ptr); #endif /* ENABLE_COLOR */ @@ -481,9 +580,25 @@ void do_rcfile(void) return; } + parse_rcfile(rcstream); fclose(rcstream); + { + syntaxtype *s; + exttype *e; + colortype *c; + + for (s = syntaxes; s != NULL; s = s->next) { + fprintf(stderr, "Syntax \"%s\"\n", s->desc); + for (e = s->extensions; e != NULL; e = e->next) + fprintf(stderr, " extension \"%s\"\n", e->val); + for (c = s->color; c != NULL; c = c->next) + fprintf(stderr, "Color string regex \"%s\"\n", c->start); + + } + } + }