header.h 12 KB

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