gap.c 18 KB

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