header.h 13 KB

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