editdraw.c 15 KB

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