bim.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. #ifndef _BIM_CORE_H
  2. #define _BIM_CORE_H
  3. #define _XOPEN_SOURCE 700
  4. #define _DARWIN_C_SOURCE
  5. #define _DEFAULT_SOURCE
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdint.h>
  10. #include <stdarg.h>
  11. #include <unistd.h>
  12. #include <termios.h>
  13. #include <signal.h>
  14. #include <libgen.h>
  15. #include <locale.h>
  16. #include <wchar.h>
  17. #include <ctype.h>
  18. #include <dirent.h>
  19. #include <poll.h>
  20. #include <limits.h>
  21. #include <errno.h>
  22. #include <sys/types.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/stat.h>
  25. #include <sys/wait.h>
  26. #include <kuroko/vm.h>
  27. #ifdef __DATE__
  28. # define BIM_BUILD_DATE " built " __DATE__ " at " __TIME__
  29. #else
  30. # define BIM_BUILD_DATE DATE ""
  31. #endif
  32. #ifdef GIT_TAG
  33. # define TAG "-" GIT_TAG
  34. #else
  35. # define TAG "-alpha"
  36. #endif
  37. #define BLOCK_SIZE 4096
  38. #define ENTER_KEY '\r'
  39. #define LINE_FEED '\n'
  40. #define BACKSPACE_KEY 0x08
  41. #define DELETE_KEY 0x7F
  42. #define DEFAULT_KEY_WAIT (global_config.background_task ? 0 : -1)
  43. enum Key {
  44. KEY_TIMEOUT = -1,
  45. KEY_CTRL_AT = 0, /* Base */
  46. KEY_CTRL_A, KEY_CTRL_B, KEY_CTRL_C, KEY_CTRL_D, KEY_CTRL_E, KEY_CTRL_F, KEY_CTRL_G, KEY_CTRL_H,
  47. KEY_CTRL_I, KEY_CTRL_J, KEY_CTRL_K, KEY_CTRL_L, KEY_CTRL_M, KEY_CTRL_N, KEY_CTRL_O, KEY_CTRL_P,
  48. KEY_CTRL_Q, KEY_CTRL_R, KEY_CTRL_S, KEY_CTRL_T, KEY_CTRL_U, KEY_CTRL_V, KEY_CTRL_W, KEY_CTRL_X,
  49. KEY_CTRL_Y, KEY_CTRL_Z, /* Note we keep ctrl-z mapped in termios as suspend */
  50. KEY_CTRL_OPEN, KEY_CTRL_BACKSLASH, KEY_CTRL_CLOSE, KEY_CTRL_CARAT, KEY_CTRL_UNDERSCORE,
  51. /* Space... */
  52. /* Some of these are equivalent to things above */
  53. KEY_BACKSPACE = 0x08,
  54. KEY_LINEFEED = '\n',
  55. KEY_ENTER = '\r',
  56. KEY_TAB = '\t',
  57. /* Basic printable characters go here. */
  58. /* Delete is special */
  59. KEY_DELETE = 0x7F,
  60. /* Unicode codepoints go here */
  61. KEY_ESCAPE = 0x400000, /* Escape would normally be 27, but is special because reasons */
  62. KEY_F1, KEY_F2, KEY_F3, KEY_F4,
  63. KEY_F5, KEY_F6, KEY_F7, KEY_F8,
  64. KEY_F9, KEY_F10, KEY_F11, KEY_F12,
  65. /* TODO ALT, SHIFT, etc., for F keys */
  66. KEY_MOUSE, /* Must be followed with a 3-byte mouse read */
  67. KEY_MOUSE_SGR, /* Followed by an SGR-style sequence of mouse data */
  68. KEY_HOME, KEY_END, KEY_PAGE_UP, KEY_PAGE_DOWN,
  69. KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT,
  70. KEY_SHIFT_UP, KEY_SHIFT_DOWN, KEY_SHIFT_RIGHT, KEY_SHIFT_LEFT,
  71. KEY_CTRL_UP, KEY_CTRL_DOWN, KEY_CTRL_RIGHT, KEY_CTRL_LEFT,
  72. KEY_ALT_UP, KEY_ALT_DOWN, KEY_ALT_RIGHT, KEY_ALT_LEFT,
  73. KEY_ALT_SHIFT_UP, KEY_ALT_SHIFT_DOWN, KEY_ALT_SHIFT_RIGHT, KEY_ALT_SHIFT_LEFT,
  74. KEY_SHIFT_TAB,
  75. /* Special signals for paste start, paste end */
  76. KEY_PASTE_BEGIN, KEY_PASTE_END,
  77. };
  78. struct key_name_map {
  79. enum Key keycode;
  80. char * name;
  81. };
  82. extern struct key_name_map KeyNames[];
  83. /**
  84. * Syntax highlighting flags.
  85. */
  86. #define FLAG_NONE 0
  87. #define FLAG_KEYWORD 1
  88. #define FLAG_STRING 2
  89. #define FLAG_COMMENT 3
  90. #define FLAG_TYPE 4
  91. #define FLAG_PRAGMA 5
  92. #define FLAG_NUMERAL 6
  93. #define FLAG_ERROR 7
  94. #define FLAG_DIFFPLUS 8
  95. #define FLAG_DIFFMINUS 9
  96. #define FLAG_NOTICE 10
  97. #define FLAG_BOLD 11
  98. #define FLAG_LINK (12 + (1<<4))
  99. #define FLAG_ESCAPE 13
  100. #define FLAG_EXTRA 14
  101. #define FLAG_SPECIAL 15
  102. #define FLAG_LINK_COLOR 12
  103. #define FLAG_UNDERLINE (1 << 4)
  104. #define FLAG_SELECT (1 << 5)
  105. #define FLAG_SEARCH (1 << 6)
  106. #define FLAG_MASK_COLORS 0x0F
  107. #define FLAG_MASK_ATTRIB 0x70
  108. /**
  109. * Line buffer definitions
  110. *
  111. * Lines are essentially resizable vectors of char_t structs,
  112. * which represent single codepoints in the file.
  113. */
  114. typedef struct {
  115. uint32_t display_width:4;
  116. uint32_t flags:7;
  117. uint32_t codepoint:21;
  118. } __attribute__((packed)) char_t;
  119. /**
  120. * Lines have available and actual lengths, describing
  121. * how much space was allocated vs. how much is being
  122. * used at the moment.
  123. */
  124. typedef struct {
  125. int available;
  126. int actual;
  127. int istate;
  128. int is_current;
  129. int rev_status;
  130. char_t text[];
  131. } line_t;
  132. typedef struct background_task {
  133. struct _env * env;
  134. void (*func)(struct background_task*);
  135. struct background_task * next;
  136. int _private_i;
  137. void * _private_p;
  138. } background_task_t;
  139. /**
  140. * Global configuration state
  141. *
  142. * At the moment, this is all in a global, but in the future
  143. * this should be passed around to various functions.
  144. */
  145. typedef struct {
  146. /* Terminal size */
  147. int term_width, term_height;
  148. int bottom_size;
  149. line_t ** yanks;
  150. size_t yank_count;
  151. int yank_is_full_lines;
  152. int tty_in;
  153. const char * bimrc_path;
  154. const char * syntax_fallback;
  155. uint32_t * search;
  156. int overlay_mode;
  157. line_t * command_buffer;
  158. int command_offset, command_col_no;
  159. struct syntax_definition * command_syn, * command_syn_back;
  160. int history_point;
  161. int search_point;
  162. int search_direction;
  163. int prev_line, prev_col, prev_coffset, prev_offset;
  164. unsigned int highlight_on_open:1;
  165. unsigned int initial_file_is_read_only:1;
  166. unsigned int go_to_line:1;
  167. unsigned int break_from_selection:1;
  168. unsigned int can_scroll:1;
  169. unsigned int can_hideshow:1;
  170. unsigned int can_altscreen:1;
  171. unsigned int can_mouse:1;
  172. unsigned int can_unicode:1;
  173. unsigned int can_bright:1;
  174. unsigned int can_title:1;
  175. unsigned int can_bce:1;
  176. unsigned int can_24bit:1;
  177. unsigned int can_256color:1;
  178. unsigned int can_italic:1;
  179. unsigned int can_insert:1;
  180. unsigned int can_bracketedpaste:1;
  181. unsigned int can_sgrmouse:1;
  182. unsigned int history_enabled:1;
  183. unsigned int highlight_parens:1;
  184. unsigned int smart_case:1;
  185. unsigned int highlight_current_line:1;
  186. unsigned int shift_scrolling:1;
  187. unsigned int check_git:1;
  188. unsigned int color_gutter:1;
  189. unsigned int relative_lines:1;
  190. unsigned int numbers:1;
  191. unsigned int horizontal_shift_scrolling:1;
  192. unsigned int hide_statusbar:1;
  193. unsigned int tabs_visible:1;
  194. unsigned int autohide_tabs:1;
  195. unsigned int smart_complete:1;
  196. unsigned int has_terminal:1;
  197. unsigned int search_wraps:1;
  198. unsigned int had_error:1;
  199. unsigned int use_biminfo:1;
  200. int cursor_padding;
  201. int split_percent;
  202. int scroll_amount;
  203. int tab_offset;
  204. char * tab_indicator;
  205. char * space_indicator;
  206. background_task_t * background_task;
  207. background_task_t * tail_task;
  208. } global_config_t;
  209. #define OVERLAY_MODE_NONE 0
  210. #define OVERLAY_MODE_READ_ONE 1
  211. #define OVERLAY_MODE_COMMAND 2
  212. #define OVERLAY_MODE_SEARCH 3
  213. #define OVERLAY_MODE_COMPLETE 4
  214. #define OVERLAY_MODE_FILESEARCH 5
  215. #define HISTORY_SENTINEL 0
  216. #define HISTORY_INSERT 1
  217. #define HISTORY_DELETE 2
  218. #define HISTORY_REPLACE 3
  219. #define HISTORY_REMOVE_LINE 4
  220. #define HISTORY_ADD_LINE 5
  221. #define HISTORY_REPLACE_LINE 6
  222. #define HISTORY_MERGE_LINES 7
  223. #define HISTORY_SPLIT_LINE 8
  224. #define HISTORY_BREAK 10
  225. typedef struct history {
  226. struct history * previous;
  227. struct history * next;
  228. int type;
  229. int line;
  230. int col;
  231. union {
  232. struct {
  233. int lineno;
  234. int offset;
  235. int codepoint;
  236. int old_codepoint;
  237. } insert_delete_replace;
  238. struct {
  239. int lineno;
  240. line_t * contents;
  241. line_t * old_contents;
  242. } remove_replace_line;
  243. struct {
  244. int lineno;
  245. int split;
  246. } add_merge_split_lines;
  247. } contents;
  248. } history_t;
  249. /**
  250. * Buffer data
  251. *
  252. * A buffer describes a file, and stores
  253. * its name as well as the editor state
  254. * (cursor offsets, etc.) and the actual
  255. * line buffers.
  256. */
  257. typedef struct _env {
  258. unsigned int loading:1;
  259. unsigned int tabs:1;
  260. unsigned int modified:1;
  261. unsigned int readonly:1;
  262. unsigned int indent:1;
  263. unsigned int checkgitstatusonwrite:1;
  264. unsigned int crnl:1;
  265. unsigned int numbers:1;
  266. unsigned int gutter:1;
  267. unsigned int slowop:1;
  268. int highlighting_paren;
  269. int maxcolumn;
  270. short mode;
  271. short tabstop;
  272. char * file_name;
  273. int offset;
  274. int coffset;
  275. int line_no;
  276. int line_count;
  277. int line_avail;
  278. int col_no;
  279. int preferred_column;
  280. struct syntax_definition * syntax;
  281. line_t ** lines;
  282. history_t * history;
  283. history_t * last_save_history;
  284. int width;
  285. int left;
  286. int start_line;
  287. int sel_col;
  288. int start_col;
  289. int prev_line;
  290. } buffer_t;
  291. struct theme_def {
  292. const char * name;
  293. void * callable;
  294. };
  295. extern struct theme_def * themes;
  296. extern void add_colorscheme(struct theme_def theme);
  297. struct syntax_state {
  298. buffer_t * env;
  299. line_t * line;
  300. int line_no;
  301. int state;
  302. int i;
  303. };
  304. struct completion_match {
  305. char * string;
  306. char * file;
  307. char * search;
  308. };
  309. struct syntax_definition {
  310. char * name;
  311. char ** ext;
  312. int (*calculate)(struct syntax_state *);
  313. int prefers_spaces;
  314. int (*completion_qualifier)(int c);
  315. int (*completion_matcher)(uint32_t * comp, struct completion_match ** matches, int * matches_count, int complete_match, int * matches_len, buffer_t * env);
  316. void * krkFunc;
  317. void * krkClass;
  318. };
  319. extern struct syntax_definition * syntaxes;
  320. /**
  321. * Editor mode states
  322. */
  323. #define MODE_NORMAL 0
  324. #define MODE_INSERT 1
  325. #define MODE_LINE_SELECTION 2
  326. #define MODE_REPLACE 3
  327. #define MODE_CHAR_SELECTION 4
  328. #define MODE_COL_SELECTION 5
  329. #define MODE_COL_INSERT 6
  330. #define MODE_DIRECTORY_BROWSE 7
  331. struct action_def {
  332. char * name;
  333. void (*action)();
  334. int options;
  335. const char * description;
  336. };
  337. extern struct action_def * mappable_actions;
  338. #define ARG_IS_INPUT 0x01 /* Takes the key that triggered it as the first argument */
  339. #define ARG_IS_CUSTOM 0x02 /* Takes a custom argument which is specific to the method */
  340. #define ARG_IS_PROMPT 0x04 /* Prompts for an argument. */
  341. #define ACTION_IS_RW 0x08 /* Needs to be able to write. */
  342. #define BIM_ACTION(name, options, description) \
  343. void name (); /* Define the action with unknown arguments */ \
  344. void __attribute__((constructor)) _install_ ## name (void) { \
  345. add_action((struct action_def){#name, name, options, description}); \
  346. } \
  347. void name
  348. struct command_def {
  349. char * name;
  350. int (*command)(char *, int, char * arg[]);
  351. const char * description;
  352. };
  353. #define BIM_COMMAND(cmd_name, cmd_str, description) \
  354. int bim_command_ ## cmd_name (char * cmd, int argc, char * argv[]); \
  355. void __attribute__((constructor)) _install_cmd_ ## cmd_name (void) { \
  356. add_command((struct command_def){cmd_str, bim_command_ ## cmd_name, description}); \
  357. } \
  358. int bim_command_ ## cmd_name (char * cmd __attribute__((unused)), int argc __attribute__((unused)), char * argv[] __attribute__((unused)))
  359. #define BIM_ALIAS(alias, alias_name, cmd_name) \
  360. void __attribute__((constructor)) _install_alias_ ## alias_name (void) { \
  361. add_command((struct command_def){alias, bim_command_ ## cmd_name, "Alias for " #cmd_name}); \
  362. }
  363. #define BIM_PREFIX_COMMAND(cmd_name, cmd_prefix, description) \
  364. int bim_command_ ## cmd_name (char * cmd, int argc, char * argv[]); \
  365. void __attribute__((constructor)) _install_cmd_ ## cmd_name (void) { \
  366. add_prefix_command((struct command_def){cmd_prefix, bim_command_ ## cmd_name, description}); \
  367. } \
  368. int bim_command_ ## cmd_name (char * cmd __attribute__((unused)), int argc __attribute__((unused)), char * argv[] __attribute__((unused)))
  369. extern const char * flag_to_color(int _flag);
  370. extern void redraw_line(int x);
  371. extern int git_examine(char * filename);
  372. extern void search_next(void);
  373. extern void set_preferred_column(void);
  374. extern void quit(const char * message);
  375. extern void close_buffer(void);
  376. extern void set_syntax_by_name(const char * name);
  377. extern void rehighlight_search(line_t * line);
  378. extern void try_to_center();
  379. extern int read_one_character(char * message);
  380. extern void bim_unget(int c);
  381. #define bim_getch() bim_getch_timeout(200)
  382. extern int bim_getch_timeout(int timeout);
  383. extern buffer_t * buffer_new(void);
  384. extern FILE * open_biminfo(void);
  385. extern int fetch_from_biminfo(buffer_t * buf);
  386. extern int update_biminfo(buffer_t * buf, int is_open);
  387. extern buffer_t * buffer_close(buffer_t * buf);
  388. extern int to_eight(uint32_t codepoint, char * out);
  389. extern char * name_from_key(enum Key keycode);
  390. extern void add_action(struct action_def action);
  391. extern void open_file(char * file);
  392. extern void recalculate_selected_lines(void);
  393. extern void add_command(struct command_def command);
  394. extern void add_prefix_command(struct command_def command);
  395. extern void render_command_input_buffer(void);
  396. extern void unhighlight_matching_paren(void);
  397. extern void add_syntax(struct syntax_definition def);
  398. struct ColorName {
  399. const char * name;
  400. const char ** value;
  401. };
  402. extern struct ColorName color_names[];
  403. struct bim_function {
  404. char * command;
  405. struct bim_function * next;
  406. };
  407. extern struct bim_function ** user_functions;
  408. extern int run_function(char * name);
  409. extern int has_function(char * name);
  410. extern void find_matching_paren(int * out_line, int * out_col, int in_col);
  411. extern void render_error(char * message, ...);
  412. extern void render_commandline_message(char * message, ...);
  413. extern void pause_for_key(void);
  414. #define add_match(match_string, match_file, match_search) do { \
  415. if (*matches_count == *matches_len) { \
  416. (*matches_len) *= 2; \
  417. *matches = realloc(*matches, sizeof(struct completion_match) * (*matches_len)); \
  418. } \
  419. (*matches)[*matches_count].string = strdup(match_string); \
  420. (*matches)[*matches_count].file = strdup(match_file); \
  421. (*matches)[*matches_count].search = strdup(match_search); \
  422. (*matches_count)++; \
  423. } while (0)
  424. #define add_if_match(name,desc) do { \
  425. int i = 0; \
  426. while (comp[i] && comp[i] == (unsigned char)name[i]) i++; \
  427. if (comp[i] == '\0') { \
  428. add_match(name,desc,""); \
  429. } \
  430. } while (0)
  431. struct action_map {
  432. int key;
  433. void (*method)();
  434. int options;
  435. int arg;
  436. };
  437. #define opt_rep 0x1 /* This action will be repeated */
  438. #define opt_arg 0x2 /* This action will take a specified argument */
  439. #define opt_char 0x4 /* This action will read a character to pass as an argument */
  440. #define opt_nav 0x8 /* This action will consume the nav buffer as its argument */
  441. #define opt_rw 0x10 /* Must not be read-only */
  442. #define opt_norm 0x20 /* Returns to normal mode */
  443. #define opt_byte 0x40 /* Same as opt_char but forces a byte */
  444. struct mode_names {
  445. const char * description;
  446. const char * name;
  447. struct action_map ** mode;
  448. };
  449. extern struct mode_names mode_names[];
  450. #define paint(length, flag) do { for (int i = 0; i < (length) && state->i < state->line->actual; i++, state->i++) { \
  451. state->line->text[state->i].flags = (state->line->text[state->i].flags & (3 << 5)) | (flag); \
  452. } } while (0)
  453. #define charat() (state->i < state->line->actual ? state->line->text[(state->i)].codepoint : -1)
  454. #define nextchar() (state->i + 1 < state->line->actual ? state->line->text[(state->i+1)].codepoint : -1)
  455. #define lastchar() (state->i - 1 >= 0 ? state->line->text[(state->i-1)].codepoint : -1)
  456. #define skip() (state->i++)
  457. #define charrel(x) ((state->i + (x) >= 0 && state->i + (x) < state->line->actual) ? state->line->text[(state->i+(x))].codepoint : -1)
  458. static int match_and_paint(struct syntax_state * state, const char * keyword, int flag, int (*keyword_qualifier)(int c));
  459. static int common_comment_buzzwords(struct syntax_state * state);
  460. static int paint_comment(struct syntax_state * state);
  461. static struct syntax_definition * find_syntax_calculator(const char * name);
  462. #define nest(lang, low) \
  463. do { \
  464. state->state = (state->state < 1 ? 0 : state->state - low); \
  465. do { state->state = lang(state); } while (state->state == 0); \
  466. if (state->state == -1) return low; \
  467. return state->state + low; \
  468. } while (0)
  469. /* Hacky workaround for isdigit not really accepting Unicode stuff */
  470. static __attribute__((used)) int _isdigit(int c) { if (c > 128) return 0; return isdigit(c); }
  471. static __attribute__((used)) int _isxdigit(int c) { if (c > 128) return 0; return isxdigit(c); }
  472. #undef isdigit
  473. #undef isxdigit
  474. #define isdigit(c) _isdigit(c)
  475. #define isxdigit(c) _isxdigit(c)
  476. #endif /* _BIM_CORE_H */