Skip to content

Commit

Permalink
Upgrade scripting engine to work with Lua 5.{1,2,3}
Browse files Browse the repository at this point in the history
  • Loading branch information
hishamhm committed Feb 19, 2015
1 parent e9e5ed5 commit dddbd05
Show file tree
Hide file tree
Showing 12 changed files with 179 additions and 134 deletions.
20 changes: 7 additions & 13 deletions Buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,9 @@
#include <unistd.h>
#include <sys/time.h>

#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>

#include "Prototypes.h"
//#needs Line
//#needs Script

/*{
Expand All @@ -26,9 +23,6 @@
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
#include <lua.h>
#include <lauxlib.h>
#ifndef iswword
#define iswword(x) (iswalpha(x) || x == '_')
#endif
Expand Down Expand Up @@ -92,7 +86,7 @@ struct Buffer_ {
// width of Tab keys (\t)
int tabWidth;
// Lua state
lua_State* L;
ScriptState script;
bool skipOnChange;
bool skipOnKey;
bool skipOnCtrl;
Expand Down Expand Up @@ -173,7 +167,7 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command, Tab
this->dosLineBreaks = false;
this->tabWidth = 8;

this->L = Script_newState(tabs, this);
Script_initState(&this->script, tabs, this);

/* Hack to disable auto-indent when pasting through X11, part 1 */
struct timeval tv;
Expand All @@ -195,7 +189,7 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command, Tab
if (file && !FileReader_eof(file)) {
newFile = false;
Text text = Text_new(FileReader_readLine(file));
this->hl = Highlight_new(fileName, text, this->L);
this->hl = Highlight_new(fileName, text, &this->script);
if (Text_toString(text)[0] == '\t') indents[0] = 1;
if (Text_hasChar(text, '\015')) this->dosLineBreaks = true;
Line* line = Line_new(p->items, text, this->hl->mainContext);
Expand Down Expand Up @@ -229,7 +223,7 @@ Buffer* Buffer_new(int x, int y, int w, int h, char* fileName, bool command, Tab
this->readOnly = false;
}
if (newFile) {
this->hl = Highlight_new(fileName, Text_new(""), this->L);
this->hl = Highlight_new(fileName, Text_new(""), &this->script);
Panel_set(p, 0, (ListItem*) Line_new(p->items, Text_new(strdup("")), this->hl->mainContext));
}

Expand Down Expand Up @@ -460,7 +454,7 @@ void Buffer_delete(Buffer* this) {

Undo_delete(this->undo);
Highlight_delete(this->hl);
lua_close(this->L);
Script_doneState(&this->script);
free(this);
}

Expand All @@ -472,7 +466,7 @@ void Buffer_refreshHighlight(Buffer* this) {
firstText = firstLine->text;
else
firstText = Text_new("");
Highlight* hl = Highlight_new(this->fileName, firstText, this->L);
Highlight* hl = Highlight_new(this->fileName, firstText, &this->script);
this->hl = hl;
int size = Panel_size(this->panel);

Expand Down
79 changes: 40 additions & 39 deletions Highlight.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <stdbool.h>

#include "Prototypes.h"
//#needs Script
//#needs PatternMatcher
//#needs Object
//#needs Text
Expand All @@ -29,12 +30,12 @@ struct HighlightContextClass_ {
struct HighlightContext_ {
Object super;
int id;
PatternMatcher* follows;
HighlightContext* parent;
HighlightContext* nextLine;
Color defaultColor;
PatternMatcher* rules;
bool singleLine;
int id;
Color defaultColor;
};
extern HighlightContextClass HighlightContextType;
Expand All @@ -44,7 +45,7 @@ struct Highlight_ {
HighlightContext* mainContext;
HighlightContext* currentContext;
bool toLower;
lua_State* L;
ScriptState* script;
bool hasScript;
};
Expand All @@ -59,8 +60,6 @@ struct ReadHighlightFileArgs_ {
struct MatchArgs_ {
const char* buffer;
int* attrs;
GraphNode* rules;
GraphNode* follows;
HighlightContext* ctx;
Method_PatternMatcher_match match;
int attrsAt;
Expand Down Expand Up @@ -119,9 +118,9 @@ static Color Highlight_translateColor(char* color) {
return NormalColor;
}

Highlight* Highlight_new(const char* fileName, Text firstLine, lua_State* L) {
Highlight* Highlight_new(const char* fileName, Text firstLine, ScriptState* script) {
Highlight* this = (Highlight*) calloc(1, sizeof(Highlight));
this->L = L;
this->script = script;
this->hasScript = false;

this->contexts = Vector_new(ClassAs(HighlightContext, Object), true, DEFAULT_SIZE);
Expand Down Expand Up @@ -253,16 +252,16 @@ HighlightParserState parseFile(ReadHighlightFileArgs* args, FILE* file, const ch
char* close = (String_eq(tokens[2], "`$") ? NULL : tokens[2]);
Color color;
if (ntokens == 6) {
HighlightContext_addRule(args->context, open, Highlight_translateColor(tokens[3]), false);
HighlightContext_addRule(args->context, open, Highlight_translateColor(tokens[3]), false, false);
color = Highlight_translateColor(tokens[5]);
} else {
color = Highlight_translateColor(tokens[3]);
HighlightContext_addRule(args->context, open, color, false);
HighlightContext_addRule(args->context, open, color, false, false);
}
args->context = Highlight_addContext(this, open, close, Stack_peek(args->contexts, NULL), color);
if (close) {
color = (ntokens == 6 ? Highlight_translateColor(tokens[4]) : color);
HighlightContext_addRule(args->context, close, color, false);
HighlightContext_addRule(args->context, close, color, false, false);
}
Stack_push(args->contexts, args->context, 0);
} else if (String_eq(tokens[0], "/context") && ntokens == 1) {
Expand All @@ -271,13 +270,15 @@ HighlightParserState parseFile(ReadHighlightFileArgs* args, FILE* file, const ch
args->context = Stack_peek(args->contexts, NULL);
}
} else if (String_eq(tokens[0], "rule") && ntokens == 3) {
HighlightContext_addRule(args->context, tokens[1], Highlight_translateColor(tokens[2]), false);
HighlightContext_addRule(args->context, tokens[1], Highlight_translateColor(tokens[2]), false, false);
} else if (String_eq(tokens[0], "eager_rule") && ntokens == 3) {
HighlightContext_addRule(args->context, tokens[1], Highlight_translateColor(tokens[2]), true);
HighlightContext_addRule(args->context, tokens[1], Highlight_translateColor(tokens[2]), true, false);
} else if (String_eq(tokens[0], "handover_rule") && ntokens == 3) {
HighlightContext_addRule(args->context, tokens[1], Highlight_translateColor(tokens[2]), false, true);
} else if (String_eq(tokens[0], "insensitive") && ntokens == 1) {
this->toLower = true;
} else if (String_eq(tokens[0], "script") && ntokens == 2) {
this->hasScript = Script_load(this->L, tokens[1]);
this->hasScript = Script_load(this->script, tokens[1]);
} else {
Display_clear();
Display_printAt(0,0,"Error reading %s: line %d: %s", name, lineno, buffer);
Expand Down Expand Up @@ -331,27 +332,23 @@ bool Highlight_readHighlightFile(ReadHighlightFileArgs* args, char* name) {

HighlightContext* Highlight_addContext(Highlight* this, char* open, char* close, HighlightContext* parent, Color color) {
int id = Vector_size(this->contexts);
HighlightContext* ctx = HighlightContext_new(id, color, close == NULL);
HighlightContext* ctx = HighlightContext_new(id, color, parent);
Vector_add(this->contexts, ctx);
if (open) {
assert(parent);
PatternMatcher_add(parent->follows, (unsigned char*) open, (intptr_t) ctx, false);
PatternMatcher_add(parent->follows, (unsigned char*) open, (intptr_t) ctx, false, false);
}
if (close) {
assert(parent);
PatternMatcher_add(ctx->follows, (unsigned char*) close, (intptr_t) parent, false);
PatternMatcher_add(ctx->follows, (unsigned char*) close, (intptr_t) parent, false, false);
} else {
// When multiple contexts ending with `$ are nested,
// they should jump out to the outermost context.
for(;;) {
if (parent) {
ctx->nextLine = parent;
if (parent->singleLine && parent->nextLine != parent) {
parent = parent->nextLine;
continue;
}
if (parent) {
// When multiple contexts ending with `$ are nested,
// they should jump out to the outermost context.
while (parent && parent->nextLine && parent->nextLine != parent) {
parent = parent->nextLine;
}
break;
ctx->nextLine = parent;
}
}
return ctx;
Expand All @@ -360,10 +357,13 @@ HighlightContext* Highlight_addContext(Highlight* this, char* open, char* close,
#define PAINT(_args, _here, _attr) do { _args->attrs[_args->attrsAt++] = _attr; _here = UTF8_forward(_here, 1); } while(0)

static inline void Highlight_tryMatch(Highlight* this, MatchArgs* args, bool paintUnmatched) {
GraphNode* rules = args->ctx->rules->start;
GraphNode* follows = args->ctx->follows->start;
const char* here = args->buffer;
intptr_t intColor;
bool eager;
intptr_t matchlen = args->match(args->rules, args->buffer, &intColor, &eager);
bool eager, handOver;
int saveAttrsAt = args->attrsAt;
intptr_t matchlen = args->match(rules, args->buffer, &intColor, &eager, &handOver);
Color color = (Color) intColor;
assert(color >= 0 && color < Colors);
if (matchlen && (eager || ( !(isword(here[matchlen-1]) && isword(here[matchlen]))))) {
Expand All @@ -372,8 +372,14 @@ static inline void Highlight_tryMatch(Highlight* this, MatchArgs* args, bool pai
while (here < nextStop) {
PAINT(args, here, attr);
}
if (handOver) {
args->ctx = args->ctx->parent;
args->attrsAt = saveAttrsAt;
Highlight_tryMatch(this, args, paintUnmatched);
return;
}
intptr_t nextCtx = 0;
int followMatchlen = args->match(args->follows, args->buffer, &nextCtx, &eager);
int followMatchlen = args->match(follows, args->buffer, &nextCtx, &eager, &handOver);
if (followMatchlen == matchlen) {
assert(nextCtx);
args->ctx = (HighlightContext*) nextCtx;
Expand Down Expand Up @@ -412,14 +418,9 @@ void Highlight_setAttrs(Highlight* this, const char* buffer, int* attrs, int len
if (curCtx->rules->lineStart) {
if (!curCtx->follows->lineStart)
curCtx->follows->lineStart = GraphNode_new();
args.rules = curCtx->rules->lineStart;
args.follows = curCtx->follows->lineStart;

Highlight_tryMatch(this, &args, false);
}
while (args.buffer < buffer + len) {
args.rules = args.ctx->rules->start;
args.follows = args.ctx->follows->start;
Highlight_tryMatch(this, &args, true);
}
Script_highlightLine(this, buffer, attrs, len, y);
Expand All @@ -434,14 +435,14 @@ inline void Highlight_setContext(Highlight* this, HighlightContext* context) {
this->currentContext = (HighlightContext*) context;
}

HighlightContext* HighlightContext_new(int id, Color defaultColor, bool singleLine) {
HighlightContext* HighlightContext_new(int id, Color defaultColor, HighlightContext* parent) {
HighlightContext* this = Alloc(HighlightContext);
this->id = id;
this->follows = PatternMatcher_new();
this->defaultColor = defaultColor;
this->rules = PatternMatcher_new();
this->nextLine = this;
this->singleLine = singleLine;
this->parent = parent;
return this;
}

Expand All @@ -452,6 +453,6 @@ void HighlightContext_delete(Object* cast) {
free(this);
}

void HighlightContext_addRule(HighlightContext* this, char* rule, Color color, bool eager) {
PatternMatcher_add(this->rules, (unsigned char*) rule, (int) color, eager);
void HighlightContext_addRule(HighlightContext* this, char* rule, Color color, bool eager, bool handOver) {
PatternMatcher_add(this->rules, (unsigned char*) rule, (int) color, eager, handOver);
}
2 changes: 1 addition & 1 deletion Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ my_dit_C_sources = dit.c Buffer.c CRT.c Clipboard.c DebugMemory.c Field.c \
FileReader.c FunctionBar.c Hashtable.c Highlight.c Line.c List.c Object.c \
Panel.c PatternMatcher.c Pool.c RichString.c Script.c \
Stack.c String.c StringBuffer.c TabManager.c Undo.c Vector.c Files.c Display.c \
Text.c
Text.c lua-compat-5.3/compat-5.3.c
BUILT_SOURCES = Prototypes.h Structures.h

# find bindings terminals highlight | grep -v \\.svn
Expand Down
Loading

0 comments on commit dddbd05

Please sign in to comment.