header.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /* header.h, Ait, BSD 3-Clause, Kevin Bloom, 2023-2024,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #define _XOPEN_SOURCE
  6. // #include <locale.h>
  7. #include <stdlib.h>
  8. #include <stdarg.h>
  9. #include <assert.h>
  10. #include <stdio.h>
  11. #include <sys/types.h>
  12. #include <ctype.h>
  13. #include <limits.h>
  14. #include <string.h>
  15. #include <unistd.h>
  16. #include <wchar.h>
  17. #include "termbox.h"
  18. int mkstemp(char *);
  19. #if defined(__APPLE__) || defined(__NetBSD__)
  20. #define st_atim st_atimespec
  21. #define st_ctim st_ctimespec
  22. #define st_mtim st_mtimespec
  23. #endif
  24. #define VERSION "Ait 1.10, BSD 3-Clause, 2024, by Kevin Bloom, No warranty."
  25. #define PROG_NAME "ait"
  26. #define B_MODIFIED 0x01 /* modified buffer */
  27. #define B_OVERWRITE 0x02 /* overwrite mode */
  28. #define NOMARK -1
  29. #define CHUNK 8096L
  30. #define K_BUFFER_LENGTH 256
  31. #define TEMPBUF 1024
  32. #define STRBUF_L 256
  33. #define STRBUF_M 64
  34. #define STRBUF_S 64
  35. #define MIN_GAP_EXPAND 512
  36. #define TEMPFILE "/tmp/feXXXXXX"
  37. #define F_NONE 0
  38. #define F_CLEAR 1
  39. #define ID_DEFAULT 1
  40. #define ID_SYMBOL 2
  41. #define ID_MODELINE 3
  42. #define ID_DIGITS 4
  43. #define ID_LINE_COMMENT 5
  44. #define ID_BLOCK_COMMENT 6
  45. #define ID_DOUBLE_STRING 7
  46. #define ID_SINGLE_STRING 8
  47. #define ID_REGION 9
  48. #define ID_BACK_STRING 10
  49. #define ID_FUNCTION 11
  50. #define ID_CONSTANT 12
  51. #define TRUE 1
  52. #define FALSE 0
  53. #define TAB_SIZE 8
  54. #define TAB_SPACE_SIZE 2
  55. #define PMARK_SIZE 10
  56. #define KILLRING_SIZE 10
  57. typedef unsigned char char_t;
  58. typedef long point_t;
  59. #define UA_NOTHING 0
  60. #define UA_REPEAT 1
  61. #define UA_CUSTOM 2
  62. #define UA_PREVENT 3
  63. #define KBD_MACRO 4
  64. #define KBD_DEFAULT 0
  65. #define KBD_INSERT 1
  66. #define KBD_DELETE_CHAR 2
  67. #define KBD_CUT 3
  68. #define KBD_UNDO 4
  69. #define KBD_DELETE_WORD 5
  70. #define KBD_YANK 6
  71. #define KBD_EXPAND 7
  72. /* Window modes
  73. */
  74. #define WINDOW_ERROR 0
  75. #define WINDOW_DEFAULT 1
  76. #define WINDOW_HORIZ 2
  77. #define WINDOW_VERT 3
  78. #define WINDOW_TRIHORIZ 4
  79. #define WINDOW_TRIVERT 5
  80. #define WINDOW_FIBLEFT 6
  81. #define WINDOW_FIBRIGHT 7
  82. #define WINDOW_QUAD 8
  83. #define WINDOW_OTHER 9
  84. typedef struct keymap_t {
  85. char *key_desc; /* name of bound function */
  86. int key_mod; /* Termbox modifier number */
  87. int *key_bytes; /* Array of Termbox key or char numbers */
  88. void (*func)(void);
  89. int universal_argument_action;
  90. } keymap_t;
  91. typedef struct syntax_t {
  92. char *word;
  93. int color;
  94. } syntax_t;
  95. typedef struct keywords_t {
  96. char *extension; /* file extension */
  97. char *slc; /* Single Line Comment */
  98. char *mlc; /* Multiple Line Comment */
  99. char *emlc; /* Multiple Line Comment End */
  100. int sqas; /* Single Quote As String */
  101. int bqas; /* Back Quote As String */
  102. syntax_t *keywords;
  103. } keywords_t;
  104. typedef struct undo_t {
  105. point_t u_point; /* point at which the undo happens */
  106. char_t *u_data; /* data effected by the undo */
  107. unsigned long u_size; /* used only for REPLACE & LOAD */
  108. int u_line; /* original line number */
  109. int u_adjust; /* should we adjust the point after undoing? */
  110. enum {
  111. INSERT = 1,
  112. DELETE,
  113. BACKSP,
  114. CUT,
  115. YANK,
  116. REPLACE,
  117. CLIPBOARD,
  118. LOAD,
  119. } u_type;
  120. struct undo_t *u_next; /* next undo in the trail */
  121. } undo_t;
  122. typedef struct dynars_t {
  123. char_t *result;
  124. struct dynars_t *d_next;
  125. } dynars_t;
  126. typedef struct dyna_t {
  127. char_t *query; /* the query */
  128. int nquery; /* size of the query */
  129. point_t start; /* The beginning of the query in the buffer */
  130. point_t end; /* The end of the query in the buffer */
  131. point_t sp; /* The Search Point, where we've searched already */
  132. int nresult;
  133. dynars_t *results;
  134. } dyna_t;
  135. typedef struct buffer_t
  136. {
  137. struct buffer_t *b_next; /* Link to next buffer_t */
  138. struct buffer_t *b_prev; /* Link to prev buffer_t */
  139. point_t b_mark; /* the mark */
  140. point_t b_pmark[PMARK_SIZE]; /* the previous marks */
  141. point_t b_point; /* the point */
  142. point_t b_cpoint; /* the original current point, used for multiple window displaying */
  143. point_t b_opoint; /* orignial point */
  144. point_t b_page; /* start of page */
  145. point_t b_opage; /* original start of page */
  146. point_t b_epage; /* end of page */
  147. int b_reframe; /* force a reframe of the display */
  148. int b_cnt; /* count of windows referencing this buffer */
  149. int b_size; /* current size of text being edited (not including gap) */
  150. int b_psize; /* previous size */
  151. char_t *b_buf; /* start of buffer */
  152. char_t *b_ebuf; /* end of buffer */
  153. char_t *b_gap; /* start of gap */
  154. char_t *b_egap; /* end of gap */
  155. int b_row; /* cursor row */
  156. int b_col; /* cursor col */
  157. int b_pcol; /* previous cursor col */
  158. int b_line; /* current line */
  159. char b_fname[PATH_MAX + 1]; /* filename */
  160. size_t b_fmtime;
  161. char b_bname[STRBUF_L]; /* buffer name */
  162. char b_flags; /* buffer flags */
  163. undo_t b_ubuf[STRBUF_M]; /* undoset */
  164. undo_t *b_undo; /* start of undo */
  165. undo_t *b_redo; /* start of redo */
  166. int b_path; /* does the buffer point to a real path */
  167. keywords_t *b_keywords;
  168. } buffer_t;
  169. typedef struct window_t
  170. {
  171. struct window_t *w_next; /* Next window */
  172. struct buffer_t *w_bufp; /* Buffer displayed in window */
  173. point_t w_point;
  174. point_t w_mark;
  175. point_t w_page;
  176. point_t w_epage;
  177. int w_top; /* Origin 0 top row of window */
  178. int w_left; /* Origin 0 left-most column of window */
  179. int w_rows; /* no. of rows of text in window */
  180. int w_cols; /* no. of cols of text in window */
  181. int w_row; /* cursor row */
  182. int w_col; /* cursor col */
  183. int w_pcol; /* previous cursor col */
  184. int w_update;
  185. char w_name[STRBUF_S];
  186. int w_mcol; /* the last longest column (for rendering) */
  187. int w_mlen; /* length of the modeline */
  188. int w_recenter; /* force recenter */
  189. int w_hilite; /* starting state for highlight */
  190. } window_t;
  191. typedef struct kill_t {
  192. char_t *data;
  193. point_t len;
  194. } kill_t;
  195. extern buffer_t *curbp; /* current buffer */
  196. extern buffer_t *bheadp; /* head of list of buffers */
  197. extern window_t *curwp;
  198. extern window_t *wheadp;
  199. /*
  200. * Some compilers define size_t as a unsigned 16 bit number while
  201. * point_t and off_t might be defined as a signed 32 bit number.
  202. * malloc(), realloc(), fread(), and fwrite() take size_t parameters,
  203. * which means there will be some size limits because size_t is too
  204. * small of a type.
  205. */
  206. #define MAX_SIZE_T ((unsigned long) (size_t) ~0)
  207. extern int LINES;
  208. extern int COLS;
  209. extern int MSGLINE;
  210. extern int done; /* Quit flag. */
  211. extern int msgflag; /* True if msgline should be displayed. */
  212. extern kill_t scrap; /* Allocated scrap buffer. */
  213. extern kill_t kill_ring[KILLRING_SIZE]; /* Previous scraps */
  214. extern char_t *input;
  215. extern char msgline[]; /* Message line input/output buffer. */
  216. extern char temp[]; /* Temporary buffer. */
  217. extern char *gtemp; /* Growable Temporary buffer. */
  218. extern char searchtext[];
  219. extern int found_point; /* Page line where found search item is */
  220. extern int search_dir; /* 1 = fwd, 2 = back */
  221. extern char replace[];
  222. extern keymap_t *key_map; /* Command key mappings. */
  223. extern keymap_t keymap[];
  224. extern keymap_t *key_return; /* Command key return */
  225. extern int universal_argument;
  226. extern int numeric_argument;
  227. extern int negated;
  228. extern int submatch;
  229. extern uint32_t input_char; /* Used if the input char is wanted later. Such as numeric-argument */
  230. extern int undoset_flag; /* Used to determine if the undo should be set on movegap */
  231. extern char editor_dir[]; /* The directory the editor was opened in. */
  232. extern int record_input;
  233. extern int record_buffer_index;
  234. extern int run_buffer_index;
  235. extern struct tb_event record_buffer[];
  236. extern int execute_kbd_macro;
  237. extern int undo_index;
  238. extern char unicode_buf[7];
  239. extern char character[1];
  240. extern int lastcommand;
  241. extern int currentcommand;
  242. extern int window_mode;
  243. extern int ignorenotbound;
  244. extern char *backup_dir; /* Backup file supplied with -b */
  245. extern char *switch_command; /* Command supplied with -s */
  246. extern int lastcol; /* Last column used with goto-column */
  247. extern int lastline; /* Last line used with goto-line */
  248. extern char lastchar; /* Last char used with zap-/jump-to-char */
  249. extern char lastsymb; /* Last symbol used with delete-between */
  250. extern point_t recentbracket; /* The bracket you just input */
  251. extern const char* term;
  252. extern dyna_t dynaex; /* The dynamic expansion struct */
  253. extern void fatal(char *);
  254. extern void msg(char *, ...);
  255. extern void display(window_t *, int);
  256. extern void dispmsg(void);
  257. extern void modeline(window_t *);
  258. extern int utf8_size(char_t);
  259. extern int prev_utf8_char_size(void);
  260. extern void display_utf8(char_t *, int, int, int);
  261. extern point_t lnstart(buffer_t *, point_t);
  262. extern point_t lncolumn(buffer_t *, point_t, int);
  263. extern point_t segstart(buffer_t *, window_t *, point_t, point_t);
  264. extern point_t segnext(buffer_t *, window_t *, point_t, point_t);
  265. extern point_t upup(buffer_t *, window_t *, point_t);
  266. extern point_t dndn(buffer_t *, window_t *, point_t);
  267. extern char_t *get_key(keymap_t *, keymap_t **);
  268. extern int getinput(char *, char *, int, int, int);
  269. extern int getfilename(char *, char *, int);
  270. extern buffer_t * getbuffername(char *, char *, int, int *);
  271. extern void display_prompt_and_response(char *, char *);
  272. extern int growgap(buffer_t *, point_t);
  273. extern point_t movegap(buffer_t *, point_t);
  274. extern point_t pos(buffer_t *, char_t *);
  275. extern char_t *ptr(buffer_t *, point_t);
  276. extern int posix_file(char *);
  277. extern int save(char *);
  278. extern int load_file(char *);
  279. extern int insert_file(char *, int);
  280. extern void undoset(int, int);
  281. extern void undo(void);
  282. extern void redo(void);
  283. extern void backsp(void);
  284. extern void block(void);
  285. extern void iblock(void);
  286. extern void unmark(void);
  287. extern void bottom(void);
  288. extern void cut(void);
  289. extern void copy(void);
  290. extern void copy_cut(int, int, int);
  291. extern void delete(void);
  292. extern void toggle_overwrite_mode(void);
  293. extern void down(void);
  294. extern void insert(void);
  295. extern void insert_str(void);
  296. extern void insert_unicode(void);
  297. extern void left(void);
  298. extern void lnbegin(void);
  299. extern void lnend(void);
  300. extern void paste_internal(int);
  301. extern void paste(void);
  302. extern void clipboard(void);
  303. extern void pgdown(void);
  304. extern void pgup(void);
  305. extern void quit(void);
  306. extern int yesno(int);
  307. extern char yesnomaybeso(char);
  308. extern void quit_ask(void);
  309. extern void redraw(void);
  310. extern void readfile(void);
  311. extern void insertfile(void);
  312. extern void right(void);
  313. extern void top(void);
  314. extern void up(void);
  315. extern void version(void);
  316. extern void wleft(void);
  317. extern void wleftdelete(void);
  318. extern void wright(void);
  319. extern void wrightdelete(void);
  320. extern void writefile(void);
  321. extern void savebuffer(void);
  322. extern void showpos(void);
  323. extern void killtoeol(void);
  324. extern void gotoline(void);
  325. extern void gotocolumn(void);
  326. extern void jumptorow(void);
  327. extern void jumpword(void);
  328. extern void search(int);
  329. extern void search_fwd(void);
  330. extern void search_rev(void);
  331. extern void query_replace(void);
  332. extern void delete_between(void);
  333. extern point_t line_to_point(int);
  334. extern point_t search_forward(buffer_t *, point_t, char *);
  335. extern point_t search_backwards(buffer_t *, point_t, char *);
  336. extern void update_search_prompt(char *, char *);
  337. extern void display_search_result(point_t, int, char *, char *, int *);
  338. extern buffer_t* find_buffer(char *, int, int);
  339. extern void buffer_init(buffer_t *);
  340. extern int delete_buffer(buffer_t *);
  341. extern void next_buffer(void);
  342. extern void prev_buffer(void);
  343. extern void switch_buffer(void);
  344. extern int count_buffers(void);
  345. extern int modified_buffers(void);
  346. extern void killbuffer(void);
  347. extern char* get_buffer_name(buffer_t *);
  348. extern void get_line_stats(int *, int *, buffer_t *);
  349. extern void query_replace(void);
  350. extern window_t *new_window();
  351. extern void one_window(window_t *);
  352. extern void split_window();
  353. extern void tri_split_window();
  354. extern void chop_window();
  355. extern void tri_chop_window();
  356. extern void fib_left();
  357. extern void fib_right();
  358. extern void quad_window();
  359. extern void close_window();
  360. extern void next_window();
  361. extern void delete_other_windows();
  362. extern void free_other_windows(window_t *);
  363. extern void update_display();
  364. extern void w2b(window_t *);
  365. extern void b2w(window_t *);
  366. extern void associate_b2w(buffer_t *, window_t *);
  367. extern void disassociate_b(window_t *);
  368. extern void set_parse_state(buffer_t *, point_t, window_t *, int);
  369. extern int parse_text(buffer_t *, point_t);
  370. extern void write_parse_state(window_t *);
  371. extern void resize_terminal();
  372. extern void insertnewlinebelow();
  373. extern void insertnewline();
  374. extern void inserttab();
  375. extern void inserttabasspace();
  376. extern void recenter();
  377. extern void print_tb(const char *, int, int, uint16_t, uint16_t);
  378. extern void printf_tb(int, int, uint16_t, uint16_t, const char *, ...);
  379. extern void print_to_msgline(const char *);
  380. extern void addch(char);
  381. extern void addstr(const char *);
  382. extern void addchxy(char, int, int, int);
  383. extern void addstrxy(char *, int, int, int);
  384. extern void clrtoeol(const char *, int);
  385. extern void suspend();
  386. extern void transpose();
  387. extern void transposeword();
  388. extern void lowercaseword();
  389. extern void capitalizeword();
  390. extern void uppercaseword();
  391. extern void zaptochar();
  392. extern void negated_zaptochar();
  393. extern void jumptochar();
  394. extern void negated_jumptochar();
  395. extern void poptomark();
  396. extern void universal_argument_load();
  397. extern void numeric_argument_load();
  398. extern void back_to_indentation();
  399. extern void negate();
  400. extern void forward_bracket();
  401. extern void backward_bracket();
  402. extern void start_kbd_macro();
  403. extern void end_kbd_macro();
  404. extern void run_kbd_macro();
  405. extern void get_popen_data(int);
  406. extern void open_file_from_shell();
  407. extern void insert_from_shell();
  408. extern void modify_buffer_name(buffer_t *, int);
  409. extern int compare_buffer_name(buffer_t *, buffer_t *);
  410. extern int msgline_editor(struct tb_event, char *, char *, int, int *);
  411. extern int is_file_modified(char *);
  412. extern int file_was_modified_prompt();
  413. extern void insert_control_char();
  414. extern void comment();
  415. extern void comment_at_eol();
  416. extern void resize();
  417. extern void use_kbd_macro(struct tb_event *);
  418. extern void dynamically_expand();