editdraw.c 15 KB

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