buffer.c 6.2 KB

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