bim.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  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 <kuroko/vm.h>
  26. #ifdef __DATE__
  27. # define BIM_BUILD_DATE " built " __DATE__ " at " __TIME__
  28. #else
  29. # define BIM_BUILD_DATE DATE ""
  30. #endif
  31. #ifdef GIT_TAG
  32. # define TAG "-" GIT_TAG
  33. #else
  34. # define TAG "-alpha"
  35. #endif
  36. #define BIM_VERSION "2.99.0" TAG
  37. #define BIM_COPYRIGHT "Copyright 2012-2021 K. Lange <\033[3mklange@toaruos.org\033[23m>"
  38. #define BLOCK_SIZE 4096
  39. #define ENTER_KEY '\r'
  40. #define LINE_FEED '\n'
  41. #define BACKSPACE_KEY 0x08
  42. #define DELETE_KEY 0x7F
  43. #define DEFAULT_KEY_WAIT (global_config.background_task ? 0 : -1)
  44. enum Key {
  45. KEY_TIMEOUT = -1,
  46. KEY_CTRL_AT = 0, /* Base */
  47. KEY_CTRL_A, KEY_CTRL_B, KEY_CTRL_C, KEY_CTRL_D, KEY_CTRL_E, KEY_CTRL_F, KEY_CTRL_G, KEY_CTRL_H,
  48. KEY_CTRL_I, KEY_CTRL_J, KEY_CTRL_K, KEY_CTRL_L, KEY_CTRL_M, KEY_CTRL_N, KEY_CTRL_O, KEY_CTRL_P,
  49. KEY_CTRL_Q, KEY_CTRL_R, KEY_CTRL_S, KEY_CTRL_T, KEY_CTRL_U, KEY_CTRL_V, KEY_CTRL_W, KEY_CTRL_X,
  50. KEY_CTRL_Y, KEY_CTRL_Z, /* Note we keep ctrl-z mapped in termios as suspend */
  51. KEY_CTRL_OPEN, KEY_CTRL_BACKSLASH, KEY_CTRL_CLOSE, KEY_CTRL_CARAT, KEY_CTRL_UNDERSCORE,
  52. /* Space... */
  53. /* Some of these are equivalent to things above */
  54. KEY_BACKSPACE = 0x08,
  55. KEY_LINEFEED = '\n',
  56. KEY_ENTER = '\r',
  57. KEY_TAB = '\t',
  58. /* Basic printable characters go here. */
  59. /* Delete is special */
  60. KEY_DELETE = 0x7F,
  61. /* Unicode codepoints go here */
  62. KEY_ESCAPE = 0x400000, /* Escape would normally be 27, but is special because reasons */
  63. KEY_F1, KEY_F2, KEY_F3, KEY_F4,
  64. KEY_F5, KEY_F6, KEY_F7, KEY_F8,
  65. KEY_F9, KEY_F10, KEY_F11, KEY_F12,
  66. /* TODO ALT, SHIFT, etc., for F keys */
  67. KEY_MOUSE, /* Must be followed with a 3-byte mouse read */
  68. KEY_MOUSE_SGR, /* Followed by an SGR-style sequence of mouse data */
  69. KEY_HOME, KEY_END, KEY_PAGE_UP, KEY_PAGE_DOWN,
  70. KEY_UP, KEY_DOWN, KEY_RIGHT, KEY_LEFT,
  71. KEY_SHIFT_UP, KEY_SHIFT_DOWN, KEY_SHIFT_RIGHT, KEY_SHIFT_LEFT,
  72. KEY_CTRL_UP, KEY_CTRL_DOWN, KEY_CTRL_RIGHT, KEY_CTRL_LEFT,
  73. KEY_ALT_UP, KEY_ALT_DOWN, KEY_ALT_RIGHT, KEY_ALT_LEFT,
  74. KEY_ALT_SHIFT_UP, KEY_ALT_SHIFT_DOWN, KEY_ALT_SHIFT_RIGHT, KEY_ALT_SHIFT_LEFT,
  75. KEY_SHIFT_TAB,
  76. /* Special signals for paste start, paste end */
  77. KEY_PASTE_BEGIN, KEY_PASTE_END,
  78. };
  79. struct key_name_map {
  80. enum Key keycode;
  81. char * name;
  82. };
  83. extern struct key_name_map KeyNames[];
  84. /**
  85. * Syntax highlighting flags.
  86. */
  87. #define FLAG_NONE 0
  88. #define FLAG_KEYWORD 1
  89. #define FLAG_STRING 2
  90. #define FLAG_COMMENT 3
  91. #define FLAG_TYPE 4
  92. #define FLAG_PRAGMA 5
  93. #define FLAG_NUMERAL 6
  94. #define FLAG_ERROR 7
  95. #define FLAG_DIFFPLUS 8
  96. #define FLAG_DIFFMINUS 9
  97. #define FLAG_NOTICE 10
  98. #define FLAG_BOLD 11
  99. #define FLAG_LINK (12 + (1<<4))
  100. #define FLAG_ESCAPE 13
  101. #define FLAG_EXTRA 14
  102. #define FLAG_SPECIAL 15
  103. #define FLAG_LINK_COLOR 12
  104. #define FLAG_UNDERLINE (1 << 4)
  105. #define FLAG_SELECT (1 << 5)
  106. #define FLAG_SEARCH (1 << 6)
  107. #define FLAG_MASK_COLORS 0x0F
  108. #define FLAG_MASK_ATTRIB 0x70
  109. /**
  110. * Line buffer definitions
  111. *
  112. * Lines are essentially resizable vectors of char_t structs,
  113. * which represent single codepoints in the file.
  114. */
  115. typedef struct {
  116. uint32_t display_width:4;
  117. uint32_t flags:7;
  118. uint32_t codepoint:21;
  119. } __attribute__((packed)) char_t;
  120. /**
  121. * Lines have available and actual lengths, describing
  122. * how much space was allocated vs. how much is being
  123. * used at the moment.
  124. */
  125. typedef struct {
  126. int available;
  127. int actual;
  128. int istate;
  129. int is_current;
  130. int rev_status;
  131. char_t text[];
  132. } line_t;
  133. typedef struct background_task {
  134. struct _env * env;
  135. void (*func)(struct background_task*);
  136. struct background_task * next;
  137. int _private_i;
  138. void * _private_p;
  139. } background_task_t;
  140. /**
  141. * Global configuration state
  142. *
  143. * At the moment, this is all in a global, but in the future
  144. * this should be passed around to various functions.
  145. */
  146. typedef struct {
  147. /* Terminal size */
  148. int term_width, term_height;
  149. int bottom_size;
  150. line_t ** yanks;
  151. size_t yank_count;
  152. int yank_is_full_lines;
  153. int tty_in;
  154. const char * bimrc_path;
  155. const char * syntax_fallback;
  156. uint32_t * search;
  157. int overlay_mode;
  158. line_t * command_buffer;
  159. int command_offset, command_col_no;
  160. struct syntax_definition * command_syn, * command_syn_back;
  161. int history_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. extern int find_keywords(struct syntax_state * state, char ** keywords, int flag, int (*keyword_qualifier)(int c));
  459. extern int match_and_paint(struct syntax_state * state, const char * keyword, int flag, int (*keyword_qualifier)(int c));
  460. extern void paint_single_string(struct syntax_state * state);
  461. extern void paint_simple_string(struct syntax_state * state);
  462. extern int common_comment_buzzwords(struct syntax_state * state);
  463. extern int paint_comment(struct syntax_state * state);
  464. extern int match_forward(struct syntax_state * state, char * c);
  465. extern struct syntax_definition * find_syntax_calculator(const char * name);
  466. #define nest(lang, low) \
  467. do { \
  468. state->state = (state->state < 1 ? 0 : state->state - low); \
  469. do { state->state = lang(state); } while (state->state == 0); \
  470. if (state->state == -1) return low; \
  471. return state->state + low; \
  472. } while (0)
  473. /* Some of the C stuff is widely used */
  474. extern int c_keyword_qualifier(int c);
  475. extern int paint_c_numeral(struct syntax_state * state);
  476. extern int paint_c_comment(struct syntax_state * state);
  477. extern void paint_c_char(struct syntax_state * state);
  478. /* Hacky workaround for isdigit not really accepting Unicode stuff */
  479. static __attribute__((used)) int _isdigit(int c) { if (c > 128) return 0; return isdigit(c); }
  480. static __attribute__((used)) int _isxdigit(int c) { if (c > 128) return 0; return isxdigit(c); }
  481. #undef isdigit
  482. #undef isxdigit
  483. #define isdigit(c) _isdigit(c)
  484. #define isxdigit(c) _isxdigit(c)
  485. #endif /* _BIM_CORE_H */