123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561 |
- /* gap.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
- Derived from: Atto January 2017
- Derived from: Anthony's Editor January 93
- */
- #include <sys/stat.h>
- #include "header.h"
- #include "util.h"
- /* Enlarge gap by n chars, position of gap cannot change */
- int growgap(buffer_t *bp, point_t n)
- {
- char_t *new;
- point_t buflen, newlen, xgap, xegap;
- assert(bp->b_buf <= bp->b_gap);
- assert(bp->b_gap <= bp->b_egap);
- assert(bp->b_egap <= bp->b_ebuf);
- xgap = bp->b_gap - bp->b_buf;
- xegap = bp->b_egap - bp->b_buf;
- buflen = bp->b_ebuf - bp->b_buf;
- /* reduce number of reallocs by growing by a minimum amount */
- n = (n < MIN_GAP_EXPAND ? MIN_GAP_EXPAND : n);
- newlen = buflen + n * sizeof (char_t);
- if (buflen == 0) {
- if (newlen < 0 || MAX_SIZE_T < newlen)
- fatal("%s: Failed to allocate required memory.\n");
- new = (char_t*) malloc((size_t) newlen);
- if (new == NULL)
- fatal("%s: Failed to allocate required memory.\n"); /* Cannot edit a file without a buffer. */
- } else {
- if (newlen < 0 || MAX_SIZE_T < newlen) {
- msg("Failed to allocate required memory");
- return (FALSE);
- }
- new = (char_t*) realloc(bp->b_buf, (size_t) newlen);
- if (new == NULL) {
- msg("Failed to allocate required memory"); /* Report non-fatal error. */
- return (FALSE);
- }
- }
- /* Relocate pointers in new buffer and append the new
- * extension to the end of the gap.
- */
- bp->b_buf = new;
- bp->b_gap = bp->b_buf + xgap;
- bp->b_ebuf = bp->b_buf + buflen;
- bp->b_egap = bp->b_buf + newlen;
- while (xegap < buflen--)
- *--bp->b_egap = *--bp->b_ebuf;
- bp->b_ebuf = bp->b_buf + newlen;
- assert(bp->b_buf < bp->b_ebuf); /* Buffer must exist. */
- assert(bp->b_buf <= bp->b_gap);
- assert(bp->b_gap < bp->b_egap); /* Gap must grow only. */
- assert(bp->b_egap <= bp->b_ebuf);
- return (TRUE);
- }
- point_t movegap(buffer_t *bp, point_t offset)
- {
- char_t *p = ptr(bp, offset);
- while (p < bp->b_gap)
- *--bp->b_egap = *--bp->b_gap;
- while (bp->b_egap < p)
- *bp->b_gap++ = *bp->b_egap++;
- assert(bp->b_gap <= bp->b_egap);
- assert(bp->b_buf <= bp->b_gap);
- assert(bp->b_egap <= bp->b_ebuf);
- return (pos(bp, bp->b_egap));
- }
- /* Given a buffer offset, convert it to a pointer into the buffer */
- char_t * ptr(buffer_t *bp, register point_t offset)
- {
- if (offset < 0)
- return (bp->b_buf);
- return (bp->b_buf+offset + (bp->b_buf + offset < bp->b_gap ? 0 : bp->b_egap-bp->b_gap));
- }
- /* Given a pointer into the buffer, convert it to a buffer offset */
- point_t pos(buffer_t *bp, register char_t *cp)
- {
- assert(bp->b_buf <= cp && cp <= bp->b_ebuf);
- return (cp - bp->b_buf - (cp < bp->b_egap ? 0 : bp->b_egap - bp->b_gap));
- }
- int posix_file(char *fn)
- {
- if (fn[0] == '_')
- return (FALSE);
- for (; *fn != '\0'; ++fn) {
- if (!isalnum(*fn) && *fn != '.' && *fn != '_' && *fn != '-' && *fn != '/')
- return (FALSE);
- }
- return (TRUE);
- }
- void relocate_backup(char *fp)
- {
- char ltemp[NAME_MAX+2];
- strcpy(ltemp, fp);
- replace_all(ltemp, '/', '!');
- strcpy(fp, BACKUP_DIR);
- strcat(fp, ltemp);
- }
- /*
- TODO: make the location of backup files an option. Either put them in the dir
- of the file or in `backup_dir`.
- */
- int backup_file(char *fn)
- {
- FILE *orig, *backup;
- char bfn[NAME_MAX+2];
- char ch;
- strcpy(bfn, fn);
- if(BACKUP_DIR != NULL)
- relocate_backup(bfn);
- strcat(bfn, "~");
- orig = fopen(fn, "r");
- if(orig == NULL) {
- msg("Failed to open original file \"%s\".", fn);
- return -1;
- }
- backup = fopen(bfn, "w");
- if(backup == NULL) {
- msg("Failed to open backup file.");
- return -1;
- }
- while((ch = fgetc(orig)) != EOF)
- fputc(ch, backup);
- fclose(orig);
- fclose(backup);
- return 1;
- }
- int save(char *fn)
- {
- FILE *fp;
- point_t length;
- struct stat sb;
- // if (!posix_file(fn)) {
- // msg("Not a portable POSIX file name.");
- // return (FALSE);
- // }
- stat(fn, &sb);
- if (!backup_file(fn)) {
- msg("Failed to backup file \"%s\".", fn);
- return (FALSE);
- }
- fp = fopen(fn, "w");
- if (fp == NULL) {
- msg("Failed to open file \"%s\".", fn);
- return (FALSE);
- }
- (void) movegap(curbp, (point_t) 0);
- length = (point_t) (curbp->b_ebuf - curbp->b_egap);
- if (fwrite(curbp->b_egap, sizeof (char), (size_t) length, fp) != length) {
- msg("Failed to write file \"%s\".", fn);
- return (FALSE);
- }
- if (fclose(fp) != 0) {
- msg("Failed to close file \"%s\".", fn);
- return (FALSE);
- }
- curbp->b_flags &= ~B_MODIFIED;
- if (stat(fn, &sb) < 0) {
- msg("Failed to find file \"%s\".", fn);
- return (FALSE);
- }
- if (MAX_SIZE_T < sb.st_size) {
- msg("File \"%s\" is too big to load.", fn);
- return (FALSE);
- }
- curbp->b_fmtime = sb.st_mtime;
- msg("File \"%s\" %ld bytes saved.", fn, pos(curbp, curbp->b_ebuf));
- return (TRUE);
- }
- int load_file(char *fn)
- {
- /* reset the gap, make it the whole buffer */
- curbp->b_gap = curbp->b_buf;
- curbp->b_egap = curbp->b_ebuf;
- top();
- return insert_file(fn, FALSE);
- }
- /* reads file into buffer at point */
- int insert_file(char *fn, int modflag)
- {
- FILE *fp;
- size_t len;
- struct stat sb;
- if (stat(fn, &sb) < 0) {
- msg("Failed to find file \"%s\".", fn);
- return (FALSE);
- }
- if (MAX_SIZE_T < sb.st_size) {
- msg("File \"%s\" is too big to load.", fn);
- return (FALSE);
- }
- if (curbp->b_egap - curbp->b_gap < sb.st_size * sizeof (char_t) && !growgap(curbp, sb.st_size))
- return (FALSE);
- if ((fp = fopen(fn, "r")) == NULL) {
- msg("Failed to open file \"%s\".", fn);
- return (FALSE);
- }
- curbp->b_point = movegap(curbp, curbp->b_point);
- // undoset();
- curbp->b_gap += len = fread(curbp->b_gap, sizeof (char), (size_t) sb.st_size, fp);
- if (fclose(fp) != 0) {
- msg("Failed to close file \"%s\".", fn);
- return (FALSE);
- }
- curbp->b_flags &= (modflag ? B_MODIFIED : ~B_MODIFIED);
- curbp->b_fmtime = sb.st_mtime;
- msg("%ld bytes read.", len);
- return (TRUE);
- }
- /* Record a new undo */
- void undoset(int type, int shouldconcat)
- {
- int length = strlen((const char *)input);
- int ulen = 0;
- int npc = 0;
- char_t *p, *temp = NULL;
- undo_t *u = (undo_t *)malloc(sizeof(undo_t));
- assert(u != NULL);
- u->u_type = type;
- u->u_point = curbp->b_point;
- u->u_data = NULL;
- switch(type) {
- case INSERT:
- if(curbp->b_undo != NULL && curbp->b_undo->u_type == INSERT && shouldconcat) {
- ulen = strlen((const char *)curbp->b_undo->u_data);
- temp = (char_t *)(malloc(sizeof(char_t)*ulen));
- strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
- /* if(curbp->b_undo->u_data != NULL) {
- free(curbp->b_undo->u_data);
- curbp->b_undo->u_data = NULL;
- } */
- // memset(curbp->b_undo->u_data, 0, ulen);
- curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(length + ulen)+1));
- strncpy((char *)curbp->b_undo->u_data, (char *)temp, ulen);
- strncat((char *)curbp->b_undo->u_data, (char *)input, length);
- curbp->b_undo->u_data[(length + ulen)] = '\0';
- free(temp);
- return;
- } else {
- u->u_data = (char_t *)(malloc(sizeof(char_t)*length+1));
- strncpy((char *)u->u_data, (char *)input, length);
- u->u_data[length] = '\0';
- }
- break;
- case DELETE:
- npc = utf8_size(*ptr(curbp,curbp->b_point));
- if(curbp->b_undo != NULL && curbp->b_undo->u_type == DELETE && shouldconcat) {
- ulen = strlen((const char *)curbp->b_undo->u_data);
- temp = (char_t *)(malloc(sizeof(char_t)*ulen));
- strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
- /* if(curbp->b_undo->u_data != NULL) {
- free(curbp->b_undo->u_data);
- curbp->b_undo->u_data = NULL;
- } */
- curbp->b_undo->u_data = NULL;
- curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
- // memset(curbp->b_undo->u_data, 0, npc+ulen);
- strncpy((char *)curbp->b_undo->u_data, (char *)temp, ulen);
- strncat((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
- curbp->b_undo->u_data[npc+ulen] = '\0';
- free(temp);
- return;
- } else {
- u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
- strncpy((char *)u->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
- u->u_data[npc] = '\0';
- }
- break;
- case BACKSP:
- npc = prev_utf8_char_size();
- if(curbp->b_undo != NULL && curbp->b_undo->u_type == BACKSP && shouldconcat) {
- curbp->b_undo->u_point -= npc;
- ulen = strlen((const char *)curbp->b_undo->u_data);
- temp = (char_t *)(malloc(sizeof(char_t)*ulen));
- strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
- /* if(curbp->b_undo->u_data != NULL) {
- free(curbp->b_undo->u_data);
- curbp->b_undo->u_data = NULL;
- } */
- curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
- // memset(curbp->b_undo->u_data, 0, 1+ulen);
- strncpy((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_undo->u_point), npc);
- strncat((char *)curbp->b_undo->u_data, (char *)temp, ulen);
- curbp->b_undo->u_data[(npc + ulen)] = '\0';
- if(temp != NULL)
- free(temp);
- return;
- } else {
- u->u_point = curbp->b_point - npc;
- u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
- strncpy((char *)u->u_data, (const char *)ptr(curbp, u->u_point), npc);
- u->u_data[npc] = '\0';
- }
- break;
- case CUT:
- if (curbp->b_point < curbp->b_mark) {
- (void) movegap(curbp, curbp->b_point);
- p = ptr(curbp, curbp->b_point);
- length = curbp->b_mark - curbp->b_point;
- } else {
- (void) movegap(curbp, curbp->b_mark);
- p = ptr(curbp, curbp->b_mark);
- length = curbp->b_point - curbp->b_mark;
- u->u_point = curbp->b_mark;
- }
- u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
- (void) memcpy(u->u_data, p, length * sizeof (char_t));
- u->u_data[length] = '\0';
- break;
- case YANK:
- u->u_data = (char_t*) malloc(sizeof(char_t)*nscrap+1);
- (void) memcpy(u->u_data, scrap, nscrap * sizeof (char_t));
- u->u_data[nscrap] = '\0';
- break;
- case REPLACE:
- (void) movegap(curbp, curbp->b_point);
- p = ptr(curbp, curbp->b_point);
- length = curbp->b_mark - curbp->b_point;
- u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
- (void) memcpy(u->u_data, p, length * sizeof (char_t));
- u->u_data[length] = '\0';
- u->u_size = shouldconcat;
- }
- u->u_next = curbp->b_undo;
- curbp->b_undo = u;
- curbp->b_redo = NULL;
- }
- /* Record a new redo if you've just undid or a undo if you've just redid. */
- void redo_or_undo_set(undo_t *up, int datalen, int isundo)
- {
- undo_t *rp = (undo_t *)malloc(sizeof(undo_t));
- char_t *p;
- assert(rp != NULL);
- rp->u_point = up->u_point;
- switch(up->u_type) {
- case INSERT:
- rp->u_type = DELETE;
- break;
- case BACKSP:
- case DELETE:
- rp->u_type = INSERT;
- break;
- case CUT:
- rp->u_type = YANK;
- break;
- case YANK:
- rp->u_type = CUT;
- break;
- case REPLACE:
- rp->u_type = REPLACE;
- (void) movegap(curbp, up->u_point);
- p = ptr(curbp, up->u_point);
- rp->u_data = (char_t*) malloc(sizeof(char_t)*up->u_size+1);
- (void) memcpy(rp->u_data, p, up->u_size * sizeof (char_t));
- rp->u_data[up->u_size] = '\0';
- rp->u_size = datalen;
- break;
- }
- /* if(rp != NULL && rp->u_data != NULL && rp->u_type != REPLACE) {
- free(rp->u_data);
- rp->u_data = NULL;
- } */
- if(rp->u_type != REPLACE) {
- rp->u_data = (char_t *) calloc(datalen+1, sizeof(char_t));
- (void) memcpy(rp->u_data, up->u_data, datalen * sizeof (char_t));
- rp->u_data[datalen] = '\0';
- }
- /* if an undo was done, save to redo */
- if(isundo) {
- rp->u_next = curbp->b_redo;
- curbp->b_redo = rp;
- } else {
- rp->u_next = curbp->b_undo;
- curbp->b_undo = rp;
- }
- }
- /* Undo */
- void undo_or_redo(buffer_t *bp, undo_t *up, int isundo)
- {
- int n = 0;
- currentcommand = KBD_DEFAULT;
- if(up == NULL) {
- if(isundo)
- msg("Nothing to undo!");
- else
- msg("Nothing to redo!");
- return;
- }
- bp->b_point = up->u_point;
- n = strlen((const char *)up->u_data);
- redo_or_undo_set(up, n, isundo);
- switch(up->u_type) {
- case INSERT:
- case YANK:
- (void) movegap(bp, bp->b_point);
- bp->b_egap += n;
- bp->b_point = pos(bp, bp->b_egap);
- break;
- case BACKSP:
- case DELETE:
- case CUT:
- bp->b_point = movegap(bp, bp->b_point);
- memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
- bp->b_gap += n;
- bp->b_point = pos(bp, bp->b_egap);
- break;
- case REPLACE:
- (void) movegap(bp, bp->b_point);
- bp->b_egap += up->u_size;
- bp->b_point = pos(bp, bp->b_egap);
- bp->b_point = movegap(bp, bp->b_point);
- memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
- bp->b_gap += n;
- bp->b_point = pos(bp, bp->b_egap);
- break;
- }
- bp->b_point = movegap(bp, up->u_point);
- bp->b_flags |= B_MODIFIED;
- if(isundo)
- bp->b_undo = up->u_next;
- else
- bp->b_redo = up->u_next;
- if(curbp->b_point < curbp->b_page || curbp->b_point > curbp->b_epage)
- bp->b_reframe = 1;
- }
- void undo()
- {
- undo_or_redo(curbp, curbp->b_undo, TRUE);
- }
- /* Redo */
- void redo()
- {
- undo_or_redo(curbp, curbp->b_redo, FALSE);
- }
- /* find the point for start of line ln */
- point_t line_to_point(int ln)
- {
- point_t end_p = pos(curbp, curbp->b_ebuf);
- point_t p, start;
- for (p=0, start=0; p <= end_p; p++) {
- char_t *c = ptr(curbp, p);
- if(c == 0)
- break;
- if ( *c == '\n') {
- if (--ln == 0)
- return start;
- if (p + 1 <= end_p)
- start = p + 1;
- }
- if(!*c && ln == 1)
- return start;
- }
- return -1;
- }
- /* scan buffer and fill in curline and lastline */
- void get_line_stats(int *curline, int *lastline, buffer_t *bp)
- {
- point_t end_p = pos(bp, bp->b_ebuf);
- point_t p;
- int line;
- *curline = -1;
- for (p=0, line=0; p < end_p; p++) {
- line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
- *lastline = line;
- if (*curline == -1 && p == bp->b_point) {
- *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
- }
- }
- *lastline = *lastline + 1;
- if (bp->b_point == end_p)
- *curline = *lastline;
- }
- /* Return TRUE if file was modified after the current buffer's recored mtime. */
- int is_file_modified(char *fn)
- {
- struct stat sb;
- if(stat(fn, &sb) < 0) {
- return (FALSE);
- }
- if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
- return (TRUE);
- }
- return (FALSE);
- }
- /* Return TRUE to continue, FALSE to stop. Revert happens here. */
- int file_was_modified_prompt()
- {
- const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
- char c = '\0';
- point_t p = curbp->b_point;
- struct stat sb;
- print_to_msgline(prompt);
- clrtoeol(prompt, MSGLINE);
- c = yesnomaybeso('n');
- if (c == 'n') {
- clrtoeol("", MSGLINE);
- return FALSE;
- } else if(c == 'r') {
- curbp->b_point = 0;
- load_file(curbp->b_fname);
- clrtoeol("", MSGLINE);
- curbp->b_point = p;
- msg("Buffer reverted.");
- return FALSE;
- } else if(c == 'y') {
- clrtoeol("", MSGLINE);
- if (stat(curbp->b_fname, &sb) < 0) {
- msg("Failed to find file \"%s\".", curbp->b_fname);
- return (FALSE);
- }
- if (MAX_SIZE_T < sb.st_size) {
- msg("File \"%s\" is too big to load.", curbp->b_fname);
- return (FALSE);
- }
- curbp->b_fmtime = sb.st_mtime;
- return TRUE;
- }
- clrtoeol("", MSGLINE);
- return (FALSE);
- }
|