header.h 11 KB

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