editdraw.c 13 KB

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