buffer.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* buffer.c, Ait Emacs, Kevin Bloom, Public Domain, 2023-2024 */
  2. #include <assert.h>
  3. #include <string.h>
  4. #include "header.h"
  5. #include "termbox.h"
  6. /* list of keywords _must_ end with NULL */
  7. keywords_t keywords[] = {
  8. /* programming */
  9. {".c", "//", "/*", "*/", FALSE, FALSE},
  10. {".h", "//", "/*", "*/", FALSE, FALSE},
  11. {".css", "//", "/*", "*/", FALSE, FALSE},
  12. {".html", "", "<!--", "-->", FALSE, FALSE},
  13. {".js", "//", "/*", "*/", TRUE, TRUE},
  14. {".jsx", "//", "/*", "*/", TRUE, TRUE},
  15. {".ts", "//", "/*", "*/", TRUE, TRUE},
  16. {".tsx", "//", "/*", "*/", TRUE, TRUE},
  17. {".lisp", ";;", NULL, NULL, FALSE, FALSE},
  18. {".clj", ";;", NULL, NULL, FALSE, FALSE},
  19. {".cljs", ";;", NULL, NULL, FALSE, FALSE},
  20. {".joke", ";;", NULL, NULL, FALSE, FALSE},
  21. {".hs", "--", NULL, NULL, FALSE, FALSE},
  22. {".sh", "#", NULL, NULL, FALSE, FALSE},
  23. {".awk", "#", NULL, NULL, FALSE, FALSE},
  24. {".java", "//", "/*", "*/", FALSE, FALSE},
  25. /* non-programming */
  26. {".md", "#", NULL, NULL, FALSE, TRUE},
  27. {"NULL", NULL, NULL, NULL, FALSE, FALSE },
  28. };
  29. void buffer_init(buffer_t *bp)
  30. {
  31. bp->b_mark = NOMARK;
  32. bp->b_pmark[0] = NOMARK;
  33. bp->b_pmark[1] = NOMARK;
  34. bp->b_pmark[2] = NOMARK;
  35. bp->b_pmark[3] = NOMARK;
  36. bp->b_pmark[4] = NOMARK;
  37. bp->b_point = 0;
  38. bp->b_cpoint = 0;
  39. bp->b_opoint = 0;
  40. bp->b_page = 0;
  41. bp->b_epage = 0;
  42. bp->b_reframe = 0;
  43. bp->b_size = 0;
  44. bp->b_psize = 0;
  45. bp->b_flags = 0;
  46. bp->b_cnt = 0;
  47. bp->b_buf = NULL;
  48. bp->b_ebuf = NULL;
  49. bp->b_gap = NULL;
  50. bp->b_egap = NULL;
  51. bp->b_next = NULL;
  52. bp->b_fname[0] = '\0';
  53. bp->b_fmtime = 0;
  54. bp->b_bname[0] = '\0';
  55. bp->b_path = TRUE;
  56. bp->b_undo = NULL;
  57. bp->b_redo = NULL;
  58. bp->b_keywords = NULL;
  59. bp->b_line = 1;
  60. }
  61. /* Find a buffer by filename or create if requested */
  62. buffer_t* find_buffer (char *fname, int cflag)
  63. {
  64. buffer_t *bp = NULL;
  65. buffer_t *ebp = NULL;
  66. buffer_t *sb = NULL;
  67. keywords_t *k;
  68. char filepath[PATH_MAX];
  69. int len, extlen, c = 0;
  70. int match = FALSE, i = 1;
  71. strcpy(filepath, fname);
  72. bp = bheadp;
  73. lastbp = curbp;
  74. while (bp != NULL) {
  75. if (strcmp (fname, bp->b_fname) == 0 || strcmp(fname, bp->b_bname) == 0) {
  76. return (bp);
  77. }
  78. bp = bp->b_next;
  79. }
  80. if (cflag != FALSE) {
  81. if ((bp = (buffer_t *) malloc (sizeof (buffer_t))) == NULL)
  82. return (0);
  83. buffer_init(bp);
  84. assert(bp != NULL);
  85. if(filepath[0] != '\0') {
  86. strcpy(bp->b_fname, fname);
  87. modify_buffer_name(bp, 0);
  88. bp->b_fname[0] = '\0';
  89. for (c = 0, ebp = bheadp; ebp != NULL; ebp = ebp->b_next, c++) {
  90. while((match = compare_buffer_name(bp, ebp)) == TRUE && i < 20) {
  91. strcpy(bp->b_fname, fname);
  92. modify_buffer_name(bp, i);
  93. bp->b_fname[0] = '\0';
  94. modify_buffer_name(ebp, i);
  95. i++;
  96. }
  97. if(match) break;
  98. i = 1;
  99. }
  100. bp->b_bname[strlen(bp->b_bname)] = '\0';
  101. for(k = keywords; k->slc != NULL; ++k) {
  102. len = strlen(bp->b_bname);
  103. extlen = strlen(k->extension) - 1;
  104. c = 0;
  105. for(int f = len - 1 - extlen; c <= extlen; f++, c++) {
  106. if(bp->b_bname[f] != k->extension[c]) {
  107. c = 0;
  108. break;
  109. }
  110. }
  111. if(c > 0) {
  112. bp->b_keywords = k;
  113. break;
  114. }
  115. }
  116. }
  117. /* find the place in the list to insert this buffer */
  118. if (bheadp == NULL) {
  119. bheadp = bp;
  120. } else if (strcmp (bheadp->b_fname, fname) > 0) {
  121. /* insert at the beginning */
  122. bp->b_next = bheadp;
  123. bheadp = bp;
  124. } else {
  125. for (sb = bheadp; sb->b_next != NULL; sb = sb->b_next)
  126. if (strcmp (sb->b_next->b_fname, fname) > 0)
  127. break;
  128. /* and insert it */
  129. bp->b_next = sb->b_next;
  130. sb->b_next = bp;
  131. }
  132. }
  133. return bp;
  134. }
  135. /* unlink from the list of buffers, free associated memory, assumes buffer has been saved if modified */
  136. int delete_buffer (buffer_t *bp)
  137. {
  138. buffer_t *sb = NULL;
  139. /* we must have switched to a different buffer first */
  140. assert(bp != curbp);
  141. /* if buffer is the head buffer */
  142. if (bp == bheadp) {
  143. bheadp = bp->b_next;
  144. } else {
  145. /* find place where the bp buffer is next */
  146. for (sb = bheadp; sb->b_next != bp && sb->b_next != NULL; sb = sb->b_next)
  147. ;
  148. assert(sb->b_next == bp || sb->b_next == NULL);
  149. sb->b_next = bp->b_next;
  150. }
  151. if(bp == lastbp) {
  152. lastbp = NULL;
  153. }
  154. /* now we can delete */
  155. free(bp->b_buf);
  156. free(bp);
  157. return TRUE;
  158. }
  159. void last_buffer()
  160. {
  161. buffer_t *t;
  162. disassociate_b(curwp);
  163. if(lastbp == NULL) {
  164. lastbp = curbp;
  165. curbp = curbp->b_next != NULL ? curbp->b_next : bheadp;
  166. } else {
  167. t = curbp;
  168. curbp = lastbp;
  169. lastbp = t;
  170. }
  171. associate_b2w(curbp,curwp);
  172. }
  173. void next_buffer()
  174. {
  175. disassociate_b(curwp);
  176. lastbp = curbp;
  177. curbp = curbp->b_next != NULL ? curbp->b_next : bheadp;
  178. associate_b2w(curbp,curwp);
  179. }
  180. void switch_buffer()
  181. {
  182. buffer_t *next;
  183. int ret = 0;
  184. char message[TEMPBUF] = "Switch to buffer (default ";
  185. assert(curbp != NULL);
  186. assert(bheadp != NULL);
  187. next = lastbp != NULL ? lastbp : curbp->b_next;
  188. if(next == NULL)
  189. next = bheadp;
  190. strcat(message, next->b_bname);
  191. strcat(message, "): ");
  192. next = getbuffername(message, (char*)temp, PATH_MAX, &ret);
  193. if(!ret)
  194. return;
  195. if(next == curbp) {
  196. msg("Same buffer!");
  197. return;
  198. }
  199. if(next == NULL) {
  200. next = lastbp != NULL ? lastbp : curbp->b_next;
  201. if(next == NULL)
  202. next = bheadp;
  203. }
  204. if(next != NULL) {
  205. tb_present();
  206. disassociate_b(curwp);
  207. lastbp = curbp;
  208. curbp = next;
  209. associate_b2w(curbp,curwp);
  210. } else {
  211. msg("Buffer doesn't exist");
  212. }
  213. tb_set_cursor(0, MSGLINE);
  214. clrtoeol("", MSGLINE);
  215. }
  216. char* get_buffer_name(buffer_t *bp)
  217. {
  218. return (strlen(bp->b_fname) > 0) ? bp->b_fname : bp->b_bname;
  219. }
  220. int count_buffers()
  221. {
  222. buffer_t* bp;
  223. int i = 0;
  224. for (i=0, bp=bheadp; bp != NULL; bp = bp->b_next)
  225. i++;
  226. return i;
  227. }
  228. int modified_buffers()
  229. {
  230. buffer_t* bp;
  231. for (bp=bheadp; bp != NULL; bp = bp->b_next)
  232. if (bp->b_flags & B_MODIFIED)
  233. return TRUE;
  234. return FALSE;
  235. }
  236. int compare_buffer_name(buffer_t *bp, buffer_t *ebp)
  237. {
  238. int match = FALSE;
  239. int elen = strlen(ebp->b_bname);
  240. int len = strlen(bp->b_bname);
  241. int longer_name = elen > len ? elen : len;
  242. for(int i = 0; i < longer_name; i++)
  243. if(ebp->b_bname[i] == bp->b_bname[i]) {
  244. match = TRUE;
  245. } else {
  246. match = FALSE;
  247. break;
  248. }
  249. return match;
  250. }
  251. /* Used to either truncate or expand buffer name
  252. flag = 0, truncate to file name only (.mailrc)
  253. flag > 0, truncate to previous directory / file name (i.e. home/.mailrc)
  254. i.e. for a file found at /home/kev/src/ait/README.md, if we had a flag
  255. of 3, we'd see: kev/src/ait/README.md
  256. */
  257. void modify_buffer_name(buffer_t *bp, int flag)
  258. {
  259. char *dir, fname[PATH_MAX + 1];
  260. const char *list_dirs[20];
  261. int d = 0;
  262. strcpy(fname, bp->b_fname);
  263. dir = "\0";
  264. dir = strtok(fname, "/");
  265. while( dir != NULL ) {
  266. list_dirs[d] = dir;
  267. d++;
  268. dir = strtok(NULL, "/");
  269. }
  270. if(flag > 0) {
  271. strcpy(bp->b_bname, list_dirs[d-(flag + 1)]);
  272. strcat(bp->b_bname, "/");
  273. flag--;
  274. for(; flag > 0; flag--) {
  275. strcat(bp->b_bname, list_dirs[d-(flag + 1)]);
  276. strcat(bp->b_bname, "/");
  277. }
  278. }
  279. strcat(bp->b_bname, list_dirs[d-1]);
  280. free(dir);
  281. }