Skip to content

Commit

Permalink
vis: remove regex based syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
martanne committed Nov 8, 2015
1 parent 2d4408f commit b1ec600
Show file tree
Hide file tree
Showing 10 changed files with 17 additions and 792 deletions.
622 changes: 0 additions & 622 deletions config.def.h

Large diffs are not rendered by default.

3 changes: 0 additions & 3 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -1584,9 +1584,6 @@ int main(int argc, char *argv[]) {
vis_die(vis, "Could not load default bindings\n");
}

if (!vis_syntax_load(vis, syntaxes))
vis_die(vis, "Could not load syntax highlighting definitions\n");

/* install signal handlers etc. */
struct sigaction sa;
memset(&sa, 0, sizeof sa);
Expand Down
20 changes: 0 additions & 20 deletions syntax.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,6 @@
#ifndef SYNTAX_H
#define SYNTAX_H

#include <regex.h>

typedef struct {
char *rule; /* regex to search for */
int style; /* settings to apply in case of a match */
bool multiline; /* whether . should match new lines */
regex_t regex; /* compiled form of the above rule */
} SyntaxRule;

typedef struct {
char *symbol;
int style;
Expand All @@ -24,15 +15,4 @@ enum {
SYNTAX_SYMBOL_LAST,
};

typedef struct Syntax Syntax;
struct Syntax { /* a syntax definition */
char *name; /* syntax name */
char *file; /* apply to files matching this regex */
regex_t file_regex; /* compiled file name regex */
const char **settings;/* settings associated with this file type */
const char **styles; /* settings associated with this file type */
SyntaxSymbol symbols[SYNTAX_SYMBOL_LAST]; /* symbols for white space handling */
SyntaxRule rules[24]; /* all rules for this file type */
};

#endif
83 changes: 7 additions & 76 deletions view.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ struct View {
Cursor *cursor; /* main cursor, always placed within the visible viewport */
Line *line; /* used while drawing view content, line where next char will be drawn */
int col; /* used while drawing view content, column where next char will be drawn */
Syntax *syntax; /* syntax highlighting definitions for this view or NULL */
const SyntaxSymbol *symbols[SYNTAX_SYMBOL_LAST]; /* symbols to use for white spaces etc */
int tabwidth; /* how many spaces should be used to display a tab character */
Cursor *cursors; /* all cursors currently active */
Expand Down Expand Up @@ -346,11 +345,6 @@ void view_draw(View *view) {
text[rem] = '\0';
/* current position into buffer from which to interpret a character */
char *cur = text;
/* syntax definition to use */
Syntax *syntax = view->syntax;
/* matched tokens for each syntax rule */
regmatch_t match[syntax ? LENGTH(syntax->rules) : 1][1], *matched = NULL;
memset(match, 0, sizeof match);
/* default and current curses attributes to use */
int default_attrs = 0, attrs = default_attrs;
/* start from known multibyte state */
Expand All @@ -362,52 +356,6 @@ void view_draw(View *view) {
wchar_t wchar;
Cell cell;
memset(&cell, 0, sizeof cell);

if (syntax) {
if (matched && cur >= text + matched->rm_eo) {
/* end of current match */
matched = NULL;
attrs = default_attrs;
for (int i = 0; i < LENGTH(syntax->rules); i++) {
if (match[i][0].rm_so == -1)
continue; /* no match on whole text */
/* reset matches which overlap with matched */
if (text + match[i][0].rm_so <= cur && cur < text + match[i][0].rm_eo) {
match[i][0].rm_so = 0;
match[i][0].rm_eo = 0;
}
}
}

if (!matched) {
size_t off = cur - text; /* number of already processed bytes */
for (int i = 0; i < LENGTH(syntax->rules); i++) {
SyntaxRule *rule = &syntax->rules[i];
if (!rule->rule)
break;
if (match[i][0].rm_so == -1)
continue; /* no match on whole text */
if (off >= (size_t)match[i][0].rm_eo) {
/* past match, continue search from current position */
if (regexec(&rule->regex, cur, 1, match[i], 0) ||
match[i][0].rm_so == match[i][0].rm_eo) {
match[i][0].rm_so = -1;
match[i][0].rm_eo = -1;
continue;
}
match[i][0].rm_so += off;
match[i][0].rm_eo += off;
}

if (text + match[i][0].rm_so <= cur && cur < text + match[i][0].rm_eo) {
/* within matched expression */
matched = &match[i][0];
attrs = rule->style;
break; /* first match views */
}
}
}
}

size_t len = mbrtowc(&wchar, cur, rem, &mbstate);
if (len == (size_t)-1 && errno == EILSEQ) {
Expand Down Expand Up @@ -504,7 +452,7 @@ void view_draw(View *view) {
size_t pos = view_cursors_pos(c);
if (view_coord_get(view, pos, &c->line, &c->row, &c->col)) {
c->line->cells[c->col].cursor = true;
if (view->ui && view->syntax) {
if (view->ui) {
Line *line_match; int col_match;
size_t pos_match = text_bracket_match_except(view->text, pos, "<>");
if (pos != pos_match && view_coord_get(view, pos_match, &line_match, NULL, &col_match)) {
Expand Down Expand Up @@ -834,23 +782,12 @@ void view_scroll_to(View *view, size_t pos) {
view_cursors_scroll_to(view->cursor, pos);
}

void view_syntax_set(View *view, Syntax *syntax) {
view->syntax = syntax;
for (int i = 0; i < LENGTH(view->symbols); i++) {
if (syntax && syntax->symbols[i].symbol)
view->symbols[i] = &syntax->symbols[i];
else
view->symbols[i] = &symbols_none[i];
}
if (syntax) {
for (const char **style = syntax->styles; *style; style++) {
view->ui->syntax_style(view->ui, style - syntax->styles, *style);
}
}
bool view_syntax_set(View *view, const char *name) {
return false;
}

Syntax *view_syntax_get(View *view) {
return view->syntax;
const char *view_syntax_get(View *view) {
return NULL;
}

void view_options_set(View *view, enum UiOption options) {
Expand All @@ -862,14 +799,8 @@ void view_options_set(View *view, enum UiOption options) {
[SYNTAX_SYMBOL_EOF] = UI_OPTION_SYMBOL_EOF,
};
for (int i = 0; i < LENGTH(mapping); i++) {
if (options & mapping[i]) {
if (view->syntax && view->syntax->symbols[i].symbol)
view->symbols[i] = &view->syntax->symbols[i];
else
view->symbols[i] = &symbols_default[i];
} else {
view->symbols[i] = &symbols_none[i];
}
view->symbols[i] = (options & mapping[i]) ? &symbols_default[i] :
&symbols_none[i];
}
if (view->ui)
view->ui->options_set(view->ui, options);
Expand Down
4 changes: 2 additions & 2 deletions view.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ Filerange view_viewport_get(View*);
bool view_viewport_up(View *view, int n);
bool view_viewport_down(View *view, int n);
/* associate a set of syntax highlighting rules to this window. */
void view_syntax_set(View*, Syntax*);
Syntax *view_syntax_get(View*);
bool view_syntax_set(View*, const char *name);
const char *view_syntax_get(View*);

void view_options_set(View*, enum UiOption options);
enum UiOption view_options_get(View*);
Expand Down
17 changes: 6 additions & 11 deletions vis-cmds.c
Original file line number Diff line number Diff line change
Expand Up @@ -246,25 +246,20 @@ static bool cmd_set(Vis *vis, Filerange *range, enum CmdOpt cmdopt, const char *
break;
case OPTION_SYNTAX:
if (!argv[2]) {
Syntax *syntax = view_syntax_get(vis->win->view);
const char *syntax = view_syntax_get(vis->win->view);
if (syntax)
vis_info_show(vis, "Syntax definition in use: `%s'", syntax->name);
vis_info_show(vis, "Syntax definition in use: `%s'", syntax);
else
vis_info_show(vis, "No syntax definition in use");
return true;
}

for (Syntax *syntax = vis->syntaxes; syntax && syntax->name; syntax++) {
if (!strcasecmp(syntax->name, argv[2])) {
view_syntax_set(vis->win->view, syntax);
return true;
}
}

if (parse_bool(argv[2], &arg.b) && !arg.b)
view_syntax_set(vis->win->view, NULL);
else
return view_syntax_set(vis->win->view, NULL);
if (!view_syntax_set(vis->win->view, argv[2])) {
vis_info_show(vis, "Unknown syntax definition: `%s'", argv[2]);
return false;
}
break;
case OPTION_SHOW:
if (!argv[2]) {
Expand Down
1 change: 0 additions & 1 deletion vis-core.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ struct Vis {
File *files; /* all files currently managed by this editor instance */
Win *windows; /* all windows currently managed by this editor instance */
Win *win; /* currently active/focused window */
Syntax *syntaxes; /* NULL terminated array of syntax definitions */
Register registers[VIS_REG_INVALID]; /* registers used for yank and put */
Macro macros[VIS_MACRO_INVALID]; /* recorded macros */
Macro *recording, *last_recording; /* currently (if non NULL) and least recently recorded macro */
Expand Down
1 change: 1 addition & 0 deletions vis-motions.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <regex.h>
#include "vis-core.h"
#include "text-motions.h"
#include "text-objects.h"
Expand Down
50 changes: 1 addition & 49 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <limits.h>
#include <ctype.h>
#include <time.h>
#include <regex.h>
#include <sys/select.h>
#include <sys/types.h>
#include <sys/wait.h>
Expand Down Expand Up @@ -124,18 +125,6 @@ void vis_window_name(Win *win, const char *filename) {
free((char*)file->name);
file->name = filename ? strdup(filename) : NULL;
}

if (filename) {
Vis *vis = win->vis;
for (Syntax *syn = vis->syntaxes; syn && syn->name; syn++) {
if (!regexec(&syn->file_regex, filename, 0, NULL, 0)) {
view_syntax_set(win->view, syn);
for (const char **opt = syn->settings; opt && *opt; opt++)
vis_cmd(vis, *opt);
break;
}
}
}
}

static void windows_invalidate(Vis *vis, size_t start, size_t end) {
Expand Down Expand Up @@ -245,42 +234,6 @@ void vis_window_prev(Vis *vis) {
vis->ui->window_focus(vis->win->ui);
}

bool vis_syntax_load(Vis *vis, Syntax *syntaxes) {
bool success = true;
vis->syntaxes = syntaxes;

for (Syntax *syn = syntaxes; syn && syn->name; syn++) {
if (regcomp(&syn->file_regex, syn->file, REG_EXTENDED|REG_NOSUB|REG_ICASE|REG_NEWLINE))
success = false;
for (int j = 0; j < LENGTH(syn->rules); j++) {
SyntaxRule *rule = &syn->rules[j];
if (!rule->rule)
break;
int cflags = REG_EXTENDED;
if (!rule->multiline)
cflags |= REG_NEWLINE;
if (regcomp(&rule->regex, rule->rule, cflags))
success = false;
}
}

return success;
}

void vis_syntax_unload(Vis *vis) {
for (Syntax *syn = vis->syntaxes; syn && syn->name; syn++) {
regfree(&syn->file_regex);
for (int j = 0; j < LENGTH(syn->rules); j++) {
SyntaxRule *rule = &syn->rules[j];
if (!rule->rule)
break;
regfree(&rule->regex);
}
}

vis->syntaxes = NULL;
}

void vis_draw(Vis *vis) {
vis->ui->draw(vis->ui);
}
Expand Down Expand Up @@ -374,7 +327,6 @@ void vis_free(Vis *vis) {
register_release(&vis->registers[i]);
for (int i = 0; i < LENGTH(vis->macros); i++)
macro_release(&vis->macros[i]);
vis_syntax_unload(vis);
vis->ui->free(vis->ui);
map_free(vis->cmds);
map_free(vis->options);
Expand Down
8 changes: 0 additions & 8 deletions vis.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,6 @@ void vis_update(Vis*);
/* temporarily supsend the editor process, resumes upon receiving SIGCONT */
void vis_suspend(Vis*);

/* load a set of syntax highlighting definitions which will be associated
* to the underlying window based on the file type loaded.
*
* The parameter `syntaxes' has to point to a NULL terminated array.
*/
bool vis_syntax_load(Vis*, Syntax *syntaxes);
void vis_syntax_unload(Vis*);

/* creates a new window, and loads the given file. if filename is NULL
* an unamed / empty buffer is created. If the given file is already opened
* in another window, share the underlying text that is changes will be
Expand Down

0 comments on commit b1ec600

Please sign in to comment.