gap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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_line = curbp->b_line;
  221. u->u_data = NULL;
  222. switch(type) {
  223. case INSERT:
  224. if(curbp->b_undo != NULL && curbp->b_undo->u_type == INSERT && shouldconcat) {
  225. ulen = strlen((const char *)curbp->b_undo->u_data);
  226. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  227. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  228. /* if(curbp->b_undo->u_data != NULL) {
  229. free(curbp->b_undo->u_data);
  230. curbp->b_undo->u_data = NULL;
  231. } */
  232. // memset(curbp->b_undo->u_data, 0, ulen);
  233. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(length + ulen)+1));
  234. strncpy((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  235. strncat((char *)curbp->b_undo->u_data, (char *)input, length);
  236. curbp->b_undo->u_data[(length + ulen)] = '\0';
  237. free(temp);
  238. return;
  239. } else {
  240. u->u_data = (char_t *)(malloc(sizeof(char_t)*length+1));
  241. strncpy((char *)u->u_data, (char *)input, length);
  242. u->u_data[length] = '\0';
  243. }
  244. break;
  245. case DELETE:
  246. npc = utf8_size(*ptr(curbp,curbp->b_point));
  247. if(curbp->b_undo != NULL && curbp->b_undo->u_type == DELETE && shouldconcat) {
  248. ulen = strlen((const char *)curbp->b_undo->u_data);
  249. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  250. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  251. /* if(curbp->b_undo->u_data != NULL) {
  252. free(curbp->b_undo->u_data);
  253. curbp->b_undo->u_data = NULL;
  254. } */
  255. curbp->b_undo->u_data = NULL;
  256. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
  257. // memset(curbp->b_undo->u_data, 0, npc+ulen);
  258. strncpy((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  259. strncat((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  260. curbp->b_undo->u_data[npc+ulen] = '\0';
  261. free(temp);
  262. return;
  263. } else {
  264. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  265. strncpy((char *)u->u_data, (const char *)ptr(curbp, curbp->b_point), npc);
  266. u->u_data[npc] = '\0';
  267. }
  268. break;
  269. case BACKSP:
  270. npc = prev_utf8_char_size();
  271. if(curbp->b_undo != NULL && curbp->b_undo->u_type == BACKSP && shouldconcat) {
  272. curbp->b_undo->u_point -= npc;
  273. ulen = strlen((const char *)curbp->b_undo->u_data);
  274. temp = (char_t *)(malloc(sizeof(char_t)*ulen));
  275. strncpy((char *)temp, (char *)curbp->b_undo->u_data, ulen);
  276. /* if(curbp->b_undo->u_data != NULL) {
  277. free(curbp->b_undo->u_data);
  278. curbp->b_undo->u_data = NULL;
  279. } */
  280. curbp->b_undo->u_data = (char_t *)(malloc(sizeof(char_t)*(npc + ulen)+1));
  281. // memset(curbp->b_undo->u_data, 0, 1+ulen);
  282. strncpy((char *)curbp->b_undo->u_data, (const char *)ptr(curbp, curbp->b_undo->u_point), npc);
  283. strncat((char *)curbp->b_undo->u_data, (char *)temp, ulen);
  284. curbp->b_undo->u_data[(npc + ulen)] = '\0';
  285. if(temp != NULL)
  286. free(temp);
  287. return;
  288. } else {
  289. u->u_point = curbp->b_point - npc;
  290. u->u_data = (char_t *)(malloc(sizeof(char_t)+npc));
  291. strncpy((char *)u->u_data, (const char *)ptr(curbp, u->u_point), npc);
  292. u->u_data[npc] = '\0';
  293. }
  294. break;
  295. case CUT:
  296. if (curbp->b_point < curbp->b_mark) {
  297. (void) movegap(curbp, curbp->b_point);
  298. p = ptr(curbp, curbp->b_point);
  299. length = curbp->b_mark - curbp->b_point;
  300. } else {
  301. (void) movegap(curbp, curbp->b_mark);
  302. p = ptr(curbp, curbp->b_mark);
  303. length = curbp->b_point - curbp->b_mark;
  304. u->u_point = curbp->b_mark;
  305. }
  306. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  307. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  308. u->u_data[length] = '\0';
  309. break;
  310. case YANK:
  311. u->u_data = (char_t*) malloc(sizeof(char_t)*nscrap+1);
  312. (void) memcpy(u->u_data, scrap, nscrap * sizeof (char_t));
  313. u->u_data[nscrap] = '\0';
  314. break;
  315. case REPLACE:
  316. (void) movegap(curbp, curbp->b_point);
  317. p = ptr(curbp, curbp->b_point);
  318. length = curbp->b_mark - curbp->b_point;
  319. u->u_data = (char_t*) malloc(sizeof(char_t)*length+1);
  320. (void) memcpy(u->u_data, p, length * sizeof (char_t));
  321. u->u_data[length] = '\0';
  322. u->u_size = shouldconcat;
  323. }
  324. u->u_next = curbp->b_undo;
  325. curbp->b_undo = u;
  326. curbp->b_redo = NULL;
  327. }
  328. /* Record a new redo if you've just undid or a undo if you've just redid. */
  329. void redo_or_undo_set(undo_t *up, int datalen, int isundo)
  330. {
  331. undo_t *rp = (undo_t *)malloc(sizeof(undo_t));
  332. char_t *p;
  333. assert(rp != NULL);
  334. rp->u_point = up->u_point;
  335. rp->u_line = up->u_line;
  336. switch(up->u_type) {
  337. case INSERT:
  338. rp->u_type = DELETE;
  339. break;
  340. case BACKSP:
  341. case DELETE:
  342. rp->u_type = INSERT;
  343. break;
  344. case CUT:
  345. rp->u_type = YANK;
  346. break;
  347. case YANK:
  348. rp->u_type = CUT;
  349. break;
  350. case REPLACE:
  351. rp->u_type = REPLACE;
  352. (void) movegap(curbp, up->u_point);
  353. p = ptr(curbp, up->u_point);
  354. rp->u_data = (char_t*) malloc(sizeof(char_t)*up->u_size+1);
  355. (void) memcpy(rp->u_data, p, up->u_size * sizeof (char_t));
  356. rp->u_data[up->u_size] = '\0';
  357. rp->u_size = datalen;
  358. break;
  359. }
  360. /* if(rp != NULL && rp->u_data != NULL && rp->u_type != REPLACE) {
  361. free(rp->u_data);
  362. rp->u_data = NULL;
  363. } */
  364. if(rp->u_type != REPLACE) {
  365. rp->u_data = (char_t *) calloc(datalen+1, sizeof(char_t));
  366. (void) memcpy(rp->u_data, up->u_data, datalen * sizeof (char_t));
  367. rp->u_data[datalen] = '\0';
  368. }
  369. /* if an undo was done, save to redo */
  370. if(isundo) {
  371. rp->u_next = curbp->b_redo;
  372. curbp->b_redo = rp;
  373. } else {
  374. rp->u_next = curbp->b_undo;
  375. curbp->b_undo = rp;
  376. }
  377. }
  378. /* Undo */
  379. void undo_or_redo(buffer_t *bp, undo_t *up, int isundo)
  380. {
  381. int n = 0;
  382. currentcommand = KBD_UNDO;
  383. if(up == NULL) {
  384. if(isundo)
  385. msg("Nothing to undo!");
  386. else
  387. msg("Nothing to redo!");
  388. return;
  389. }
  390. bp->b_point = up->u_point;
  391. bp->b_line = up->u_line;
  392. n = strlen((const char *)up->u_data);
  393. redo_or_undo_set(up, n, isundo);
  394. switch(up->u_type) {
  395. case INSERT:
  396. case YANK:
  397. (void) movegap(bp, bp->b_point);
  398. bp->b_egap += n;
  399. bp->b_point = pos(bp, bp->b_egap);
  400. break;
  401. case BACKSP:
  402. case DELETE:
  403. case CUT:
  404. bp->b_point = movegap(bp, bp->b_point);
  405. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  406. bp->b_gap += n;
  407. bp->b_point = pos(bp, bp->b_egap);
  408. break;
  409. case REPLACE:
  410. (void) movegap(bp, bp->b_point);
  411. bp->b_egap += up->u_size;
  412. bp->b_point = pos(bp, bp->b_egap);
  413. bp->b_point = movegap(bp, bp->b_point);
  414. memcpy(bp->b_gap, up->u_data, n*sizeof(char_t));
  415. bp->b_gap += n;
  416. bp->b_point = pos(bp, bp->b_egap);
  417. break;
  418. }
  419. bp->b_point = movegap(bp, up->u_point);
  420. bp->b_flags |= B_MODIFIED;
  421. if(isundo)
  422. bp->b_undo = up->u_next;
  423. else
  424. bp->b_redo = up->u_next;
  425. if(curbp->b_point < curbp->b_page || curbp->b_point > curbp->b_epage)
  426. bp->b_reframe = 1;
  427. }
  428. void undo()
  429. {
  430. undo_or_redo(curbp, curbp->b_undo, TRUE);
  431. }
  432. /* Redo */
  433. void redo()
  434. {
  435. undo_or_redo(curbp, curbp->b_redo, FALSE);
  436. }
  437. /* find the point for start of line ln */
  438. point_t line_to_point(int ln)
  439. {
  440. point_t end_p = pos(curbp, curbp->b_ebuf);
  441. point_t p, start;
  442. for (p=0, start=0; p <= end_p; p++) {
  443. char_t *c = ptr(curbp, p);
  444. if(c == 0)
  445. break;
  446. if ( *c == '\n') {
  447. if (--ln == 0)
  448. return start;
  449. if (p + 1 <= end_p)
  450. start = p + 1;
  451. }
  452. if(!*c && ln == 1)
  453. return start;
  454. }
  455. return -1;
  456. }
  457. /* scan buffer and fill in curline and lastline */
  458. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  459. {
  460. point_t end_p = pos(bp, bp->b_ebuf);
  461. point_t p;
  462. int line;
  463. *curline = -1;
  464. for (p=0, line=0; p < end_p; p++) {
  465. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  466. *lastline = line;
  467. if (*curline == -1 && p == bp->b_point) {
  468. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  469. }
  470. }
  471. *lastline = *lastline + 1;
  472. if (bp->b_point == end_p)
  473. *curline = *lastline;
  474. }
  475. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  476. int is_file_modified(char *fn)
  477. {
  478. struct stat sb;
  479. if(stat(fn, &sb) < 0) {
  480. return (FALSE);
  481. }
  482. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  483. return (TRUE);
  484. }
  485. return (FALSE);
  486. }
  487. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  488. int file_was_modified_prompt()
  489. {
  490. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  491. char c = '\0';
  492. point_t p = curbp->b_point;
  493. struct stat sb;
  494. print_to_msgline(prompt);
  495. clrtoeol(prompt, MSGLINE);
  496. c = yesnomaybeso('n');
  497. if (c == 'n') {
  498. clrtoeol("", MSGLINE);
  499. return FALSE;
  500. } else if(c == 'r') {
  501. curbp->b_point = 0;
  502. load_file(curbp->b_fname);
  503. clrtoeol("", MSGLINE);
  504. curbp->b_point = p;
  505. msg("Buffer reverted.");
  506. return FALSE;
  507. } else if(c == 'y') {
  508. clrtoeol("", MSGLINE);
  509. if (stat(curbp->b_fname, &sb) < 0) {
  510. msg("Failed to find file \"%s\".", curbp->b_fname);
  511. return (FALSE);
  512. }
  513. if (MAX_SIZE_T < sb.st_size) {
  514. msg("File \"%s\" is too big to load.", curbp->b_fname);
  515. return (FALSE);
  516. }
  517. curbp->b_fmtime = sb.st_mtime;
  518. return TRUE;
  519. }
  520. clrtoeol("", MSGLINE);
  521. return (FALSE);
  522. }