2 Internal file viewer for the Midnight Commander
3 Common finctions (used from some other mcviewer functions)
5 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
6 2004, 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
8 Written by: 1994, 1995, 1998 Miguel de Icaza
9 1994, 1995 Janne Kukonlehto
14 2004 Roland Illig <roland.illig@gmx.de>
15 2005 Roland Illig <roland.illig@gmx.de>
16 2009 Slava Zanko <slavazanko@google.com>
17 2009 Andrew Borodin <aborodin@vmail.ru>
18 2009 Ilia Maslakov <il.smind@gmail.com>
20 This file is part of the Midnight Commander.
22 The Midnight Commander is free software; you can redistribute it
23 and/or modify it under the terms of the GNU General Public License as
24 published by the Free Software Foundation; either version 2 of the
25 License, or (at your option) any later version.
27 The Midnight Commander is distributed in the hope that it will be
28 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
29 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
30 General Public License for more details.
32 You should have received a copy of the GNU General Public License
33 along with this program; if not, write to the Free Software
34 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
42 #include "lib/global.h"
43 #include "lib/vfs/mc-vfs/vfs.h"
44 #include "lib/strutil.h"
46 #include "src/wtools.h"
48 #include "src/charsets.h"
49 #include "src/selcodepage.h"
54 /*** global variables ****************************************************************************/
56 #define OFF_T_BITWIDTH (unsigned int) (sizeof (off_t) * CHAR_BIT - 1)
57 const off_t INVALID_OFFSET
= (off_t
) - 1;
58 const off_t OFFSETTYPE_MAX
= ((off_t
) 1 << (OFF_T_BITWIDTH
- 1)) - 1;
60 /*** file scope macro definitions ****************************************************************/
62 /*** file scope type declarations ****************************************************************/
64 /*** file scope variables ************************************************************************/
66 /*** file scope functions ************************************************************************/
68 /*** public functions ****************************************************************************/
70 /* --------------------------------------------------------------------------------------------- */
73 mcview_toggle_magic_mode (mcview_t
* view
)
75 char *filename
, *command
;
77 mcview_altered_magic_flag
= 1;
78 view
->magic_mode
= !view
->magic_mode
;
79 filename
= g_strdup (view
->filename
);
80 command
= g_strdup (view
->command
);
83 mcview_load (view
, command
, filename
, 0);
86 view
->dpy_bbar_dirty
= TRUE
;
90 /* --------------------------------------------------------------------------------------------- */
93 mcview_toggle_wrap_mode (mcview_t
* view
)
95 view
->text_wrap_mode
= !view
->text_wrap_mode
;
96 view
->dpy_bbar_dirty
= TRUE
;
100 /* --------------------------------------------------------------------------------------------- */
103 mcview_toggle_nroff_mode (mcview_t
* view
)
105 view
->text_nroff_mode
= !view
->text_nroff_mode
;
106 mcview_altered_nroff_flag
= 1;
107 view
->dpy_bbar_dirty
= TRUE
;
111 /* --------------------------------------------------------------------------------------------- */
114 mcview_toggle_hex_mode (mcview_t
* view
)
116 view
->hex_mode
= !view
->hex_mode
;
120 view
->hex_cursor
= view
->dpy_start
;
121 view
->dpy_start
= mcview_offset_rounddown (view
->dpy_start
, view
->bytes_per_line
);
122 view
->widget
.options
|= W_WANT_CURSOR
;
126 view
->dpy_start
= view
->hex_cursor
;
127 mcview_moveto_bol (view
);
128 view
->widget
.options
&= ~W_WANT_CURSOR
;
130 mcview_altered_hex_mode
= 1;
131 view
->dpy_bbar_dirty
= TRUE
;
135 /* --------------------------------------------------------------------------------------------- */
138 mcview_ok_to_quit (mcview_t
* view
)
142 if (view
->change_list
== NULL
)
145 r
= query_dialog (_("Quit"),
146 _(" File was modified, Save with exit? "), D_NORMAL
, 3,
147 _("&Cancel quit"), _("&Yes"), _("&No"));
152 return mcview_hexedit_save_changes (view
);
154 mcview_hexedit_free_change_list (view
);
161 /* --------------------------------------------------------------------------------------------- */
164 mcview_done (mcview_t
* view
)
166 /* Save current file position */
167 if (mcview_remember_file_position
&& view
->filename
!= NULL
)
170 canon_fname
= vfs_canon (view
->filename
);
171 save_file_position (canon_fname
, -1, 0, view
->dpy_start
);
172 g_free (canon_fname
);
175 /* Write back the global viewer mode */
176 mcview_default_hex_mode
= view
->hex_mode
;
177 mcview_default_nroff_flag
= view
->text_nroff_mode
;
178 mcview_default_magic_flag
= view
->magic_mode
;
179 mcview_global_wrap_mode
= view
->text_wrap_mode
;
181 /* Free memory used by the viewer */
183 /* view->widget needs no destructor */
185 g_free (view
->filename
), view
->filename
= NULL
;
186 g_free (view
->command
), view
->command
= NULL
;
188 mcview_close_datasource (view
);
189 /* the growing buffer is freed with the datasource */
191 coord_cache_free (view
->coord_cache
), view
->coord_cache
= NULL
;
193 if (!(view
->converter
== INVALID_CONV
|| view
->converter
!= str_cnv_from_term
))
195 str_close_conv (view
->converter
);
196 view
->converter
= str_cnv_from_term
;
199 mcview_hexedit_free_change_list (view
);
202 /* --------------------------------------------------------------------------------------------- */
205 mcview_set_codeset (mcview_t
* view
)
208 const char *cp_id
= NULL
;
211 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
215 conv
= str_crt_conv_from (cp_id
);
216 if (conv
!= INVALID_CONV
)
218 if (view
->converter
!= str_cnv_from_term
)
219 str_close_conv (view
->converter
);
220 view
->converter
= conv
;
222 view
->utf8
= (gboolean
) str_isutf8 (cp_id
);
229 /* --------------------------------------------------------------------------------------------- */
232 mcview_select_encoding (mcview_t
* view
)
235 if (do_select_codepage ())
237 mcview_set_codeset (view
);
244 /* --------------------------------------------------------------------------------------------- */
247 mcview_show_error (mcview_t
* view
, const char *msg
)
249 mcview_close_datasource (view
);
250 if (mcview_is_in_panel (view
))
252 mcview_set_datasource_string (view
, msg
);
256 message (D_ERROR
, MSG_ERROR
, "%s", msg
);
260 /* --------------------------------------------------------------------------------------------- */
262 /* returns index of the first char in the line */
263 /* it is constant for all line characters */
265 mcview_bol (mcview_t
* view
, off_t current
)
269 filesize
= mcview_get_filesize (view
);
272 if (current
> filesize
)
274 if (!mcview_get_byte (view
, current
, &c
))
278 if (!mcview_get_byte (view
, current
- 1, &c
))
285 if (!mcview_get_byte (view
, current
- 1, &c
))
287 if (c
== '\r' || c
== '\n')
294 /* returns index of last char on line + width EOL */
295 /* mcview_eol of the current line == mcview_bol next line */
297 mcview_eol (mcview_t
* view
, off_t current
)
301 filesize
= mcview_get_filesize (view
);
304 if (current
>= filesize
)
306 while (current
< filesize
)
308 if (!mcview_get_byte (view
, current
, &c
))
315 else if (prev_ch
== '\r')