stitch editor in

This commit is contained in:
Kartik K. Agaram 2021-11-05 22:12:24 -07:00
parent dd434b2e1c
commit 6b8da095b1
4 changed files with 26 additions and 60 deletions

View File

@ -25,7 +25,8 @@ PLATS= aix ansi bsd freebsd generic linux macosx mingw posix solaris
LUA_A= liblua.a
CORE_O= lapi.o lcode.o ldebug.o ldo.o ldump.o lfunc.o lgc.o llex.o lmem.o \
lobject.o lopcodes.o lparser.o lstate.o lstring.o ltable.o ltm.o \
lundump.o lvm.o lzio.o
lundump.o lvm.o lzio.o \
kilo.o
LIB_O= lauxlib.o lbaselib.o lcurseslib.o ldblib.o liolib.o lmathlib.o \
loslib.o ltablib.o lstrlib.o loadlib.o linit.o

View File

@ -69,11 +69,10 @@
#define HL_HIGHLIGHT_NUMBERS (1<<1)
struct editorSyntax {
char **filematch;
char **keywords;
char singleline_comment_start[2];
char multiline_comment_start[3];
char multiline_comment_end[3];
char *multiline_comment_start;
char *multiline_comment_end;
int flags;
};
@ -161,36 +160,26 @@ void editorSetStatusMessage(const char *fmt, ...);
*
* There is no support to highlight patterns currently. */
/* C / C++ */
char *C_HL_extensions[] = {".c",".h",".cpp",".hpp",".cc",NULL};
char *C_HL_keywords[] = {
/* C Keywords */
"auto","break","case","continue","default","do","else","enum",
"extern","for","goto","if","register","return","sizeof","static",
"struct","switch","typedef","union","volatile","while","NULL",
/* Lua */
char *Lua_HL_keywords[] = {
/* keywords */
"do", "end", "function", "return", "require", "local"
"if", "then", "else", "elseif",
"while", "for", "repeat", "until", "break",
"and", "or", "not", "in",
/* C++ Keywords */
"alignas","alignof","and","and_eq","asm","bitand","bitor","class",
"compl","constexpr","const_cast","deltype","delete","dynamic_cast",
"explicit","export","false","friend","inline","mutable","namespace",
"new","noexcept","not","not_eq","nullptr","operator","or","or_eq",
"private","protected","public","reinterpret_cast","static_assert",
"static_cast","template","this","thread_local","throw","true","try",
"typeid","typename","virtual","xor","xor_eq",
/* types */
"nil", "false", "true",
/* C types */
"int|","long|","double|","float|","char|","unsigned|","signed|",
"void|","short|","auto|","const|","bool|",NULL
NULL
};
/* Here we define an array of syntax highlights by extensions, keywords,
* comments delimiters and flags. */
struct editorSyntax HLDB[] = {
{
/* C / C++ */
C_HL_extensions,
C_HL_keywords,
"//","/*","*/",
Lua_HL_keywords,
"--", "--[[", "--]]",
HL_HIGHLIGHT_STRINGS | HL_HIGHLIGHT_NUMBERS
}
};
@ -530,26 +519,6 @@ int editorSyntaxToColor(int hl) {
}
}
/* Select the syntax highlight scheme depending on the filename,
* setting it in the global state E.syntax. */
void editorSelectSyntaxHighlight(char *filename) {
for (unsigned int j = 0; j < HLDB_ENTRIES; j++) {
struct editorSyntax *s = HLDB+j;
unsigned int i = 0;
while(s->filematch[i]) {
char *p;
int patlen = strlen(s->filematch[i]);
if ((p = strstr(filename,s->filematch[i])) != NULL) {
if (s->filematch[i][0] != '.' || p[patlen] == '\0') {
E.syntax = s;
return;
}
}
i++;
}
}
}
/* ======================= Editor rows implementation ======================= */
/* Update the rendered version and the syntax highlight of a row. */
@ -1283,20 +1252,14 @@ void initEditor(void) {
E.row = NULL;
E.dirty = 0;
E.filename = NULL;
E.syntax = NULL;
E.syntax = &HLDB[0];
updateWindowSize();
signal(SIGWINCH, handleSigWinCh);
}
int main(int argc, char **argv) {
if (argc != 2) {
fprintf(stderr,"Usage: kilo <filename>\n");
exit(1);
}
void edit(char* filename) {
initEditor();
editorSelectSyntaxHighlight(argv[1]);
editorOpen(argv[1]);
editorOpen(filename);
enableRawMode(STDIN_FILENO);
editorSetStatusMessage(
"HELP: Ctrl-S = save | Ctrl-Q = quit | Ctrl-F = find");
@ -1304,5 +1267,4 @@ int main(int argc, char **argv) {
editorRefreshScreen();
editorProcessKeypress(STDIN_FILENO);
}
return 0;
}

View File

@ -88,13 +88,16 @@ static int Pcolor_pair (lua_State *L)
}
extern const char *Script_name;
static int Pgetch (lua_State *L) {
int c = wgetch(stdscr);
if (c == ERR)
return 0;
if (c == 24) /* ctrl-x */
exit(0);
/* TODO: handle other standard menu hotkeys here */
if (c == 5) /* ctrl-e */
edit(Script_name);
/* handle other standard menu hotkeys here */
lua_pushinteger(L, c);
return 1;
}

View File

@ -234,13 +234,13 @@ static void dotty (lua_State *L) {
}
const char *Script_name = NULL;
static int handle_script (lua_State *L, char **argv, int n) {
int status;
const char *fname;
int narg = getargs(L, argv, n); /* collect arguments */
lua_setglobal(L, "arg");
fname = argv[n];
status = luaL_loadfile(L, fname);
Script_name = argv[n];
status = luaL_loadfile(L, Script_name);
lua_insert(L, -(narg+1));
if (status == 0)
status = docall(L, narg, 0);