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