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,
41 #include <sys/types.h>
43 #include "lib/global.h"
44 #include "lib/vfs/mc-vfs/vfs.h"
45 #include "lib/strutil.h"
46 #include "lib/util.h" /* save_file_position() */
47 #include "lib/lock.h" /* unlock_file() */
49 #include "src/wtools.h"
51 #include "src/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 ************************************************************************/
71 /*** public functions ****************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
76 mcview_toggle_magic_mode (mcview_t
* view
)
78 char *filename
, *command
;
80 mcview_altered_magic_flag
= 1;
81 view
->magic_mode
= !view
->magic_mode
;
82 filename
= g_strdup (view
->filename
);
83 command
= g_strdup (view
->command
);
87 mcview_load (view
, command
, filename
, 0);
90 view
->dpy_bbar_dirty
= TRUE
;
94 /* --------------------------------------------------------------------------------------------- */
97 mcview_toggle_wrap_mode (mcview_t
* view
)
99 if (view
->text_wrap_mode
)
100 view
->dpy_start
= mcview_bol (view
, view
->dpy_start
, 0);
101 view
->text_wrap_mode
= !view
->text_wrap_mode
;
102 view
->dpy_bbar_dirty
= TRUE
;
106 /* --------------------------------------------------------------------------------------------- */
109 mcview_toggle_nroff_mode (mcview_t
* view
)
111 view
->text_nroff_mode
= !view
->text_nroff_mode
;
112 mcview_altered_nroff_flag
= 1;
113 view
->dpy_bbar_dirty
= TRUE
;
117 /* --------------------------------------------------------------------------------------------- */
120 mcview_toggle_hex_mode (mcview_t
* view
)
122 view
->hex_mode
= !view
->hex_mode
;
126 view
->hex_cursor
= view
->dpy_start
;
127 view
->dpy_start
= mcview_offset_rounddown (view
->dpy_start
, view
->bytes_per_line
);
128 widget_want_cursor (view
->widget
, 1);
132 view
->dpy_start
= view
->hex_cursor
;
133 mcview_moveto_bol (view
);
134 widget_want_cursor (view
->widget
, 0);
136 mcview_altered_hex_mode
= 1;
137 view
->dpy_bbar_dirty
= TRUE
;
141 /* --------------------------------------------------------------------------------------------- */
144 mcview_ok_to_quit (mcview_t
* view
)
148 if (view
->change_list
== NULL
)
151 if (!midnight_shutdown
)
154 r
= query_dialog (_("Quit"),
155 _("File was modified. Save with exit?"), D_NORMAL
, 3,
156 _("&Yes"), _("&No"), _("&Cancel quit"));
160 r
= query_dialog (_("Quit"),
161 _("Midnight Commander is being shut down.\nSave modified file?"),
162 D_NORMAL
, 2, _("&Yes"), _("&No"));
171 return mcview_hexedit_save_changes (view
) || midnight_shutdown
;
173 mcview_hexedit_free_change_list (view
);
180 /* --------------------------------------------------------------------------------------------- */
183 mcview_init (mcview_t
* view
)
187 view
->filename
= NULL
;
188 view
->workdir
= NULL
;
189 view
->command
= NULL
;
190 view
->search_nroff_seq
= NULL
;
192 mcview_set_datasource_none (view
);
194 view
->growbuf_in_use
= FALSE
;
195 /* leave the other growbuf fields uninitialized */
197 view
->hexedit_lownibble
= FALSE
;
198 view
->locked
= FALSE
;
199 view
->coord_cache
= NULL
;
202 view
->dpy_text_column
= 0;
204 view
->hex_cursor
= 0;
205 view
->cursor_col
= 0;
206 view
->cursor_row
= 0;
207 view
->change_list
= NULL
;
209 /* {status,ruler,data}_area are left uninitialized */
212 view
->dpy_bbar_dirty
= TRUE
;
213 view
->bytes_per_line
= 1;
215 view
->search_start
= 0;
216 view
->search_end
= 0;
219 for (i
= 0; i
< sizeof (view
->marks
) / sizeof (view
->marks
[0]); i
++)
223 view
->update_steps
= 0;
224 view
->update_activate
= 0;
226 view
->saved_bookmarks
= NULL
;
229 /* --------------------------------------------------------------------------------------------- */
232 mcview_done (mcview_t
* view
)
234 /* Save current file position */
235 if (mcview_remember_file_position
&& view
->filename
!= NULL
)
238 canon_fname
= vfs_canon (view
->filename
);
239 save_file_position (canon_fname
, -1, 0, view
->dpy_start
, view
->saved_bookmarks
);
240 view
->saved_bookmarks
= NULL
;
241 g_free (canon_fname
);
244 /* Write back the global viewer mode */
245 mcview_default_hex_mode
= view
->hex_mode
;
246 mcview_default_nroff_flag
= view
->text_nroff_mode
;
247 mcview_default_magic_flag
= view
->magic_mode
;
248 mcview_global_wrap_mode
= view
->text_wrap_mode
;
250 /* Free memory used by the viewer */
252 /* view->widget needs no destructor */
254 g_free (view
->filename
);
255 view
->filename
= NULL
;
256 g_free (view
->workdir
);
257 view
->workdir
= NULL
;
258 g_free (view
->command
);
259 view
->command
= NULL
;
261 mcview_close_datasource (view
);
262 /* the growing buffer is freed with the datasource */
264 coord_cache_free (view
->coord_cache
), view
->coord_cache
= NULL
;
266 if (view
->converter
== INVALID_CONV
)
267 view
->converter
= str_cnv_from_term
;
269 if (view
->converter
!= str_cnv_from_term
)
271 str_close_conv (view
->converter
);
272 view
->converter
= str_cnv_from_term
;
275 mc_search_free (view
->search
);
277 g_free (view
->last_search_string
);
278 view
->last_search_string
= NULL
;
279 mcview_nroff_seq_free (&view
->search_nroff_seq
);
280 mcview_hexedit_free_change_list (view
);
283 /* --------------------------------------------------------------------------------------------- */
286 mcview_set_codeset (mcview_t
* view
)
289 const char *cp_id
= NULL
;
292 cp_id
= get_codepage_id (source_codepage
>= 0 ? source_codepage
: display_codepage
);
296 conv
= str_crt_conv_from (cp_id
);
297 if (conv
!= INVALID_CONV
)
299 if (view
->converter
!= str_cnv_from_term
)
300 str_close_conv (view
->converter
);
301 view
->converter
= conv
;
303 view
->utf8
= (gboolean
) str_isutf8 (cp_id
);
310 /* --------------------------------------------------------------------------------------------- */
313 mcview_select_encoding (mcview_t
* view
)
316 if (do_select_codepage ())
318 mcview_set_codeset (view
);
325 /* --------------------------------------------------------------------------------------------- */
328 mcview_show_error (mcview_t
* view
, const char *msg
)
330 mcview_close_datasource (view
);
331 if (mcview_is_in_panel (view
))
333 mcview_set_datasource_string (view
, msg
);
337 message (D_ERROR
, MSG_ERROR
, "%s", msg
);
341 /* --------------------------------------------------------------------------------------------- */
343 /* returns index of the first char in the line */
344 /* it is constant for all line characters */
346 mcview_bol (mcview_t
* view
, off_t current
, off_t limit
)
350 filesize
= mcview_get_filesize (view
);
353 if (current
> filesize
)
355 if (!mcview_get_byte (view
, current
, &c
))
359 if (!mcview_get_byte (view
, current
- 1, &c
))
364 while (current
> 0 && current
>= limit
)
366 if (!mcview_get_byte (view
, current
- 1, &c
))
368 if (c
== '\r' || c
== '\n')
375 /* --------------------------------------------------------------------------------------------- */
377 /* returns index of last char on line + width EOL */
378 /* mcview_eol of the current line == mcview_bol next line */
380 mcview_eol (mcview_t
* view
, off_t current
, off_t limit
)
384 filesize
= mcview_get_filesize (view
);
387 if (current
>= filesize
)
389 while (current
< filesize
&& current
< limit
)
391 if (!mcview_get_byte (view
, current
, &c
))
398 else if (prev_ch
== '\r')
408 /* --------------------------------------------------------------------------------------------- */
411 mcview_get_title (const Dlg_head
* h
, size_t len
)
413 const mcview_t
*view
= (const mcview_t
*) find_widget_type (h
, mcview_callback
);
414 const char *modified
= view
->hexedit_mode
&& (view
->change_list
!= NULL
) ? "(*) " : " ";
415 const char *file_label
;
419 file_label
= view
->filename
!= NULL
? view
->filename
:
420 view
->command
!= NULL
? view
->command
: "";
421 file_label
= str_term_trim (file_label
, len
- str_term_width1 (_("View: ")));
423 return g_strconcat (_("View: "), modified
, file_label
, (char *) NULL
);
425 /* --------------------------------------------------------------------------------------------- */
428 mcview_lock_file (mcview_t
* view
)
433 fullpath
= g_build_filename (view
->workdir
, view
->filename
, (char *) NULL
);
434 ret
= lock_file (fullpath
);
440 /* --------------------------------------------------------------------------------------------- */
443 mcview_unlock_file (mcview_t
* view
)
448 fullpath
= g_build_filename (view
->workdir
, view
->filename
, (char *) NULL
);
449 ret
= unlock_file (fullpath
);
455 /* --------------------------------------------------------------------------------------------- */