gap.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  1. /* gap.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2024,
  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((int)*fn) && *fn != '.' && *fn != '_' && *fn != '-' && *fn != '/')
  86. return (FALSE);
  87. }
  88. return (TRUE);
  89. }
  90. void relocate_backup(char *fp)
  91. {
  92. char ltemp[PATH_MAX+2];
  93. strcpy(ltemp, fp);
  94. replace_all(ltemp, '/', '!');
  95. strcpy(fp, backup_dir);
  96. strcat(fp, ltemp);
  97. }
  98. /*
  99. This code is heavily based on OpenBSD's mg(1)
  100. See mg's `fileio.c` file.
  101. */
  102. int backup_file(char *fn)
  103. {
  104. int orig, backup;
  105. char bfn[PATH_MAX+2];
  106. char *tname;
  107. char buf[STRBUF_L];
  108. struct stat sb;
  109. ssize_t nread;
  110. struct timespec new_times[2];
  111. sprintf(bfn, "%s~", fn);
  112. if(backup_dir != NULL)
  113. relocate_backup(bfn);
  114. /* If we can't open the original, just don't make a back up */
  115. if (stat(fn, &sb) == -1) {
  116. msg("Can't stat %s : %s", fn, strerror(errno));
  117. return TRUE;
  118. }
  119. if((orig = open(fn, O_RDONLY)) == -1) {
  120. return TRUE;
  121. }
  122. if (asprintf(&tname, "%s.XXXXXXXXXX", bfn) == -1) {
  123. msg("Can't allocate temp file name : %s", strerror(errno));
  124. return FALSE;
  125. }
  126. backup = mkstemp(tname);
  127. if(backup == -1) {
  128. msg("Failed to open backup file: \"%s\"", bfn);
  129. return FALSE;
  130. }
  131. while ((nread = read(orig, buf, sizeof(buf))) > 0) {
  132. if (write(backup, buf, (size_t)nread) != nread) {
  133. nread = -1;
  134. break;
  135. }
  136. }
  137. (void) fchmod(backup, (sb.st_mode & 0777));
  138. new_times[0] = sb.st_atim;
  139. new_times[1] = sb.st_mtim;
  140. futimens(backup, new_times);
  141. close(orig);
  142. close(backup);
  143. if (nread == -1) {
  144. if (unlink(tname) == -1)
  145. msg("Can't unlink temp : %s", strerror(errno));
  146. } else {
  147. if (rename(tname, bfn) == -1) {
  148. msg("Can't rename temp : %s", strerror(errno));
  149. (void) unlink(tname);
  150. nread = -1;
  151. }
  152. }
  153. return (nread == -1 ? FALSE : TRUE);
  154. }
  155. int save(char *fn)
  156. {
  157. FILE *fp;
  158. point_t length;
  159. struct stat sb;
  160. // if (!posix_file(fn)) {
  161. // msg("Not a portable POSIX file name.");
  162. // return (FALSE);
  163. // }
  164. stat(fn, &sb);
  165. if (!backup_file(fn)) {
  166. // msg("Failed to backup file \"%s\".", fn);
  167. return (FALSE);
  168. }
  169. fp = fopen(fn, "w");
  170. if (fp == NULL) {
  171. msg("Failed to open file \"%s\".", fn);
  172. return (FALSE);
  173. }
  174. (void) movegap(curbp, (point_t) 0);
  175. length = (point_t) (curbp->b_ebuf - curbp->b_egap);
  176. if (fwrite(curbp->b_egap, sizeof (char), (size_t) length, fp) != length) {
  177. msg("Failed to write file \"%s\".", fn);
  178. return (FALSE);
  179. }
  180. if (fclose(fp) != 0) {
  181. msg("Failed to close file \"%s\".", fn);
  182. return (FALSE);
  183. }
  184. curbp->b_flags &= ~B_MODIFIED;
  185. if (stat(fn, &sb) < 0) {
  186. msg("Failed to find file \"%s\".", fn);
  187. return (FALSE);
  188. }
  189. if (MAX_SIZE_T < sb.st_size) {
  190. msg("File \"%s\" is too big to load.", fn);
  191. return (FALSE);
  192. }
  193. curbp->b_fmtime = sb.st_mtime;
  194. msg("File \"%s\" %ld bytes saved.", fn, pos(curbp, curbp->b_ebuf));
  195. return (TRUE);
  196. }
  197. int load_file(char *fn)
  198. {
  199. /* reset the gap, make it the whole buffer */
  200. curbp->b_gap = curbp->b_buf;
  201. curbp->b_egap = curbp->b_ebuf;
  202. top();
  203. return insert_file(fn, FALSE);
  204. }
  205. /* reads file into buffer at point */
  206. int insert_file(char *fn, int modflag)
  207. {
  208. FILE *fp;
  209. size_t len;
  210. struct stat sb;
  211. if (stat(fn, &sb) < 0) {
  212. msg("Failed to find file \"%s\".", fn);
  213. return (FALSE);
  214. }
  215. if (MAX_SIZE_T < sb.st_size) {
  216. msg("File \"%s\" is too big to load.", fn);
  217. return (FALSE);
  218. }
  219. if (curbp->b_egap - curbp->b_gap < sb.st_size * sizeof (char_t) && !growgap(curbp, sb.st_size))
  220. return (FALSE);
  221. if ((fp = fopen(fn, "r")) == NULL) {
  222. msg("Failed to open file \"%s\".", fn);
  223. return (FALSE);
  224. }
  225. curbp->b_point = movegap(curbp, curbp->b_point);
  226. curbp->b_gap += len = fread(curbp->b_gap, sizeof (char), (size_t) sb.st_size, fp);
  227. if (fclose(fp) != 0) {
  228. msg("Failed to close file \"%s\".", fn);
  229. return (FALSE);
  230. }
  231. curbp->b_flags &= (modflag ? B_MODIFIED : ~B_MODIFIED);
  232. if(!modflag)
  233. curbp->b_fmtime = sb.st_mtime;
  234. msg("%ld bytes read.", len);
  235. return (TRUE);
  236. }
  237. /* Record a new undo */
  238. void undoset(int type, int shouldconcat)
  239. {
  240. int length = strlen((const char *)input);
  241. int npc = 0;
  242. char_t *p, *utemp, *dtemp;
  243. undo_t *u = (undo_t *)malloc(sizeof(undo_t));
  244. assert(u != NULL);
  245. u->u_type = type;
  246. u->u_point = curbp->b_point;
  247. u->u_line = curbp->b_line;
  248. u->u_adjust = FALSE;
  249. u->u_data = NULL;
  250. switch(type) {
  251. case INSERT:
  252. if(curbp->b_undo != NULL && curbp->b_undo->u_type == INSERT && shouldconcat) {
  253. asprintf((char **)&utemp, "%s%s", curbp->b_undo->u_data, input);
  254. free(curbp->b_undo->u_data);
  255. curbp->b_undo->u_data = utemp;
  256. return;
  257. } else {
  258. asprintf((char **)&u->u_data, "%s", input);
  259. }
  260. break;
  261. case DELETE:
  262. npc = utf8_size(*ptr(curbp,curbp->b_point));
  263. if(curbp->b_undo != NULL && curbp->b_undo->u_type == DELETE && shouldconcat) {
  264. // ulen = strlen((const char *)curbp->b_undo->u_data);
  265. dtemp = (char_t *)malloc(npc);
  266. (void) memcpy(dtemp, ptr(curbp, curbp->b_point), npc);
  267. // dtemp[npc] = '\0';
  268. // utemp = (char_t *) strndup((const char *)curbp->b_undo->u_data, ulen);
  269. // curbp->b_undo->u_data = (char_t *)(realloc(curbp->b_undo->u_data, (size_t)(npc + ulen)));
  270. asprintf((char **)&utemp, "%s%s", curbp->b_undo->u_data, dtemp);
  271. // curbp->b_undo->u_data[npc+ulen] = '\0';
  272. free(curbp->b_undo->u_data);
  273. curbp->b_undo->u_data = utemp;
  274. free(dtemp);
  275. // free(utemp);
  276. dtemp = NULL;
  277. // utemp = NULL;
  278. return;
  279. } else {
  280. u->u_data = (char_t *)(malloc(npc));
  281. memcpy(u->u_data, ptr(curbp, curbp->b_point), npc);
  282. u->u_data[npc] = '\0';
  283. }
  284. break;
  285. case BACKSP:
  286. npc = prev_utf8_char_size();
  287. if(curbp->b_undo != NULL && curbp->b_undo->u_type == BACKSP && shouldconcat) {
  288. curbp->b_undo->u_point -= npc;
  289. dtemp = (char_t *)malloc(npc);
  290. (void) memcpy(dtemp, ptr(curbp, curbp->b_undo->u_point), npc);
  291. dtemp[npc] = '\0';
  292. // ulen = strlen((const char *)curbp->b_undo->u_data);
  293. // utemp = (char_t *) strndup((const char *)curbp->b_undo->u_data, ulen);
  294. // curbp->b_undo->u_data = (char_t *)(realloc(curbp->b_undo->u_data, (size_t)(npc + ulen)));
  295. asprintf((char **)&utemp, "%s%s", dtemp, curbp->b_undo->u_data);
  296. // curbp->b_undo->u_data[(npc + ulen)] = '\0';
  297. free(curbp->b_undo->u_data);
  298. curbp->b_undo->u_data = utemp;
  299. free(dtemp);
  300. // utemp = NULL;
  301. dtemp = NULL;
  302. return;
  303. } else {
  304. u->u_point = curbp->b_point - npc;
  305. u->u_data = (char_t *)(malloc(npc));
  306. strncpy((char *)u->u_data, (const char *)ptr(curbp, u->u_point), npc);
  307. u->u_data[npc] = '\0';
  308. u->u_adjust = TRUE;
  309. }
  310. break;
  311. case CUT: {
  312. int bigger_mark = FALSE;
  313. if (curbp->b_point < curbp->b_mark) {
  314. (void) movegap(curbp, curbp->b_point);
  315. p = ptr(curbp, curbp->b_point);
  316. length = curbp->b_mark - curbp->b_point;
  317. bigger_mark = TRUE;
  318. u->u_adjust = TRUE;
  319. } else {
  320. (void) movegap(curbp, curbp->b_mark);
  321. p = ptr(curbp, curbp->b_mark);
  322. length = curbp->b_point - curbp->b_mark;
  323. u->u_point = curbp->b_mark;
  324. }
  325. if(curbp->b_undo != NULL &&
  326. curbp->b_undo->u_type == CUT &&
  327. shouldconcat) {
  328. // int npc = strlen((char *)curbp->b_undo->u_data);
  329. // utemp = (char_t *) strndup((const char *)curbp->b_undo->u_data, npc);
  330. dtemp = malloc(length);
  331. memcpy(dtemp, p, length);
  332. dtemp[length] = '\0';
  333. if(shouldconcat < 0) {
  334. asprintf(
  335. (char **)&utemp,
  336. "%s%s", dtemp, curbp->b_undo->u_data
  337. );
  338. curbp->b_undo->u_point = curbp->b_point;
  339. } else
  340. asprintf(
  341. (char **)&utemp,
  342. "%s%s", curbp->b_undo->u_data, dtemp
  343. );
  344. // curbp->b_undo->u_data[length+npc] = '\0';
  345. if(curbp->b_undo->u_data != NULL)
  346. free(curbp->b_undo->u_data);
  347. curbp->b_undo->u_data = utemp;
  348. curbp->b_undo->u_size = length+curbp->b_undo->u_size;
  349. /* Only adjust u_adjust if it's not a delete word command,
  350. otherwise the line count will be off.
  351. */
  352. if(currentcommand != KBD_DELETE_WORD)
  353. for(int i = 0; curbp->b_undo->u_data[i] != '\0'; i++) {
  354. if(curbp->b_undo->u_data[i] == '\n') {
  355. curbp->b_undo->u_adjust = !bigger_mark;
  356. break;
  357. }
  358. }
  359. // free(utemp);
  360. free(dtemp);
  361. return;
  362. } else {
  363. u->u_data = (char_t*) malloc(length+1);
  364. (void) memcpy(u->u_data, p, length);
  365. u->u_data[length] = '\0';
  366. u->u_size = length;
  367. /* Only adjust u_adjust if it's not a delete word command,
  368. otherwise the line count will be off.
  369. */
  370. if(currentcommand != KBD_DELETE_WORD)
  371. for(int i = 0; u->u_data[i] != '\0'; i++) {
  372. if(u->u_data[i] == '\n') {
  373. u->u_adjust = !bigger_mark;
  374. break;
  375. }
  376. }
  377. }
  378. break;
  379. }
  380. case YANK:
  381. u->u_data = (char_t *) strndup((const char *)scrap.data, scrap.len);
  382. u->u_data[scrap.len] = '\0';
  383. break;
  384. case REPLACE:
  385. (void) movegap(curbp, curbp->b_point);
  386. p = ptr(curbp, curbp->b_point);
  387. length = curbp->b_mark - curbp->b_point;
  388. u->u_data = (char_t*) malloc(length);
  389. (void) memcpy(u->u_data, p, length);
  390. u->u_data[length] = '\0';
  391. u->u_size = shouldconcat;
  392. break;
  393. case CLIPBOARD: {
  394. int ntemp = strlen(gtemp);
  395. u->u_data = (char_t*) malloc(ntemp);
  396. (void) memcpy(u->u_data, gtemp, ntemp);
  397. u->u_data[ntemp] = '\0';
  398. break;
  399. }
  400. case LOAD: {
  401. (void) movegap(curbp, (point_t) 0);
  402. u->u_data = (char_t*) malloc(curbp->b_size);
  403. (void) memcpy(u->u_data, curbp->b_egap, curbp->b_size);
  404. u->u_data[curbp->b_size] = '\0';
  405. u->u_size = curbp->b_size;
  406. break;
  407. }
  408. }
  409. u->u_next = curbp->b_undo;
  410. curbp->b_undo = u;
  411. curbp->b_redo = NULL;
  412. }
  413. /* Record a new redo if you've just undid or a undo if you've just redid. */
  414. void redo_or_undo_set(undo_t *up, int datalen, int isundo)
  415. {
  416. undo_t *rp = (undo_t *)malloc(sizeof(undo_t));
  417. char_t *p;
  418. int newlines = 0;
  419. assert(rp != NULL);
  420. rp->u_point = up->u_point;
  421. rp->u_line = up->u_line;
  422. if(up->u_adjust)
  423. for(int i = 0 ; up->u_data[i] != '\0'; i++) {
  424. if(up->u_data[i] == '\n')
  425. newlines++;
  426. }
  427. switch(up->u_type) {
  428. case INSERT:
  429. rp->u_type = DELETE;
  430. break;
  431. case BACKSP:
  432. rp->u_adjust = TRUE;
  433. rp->u_line -= newlines;
  434. case DELETE:
  435. rp->u_type = INSERT;
  436. break;
  437. case CUT:
  438. rp->u_type = YANK;
  439. rp->u_line -= newlines;
  440. break;
  441. case CLIPBOARD:
  442. case YANK:
  443. rp->u_type = CUT;
  444. rp->u_line += newlines;
  445. break;
  446. case REPLACE:
  447. rp->u_type = REPLACE;
  448. (void) movegap(curbp, up->u_point);
  449. p = ptr(curbp, up->u_point);
  450. rp->u_data = (char_t*) malloc(up->u_size);
  451. (void) memcpy(rp->u_data, p, up->u_size);
  452. rp->u_data[up->u_size] = '\0';
  453. rp->u_size = datalen;
  454. break;
  455. case LOAD:
  456. rp->u_type = LOAD;
  457. (void) movegap(curbp, (point_t) 0);
  458. rp->u_data = (char_t*) malloc(curbp->b_size);
  459. (void) memcpy(rp->u_data, curbp->b_egap, curbp->b_size);
  460. rp->u_data[curbp->b_size] = '\0';
  461. rp->u_size = curbp->b_size;
  462. break;
  463. }
  464. /* if(rp != NULL && rp->u_data != NULL && rp->u_type != REPLACE) {
  465. free(rp->u_data);
  466. rp->u_data = NULL;
  467. } */
  468. if(rp->u_type != REPLACE && rp->u_type != LOAD) {
  469. rp->u_data = (char_t *) calloc(datalen+1, sizeof(char_t));
  470. (void) memcpy(rp->u_data, up->u_data, datalen);
  471. rp->u_data[datalen] = '\0';
  472. }
  473. /* if an undo was done, save to redo */
  474. if(isundo) {
  475. rp->u_next = curbp->b_redo;
  476. curbp->b_redo = rp;
  477. } else {
  478. rp->u_next = curbp->b_undo;
  479. curbp->b_undo = rp;
  480. }
  481. }
  482. /* Undo */
  483. void undo_or_redo(buffer_t *bp, undo_t *up, int isundo)
  484. {
  485. int n = 0;
  486. currentcommand = KBD_UNDO;
  487. if(up == NULL) {
  488. if(isundo)
  489. msg("Nothing to undo!");
  490. else
  491. msg("Nothing to redo!");
  492. return;
  493. }
  494. bp->b_point = up->u_point;
  495. bp->b_line = up->u_line;
  496. n = strlen((const char *)up->u_data);
  497. redo_or_undo_set(up, n, isundo);
  498. switch(up->u_type) {
  499. case INSERT:
  500. case YANK:
  501. case CLIPBOARD:
  502. (void) movegap(bp, bp->b_point);
  503. bp->b_egap += n;
  504. bp->b_point = pos(bp, bp->b_egap);
  505. break;
  506. case BACKSP:
  507. case DELETE:
  508. case CUT:
  509. bp->b_point = movegap(bp, bp->b_point);
  510. memcpy(bp->b_gap, up->u_data, n);
  511. bp->b_gap += n;
  512. bp->b_point = pos(bp, bp->b_egap);
  513. break;
  514. case REPLACE:
  515. (void) movegap(bp, bp->b_point);
  516. bp->b_egap += up->u_size;
  517. bp->b_point = pos(bp, bp->b_egap);
  518. bp->b_point = movegap(bp, bp->b_point);
  519. memcpy(bp->b_gap, up->u_data, n);
  520. bp->b_gap += n;
  521. bp->b_point = pos(bp, bp->b_egap);
  522. break;
  523. case LOAD: {
  524. bp->b_gap = bp->b_buf;
  525. bp->b_egap = bp->b_ebuf;
  526. (void) movegap(bp, 0);
  527. if (bp->b_egap - bp->b_gap < up->u_size * sizeof (char_t))
  528. (void) growgap(bp, up->u_size);
  529. bp->b_point = movegap(bp, bp->b_point);
  530. memcpy(bp->b_gap, up->u_data, up->u_size);
  531. bp->b_gap += up->u_size;
  532. bp->b_point = pos(bp, bp->b_egap);
  533. bp->b_line = 1;
  534. break;
  535. }
  536. }
  537. if(up->u_adjust && isundo)
  538. bp->b_point = movegap(bp, up->u_point + n);
  539. else
  540. bp->b_point = movegap(bp, up->u_point);
  541. bp->b_flags |= B_MODIFIED;
  542. if(isundo)
  543. bp->b_undo = up->u_next;
  544. else
  545. bp->b_redo = up->u_next;
  546. if(curbp->b_point < curbp->b_page || curbp->b_point > curbp->b_epage)
  547. bp->b_reframe = 1;
  548. }
  549. void undo()
  550. {
  551. undo_or_redo(curbp, curbp->b_undo, TRUE);
  552. }
  553. /* Redo */
  554. void redo()
  555. {
  556. undo_or_redo(curbp, curbp->b_redo, FALSE);
  557. }
  558. /* find the point for start of line ln */
  559. point_t line_to_point(int ln)
  560. {
  561. point_t end_p = pos(curbp, curbp->b_ebuf);
  562. point_t p, start;
  563. for (p=0, start=0; p <= end_p; p++) {
  564. char_t *c = ptr(curbp, p);
  565. if(c == 0)
  566. break;
  567. if ( *c == '\n') {
  568. if (--ln == 0)
  569. return start;
  570. if (p + 1 <= end_p)
  571. start = p + 1;
  572. }
  573. if(!*c && ln == 1)
  574. return start;
  575. }
  576. return -1;
  577. }
  578. /* scan buffer and fill in curline and lastline */
  579. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  580. {
  581. point_t end_p = pos(bp, bp->b_ebuf);
  582. point_t p;
  583. int line;
  584. *curline = -1;
  585. for (p=0, line=0; p < end_p; p++) {
  586. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  587. *lastline = line;
  588. if (*curline == -1 && p == bp->b_point) {
  589. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  590. }
  591. }
  592. *lastline = *lastline + 1;
  593. if (bp->b_point == end_p)
  594. *curline = *lastline;
  595. }
  596. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  597. int is_file_modified(char *fn)
  598. {
  599. struct stat sb;
  600. if(stat(fn, &sb) < 0) {
  601. return (FALSE);
  602. }
  603. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  604. return (TRUE);
  605. }
  606. return (FALSE);
  607. }
  608. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  609. int file_was_modified_prompt()
  610. {
  611. int current = 0, lastln = 0;
  612. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  613. char c = '\0';
  614. point_t p = curbp->b_point;
  615. struct stat sb;
  616. print_to_msgline(prompt);
  617. clrtoeol(prompt, MSGLINE);
  618. c = yesnomaybeso('n');
  619. if (c == 'n') {
  620. clrtoeol("", MSGLINE);
  621. return FALSE;
  622. } else if(c == 'r') {
  623. curbp->b_point = 0;
  624. undoset(LOAD, FALSE);
  625. load_file(curbp->b_fname);
  626. clrtoeol("", MSGLINE);
  627. curbp->b_point = p;
  628. get_line_stats(&current, &lastln, curbp);
  629. curbp->b_line = current;
  630. msg("Buffer reverted.");
  631. return FALSE;
  632. } else if(c == 'y') {
  633. clrtoeol("", MSGLINE);
  634. if (stat(curbp->b_fname, &sb) < 0) {
  635. msg("Failed to find file \"%s\".", curbp->b_fname);
  636. return (FALSE);
  637. }
  638. if (MAX_SIZE_T < sb.st_size) {
  639. msg("File \"%s\" is too big to load.", curbp->b_fname);
  640. return (FALSE);
  641. }
  642. curbp->b_fmtime = sb.st_mtime;
  643. return TRUE;
  644. }
  645. clrtoeol("", MSGLINE);
  646. return (FALSE);
  647. }