gap.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  1. /* gap.c, Ait, BSD 3-Clause, Kevin Bloom, 2023-2025,
  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. dtemp = (char_t *) strndup((const char *)ptr(curbp, curbp->b_point), npc);
  265. asprintf((char **)&utemp, "%s%s", curbp->b_undo->u_data, dtemp);
  266. free(curbp->b_undo->u_data);
  267. curbp->b_undo->u_data = utemp;
  268. free(dtemp);
  269. dtemp = NULL;
  270. return;
  271. } else {
  272. u->u_data = (char_t *)(malloc(npc));
  273. memcpy(u->u_data, ptr(curbp, curbp->b_point), npc);
  274. u->u_data[npc] = '\0';
  275. }
  276. break;
  277. case BACKSP:
  278. npc = prev_utf8_char_size();
  279. if(curbp->b_undo != NULL && curbp->b_undo->u_type == BACKSP && shouldconcat) {
  280. curbp->b_undo->u_point -= npc;
  281. dtemp = (char_t *)malloc(npc);
  282. (void) memcpy(dtemp, ptr(curbp, curbp->b_undo->u_point), npc);
  283. dtemp[npc] = '\0';
  284. asprintf((char **)&utemp, "%s%s", dtemp, curbp->b_undo->u_data);
  285. free(curbp->b_undo->u_data);
  286. curbp->b_undo->u_data = utemp;
  287. free(dtemp);
  288. dtemp = NULL;
  289. return;
  290. } else {
  291. u->u_point = curbp->b_point - npc;
  292. u->u_data = (char_t *)(malloc(npc));
  293. strncpy((char *)u->u_data, (const char *)ptr(curbp, u->u_point), npc);
  294. u->u_data[npc] = '\0';
  295. u->u_adjust = TRUE;
  296. }
  297. break;
  298. case CUT: {
  299. int bigger_mark = FALSE;
  300. if (curbp->b_point < curbp->b_mark) {
  301. (void) movegap(curbp, curbp->b_point);
  302. p = ptr(curbp, curbp->b_point);
  303. length = curbp->b_mark - curbp->b_point;
  304. bigger_mark = TRUE;
  305. u->u_adjust = TRUE;
  306. } else {
  307. (void) movegap(curbp, curbp->b_mark);
  308. p = ptr(curbp, curbp->b_mark);
  309. length = curbp->b_point - curbp->b_mark;
  310. u->u_point = curbp->b_mark;
  311. }
  312. if(curbp->b_undo != NULL &&
  313. curbp->b_undo->u_type == CUT &&
  314. shouldconcat) {
  315. dtemp = malloc(length);
  316. memcpy(dtemp, p, length);
  317. dtemp[length] = '\0';
  318. if(shouldconcat < 0) {
  319. asprintf(
  320. (char **)&utemp,
  321. "%s%s", dtemp, curbp->b_undo->u_data
  322. );
  323. curbp->b_undo->u_point = curbp->b_point;
  324. } else
  325. asprintf(
  326. (char **)&utemp,
  327. "%s%s", curbp->b_undo->u_data, dtemp
  328. );
  329. if(curbp->b_undo->u_data != NULL)
  330. free(curbp->b_undo->u_data);
  331. curbp->b_undo->u_data = utemp;
  332. curbp->b_undo->u_size = length+curbp->b_undo->u_size;
  333. /* Only adjust u_adjust if it's not a delete word command,
  334. otherwise the line count will be off.
  335. */
  336. if(currentcommand != KBD_DELETE_WORD)
  337. for(int i = 0; curbp->b_undo->u_data[i] != '\0'; i++) {
  338. if(curbp->b_undo->u_data[i] == '\n') {
  339. curbp->b_undo->u_adjust = !bigger_mark;
  340. break;
  341. }
  342. }
  343. free(dtemp);
  344. return;
  345. } else {
  346. u->u_data = (char_t*) malloc(length+1);
  347. (void) memcpy(u->u_data, p, length);
  348. u->u_data[length] = '\0';
  349. u->u_size = length;
  350. /* Only adjust u_adjust if it's not a delete word command,
  351. otherwise the line count will be off.
  352. */
  353. if(currentcommand != KBD_DELETE_WORD)
  354. for(int i = 0; u->u_data[i] != '\0'; i++) {
  355. if(u->u_data[i] == '\n') {
  356. u->u_adjust = !bigger_mark;
  357. break;
  358. }
  359. }
  360. }
  361. break;
  362. }
  363. case YANK:
  364. u->u_data = (char_t *) strndup((const char *)scrap.data, scrap.len);
  365. u->u_data[scrap.len] = '\0';
  366. break;
  367. case REPLACE:
  368. (void) movegap(curbp, curbp->b_point);
  369. p = ptr(curbp, curbp->b_point);
  370. length = curbp->b_mark - curbp->b_point;
  371. u->u_data = (char_t*) malloc(length);
  372. (void) memcpy(u->u_data, p, length);
  373. u->u_data[length] = '\0';
  374. u->u_size = shouldconcat;
  375. break;
  376. case CLIPBOARD: {
  377. int ntemp = strlen(gtemp);
  378. u->u_data = (char_t*) malloc(ntemp);
  379. (void) memcpy(u->u_data, gtemp, ntemp);
  380. u->u_data[ntemp] = '\0';
  381. break;
  382. }
  383. case LOAD: {
  384. (void) movegap(curbp, (point_t) 0);
  385. u->u_data = (char_t*) malloc(curbp->b_size);
  386. (void) memcpy(u->u_data, curbp->b_egap, curbp->b_size);
  387. u->u_data[curbp->b_size] = '\0';
  388. u->u_size = curbp->b_size;
  389. break;
  390. }
  391. }
  392. u->u_next = curbp->b_undo;
  393. curbp->b_undo = u;
  394. curbp->b_redo = NULL;
  395. }
  396. /* Record a new redo if you've just undid or a undo if you've just redid. */
  397. void redo_or_undo_set(undo_t *up, int datalen, int isundo)
  398. {
  399. undo_t *rp = (undo_t *)malloc(sizeof(undo_t));
  400. char_t *p;
  401. int newlines = 0;
  402. assert(rp != NULL);
  403. rp->u_point = up->u_point;
  404. rp->u_line = up->u_line;
  405. if(up->u_adjust)
  406. for(int i = 0 ; up->u_data[i] != '\0'; i++) {
  407. if(up->u_data[i] == '\n')
  408. newlines++;
  409. }
  410. switch(up->u_type) {
  411. case INSERT:
  412. rp->u_type = DELETE;
  413. break;
  414. case BACKSP:
  415. rp->u_adjust = TRUE;
  416. rp->u_line -= newlines;
  417. case DELETE:
  418. rp->u_type = INSERT;
  419. break;
  420. case CUT:
  421. rp->u_type = YANK;
  422. rp->u_line -= newlines;
  423. break;
  424. case CLIPBOARD:
  425. case YANK:
  426. rp->u_type = CUT;
  427. rp->u_line += newlines;
  428. break;
  429. case REPLACE:
  430. rp->u_type = REPLACE;
  431. (void) movegap(curbp, up->u_point);
  432. p = ptr(curbp, up->u_point);
  433. rp->u_data = (char_t*) malloc(up->u_size);
  434. (void) memcpy(rp->u_data, p, up->u_size);
  435. rp->u_data[up->u_size] = '\0';
  436. rp->u_size = datalen;
  437. break;
  438. case LOAD:
  439. rp->u_type = LOAD;
  440. (void) movegap(curbp, (point_t) 0);
  441. rp->u_data = (char_t*) malloc(curbp->b_size);
  442. (void) memcpy(rp->u_data, curbp->b_egap, curbp->b_size);
  443. rp->u_data[curbp->b_size] = '\0';
  444. rp->u_size = curbp->b_size;
  445. break;
  446. }
  447. /* if(rp != NULL && rp->u_data != NULL && rp->u_type != REPLACE) {
  448. free(rp->u_data);
  449. rp->u_data = NULL;
  450. } */
  451. if(rp->u_type != REPLACE && rp->u_type != LOAD) {
  452. rp->u_data = (char_t *) calloc(datalen+1, sizeof(char_t));
  453. (void) memcpy(rp->u_data, up->u_data, datalen);
  454. rp->u_data[datalen] = '\0';
  455. }
  456. /* if an undo was done, save to redo */
  457. if(isundo) {
  458. rp->u_next = curbp->b_redo;
  459. curbp->b_redo = rp;
  460. } else {
  461. rp->u_next = curbp->b_undo;
  462. curbp->b_undo = rp;
  463. }
  464. }
  465. /* Undo */
  466. void undo_or_redo(buffer_t *bp, undo_t *up, int isundo)
  467. {
  468. int n = 0;
  469. currentcommand = KBD_UNDO;
  470. if(up == NULL) {
  471. if(isundo)
  472. msg("Nothing to undo!");
  473. else
  474. msg("Nothing to redo!");
  475. return;
  476. }
  477. bp->b_point = up->u_point;
  478. bp->b_line = up->u_line;
  479. n = strlen((const char *)up->u_data);
  480. redo_or_undo_set(up, n, isundo);
  481. switch(up->u_type) {
  482. case INSERT:
  483. case YANK:
  484. case CLIPBOARD:
  485. (void) movegap(bp, bp->b_point);
  486. bp->b_egap += n;
  487. bp->b_point = pos(bp, bp->b_egap);
  488. break;
  489. case BACKSP:
  490. case DELETE:
  491. case CUT:
  492. bp->b_point = movegap(bp, bp->b_point);
  493. memcpy(bp->b_gap, up->u_data, n);
  494. bp->b_gap += n;
  495. bp->b_point = pos(bp, bp->b_egap);
  496. break;
  497. case REPLACE:
  498. (void) movegap(bp, bp->b_point);
  499. bp->b_egap += up->u_size;
  500. bp->b_point = pos(bp, bp->b_egap);
  501. bp->b_point = movegap(bp, bp->b_point);
  502. memcpy(bp->b_gap, up->u_data, n);
  503. bp->b_gap += n;
  504. bp->b_point = pos(bp, bp->b_egap);
  505. break;
  506. case LOAD: {
  507. bp->b_gap = bp->b_buf;
  508. bp->b_egap = bp->b_ebuf;
  509. (void) movegap(bp, 0);
  510. if (bp->b_egap - bp->b_gap < up->u_size * sizeof (char_t))
  511. (void) growgap(bp, up->u_size);
  512. bp->b_point = movegap(bp, bp->b_point);
  513. memcpy(bp->b_gap, up->u_data, up->u_size);
  514. bp->b_gap += up->u_size;
  515. bp->b_point = pos(bp, bp->b_egap);
  516. bp->b_line = 1;
  517. break;
  518. }
  519. }
  520. if(up->u_adjust && isundo)
  521. bp->b_point = movegap(bp, up->u_point + n);
  522. else
  523. bp->b_point = movegap(bp, up->u_point);
  524. bp->b_flags |= B_MODIFIED;
  525. if(isundo)
  526. bp->b_undo = up->u_next;
  527. else
  528. bp->b_redo = up->u_next;
  529. if(curbp->b_point < curbp->b_page || curbp->b_point > curbp->b_epage)
  530. bp->b_reframe = 1;
  531. }
  532. void undo()
  533. {
  534. undo_or_redo(curbp, curbp->b_undo, TRUE);
  535. }
  536. /* Redo */
  537. void redo()
  538. {
  539. undo_or_redo(curbp, curbp->b_redo, FALSE);
  540. }
  541. /* find the point for start of line ln */
  542. point_t line_to_point(int ln)
  543. {
  544. point_t end_p = pos(curbp, curbp->b_ebuf);
  545. point_t p, start;
  546. for (p=0, start=0; p <= end_p; p++) {
  547. char_t *c = ptr(curbp, p);
  548. if(c == 0)
  549. break;
  550. if ( *c == '\n') {
  551. if (--ln == 0)
  552. return start;
  553. if (p + 1 <= end_p)
  554. start = p + 1;
  555. }
  556. if(!*c && ln == 1)
  557. return start;
  558. }
  559. return -1;
  560. }
  561. /* scan buffer and fill in curline and lastline */
  562. void get_line_stats(int *curline, int *lastline, buffer_t *bp)
  563. {
  564. point_t end_p = pos(bp, bp->b_ebuf);
  565. point_t p;
  566. int line;
  567. *curline = -1;
  568. for (p=0, line=0; p < end_p; p++) {
  569. line += (*(ptr(bp,p)) == '\n') ? 1 : 0;
  570. *lastline = line;
  571. if (*curline == -1 && p == bp->b_point) {
  572. *curline = (*(ptr(bp,p)) == '\n') ? line : line + 1;
  573. }
  574. }
  575. *lastline = *lastline + 1;
  576. if (bp->b_point == end_p)
  577. *curline = *lastline;
  578. }
  579. /* Return TRUE if file was modified after the current buffer's recored mtime. */
  580. int is_file_modified(char *fn)
  581. {
  582. struct stat sb;
  583. if(stat(fn, &sb) < 0) {
  584. return (FALSE);
  585. }
  586. if(sb.st_mtime != 0 && curbp->b_fmtime != sb.st_mtime) {
  587. return (TRUE);
  588. }
  589. return (FALSE);
  590. }
  591. /* Return TRUE to continue, FALSE to stop. Revert happens here. */
  592. int file_was_modified_prompt()
  593. {
  594. int current = 0, lastln = 0;
  595. const char *prompt = "This file has changed on disk; really edit (y/N/r) ?";
  596. int size = strlen(prompt);
  597. char c = '\0';
  598. point_t p = curbp->b_point;
  599. struct stat sb;
  600. print_to_msgline(prompt);
  601. clrtoeol(size, MSGLINE);
  602. c = yesnomaybeso('n');
  603. if (c == 'n') {
  604. clrtoeol(0, MSGLINE);
  605. return FALSE;
  606. } else if(c == 'r') {
  607. curbp->b_point = 0;
  608. undoset(LOAD, FALSE);
  609. load_file(curbp->b_fname);
  610. clrtoeol(0, MSGLINE);
  611. curbp->b_point = p;
  612. get_line_stats(&current, &lastln, curbp);
  613. curbp->b_line = current;
  614. msg("Buffer reverted.");
  615. return FALSE;
  616. } else if(c == 'y') {
  617. clrtoeol(0, MSGLINE);
  618. if (stat(curbp->b_fname, &sb) < 0) {
  619. msg("Failed to find file \"%s\".", curbp->b_fname);
  620. return (FALSE);
  621. }
  622. if (MAX_SIZE_T < sb.st_size) {
  623. msg("File \"%s\" is too big to load.", curbp->b_fname);
  624. return (FALSE);
  625. }
  626. curbp->b_fmtime = sb.st_mtime;
  627. return TRUE;
  628. }
  629. clrtoeol(0, MSGLINE);
  630. return (FALSE);
  631. }