123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254 |
- /* buffer.c, Ait Emacs, Kevin Bloom, Public Domain, 2023 */
- #include <assert.h>
- #include <string.h>
- #include "header.h"
- #include "termbox.h"
- /* list of keywords _must_ end with NULL */
- keywords_t keywords[] = {
- {".c", "////", "/*", "*/", FALSE, FALSE},
- {".h", "////", "/*", "*/", FALSE, FALSE},
- {".js", "////", "/*", "*/", TRUE, TRUE},
- {".jsx", "////", "/*", "*/", TRUE, TRUE},
- {".ts", "////", "/*", "*/", TRUE, TRUE},
- {".tsx", "////", "/*", "*/", TRUE, TRUE},
- {".lisp", ";;", NULL, NULL, FALSE, FALSE},
- {".clj", ";;", NULL, NULL, FALSE, FALSE},
- {".cljs", ";;", NULL, NULL, FALSE, FALSE},
- {".hs", "--", NULL, NULL, FALSE, FALSE},
- {".sh", "#", NULL, NULL, FALSE, FALSE},
- {"NULL", NULL, NULL, NULL, FALSE, FALSE },
- };
- void buffer_init(buffer_t *bp)
- {
- bp->b_mark = NOMARK;
- bp->b_pmark = NOMARK;
- bp->b_point = 0;
- bp->b_cpoint = 0;
- bp->b_page = 0;
- bp->b_epage = 0;
- bp->b_reframe = 0;
- bp->b_size = 0;
- bp->b_psize = 0;
- bp->b_flags = 0;
- bp->b_cnt = 0;
- bp->b_buf = NULL;
- bp->b_ebuf = NULL;
- bp->b_gap = NULL;
- bp->b_egap = NULL;
- bp->b_next = NULL;
- bp->b_fname[0] = '\0';
- bp->b_bname[0] = '\0';
- bp->b_path = TRUE;
- bp->b_undo = malloc(sizeof(undo_t));
- bp->b_eundo = bp->b_undo;
- bp->b_keywords = NULL;
- }
- /* Find a buffer by filename or create if requested */
- buffer_t* find_buffer (char *fname, int cflag)
- {
- buffer_t *bp = NULL;
- buffer_t *ebp = NULL;
- buffer_t *sb = NULL;
- keywords_t *k;
- char filepath[NAME_MAX];
- int len, extlen, c = 0;
- strcpy(filepath, fname);
- bp = bheadp;
- while (bp != NULL) {
- if (strcmp (fname, bp->b_fname) == 0 || strcmp(fname, bp->b_bname) == 0) {
- return (bp);
- }
- bp = bp->b_next;
- }
- if (cflag != FALSE) {
- if ((bp = (buffer_t *) malloc (sizeof (buffer_t))) == NULL)
- return (0);
- buffer_init(bp);
- assert(bp != NULL);
- if(filepath[0] != '\0') {
- strcpy(bp->b_fname, fname);
- modify_buffer_name(bp, 0);
- bp->b_fname[0] = '\0';
- for (c = 0, ebp = bheadp; ebp != NULL; ebp = ebp->b_next, c++) {
- int match = FALSE;
- for(int i = 0; i < strlen(ebp->b_bname); i++)
- if(ebp->b_bname[i] == bp->b_bname[i]) {
- match = TRUE;
- } else {
- match = FALSE;
- break;
- }
- if(match) {
- strcpy(bp->b_fname, fname);
- modify_buffer_name(bp, 1);
- bp->b_fname[0] = '\0';
- modify_buffer_name(ebp, 1);
- break;
- }
- }
- bp->b_bname[strlen(bp->b_bname)] = '\0';
- for(k = keywords; k->slc != NULL; ++k) {
- len = strlen(bp->b_bname);
- extlen = strlen(k->extension) - 1;
- c = 0;
- for(int f = len - 1 - extlen; c <= extlen; f++, c++) {
- if(bp->b_bname[f] != k->extension[c]) {
- c = 0;
- break;
- }
- }
- if(c > 0) {
- bp->b_keywords = k;
- break;
- }
- }
- }
- /* find the place in the list to insert this buffer */
- if (bheadp == NULL) {
- bheadp = bp;
- } else if (strcmp (bheadp->b_fname, fname) > 0) {
- /* insert at the beginning */
- bp->b_next = bheadp;
- bheadp = bp;
- } else {
- for (sb = bheadp; sb->b_next != NULL; sb = sb->b_next)
- if (strcmp (sb->b_next->b_fname, fname) > 0)
- break;
- /* and insert it */
- bp->b_next = sb->b_next;
- sb->b_next = bp;
- }
- }
- return bp;
- }
- /* unlink from the list of buffers, free associated memory, assumes buffer has been saved if modified */
- int delete_buffer (buffer_t *bp)
- {
- buffer_t *sb = NULL;
- /* we must have switched to a different buffer first */
- assert(bp != curbp);
- /* if buffer is the head buffer */
- if (bp == bheadp) {
- bheadp = bp->b_next;
- } else {
- /* find place where the bp buffer is next */
- for (sb = bheadp; sb->b_next != bp && sb->b_next != NULL; sb = sb->b_next)
- ;
- assert(sb->b_next == bp || sb->b_next == NULL);
- sb->b_next = bp->b_next;
- }
- /* now we can delete */
- free(bp->b_buf);
- free(bp);
- return TRUE;
- }
- void next_buffer()
- {
- disassociate_b(curwp);
- curbp = curbp->b_next != NULL ? curbp->b_next : bheadp;
- associate_b2w(curbp,curwp);
- }
- void switch_buffer()
- {
- buffer_t *next;
- int ret = 0;
- char message[TEMPBUF] = "Switch to buffer (default ";
- assert(curbp != NULL);
- assert(bheadp != NULL);
- next = curbp->b_next != NULL ? curbp->b_next : bheadp;
- strcat(message, next->b_bname);
- strcat(message, "): ");
- next = getbuffername(message, (char*)temp, NAME_MAX, &ret);
- if(!ret)
- return;
- if(next == curbp) {
- msg("Same buffer!");
- return;
- }
- if(next == NULL)
- next = curbp->b_next != NULL ? curbp->b_next : bheadp;
- if(next != NULL) {
- tb_present();
- disassociate_b(curwp);
- curbp = next;
- associate_b2w(curbp,curwp);
- } else {
- msg("Buffer doesn't exist");
- }
- }
- char* get_buffer_name(buffer_t *bp)
- {
- return (strlen(bp->b_fname) > 0) ? bp->b_fname : bp->b_bname;
- }
- int count_buffers()
- {
- buffer_t* bp;
- int i = 0;
- for (i=0, bp=bheadp; bp != NULL; bp = bp->b_next)
- i++;
- return i;
- }
- int modified_buffers()
- {
- buffer_t* bp;
- for (bp=bheadp; bp != NULL; bp = bp->b_next)
- if (bp->b_flags & B_MODIFIED)
- return TRUE;
- return FALSE;
- }
- /* Used to either truncate or expand buffer name
- flag = 0, truncate to file name only (.mailrc)
- flag = 1, truncate to previous directory / file name (i.e. home/.mailrc)
- */
- void modify_buffer_name(buffer_t *bp, int flag)
- {
- char *dir, fname[NAME_MAX + 1];
- const char *list_dirs[20];
- strcpy(fname, bp->b_fname);
- int d = 0;
- dir = "\0";
- dir = strtok(fname, "/");
- while( dir != NULL ) {
- list_dirs[d] = dir;
- d++;
- dir = strtok(NULL, "/");
- }
- if(flag) {
- strcpy(bp->b_bname, list_dirs[d-2]);
- strcat(bp->b_bname, "/");
- }
- strcat(bp->b_bname, list_dirs[d-1]);
- free(dir);
- }
|