gap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  1. /* gap.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
  2. Derived from: Atto January 2017
  3. Derived from: Anthony's Editor January 93
  4. */
  5. #include <sys/stat.h>
  6. #include "header.h"
  7. #include "util.h"
  8. /* Enlarge gap by n chars, position of gap cannot change */
  9. int growgap(buffer_t *bp, point_t n)
  10. {
  11. char_t *new;
  12. point_t buflen, newlen, xgap, xegap;
  13. assert(bp->b_buf <= bp->b_gap);
  14. assert(bp->b_gap <= bp->b_egap);
  15. assert(bp->b_egap <= bp->b_ebuf);
  16. xgap = bp->b_gap - bp->b_buf;
  17. xegap = bp->b_egap - bp->b_buf;
  18. buflen = bp->b_ebuf - bp->b_buf;
  19. /* reduce number of reallocs by growing by a minimum amount */
  20. n = (n < MIN_GAP_EXPAND ? MIN_GAP_EXPAND : n);
  21. newlen = buflen + n * sizeof (char_t);
  22. if (buflen == 0) {
  23. if (newlen < 0 || MAX_SIZE_T < newlen)
  24. fatal("%s: Failed to allocate required memory.\n");
  25. new = (char_t*) malloc((size_t) newlen);
  26. if (new == NULL)
  27. fatal("%s: Failed to allocate required memory.\n"); /* Cannot edit a file without a buffer. */
  28. } else {
  29. if (newlen < 0 || MAX_SIZE_T < newlen) {
  30. msg("Failed to allocate required memory");
  31. return (FALSE);
  32. }
  33. new = (char_t*) realloc(bp->b_buf, (size_t) newlen);
  34. if (new == NULL) {
  35. msg("Failed to allocate required memory"); /* Report non-fatal error. */
  36. return (FALSE);
  37. }
  38. }
  39. /* Relocate pointers in new buffer and append the new
  40. * extension to the end of the gap.
  41. */
  42. bp->b_buf = new;
  43. bp->b_gap = bp->b_buf + xgap;
  44. bp->b_ebuf = bp->b_buf + buflen;
  45. bp->b_egap = bp->b_buf + newlen;
  46. while (xegap < buflen--)
  47. *--bp->b_egap = *--bp->b_ebuf;
  48. bp->b_ebuf = bp->b_buf + newlen;
  49. assert(bp->b_buf < bp->b_ebuf); /* Buffer must exist. */
  50. assert(bp->b_buf <= bp->b_gap);
  51. assert(bp->b_gap < bp->b_egap); /* Gap must grow only. */
  52. assert(bp->b_egap <= bp->b_ebuf);
  53. return (TRUE);
  54. }
  55. point_t movegap(buffer_t *bp, point_t offset)
  56. {
  57. char_t *p = ptr(bp, offset);
  58. while (p < bp->b_gap)
  59. *--bp->b_egap = *--bp->b_gap;
  60. while (bp->b_egap < p)
  61. *bp->b_gap++ = *bp->b_egap++;
  62. assert(bp->b_gap <= bp->b_egap);
  63. assert(bp->b_buf <= bp->b_gap);
  64. assert(bp->b_egap <= bp->b_ebuf);
  65. return (pos(bp, bp->b_egap));
  66. }
  67. /* Given a buffer offset, convert it to a pointer into the buffer */
  68. char_t * ptr(buffer_t *bp, register point_t offset)
  69. {
  70. if (offset < 0)
  71. return (bp->b_buf);
  72. return (bp->b_buf+offset + (bp->b_buf + offset < bp->b_gap ? 0 : bp->b_egap-bp->b_gap));
  73. }
  74. /* Given a pointer into the buffer, convert it to a buffer offset */
  75. point_t pos(buffer_t *bp, register char_t *cp)
  76. {
  77. assert(bp->b_buf <= cp && cp <= bp->b_ebuf);
  78. return (cp - bp->b_buf - (cp < bp->b_egap ? 0 : bp->b_egap - bp->b_gap));
  79. }
  80. int posix_file(char *fn)
  81. {
  82. if (fn[0] == '_')
  83. return (FALSE);
  84. for (; *fn != '\0'; ++fn) {
  85. if (!isalnum(*fn) && *fn != '.' && *fn != '_' && *fn != '-' && *fn != '/')
  86. return (FALSE);
  87. }
  88. return (TRUE);
  89. }
  90. void relocate_backup(char *fp)
  91. {
  92. char ltemp[NAME_MAX+2];
  93. strcpy(ltemp, fp);
  94. replace_all(ltemp, '/', '!');
  95. strcpy(fp, BACKUP_DIR);
  96. strcat(fp, ltemp);
  97. }
  98. /*
  99. TODO: make the location of backup files an option. Either put them in the dir
  100. of the file or in `backup_dir`.
  101. */
  102. int backup_file(char *fn)
  103. {
  104. FILE *orig, *backup;
  105. char bfn[NAME_MAX+2];
  106. char ch;
  107. strcpy(bfn, fn);
  108. if(BACKUP_DIR != NULL)
  109. relocate_backup(bfn);
  110. strcat(bfn, "~");
  111. orig = fopen(fn, "r");
  112. if(orig == NULL) {
  113. msg("Failed to open original file \"%s\".", fn);
  114. return -1;
  115. }
  116. backup = fopen(bfn, "w");
  117. if(backup == NULL) {
  118. msg("Failed to open backup file.");
  119. return -1;
  120. }
  121. while((ch = fgetc(orig)) != EOF)
  122. fputc(ch, backup);
  123. fclose(orig);
  124. fclose(backup);
  125. return 1;
  126. }
  127. int save(char *fn)
  128. {
  129. FILE *fp;
  130. point_t length;
  131. struct stat sb;
  132. // if (!posix_file(fn)) {
  133. // msg("Not a portable POSIX file name.");
  134. // return (FALSE);
  135. // }
  136. stat(fn, &sb);
  137. if (!backup_file(fn)) {
  138. msg("Failed to backup file \"%s\".", fn);
  139. return (FALSE);
  140. }
  141. fp = fopen(fn, "w");
  142. if (fp == NULL) {
  143. msg("Failed to open file \"%s\".", fn);
  144. return (FALSE);
  145. }
  146. (void) movegap(curbp, (point_t) 0);
  147. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  148. if (fwrite(curbp->b_egap, sizeof (char), (size_t) length, fp) != length) {
  149. msg("Failed to write file \"%s\".", fn);
  150. return (FALSE);
  151. }
  152. if (fclose(fp) != 0) {
  153. msg("Failed to close file \"%s\".", fn);
  154. return (FALSE);
  155. }
  156. curbp->b_flags &= ~B_MODIFIED;
  157. if (stat(fn, &sb) < 0) {
  158. msg("Failed to find file \"%s\".", fn);
  159. return (FALSE);
  160. }
  161. if (MAX_SIZE_T < sb.st_size) {
  162. msg("File \"%s\" is too big to load.", fn);
  163. return (FALSE);
  164. }
  165. curbp->b_fmtime = sb.st_mtime;
  166. msg("File \"%s\" %ld bytes saved.", fn, pos(curbp, curbp->b_ebuf));
  167. return (TRUE);
  168. }
  169. int load_file(char *fn)
  170. {
  171. /* reset the gap, make it the whole buffer */
  172. curbp->b_gap = curbp->b_buf;
  173. curbp->b_egap = curbp->b_ebuf;
  174. top();
  175. return insert_file(fn, FALSE);
  176. }
  177. /* reads file into buffer at point */
  178. int insert_file(char *fn, int modflag)
  179. {
  180. FILE *fp;
  181. size_t len;
  182. struct stat sb;
  183. if (stat(fn, &sb) < 0) {
  184. msg("Failed to find file \"%s\".", fn);
  185. return (FALSE);
  186. }
  187. if (MAX_SIZE_T < sb.st_size) {
  188. msg("File \"%s\" is too big to load.", fn);
  189. return (FALSE);
  190. }
  191. if (curbp->b_egap - curbp->b_gap < sb.st_size * sizeof (char_t) && !growgap(curbp, sb.st_size))
  192. return (FALSE);
  193. if ((fp = fopen(fn, "r")) == NULL) {
  194. msg("Failed to open file \"%s\".", fn);
  195. return (FALSE);
  196. }
  197. curbp->b_point = movegap(curbp, curbp->b_point);
  198. // undoset();
  199. curbp->b_gap += len = fread(curbp->b_gap, sizeof (char), (size_t) sb.st_size, fp);
  200. if (fclose(fp) != 0) {
  201. msg("Failed to close file \"%s\".", fn);
  202. return (FALSE);
  203. }
  204. curbp->b_flags &= (modflag ? B_MODIFIED : ~B_MODIFIED);
  205. curbp->b_fmtime = sb.st_mtime;
  206. msg("%ld bytes read.", len);
  207. return (TRUE);
  208. }
  209. /* Record a new undo */
  210. void undoset(int type, int shouldconcat)
  211. {
  212. int length = strlen((const char *)input);
  213. int ulen = 0;
  214. int npc = 0;
  215. char_t *p, *temp = NULL;
  216. undo_t *u = (undo_t *)malloc(sizeof(undo_t));
  217. assert(u != NULL);
  218. u->u_type = type;
  219. u->u_point = curbp->b_point;
  220. u->u_data = NULL;
  221. switch(type) {
  222. case INSERT:
  223. if(curbp->b_undo != NULL && curbp->b_undo->u_type == INSERT && shouldconcat) {
  224. ulen = strlen((const char *)curbp->b_undo->u_data);
  225. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  226. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  227. /* if(curbp->b_undo->u_data != NULL) {
  228. free(curbp->b_undo->u_data);
  229. curbp->b_undo->u_data = NULL;
  230. } */
  231. // memset(curbp->b_undo->u_data, 0, ulen);
  232. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(length + ulen)+1));
  233. strncpy((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  234. strncat((char *)curbp->b_undo->u_data, (char *)input, length);
  235. curbp->b_undo->u_data[(length + ulen)] = '\0';
  236. free(temp);
  237. return;
  238. } else {
  239. u->u_data = (char_t *)(malloc(sizeof(char_t)*length+1));
  240. strncpy((char *)u->u_data, (char *)input, length);
  241. u->u_data[length] = '\0';
  242. }
  243. break;
  244. case DELETE:
  245. npc = utf8_size(*ptr(curbp,curbp->b_point));
  246. if(curbp->b_undo != NULL && curbp->b_undo->u_type == DELETE && shouldconcat) {
  247. ulen = strlen((const char *)curbp->b_undo->u_data);
  248. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  249. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  250. /* if(curbp->b_undo->u_data != NULL) {
  251. free(curbp->b_undo->u_data);
  252. curbp->b_undo->u_data = NULL;
  253. } */
  254. curbp->b_undo->u_data = NULL;
  255. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
  256. // memset(curbp->b_undo->u_data, 0, npc+ulen);
  257. strncpy((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  258. strncat((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  259. curbp->b_undo->u_data[npc+ulen] = '\0';
  260. free(temp);
  261. return;
  262. } else {
  263. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  264. strncpy((char *)u->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  265. u->u_data[npc] = '\0';
  266. }
  267. break;
  268. case BACKSP:
  269. npc = prev_utf8_char_size();
  270. if(curbp->b_undo != NULL && curbp->b_undo->u_type == BACKSP && shouldconcat) {
  271. curbp->b_undo->u_point -= npc;
  272. ulen = strlen((const char *)curbp->b_undo->u_data);
  273. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  274. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  275. /* if(curbp->b_undo->u_data != NULL) {
  276. free(curbp->b_undo->u_data);
  277. curbp->b_undo->u_data = NULL;
  278. } */
  279. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
  280. // memset(curbp->b_undo->u_data, 0, 1+ulen);
  281. strncpy((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_undo->u_point), npc);
  282. strncat((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  283. curbp->b_undo->u_data[(npc + ulen)] = '\0';
  284. if(temp != NULL)
  285. free(temp);
  286. return;
  287. } else {
  288. u->u_point = curbp->b_point - npc;
  289. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  290. strncpy((char *)u->u_data, (const char *)ptr(curbp, u->u_point), npc);
  291. u->u_data[npc] = '\0';
  292. }
  293. break;
  294. case CUT:
  295. if (curbp->b_point < curbp->b_mark) {
  296. (void) movegap(curbp, curbp->b_point);
  297. p = ptr(curbp, curbp->b_point);
  298. length = curbp->b_mark - curbp->b_point;
  299. } else {
  300. (void) movegap(curbp, curbp->b_mark);
  301. p = ptr(curbp, curbp->b_mark);
  302. length = curbp->b_point - curbp->b_mark;
  303. u->u_point = curbp->b_mark;
  304. }
  305. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  306. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  307. u->u_data[length] = '\0';
  308. break;
  309. case YANK:
  310. u->u_data = (char_t*) malloc(sizeof(char_t)*nscrap+1);
  311. (void) memcpy(u->u_data, scrap, nscrap * sizeof (char_t));
  312. u->u_data[nscrap] = '\0';
  313. break;
  314. case REPLACE:
  315. (void) movegap(curbp, curbp->b_point);
  316. p = ptr(curbp, curbp->b_point);
  317. length = curbp->b_mark - curbp->b_point;
  318. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  319. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  320. u->u_data[length] = '\0';
  321. u->u_size = shouldconcat;
  322. }
  323. u->u_next = curbp->b_undo;
  324. curbp->b_undo = u;
  325. curbp->b_redo = NULL;
  326. }
  327. /* Record a new redo if you've just undid or a undo if you've just redid. */
  328. void redo_or_undo_set(undo_t *up, int datalen, int isundo)
  329. {
  330. undo_t *rp = (undo_t *)malloc(sizeof(undo_t));
  331. char_t *p;
  332. assert(rp != NULL);
  333. rp->u_point = up->u_point;
  334. switch(up->u_type) {
  335. case INSERT:
  336. rp->u_type = DELETE;
  337. break;
  338. case BACKSP:
  339. case DELETE:
  340. rp->u_type = INSERT;
  341. break;
  342. case CUT:
  343. rp->u_type = YANK;
  344. break;
  345. case YANK:
  346. rp->u_type = CUT;
  347. break;
  348. case REPLACE:
  349. rp->u_type = REPLACE;
  350. (void) movegap(curbp, up->u_point);
  351. p = ptr(curbp, up->u_point);
  352. rp->u_data = (char_t*) malloc(sizeof(char_t)*up->u_size+1);
  353. (void) memcpy(rp->u_data, p, up->u_size * sizeof (char_t));
  354. rp->u_data[up->u_size] = '\0';
  355. rp->u_size = datalen;
  356. break;
  357. }
  358. /* if(rp != NULL && rp->u_data != NULL && rp->u_type != REPLACE) {
  359. free(rp->u_data);
  360. rp->u_data = NULL;
  361. } */
  362. if(rp->u_type != REPLACE) {
  363. rp->u_data = (char_t *) calloc(datalen+1, sizeof(char_t));
  364. (void) memcpy(rp->u_data, up->u_data, datalen * sizeof (char_t));
  365. rp->u_data[datalen] = '\0';
  366. }
  367. /* if an undo was done, save to redo */
  368. if(isundo) {
  369. rp->u_next = curbp->b_redo;
  370. curbp->b_redo = rp;
  371. } else {
  372. rp->u_next = curbp->b_undo;
  373. curbp->b_undo = rp;
  374. }
  375. }
  376. /* Undo */
  377. void undo_or_redo(buffer_t *bp, undo_t *up, int isundo)
  378. {
  379. int n = 0;
  380. currentcommand = KBD_DEFAULT;
  381. if(up == NULL) {
  382. if(isundo)
  383. msg("Nothing to undo!");
  384. else
  385. msg("Nothing to redo!");
  386. return;
  387. }
  388. bp->b_point = up->u_point;
  389. n = strlen((const char *)up->u_data);
  390. redo_or_undo_set(up, n, isundo);
  391. switch(up->u_type) {
  392. case INSERT:
  393. case YANK:
  394. (void) movegap(bp, bp->b_point);
  395. bp->b_egap += n;
  396. bp->b_point = pos(bp, bp->b_egap);
  397. break;
  398. case BACKSP:
  399. case DELETE:
  400. case CUT:
  401. bp->b_point = movegap(bp, bp->b_point);
  402. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  403. bp->b_gap += n;
  404. bp->b_point = pos(bp, bp->b_egap);
  405. break;
  406. case REPLACE:
  407. (void) movegap(bp, bp->b_point);
  408. bp->b_egap += up->u_size;
  409. bp->b_point = pos(bp, bp->b_egap);
  410. bp->b_point = movegap(bp, bp->b_point);
  411. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  412. bp->b_gap += n;
  413. bp->b_point = pos(bp, bp->b_egap);
  414. break;
  415. }
  416. bp->b_point = movegap(bp, up->u_point);
  417. bp->b_flags |= B_MODIFIED;
  418. if(isundo)
  419. bp->b_undo = up->u_next;
  420. else
  421. bp->b_redo = up->u_next;
  422. if(curbp->b_point < curbp->b_page || curbp->b_point > curbp->b_epage)
  423. bp->b_reframe = 1;
  424. }
  425. void undo()
  426. {
  427. undo_or_redo(curbp, curbp->b_undo, TRUE);
  428. }
  429. /* Redo */
  430. void redo()
  431. {
  432. undo_or_redo(curbp, curbp->b_redo, FALSE);
  433. }
  434. /* find the point for start of line ln */
  435. point_t line_to_point(int ln)
  436. {
  437. point_t end_p = pos(curbp, curbp->b_ebuf);
  438. point_t p, start;
  439. for (p=0, start=0; p <= end_p; p++) {
  440. char_t *c = ptr(curbp, p);
  441. if(c == 0)
  442. break;
  443. if ( *c == '\n') {
  444. if (--ln == 0)
  445. return start;
  446. if (p + 1 <= end_p)
  447. start = p + 1;
  448. }
  449. if(!*c && ln == 1)
  450. return start;
  451. }
  452. return -1;
  453. }
  454. /* scan buffer and fill in curline and lastline */
  455. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  456. {
  457. point_t end_p = pos(bp, bp->b_ebuf);
  458. point_t p;
  459. int line;
  460. *curline = -1;
  461. for (p=0, line=0; p < end_p; p++) {
  462. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  463. *lastline = line;
  464. if (*curline == -1 && p == bp->b_point) {
  465. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  466. }
  467. }
  468. *lastline = *lastline + 1;
  469. if (bp->b_point == end_p)
  470. *curline = *lastline;
  471. }
  472. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  473. int is_file_modified(char *fn)
  474. {
  475. struct stat sb;
  476. if(stat(fn, &sb) < 0) {
  477. return (FALSE);
  478. }
  479. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  480. return (TRUE);
  481. }
  482. return (FALSE);
  483. }
  484. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  485. int file_was_modified_prompt()
  486. {
  487. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  488. char c = '\0';
  489. point_t p = curbp->b_point;
  490. struct stat sb;
  491. print_to_msgline(prompt);
  492. clrtoeol(prompt, MSGLINE);
  493. c = yesnomaybeso('n');
  494. if (c == 'n') {
  495. clrtoeol("", MSGLINE);
  496. return FALSE;
  497. } else if(c == 'r') {
  498. curbp->b_point = 0;
  499. load_file(curbp->b_fname);
  500. clrtoeol("", MSGLINE);
  501. curbp->b_point = p;
  502. msg("Buffer reverted.");
  503. return FALSE;
  504. } else if(c == 'y') {
  505. clrtoeol("", MSGLINE);
  506. if (stat(curbp->b_fname, &sb) < 0) {
  507. msg("Failed to find file \"%s\".", curbp->b_fname);
  508. return (FALSE);
  509. }
  510. if (MAX_SIZE_T < sb.st_size) {
  511. msg("File \"%s\" is too big to load.", curbp->b_fname);
  512. return (FALSE);
  513. }
  514. curbp->b_fmtime = sb.st_mtime;
  515. return TRUE;
  516. }
  517. clrtoeol("", MSGLINE);
  518. return (FALSE);
  519. }