header.h 12 KB

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