bim-core.h 14 KB

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