buffer.c 6.3 KB

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