display.c 14 KB

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