Ticket #1791: a nice dark skin created by Lee Bigelow.
[kaloumi3.git] / src / viewer / lib.c
blob947e2ace53e418f3e235613b0baebf9d17456e2b
1 /*
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
10 1995 Jakub Jelinek
11 1996 Joseph M. Hinkle
12 1997 Norbert Warmuth
13 1998 Pavel Machek
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,
35 MA 02110-1301, USA.
38 #include <config.h>
40 #include <limits.h>
42 #include "lib/global.h"
43 #include "lib/vfs/mc-vfs/vfs.h"
44 #include "lib/strutil.h"
46 #include "src/wtools.h"
47 #include "src/main.h"
48 #include "src/charsets.h"
49 #include "src/selcodepage.h"
51 #include "internal.h"
52 #include "mcviewer.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 /* --------------------------------------------------------------------------------------------- */
72 void
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);
82 mcview_done (view);
83 mcview_load (view, command, filename, 0);
84 g_free (filename);
85 g_free (command);
86 view->dpy_bbar_dirty = TRUE;
87 view->dirty++;
90 /* --------------------------------------------------------------------------------------------- */
92 void
93 mcview_toggle_wrap_mode (mcview_t * view)
95 view->text_wrap_mode = !view->text_wrap_mode;
96 view->dpy_bbar_dirty = TRUE;
97 view->dirty++;
100 /* --------------------------------------------------------------------------------------------- */
102 void
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;
108 view->dirty++;
111 /* --------------------------------------------------------------------------------------------- */
113 void
114 mcview_toggle_hex_mode (mcview_t * view)
116 view->hex_mode = !view->hex_mode;
118 if (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;
124 else
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;
132 view->dirty++;
135 /* --------------------------------------------------------------------------------------------- */
137 gboolean
138 mcview_ok_to_quit (mcview_t * view)
140 int r;
142 if (view->change_list == NULL)
143 return TRUE;
145 r = query_dialog (_("Quit"),
146 _(" File was modified, Save with exit? "), D_NORMAL, 3,
147 _("&Cancel quit"), _("&Yes"), _("&No"));
149 switch (r)
151 case 1:
152 return mcview_hexedit_save_changes (view);
153 case 2:
154 mcview_hexedit_free_change_list (view);
155 return TRUE;
156 default:
157 return FALSE;
161 /* --------------------------------------------------------------------------------------------- */
163 void
164 mcview_done (mcview_t * view)
166 /* Save current file position */
167 if (mcview_remember_file_position && view->filename != NULL)
169 char *canon_fname;
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 /* --------------------------------------------------------------------------------------------- */
204 void
205 mcview_set_codeset (mcview_t * view)
207 #ifdef HAVE_CHARSET
208 const char *cp_id = NULL;
210 view->utf8 = TRUE;
211 cp_id = get_codepage_id (source_codepage >= 0 ? source_codepage : display_codepage);
212 if (cp_id != NULL)
214 GIConv conv;
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);
224 #else
225 (void) view;
226 #endif
229 /* --------------------------------------------------------------------------------------------- */
231 void
232 mcview_select_encoding (mcview_t * view)
234 #ifdef HAVE_CHARSET
235 if (do_select_codepage ())
237 mcview_set_codeset (view);
239 #else
240 (void) view;
241 #endif
244 /* --------------------------------------------------------------------------------------------- */
246 void
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);
254 else
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 */
264 off_t
265 mcview_bol (mcview_t * view, off_t current)
267 int c;
268 off_t filesize;
269 filesize = mcview_get_filesize (view);
270 if (current <= 0)
271 return 0;
272 if (current > filesize)
273 return filesize;
274 if (!mcview_get_byte (view, current, &c))
275 return current;
276 if (c == '\n')
278 if (!mcview_get_byte (view, current - 1, &c))
279 return current;
280 if (c == '\r')
281 current--;
283 while (current > 0)
285 if (!mcview_get_byte (view, current - 1, &c))
286 break;
287 if (c == '\r' || c == '\n')
288 break;
289 current--;
291 return current;
294 /* returns index of last char on line + width EOL */
295 /* mcview_eol of the current line == mcview_bol next line */
296 off_t
297 mcview_eol (mcview_t * view, off_t current)
299 int c, prev_ch = 0;
300 off_t filesize;
301 filesize = mcview_get_filesize (view);
302 if (current < 0)
303 return 0;
304 if (current >= filesize)
305 return filesize;
306 while (current < filesize)
308 if (!mcview_get_byte (view, current, &c))
309 break;
310 if (c == '\n')
312 current++;
313 break;
315 else if (prev_ch == '\r')
317 break;
319 current++;
320 prev_ch = c;
322 return current;