/** * Just for convenience, a common interface in front of mcview_get_utf and mcview_get_byte, so that * the caller doesn't have to care about utf8 vs 8-bit modes. * * Normally: stores c, updates state, returns TRUE. * At EOF: state is unchanged, c is undefined, returns FALSE. * * Also, temporary hack: handle force_max here. * TODO: move it to lower layers (datasource.c)? */ static gboolean mcview_get_next_char (WView * view, mcview_state_machine_t * state, int *c) { /* Pretend EOF if we reached force_max */ if (view->force_max >= 0 && state->offset >= view->force_max) return FALSE; #ifdef HAVE_CHARSET if (view->utf8) { gboolean result; int char_length; *c = mcview_get_utf (view, state->offset, &char_length, &result); if (!result) return FALSE; /* Pretend EOF if we crossed force_max */ if (view->force_max >= 0 && state->offset + char_length > view->force_max) return FALSE; state->offset += char_length; return TRUE; } #endif /* HAVE_CHARSET */ if (!mcview_get_byte (view, state->offset, c)) return FALSE; state->offset++; return TRUE; }
static gboolean mcview_nroff_get_char (mcview_nroff_t * nroff, int *ret_val, off_t nroff_index) { int c = 0; #ifdef HAVE_CHARSET if (nroff->view->utf8) { if (!mcview_get_utf (nroff->view, nroff_index, &c, &nroff->char_length)) { /* we need got symbol in any case */ nroff->char_length = 1; if (!mcview_get_byte (nroff->view, nroff_index, &c) || !g_ascii_isprint (c)) return FALSE; } } else #endif { nroff->char_length = 1; if (!mcview_get_byte (nroff->view, nroff_index, &c)) return FALSE; } *ret_val = c; return g_unichar_isprint (c); }
void mcview_display_nroff (mcview_t * view) { const screen_dimen left = view->data_area.left; const screen_dimen top = view->data_area.top; const screen_dimen width = view->data_area.width; const screen_dimen height = view->data_area.height; screen_dimen row, col; off_t from; int cw = 1; int c; int c_prev = 0; int c_next = 0; struct hexedit_change_node *curr = view->change_list; mcview_display_clean (view); mcview_display_ruler (view); /* Find the first displayable changed byte */ from = view->dpy_start; while (curr && (curr->offset < from)) { curr = curr->next; } tty_setcolor (NORMAL_COLOR); for (row = 0, col = 0; row < height;) { #ifdef HAVE_CHARSET if (view->utf8) { gboolean read_res = TRUE; c = mcview_get_utf (view, from, &cw, &read_res); if (!read_res) break; } else #endif { if (!mcview_get_byte (view, from, &c)) break; } from++; if (cw > 1) from += cw - 1; if (c == '\b') { if (from > 1) { #ifdef HAVE_CHARSET if (view->utf8) { gboolean read_res; c_next = mcview_get_utf (view, from, &cw, &read_res); } else #endif mcview_get_byte (view, from, &c_next); } if (g_unichar_isprint (c_prev) && g_unichar_isprint (c_next) && (c_prev == c_next || c_prev == '_' || (c_prev == '+' && c_next == 'o'))) { if (col == 0) { if (row == 0) { /* We're inside an nroff character sequence at the * beginning of the screen -- just skip the * backspace and continue with the next character. */ continue; } row--; col = width; } col--; if (c_prev == '_' && (c_next != '_' || mcview_count_backspaces (view, from + 1) == 1)) tty_setcolor (VIEW_UNDERLINED_COLOR); else tty_setcolor (VIEW_BOLD_COLOR); continue; } } if ((c == '\n') || (col >= width && view->text_wrap_mode)) { col = 0; row++; if (c == '\n' || row >= height) continue; } if (c == '\r') { mcview_get_byte_indexed (view, from, 1, &c); if (c == '\r' || c == '\n') continue; col = 0; row++; continue; } if (c == '\t') { off_t line, column; mcview_offset_to_coord (view, &line, &column, from); col += (option_tab_spacing - col % option_tab_spacing); if (view->text_wrap_mode && col >= width && width != 0) { row += col / width; col %= width; } continue; } if (view->search_start <= from && from < view->search_end) { tty_setcolor (SELECTED_COLOR); } c_prev = c; if ((off_t) col >= view->dpy_text_column && (off_t) col - view->dpy_text_column < (off_t) width) { widget_move (view, top + row, left + ((off_t) col - view->dpy_text_column)); #ifdef HAVE_CHARSET if (mc_global.utf8_display) { if (!view->utf8) { c = convert_from_8bit_to_utf_c ((unsigned char) c, view->converter); } if (!g_unichar_isprint (c)) c = '.'; } else if (view->utf8) c = convert_from_utf_to_current_c (c, view->converter); else c = convert_to_display_c (c); #endif tty_print_anychar (c); } col++; #ifdef HAVE_CHARSET if (view->utf8) { if (g_unichar_iswide (c)) col++; else if (g_unichar_iszerowidth (c)) col--; } #endif tty_setcolor (NORMAL_COLOR); } view->dpy_end = from; }
void mcview_display_hex (mcview_t * view) { const screen_dimen top = view->data_area.top; const screen_dimen left = view->data_area.left; const screen_dimen height = view->data_area.height; const screen_dimen width = view->data_area.width; const int ngroups = view->bytes_per_line / 4; const screen_dimen text_start = 8 + 13 * ngroups + ((width < 80) ? 0 : (ngroups - 1 + 1)); /* 8 characters are used for the file offset, and every hex group * takes 13 characters. On "big" screens, the groups are separated * by an extra vertical line, and there is an extra space before the * text column. */ screen_dimen row; off_t from; int c; mark_t boldflag = MARK_NORMAL; struct hexedit_change_node *curr = view->change_list; #ifdef HAVE_CHARSET int ch = 0; #endif /* HAVE_CHARSET */ char hex_buff[10]; /* A temporary buffer for sprintf and mvwaddstr */ int bytes; /* Number of bytes already printed on the line */ mcview_display_clean (view); /* Find the first displayable changed byte */ from = view->dpy_start; while (curr && (curr->offset < from)) { curr = curr->next; } for (row = 0; mcview_get_byte (view, from, NULL) == TRUE && row < height; row++) { screen_dimen col = 0; size_t i; col = 0; /* Print the hex offset */ g_snprintf (hex_buff, sizeof (hex_buff), "%08" PRIXMAX " ", (uintmax_t) from); widget_move (view, top + row, left); tty_setcolor (VIEW_BOLD_COLOR); for (i = 0; col < width && hex_buff[i] != '\0'; i++) { tty_print_char (hex_buff[i]); /* tty_print_char(hex_buff[i]); */ col += 1; } tty_setcolor (VIEW_NORMAL_COLOR); for (bytes = 0; bytes < view->bytes_per_line; bytes++, from++) { #ifdef HAVE_CHARSET if (view->utf8) { int cw = 1; gboolean read_res = TRUE; ch = mcview_get_utf (view, from, &cw, &read_res); if (!read_res) break; /* char width is greater 0 bytes */ if (cw != 0) { int cnt; char corr_buf[UTF8_CHAR_LEN + 1]; struct hexedit_change_node *corr = curr; int res; res = g_unichar_to_utf8 (ch, (char *) corr_buf); for (cnt = 0; cnt < cw; cnt++) { if (curr != NULL && from + cnt == curr->offset) { /* replace only changed bytes in array of multibyte char */ corr_buf[cnt] = curr->value; curr = curr->next; } } corr_buf[res] = '\0'; /* Determine the state of the current multibyte char */ ch = utf8_to_int ((char *) corr_buf, &cw, &read_res); curr = corr; } } #endif /* HAVE_CHARSET */ if (!mcview_get_byte (view, from, &c)) break; /* Save the cursor position for mcview_place_cursor() */ if (from == view->hex_cursor && !view->hexview_in_text) { view->cursor_row = row; view->cursor_col = col; } /* Determine the state of the current byte */ boldflag = mcview_hex_calculate_boldflag (view, from, curr); /* Determine the value of the current byte */ if (curr != NULL && from == curr->offset) { c = curr->value; curr = curr->next; } /* Select the color for the hex number */ tty_setcolor (boldflag == MARK_NORMAL ? VIEW_NORMAL_COLOR : boldflag == MARK_SELECTED ? VIEW_BOLD_COLOR : boldflag == MARK_CHANGED ? VIEW_UNDERLINED_COLOR : /* boldflag == MARK_CURSOR */ view->hexview_in_text ? VIEW_SELECTED_COLOR : VIEW_UNDERLINED_COLOR); /* Print the hex number */ widget_move (view, top + row, left + col); if (col < width) { tty_print_char (hex_char[c / 16]); col += 1; } if (col < width) { tty_print_char (hex_char[c % 16]); col += 1; } /* Print the separator */ tty_setcolor (VIEW_NORMAL_COLOR); if (bytes != view->bytes_per_line - 1) { if (col < width) { tty_print_char (' '); col += 1; } /* After every four bytes, print a group separator */ if (bytes % 4 == 3) { if (view->data_area.width >= 80 && col < width) { tty_print_one_vline (TRUE); col += 1; } if (col < width) { tty_print_char (' '); col += 1; } } } /* Select the color for the character; this differs from the * hex color when boldflag == MARK_CURSOR */ tty_setcolor (boldflag == MARK_NORMAL ? VIEW_NORMAL_COLOR : boldflag == MARK_SELECTED ? VIEW_BOLD_COLOR : boldflag == MARK_CHANGED ? VIEW_UNDERLINED_COLOR : /* boldflag == MARK_CURSOR */ view->hexview_in_text ? VIEW_SELECTED_COLOR : MARKED_SELECTED_COLOR); #ifdef HAVE_CHARSET if (mc_global.utf8_display) { if (!view->utf8) { c = convert_from_8bit_to_utf_c ((unsigned char) c, view->converter); } if (!g_unichar_isprint (c)) c = '.'; } else if (view->utf8) ch = convert_from_utf_to_current_c (ch, view->converter); else #endif { #ifdef HAVE_CHARSET c = convert_to_display_c (c); #endif if (!is_printable (c)) c = '.'; } /* Print corresponding character on the text side */ if (text_start + bytes < width) { widget_move (view, top + row, left + text_start + bytes); #ifdef HAVE_CHARSET if (view->utf8) tty_print_anychar (ch); else #endif tty_print_char (c); } /* Save the cursor position for mcview_place_cursor() */ if (from == view->hex_cursor && view->hexview_in_text) { view->cursor_row = row; view->cursor_col = text_start + bytes; } } } /* Be polite to the other functions */ tty_setcolor (VIEW_NORMAL_COLOR); mcview_place_cursor (view); view->dpy_end = from; }
void mcview_display_text (mcview_t * view) { const screen_dimen left = view->data_area.left; const screen_dimen top = view->data_area.top; const screen_dimen width = view->data_area.width; const screen_dimen height = view->data_area.height; screen_dimen row = 0, col = 0; off_t from; int cw = 1; int c, prev_ch = 0; gboolean last_row = TRUE; struct hexedit_change_node *curr = view->change_list; mcview_display_clean (view); mcview_display_ruler (view); /* Find the first displayable changed byte */ from = view->dpy_start; while ((curr != NULL) && (curr->offset < from)) curr = curr->next; while (TRUE) { tty_setcolor (NORMAL_COLOR); if (row >= height) break; #ifdef HAVE_CHARSET if (view->utf8) { gboolean read_res = TRUE; c = mcview_get_utf (view, from, &cw, &read_res); if (!read_res) break; } else #endif if (!mcview_get_byte (view, from, &c)) break; last_row = FALSE; from++; if (cw > 1) from += cw - 1; if (c != '\n' && prev_ch == '\r') { if (++row >= height) break; col = 0; /* tty_print_anychar ('\n'); */ } prev_ch = c; if (c == '\r') continue; if (c == '\n') { col = 0; row++; continue; } if (col >= width && view->text_wrap_mode) { col = 0; if (++row >= height) break; } if (c == '\t') { col += (option_tab_spacing - col % option_tab_spacing); if (view->text_wrap_mode && col >= width && width != 0) { row += col / width; col %= width; } continue; } if (view->search_start <= from && from < view->search_end) tty_setcolor (SELECTED_COLOR); if (((off_t) col >= view->dpy_text_column) && ((off_t) col - view->dpy_text_column < (off_t) width)) { widget_move (view, top + row, left + ((off_t) col - view->dpy_text_column)); #ifdef HAVE_CHARSET if (mc_global.utf8_display) { if (!view->utf8) c = convert_from_8bit_to_utf_c ((unsigned char) c, view->converter); if (!g_unichar_isprint (c)) c = '.'; } else if (view->utf8) c = convert_from_utf_to_current_c (c, view->converter); else #endif { #ifdef HAVE_CHARSET c = convert_to_display_c (c); #endif if (!is_printable (c)) c = '.'; } tty_print_anychar (c); } col++; #ifdef HAVE_CHARSET if (view->utf8) { if (g_unichar_iswide (c)) col++; else if (g_unichar_iszerowidth (c)) col--; } #endif } view->dpy_end = from; if (mcview_show_eof != NULL && mcview_show_eof[0] != '\0') { if (last_row && mcview_get_byte (view, from - 1, &c)) if (c != '\n') row--; while (++row < height) { widget_move (view, top + row, left); tty_print_string (mcview_show_eof); } } }