editdraw.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /* editor text drawing.
  2. Copyright (C) 1996, 1997, 1998, 2001, 2002, 2003, 2004, 2005, 2006,
  3. 2007 Free Software Foundation, Inc.
  4. Authors: 1996, 1997 Paul Sheer
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301, USA.
  17. */
  18. /** \file
  19. * \brief Source: editor text drawing
  20. * \author Paul Sheer
  21. * \date 1996, 1997
  22. */
  23. #include <config.h>
  24. #include <stdlib.h>
  25. #include <stdio.h>
  26. #include <stdarg.h>
  27. #include <sys/types.h>
  28. #include <unistd.h>
  29. #include <string.h>
  30. #include <ctype.h>
  31. #include <errno.h>
  32. #include <sys/stat.h>
  33. #include "../src/global.h"
  34. #include "edit-impl.h"
  35. #include "edit-widget.h"
  36. #define MAX_LINE_LEN 1024
  37. #include "../src/tty/tty.h" /* tty_printf() */
  38. #include "../src/skin/skin.h"
  39. #include "../src/tty/key.h" /* is_idle() */
  40. #include "../src/widget.h" /* buttonbar_redraw() */
  41. #include "../src/charsets.h"
  42. #include "../src/strutil.h" /* utf string functions */
  43. #include "../src/main.h" /* source_codepage */
  44. /* Text styles */
  45. #define MOD_ABNORMAL (1 << 8)
  46. #define MOD_BOLD (1 << 9)
  47. #define MOD_MARKED (1 << 10)
  48. #define MOD_CURSOR (1 << 11)
  49. #define MOD_WHITESPACE (1 << 12)
  50. #define FONT_OFFSET_X 0
  51. #define FONT_OFFSET_Y 0
  52. #define FIXED_FONT 1
  53. #define FONT_PIX_PER_LINE 1
  54. #define FONT_MEAN_WIDTH 1
  55. /* Toggles statusbar draw style */
  56. int simple_statusbar = 0;
  57. static inline void
  58. status_string (WEdit * edit, char *s, int w)
  59. {
  60. char byte_str[16];
  61. unsigned char cur_byte = 0;
  62. unsigned int cur_utf = 0;
  63. int cw = 1;
  64. /*
  65. * If we are at the end of file, print <EOF>,
  66. * otherwise print the current character as is (if printable),
  67. * as decimal and as hex.
  68. */
  69. if (edit->curs1 < edit->last_byte) {
  70. if ( !edit->utf8 ) {
  71. cur_byte = edit_get_byte (edit, edit->curs1);
  72. g_snprintf (byte_str, sizeof (byte_str), "%4d 0x%03X",
  73. (int) cur_byte,
  74. (unsigned) cur_byte);
  75. } else {
  76. cur_utf = edit_get_utf (edit, edit->curs1, &cw);
  77. if ( cw > 0 ) {
  78. g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
  79. (unsigned) cur_utf,
  80. (unsigned) cur_utf);
  81. } else {
  82. cur_utf = edit_get_byte (edit, edit->curs1);
  83. g_snprintf (byte_str, sizeof (byte_str), "%04d 0x%03X",
  84. (int) cur_utf,
  85. (unsigned) cur_utf);
  86. }
  87. }
  88. } else {
  89. strcpy (byte_str, "<EOF> ");
  90. }
  91. /* The field lengths just prevent the status line from shortening too much */
  92. if (simple_statusbar)
  93. g_snprintf (s, w,
  94. "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s",
  95. edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
  96. edit->modified ? 'M' : '-',
  97. edit->macro_i < 0 ? '-' : 'R',
  98. edit->overwrite == 0 ? '-' : 'O',
  99. edit->curs_col + edit->over_col,
  100. edit->curs_line + 1,
  101. edit->total_lines + 1,
  102. edit->curs1,
  103. edit->last_byte,
  104. byte_str,
  105. #ifdef HAVE_CHARSET
  106. source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
  107. #else
  108. ""
  109. #endif
  110. );
  111. else
  112. g_snprintf (s, w,
  113. "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s",
  114. edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
  115. edit->modified ? 'M' : '-',
  116. edit->macro_i < 0 ? '-' : 'R',
  117. edit->overwrite == 0 ? '-' : 'O',
  118. edit->curs_col + edit->over_col,
  119. edit->start_line + 1,
  120. edit->curs_row,
  121. edit->curs_line + 1,
  122. edit->total_lines + 1,
  123. edit->curs1,
  124. edit->last_byte,
  125. byte_str,
  126. #ifdef HAVE_CHARSET
  127. source_codepage >= 0 ? get_codepage_id (source_codepage) : ""
  128. #else
  129. ""
  130. #endif
  131. );
  132. }
  133. static inline void
  134. printwstr (const char *s, int len)
  135. {
  136. if (len > 0)
  137. tty_printf ("%-*.*s", len, len, s);
  138. }
  139. /* Draw the status line at the top of the widget. The size of the filename
  140. * field varies depending on the width of the screen and the length of
  141. * the filename. */
  142. void
  143. edit_status (WEdit *edit)
  144. {
  145. const int w = edit->widget.cols;
  146. const size_t status_size = w + 1;
  147. char * const status = g_malloc (status_size);
  148. int status_len;
  149. const char *fname = "";
  150. int fname_len;
  151. const int gap = 3; /* between the filename and the status */
  152. const int right_gap = 5; /* at the right end of the screen */
  153. const int preferred_fname_len = 16;
  154. status_string (edit, status, status_size);
  155. status_len = (int) str_term_width1 (status);
  156. if (edit->filename)
  157. fname = edit->filename;
  158. fname_len = str_term_width1 (fname);
  159. if (fname_len < preferred_fname_len)
  160. fname_len = preferred_fname_len;
  161. if (fname_len + gap + status_len + right_gap >= w) {
  162. if (preferred_fname_len + gap + status_len + right_gap >= w)
  163. fname_len = preferred_fname_len;
  164. else
  165. fname_len = w - (gap + status_len + right_gap);
  166. fname = str_trunc (fname, fname_len);
  167. }
  168. widget_move (edit, 0, 0);
  169. tty_setcolor (SELECTED_COLOR);
  170. printwstr (fname, fname_len + gap);
  171. printwstr (status, w - (fname_len + gap));
  172. if (simple_statusbar && edit->widget.cols > 30) {
  173. size_t percent;
  174. if (edit->total_lines + 1 != 0)
  175. percent = (edit->curs_line + 1) * 100 / (edit->total_lines + 1);
  176. else
  177. percent = 100;
  178. widget_move (edit, 0, edit->widget.cols - 5);
  179. tty_printf (" %3d%%", percent);
  180. }
  181. tty_setcolor (EDITOR_NORMAL_COLOR);
  182. g_free (status);
  183. }
  184. /* this scrolls the text so that cursor is on the screen */
  185. void edit_scroll_screen_over_cursor (WEdit * edit)
  186. {
  187. int p;
  188. int outby;
  189. int b_extreme, t_extreme, l_extreme, r_extreme;
  190. if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
  191. return;
  192. edit->num_widget_columns -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  193. edit->num_widget_lines -= EDIT_TEXT_VERTICAL_OFFSET - 1;
  194. r_extreme = EDIT_RIGHT_EXTREME;
  195. l_extreme = EDIT_LEFT_EXTREME;
  196. b_extreme = EDIT_BOTTOM_EXTREME;
  197. t_extreme = EDIT_TOP_EXTREME;
  198. if (edit->found_len) {
  199. b_extreme = max (edit->num_widget_lines / 4, b_extreme);
  200. t_extreme = max (edit->num_widget_lines / 4, t_extreme);
  201. }
  202. if (b_extreme + t_extreme + 1 > edit->num_widget_lines) {
  203. int n;
  204. n = b_extreme + t_extreme;
  205. b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
  206. t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
  207. }
  208. if (l_extreme + r_extreme + 1 > edit->num_widget_columns) {
  209. int n;
  210. n = l_extreme + t_extreme;
  211. l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
  212. r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
  213. }
  214. p = edit_get_col (edit) + edit->over_col;
  215. edit_update_curs_row (edit);
  216. outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
  217. if (outby > 0)
  218. edit_scroll_right (edit, outby);
  219. outby = l_extreme - p - edit->start_col;
  220. if (outby > 0)
  221. edit_scroll_left (edit, outby);
  222. p = edit->curs_row;
  223. outby = p - edit->num_widget_lines + 1 + b_extreme;
  224. if (outby > 0)
  225. edit_scroll_downward (edit, outby);
  226. outby = t_extreme - p;
  227. if (outby > 0)
  228. edit_scroll_upward (edit, outby);
  229. edit_update_curs_row (edit);
  230. edit->num_widget_lines += EDIT_TEXT_VERTICAL_OFFSET - 1;
  231. edit->num_widget_columns += EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  232. }
  233. #define edit_move(x,y) widget_move(edit, y, x);
  234. struct line_s {
  235. unsigned int ch;
  236. unsigned int style;
  237. };
  238. static inline void
  239. print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
  240. long end_col, struct line_s line[], char *status)
  241. {
  242. struct line_s *p;
  243. int x = start_col_real;
  244. int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  245. int y = row + EDIT_TEXT_VERTICAL_OFFSET;
  246. int cols_to_skip = abs (x);
  247. tty_setcolor (EDITOR_NORMAL_COLOR);
  248. if (!show_right_margin && -edit->start_col < option_word_wrap_line_length) {
  249. tty_draw_hline (edit->widget.y + y, edit->widget.x + x1,
  250. ' ', end_col + 1 - start_col);
  251. } else {
  252. tty_draw_hline (edit->widget.y + y,
  253. edit->widget.x + x1,
  254. ' ',
  255. option_word_wrap_line_length - edit->start_col);
  256. tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
  257. tty_draw_hline (edit->widget.y + y,
  258. edit->widget.x + x1 + option_word_wrap_line_length + edit->start_col,
  259. ' ',
  260. end_col + 1 - start_col);
  261. }
  262. if (option_line_state) {
  263. int i;
  264. for (i = 0; i < LINE_STATE_WIDTH; i++)
  265. if (status[i] == '\0')
  266. status[i] = ' ';
  267. tty_setcolor (LINE_STATE_COLOR);
  268. edit_move (x1 + FONT_OFFSET_X - option_line_state_width, y + FONT_OFFSET_Y);
  269. tty_print_string (status);
  270. }
  271. edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
  272. p = line;
  273. int i = 1;
  274. while (p->ch) {
  275. int style;
  276. unsigned int textchar;
  277. int color;
  278. if (cols_to_skip) {
  279. p++;
  280. cols_to_skip--;
  281. continue;
  282. }
  283. style = p->style & 0xFF00;
  284. textchar = p->ch;
  285. color = p->style >> 16;
  286. if (style & MOD_ABNORMAL) {
  287. /* Non-printable - use black background */
  288. color = 0;
  289. }
  290. if (style & MOD_WHITESPACE) {
  291. if (style & MOD_MARKED) {
  292. textchar = ' ';
  293. tty_setcolor (EDITOR_MARKED_COLOR);
  294. } else {
  295. #if 0
  296. if (color != EDITOR_NORMAL_COLOR) {
  297. textchar = ' ';
  298. tty_lowlevel_setcolor (color);
  299. } else
  300. #endif
  301. tty_setcolor (EDITOR_WHITESPACE_COLOR);
  302. }
  303. } else {
  304. if (style & MOD_BOLD) {
  305. tty_setcolor (EDITOR_BOLD_COLOR);
  306. } else if (style & MOD_MARKED) {
  307. tty_setcolor (EDITOR_MARKED_COLOR);
  308. } else {
  309. tty_lowlevel_setcolor (color);
  310. }
  311. }
  312. if (show_right_margin) {
  313. if (i > option_word_wrap_line_length + edit->start_col)
  314. tty_setcolor (EDITOR_RIGHT_MARGIN_COLOR);
  315. i++;
  316. }
  317. tty_print_anychar (textchar);
  318. p++;
  319. }
  320. }
  321. int visible_tabs = 1, visible_tws = 1;
  322. /* b is a pointer to the beginning of the line */
  323. static void
  324. edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
  325. long end_col)
  326. {
  327. struct line_s line[MAX_LINE_LEN];
  328. struct line_s *p = line;
  329. long m1 = 0, m2 = 0, q, c1, c2;
  330. int col, start_col_real;
  331. unsigned int c;
  332. int color;
  333. int abn_style;
  334. int i;
  335. int utf8lag = 0;
  336. unsigned int cur_line = 0;
  337. int book_mark = 0;
  338. char line_stat[LINE_STATE_WIDTH + 1];
  339. if (row > edit->num_widget_lines - EDIT_TEXT_VERTICAL_OFFSET) {
  340. return;
  341. }
  342. if (book_mark_query_color(edit, edit->start_line + row, BOOK_MARK_COLOR)) {
  343. book_mark = BOOK_MARK_COLOR;
  344. } else if (book_mark_query_color(edit, edit->start_line + row, BOOK_MARK_FOUND_COLOR)) {
  345. book_mark = BOOK_MARK_FOUND_COLOR;
  346. }
  347. if (book_mark)
  348. abn_style = book_mark << 16;
  349. else
  350. abn_style = MOD_ABNORMAL;
  351. end_col -= EDIT_TEXT_HORIZONTAL_OFFSET + option_line_state_width;
  352. edit_get_syntax_color (edit, b - 1, &color);
  353. q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
  354. start_col_real = (col =
  355. (int) edit_move_forward3 (edit, b, 0,
  356. q)) + edit->start_col;
  357. if ( option_line_state ) {
  358. cur_line = edit->start_line + row;
  359. if ( cur_line <= (unsigned int) edit->total_lines ) {
  360. g_snprintf (line_stat, LINE_STATE_WIDTH + 1, "%7i ", cur_line + 1);
  361. } else {
  362. memset(line_stat, ' ', LINE_STATE_WIDTH);
  363. line_stat[LINE_STATE_WIDTH] = '\0';
  364. }
  365. if (book_mark_query_color (edit, cur_line, BOOK_MARK_COLOR)){
  366. g_snprintf (line_stat, 2, "*");
  367. }
  368. }
  369. if (col + 16 > -edit->start_col) {
  370. eval_marks (edit, &m1, &m2);
  371. if (row <= edit->total_lines - edit->start_line) {
  372. long tws = 0;
  373. if (tty_use_colors () && visible_tws) {
  374. tws = edit_eol (edit, b);
  375. while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' '
  376. || c == '\t'))
  377. tws--;
  378. }
  379. while (col <= end_col - edit->start_col) {
  380. int cw = 1;
  381. p->ch = 0;
  382. p->style = 0;
  383. if (q == edit->curs1)
  384. p->style |= MOD_CURSOR;
  385. if (q >= m1 && q < m2) {
  386. if (column_highlighting) {
  387. int x;
  388. x = edit_move_forward3 (edit, b, 0, q);
  389. c1 = min (edit->column1, edit->column2);
  390. c2 = max (edit->column1, edit->column2);
  391. if (x >= c1 && x < c2)
  392. p->style |= MOD_MARKED;
  393. } else
  394. p->style |= MOD_MARKED;
  395. }
  396. if (q == edit->bracket)
  397. p->style |= MOD_BOLD;
  398. if (q >= edit->found_start
  399. && q < edit->found_start + edit->found_len)
  400. p->style |= MOD_BOLD;
  401. if ( !edit->utf8 ) {
  402. c = edit_get_byte (edit, q);
  403. } else {
  404. c = edit_get_utf (edit, q, &cw);
  405. }
  406. /* we don't use bg for mc - fg contains both */
  407. if (book_mark) {
  408. p->style |= book_mark << 16;
  409. } else {
  410. edit_get_syntax_color (edit, q, &color);
  411. p->style |= color << 16;
  412. }
  413. switch (c) {
  414. case '\n':
  415. col = (end_col + utf8lag) - edit->start_col + 1; /* quit */
  416. break;
  417. case '\t':
  418. i = TAB_SIZE - ((int) col % TAB_SIZE);
  419. col += i;
  420. if (tty_use_colors() &&
  421. ((visible_tabs || (visible_tws && q >= tws)) && enable_show_tabs_tws)) {
  422. if (p->style & MOD_MARKED)
  423. c = p->style;
  424. else if (book_mark)
  425. c |= book_mark << 16;
  426. else
  427. c = p->style | MOD_WHITESPACE;
  428. if (i > 2) {
  429. p->ch = '<';
  430. p->style = c;
  431. p++;
  432. while (--i > 1) {
  433. p->ch = '-';
  434. p->style = c;
  435. p++;
  436. }
  437. p->ch = '>';
  438. p->style = c;
  439. p++;
  440. } else if (i > 1) {
  441. p->ch = '<';
  442. p->style = c;
  443. p++;
  444. p->ch = '>';
  445. p->style = c;
  446. p++;
  447. } else {
  448. p->ch = '>';
  449. p->style = c;
  450. p++;
  451. }
  452. } else if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
  453. p->ch = '.';
  454. p->style |= MOD_WHITESPACE;
  455. c = p->style & ~MOD_CURSOR;
  456. p++;
  457. while (--i) {
  458. p->ch = ' ';
  459. p->style = c;
  460. p++;
  461. }
  462. } else {
  463. p->ch |= ' ';
  464. c = p->style & ~MOD_CURSOR;
  465. p++;
  466. while (--i) {
  467. p->ch = ' ';
  468. p->style = c;
  469. p++;
  470. }
  471. }
  472. break;
  473. case ' ':
  474. if (tty_use_colors() && visible_tws && q >= tws && enable_show_tabs_tws) {
  475. p->ch = '.';
  476. p->style |= MOD_WHITESPACE;
  477. p++;
  478. col++;
  479. break;
  480. }
  481. /* fallthrough */
  482. default:
  483. #ifdef HAVE_CHARSET
  484. if ( utf8_display ) {
  485. if ( !edit->utf8 ) {
  486. c = convert_from_8bit_to_utf_c ((unsigned char) c, edit->converter);
  487. }
  488. } else if ( edit->utf8 )
  489. c = convert_from_utf_to_current_c (c, edit->converter);
  490. else
  491. #endif
  492. c = convert_to_display_c (c);
  493. /* Caret notation for control characters */
  494. if (c < 32) {
  495. p->ch = '^';
  496. p->style = abn_style;
  497. p++;
  498. p->ch = c + 0x40;
  499. p->style = abn_style;
  500. p++;
  501. col += 2;
  502. break;
  503. }
  504. if (c == 127) {
  505. p->ch = '^';
  506. p->style = abn_style;
  507. p++;
  508. p->ch = '?';
  509. p->style = abn_style;
  510. p++;
  511. col += 2;
  512. break;
  513. }
  514. if (!edit->utf8) {
  515. if ( ( utf8_display && g_unichar_isprint (c) ) ||
  516. ( !utf8_display && is_printable (c) ) ) {
  517. p->ch = c;
  518. p++;
  519. } else {
  520. p->ch = '.';
  521. p->style = abn_style;
  522. p++;
  523. }
  524. } else {
  525. if ( g_unichar_isprint (c) ) {
  526. p->ch = c;
  527. p++;
  528. } else {
  529. p->ch = '.';
  530. p->style = abn_style;
  531. p++;
  532. }
  533. }
  534. col++;
  535. break;
  536. } /* case */
  537. q++;
  538. if ( cw > 1) {
  539. q += cw - 1;
  540. }
  541. }
  542. }
  543. } else {
  544. start_col_real = start_col = 0;
  545. }
  546. p->ch = '\0';
  547. print_to_widget (edit, row, start_col, start_col_real, end_col, line, line_stat);
  548. }
  549. #define key_pending(x) (!is_idle())
  550. static inline void
  551. edit_draw_this_char (WEdit * edit, long curs, long row)
  552. {
  553. int b = edit_bol (edit, curs);
  554. edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
  555. }
  556. /* cursor must be in screen for other than REDRAW_PAGE passed in force */
  557. static inline void
  558. render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
  559. long end_column)
  560. {
  561. long row = 0, curs_row;
  562. static long prev_curs_row = 0;
  563. static long prev_curs = 0;
  564. int force = edit->force;
  565. long b;
  566. /*
  567. * If the position of the page has not moved then we can draw the cursor
  568. * character only. This will prevent line flicker when using arrow keys.
  569. */
  570. if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
  571. if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
  572. start_row = 0;
  573. end_row = edit->num_widget_lines - 1;
  574. start_column = 0;
  575. end_column = edit->num_widget_columns - 1;
  576. }
  577. if (force & REDRAW_PAGE) {
  578. row = start_row;
  579. b = edit_move_forward (edit, edit->start_display, start_row, 0);
  580. while (row <= end_row) {
  581. if (key_pending (edit))
  582. goto exit_render;
  583. edit_draw_this_line (edit, b, row, start_column, end_column);
  584. b = edit_move_forward (edit, b, 1, 0);
  585. row++;
  586. }
  587. } else {
  588. curs_row = edit->curs_row;
  589. if (force & REDRAW_BEFORE_CURSOR) {
  590. if (start_row < curs_row) {
  591. long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
  592. row = start_row;
  593. b = edit->start_display;
  594. while (row <= upto) {
  595. if (key_pending (edit))
  596. goto exit_render;
  597. edit_draw_this_line (edit, b, row, start_column, end_column);
  598. b = edit_move_forward (edit, b, 1, 0);
  599. }
  600. }
  601. }
  602. /* if (force & REDRAW_LINE) ---> default */
  603. b = edit_bol (edit, edit->curs1);
  604. if (curs_row >= start_row && curs_row <= end_row) {
  605. if (key_pending (edit))
  606. goto exit_render;
  607. edit_draw_this_line (edit, b, curs_row, start_column, end_column);
  608. }
  609. if (force & REDRAW_AFTER_CURSOR) {
  610. if (end_row > curs_row) {
  611. row = curs_row + 1 < start_row ? start_row : curs_row + 1;
  612. b = edit_move_forward (edit, b, 1, 0);
  613. while (row <= end_row) {
  614. if (key_pending (edit))
  615. goto exit_render;
  616. edit_draw_this_line (edit, b, row, start_column, end_column);
  617. b = edit_move_forward (edit, b, 1, 0);
  618. row++;
  619. }
  620. }
  621. }
  622. if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
  623. row = curs_row - 1;
  624. b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
  625. if (row >= start_row && row <= end_row) {
  626. if (key_pending (edit))
  627. goto exit_render;
  628. edit_draw_this_line (edit, b, row, start_column, end_column);
  629. }
  630. }
  631. if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
  632. row = curs_row + 1;
  633. b = edit_bol (edit, edit->curs1);
  634. b = edit_move_forward (edit, b, 1, 0);
  635. if (row >= start_row && row <= end_row) {
  636. if (key_pending (edit))
  637. goto exit_render;
  638. edit_draw_this_line (edit, b, row, start_column, end_column);
  639. }
  640. }
  641. }
  642. } else {
  643. if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
  644. edit_draw_this_char (edit, prev_curs, prev_curs_row);
  645. edit_draw_this_char (edit, edit->curs1, edit->curs_row);
  646. } else {
  647. edit_draw_this_char (edit, edit->curs1, edit->curs_row);
  648. edit_draw_this_char (edit, prev_curs, prev_curs_row);
  649. }
  650. }
  651. edit->force = 0;
  652. prev_curs_row = edit->curs_row;
  653. prev_curs = edit->curs1;
  654. exit_render:
  655. edit->screen_modified = 0;
  656. }
  657. static inline void
  658. edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
  659. {
  660. if (page) /* if it was an expose event, 'page' would be set */
  661. edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
  662. if (edit->force & REDRAW_COMPLETELY)
  663. buttonbar_redraw (find_buttonbar (edit->widget.parent));
  664. render_edit_text (edit, row_start, col_start, row_end, col_end);
  665. /*
  666. * edit->force != 0 means a key was pending and the redraw
  667. * was halted, so next time we must redraw everything in case stuff
  668. * was left undrawn from a previous key press.
  669. */
  670. if (edit->force)
  671. edit->force |= REDRAW_PAGE;
  672. }
  673. void edit_render_keypress (WEdit * edit)
  674. {
  675. edit_render (edit, 0, 0, 0, 0, 0);
  676. }