display.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /* display.c, Ait, BSD 3-Clause, Kevin Bloom, 2023,
  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. c += *p == '\t' ? 8 - (c & 7) : 1;
  36. }
  37. return (c < wp->w_cols ? start : finish);
  38. }
  39. /* Forward scan for start of logical line segment following 'finish' */
  40. point_t segnext(buffer_t *bp, window_t *wp, point_t start, point_t finish)
  41. {
  42. char_t *p;
  43. int c = 0;
  44. point_t scan = segstart(bp, wp, start, finish);
  45. for (;;) {
  46. p = ptr(bp, scan);
  47. if (bp->b_ebuf <= p || wp->w_cols <= c)
  48. break;
  49. scan += utf8_size(*ptr(bp,scan));
  50. if (*p == '\n')
  51. break;
  52. c += *p == '\t' ? 8 - (c & 7) : 1;
  53. }
  54. return (p < bp->b_ebuf ? scan : pos(bp, bp->b_ebuf));
  55. }
  56. /* Move up one screen line */
  57. point_t upup(buffer_t *bp, window_t *wp, point_t off)
  58. {
  59. point_t curr = lnstart(bp, off);
  60. point_t seg = segstart(bp, wp, curr, off);
  61. if(wp->w_row == wp->w_top)
  62. moving_past = TRUE;
  63. if (curr < seg)
  64. off = segstart(bp, wp, curr, seg-1);
  65. else
  66. off = segstart(bp, wp, lnstart(bp,curr-1), curr-1);
  67. return (off);
  68. }
  69. /* Move down one screen line */
  70. point_t dndn(buffer_t *bp, window_t *wp, point_t off)
  71. {
  72. if(wp->w_row == wp->w_rows - wp->w_top - 1)
  73. moving_past = TRUE;
  74. return (segnext(bp, wp, lnstart(bp,off), off));
  75. }
  76. /* Return the offset of a column on the specified line */
  77. point_t lncolumn(buffer_t *bp, point_t offset, int column)
  78. {
  79. int c = 0;
  80. char_t *p;
  81. while ((p = ptr(bp, offset)) < bp->b_ebuf && *p != '\n' && c < column) {
  82. c += *p == '\t' ? 4 : 1;
  83. offset += utf8_size(*ptr(bp,offset));
  84. }
  85. return (offset);
  86. }
  87. void display(window_t *wp, int flag)
  88. {
  89. char_t *p, z, *g;
  90. int i, j, nch, region = 0, searchlen = strlen(searchtext), b = 0, case_sense = FALSE;
  91. int trailing_whitespace = FALSE, w = 0, token_type = ID_DEFAULT, color = TB_DEFAULT;
  92. buffer_t *bp = wp->w_bufp;
  93. point_t matching_bracket = -1;
  94. int prow = bp->b_row;
  95. /* find start of screen, handle scroll up off page or top of file */
  96. /* point is always within b_page and b_epage */
  97. if (bp->b_point < bp->b_page)
  98. bp->b_page = segstart(bp, wp, lnstart(bp,bp->b_point), bp->b_point);
  99. /* reframe when scrolled off bottom */
  100. if (bp->b_reframe == 1 ||
  101. (bp->b_epage <= bp->b_point && curbp->b_point != pos(curbp, curbp->b_ebuf) &&
  102. (found_point > 0 || wp->w_rows-1 == wp->w_row - wp->w_top))) {
  103. bp->b_reframe = 0;
  104. /* Find end of screen plus one. */
  105. bp->b_page = dndn(bp, wp, bp->b_point);
  106. /* if we scroll to EOF we show 1 blank line at bottom of screen */
  107. if (pos(bp, bp->b_ebuf) <= bp->b_page) {
  108. bp->b_page = pos(bp, bp->b_ebuf);
  109. i = wp->w_rows - 1;
  110. } else {
  111. i = wp->w_rows - 0;
  112. }
  113. /* Scan backwards the required number of lines. */
  114. while (0 < i--)
  115. bp->b_page = upup(bp, wp, bp->b_page);
  116. }
  117. i = wp->w_top;
  118. j = wp->w_left;
  119. bp->b_epage = bp->b_page;
  120. // TODO: make this efficient
  121. set_parse_state(bp, bp->b_epage); /* are we in a multline comment ? */
  122. /* paint screen from top of page until we hit maxline */
  123. while (1) {
  124. /* reached point - store the cursor position */
  125. if (bp->b_point == bp->b_epage) {
  126. bp->b_row = i;
  127. bp->b_col = j;
  128. }
  129. p = ptr(bp, bp->b_epage);
  130. nch = 1;
  131. if (wp->w_top + wp->w_rows <= i || bp->b_ebuf <= p) /* maxline */
  132. break;
  133. if(curwp == wp && found_point == -1 && is_bracket(*ptr(bp, bp->b_point), FALSE) && (z = is_bracket(*p, TRUE)) != 0) {
  134. int dir = (z == '}' || z == ']' || z == ')' || z == '>');
  135. matching_bracket = find_matching_bracket(bp, wp, dir ? -1 : 1, TRUE);
  136. }
  137. nch = utf8_size(*p);
  138. if(wp == curwp &&
  139. bp->b_mark != NOMARK &&
  140. ((bp->b_mark <= bp->b_epage &&
  141. bp->b_point > bp->b_epage) ||
  142. (bp->b_point <= bp->b_epage &&
  143. bp->b_mark > bp->b_epage))
  144. ) {
  145. region = 1;
  146. } else if(matching_bracket >= 0 && bp->b_epage == matching_bracket) {
  147. region = 1;
  148. } else if(curwp == wp) {
  149. if(found_point > 0 &&
  150. search_dir == 1 &&
  151. found_point - searchlen <= bp->b_epage &&
  152. bp->b_epage < found_point)
  153. region = 1;
  154. else if(found_point > 0 &&
  155. search_dir == 2 &&
  156. found_point + searchlen >= bp->b_epage &&
  157. bp->b_epage > found_point)
  158. region = 1;
  159. else if(found_point > 0 && tolower(searchtext[0]) == tolower(*p) && b == 0) {
  160. if(isupper(searchtext[0]))
  161. case_sense = TRUE;
  162. while(b < searchlen) {
  163. if(isupper(searchtext[b])) {
  164. case_sense = TRUE;
  165. if(searchtext[b] == *(p+b)) {
  166. region = 1;
  167. } else {
  168. region = 0;
  169. b = 0;
  170. break;
  171. }
  172. } else if (case_sense) {
  173. if(searchtext[b] == *(p+b)) {
  174. region = 1;
  175. } else {
  176. region = 0;
  177. b = 0;
  178. break;
  179. }
  180. } else if(!case_sense && searchtext[b] == tolower(*(p+b))) {
  181. region = 1;
  182. } else {
  183. region = 0;
  184. b = 0;
  185. break;
  186. }
  187. b++;
  188. }
  189. b = b < 0 ? 0 : b - 1;
  190. } else if(b > 0) {
  191. b--;
  192. region = 1;
  193. } else {
  194. region = 0;
  195. b = 0;
  196. }
  197. }
  198. if(nch > 1) {
  199. j++;
  200. uint32_t uni;
  201. tb_utf8_char_to_unicode(&uni, (char *)p);
  202. tb_set_cell(j-1, i, uni, region ? TB_BLACK : TB_DEFAULT, region ? TB_WHITE : TB_DEFAULT);
  203. } else if(*p < 32 && *p != '\n' && *p != '\t') {
  204. char ctrl = unctrl(*p);
  205. tb_set_cell(j, i, '^', TB_RED, region ? TB_WHITE : TB_DEFAULT);
  206. tb_set_cell(j+1, i, ctrl, TB_RED, region ? TB_WHITE : TB_DEFAULT);
  207. j += 2;
  208. } else if (isprint(*p) || *p == '\t' || *p == '\n') {
  209. /* Figure out if the whitespace is trailing or not. */
  210. g = ptr(bp, bp->b_epage);
  211. while(isspace(*p) && *p != '\n' && *g != '\n' && isspace(*(g = ptr(bp, bp->b_epage + w))) && bp->b_point != bp->b_epage + w) {
  212. if(*g == '\n') {
  213. trailing_whitespace = TRUE;
  214. break;
  215. }
  216. w++;
  217. }
  218. w = 0;
  219. if(trailing_whitespace && *p != '\t') {
  220. j++;
  221. uint32_t uni;
  222. tb_utf8_char_to_unicode(&uni, (char *)p);
  223. tb_set_cell(j-1, i, uni, TB_DEFAULT, TB_RED);
  224. } else if(*p == '\t') {
  225. j += 4;
  226. for(int t = 4; t > 0; t--)
  227. tb_set_cell(j-t, i, 0x2500, TB_RED, region ? TB_WHITE : TB_DEFAULT);
  228. } else {
  229. j++;
  230. uint32_t uni;
  231. tb_utf8_char_to_unicode(&uni, (char *)p);
  232. token_type = parse_text(bp, bp->b_epage);
  233. switch(token_type) {
  234. case ID_SYMBOL: {
  235. color = TB_MAGENTA;
  236. break;
  237. }
  238. case ID_DOUBLE_STRING: {
  239. color = TB_GREEN;
  240. break;
  241. }
  242. case ID_SINGLE_STRING: {
  243. color = TB_GREEN;
  244. break;
  245. }
  246. case ID_BACK_STRING: {
  247. color = TB_CYAN;
  248. break;
  249. }
  250. case ID_DIGITS: {
  251. color = TB_CYAN;
  252. break;
  253. }
  254. case ID_LINE_COMMENT: {
  255. color = TB_BLUE;
  256. break;
  257. }
  258. case ID_BLOCK_COMMENT: {
  259. color = TB_BLUE;
  260. break;
  261. }
  262. default: {
  263. color = TB_DEFAULT;
  264. break;
  265. }
  266. }
  267. tb_set_cell(j-1, i, uni, region ? TB_BLACK : color, region ? TB_WHITE : TB_DEFAULT);
  268. }
  269. }
  270. trailing_whitespace = FALSE;
  271. if (*p == '\n' || wp->w_cols <= j - wp->w_left) {
  272. j -= wp->w_cols;
  273. if (j < wp->w_left)
  274. j = wp->w_left;
  275. ++i;
  276. }
  277. bp->b_epage = bp->b_epage + nch;
  278. }
  279. b2w(wp); /* save buffer stuff on window */
  280. modeline(wp);
  281. if (wp == curwp && flag) {
  282. dispmsg();
  283. tb_set_cursor(bp->b_col, bp->b_row); /* set cursor */
  284. }
  285. wp->w_update = FALSE;
  286. if(bp->b_row == prow && !moving_past) {
  287. bp->b_pcol = bp->b_col;
  288. wp->w_pcol = bp->b_col;
  289. }
  290. moving_past = FALSE;
  291. }
  292. void modeline(window_t *wp)
  293. {
  294. int i;
  295. char lch, mch, och;
  296. char mltemp[TEMPBUF];
  297. char *percent = NULL;
  298. point_t pg = lnstart(curbp, curbp->b_page),
  299. ebuf = pos(curbp, curbp->b_ebuf);
  300. float dec = 0.0, mod = 0.0;
  301. lch = (wp == curwp ? '=' : '-');
  302. mch = ((wp->w_bufp->b_flags & B_MODIFIED) ? '*' : lch);
  303. och = ((wp->w_bufp->b_flags & B_OVERWRITE) ? 'O' : lch);
  304. /* Based on the b_page, determine if you're at the top, bottom or
  305. somewhere in-between the buffer. If in-between, calculate the
  306. percentage from the bottom. We round up always because that's
  307. what Emacs does.
  308. */
  309. percent = (char *) malloc(sizeof(char *)*3);
  310. if(pg == 0) {
  311. percent = "Top";
  312. } else if(curbp->b_epage == ebuf) {
  313. percent = "Bot";
  314. } else {
  315. mod = curbp->b_page * 100 % ebuf;
  316. dec = curbp->b_page * 100.0 / ebuf;
  317. if(mod != 0)
  318. dec++;
  319. sprintf(percent, "%d%%", (int)(dec));
  320. }
  321. sprintf(mltemp, "%c%c%c %s %c%c (%d,%d) %c%c %s ", lch,och,mch, wp->w_bufp->b_bname,
  322. lch, lch, wp->w_bufp->b_line, wp->w_col - wp->w_left, lch, lch, percent);
  323. addstrxy(mltemp, wp->w_left, wp->w_top + wp->w_rows, 1);
  324. for (i = strlen(mltemp); i < wp->w_cols; i++)
  325. addchxy(lch, i+wp->w_left, wp->w_top + wp->w_rows, 1);
  326. }
  327. void dispmsg()
  328. {
  329. tb_set_cursor(0, MSGLINE);
  330. if (msgflag) {
  331. addstr(msgline);
  332. msgflag = FALSE;
  333. }
  334. clrtoeol(msgline, MSGLINE);
  335. }
  336. void display_prompt_and_response(char *prompt, char *response)
  337. {
  338. char full[NAME_MAX];
  339. strcpy(full, prompt);
  340. strcat(full, response);
  341. print_to_msgline(prompt);
  342. /* if we have a value print it and go to end of it */
  343. if (response[0] != '\0')
  344. addstr(response);
  345. clrtoeol(full, MSGLINE);
  346. }
  347. void trim_window(buffer_t *bp, window_t *wp)
  348. {
  349. point_t page = bp->b_page, ebuf = pos(bp, bp->b_ebuf);
  350. int complete_line = FALSE;
  351. for(int r = 0; r < wp->w_rows; r++) {
  352. complete_line = FALSE;
  353. for(int c = 0; c < wp->w_cols; c++) {
  354. char_t *p = page >= bp->b_epage ? (char_t *)"\n" : ptr(bp, page);
  355. if(*p == '\n' || page >= bp->b_epage) {
  356. complete_line = TRUE;
  357. int x = wp->w_left + c, y = wp->w_top +r;
  358. struct tb_cell *front_wide, *back_wide;
  359. uint32_t space = page > ebuf && c == 0 ? (uint32_t)'~' : (uint32_t)' ';
  360. int fg = page > ebuf && c == 0 ? TB_CYAN : TB_DEFAULT;
  361. int bg = TB_DEFAULT;
  362. cellbuf_get(&global.front, x, y, &front_wide);
  363. cellbuf_get(&global.back, x, y, &back_wide);
  364. send_attr(fg, bg);
  365. send_char(x, y, space);
  366. cell_set(front_wide, &space, 1, fg, bg);
  367. cell_set(back_wide, &space, 1, fg, bg);
  368. } else {
  369. if(*p == '\t') {
  370. page++;
  371. c += 3;
  372. } else if(*p < 32) {
  373. page++;
  374. c++;
  375. } else
  376. page += utf8_size(*p);
  377. }
  378. }
  379. if(complete_line)
  380. page++;
  381. }
  382. }
  383. void update_display()
  384. {
  385. window_t *wp;
  386. buffer_t *bp;
  387. bp = curwp->w_bufp;
  388. bp->b_cpoint = bp->b_point; /* cpoint only ever set here */
  389. /* only one window */
  390. if (wheadp->w_next == NULL) {
  391. display(curwp, TRUE);
  392. trim_window(curbp, curwp);
  393. tb_present();
  394. bp->b_psize = bp->b_size;
  395. return;
  396. }
  397. display(curwp, FALSE); /* this is key, we must call our win first to get accurate page and epage etc */
  398. trim_window(curbp, curwp);
  399. if(curwp->w_left != 0)
  400. for(int i = curwp->w_top; i < curwp->w_rows+curwp->w_top+1; i++)
  401. tb_set_cell(curwp->w_left - 1, i, '|', TB_WHITE, TB_DEFAULT);
  402. /* never curwp, but same buffer in different window or update flag set*/
  403. for (wp=wheadp; wp != NULL; wp = wp->w_next) {
  404. if (wp != curwp && (wp->w_bufp == bp || wp->w_update)) {
  405. w2b(wp);
  406. for(int i = wp->w_top; i < wp->w_rows+wp->w_top+1; i++)
  407. tb_set_cell(wp->w_left - 1, i, '|', TB_WHITE, TB_DEFAULT);
  408. display(wp, FALSE);
  409. // if(wp->w_bufp == bp || wp->w_update) {
  410. trim_window(wp->w_bufp, wp);
  411. // }
  412. }
  413. }
  414. /* now display our window and buffer */
  415. w2b(curwp);
  416. dispmsg();
  417. tb_set_cursor(curwp->w_col, curwp->w_row); /* set cursor for curwp */
  418. tb_present();
  419. bp->b_psize = bp->b_size; /* now safe to save previous size for next time */
  420. }
  421. void w2b(window_t *w)
  422. {
  423. w->w_bufp->b_point = w->w_point;
  424. w->w_bufp->b_page = w->w_page;
  425. w->w_bufp->b_epage = w->w_epage;
  426. w->w_bufp->b_row = w->w_row;
  427. w->w_bufp->b_col = w->w_col;
  428. w->w_bufp->b_pcol = w->w_pcol;
  429. /* fixup pointers in other windows of the same buffer, if size of edit text changed */
  430. if (w->w_bufp->b_point > w->w_bufp->b_cpoint) {
  431. w->w_bufp->b_point += (w->w_bufp->b_size - w->w_bufp->b_psize);
  432. w->w_bufp->b_page += (w->w_bufp->b_size - w->w_bufp->b_psize);
  433. w->w_bufp->b_epage += (w->w_bufp->b_size - w->w_bufp->b_psize);
  434. }
  435. }
  436. void b2w(window_t *w)
  437. {
  438. w->w_point = w->w_bufp->b_point;
  439. w->w_page = w->w_bufp->b_page;
  440. w->w_epage = w->w_bufp->b_epage;
  441. w->w_row = w->w_bufp->b_row;
  442. w->w_col = w->w_bufp->b_col;
  443. w->w_pcol = w->w_bufp->b_pcol;
  444. 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);
  445. }
  446. /* flag = default answer, FALSE=n, TRUE=y */
  447. int yesno(int flag)
  448. {
  449. struct tb_event ev;
  450. tb_present();
  451. if(tb_poll_event(&ev) != TB_OK) return 0;
  452. if (ev.ch == '\r' || ev.ch == '\n')
  453. return (flag);
  454. return (tolower(ev.ch) == 'y');
  455. }
  456. char yesnomaybeso(char d)
  457. {
  458. struct tb_event ev;
  459. tb_present();
  460. if(tb_poll_event(&ev) != TB_OK) return 0;
  461. if (ev.ch == '\r' || ev.ch == '\n')
  462. return (d);
  463. return tolower(ev.ch);
  464. }
  465. void print_tb(const char *str, int x, int y, uint16_t fg, uint16_t bg)
  466. {
  467. while (*str) {
  468. uint32_t uni;
  469. str += tb_utf8_char_to_unicode(&uni, str);
  470. tb_set_cell(x, y, uni, fg, bg);
  471. x++;
  472. }
  473. }
  474. void printf_tb(int x, int y, uint16_t fg, uint16_t bg, const char *fmt, ...)
  475. {
  476. char buf[4096];
  477. va_list vl;
  478. va_start(vl, fmt);
  479. vsnprintf(buf, sizeof(buf), fmt, vl);
  480. va_end(vl);
  481. print_tb(buf, x, y, fg, bg);
  482. }
  483. void addstr(const char *str)
  484. {
  485. printf_tb(global.cursor_x, global.cursor_y, TB_DEFAULT, TB_DEFAULT, str);
  486. tb_set_cursor(global.cursor_x+strlen(str), global.cursor_y);
  487. }
  488. void addch(char c)
  489. {
  490. printf_tb(global.cursor_x, global.cursor_y, TB_DEFAULT, TB_DEFAULT, "%c", c);
  491. tb_set_cursor(global.cursor_x+1, global.cursor_y);
  492. }
  493. /*
  494. Print char at point x,y. If within the region, highlight it.
  495. */
  496. void addchxy(char c, int x, int y, int region)
  497. {
  498. int fg = region == 0 ? TB_DEFAULT : TB_BLACK;
  499. int bg = region == 0 ? TB_DEFAULT : TB_WHITE;
  500. if(c != '\n' && c != '\r')
  501. printf_tb(x, y, fg, bg, "%c", c);
  502. else
  503. printf_tb(x, y, fg, bg, " ");
  504. }
  505. void addstrxy(char *c, int x, int y, int region)
  506. {
  507. int fg = region == 0 ? TB_DEFAULT : TB_BLACK;
  508. int bg = region == 0 ? TB_DEFAULT : TB_WHITE;
  509. printf_tb(x, y, fg, bg, "%s", c);
  510. }
  511. void clrtoeol(const char *str, int line)
  512. {
  513. int l = strlen(str);
  514. int size = COLS-l;
  515. char blanks[size], entire[COLS];
  516. if(size == 0) {
  517. for(int i=0; i<size; i++) {
  518. entire[i]=' ';
  519. }
  520. printf_tb(l, line, TB_DEFAULT, TB_DEFAULT, "%s", entire);
  521. } else {
  522. for(int i=0; i<size; i++) {
  523. blanks[i]=' ';
  524. }
  525. printf_tb(l, line, TB_DEFAULT, TB_DEFAULT, "%s", blanks);
  526. }
  527. }