mcviewer: refactor mode flags.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2017-12-17 12:48:00 +03:00
parent 6e994011f5
commit 074008025b
16 changed files with 175 additions and 163 deletions

View File

@ -522,31 +522,30 @@ view_file_at_line (const vfs_path_t * filename_vpath, gboolean plain_view, gbool
if (plain_view) if (plain_view)
{ {
int changed_hex_mode = 0; mcview_mode_flags_t changed_flags;
int changed_nroff_flag = 0;
int changed_magic_flag = 0;
mcview_altered_hex_mode = 0; mcview_clear_mode_flags (&changed_flags);
mcview_altered_nroff_flag = 0; mcview_altered_flags.hex = FALSE;
mcview_altered_magic_flag = 0; mcview_altered_flags.magic = FALSE;
if (mcview_default_hex_mode) mcview_altered_flags.nroff = FALSE;
changed_hex_mode = 1; if (mcview_global_flags.hex)
if (mcview_default_nroff_flag) changed_flags.hex = TRUE;
changed_nroff_flag = 1; if (mcview_global_flags.magic)
if (mcview_default_magic_flag) changed_flags.magic = TRUE;
changed_magic_flag = 1; if (mcview_global_flags.nroff)
mcview_default_hex_mode = 0; changed_flags.nroff = TRUE;
mcview_default_nroff_flag = 0; mcview_global_flags.hex = FALSE;
mcview_default_magic_flag = 0; mcview_global_flags.magic = FALSE;
mcview_global_flags.nroff = FALSE;
ret = mcview_viewer (NULL, filename_vpath, start_line, search_start, search_end); ret = mcview_viewer (NULL, filename_vpath, start_line, search_start, search_end);
if (changed_hex_mode && !mcview_altered_hex_mode) if (changed_flags.hex && !mcview_altered_flags.hex)
mcview_default_hex_mode = 1; mcview_global_flags.hex = TRUE;
if (changed_nroff_flag && !mcview_altered_nroff_flag) if (changed_flags.magic && !mcview_altered_flags.magic)
mcview_default_nroff_flag = 1; mcview_global_flags.magic = TRUE;
if (changed_magic_flag && !mcview_altered_magic_flag) if (changed_flags.nroff && !mcview_altered_flags.nroff)
mcview_default_magic_flag = 1; mcview_global_flags.nroff = TRUE;
dialog_switch_process_pending (); dialog_switch_process_pending ();
} }

View File

@ -353,25 +353,34 @@ exec_make_shell_string (const char *lc_data, const vfs_path_t * filename_vpath)
static void static void
exec_extension_view (void *target, char *cmd, const vfs_path_t * filename_vpath, int start_line) exec_extension_view (void *target, char *cmd, const vfs_path_t * filename_vpath, int start_line)
{ {
int def_hex_mode = mcview_default_hex_mode, changed_hex_mode = 0; mcview_mode_flags_t def_flags = {
int def_nroff_flag = mcview_default_nroff_flag, changed_nroff_flag = 0; /* *INDENT-OFF* */
.wrap = FALSE,
.hex = mcview_global_flags.hex,
.magic = FALSE,
.nroff = mcview_global_flags.nroff
/* *INDENT-ON* */
};
mcview_altered_hex_mode = 0; mcview_mode_flags_t changed_flags;
mcview_altered_nroff_flag = 0;
if (def_hex_mode != mcview_default_hex_mode) mcview_clear_mode_flags (&changed_flags);
changed_hex_mode = 1; mcview_altered_flags.hex = FALSE;
if (def_nroff_flag != mcview_default_nroff_flag) mcview_altered_flags.nroff = FALSE;
changed_nroff_flag = 1; if (def_flags.hex != mcview_global_flags.hex)
changed_flags.hex = TRUE;
if (def_flags.nroff != mcview_global_flags.nroff)
changed_flags.nroff = TRUE;
if (target == NULL) if (target == NULL)
mcview_viewer (cmd, filename_vpath, start_line, 0, 0); mcview_viewer (cmd, filename_vpath, start_line, 0, 0);
else else
mcview_load ((WView *) target, cmd, vfs_path_as_str (filename_vpath), start_line, 0, 0); mcview_load ((WView *) target, cmd, vfs_path_as_str (filename_vpath), start_line, 0, 0);
if (changed_hex_mode && !mcview_altered_hex_mode) if (changed_flags.hex && !mcview_altered_flags.hex)
mcview_default_hex_mode = def_hex_mode; mcview_global_flags.hex = def_flags.hex;
if (changed_nroff_flag && !mcview_altered_nroff_flag) if (changed_flags.nroff && !mcview_altered_flags.nroff)
mcview_default_nroff_flag = def_nroff_flag; mcview_global_flags.nroff = def_flags.nroff;
dialog_switch_process_pending (); dialog_switch_process_pending ();
} }

View File

@ -302,7 +302,7 @@ static const struct
{ "mouse_close_dialog", &mouse_close_dialog}, { "mouse_close_dialog", &mouse_close_dialog},
{ "fast_refresh", &fast_refresh }, { "fast_refresh", &fast_refresh },
{ "drop_menus", &drop_menus }, { "drop_menus", &drop_menus },
{ "wrap_mode", &mcview_global_wrap_mode }, { "wrap_mode", &mcview_global_flags.wrap },
{ "old_esc_mode", &old_esc_mode }, { "old_esc_mode", &old_esc_mode },
{ "cd_symlinks", &mc_global.vfs.cd_symlinks }, { "cd_symlinks", &mc_global.vfs.cd_symlinks },
{ "show_all_if_ambiguous", &mc_global.widget.show_all_if_ambiguous }, { "show_all_if_ambiguous", &mc_global.widget.show_all_if_ambiguous },

View File

@ -640,22 +640,22 @@ check_format_view (const char *p)
{ {
if (strncmp (q, DEFAULT_CHARSET, 5) == 0) if (strncmp (q, DEFAULT_CHARSET, 5) == 0)
{ {
mcview_default_hex_mode = 0; mcview_global_flags.hex = FALSE;
q += 4; q += 4;
} }
else if (strncmp (q, "hex", 3) == 0) else if (strncmp (q, "hex", 3) == 0)
{ {
mcview_default_hex_mode = 1; mcview_global_flags.hex = TRUE;
q += 2; q += 2;
} }
else if (strncmp (q, "nroff", 5) == 0) else if (strncmp (q, "nroff", 5) == 0)
{ {
mcview_default_nroff_flag = 1; mcview_global_flags.nroff = TRUE;
q += 4; q += 4;
} }
else if (strncmp (q, "unform", 6) == 0) else if (strncmp (q, "unform", 6) == 0)
{ {
mcview_default_nroff_flag = 0; mcview_global_flags.nroff = FALSE;
q += 5; q += 5;
} }
} }

View File

@ -105,7 +105,7 @@ mcview_search (WView * view, gboolean start_search)
{ {
if (mcview_dialog_search (view)) if (mcview_dialog_search (view))
{ {
if (view->hex_mode) if (view->mode_flags.hex)
want_search_start = view->hex_cursor; want_search_start = view->hex_cursor;
mcview_do_search (view, want_search_start); mcview_do_search (view, want_search_start);
@ -113,7 +113,7 @@ mcview_search (WView * view, gboolean start_search)
} }
else else
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
if (!mcview_search_options.backwards) if (!mcview_search_options.backwards)
want_search_start = view->hex_cursor + 1; want_search_start = view->hex_cursor + 1;
@ -459,11 +459,11 @@ mcview_execute_cmd (WView * view, long command)
mcview_move_right (view, 1); mcview_move_right (view, 1);
break; break;
case CK_LeftQuick: case CK_LeftQuick:
if (!view->hex_mode) if (!view->mode_flags.hex)
mcview_move_left (view, 10); mcview_move_left (view, 10);
break; break;
case CK_RightQuick: case CK_RightQuick:
if (!view->hex_mode) if (!view->mode_flags.hex)
mcview_move_right (view, 10); mcview_move_right (view, 10);
break; break;
case CK_SearchContinue: case CK_SearchContinue:
@ -552,7 +552,7 @@ mcview_handle_key (WView * view, int key)
key = convert_from_input_c (key); key = convert_from_input_c (key);
#endif #endif
if (view->hex_mode) if (view->mode_flags.hex)
{ {
if (view->hexedit_mode && (mcview_handle_editkey (view, key) == MSG_HANDLED)) if (view->hexedit_mode && (mcview_handle_editkey (view, key) == MSG_HANDLED))
return MSG_HANDLED; return MSG_HANDLED;
@ -667,7 +667,7 @@ mcview_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *
return MSG_HANDLED; return MSG_HANDLED;
case MSG_CURSOR: case MSG_CURSOR:
if (view->hex_mode) if (view->mode_flags.hex)
mcview_place_cursor (view); mcview_place_cursor (view);
return MSG_HANDLED; return MSG_HANDLED;

View File

@ -402,7 +402,7 @@ mcview_get_next_maybe_nroff_char (WView * view, mcview_state_machine_t * state,
if (color != NULL) if (color != NULL)
*color = VIEW_NORMAL_COLOR; *color = VIEW_NORMAL_COLOR;
if (!view->text_nroff_mode) if (!view->mode_flags.nroff)
return mcview_get_next_char (view, state, c); return mcview_get_next_char (view, state, c);
if (!mcview_get_next_char (view, state, c)) if (!mcview_get_next_char (view, state, c))
@ -583,7 +583,7 @@ mcview_display_line (WView * view, mcview_state_machine_t * state, int row,
const screen_dimen top = view->data_area.top; const screen_dimen top = view->data_area.top;
const screen_dimen width = view->data_area.width; const screen_dimen width = view->data_area.width;
const screen_dimen height = view->data_area.height; const screen_dimen height = view->data_area.height;
off_t dpy_text_column = view->text_wrap_mode ? 0 : view->dpy_text_column; off_t dpy_text_column = view->mode_flags.wrap ? 0 : view->dpy_text_column;
screen_dimen col = 0; screen_dimen col = 0;
int cs[1 + MAX_COMBINING_CHARS]; int cs[1 + MAX_COMBINING_CHARS];
char str[(1 + MAX_COMBINING_CHARS) * UTF8_CHAR_LEN + 1]; char str[(1 + MAX_COMBINING_CHARS) * UTF8_CHAR_LEN + 1];
@ -592,7 +592,7 @@ mcview_display_line (WView * view, mcview_state_machine_t * state, int row,
if (paragraph_ended != NULL) if (paragraph_ended != NULL)
*paragraph_ended = TRUE; *paragraph_ended = TRUE;
if (!view->text_wrap_mode && (row < 0 || row >= (int) height) && linewidth == NULL) if (!view->mode_flags.wrap && (row < 0 || row >= (int) height) && linewidth == NULL)
{ {
/* Optimization: Fast forward to the end of the line, rather than carefully /* Optimization: Fast forward to the end of the line, rather than carefully
* parsing and then not actually displaying it. */ * parsing and then not actually displaying it. */
@ -661,7 +661,7 @@ mcview_display_line (WView * view, mcview_state_machine_t * state, int row,
/* In wrap mode only: We're done with this row if the character sequence wouldn't fit. /* In wrap mode only: We're done with this row if the character sequence wouldn't fit.
* Except if at the first column, because then it wouldn't fit in the next row either. * Except if at the first column, because then it wouldn't fit in the next row either.
* In this extreme case let the unwrapped code below do its best to display it. */ * In this extreme case let the unwrapped code below do its best to display it. */
if (view->text_wrap_mode && (off_t) col + charwidth > dpy_text_column + (off_t) width if (view->mode_flags.wrap && (off_t) col + charwidth > dpy_text_column + (off_t) width
&& col > 0) && col > 0)
{ {
*state = state_saved; *state = state_saved;
@ -730,7 +730,7 @@ mcview_display_line (WView * view, mcview_state_machine_t * state, int row,
col += charwidth; col += charwidth;
state->unwrapped_column += charwidth; state->unwrapped_column += charwidth;
if (!view->text_wrap_mode && (off_t) col >= dpy_text_column + (off_t) width if (!view->mode_flags.wrap && (off_t) col >= dpy_text_column + (off_t) width
&& linewidth == NULL) && linewidth == NULL)
{ {
/* Optimization: Fast forward to the end of the line, rather than carefully /* Optimization: Fast forward to the end of the line, rather than carefully
@ -870,7 +870,7 @@ mcview_display_text (WView * view)
mcview_display_clean (view); mcview_display_clean (view);
mcview_display_ruler (view); mcview_display_ruler (view);
if (!view->text_wrap_mode) if (!view->mode_flags.wrap)
mcview_state_machine_init (&state, view->dpy_start); mcview_state_machine_init (&state, view->dpy_start);
else else
{ {
@ -888,7 +888,7 @@ mcview_display_text (WView * view)
* scroll the file and display again. This happens when e.g. the * scroll the file and display again. This happens when e.g. the
* window is made bigger, or the file becomes shorter due to * window is made bigger, or the file becomes shorter due to
* charset change or enabling nroff. */ * charset change or enabling nroff. */
if ((view->text_wrap_mode ? view->dpy_state_top.offset : view->dpy_start) > 0) if ((view->mode_flags.wrap ? view->dpy_state_top.offset : view->dpy_start) > 0)
{ {
mcview_ascii_move_up (view, height - row); mcview_ascii_move_up (view, height - row);
again = TRUE; again = TRUE;
@ -940,7 +940,7 @@ mcview_ascii_move_down (WView * view, off_t lines)
/* Okay, there's enough data. Move by 1 row at the top, too. No need to check for /* Okay, there's enough data. Move by 1 row at the top, too. No need to check for
* EOF, that can't happen. */ * EOF, that can't happen. */
if (!view->text_wrap_mode) if (!view->mode_flags.wrap)
{ {
view->dpy_start = mcview_eol (view, view->dpy_start); view->dpy_start = mcview_eol (view, view->dpy_start);
view->dpy_paragraph_skip_lines = 0; view->dpy_paragraph_skip_lines = 0;
@ -977,7 +977,7 @@ mcview_ascii_move_down (WView * view, off_t lines)
void void
mcview_ascii_move_up (WView * view, off_t lines) mcview_ascii_move_up (WView * view, off_t lines)
{ {
if (!view->text_wrap_mode) if (!view->mode_flags.wrap)
{ {
while (lines-- != 0) while (lines-- != 0)
view->dpy_start = mcview_bol (view, view->dpy_start - 1, 0); view->dpy_start = mcview_bol (view, view->dpy_start - 1, 0);
@ -1027,7 +1027,7 @@ mcview_ascii_move_up (WView * view, off_t lines)
void void
mcview_ascii_moveto_bol (WView * view) mcview_ascii_moveto_bol (WView * view)
{ {
if (!view->text_wrap_mode) if (!view->mode_flags.wrap)
view->dpy_text_column = 0; view->dpy_text_column = 0;
} }
@ -1036,7 +1036,7 @@ mcview_ascii_moveto_bol (WView * view)
void void
mcview_ascii_moveto_eol (WView * view) mcview_ascii_moveto_eol (WView * view)
{ {
if (!view->text_wrap_mode) if (!view->mode_flags.wrap)
{ {
mcview_state_machine_t state; mcview_state_machine_t state;
off_t linewidth; off_t linewidth;

View File

@ -312,7 +312,7 @@ mcview_ccache_lookup (WView * view, coord_cache_entry_t * coord, enum ccache_typ
if (sorter == CCACHE_OFFSET) if (sorter == CCACHE_OFFSET)
cmp_func = mcview_coord_cache_entry_less_offset; cmp_func = mcview_coord_cache_entry_less_offset;
else if (view->text_nroff_mode) else if (view->mode_flags.nroff)
cmp_func = mcview_coord_cache_entry_less_nroff; cmp_func = mcview_coord_cache_entry_less_nroff;
else else
cmp_func = mcview_coord_cache_entry_less_plain; cmp_func = mcview_coord_cache_entry_less_plain;
@ -342,7 +342,8 @@ mcview_ccache_lookup (WView * view, coord_cache_entry_t * coord, enum ccache_typ
if (!cmp_func (&current, coord)) if (!cmp_func (&current, coord))
{ {
if (lookup_what == CCACHE_OFFSET && view->text_nroff_mode && nroff_state != NROFF_START) if (lookup_what == CCACHE_OFFSET && view->mode_flags.nroff
&& nroff_state != NROFF_START)
{ {
/* don't break here */ /* don't break here */
} }

View File

@ -251,12 +251,12 @@ mcview_dialog_goto (WView * view, off_t * offset)
if (view->growbuf_in_use) if (view->growbuf_in_use)
mcview_growbuf_read_all_data (view); mcview_growbuf_read_all_data (view);
*offset = addr * mcview_get_filesize (view) / 100; *offset = addr * mcview_get_filesize (view) / 100;
if (!view->hex_mode) if (!view->mode_flags.hex)
*offset = mcview_bol (view, *offset, 0); *offset = mcview_bol (view, *offset, 0);
break; break;
case MC_VIEW_GOTO_OFFSET_DEC: case MC_VIEW_GOTO_OFFSET_DEC:
case MC_VIEW_GOTO_OFFSET_HEX: case MC_VIEW_GOTO_OFFSET_HEX:
if (!view->hex_mode) if (!view->mode_flags.hex)
{ {
if (view->growbuf_in_use) if (view->growbuf_in_use)
mcview_growbuf_read_until (view, addr); mcview_growbuf_read_until (view, addr);

View File

@ -80,11 +80,11 @@ mcview_set_buttonbar (WView * view)
{ {
WDialog *h = WIDGET (view)->owner; WDialog *h = WIDGET (view)->owner;
WButtonBar *b = find_buttonbar (h); WButtonBar *b = find_buttonbar (h);
const global_keymap_t *keymap = view->hex_mode ? viewer_hex_map : viewer_map; const global_keymap_t *keymap = view->mode_flags.hex ? viewer_hex_map : viewer_map;
buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), keymap, WIDGET (view)); buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), keymap, WIDGET (view));
if (view->hex_mode) if (view->mode_flags.hex)
{ {
if (view->hexedit_mode) if (view->hexedit_mode)
buttonbar_set_label (b, 2, Q_ ("ButtonBar|View"), keymap, WIDGET (view)); buttonbar_set_label (b, 2, Q_ ("ButtonBar|View"), keymap, WIDGET (view));
@ -100,7 +100,7 @@ mcview_set_buttonbar (WView * view)
} }
else else
{ {
buttonbar_set_label (b, 2, view->text_wrap_mode ? Q_ ("ButtonBar|UnWrap") buttonbar_set_label (b, 2, view->mode_flags.wrap ? Q_ ("ButtonBar|UnWrap")
: Q_ ("ButtonBar|Wrap"), keymap, WIDGET (view)); : Q_ ("ButtonBar|Wrap"), keymap, WIDGET (view));
buttonbar_set_label (b, 4, Q_ ("ButtonBar|Hex"), keymap, WIDGET (view)); buttonbar_set_label (b, 4, Q_ ("ButtonBar|Hex"), keymap, WIDGET (view));
buttonbar_set_label (b, 6, "", keymap, WIDGET (view)); buttonbar_set_label (b, 6, "", keymap, WIDGET (view));
@ -108,13 +108,13 @@ mcview_set_buttonbar (WView * view)
} }
buttonbar_set_label (b, 5, Q_ ("ButtonBar|Goto"), keymap, WIDGET (view)); buttonbar_set_label (b, 5, Q_ ("ButtonBar|Goto"), keymap, WIDGET (view));
buttonbar_set_label (b, 8, view->magic_mode ? Q_ ("ButtonBar|Raw") buttonbar_set_label (b, 8, view->mode_flags.magic ? Q_ ("ButtonBar|Raw")
: Q_ ("ButtonBar|Parse"), keymap, WIDGET (view)); : Q_ ("ButtonBar|Parse"), keymap, WIDGET (view));
if (!mcview_is_in_panel (view)) /* don't override some panel buttonbar keys */ if (!mcview_is_in_panel (view)) /* don't override some panel buttonbar keys */
{ {
buttonbar_set_label (b, 3, Q_ ("ButtonBar|Quit"), keymap, WIDGET (view)); buttonbar_set_label (b, 3, Q_ ("ButtonBar|Quit"), keymap, WIDGET (view));
buttonbar_set_label (b, 9, view->text_nroff_mode ? Q_ ("ButtonBar|Unform") buttonbar_set_label (b, 9, view->mode_flags.nroff ? Q_ ("ButtonBar|Unform")
: Q_ ("ButtonBar|Format"), keymap, WIDGET (view)); : Q_ ("ButtonBar|Format"), keymap, WIDGET (view));
buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), keymap, WIDGET (view)); buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), keymap, WIDGET (view));
} }
@ -165,7 +165,7 @@ mcview_display_status (WView * view)
if (width > 40) if (width > 40)
{ {
widget_move (view, top, width - 32); widget_move (view, top, width - 32);
if (view->hex_mode) if (view->mode_flags.hex)
tty_printf ("0x%08" PRIxMAX, (uintmax_t) view->hex_cursor); tty_printf ("0x%08" PRIxMAX, (uintmax_t) view->hex_cursor);
else else
{ {
@ -188,7 +188,7 @@ mcview_display_status (WView * view)
else else
tty_print_string (str_fit_to_term (file_label, width - 5, J_LEFT_FIT)); tty_print_string (str_fit_to_term (file_label, width - 5, J_LEFT_FIT));
if (width > 26) if (width > 26)
mcview_display_percent (view, view->hex_mode ? view->hex_cursor : view->dpy_end); mcview_display_percent (view, view->mode_flags.hex ? view->hex_cursor : view->dpy_end);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -244,14 +244,10 @@ mcview_update (WView * view)
void void
mcview_display (WView * view) mcview_display (WView * view)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{
mcview_display_hex (view); mcview_display_hex (view);
}
else else
{
mcview_display_text (view); mcview_display_text (view);
}
mcview_display_status (view); mcview_display_status (view);
} }
@ -285,7 +281,7 @@ mcview_compute_areas (WView * view)
view->status_area.height = height; view->status_area.height = height;
rest -= height; rest -= height;
height = (ruler == RULER_NONE || view->hex_mode) ? 0 : 2; height = (ruler == RULER_NONE || view->mode_flags.hex) ? 0 : 2;
height = MIN (rest, height); height = MIN (rest, height);
view->ruler_area.height = height; view->ruler_area.height = height;
rest -= height; rest -= height;

View File

@ -134,13 +134,13 @@ struct WView
growing buffer */ growing buffer */
gboolean growbuf_finished; /* TRUE when all data has been read. */ gboolean growbuf_finished; /* TRUE when all data has been read. */
/* Editor modes */ mcview_mode_flags_t mode_flags;
gboolean hex_mode; /* Hexview or Hexedit */
gboolean hexedit_mode; /* Hexedit */ /* Hex editor modes */
gboolean hexedit_mode; /* Hexview or Hexedit */
gboolean hexview_in_text; /* Is the hexview cursor in the text area? */ gboolean hexview_in_text; /* Is the hexview cursor in the text area? */
gboolean text_nroff_mode; /* Nroff-style highlighting */ int bytes_per_line; /* Number of bytes per line in hex mode */
gboolean text_wrap_mode; /* Wrap text lines to fit them on the screen */ off_t hex_cursor; /* Hexview cursor position in file */
gboolean magic_mode; /* Preprocess the file using external programs */
gboolean hexedit_lownibble; /* Are we editing the last significant nibble? */ gboolean hexedit_lownibble; /* Are we editing the last significant nibble? */
gboolean locked; /* We hold lock on current file */ gboolean locked; /* We hold lock on current file */
@ -160,7 +160,6 @@ struct WView
gboolean dpy_wrap_dirty; /* dpy_state_top needs to be recomputed */ gboolean dpy_wrap_dirty; /* dpy_state_top needs to be recomputed */
off_t dpy_text_column; /* Number of skipped columns in non-wrap off_t dpy_text_column; /* Number of skipped columns in non-wrap
* text mode */ * text mode */
off_t hex_cursor; /* Hexview cursor position in file */
screen_dimen cursor_col; /* Cursor column */ screen_dimen cursor_col; /* Cursor column */
screen_dimen cursor_row; /* Cursor row */ screen_dimen cursor_row; /* Cursor row */
struct hexedit_change_node *change_list; /* Linked list of changes */ struct hexedit_change_node *change_list; /* Linked list of changes */
@ -173,31 +172,25 @@ struct WView
int dirty; /* Number of skipped updates */ int dirty; /* Number of skipped updates */
gboolean dpy_bbar_dirty; /* Does the button bar need to be updated? */ gboolean dpy_bbar_dirty; /* Does the button bar need to be updated? */
/* Mode variables */
int bytes_per_line; /* Number of bytes per line in hex mode */
/* Search variables */
off_t search_start; /* First character to start searching from */
off_t search_end; /* Length of found string or 0 if none was found */
/* Markers */
int marker; /* mark to use */
off_t marks[10]; /* 10 marks: 0..9 */
off_t update_steps; /* The number of bytes between percent
* increments */
off_t update_activate; /* Last point where we updated the status */
/* converter for translation of text */
GIConv converter;
/* handle of search engine */ /* handle of search engine */
mc_search_t *search; mc_search_t *search;
gchar *last_search_string; gchar *last_search_string;
struct mcview_nroff_struct *search_nroff_seq; struct mcview_nroff_struct *search_nroff_seq;
off_t search_start; /* First character to start searching from */
off_t search_end; /* Length of found string or 0 if none was found */
int search_numNeedSkipChar; int search_numNeedSkipChar;
/* Markers */
int marker; /* mark to use */
off_t marks[10]; /* 10 marks: 0..9 */
off_t update_steps; /* The number of bytes between percent increments */
off_t update_activate; /* Last point where we updated the status */
/* converter for translation of text */
GIConv converter;
GArray *saved_bookmarks; GArray *saved_bookmarks;
dir_list *dir; /* List of current directory files dir_list *dir; /* List of current directory files

View File

@ -35,6 +35,7 @@
#include <config.h> #include <config.h>
#include <string.h> /* memset() */
#include <sys/types.h> #include <sys/types.h>
#include "lib/global.h" #include "lib/global.h"
@ -74,8 +75,8 @@ mcview_toggle_magic_mode (WView * view)
dir_list *dir; dir_list *dir;
int *dir_idx; int *dir_idx;
mcview_altered_magic_flag = 1; mcview_altered_flags.magic = TRUE;
view->magic_mode = !view->magic_mode; view->mode_flags.magic = !view->mode_flags.magic;
/* reinit view */ /* reinit view */
filename = g_strdup (vfs_path_as_str (view->filename_vpath)); filename = g_strdup (vfs_path_as_str (view->filename_vpath));
@ -101,7 +102,7 @@ mcview_toggle_magic_mode (WView * view)
void void
mcview_toggle_wrap_mode (WView * view) mcview_toggle_wrap_mode (WView * view)
{ {
view->text_wrap_mode = !view->text_wrap_mode; view->mode_flags.wrap = !view->mode_flags.wrap;
view->dpy_wrap_dirty = TRUE; view->dpy_wrap_dirty = TRUE;
view->dpy_bbar_dirty = TRUE; view->dpy_bbar_dirty = TRUE;
view->dirty++; view->dirty++;
@ -112,8 +113,8 @@ mcview_toggle_wrap_mode (WView * view)
void void
mcview_toggle_nroff_mode (WView * view) mcview_toggle_nroff_mode (WView * view)
{ {
view->text_nroff_mode = !view->text_nroff_mode; view->mode_flags.nroff = !view->mode_flags.nroff;
mcview_altered_nroff_flag = 1; mcview_altered_flags.nroff = TRUE;
view->dpy_wrap_dirty = TRUE; view->dpy_wrap_dirty = TRUE;
view->dpy_bbar_dirty = TRUE; view->dpy_bbar_dirty = TRUE;
view->dirty++; view->dirty++;
@ -124,9 +125,9 @@ mcview_toggle_nroff_mode (WView * view)
void void
mcview_toggle_hex_mode (WView * view) mcview_toggle_hex_mode (WView * view)
{ {
view->hex_mode = !view->hex_mode; view->mode_flags.hex = !view->mode_flags.hex;
if (view->hex_mode) if (view->mode_flags.hex)
{ {
view->hex_cursor = view->dpy_start; view->hex_cursor = view->dpy_start;
view->dpy_start = mcview_offset_rounddown (view->dpy_start, view->bytes_per_line); view->dpy_start = mcview_offset_rounddown (view->dpy_start, view->bytes_per_line);
@ -138,7 +139,7 @@ mcview_toggle_hex_mode (WView * view)
view->hex_cursor = view->dpy_start; view->hex_cursor = view->dpy_start;
widget_want_cursor (WIDGET (view), FALSE); widget_want_cursor (WIDGET (view), FALSE);
} }
mcview_altered_hex_mode = 1; mcview_altered_flags.hex = TRUE;
view->dpy_paragraph_skip_lines = 0; view->dpy_paragraph_skip_lines = 0;
view->dpy_wrap_dirty = TRUE; view->dpy_wrap_dirty = TRUE;
view->dpy_bbar_dirty = TRUE; view->dpy_bbar_dirty = TRUE;
@ -206,21 +207,16 @@ mcview_done (WView * view)
if (mcview_remember_file_position && view->filename_vpath != NULL) if (mcview_remember_file_position && view->filename_vpath != NULL)
{ {
save_file_position (view->filename_vpath, -1, 0, save_file_position (view->filename_vpath, -1, 0,
view->hex_mode ? view->hex_cursor : view->dpy_start, view->mode_flags.hex ? view->hex_cursor : view->dpy_start,
view->saved_bookmarks); view->saved_bookmarks);
view->saved_bookmarks = NULL; view->saved_bookmarks = NULL;
} }
/* Write back the global viewer mode */ /* Write back the global viewer mode */
mcview_default_hex_mode = view->hex_mode; mcview_global_flags = view->mode_flags;
mcview_default_nroff_flag = view->text_nroff_mode;
mcview_default_magic_flag = view->magic_mode;
mcview_global_wrap_mode = view->text_wrap_mode;
/* Free memory used by the viewer */ /* Free memory used by the viewer */
/* view->widget needs no destructor */ /* view->widget needs no destructor */
vfs_path_free (view->filename_vpath); vfs_path_free (view->filename_vpath);
view->filename_vpath = NULL; view->filename_vpath = NULL;
vfs_path_free (view->workdir_vpath); vfs_path_free (view->workdir_vpath);
@ -417,7 +413,7 @@ mcview_calc_percent (WView * view, off_t p)
return (-1); return (-1);
filesize = mcview_get_filesize (view); filesize = mcview_get_filesize (view);
if (view->hex_mode && filesize > 0) if (view->mode_flags.hex && filesize > 0)
{ {
/* p can't be beyond the last char, only over that. Compensate for this. */ /* p can't be beyond the last char, only over that. Compensate for this. */
filesize--; filesize--;
@ -434,3 +430,11 @@ mcview_calc_percent (WView * view, off_t p)
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
void
mcview_clear_mode_flags (mcview_mode_flags_t * flags)
{
memset (flags, 0, sizeof (*flags));
}
/* --------------------------------------------------------------------------------------------- */

View File

@ -50,14 +50,19 @@
/*** global variables ****************************************************************************/ /*** global variables ****************************************************************************/
int mcview_default_hex_mode = 0; mcview_mode_flags_t mcview_global_flags = {
int mcview_default_nroff_flag = 0; .wrap = TRUE,
gboolean mcview_global_wrap_mode = TRUE; .hex = FALSE,
int mcview_default_magic_flag = 1; .magic = TRUE,
.nroff = FALSE
};
int mcview_altered_hex_mode = 0; mcview_mode_flags_t mcview_altered_flags = {
int mcview_altered_magic_flag = 0; .wrap = FALSE,
int mcview_altered_nroff_flag = 0; .hex = FALSE,
.magic = FALSE,
.nroff = FALSE
};
gboolean mcview_remember_file_position = FALSE; gboolean mcview_remember_file_position = FALSE;
@ -109,7 +114,7 @@ mcview_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
MC_FALLTHROUGH; MC_FALLTHROUGH;
case MSG_MOUSE_CLICK: case MSG_MOUSE_CLICK:
if (!view->text_wrap_mode) if (!view->mode_flags.wrap)
{ {
/* Scrolling left and right */ /* Scrolling left and right */
screen_dimen x; screen_dimen x;
@ -197,26 +202,23 @@ mcview_new (int y, int x, int lines, int cols, gboolean is_panel)
widget_init (w, y, x, lines, cols, mcview_callback, mcview_mouse_callback); widget_init (w, y, x, lines, cols, mcview_callback, mcview_mouse_callback);
w->options |= WOP_SELECTABLE | WOP_TOP_SELECT; w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
view->hex_mode = FALSE; mcview_clear_mode_flags (&view->mode_flags);
view->hexedit_mode = FALSE; view->hexedit_mode = FALSE;
view->locked = FALSE;
view->hexview_in_text = FALSE; view->hexview_in_text = FALSE;
view->text_nroff_mode = FALSE; view->locked = FALSE;
view->text_wrap_mode = FALSE;
view->magic_mode = FALSE;
view->dpy_frame_size = is_panel ? 1 : 0; view->dpy_frame_size = is_panel ? 1 : 0;
view->converter = str_cnv_from_term; view->converter = str_cnv_from_term;
mcview_init (view); mcview_init (view);
if (mcview_default_hex_mode) if (mcview_global_flags.hex)
mcview_toggle_hex_mode (view); mcview_toggle_hex_mode (view);
if (mcview_default_nroff_flag) if (mcview_global_flags.nroff)
mcview_toggle_nroff_mode (view); mcview_toggle_nroff_mode (view);
if (mcview_global_wrap_mode) if (mcview_global_flags.wrap)
mcview_toggle_wrap_mode (view); mcview_toggle_wrap_mode (view);
if (mcview_default_magic_flag) if (mcview_global_flags.magic)
mcview_toggle_magic_mode (view); mcview_toggle_magic_mode (view);
return view; return view;
@ -306,7 +308,7 @@ mcview_load (WView * view, const char *command, const char *file, int start_line
mcview_set_codeset (view); mcview_set_codeset (view);
if (command != NULL && (view->magic_mode || file == NULL || file[0] == '\0')) if (command != NULL && (view->mode_flags.magic || file == NULL || file[0] == '\0'))
retval = mcview_load_command_output (view, command); retval = mcview_load_command_output (view, command);
else if (file != NULL && file[0] != '\0') else if (file != NULL && file[0] != '\0')
{ {
@ -364,7 +366,7 @@ mcview_load (WView * view, const char *command, const char *file, int start_line
} }
else else
{ {
if (view->magic_mode) if (view->mode_flags.magic)
{ {
int type; int type;
@ -426,7 +428,7 @@ mcview_load (WView * view, const char *command, const char *file, int start_line
new_offset = 0; new_offset = 0;
else else
new_offset = MIN (new_offset, max_offset); new_offset = MIN (new_offset, max_offset);
if (!view->hex_mode) if (!view->mode_flags.hex)
{ {
view->dpy_start = mcview_bol (view, new_offset, 0); view->dpy_start = mcview_bol (view, new_offset, 0);
view->dpy_wrap_dirty = TRUE; view->dpy_wrap_dirty = TRUE;

View File

@ -16,16 +16,18 @@
struct WView; struct WView;
typedef struct WView WView; typedef struct WView WView;
typedef struct
{
gboolean wrap; /* Wrap text lines to fit them on the screen */
gboolean hex; /* Plainview or hexview */
gboolean magic; /* Preprocess the file using external programs */
gboolean nroff; /* Nroff-style highlighting */
} mcview_mode_flags_t;
/*** global variables defined in .c file *********************************************************/ /*** global variables defined in .c file *********************************************************/
extern int mcview_default_hex_mode; extern mcview_mode_flags_t mcview_global_flags;
extern int mcview_default_nroff_flag; extern mcview_mode_flags_t mcview_altered_flags;
extern gboolean mcview_global_wrap_mode;
extern int mcview_default_magic_flag;
extern int mcview_altered_hex_mode;
extern int mcview_altered_magic_flag;
extern int mcview_altered_nroff_flag;
extern gboolean mcview_remember_file_position; extern gboolean mcview_remember_file_position;
extern int mcview_max_dirt_limit; extern int mcview_max_dirt_limit;
@ -49,5 +51,7 @@ extern gboolean mcview_viewer (const char *command, const vfs_path_t * file_vpat
extern gboolean mcview_load (WView * view, const char *command, const char *file, int start_line, extern gboolean mcview_load (WView * view, const char *command, const char *file, int start_line,
off_t search_start, off_t search_end); off_t search_start, off_t search_end);
extern void mcview_clear_mode_flags (mcview_mode_flags_t * flags);
/*** inline functions ****************************************************************************/ /*** inline functions ****************************************************************************/
#endif /* MC__VIEWER_H */ #endif /* MC__VIEWER_H */

View File

@ -70,7 +70,7 @@
static void static void
mcview_scroll_to_cursor (WView * view) mcview_scroll_to_cursor (WView * view)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
off_t bytes = view->bytes_per_line; off_t bytes = view->bytes_per_line;
off_t cursor = view->hex_cursor; off_t cursor = view->hex_cursor;
@ -96,7 +96,7 @@ mcview_movement_fixups (WView * view, gboolean reset_search)
mcview_scroll_to_cursor (view); mcview_scroll_to_cursor (view);
if (reset_search) if (reset_search)
{ {
view->search_start = view->hex_mode ? view->hex_cursor : view->dpy_start; view->search_start = view->mode_flags.hex ? view->hex_cursor : view->dpy_start;
view->search_end = view->search_start; view->search_end = view->search_start;
} }
view->dirty++; view->dirty++;
@ -109,9 +109,10 @@ mcview_movement_fixups (WView * view, gboolean reset_search)
void void
mcview_move_up (WView * view, off_t lines) mcview_move_up (WView * view, off_t lines)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
off_t bytes = lines * view->bytes_per_line; off_t bytes = lines * view->bytes_per_line;
if (view->hex_cursor >= bytes) if (view->hex_cursor >= bytes)
{ {
view->hex_cursor -= bytes; view->hex_cursor -= bytes;
@ -140,8 +141,10 @@ void
mcview_move_down (WView * view, off_t lines) mcview_move_down (WView * view, off_t lines)
{ {
off_t last_byte; off_t last_byte;
last_byte = mcview_get_filesize (view); last_byte = mcview_get_filesize (view);
if (view->hex_mode)
if (view->mode_flags.hex)
{ {
off_t i, limit; off_t i, limit;
@ -170,7 +173,7 @@ mcview_move_down (WView * view, off_t lines)
void void
mcview_move_left (WView * view, off_t columns) mcview_move_left (WView * view, off_t columns)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
off_t old_cursor = view->hex_cursor; off_t old_cursor = view->hex_cursor;
@ -185,7 +188,7 @@ mcview_move_left (WView * view, off_t columns)
if (old_cursor > 0 || view->hexedit_lownibble) if (old_cursor > 0 || view->hexedit_lownibble)
view->hexedit_lownibble = !view->hexedit_lownibble; view->hexedit_lownibble = !view->hexedit_lownibble;
} }
else if (!view->text_wrap_mode) else if (!view->mode_flags.wrap)
view->dpy_text_column = mcview_offset_doz (view->dpy_text_column, columns); view->dpy_text_column = mcview_offset_doz (view->dpy_text_column, columns);
mcview_movement_fixups (view, FALSE); mcview_movement_fixups (view, FALSE);
} }
@ -195,7 +198,7 @@ mcview_move_left (WView * view, off_t columns)
void void
mcview_move_right (WView * view, off_t columns) mcview_move_right (WView * view, off_t columns)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
off_t last_byte; off_t last_byte;
off_t old_cursor = view->hex_cursor; off_t old_cursor = view->hex_cursor;
@ -213,7 +216,7 @@ mcview_move_right (WView * view, off_t columns)
if (old_cursor < last_byte || !view->hexedit_lownibble) if (old_cursor < last_byte || !view->hexedit_lownibble)
view->hexedit_lownibble = !view->hexedit_lownibble; view->hexedit_lownibble = !view->hexedit_lownibble;
} }
else if (!view->text_wrap_mode) else if (!view->mode_flags.wrap)
{ {
view->dpy_text_column += columns; view->dpy_text_column += columns;
} }
@ -247,7 +250,7 @@ mcview_moveto_bottom (WView * view)
filesize = mcview_get_filesize (view); filesize = mcview_get_filesize (view);
if (view->hex_mode) if (view->mode_flags.hex)
{ {
view->hex_cursor = mcview_offset_doz (filesize, 1); view->hex_cursor = mcview_offset_doz (filesize, 1);
mcview_movement_fixups (view, TRUE); mcview_movement_fixups (view, TRUE);
@ -268,7 +271,7 @@ mcview_moveto_bottom (WView * view)
void void
mcview_moveto_bol (WView * view) mcview_moveto_bol (WView * view)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
view->hex_cursor -= view->hex_cursor % view->bytes_per_line; view->hex_cursor -= view->hex_cursor % view->bytes_per_line;
view->dpy_text_column = 0; view->dpy_text_column = 0;
@ -286,7 +289,8 @@ void
mcview_moveto_eol (WView * view) mcview_moveto_eol (WView * view)
{ {
off_t bol; off_t bol;
if (view->hex_mode)
if (view->mode_flags.hex)
{ {
off_t filesize; off_t filesize;
@ -313,7 +317,7 @@ mcview_moveto_eol (WView * view)
void void
mcview_moveto_offset (WView * view, off_t offset) mcview_moveto_offset (WView * view, off_t offset)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
view->hex_cursor = offset; view->hex_cursor = offset;
view->dpy_start = offset - offset % view->bytes_per_line; view->dpy_start = offset - offset % view->bytes_per_line;
@ -365,7 +369,7 @@ mcview_offset_to_coord (WView * view, off_t * ret_line, off_t * ret_column, off_
mcview_ccache_lookup (view, &coord, CCACHE_LINECOL); mcview_ccache_lookup (view, &coord, CCACHE_LINECOL);
*ret_line = coord.cc_line; *ret_line = coord.cc_line;
*ret_column = (view->text_nroff_mode) ? coord.cc_nroff_column : coord.cc_column; *ret_column = view->mode_flags.nroff ? coord.cc_nroff_column : coord.cc_column;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -389,7 +393,7 @@ mcview_place_cursor (WView * view)
void void
mcview_moveto_match (WView * view) mcview_moveto_match (WView * view)
{ {
if (view->hex_mode) if (view->mode_flags.hex)
{ {
view->hex_cursor = view->search_start; view->hex_cursor = view->search_start;
view->hexedit_lownibble = FALSE; view->hexedit_lownibble = FALSE;

View File

@ -97,7 +97,7 @@ mcview__get_nroff_real_len (WView * view, off_t start, off_t length)
int ret = 0; int ret = 0;
off_t i = 0; off_t i = 0;
if (!view->text_nroff_mode) if (!view->mode_flags.nroff)
return 0; return 0;
nroff = mcview_nroff_seq_new_num (view, start); nroff = mcview_nroff_seq_new_num (view, start);

View File

@ -148,7 +148,7 @@ mcview_find (mcview_search_status_msg_t * ssm, off_t search_start, off_t search_
ok = mc_search_run (view->search, (void *) ssm, search_start, search_end, len); ok = mc_search_run (view->search, (void *) ssm, search_start, search_end, len);
if (ok && view->search->normal_offset == search_start) if (ok && view->search->normal_offset == search_start)
{ {
if (view->text_nroff_mode) if (view->mode_flags.nroff)
view->search->normal_offset++; view->search->normal_offset++;
return TRUE; return TRUE;
} }
@ -178,17 +178,17 @@ mcview_search_show_result (WView * view, size_t match_len)
int nroff_len; int nroff_len;
nroff_len = nroff_len =
view->text_nroff_mode view->mode_flags.nroff
? mcview__get_nroff_real_len (view, view->search->start_buffer, ? mcview__get_nroff_real_len (view, view->search->start_buffer,
view->search->normal_offset - view->search->start_buffer) : 0; view->search->normal_offset - view->search->start_buffer) : 0;
view->search_start = view->search->normal_offset + nroff_len; view->search_start = view->search->normal_offset + nroff_len;
if (!view->hex_mode) if (!view->mode_flags.hex)
view->search_start++; view->search_start++;
nroff_len = nroff_len =
view->text_nroff_mode ? mcview__get_nroff_real_len (view, view->search_start - 1, view->mode_flags.nroff ? mcview__get_nroff_real_len (view, view->search_start - 1,
match_len) : 0; match_len) : 0;
view->search_end = view->search_start + match_len + nroff_len; view->search_end = view->search_start + match_len + nroff_len;
mcview_moveto_match (view); mcview_moveto_match (view);
@ -204,7 +204,7 @@ mcview_search_cmd_callback (const void *user_data, gsize char_offset, int *curre
WView *view = ((const mcview_search_status_msg_t *) user_data)->view; WView *view = ((const mcview_search_status_msg_t *) user_data)->view;
/* view_read_continue (view, &view->search_onechar_info); *//* AB:FIXME */ /* view_read_continue (view, &view->search_onechar_info); *//* AB:FIXME */
if (!view->text_nroff_mode) if (!view->mode_flags.nroff)
{ {
mcview_get_byte (view, char_offset, current_char); mcview_get_byte (view, char_offset, current_char);
return MC_SEARCH_CB_OK; return MC_SEARCH_CB_OK;
@ -309,7 +309,7 @@ mcview_do_search (WView * view, off_t want_search_start)
if (view->search_start != 0) if (view->search_start != 0)
{ {
if (!view->text_nroff_mode) if (!view->mode_flags.nroff)
search_start = view->search_start + (mcview_search_options.backwards ? -2 : 0); search_start = view->search_start + (mcview_search_options.backwards ? -2 : 0);
else else
{ {