buffer.c 6.5 KB

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