elisp.c (6696B)
1 #include "elisp.h" 2 #include <stdio.h> 3 4 // Emacs symbols 5 emacs_value Qt; 6 emacs_value Qnil; 7 emacs_value Qnormal; 8 emacs_value Qbold; 9 emacs_value Qitalic; 10 emacs_value Qforeground; 11 emacs_value Qbackground; 12 emacs_value Qweight; 13 emacs_value Qunderline; 14 emacs_value Qslant; 15 emacs_value Qreverse; 16 emacs_value Qstrike; 17 emacs_value Qextend; 18 emacs_value Qface; 19 emacs_value Qbox; 20 emacs_value Qbar; 21 emacs_value Qhbar; 22 emacs_value Qcursor_type; 23 emacs_value Qemacs_major_version; 24 emacs_value Qvterm_line_wrap; 25 emacs_value Qrear_nonsticky; 26 emacs_value Qvterm_prompt; 27 28 // Emacs functions 29 emacs_value Fblink_cursor_mode; 30 emacs_value Fsymbol_value; 31 emacs_value Flength; 32 emacs_value Flist; 33 emacs_value Fnth; 34 emacs_value Ferase_buffer; 35 emacs_value Finsert; 36 emacs_value Fgoto_char; 37 emacs_value Fforward_char; 38 emacs_value Fforward_line; 39 emacs_value Fgoto_line; 40 emacs_value Fdelete_lines; 41 emacs_value Frecenter; 42 emacs_value Fset_window_point; 43 emacs_value Fwindow_body_height; 44 emacs_value Fpoint; 45 emacs_value Fapply; 46 47 emacs_value Fput_text_property; 48 emacs_value Fadd_text_properties; 49 emacs_value Fset; 50 emacs_value Fvterm_flush_output; 51 emacs_value Fget_buffer_window_list; 52 emacs_value Fselected_window; 53 emacs_value Fvterm_set_title; 54 emacs_value Fvterm_set_directory; 55 emacs_value Fvterm_invalidate; 56 emacs_value Feq; 57 emacs_value Fvterm_get_color; 58 emacs_value Fvterm_eval; 59 emacs_value Fvterm_set_selection; 60 61 /* Set the function cell of the symbol named NAME to SFUN using 62 the 'fset' function. */ 63 void bind_function(emacs_env *env, const char *name, emacs_value Sfun) { 64 emacs_value Qfset = env->intern(env, "fset"); 65 emacs_value Qsym = env->intern(env, name); 66 67 env->funcall(env, Qfset, 2, (emacs_value[]){Qsym, Sfun}); 68 } 69 70 /* Provide FEATURE to Emacs. */ 71 void provide(emacs_env *env, const char *feature) { 72 emacs_value Qfeat = env->intern(env, feature); 73 emacs_value Qprovide = env->intern(env, "provide"); 74 75 env->funcall(env, Qprovide, 1, (emacs_value[]){Qfeat}); 76 } 77 78 emacs_value symbol_value(emacs_env *env, emacs_value symbol) { 79 return env->funcall(env, Fsymbol_value, 1, (emacs_value[]){symbol}); 80 } 81 82 int string_bytes(emacs_env *env, emacs_value string) { 83 ptrdiff_t size = 0; 84 env->copy_string_contents(env, string, NULL, &size); 85 return size; 86 } 87 88 emacs_value length(emacs_env *env, emacs_value string) { 89 return env->funcall(env, Flength, 1, (emacs_value[]){string}); 90 } 91 92 emacs_value list(emacs_env *env, emacs_value elements[], ptrdiff_t len) { 93 return env->funcall(env, Flist, len, elements); 94 } 95 emacs_value nth(emacs_env *env, int idx, emacs_value list) { 96 emacs_value eidx = env->make_integer(env, idx); 97 return env->funcall(env, Fnth, 2, (emacs_value[]){eidx, list}); 98 } 99 100 void put_text_property(emacs_env *env, emacs_value string, emacs_value property, 101 emacs_value value) { 102 emacs_value start = env->make_integer(env, 0); 103 emacs_value end = length(env, string); 104 105 env->funcall(env, Fput_text_property, 5, 106 (emacs_value[]){start, end, property, value, string}); 107 } 108 109 void add_text_properties(emacs_env *env, emacs_value string, 110 emacs_value property) { 111 emacs_value start = env->make_integer(env, 0); 112 emacs_value end = length(env, string); 113 114 env->funcall(env, Fadd_text_properties, 4, 115 (emacs_value[]){start, end, property, string}); 116 } 117 118 void erase_buffer(emacs_env *env) { env->funcall(env, Ferase_buffer, 0, NULL); } 119 120 void insert(emacs_env *env, emacs_value string) { 121 env->funcall(env, Finsert, 1, (emacs_value[]){string}); 122 } 123 124 void goto_char(emacs_env *env, int pos) { 125 emacs_value point = env->make_integer(env, pos); 126 env->funcall(env, Fgoto_char, 1, (emacs_value[]){point}); 127 } 128 129 void forward_line(emacs_env *env, int n) { 130 emacs_value nline = env->make_integer(env, n); 131 env->funcall(env, Fforward_line, 1, (emacs_value[]){nline}); 132 } 133 void goto_line(emacs_env *env, int n) { 134 emacs_value nline = env->make_integer(env, n); 135 env->funcall(env, Fgoto_line, 1, (emacs_value[]){nline}); 136 } 137 void delete_lines(emacs_env *env, int linenum, int count, bool del_whole_line) { 138 emacs_value Qlinenum = env->make_integer(env, linenum); 139 emacs_value Qcount = env->make_integer(env, count); 140 if (del_whole_line) { 141 env->funcall(env, Fdelete_lines, 3, (emacs_value[]){Qlinenum, Qcount, Qt}); 142 } else { 143 env->funcall(env, Fdelete_lines, 3, 144 (emacs_value[]){Qlinenum, Qcount, Qnil}); 145 } 146 } 147 void recenter(emacs_env *env, emacs_value pos) { 148 env->funcall(env, Frecenter, 1, (emacs_value[]){pos}); 149 } 150 emacs_value point(emacs_env *env) { return env->funcall(env, Fpoint, 0, NULL); } 151 152 void set_window_point(emacs_env *env, emacs_value win, emacs_value point) { 153 env->funcall(env, Fset_window_point, 2, (emacs_value[]){win, point}); 154 } 155 emacs_value window_body_height(emacs_env *env, emacs_value win) { 156 return env->funcall(env, Fwindow_body_height, 1, (emacs_value[]){win}); 157 } 158 159 bool eq(emacs_env *env, emacs_value e1, emacs_value e2) { 160 emacs_value Qeq = env->funcall(env, Feq, 2, (emacs_value[]){e1, e2}); 161 return env->is_not_nil(env, Qeq); 162 } 163 164 void forward_char(emacs_env *env, emacs_value n) { 165 env->funcall(env, Fforward_char, 1, (emacs_value[]){n}); 166 } 167 168 emacs_value get_buffer_window_list(emacs_env *env) { 169 return env->funcall(env, Fget_buffer_window_list, 3, 170 (emacs_value[]){Qnil, Qnil, Qt}); 171 } 172 173 emacs_value selected_window(emacs_env *env) { 174 return env->funcall(env, Fselected_window, 0, (emacs_value[]){}); 175 } 176 177 void set_cursor_type(emacs_env *env, emacs_value cursor_type) { 178 env->funcall(env, Fset, 2, (emacs_value[]){Qcursor_type, cursor_type}); 179 } 180 181 void set_cursor_blink(emacs_env *env, bool blink) { 182 env->funcall(env, Fblink_cursor_mode, 1, 183 (emacs_value[]){env->make_integer(env, blink)}); 184 } 185 186 emacs_value vterm_get_color(emacs_env *env, int index, emacs_value args) { 187 emacs_value idx = env->make_integer(env, index); 188 return env->funcall(env, Fapply, 3, (emacs_value[]){ Fvterm_get_color, idx, args }); 189 } 190 191 void set_title(emacs_env *env, emacs_value string) { 192 env->funcall(env, Fvterm_set_title, 1, (emacs_value[]){string}); 193 } 194 195 void set_directory(emacs_env *env, emacs_value string) { 196 env->funcall(env, Fvterm_set_directory, 1, (emacs_value[]){string}); 197 } 198 199 void vterm_invalidate(emacs_env *env) { 200 env->funcall(env, Fvterm_invalidate, 0, NULL); 201 } 202 emacs_value vterm_eval(emacs_env *env, emacs_value string) { 203 return env->funcall(env, Fvterm_eval, 1, (emacs_value[]){string}); 204 } 205 206 emacs_value vterm_set_selection(emacs_env *env, emacs_value selection_target, 207 emacs_value selection_data) { 208 return env->funcall(env, Fvterm_set_selection, 2, 209 (emacs_value[]){selection_target, selection_data}); 210 }