Split file src/keybind.[ch] to lib/keybind.[ch] and src/keybind-defaults.[ch].
[midnight-commander.git] / src / viewer / mcviewer.c
blobace207fb440f43378fa70294129ad7ee2027217a
1 /*
2 Internal file viewer for the Midnight Commander
3 Interface 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>
39 #include <errno.h>
40 #include <fcntl.h>
42 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/mouse.h"
45 #include "lib/vfs/mc-vfs/vfs.h"
46 #include "lib/strutil.h"
47 #include "lib/util.h" /* load_file_position() */
48 #include "lib/widget.h"
49 #include "lib/charsets.h"
51 #include "src/main.h"
52 #include "src/layout.h" /* menubar_visible */
53 #include "src/main-widgets.h" /* the_menubar */
55 #include "internal.h"
56 #include "mcviewer.h"
58 /*** global variables ****************************************************************************/
60 int mcview_default_hex_mode = 0;
61 int mcview_default_nroff_flag = 0;
62 int mcview_global_wrap_mode = 1;
63 int mcview_default_magic_flag = 1;
65 int mcview_altered_hex_mode = 0;
66 int mcview_altered_magic_flag = 0;
67 int mcview_altered_nroff_flag = 0;
69 int mcview_remember_file_position = FALSE;
71 /* Maxlimit for skipping updates */
72 int mcview_max_dirt_limit = 10;
74 /* Scrolling is done in pages or line increments */
75 int mcview_mouse_move_pages = 1;
77 /* end of file will be showen from mcview_show_eof */
78 char *mcview_show_eof = NULL;
80 /*** file scope macro definitions ****************************************************************/
82 /*** file scope type declarations ****************************************************************/
84 /*** file scope variables ************************************************************************/
87 /*** file scope functions ************************************************************************/
88 /* --------------------------------------------------------------------------------------------- */
90 /** Both views */
92 static int
93 mcview_event (mcview_t * view, Gpm_Event * event, int *result)
95 screen_dimen y, x;
97 *result = MOU_NORMAL;
99 /* rest of the upper frame, the menu is invisible - call menu */
100 if (mcview_is_in_panel (view) && (event->type & GPM_DOWN) && event->y == 1 && !menubar_visible)
102 event->x += view->widget.x;
103 *result = the_menubar->widget.mouse (event, the_menubar);
104 return 0; /* don't draw viewer over menu */
107 /* We are not interested in the release events */
108 if (!(event->type & (GPM_DOWN | GPM_DRAG)))
109 return 0;
111 /* Wheel events */
112 if ((event->buttons & GPM_B_UP) && (event->type & GPM_DOWN))
114 mcview_move_up (view, 2);
115 return 1;
117 if ((event->buttons & GPM_B_DOWN) && (event->type & GPM_DOWN))
119 mcview_move_down (view, 2);
120 return 1;
123 x = event->x;
124 y = event->y;
126 /* Scrolling left and right */
127 if (!view->text_wrap_mode)
129 if (x < view->data_area.width * 1 / 4)
131 mcview_move_left (view, 1);
132 goto processed;
134 else if (x < view->data_area.width * 3 / 4)
136 /* ignore the click */
138 else
140 mcview_move_right (view, 1);
141 goto processed;
145 /* Scrolling up and down */
146 if (y < view->data_area.top + view->data_area.height * 1 / 3)
148 if (mcview_mouse_move_pages)
149 mcview_move_up (view, view->data_area.height / 2);
150 else
151 mcview_move_up (view, 1);
152 goto processed;
154 else if (y < view->data_area.top + view->data_area.height * 2 / 3)
156 /* ignore the click */
158 else
160 if (mcview_mouse_move_pages)
161 mcview_move_down (view, view->data_area.height / 2);
162 else
163 mcview_move_down (view, 1);
164 goto processed;
167 return 0;
169 processed:
170 *result = MOU_REPEAT;
171 return 1;
174 /* --------------------------------------------------------------------------------------------- */
175 /** Real view only */
177 static int
178 mcview_real_event (Gpm_Event * event, void *x)
180 mcview_t *view = (mcview_t *) x;
181 int result;
183 if (mcview_event (view, event, &result))
184 mcview_update (view);
185 return result;
188 /* --------------------------------------------------------------------------------------------- */
190 static void
191 mcview_set_keymap (mcview_t * view)
193 view->plain_map = default_viewer_keymap;
194 if (viewer_keymap && viewer_keymap->len > 0)
195 view->plain_map = (global_keymap_t *) viewer_keymap->data;
197 view->hex_map = default_viewer_hex_keymap;
198 if (viewer_hex_keymap && viewer_hex_keymap->len > 0)
199 view->hex_map = (global_keymap_t *) viewer_hex_keymap->data;
202 /* --------------------------------------------------------------------------------------------- */
203 /*** public functions ****************************************************************************/
204 /* --------------------------------------------------------------------------------------------- */
206 mcview_t *
207 mcview_new (int y, int x, int lines, int cols, gboolean is_panel)
209 mcview_t *view = g_new0 (mcview_t, 1);
211 init_widget (&view->widget, y, x, lines, cols, mcview_callback, mcview_real_event);
213 mcview_set_keymap (view);
215 view->hex_mode = FALSE;
216 view->hexedit_mode = FALSE;
217 view->locked = FALSE;
218 view->hexview_in_text = FALSE;
219 view->text_nroff_mode = FALSE;
220 view->text_wrap_mode = FALSE;
221 view->magic_mode = FALSE;
223 view->dpy_frame_size = is_panel ? 1 : 0;
224 view->converter = str_cnv_from_term;
226 mcview_init (view);
228 if (mcview_default_hex_mode)
229 mcview_toggle_hex_mode (view);
230 if (mcview_default_nroff_flag)
231 mcview_toggle_nroff_mode (view);
232 if (mcview_global_wrap_mode)
233 mcview_toggle_wrap_mode (view);
234 if (mcview_default_magic_flag)
235 mcview_toggle_magic_mode (view);
237 return view;
240 /* --------------------------------------------------------------------------------------------- */
241 /** Real view only */
243 mcview_ret_t
244 mcview_viewer (const char *command, const char *file, int start_line)
246 gboolean succeeded;
247 mcview_t *lc_mcview;
248 Dlg_head *view_dlg;
249 mcview_ret_t ret;
251 /* Create dialog and widgets, put them on the dialog */
252 view_dlg = create_dlg (FALSE, 0, 0, LINES, COLS, NULL, mcview_dialog_callback,
253 "[Internal File Viewer]", NULL, DLG_WANT_TAB);
255 lc_mcview = mcview_new (0, 0, LINES - 1, COLS, FALSE);
256 add_widget (view_dlg, lc_mcview);
258 add_widget (view_dlg, buttonbar_new (TRUE));
260 view_dlg->get_title = mcview_get_title;
262 succeeded = mcview_load (lc_mcview, command, file, start_line);
264 if (succeeded)
266 run_dlg (view_dlg);
268 ret = lc_mcview->move_dir == 0 ? MCVIEW_EXIT_OK :
269 lc_mcview->move_dir > 0 ? MCVIEW_WANT_NEXT : MCVIEW_WANT_PREV;
271 else
273 view_dlg->state = DLG_CLOSED;
274 ret = MCVIEW_EXIT_FAILURE;
277 if (view_dlg->state == DLG_CLOSED)
278 destroy_dlg (view_dlg);
280 return ret;
283 /* {{{ Miscellaneous functions }}} */
285 /* --------------------------------------------------------------------------------------------- */
287 gboolean
288 mcview_load (mcview_t * view, const char *command, const char *file, int start_line)
290 gboolean retval = FALSE;
292 assert (view->bytes_per_line != 0);
294 view->filename = g_strdup (file);
296 if ((view->workdir == NULL) && (file != NULL))
298 if (!g_path_is_absolute (file))
299 view->workdir = g_strdup (vfs_get_current_dir ());
300 else
302 /* try extract path form filename */
303 char *dirname;
305 dirname = g_path_get_dirname (file);
306 if (strcmp (dirname, ".") != 0)
307 view->workdir = dirname;
308 else
310 g_free (dirname);
311 view->workdir = g_strdup (vfs_get_current_dir ());
316 if (!mcview_is_in_panel (view))
317 view->dpy_text_column = 0;
319 mcview_set_codeset (view);
321 if (command != NULL && (view->magic_mode || file == NULL || file[0] == '\0'))
322 retval = mcview_load_command_output (view, command);
323 else if (file != NULL && file[0] != '\0')
325 int fd = -1;
326 char tmp[BUF_MEDIUM];
327 struct stat st;
329 /* Open the file */
330 fd = mc_open (file, O_RDONLY | O_NONBLOCK);
331 if (fd == -1)
333 g_snprintf (tmp, sizeof (tmp), _("Cannot open \"%s\"\n%s"),
334 file, unix_error_string (errno));
335 mcview_show_error (view, tmp);
336 g_free (view->filename);
337 view->filename = NULL;
338 g_free (view->workdir);
339 view->workdir = NULL;
340 goto finish;
343 /* Make sure we are working with a regular file */
344 if (mc_fstat (fd, &st) == -1)
346 mc_close (fd);
347 g_snprintf (tmp, sizeof (tmp), _("Cannot stat \"%s\"\n%s"),
348 file, unix_error_string (errno));
349 mcview_show_error (view, tmp);
350 g_free (view->filename);
351 view->filename = NULL;
352 g_free (view->workdir);
353 view->workdir = NULL;
354 goto finish;
357 if (!S_ISREG (st.st_mode))
359 mc_close (fd);
360 mcview_show_error (view, _("Cannot view: not a regular file"));
361 g_free (view->filename);
362 view->filename = NULL;
363 g_free (view->workdir);
364 view->workdir = NULL;
365 goto finish;
368 if (st.st_size == 0 || mc_lseek (fd, 0, SEEK_SET) == -1)
370 /* Must be one of those nice files that grow (/proc) */
371 mcview_set_datasource_vfs_pipe (view, fd);
373 else
375 int type;
377 type = get_compression_type (fd, file);
379 if (view->magic_mode && (type != COMPRESSION_NONE))
381 g_free (view->filename);
382 view->filename = g_strconcat (file, decompress_extension (type), (char *) NULL);
384 mcview_set_datasource_file (view, fd, &st);
386 retval = TRUE;
389 finish:
390 view->command = g_strdup (command);
391 view->dpy_start = 0;
392 view->search_start = 0;
393 view->search_end = 0;
394 view->dpy_text_column = 0;
396 mcview_compute_areas (view);
397 mcview_update_bytes_per_line (view);
399 if (mcview_remember_file_position && view->filename != NULL && start_line == 0)
401 char *canon_fname;
402 long line, col;
403 off_t new_offset;
405 canon_fname = vfs_canon (view->filename);
406 load_file_position (canon_fname, &line, &col, &new_offset, &view->saved_bookmarks);
407 new_offset = min (new_offset, mcview_get_filesize (view));
408 view->dpy_start = mcview_bol (view, new_offset, 0);
409 g_free (canon_fname);
411 else if (start_line > 0)
412 mcview_moveto (view, start_line - 1, 0);
414 view->hexedit_lownibble = FALSE;
415 view->hexview_in_text = FALSE;
416 view->change_list = NULL;
417 return retval;
420 /* --------------------------------------------------------------------------------------------- */