about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2021-11-05 22:12:24 -0700
committerKartik K. Agaram <vc@akkartik.com>2021-11-05 22:12:24 -0700
commit6b8da095b1e14d0dad17e78513217abb4cfed8bf (patch)
tree17f105f3ef47a9cfac62ff41cf5e0d121e1da673
parentdd434b2e1c4e7bf027d620575f7d697ff872cacb (diff)
downloadteliva-6b8da095b1e14d0dad17e78513217abb4cfed8bf.tar.gz
stitch editor in
-rw-r--r--src/Makefile3
-rw-r--r--src/kilo.c76
-rw-r--r--src/lcurseslib.c5
-rw-r--r--src/lua.c6
4 files changed, 28 insertions, 62 deletions
diff --git a/src/Makefile b/src/Makefile
index 560781e..ccf1969 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -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
 
diff --git a/src/kilo.c b/src/kilo.c
index 406eb7b..f3b5665 100644
--- a/src/kilo.c
+++ b/src/kilo.c
@@ -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",
-
-	/* 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",
-
-	/* C types */
-        "int|","long|","double|","float|","char|","unsigned|","signed|",
-        "void|","short|","auto|","const|","bool|",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",
+
+  /* types */
+  "nil", "false", "true",
+
+  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;
 }
diff --git a/src/lcurseslib.c b/src/lcurseslib.c
index 4bdbee1..d9bcd37 100644
--- a/src/lcurseslib.c
+++ b/src/lcurseslib.c
@@ -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;
 }
diff --git a/src/lua.c b/src/lua.c
index 6eca245..0b6dcb3 100644
--- a/src/lua.c
+++ b/src/lua.c
@@ -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);