gap.c 18 KB

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