display.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /* display.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 "header.h"
  6. #include "util.h"
  7. #define TB_IMPL
  8. #include "termbox.h"
  9. int moving_past = FALSE;
  10. /* Reverse scan for start of logical line containing offset */
  11. point_t lnstart(buffer_t *bp, register point_t off)
  12. {
  13. register char_t *p;
  14. do
  15. p = ptr(bp, --off);
  16. while (bp->b_buf < p && *p != '\n');
  17. return (bp->b_buf < p ? ++off : 0);
  18. }
  19. /* Forward scan for start of logical line segment (corresponds to screen line) containing 'finish' */
  20. point_t segstart(buffer_t *bp, window_t *wp, point_t start, point_t finish)
  21. {
  22. char_t *p;
  23. int c = 0;
  24. point_t scan = start;
  25. while (scan < finish) {
  26. p = ptr(bp, scan);
  27. if (*p == '\n') {
  28. c = 0;
  29. start = scan + 1;
  30. } else if (wp->w_cols <= c) {
  31. c = 0;
  32. start = scan;
  33. }
  34. scan += utf8_size(*ptr(bp,scan));
  35. if(*p == '\t')
  36. c += TAB_SIZE;
  37. else if(*p < 32)
  38. c += 2;
  39. else
  40. c++;
  41. }
  42. return (c < wp->w_cols ? start : finish);
  43. }
  44. /* Forward scan for start of logical line segment following 'finish' */
  45. point_t segnext(buffer_t *bp, window_t *wp, point_t start, point_t finish)
  46. {
  47. char_t *p;
  48. int c = 0;
  49. point_t scan = segstart(bp, wp, start, finish);
  50. for (;;) {
  51. p = ptr(bp, scan);
  52. if (bp->b_ebuf <= p || wp->w_cols <= c)
  53. break;
  54. scan += utf8_size(*ptr(bp,scan));
  55. if (*p == '\n')
  56. break;
  57. if(*p == '\t')
  58. c += TAB_SIZE;
  59. else if(*p < 32)
  60. c += 2;
  61. else
  62. c++;
  63. }
  64. return (p < bp->b_ebuf ? scan : pos(bp, bp->b_ebuf));
  65. }
  66. /* Move up one screen line */
  67. point_t upup(buffer_t *bp, window_t *wp, point_t off)
  68. {
  69. point_t curr = lnstart(bp, off);
  70. point_t seg = segstart(bp, wp, curr, off);
  71. if(wp->w_row == wp->w_top)
  72. moving_past = TRUE;
  73. if (curr < seg)
  74. off = segstart(bp, wp, curr, seg-1);
  75. else
  76. off = segstart(bp, wp, lnstart(bp,curr-1), curr-1);
  77. return (off);
  78. }
  79. /* Move down one screen line */
  80. point_t dndn(buffer_t *bp, window_t *wp, point_t off)
  81. {
  82. if(wp->w_row == wp->w_rows - wp->w_top - 1)
  83. moving_past = TRUE;
  84. return (segnext(bp, wp, lnstart(bp,off), off));
  85. }
  86. /* Return the offset of a column on the specified line */
  87. point_t lncolumn(buffer_t *bp, point_t offset, int column)
  88. {
  89. int c = 0;
  90. char_t *p;
  91. while ((p = ptr(bp, offset)) < bp->b_ebuf && *p != '\n' && c < column) {
  92. c += *p == '\t' ? TAB_SIZE : 1;
  93. offset += utf8_size(*ptr(bp,offset));
  94. }
  95. return (offset);
  96. }
  97. int syntax_color(buffer_t *bp)
  98. {
  99. int color = TB_DEFAULT;
  100. int token_type = parse_text(bp, bp->b_epage);
  101. switch(token_type) {
  102. case ID_SYMBOL: {
  103. color = TB_MAGENTA;
  104. break;
  105. }
  106. case ID_FUNCTION: {
  107. color = TB_RED;
  108. break;
  109. }
  110. case ID_DOUBLE_STRING: {
  111. color = TB_GREEN;
  112. break;
  113. }
  114. case ID_SINGLE_STRING: {
  115. color = TB_GREEN;
  116. break;
  117. }
  118. case ID_BACK_STRING: {
  119. color = TB_CYAN;
  120. break;
  121. }
  122. case ID_DIGITS: {
  123. color = TB_CYAN;
  124. break;
  125. }
  126. case ID_CONSTANT: {
  127. color = TB_YELLOW;
  128. break;
  129. }
  130. case ID_LINE_COMMENT: {
  131. color = TB_BLUE;
  132. break;
  133. }
  134. case ID_BLOCK_COMMENT: {
  135. color = TB_BLUE;
  136. break;
  137. }
  138. default: {
  139. color = TB_DEFAULT;
  140. break;
  141. }
  142. }
  143. return color;
  144. }
  145. /* TODO: document this crazy function */
  146. void display(window_t *wp, int flag)
  147. {
  148. char_t *p, z, *g;
  149. int i, j, nch, region = 0, searchlen = strlen(searchtext), b = 0, case_sense = FALSE;
  150. int trailing_whitespace = FALSE, w = 0;
  151. int matched = 0, shift = 0, emoji_skip = FALSE, hasSep = FALSE;
  152. buffer_t *bp = wp->w_bufp;
  153. point_t matching_bracket = -1;
  154. int prow = bp->b_row, max_col = 0, fg = TB_DEFAULT, bg = TB_DEFAULT;
  155. char_t *pnext0, *pnext1;
  156. uint32_t result, presult0, presult1;
  157. int notUTF8 = term == NULL ? TRUE : strcmp(term, "en_US.UTF-8");
  158. /* find start of screen, handle scroll up off page or top of file */
  159. /* point is always within b_page and b_epage */
  160. if (bp->b_point < bp->b_page)
  161. bp->b_page = segstart(bp, wp, lnstart(bp,bp->b_point), bp->b_point);
  162. /* reframe when scrolled off bottom */
  163. if (bp->b_reframe == 1 ||
  164. (bp->b_epage <= bp->b_point && curbp->b_point != pos(curbp, curbp->b_ebuf) &&
  165. (found_point > 0 || wp->w_rows-1 == wp->w_row - wp->w_top))) {
  166. bp->b_reframe = 0;
  167. /* Find end of screen plus one. */
  168. bp->b_page = dndn(bp, wp, bp->b_point);
  169. /* if we scroll to EOF we show 1 blank line at bottom of screen */
  170. if (pos(bp, bp->b_ebuf) <= bp->b_page) {
  171. bp->b_page = pos(bp, bp->b_ebuf);
  172. i = wp->w_rows - 1;
  173. } else {
  174. i = wp->w_rows - 0;
  175. }
  176. /* Scan backwards the required number of lines. */
  177. while (0 < i--)
  178. bp->b_page = upup(bp, wp, bp->b_page);
  179. }
  180. i = wp->w_top;
  181. j = wp->w_left;
  182. bp->b_epage = bp->b_page;
  183. /* We only need to check this when the top of the buffer has changed */
  184. if(bp->b_opage != bp->b_page || wp->w_update) {
  185. set_parse_state(bp, bp->b_epage, wp, TRUE); /* are we in a multline comment ? */
  186. } else {
  187. write_parse_state(wp);
  188. }
  189. /* paint screen from top of page until we hit maxline */
  190. while (1) {
  191. /* reached point - store the cursor position */
  192. if (bp->b_point == bp->b_epage) {
  193. bp->b_row = i;
  194. bp->b_col = j-shift;
  195. }
  196. p = ptr(bp, bp->b_epage);
  197. if (wp->w_top + wp->w_rows <= i || bp->b_ebuf <= p) /* maxline */
  198. break;
  199. if(curwp == wp && found_point == -1 &&
  200. is_bracket(*ptr(bp, bp->b_point), FALSE, NULL) &&
  201. (z = is_bracket(*p, TRUE, NULL)) != 0) {
  202. int dir = (z == '}' || z == ']' || z == ')' || z == '>');
  203. matching_bracket = find_matching_bracket(bp, wp, dir ? -1 : 1, TRUE);
  204. }
  205. nch = utf8_size(*p);
  206. pnext0 = ptr(bp, bp->b_epage+nch);
  207. pnext1 = ptr(bp, bp->b_epage+utf8_size(*pnext0));
  208. result = char_to_unicode(p, nch);
  209. presult0 = char_to_unicode(pnext0, utf8_size(*pnext0));
  210. presult1 = char_to_unicode(pnext1, utf8_size(*pnext1));
  211. /* prevent floating UTF-8 stuff. Maybe a termbox bug */
  212. /* This prevents extra characters from being printed when there
  213. is a combining unicode character. For emojis we they need
  214. to be repainted to the end of the line.
  215. TODO: this causes a flicker on these lines
  216. */
  217. if(result == 0x200D || presult0 == 0x200D)
  218. hasSep = TRUE;
  219. if(hasSep || is_combining_unicode(result) || is_combining_unicode(presult1)) {
  220. int x = j, y = i, iii = bp->b_epage, len = nch;
  221. int limit = wp->w_cols-j;
  222. char_t *uni;
  223. uni = ptr(bp, iii);
  224. for(int num = 0; *uni != '\n' && num < limit ; x++, num++) {
  225. struct tb_cell *front_wide, *back_wide;
  226. uint32_t space = j == (uint32_t)' ';
  227. int fg = TB_DEFAULT;
  228. int bg = TB_DEFAULT;
  229. cellbuf_get(&global.front, x, y, &front_wide);
  230. cellbuf_get(&global.back, x, y, &back_wide);
  231. send_attr(fg, bg);
  232. send_char(x, y, space);
  233. cell_set(front_wide, &space, 1, fg, bg);
  234. cell_set(back_wide, &space, 1, fg, bg);
  235. len = tb_utf8_char_length(*uni);
  236. iii += len;
  237. uni = ptr(bp, iii);
  238. }
  239. }
  240. if(wp == curwp &&
  241. bp->b_mark != NOMARK &&
  242. ((bp->b_mark <= bp->b_epage &&
  243. bp->b_point > bp->b_epage) ||
  244. (bp->b_point <= bp->b_epage &&
  245. bp->b_mark > bp->b_epage))
  246. ) {
  247. region = 1;
  248. } else if(matching_bracket >= 0 && bp->b_epage == matching_bracket) {
  249. matched = 1;
  250. } else if(curwp == wp) {
  251. if(found_point > 0 &&
  252. search_dir == 1 &&
  253. found_point - searchlen <= bp->b_epage &&
  254. bp->b_epage < found_point)
  255. region = 1;
  256. else if(found_point > 0 &&
  257. search_dir == 2 &&
  258. found_point + searchlen >= bp->b_epage &&
  259. bp->b_epage > found_point)
  260. region = 1;
  261. else if(found_point > 0 && tolower(searchtext[0]) == tolower(*p) && b == 0) {
  262. if(isupper(searchtext[0]))
  263. case_sense = TRUE;
  264. while(b < searchlen) {
  265. if(isupper(searchtext[b])) {
  266. case_sense = TRUE;
  267. if(searchtext[b] == *(p+b)) {
  268. region = 1;
  269. } else {
  270. region = 0;
  271. b = 0;
  272. break;
  273. }
  274. } else if (case_sense) {
  275. if(searchtext[b] == *(p+b)) {
  276. region = 1;
  277. } else {
  278. region = 0;
  279. b = 0;
  280. break;
  281. }
  282. } else if(!case_sense && searchtext[b] == tolower(*(p+b))) {
  283. region = 1;
  284. } else {
  285. region = 0;
  286. b = 0;
  287. break;
  288. }
  289. b++;
  290. }
  291. b = b <= 0 ? 0 : b - 1;
  292. } else if(b > 0) {
  293. b--;
  294. region = 1;
  295. } else {
  296. region = 0;
  297. b = 0;
  298. }
  299. }
  300. if(!isspace(*p) || *p == '\n' || *p > 32) {
  301. int color = syntax_color(bp);
  302. fg = region ? TB_DEFAULT : color;
  303. }
  304. bg = region ? TB_WHITE : TB_DEFAULT;
  305. if(nch > 1) {
  306. j++;
  307. tb_set_cell(j-1, i, result, fg, bg);
  308. if(is_combining_unicode(result)) {
  309. shift++;
  310. }
  311. if(nch > 3 && !emoji_skip) {
  312. /* probably an emoji and they are fat, give them extra space
  313. */
  314. j++;
  315. } else if(emoji_skip) {
  316. shift++;
  317. }
  318. if(result == 0x200D) {
  319. shift++;
  320. emoji_skip = TRUE;
  321. }
  322. } else if(*p < 32 && *p != '\n' && *p != '\t') {
  323. char ctrl = unctrl(*p);
  324. /* Due to control chars being displayed as 2 chars phantom
  325. chars will appear at the end. Also, the letter of the
  326. control char can appear on the window split if we don't
  327. cause a line wrap.
  328. */
  329. if(j-wp->w_left >= wp->w_cols-2) {
  330. tb_set_cell(j, i, ' ', TB_RED, bg);
  331. tb_set_cell(j+1, i, ' ', TB_RED, bg);
  332. i++;
  333. j=0;
  334. tb_set_cell(j, i, '^', TB_RED, bg);
  335. tb_set_cell(j+1, i, ctrl, TB_RED, bg);
  336. } else {
  337. tb_set_cell(j, i, '^', TB_RED, bg);
  338. tb_set_cell(j+1, i, ctrl, TB_RED, bg);
  339. }
  340. j += 2;
  341. emoji_skip = FALSE;
  342. } else if (isprint(*p) || *p == '\t' || *p == '\n') {
  343. /* Figure out if the whitespace is trailing or not. */
  344. g = ptr(bp, bp->b_epage);
  345. while(isspace(*p) && *p != '\n' && *g != '\n' && isspace(*(g = ptr(bp, bp->b_epage + w))) && bp->b_point != bp->b_epage + w) {
  346. if(*g == '\n') {
  347. trailing_whitespace = TRUE;
  348. break;
  349. }
  350. w++;
  351. }
  352. w = 0;
  353. if(trailing_whitespace && *p != '\t') {
  354. j++;
  355. uint32_t uni;
  356. tb_utf8_char_to_unicode(&uni, (char *)p);
  357. tb_set_cell(j-1, i, uni, TB_DEFAULT, TB_RED);
  358. } else if(*p == '\t') {
  359. int t = TAB_SIZE;
  360. if((j - wp->w_left) + TAB_SIZE > wp->w_cols) {
  361. t = wp->w_cols - j;
  362. j += t;
  363. } else {
  364. j += TAB_SIZE;
  365. }
  366. for(; t > 0; t--)
  367. tb_set_cell(j-t, i, notUTF8 ? ' ' : 0x2500, TB_YELLOW, bg);
  368. } else {
  369. j++;
  370. uint32_t uni;
  371. tb_utf8_char_to_unicode(&uni, (char *)p);
  372. int mc = matched ? TB_MAGENTA : TB_DEFAULT;
  373. mc = region ? TB_WHITE : mc;
  374. mc = recentbracket == curbp->b_epage ? TB_GREEN : mc;
  375. if(j-wp->w_left >= wp->w_cols) {
  376. tb_set_cell(j-1, i, uni, fg, TB_YELLOW);
  377. } else {
  378. /* Doing all this work prevents a bug with termbox where the
  379. cell buffer for the first row doesn't get overwritten
  380. correctly and you end up with an empty space.
  381. See https://notabug.org/nuclearkev/ait/issues/65
  382. */
  383. struct tb_cell *front_wide, *back_wide;
  384. cellbuf_get(&global.front, j-1, i, &front_wide);
  385. cellbuf_get(&global.back, j-1, i, &back_wide);
  386. if(front_wide->ch != uni ||
  387. front_wide->fg != fg ||
  388. front_wide->bg != mc ||
  389. wp->w_update) {
  390. send_attr(fg, mc);
  391. send_char(j-1, i, uni);
  392. cell_set(front_wide, &uni, 1, fg, mc);
  393. cell_set(back_wide, &uni, 1, fg, mc);
  394. }
  395. // tb_set_cell(j-1, i, uni, fg, mc);
  396. }
  397. }
  398. if(j > max_col) {
  399. max_col = j;
  400. }
  401. emoji_skip = FALSE;
  402. hasSep = FALSE;
  403. }
  404. trailing_whitespace = FALSE;
  405. matched = 0;
  406. if (*p == '\n' || wp->w_cols <= j - wp->w_left) {
  407. int cutoff = wp->w_mcol == 0 ? wp->w_left + wp->w_cols : wp->w_mcol;
  408. for(;*p == '\n' && j <= cutoff; j++) {
  409. int x = j - 1, y = i;
  410. struct tb_cell *front_wide, *back_wide;
  411. uint32_t space = (uint32_t)' ';
  412. int fg = TB_DEFAULT;
  413. int bg = TB_DEFAULT;
  414. if(y > 0)
  415. cellbuf_get(&global.front, x, y, &front_wide);
  416. cellbuf_get(&global.back, x, y, &back_wide);
  417. send_attr(fg, bg);
  418. send_char(x, y, space);
  419. if(y > 0)
  420. cell_set(front_wide, &space, 1, fg, bg);
  421. cell_set(back_wide, &space, 1, fg, bg);
  422. }
  423. j -= wp->w_cols + 1;
  424. if (j < wp->w_left)
  425. j = wp->w_left;
  426. ++i;
  427. shift = 0;
  428. emoji_skip = FALSE;
  429. }
  430. bp->b_epage = bp->b_epage + nch;
  431. }
  432. /* Clean up unused chars and, if at the end of buffer, display ~ to
  433. inform the user it's the end.
  434. */
  435. int first = TRUE;
  436. for(;i <= wp->w_top + wp->w_rows - 1; i++) {
  437. for(; j < wp->w_left + wp->w_cols; j++) {
  438. int x = j, y = i;
  439. struct tb_cell *front_wide, *back_wide;
  440. uint32_t space = j == wp->w_left && !first ? (uint32_t)'~' : (uint32_t)' ';
  441. int fg = j == wp->w_left ? TB_CYAN : TB_DEFAULT;
  442. int bg = TB_DEFAULT;
  443. if(y > 0)
  444. cellbuf_get(&global.front, x, y, &front_wide);
  445. cellbuf_get(&global.back, x, y, &back_wide);
  446. send_attr(fg, bg);
  447. send_char(x, y, space);
  448. if(y > 0)
  449. cell_set(front_wide, &space, 1, fg, bg);
  450. cell_set(back_wide, &space, 1, fg, bg);
  451. first = FALSE;
  452. }
  453. j -= wp->w_cols + 1;
  454. if (j < wp->w_left)
  455. j = wp->w_left;
  456. }
  457. wp->w_mcol = max_col;
  458. b2w(wp); /* save buffer stuff on window */
  459. modeline(wp);
  460. if (wp == curwp && flag) {
  461. dispmsg();
  462. tb_set_cursor(bp->b_col, bp->b_row); /* set cursor */
  463. }
  464. wp->w_update = FALSE;
  465. if(bp->b_row == prow && !moving_past) {
  466. bp->b_pcol = bp->b_col;
  467. wp->w_pcol = bp->b_col;
  468. }
  469. moving_past = FALSE;
  470. /* Smartly reset the syntax highlighting at the end of the page */
  471. set_parse_state(bp, bp->b_epage, wp, FALSE);
  472. }
  473. void modeline(window_t *wp)
  474. {
  475. int i, k, col = wp->w_col - wp->w_left + 1;
  476. int tabs = 0, ccs = 0, line = wp->w_bufp->b_line;
  477. char lch, mch, och;
  478. char mltemp[TEMPBUF];
  479. char *percent = NULL;
  480. char_t *p;
  481. point_t pg = lnstart(curbp, curbp->b_page),
  482. ebuf = pos(curbp, curbp->b_ebuf);
  483. float dec = 0.0, mod = 0.0;
  484. lch = (wp == curwp ? '=' : '-');
  485. mch = ((wp->w_bufp->b_flags & B_MODIFIED) ? '*' : lch);
  486. och = ((wp->w_bufp->b_flags & B_OVERWRITE) ? 'O' : lch);
  487. if(record_input)
  488. och = 'K';
  489. /* update line number when searching */
  490. if(found_point > -1 && wp == curwp) {
  491. point_t end_p = pos(wp->w_bufp, wp->w_bufp->b_ebuf);
  492. line = 1;
  493. for (point_t p=0; p < end_p && found_point > p; p++) {
  494. line += (*(ptr(wp->w_bufp,p)) == '\n') ? 1 : 0;
  495. }
  496. }
  497. /* Based on the b_page, determine if you're at the top, bottom or
  498. somewhere in-between the buffer. If in-between, calculate the
  499. percentage from the bottom. We round up always because that's
  500. what Emacs does.
  501. */
  502. percent = (char *) malloc(sizeof(char *)*3);
  503. if(pg == 0) {
  504. percent = "Top";
  505. } else if(curbp->b_epage == ebuf) {
  506. percent = "Bot";
  507. } else {
  508. mod = curbp->b_page * 100 % ebuf;
  509. dec = curbp->b_page * 100.0 / ebuf;
  510. if(mod != 0)
  511. dec++;
  512. sprintf(percent, "%d%%", (int)(dec));
  513. }
  514. for(k = 0; k <= wp->w_cols; k++) {
  515. if(wp->w_bufp->b_point-k < 0) {
  516. break;
  517. }
  518. p = ptr(wp->w_bufp, wp->w_bufp->b_point-k);
  519. if(k != 0) {
  520. if(*p == '\n') {
  521. break;
  522. }
  523. if(*p == '\t') {
  524. tabs++;
  525. }
  526. if(*p < 32) {
  527. ccs++;
  528. }
  529. }
  530. }
  531. if(k >= wp->w_cols && col != wp->w_cols) {
  532. col += wp->w_cols;
  533. }
  534. /* remove the extra spaces from the control and tab characters */
  535. if(col > 1) {
  536. col -= tabs * (TAB_SIZE - 2);
  537. col -= ccs;
  538. }
  539. sprintf(mltemp, "%c%c%c %s %c%c (%d,%d) %c%c %s ", lch,och,mch, wp->w_bufp->b_bname,
  540. lch, lch, line, col, lch, lch, percent);
  541. addstrxy(mltemp, wp->w_left, wp->w_top + wp->w_rows, 1);
  542. if(wp->w_update || wp->w_mlen != strlen(mltemp))
  543. for (i = strlen(mltemp); i < wp->w_cols; i++)
  544. addchxy(lch, i+wp->w_left, wp->w_top + wp->w_rows, 1);
  545. wp->w_mlen = strlen(mltemp);
  546. }
  547. void dispmsg()
  548. {
  549. tb_set_cursor(0, MSGLINE);
  550. if (msgflag) {
  551. addstr(msgline);
  552. msgflag = FALSE;
  553. }
  554. clrtoeol(msgline, MSGLINE);
  555. }
  556. void display_prompt_and_response(char *prompt, char *response)
  557. {
  558. char *full;
  559. asprintf((char **)&full, "%s%s", prompt, response);
  560. full[strlen(full)] = '\0';
  561. print_to_msgline(prompt);
  562. /* if we have a value print it and go to end of it */
  563. if (response[0] != '\0')
  564. addstr(response);
  565. clrtoeol(full, MSGLINE);
  566. }
  567. void update_display()
  568. {
  569. window_t *wp;
  570. buffer_t *bp;
  571. bp = curwp->w_bufp;
  572. bp->b_cpoint = bp->b_point; /* cpoint only ever set here */
  573. /* only one window */
  574. if (wheadp->w_next == NULL) {
  575. display(curwp, TRUE);
  576. tb_present();
  577. bp->b_psize = bp->b_size;
  578. return;
  579. }
  580. display(curwp, FALSE); /* this is key, we must call our win first to get accurate page and epage etc */
  581. if(curwp->w_left != 0)
  582. for(int i = curwp->w_top; i < curwp->w_rows+curwp->w_top+1; i++)
  583. tb_set_cell(curwp->w_left - 1, i, '|', TB_WHITE, TB_DEFAULT);
  584. /* never curwp, but same buffer in different window or update flag set*/
  585. for (wp=wheadp; wp != NULL; wp = wp->w_next) {
  586. if (wp != curwp && (wp->w_bufp == bp || wp->w_update)) {
  587. w2b(wp);
  588. for(int i = wp->w_top; i < wp->w_rows+wp->w_top+1; i++)
  589. tb_set_cell(wp->w_left - 1, i, '|', TB_WHITE, TB_DEFAULT);
  590. display(wp, FALSE);
  591. }
  592. }
  593. /* now display our window and buffer */
  594. w2b(curwp);
  595. dispmsg();
  596. tb_set_cursor(curwp->w_col, curwp->w_row); /* set cursor for curwp */
  597. tb_present();
  598. bp->b_psize = bp->b_size; /* now safe to save previous size for next time */
  599. }
  600. void w2b(window_t *w)
  601. {
  602. w->w_bufp->b_point = w->w_point;
  603. w->w_bufp->b_page = w->w_page;
  604. w->w_bufp->b_epage = w->w_epage;
  605. w->w_bufp->b_row = w->w_row;
  606. w->w_bufp->b_col = w->w_col;
  607. w->w_bufp->b_pcol = w->w_pcol;
  608. /* fixup pointers in other windows of the same buffer, if size of edit text changed */
  609. if (w->w_bufp->b_point > w->w_bufp->b_cpoint) {
  610. w->w_bufp->b_point += (w->w_bufp->b_size - w->w_bufp->b_psize);
  611. w->w_bufp->b_page += (w->w_bufp->b_size - w->w_bufp->b_psize);
  612. w->w_bufp->b_epage += (w->w_bufp->b_size - w->w_bufp->b_psize);
  613. }
  614. }
  615. void b2w(window_t *w)
  616. {
  617. w->w_point = w->w_bufp->b_point;
  618. w->w_page = w->w_bufp->b_page;
  619. w->w_epage = w->w_bufp->b_epage;
  620. w->w_row = w->w_bufp->b_row;
  621. w->w_col = w->w_bufp->b_col;
  622. w->w_pcol = w->w_bufp->b_pcol;
  623. w->w_bufp->b_size = (w->w_bufp->b_ebuf - w->w_bufp->b_buf) - (w->w_bufp->b_egap - w->w_bufp->b_gap);
  624. }
  625. /* flag = default answer, FALSE=n, TRUE=y */
  626. int yesno(int flag)
  627. {
  628. struct tb_event ev;
  629. tb_present();
  630. if(tb_poll_event(&ev) != TB_OK) return 0;
  631. if (ev.ch == '\r' || ev.ch == '\n')
  632. return (flag);
  633. return (tolower(ev.ch) == 'y');
  634. }
  635. char yesnomaybeso(char d)
  636. {
  637. struct tb_event ev;
  638. tb_present();
  639. if(tb_poll_event(&ev) != TB_OK) return 0;
  640. if (ev.ch == '\r' || ev.ch == '\n')
  641. return (d);
  642. return tolower(ev.ch);
  643. }
  644. void print_tb(const char *str, int x, int y, uint16_t fg, uint16_t bg)
  645. {
  646. while (*str) {
  647. uint32_t uni;
  648. str += tb_utf8_char_to_unicode(&uni, str);
  649. tb_set_cell(x, y, uni, fg, bg);
  650. x++;
  651. }
  652. }
  653. void printf_tb(int x, int y, uint16_t fg, uint16_t bg, const char *fmt, ...)
  654. {
  655. char buf[4096];
  656. va_list vl;
  657. va_start(vl, fmt);
  658. vsnprintf(buf, sizeof(buf), fmt, vl);
  659. va_end(vl);
  660. print_tb(buf, x, y, fg, bg);
  661. }
  662. void addstr(const char *str)
  663. {
  664. char *modstr;
  665. modstr = strndup(str, COLS);
  666. replace_all(modstr, '\t', ' ');
  667. replace_all(modstr, '\n', ' ');
  668. print_tb(modstr, global.cursor_x, global.cursor_y, TB_DEFAULT, TB_DEFAULT);
  669. tb_set_cursor(global.cursor_x+strlen(modstr), global.cursor_y);
  670. free(modstr);
  671. modstr = NULL;
  672. }
  673. void addch(char c)
  674. {
  675. printf_tb(global.cursor_x, global.cursor_y, TB_DEFAULT, TB_DEFAULT, "%c", c);
  676. tb_set_cursor(global.cursor_x+1, global.cursor_y);
  677. }
  678. /*
  679. Print char at point x,y. If within the region, highlight it.
  680. */
  681. void addchxy(char c, int x, int y, int region)
  682. {
  683. int fg = region == 0 ? TB_DEFAULT : TB_BLACK;
  684. int bg = region == 0 ? TB_DEFAULT : TB_WHITE;
  685. if(c != '\n' && c != '\r')
  686. printf_tb(x, y, fg, bg, "%c", c);
  687. else
  688. printf_tb(x, y, fg, bg, " ");
  689. }
  690. void addstrxy(char *c, int x, int y, int region)
  691. {
  692. int fg = region == 0 ? TB_DEFAULT : TB_BLACK;
  693. int bg = region == 0 ? TB_DEFAULT : TB_WHITE;
  694. printf_tb(x, y, fg, bg, "%s", c);
  695. }
  696. void clrtoeol(const char *str, int line)
  697. {
  698. int l = strlen(str);
  699. int size = COLS-l;
  700. if(size < 0)
  701. return;
  702. char blanks[size], entire[COLS];
  703. if(size == 0) {
  704. for(int i=0; i<size; i++) {
  705. entire[i]=' ';
  706. }
  707. printf_tb(l, line, TB_DEFAULT, TB_DEFAULT, "%s", entire);
  708. } else {
  709. for(int i=0; i<size; i++) {
  710. blanks[i]=' ';
  711. }
  712. printf_tb(l, line, TB_DEFAULT, TB_DEFAULT, "%s", blanks);
  713. }
  714. }