aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLukas Fürmetz <fuermetz@mailbox.org>2017-11-15 22:13:55 +0100
committerLukas Fürmetz <fuermetz@mailbox.org>2017-11-16 12:05:24 +0100
commit14ef6c59279d6fde95e70db924e376ea9bb57516 (patch)
tree9cde46df7a480a9da0458526d6168e9f5111db9b
parent865528b34398ee4ad9d7dcfa86e54e27106bd4d5 (diff)
Extract elisp related functionality into elisp.c
-rw-r--r--CMakeLists.txt2
-rw-r--r--elisp.c108
-rw-r--r--elisp.h41
-rw-r--r--vterm-module.c108
-rw-r--r--vterm-module.h38
5 files changed, 151 insertions, 146 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index c371555..b85460e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(ExternalProject)
project(emacs-libvterm C)
-add_library(vterm-module SHARED vterm-module.c utf8.c)
+add_library(vterm-module SHARED vterm-module.c utf8.c elisp.c)
set_property(TARGET vterm-module PROPERTY POSITION_INDEPENDENT_CODE ON)
set_target_properties(vterm-module PROPERTIES PREFIX "")
diff --git a/elisp.c b/elisp.c
new file mode 100644
index 0000000..c17b7e1
--- /dev/null
+++ b/elisp.c
@@ -0,0 +1,108 @@
+#include "elisp.h"
+#include <stdio.h>
+
+/* Bind NAME to FUN. */
+void bind_function(emacs_env *env, const char *name, emacs_value Sfun) {
+ /* Set the function cell of the symbol named NAME to SFUN using
+ the 'fset' function. */
+
+ /* Convert the strings to symbols by interning them */
+ emacs_value Qfset = env->intern(env, "fset");
+ emacs_value Qsym = env->intern(env, name);
+
+ /* Prepare the arguments array */
+ emacs_value args[] = {Qsym, Sfun};
+
+ /* Make the call (2 == nb of arguments) */
+ env->funcall(env, Qfset, 2, args);
+}
+
+/* Provide FEATURE to Emacs. */
+void provide(emacs_env *env, const char *feature) {
+ /* call 'provide' with FEATURE converted to a symbol */
+
+ emacs_value Qfeat = env->intern(env, feature);
+ emacs_value Qprovide = env->intern(env, "provide");
+ emacs_value args[] = {Qfeat};
+
+ env->funcall(env, Qprovide, 1, args);
+}
+
+int string_bytes(emacs_env *env, emacs_value string) {
+ ptrdiff_t size = 0;
+ env->copy_string_contents(env, string, NULL, &size);
+ return size;
+}
+
+emacs_value string_length(emacs_env *env, emacs_value string) {
+ return env->funcall(env, Flength, 1, (emacs_value[]){string});
+}
+
+emacs_value list(emacs_env *env, emacs_value *elements, ptrdiff_t len) {
+ return env->funcall(env, Flist, len, elements);
+}
+
+void put_text_property(emacs_env *env, emacs_value string,
+ emacs_value property, emacs_value value) {
+ emacs_value start = env->make_integer(env, 0);
+ emacs_value end = string_length(env, string);
+
+ env->funcall(env, Fput_text_property, 5,
+ (emacs_value[]){start, end, property, value, string});
+}
+
+/*
+ * Color must be a string #RGB
+ */
+emacs_value render_text(emacs_env *env, char *buffer, int len,
+ VTermScreenCell *cell) {
+ emacs_value text = env->make_string(env, buffer, len);
+
+ emacs_value foreground = color_to_rgb_string(env, cell->fg);
+ emacs_value background = color_to_rgb_string(env, cell->bg);
+ emacs_value bold = cell->attrs.bold ? Qbold : Qnormal;
+ emacs_value underline = cell->attrs.underline ? Qt : Qnil;
+ emacs_value italic = cell->attrs.italic ? Qitalic : Qnormal;
+ emacs_value reverse = cell->attrs.reverse ? Qt : Qnil;
+ emacs_value strike = cell->attrs.strike ? Qt : Qnil;
+
+ // TODO: Blink, font, dwl, dhl is missing
+ emacs_value properties =
+ list(env,
+ (emacs_value[]){Qforeground, foreground, Qbackground, background,
+ Qweight, bold, Qunderline, underline, Qslant, italic,
+ Qreverse, reverse, Qstrike, strike},
+ 14);
+
+ put_text_property(env, text, Qface, properties);
+
+ return text;
+}
+
+void byte_to_hex(uint8_t byte, char *hex) {
+ snprintf(hex, 3, "%.2X", byte);
+}
+
+emacs_value color_to_rgb_string(emacs_env *env, VTermColor color) {
+ char buffer[8];
+ buffer[0] = '#';
+ buffer[7] = '\0';
+ byte_to_hex(color.red, buffer + 1);
+ byte_to_hex(color.green, buffer + 3);
+ byte_to_hex(color.blue, buffer + 5);
+
+ return env->make_string(env, buffer, 7);
+};
+
+void erase_buffer(emacs_env *env) {
+ env->funcall(env, Ferase_buffer, 0, NULL);
+}
+
+void insert(emacs_env *env, emacs_value string) {
+ env->funcall(env, Finsert, 1, (emacs_value[]){string});
+}
+
+void goto_char(emacs_env *env, int pos) {
+ emacs_value point = env->make_integer(env, pos);
+ env->funcall(env, Fgoto_char, 1, (emacs_value[]){point});
+}
diff --git a/elisp.h b/elisp.h
new file mode 100644
index 0000000..2cde1a0
--- /dev/null
+++ b/elisp.h
@@ -0,0 +1,41 @@
+#include <emacs-module.h>
+#include "vterm.h"
+
+// Emacs symbols
+emacs_value Qt;
+emacs_value Qnil;
+emacs_value Qnormal;
+emacs_value Qbold;
+emacs_value Qitalic;
+emacs_value Qforeground;
+emacs_value Qbackground;
+emacs_value Qweight;
+emacs_value Qunderline;
+emacs_value Qslant;
+emacs_value Qreverse;
+emacs_value Qstrike;
+emacs_value Qface;
+
+// Emacs functions
+emacs_value Flength;
+emacs_value Flist;
+emacs_value Ferase_buffer;
+emacs_value Finsert;
+emacs_value Fgoto_char;
+emacs_value Fput_text_property;
+
+// Utils
+void bind_function(emacs_env *env, const char *name, emacs_value Sfun);
+void provide(emacs_env *env, const char *feature);
+int string_bytes(emacs_env *env, emacs_value string);
+emacs_value string_length(emacs_env *env, emacs_value string);
+emacs_value list(emacs_env *env, emacs_value *elements, ptrdiff_t len);
+void put_text_property(emacs_env *env, emacs_value string, emacs_value property,
+ emacs_value value);
+emacs_value render_text(emacs_env *env, char *string, int len,
+ VTermScreenCell *cell);
+void byte_to_hex(uint8_t byte, char *hex);
+emacs_value color_to_rgb_string(emacs_env *env, VTermColor color);
+void erase_buffer(emacs_env *env);
+void insert(emacs_env *env, emacs_value string);
+void goto_char(emacs_env *env, int pos);
diff --git a/vterm-module.c b/vterm-module.c
index d320f41..1b7e2de 100644
--- a/vterm-module.c
+++ b/vterm-module.c
@@ -1,4 +1,5 @@
#include "vterm-module.h"
+#include "elisp.h"
#include <fcntl.h>
#ifdef __APPLE__
#include <util.h>
@@ -8,7 +9,6 @@
#include "utf8.h"
#include <pthread.h>
#include <signal.h>
-#include <stdio.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/select.h>
@@ -20,112 +20,6 @@
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
-/* Bind NAME to FUN. */
-static void bind_function(emacs_env *env, const char *name, emacs_value Sfun) {
- /* Set the function cell of the symbol named NAME to SFUN using
- the 'fset' function. */
-
- /* Convert the strings to symbols by interning them */
- emacs_value Qfset = env->intern(env, "fset");
- emacs_value Qsym = env->intern(env, name);
-
- /* Prepare the arguments array */
- emacs_value args[] = {Qsym, Sfun};
-
- /* Make the call (2 == nb of arguments) */
- env->funcall(env, Qfset, 2, args);
-}
-
-/* Provide FEATURE to Emacs. */
-static void provide(emacs_env *env, const char *feature) {
- /* call 'provide' with FEATURE converted to a symbol */
-
- emacs_value Qfeat = env->intern(env, feature);
- emacs_value Qprovide = env->intern(env, "provide");
- emacs_value args[] = {Qfeat};
-
- env->funcall(env, Qprovide, 1, args);
-}
-
-static int string_bytes(emacs_env *env, emacs_value string) {
- ptrdiff_t size = 0;
- env->copy_string_contents(env, string, NULL, &size);
- return size;
-}
-
-static emacs_value string_length(emacs_env *env, emacs_value string) {
- return env->funcall(env, Flength, 1, (emacs_value[]){string});
-}
-
-static emacs_value list(emacs_env *env, emacs_value *elements, ptrdiff_t len) {
- return env->funcall(env, Flist, len, elements);
-}
-
-static void put_text_property(emacs_env *env, emacs_value string,
- emacs_value property, emacs_value value) {
- emacs_value start = env->make_integer(env, 0);
- emacs_value end = string_length(env, string);
-
- env->funcall(env, Fput_text_property, 5,
- (emacs_value[]){start, end, property, value, string});
-}
-
-/*
- * Color must be a string #RGB
- */
-static emacs_value render_text(emacs_env *env, char *buffer, int len,
- VTermScreenCell *cell) {
- emacs_value text = env->make_string(env, buffer, len);
-
- emacs_value foreground = color_to_rgb_string(env, cell->fg);
- emacs_value background = color_to_rgb_string(env, cell->bg);
- emacs_value bold = cell->attrs.bold ? Qbold : Qnormal;
- emacs_value underline = cell->attrs.underline ? Qt : Qnil;
- emacs_value italic = cell->attrs.italic ? Qitalic : Qnormal;
- emacs_value reverse = cell->attrs.reverse ? Qt : Qnil;
- emacs_value strike = cell->attrs.strike ? Qt : Qnil;
-
- // TODO: Blink, font, dwl, dhl is missing
- emacs_value properties =
- list(env,
- (emacs_value[]){Qforeground, foreground, Qbackground, background,
- Qweight, bold, Qunderline, underline, Qslant, italic,
- Qreverse, reverse, Qstrike, strike},
- 14);
-
- put_text_property(env, text, Qface, properties);
-
- return text;
-}
-
-static void byte_to_hex(uint8_t byte, char *hex) {
- snprintf(hex, 3, "%.2X", byte);
-}
-
-static emacs_value color_to_rgb_string(emacs_env *env, VTermColor color) {
- char buffer[8];
- buffer[0] = '#';
- buffer[7] = '\0';
- byte_to_hex(color.red, buffer + 1);
- byte_to_hex(color.green, buffer + 3);
- byte_to_hex(color.blue, buffer + 5);
-
- return env->make_string(env, buffer, 7);
-};
-
-static void erase_buffer(emacs_env *env) {
- env->funcall(env, Ferase_buffer, 0, NULL);
-}
-
-static void insert(emacs_env *env, emacs_value string) {
- env->funcall(env, Finsert, 1, (emacs_value[]){string});
-}
-
-static void goto_char(emacs_env *env, int pos) {
- emacs_value point = env->make_integer(env, pos);
- env->funcall(env, Fgoto_char, 1, (emacs_value[]){point});
-}
-
static void vterm_put_caret(VTerm *vt, emacs_env *env, int row, int col,
int offset) {
int rows, cols;
diff --git a/vterm-module.h b/vterm-module.h
index cd755b6..86bf79a 100644
--- a/vterm-module.h
+++ b/vterm-module.h
@@ -13,44 +13,6 @@ struct Term {
pthread_t thread;
};
-// Emacs symbols
-static emacs_value Qt;
-static emacs_value Qnil;
-static emacs_value Qnormal;
-static emacs_value Qbold;
-static emacs_value Qitalic;
-static emacs_value Qforeground;
-static emacs_value Qbackground;
-static emacs_value Qweight;
-static emacs_value Qunderline;
-static emacs_value Qslant;
-static emacs_value Qreverse;
-static emacs_value Qstrike;
-static emacs_value Qface;
-
-// Emacs functions
-static emacs_value Flength;
-static emacs_value Flist;
-static emacs_value Ferase_buffer;
-static emacs_value Finsert;
-static emacs_value Fgoto_char;
-static emacs_value Fput_text_property;
-
-static void bind_function(emacs_env *env, const char *name, emacs_value Sfun);
-static void provide(emacs_env *env, const char *feature);
-static int string_bytes(emacs_env *env, emacs_value string);
-static emacs_value string_length(emacs_env *env, emacs_value string);
-static emacs_value list(emacs_env *env, emacs_value *elements, ptrdiff_t len);
-static void put_text_property(emacs_env *env, emacs_value string,
- emacs_value property, emacs_value value);
-static emacs_value render_text(emacs_env *env, char *string, int len,
- VTermScreenCell *cell);
-static void byte_to_hex(uint8_t byte, char *hex);
-static emacs_value color_to_rgb_string(emacs_env *env, VTermColor color);
-static void erase_buffer(emacs_env *env);
-static void insert(emacs_env *env, emacs_value string);
-static void goto_char(emacs_env *env, int pos);
-
static void vterm_put_caret(VTerm *vt, emacs_env *env, int row, int col,
int offset);
static void vterm_redraw(VTerm *vt, emacs_env *env);