editdraw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. #include <config.h>
  19. #include <stdlib.h>
  20. #include <stdio.h>
  21. #include <stdarg.h>
  22. #include <sys/types.h>
  23. #ifdef HAVE_UNISTD_H
  24. # include <unistd.h>
  25. #endif
  26. #include <string.h>
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <sys/stat.h>
  30. #include "../src/global.h"
  31. #include "edit.h"
  32. #include "edit-widget.h"
  33. #define MAX_LINE_LEN 1024
  34. #include "../src/color.h" /* EDITOR_NORMAL_COLOR */
  35. #include "../src/tty.h" /* attrset() */
  36. #include "../src/widget.h" /* buttonbar_redraw() */
  37. #include "../src/key.h" /* is_idle() */
  38. #include "../src/charsets.h"
  39. /* Text styles */
  40. #define MOD_ABNORMAL (1 << 8)
  41. #define MOD_BOLD (1 << 9)
  42. #define MOD_MARKED (1 << 10)
  43. #define MOD_CURSOR (1 << 11)
  44. #define MOD_WHITESPACE (1 << 12)
  45. #define FONT_OFFSET_X 0
  46. #define FONT_OFFSET_Y 0
  47. #define FIXED_FONT 1
  48. #define FONT_PIX_PER_LINE 1
  49. #define FONT_MEAN_WIDTH 1
  50. static void status_string (WEdit * edit, char *s, int w)
  51. {
  52. char byte_str[16];
  53. /*
  54. * If we are at the end of file, print <EOF>,
  55. * otherwise print the current character as is (if printable),
  56. * as decimal and as hex.
  57. */
  58. if (edit->curs1 < edit->last_byte) {
  59. mc_wchar_t cur_byte = edit_get_byte (edit, edit->curs1);
  60. mc_wchar_t cur_byte2 = cur_byte;
  61. #ifndef UTF8
  62. g_snprintf (byte_str, sizeof (byte_str), "%c %3d 0x%02X",
  63. is_printable (cur_byte) ? cur_byte : '.',
  64. #else /* UTF8 */
  65. /* In 8-bit locales show the byte itself instead of its Unicode value */
  66. if (MB_CUR_MAX == 1) {
  67. unsigned char cur_8bit_byte;
  68. mbstate_t mbs;
  69. memset (&mbs, 0, sizeof (mbs));
  70. if (wcrtomb(&cur_8bit_byte, cur_byte, &mbs) == 1) {
  71. cur_byte = cur_8bit_byte;
  72. }
  73. }
  74. g_snprintf (byte_str, sizeof(byte_str), "%lc %3d 0x%02X",
  75. iswprint(cur_byte2) ? cur_byte2 : '.',
  76. #endif /* UTF8 */
  77. (int) cur_byte,
  78. (unsigned) cur_byte);
  79. } else {
  80. strcpy (byte_str, "<EOF>");
  81. }
  82. /* The field lengths just prevent the status line from shortening too much */
  83. g_snprintf (s, w,
  84. "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb)= %s",
  85. edit->mark1 != edit->mark2 ? ( column_highlighting ? 'C' : 'B') : '-',
  86. edit->modified ? 'M' : '-',
  87. edit->macro_i < 0 ? '-' : 'R',
  88. edit->overwrite == 0 ? '-' : 'O',
  89. edit->curs_col,
  90. edit->start_line + 1,
  91. edit->curs_row,
  92. edit->curs_line + 1,
  93. edit->total_lines + 1,
  94. edit->curs1,
  95. edit->last_byte,
  96. byte_str);
  97. }
  98. static inline void
  99. printwstr (const char *s, int len)
  100. {
  101. if (len > 0)
  102. tty_printf ("%-*.*s", len, len, s);
  103. }
  104. /* Draw the status line at the top of the widget. The size of the filename
  105. * field varies depending on the width of the screen and the length of
  106. * the filename. */
  107. void
  108. edit_status (WEdit *edit)
  109. {
  110. const int w = edit->widget.cols;
  111. const size_t status_size = w + 1;
  112. char * const status = g_malloc (status_size);
  113. int status_len;
  114. const char *fname = "";
  115. int fname_len;
  116. const int gap = 3; /* between the filename and the status */
  117. const int right_gap = 2; /* at the right end of the screen */
  118. const int preferred_fname_len = 16;
  119. status_string (edit, status, status_size);
  120. status_len = (int) strlen (status);
  121. if (edit->filename)
  122. fname = edit->filename;
  123. fname_len = strlen(fname);
  124. if (fname_len < preferred_fname_len)
  125. fname_len = preferred_fname_len;
  126. if (fname_len + gap + status_len + right_gap >= w) {
  127. if (preferred_fname_len + gap + status_len + right_gap >= w)
  128. fname_len = preferred_fname_len;
  129. else
  130. fname_len = w - (gap + status_len + right_gap);
  131. fname = name_trunc (fname, fname_len);
  132. }
  133. widget_move (edit, 0, 0);
  134. attrset (SELECTED_COLOR);
  135. printwstr (fname, fname_len + gap);
  136. printwstr (status, w - (fname_len + gap));
  137. attrset (EDITOR_NORMAL_COLOR);
  138. g_free (status);
  139. }
  140. /* this scrolls the text so that cursor is on the screen */
  141. void edit_scroll_screen_over_cursor (WEdit * edit)
  142. {
  143. int p;
  144. int outby;
  145. int b_extreme, t_extreme, l_extreme, r_extreme;
  146. if (edit->num_widget_lines <= 0 || edit->num_widget_columns <= 0)
  147. return;
  148. r_extreme = EDIT_RIGHT_EXTREME;
  149. l_extreme = EDIT_LEFT_EXTREME;
  150. b_extreme = EDIT_BOTTOM_EXTREME;
  151. t_extreme = EDIT_TOP_EXTREME;
  152. if (edit->found_len) {
  153. b_extreme = max (edit->num_widget_lines / 4, b_extreme);
  154. t_extreme = max (edit->num_widget_lines / 4, t_extreme);
  155. }
  156. if (b_extreme + t_extreme + 1 > edit->num_widget_lines) {
  157. int n;
  158. n = b_extreme + t_extreme;
  159. b_extreme = (b_extreme * (edit->num_widget_lines - 1)) / n;
  160. t_extreme = (t_extreme * (edit->num_widget_lines - 1)) / n;
  161. }
  162. if (l_extreme + r_extreme + 1 > edit->num_widget_columns) {
  163. int n;
  164. n = l_extreme + t_extreme;
  165. l_extreme = (l_extreme * (edit->num_widget_columns - 1)) / n;
  166. r_extreme = (r_extreme * (edit->num_widget_columns - 1)) / n;
  167. }
  168. p = edit_get_col (edit);
  169. edit_update_curs_row (edit);
  170. outby = p + edit->start_col - edit->num_widget_columns + 1 + (r_extreme + edit->found_len);
  171. if (outby > 0)
  172. edit_scroll_right (edit, outby);
  173. outby = l_extreme - p - edit->start_col;
  174. if (outby > 0)
  175. edit_scroll_left (edit, outby);
  176. p = edit->curs_row;
  177. outby = p - edit->num_widget_lines + 1 + b_extreme;
  178. if (outby > 0)
  179. edit_scroll_downward (edit, outby);
  180. outby = t_extreme - p;
  181. if (outby > 0)
  182. edit_scroll_upward (edit, outby);
  183. edit_update_curs_row (edit);
  184. }
  185. #define set_color(font) attrset (font)
  186. #define edit_move(x,y) widget_move(edit, y, x);
  187. /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
  188. #ifdef HAVE_SLANG
  189. #define lowlevel_set_color(x) SLsmg_set_color(x & 0x7F)
  190. #else
  191. #define lowlevel_set_color(x) attrset(MY_COLOR_PAIR(color))
  192. #endif
  193. struct line_s {
  194. mc_wchar_t ch;
  195. unsigned int style;
  196. };
  197. static void
  198. print_to_widget (WEdit *edit, long row, int start_col, int start_col_real,
  199. long end_col, struct line_s line[])
  200. {
  201. struct line_s *p;
  202. int x = start_col_real + EDIT_TEXT_HORIZONTAL_OFFSET;
  203. int x1 = start_col + EDIT_TEXT_HORIZONTAL_OFFSET;
  204. int y = row + EDIT_TEXT_VERTICAL_OFFSET;
  205. int cols_to_skip = abs (x);
  206. set_color (EDITOR_NORMAL_COLOR);
  207. edit_move (x1, y);
  208. hline (' ', end_col + 1 - EDIT_TEXT_HORIZONTAL_OFFSET - x1);
  209. edit_move (x1 + FONT_OFFSET_X, y + FONT_OFFSET_Y);
  210. p = line;
  211. while (p->ch) {
  212. int style;
  213. mc_wchar_t textchar;
  214. int color;
  215. if (cols_to_skip) {
  216. p++;
  217. cols_to_skip--;
  218. continue;
  219. }
  220. style = p->style & 0xFF00;
  221. textchar = p->ch;
  222. color = p->style >> 16;
  223. if (style & MOD_ABNORMAL) {
  224. /* Non-printable - use black background */
  225. color = 0;
  226. }
  227. if (style & MOD_WHITESPACE) {
  228. if (style & MOD_MARKED) {
  229. textchar = ' ';
  230. set_color (EDITOR_MARKED_COLOR);
  231. } else {
  232. #if 0
  233. if (color != EDITOR_NORMAL_COLOR) {
  234. textchar = ' ';
  235. lowlevel_set_color (color);
  236. } else
  237. #endif
  238. set_color (EDITOR_WHITESPACE_COLOR);
  239. }
  240. } else {
  241. if (style & MOD_BOLD) {
  242. set_color (EDITOR_BOLD_COLOR);
  243. } else if (style & MOD_MARKED) {
  244. set_color (EDITOR_MARKED_COLOR);
  245. } else {
  246. lowlevel_set_color (color);
  247. }
  248. }
  249. #ifdef UTF8
  250. SLsmg_write_nwchars(&textchar, 1);
  251. #else
  252. addch (textchar);
  253. #endif
  254. p++;
  255. }
  256. }
  257. int visible_tabs = 1, visible_tws = 1;
  258. /* b is a pointer to the beginning of the line */
  259. static void
  260. edit_draw_this_line (WEdit *edit, long b, long row, long start_col,
  261. long end_col)
  262. {
  263. struct line_s line[MAX_LINE_LEN];
  264. struct line_s *p = line;
  265. long m1 = 0, m2 = 0, q, c1, c2;
  266. int col, start_col_real;
  267. mc_wint_t c;
  268. int color;
  269. int i;
  270. edit_get_syntax_color (edit, b - 1, &color);
  271. q = edit_move_forward3 (edit, b, start_col - edit->start_col, 0);
  272. start_col_real = (col =
  273. (int) edit_move_forward3 (edit, b, 0,
  274. q)) + edit->start_col;
  275. c1 = min (edit->column1, edit->column2);
  276. c2 = max (edit->column1, edit->column2);
  277. if (col + 16 > -edit->start_col) {
  278. eval_marks (edit, &m1, &m2);
  279. if (row <= edit->total_lines - edit->start_line) {
  280. long tws;
  281. if (use_colors && visible_tws) {
  282. tws = edit_eol (edit, b);
  283. while (tws > b && ((c = edit_get_byte (edit, tws - 1)) == ' '
  284. || c == '\t'))
  285. tws--;
  286. }
  287. while (col <= end_col - edit->start_col) {
  288. p->ch = 0;
  289. p->style = 0;
  290. if (q == edit->curs1)
  291. p->style |= MOD_CURSOR;
  292. if (q >= m1 && q < m2) {
  293. if (column_highlighting) {
  294. int x;
  295. x = edit_move_forward3 (edit, b, 0, q);
  296. if (x >= c1 && x < c2)
  297. p->style |= MOD_MARKED;
  298. } else
  299. p->style |= MOD_MARKED;
  300. }
  301. if (q == edit->bracket)
  302. p->style |= MOD_BOLD;
  303. if (q >= edit->found_start
  304. && q < edit->found_start + edit->found_len)
  305. p->style |= MOD_BOLD;
  306. c = edit_get_byte (edit, q);
  307. /* we don't use bg for mc - fg contains both */
  308. edit_get_syntax_color (edit, q, &color);
  309. p->style |= color << 16;
  310. switch (c) {
  311. case '\n':
  312. col = end_col - edit->start_col + 1; /* quit */
  313. p->ch = ' ';
  314. p++;
  315. break;
  316. case '\t':
  317. i = TAB_SIZE - ((int) col % TAB_SIZE);
  318. col += i;
  319. if (use_colors && visible_tabs) {
  320. c = (p->style & ~MOD_CURSOR) | MOD_WHITESPACE;
  321. if (i > 2) {
  322. p->ch = '<';
  323. p->style |= MOD_WHITESPACE;
  324. p++;
  325. while (--i > 1) {
  326. p->ch = '-';
  327. p->style = c;
  328. p++;
  329. }
  330. p->ch = '>';
  331. p->style = c;
  332. p++;
  333. } else if (i > 1) {
  334. p->ch = '<';
  335. p->style |= MOD_WHITESPACE;
  336. p++;
  337. p->ch = '>';
  338. p->style = c;
  339. p++;
  340. } else {
  341. p->ch = '>';
  342. p->style |= MOD_WHITESPACE;
  343. p++;
  344. }
  345. } else if (use_colors && visible_tws && q >= tws) {
  346. p->ch = '.';
  347. p->style |= MOD_WHITESPACE;
  348. c = p->style & ~MOD_CURSOR;
  349. p++;
  350. while (--i) {
  351. p->ch = '.';
  352. p->style = c;
  353. p++;
  354. }
  355. } else {
  356. p->ch = ' ';
  357. c = p->style & ~MOD_CURSOR;
  358. p++;
  359. while (--i) {
  360. p->ch = ' '; p->style = c;
  361. p++;
  362. }
  363. }
  364. break;
  365. case ' ':
  366. if (use_colors && visible_tws && q >= tws) {
  367. p->ch = '.';
  368. p->style |= MOD_WHITESPACE;
  369. p++;
  370. col++;
  371. break;
  372. }
  373. /* fallthrough */
  374. default:
  375. c = convert_to_display_c (c);
  376. /* Caret notation for control characters */
  377. if (c < 32) {
  378. p->ch = '^';
  379. p->style = MOD_ABNORMAL;
  380. p++;
  381. p->ch = c + 0x40;
  382. p->style = MOD_ABNORMAL;
  383. col += 2;
  384. break;
  385. }
  386. if (c == 127) {
  387. p->ch = '^';
  388. p->style = MOD_ABNORMAL;
  389. p++;
  390. p->ch = '?';
  391. p->style = MOD_ABNORMAL;
  392. p++;
  393. col += 2;
  394. break;
  395. }
  396. #ifndef UTF8
  397. if (is_printable (c)
  398. #else /* UTF8 */
  399. if (iswprint (c)
  400. #ifdef __STDC_ISO_10646__
  401. && (c < BINARY_CHAR_OFFSET || c >= (BINARY_CHAR_OFFSET + 256))
  402. #endif
  403. #endif /* UTF8 */
  404. ) {
  405. p->ch = c;
  406. p++;
  407. #ifdef UTF8
  408. i = wcwidth(c);
  409. if (i > 1) {
  410. col += i - 1;
  411. }
  412. #endif /* UTF8 */
  413. } else {
  414. p->ch = '.';
  415. p->style = MOD_ABNORMAL;
  416. p++;
  417. }
  418. col++;
  419. break;
  420. }
  421. q++;
  422. }
  423. }
  424. } else {
  425. start_col_real = start_col = 0;
  426. }
  427. p->ch = 0;
  428. print_to_widget (edit, row, start_col, start_col_real, end_col, line);
  429. }
  430. #define key_pending(x) (!is_idle())
  431. static void edit_draw_this_char (WEdit * edit, long curs, long row)
  432. {
  433. int b = edit_bol (edit, curs);
  434. edit_draw_this_line (edit, b, row, 0, edit->num_widget_columns - 1);
  435. }
  436. /* cursor must be in screen for other than REDRAW_PAGE passed in force */
  437. static void
  438. render_edit_text (WEdit * edit, long start_row, long start_column, long end_row,
  439. long end_column)
  440. {
  441. long row = 0, curs_row;
  442. static int prev_curs_row = 0;
  443. static long prev_curs = 0;
  444. int force = edit->force;
  445. long b;
  446. /*
  447. * If the position of the page has not moved then we can draw the cursor
  448. * character only. This will prevent line flicker when using arrow keys.
  449. */
  450. if ((!(force & REDRAW_CHAR_ONLY)) || (force & REDRAW_PAGE)) {
  451. if (!(force & REDRAW_IN_BOUNDS)) { /* !REDRAW_IN_BOUNDS means to ignore bounds and redraw whole rows */
  452. start_row = 0;
  453. end_row = edit->num_widget_lines - 1;
  454. start_column = 0;
  455. end_column = edit->num_widget_columns - 1;
  456. }
  457. if (force & REDRAW_PAGE) {
  458. row = start_row;
  459. b = edit_move_forward (edit, edit->start_display, start_row, 0);
  460. while (row <= end_row) {
  461. if (key_pending (edit))
  462. goto exit_render;
  463. edit_draw_this_line (edit, b, row, start_column, end_column);
  464. b = edit_move_forward (edit, b, 1, 0);
  465. row++;
  466. }
  467. } else {
  468. curs_row = edit->curs_row;
  469. if (force & REDRAW_BEFORE_CURSOR) {
  470. if (start_row < curs_row) {
  471. long upto = curs_row - 1 <= end_row ? curs_row - 1 : end_row;
  472. row = start_row;
  473. b = edit->start_display;
  474. while (row <= upto) {
  475. if (key_pending (edit))
  476. goto exit_render;
  477. edit_draw_this_line (edit, b, row, start_column, end_column);
  478. b = edit_move_forward (edit, b, 1, 0);
  479. }
  480. }
  481. }
  482. /* if (force & REDRAW_LINE) ---> default */
  483. b = edit_bol (edit, edit->curs1);
  484. if (curs_row >= start_row && curs_row <= end_row) {
  485. if (key_pending (edit))
  486. goto exit_render;
  487. edit_draw_this_line (edit, b, curs_row, start_column, end_column);
  488. }
  489. if (force & REDRAW_AFTER_CURSOR) {
  490. if (end_row > curs_row) {
  491. row = curs_row + 1 < start_row ? start_row : curs_row + 1;
  492. b = edit_move_forward (edit, b, 1, 0);
  493. while (row <= end_row) {
  494. if (key_pending (edit))
  495. goto exit_render;
  496. edit_draw_this_line (edit, b, row, start_column, end_column);
  497. b = edit_move_forward (edit, b, 1, 0);
  498. row++;
  499. }
  500. }
  501. }
  502. if (force & REDRAW_LINE_ABOVE && curs_row >= 1) {
  503. row = curs_row - 1;
  504. b = edit_move_backward (edit, edit_bol (edit, edit->curs1), 1);
  505. if (row >= start_row && row <= end_row) {
  506. if (key_pending (edit))
  507. goto exit_render;
  508. edit_draw_this_line (edit, b, row, start_column, end_column);
  509. }
  510. }
  511. if (force & REDRAW_LINE_BELOW && row < edit->num_widget_lines - 1) {
  512. row = curs_row + 1;
  513. b = edit_bol (edit, edit->curs1);
  514. b = edit_move_forward (edit, b, 1, 0);
  515. if (row >= start_row && row <= end_row) {
  516. if (key_pending (edit))
  517. goto exit_render;
  518. edit_draw_this_line (edit, b, row, start_column, end_column);
  519. }
  520. }
  521. }
  522. } else {
  523. if (prev_curs_row < edit->curs_row) { /* with the new text highlighting, we must draw from the top down */
  524. edit_draw_this_char (edit, prev_curs, prev_curs_row);
  525. edit_draw_this_char (edit, edit->curs1, edit->curs_row);
  526. } else {
  527. edit_draw_this_char (edit, edit->curs1, edit->curs_row);
  528. edit_draw_this_char (edit, prev_curs, prev_curs_row);
  529. }
  530. }
  531. edit->force = 0;
  532. prev_curs_row = edit->curs_row;
  533. prev_curs = edit->curs1;
  534. exit_render:
  535. edit->screen_modified = 0;
  536. return;
  537. }
  538. static void
  539. edit_render (WEdit * edit, int page, int row_start, int col_start, int row_end, int col_end)
  540. {
  541. if (page) /* if it was an expose event, 'page' would be set */
  542. edit->force |= REDRAW_PAGE | REDRAW_IN_BOUNDS;
  543. if (edit->force & REDRAW_COMPLETELY)
  544. buttonbar_redraw (edit->widget.parent);
  545. render_edit_text (edit, row_start, col_start, row_end, col_end);
  546. /*
  547. * edit->force != 0 means a key was pending and the redraw
  548. * was halted, so next time we must redraw everything in case stuff
  549. * was left undrawn from a previous key press.
  550. */
  551. if (edit->force)
  552. edit->force |= REDRAW_PAGE;
  553. }
  554. void edit_render_keypress (WEdit * edit)
  555. {
  556. edit_render (edit, 0, 0, 0, 0, 0);
  557. }