aboutsummaryrefslogtreecommitdiff
path: root/elisp.c
blob: df713277b180ec9c7286932dba19d68e4273ae19 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "elisp.h"
#include <stdio.h>

/* Set the function cell of the symbol named NAME to SFUN using
   the 'fset' function.  */
void bind_function(emacs_env *env, const char *name, emacs_value Sfun) {
  emacs_value Qfset = env->intern(env, "fset");
  emacs_value Qsym = env->intern(env, name);

  env->funcall(env, Qfset, 2, (emacs_value[]){Qsym, Sfun});
}

/* Provide FEATURE to Emacs.  */
void provide(emacs_env *env, const char *feature) {
  emacs_value Qfeat = env->intern(env, feature);
  emacs_value Qprovide = env->intern(env, "provide");

  env->funcall(env, Qprovide, 1, (emacs_value[]){Qfeat});
}

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});
}

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);
};

uint8_t hex_to_byte(char *hex) { return strtoul(hex, NULL, 16); }

VTermColor rgb_string_to_color(emacs_env *env, emacs_value string) {
  VTermColor color;
  ptrdiff_t len = 8;
  char buffer[len];
  char hex[3];
  env->copy_string_contents(env, string, buffer, &len);
  hex[0] = buffer[1];
  hex[1] = buffer[2];
  color.red = hex_to_byte(hex);
  hex[0] = buffer[3];
  hex[1] = buffer[4];
  color.green = hex_to_byte(hex);
  hex[0] = buffer[5];
  hex[1] = buffer[6];
  color.blue = hex_to_byte(hex);

  return color;
};

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});
}

void toggle_cursor(emacs_env *env, bool visible) {
  emacs_value Qvisible = visible ? Qt : Qnil;
  env->funcall(env, Fset, 2, (emacs_value[]){Qcursor_type, Qvisible});
}

emacs_value get_hex_color_fg(emacs_env *env, emacs_value face) {
  return env->funcall(env, Fvterm_face_color_hex, 2,
                      (emacs_value[]){face, Qforeground});
}

emacs_value get_hex_color_bg(emacs_env *env, emacs_value face) {
  return env->funcall(env, Fvterm_face_color_hex, 2,
                      (emacs_value[]){face, Qbackground});
}