buffer.c 5.8 KB

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