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, 2011, 2013
7 The Free Software Foundation, Inc.
10 Miguel de Icaza, 1994, 1995, 1998
11 Janne Kukonlehto, 1994, 1995
13 Joseph M. Hinkle, 1996
16 Roland Illig <roland.illig@gmx.de>, 2004, 2005
17 Slava Zanko <slavazanko@google.com>, 2009, 2013
18 Andrew Borodin <aborodin@vmail.ru>, 2009, 2013
19 Ilia Maslakov <il.smind@gmail.com>, 2009
21 This file is part of the Midnight Commander.
23 The Midnight Commander is free software: you can redistribute it
24 and/or modify it under the terms of the GNU General Public License as
25 published by the Free Software Foundation, either version 3 of the License,
26 or (at your option) any later version.
28 The Midnight Commander is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU General Public License for more details.
33 You should have received a copy of the GNU General Public License
34 along with this program. If not, see <http://www.gnu.org/licenses/>.
40 #include <sys/types.h>
42 #include "lib/global.h"
43 #include "lib/vfs/vfs.h"
44 #include "lib/strutil.h"
45 #include "lib/util.h" /* save_file_position() */
46 #include "lib/widget.h"
48 #include "lib/charsets.h"
52 #include "src/selcodepage.h"
57 /*** global variables ****************************************************************************/
59 #define OFF_T_BITWIDTH (unsigned int) (sizeof (off_t) * CHAR_BIT - 1)
60 const off_t INVALID_OFFSET
= (off_t
) (-1);
61 const off_t OFFSETTYPE_MAX
= ((off_t
) 1 << (OFF_T_BITWIDTH
- 1)) - 1;
63 /*** file scope macro definitions ****************************************************************/
65 /*** file scope type declarations ****************************************************************/
67 /*** file scope variables ************************************************************************/
69 /*** file scope functions ************************************************************************/
70 /* --------------------------------------------------------------------------------------------- */
72 /* --------------------------------------------------------------------------------------------- */
73 /*** public functions ****************************************************************************/
74 /* --------------------------------------------------------------------------------------------- */
77 mcview_toggle_magic_mode (mcview_t
* view
)
83 mcview_altered_magic_flag
= 1;
84 view
->magic_mode
= !view
->magic_mode
;
87 command
= g_strdup (view
->command
);
89 dir_idx
= view
->dir_idx
;
94 mcview_load (view
, command
, vfs_path_as_str (view
->filename_vpath
), 0);
96 view
->dir_idx
= dir_idx
;
99 view
->dpy_bbar_dirty
= TRUE
;
103 /* --------------------------------------------------------------------------------------------- */
106 mcview_toggle_wrap_mode (mcview_t
* view
)
108 if (view
->text_wrap_mode
)
109 view
->dpy_start
= mcview_bol (view
, view
->dpy_start
, 0);
110 view
->text_wrap_mode
= !view
->text_wrap_mode
;
111 view
->dpy_bbar_dirty
= TRUE
;
115 /* --------------------------------------------------------------------------------------------- */
118 mcview_toggle_nroff_mode (mcview_t
* view
)
120 view
->text_nroff_mode
= !view
->text_nroff_mode
;
121 mcview_altered_nroff_flag
= 1;
122 view
->dpy_bbar_dirty
= TRUE
;
126 /* --------------------------------------------------------------------------------------------- */
129 mcview_toggle_hex_mode (mcview_t
* view
)
131 view
->hex_mode
= !view
->hex_mode
;
135 view
->hex_cursor
= view
->dpy_start
;
136 view
->dpy_start
= mcview_offset_rounddown (view
->dpy_start
, view
->bytes_per_line
);
137 widget_want_cursor (WIDGET (view
), TRUE
);
141 view
->dpy_start
= mcview_bol (view
, view
->hex_cursor
, 0);
142 view
->hex_cursor
= view
->dpy_start
;
143 widget_want_cursor (WIDGET (view
), FALSE
);
145 mcview_altered_hex_mode
= 1;
146 view
->dpy_bbar_dirty
= TRUE
;
150 /* --------------------------------------------------------------------------------------------- */
153 mcview_init (mcview_t
* view
)
157 view
->filename_vpath
= NULL
;
158 view
->workdir_vpath
= NULL
;
159 view
->command
= NULL
;
160 view
->search_nroff_seq
= NULL
;
162 mcview_set_datasource_none (view
);
164 view
->growbuf_in_use
= FALSE
;
165 /* leave the other growbuf fields uninitialized */
167 view
->hexedit_lownibble
= FALSE
;
168 view
->locked
= FALSE
;
169 view
->coord_cache
= NULL
;
172 view
->dpy_text_column
= 0;
174 view
->hex_cursor
= 0;
175 view
->cursor_col
= 0;
176 view
->cursor_row
= 0;
177 view
->change_list
= NULL
;
179 /* {status,ruler,data}_area are left uninitialized */
182 view
->dpy_bbar_dirty
= TRUE
;
183 view
->bytes_per_line
= 1;
185 view
->search_start
= 0;
186 view
->search_end
= 0;
189 for (i
= 0; i
< G_N_ELEMENTS (view
->marks
); i
++)
192 view
->update_steps
= 0;
193 view
->update_activate
= 0;
195 view
->saved_bookmarks
= NULL
;
198 /* --------------------------------------------------------------------------------------------- */
201 mcview_done (mcview_t
* view
)
203 /* Save current file position */
204 if (mcview_remember_file_position
&& view
->filename_vpath
!= NULL
)
206 save_file_position (view
->filename_vpath
, -1, 0,
207 view
->hex_mode
? view
->hex_cursor
: view
->dpy_start
,
208 view
->saved_bookmarks
);
209 view
->saved_bookmarks
= NULL
;
212 /* Write back the global viewer mode */
213 mcview_default_hex_mode
= view
->hex_mode
;
214 mcview_default_nroff_flag
= view
->text_nroff_mode
;
215 mcview_default_magic_flag
= view
->magic_mode
;
216 mcview_global_wrap_mode
= view
->text_wrap_mode
;
218 /* Free memory used by the viewer */
220 /* view->widget needs no destructor */
222 vfs_path_free (view
->filename_vpath
);
223 view
->filename_vpath
= NULL
;
224 vfs_path_free (view
->workdir_vpath
);
225 view
->workdir_vpath
= NULL
;
226 g_free (view
->command
);
227 view
->command
= NULL
;
229 mcview_close_datasource (view
);
230 /* the growing buffer is freed with the datasource */
232 coord_cache_free (view
->coord_cache
), view
->coord_cache
= NULL
;
234 if (view
->converter
== INVALID_CONV
)
235 view
->converter
= str_cnv_from_term
;
237 if (view
->converter
!= str_cnv_from_term
)
239 str_close_conv (view
->converter
);
240 view
->converter
= str_cnv_from_term
;
243 mc_search_free (view
->search
);
245 g_free (view
->last_search_string
);
246 view
->last_search_string
= NULL
;
247 mcview_nroff_seq_free (&view
->search_nroff_seq
);
248 mcview_hexedit_free_change_list (view
);
250 if (mc_global
.mc_run_mode
== MC_RUN_VIEWER
&& view
->dir
!= NULL
)
252 /* mcviewer is the owner of file list */
253 dir_list_clean (view
->dir
);
254 g_free (view
->dir
->list
);
255 g_free (view
->dir_idx
);
262 /* --------------------------------------------------------------------------------------------- */
265 mcview_set_codeset (mcview_t
* view
)
268 const char *cp_id
= NULL
;
272 get_codepage_id (mc_global
.source_codepage
>=
273 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
277 conv
= str_crt_conv_from (cp_id
);
278 if (conv
!= INVALID_CONV
)
280 if (view
->converter
!= str_cnv_from_term
)
281 str_close_conv (view
->converter
);
282 view
->converter
= conv
;
284 view
->utf8
= (gboolean
) str_isutf8 (cp_id
);
291 /* --------------------------------------------------------------------------------------------- */
295 mcview_select_encoding (mcview_t
* view
)
297 if (do_select_codepage ())
298 mcview_set_codeset (view
);
302 /* --------------------------------------------------------------------------------------------- */
305 mcview_show_error (mcview_t
* view
, const char *msg
)
307 mcview_close_datasource (view
);
308 if (mcview_is_in_panel (view
))
310 mcview_set_datasource_string (view
, msg
);
314 message (D_ERROR
, MSG_ERROR
, "%s", msg
);
318 /* --------------------------------------------------------------------------------------------- */
319 /** returns index of the first char in the line
320 * it is constant for all line characters
324 mcview_bol (mcview_t
* view
, off_t current
, off_t limit
)
328 filesize
= mcview_get_filesize (view
);
331 if (current
> filesize
)
333 if (!mcview_get_byte (view
, current
, &c
))
337 if (!mcview_get_byte (view
, current
- 1, &c
))
342 while (current
> 0 && current
>= limit
)
344 if (!mcview_get_byte (view
, current
- 1, &c
))
346 if (c
== '\r' || c
== '\n')
353 /* --------------------------------------------------------------------------------------------- */
354 /** returns index of last char on line + width EOL
355 * mcview_eol of the current line == mcview_bol next line
359 mcview_eol (mcview_t
* view
, off_t current
, off_t limit
)
363 filesize
= mcview_get_filesize (view
);
366 if (current
>= filesize
)
368 while (current
< filesize
&& current
< limit
)
370 if (!mcview_get_byte (view
, current
, &c
))
377 else if (prev_ch
== '\r')
387 /* --------------------------------------------------------------------------------------------- */
390 mcview_get_title (const WDialog
* h
, size_t len
)
392 const mcview_t
*view
= (const mcview_t
*) find_widget_type (h
, mcview_callback
);
393 const char *modified
= view
->hexedit_mode
&& (view
->change_list
!= NULL
) ? "(*) " : " ";
394 const char *file_label
;
395 const char *view_filename
;
398 view_filename
= vfs_path_as_str (view
->filename_vpath
);
402 file_label
= view_filename
!= NULL
? view_filename
: view
->command
!= NULL
? view
->command
: "";
403 file_label
= str_term_trim (file_label
, len
- str_term_width1 (_("View: ")));
405 ret_str
= g_strconcat (_("View: "), modified
, file_label
, (char *) NULL
);
409 /* --------------------------------------------------------------------------------------------- */