Fixed search for first symbol in string.
[midnight-commander.git] / src / viewer / lib.c
bloba45c0353e9288052ee87cb7bef4deecbe714b41c
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>
41 #include <sys/types.h>
43 #include "lib/global.h"
44 #include "lib/vfs/vfs.h"
45 #include "lib/strutil.h"
46 #include "lib/util.h" /* save_file_position() */
47 #include "lib/lock.h" /* unlock_file() */
48 #include "lib/widget.h"
49 #include "lib/charsets.h"
51 #include "src/main.h"
52 #include "src/selcodepage.h"
54 #include "internal.h"
55 #include "mcviewer.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 /* --------------------------------------------------------------------------------------------- */
76 void
77 mcview_toggle_magic_mode (mcview_t * view)
79 char *filename, *command;
81 mcview_altered_magic_flag = 1;
82 view->magic_mode = !view->magic_mode;
83 filename = g_strdup (view->filename);
84 command = g_strdup (view->command);
86 mcview_done (view);
87 mcview_init (view);
88 mcview_load (view, command, filename, 0);
89 g_free (filename);
90 g_free (command);
91 view->dpy_bbar_dirty = TRUE;
92 view->dirty++;
95 /* --------------------------------------------------------------------------------------------- */
97 void
98 mcview_toggle_wrap_mode (mcview_t * view)
100 if (view->text_wrap_mode)
101 view->dpy_start = mcview_bol (view, view->dpy_start, 0);
102 view->text_wrap_mode = !view->text_wrap_mode;
103 view->dpy_bbar_dirty = TRUE;
104 view->dirty++;
107 /* --------------------------------------------------------------------------------------------- */
109 void
110 mcview_toggle_nroff_mode (mcview_t * view)
112 view->text_nroff_mode = !view->text_nroff_mode;
113 mcview_altered_nroff_flag = 1;
114 view->dpy_bbar_dirty = TRUE;
115 view->dirty++;
118 /* --------------------------------------------------------------------------------------------- */
120 void
121 mcview_toggle_hex_mode (mcview_t * view)
123 view->hex_mode = !view->hex_mode;
125 if (view->hex_mode)
127 view->hex_cursor = view->dpy_start;
128 view->dpy_start = mcview_offset_rounddown (view->dpy_start, view->bytes_per_line);
129 widget_want_cursor (view->widget, 1);
131 else
133 view->dpy_start = view->hex_cursor;
134 mcview_moveto_bol (view);
135 widget_want_cursor (view->widget, 0);
137 mcview_altered_hex_mode = 1;
138 view->dpy_bbar_dirty = TRUE;
139 view->dirty++;
142 /* --------------------------------------------------------------------------------------------- */
144 gboolean
145 mcview_ok_to_quit (mcview_t * view)
147 int r;
149 if (view->change_list == NULL)
150 return TRUE;
152 if (!mc_global.widget.midnight_shutdown)
154 query_set_sel (2);
155 r = query_dialog (_("Quit"),
156 _("File was modified. Save with exit?"), D_NORMAL, 3,
157 _("&Yes"), _("&No"), _("&Cancel quit"));
159 else
161 r = query_dialog (_("Quit"),
162 _("Midnight Commander is being shut down.\nSave modified file?"),
163 D_NORMAL, 2, _("&Yes"), _("&No"));
164 /* Esc is No */
165 if (r == -1)
166 r = 1;
169 switch (r)
171 case 0: /* Yes */
172 return mcview_hexedit_save_changes (view) || mc_global.widget.midnight_shutdown;
173 case 1: /* No */
174 mcview_hexedit_free_change_list (view);
175 return TRUE;
176 default:
177 return FALSE;
181 /* --------------------------------------------------------------------------------------------- */
183 void
184 mcview_init (mcview_t * view)
186 size_t i;
188 view->filename = NULL;
189 view->workdir = NULL;
190 view->command = NULL;
191 view->search_nroff_seq = NULL;
193 mcview_set_datasource_none (view);
195 view->growbuf_in_use = FALSE;
196 /* leave the other growbuf fields uninitialized */
198 view->hexedit_lownibble = FALSE;
199 view->locked = FALSE;
200 view->coord_cache = NULL;
202 view->dpy_start = 0;
203 view->dpy_text_column = 0;
204 view->dpy_end = 0;
205 view->hex_cursor = 0;
206 view->cursor_col = 0;
207 view->cursor_row = 0;
208 view->change_list = NULL;
210 /* {status,ruler,data}_area are left uninitialized */
212 view->dirty = 0;
213 view->dpy_bbar_dirty = TRUE;
214 view->bytes_per_line = 1;
216 view->search_start = 0;
217 view->search_end = 0;
219 view->marker = 0;
220 for (i = 0; i < sizeof (view->marks) / sizeof (view->marks[0]); i++)
221 view->marks[i] = 0;
223 view->move_dir = 0;
224 view->update_steps = 0;
225 view->update_activate = 0;
227 view->saved_bookmarks = NULL;
230 /* --------------------------------------------------------------------------------------------- */
232 void
233 mcview_done (mcview_t * view)
235 /* Save current file position */
236 if (mcview_remember_file_position && view->filename != NULL)
238 char *canon_fname;
239 vfs_path_t *vpath;
240 vpath = vfs_path_from_str (view->filename);
241 canon_fname = vfs_path_to_str (vpath);
242 save_file_position (canon_fname, -1, 0, view->dpy_start, view->saved_bookmarks);
243 view->saved_bookmarks = NULL;
244 g_free (canon_fname);
245 vfs_path_free (vpath);
248 /* Write back the global viewer mode */
249 mcview_default_hex_mode = view->hex_mode;
250 mcview_default_nroff_flag = view->text_nroff_mode;
251 mcview_default_magic_flag = view->magic_mode;
252 mcview_global_wrap_mode = view->text_wrap_mode;
254 /* Free memory used by the viewer */
256 /* view->widget needs no destructor */
258 g_free (view->filename);
259 view->filename = NULL;
260 g_free (view->workdir);
261 view->workdir = NULL;
262 g_free (view->command);
263 view->command = NULL;
265 mcview_close_datasource (view);
266 /* the growing buffer is freed with the datasource */
268 coord_cache_free (view->coord_cache), view->coord_cache = NULL;
270 if (view->converter == INVALID_CONV)
271 view->converter = str_cnv_from_term;
273 if (view->converter != str_cnv_from_term)
275 str_close_conv (view->converter);
276 view->converter = str_cnv_from_term;
279 mc_search_free (view->search);
280 view->search = NULL;
281 g_free (view->last_search_string);
282 view->last_search_string = NULL;
283 mcview_nroff_seq_free (&view->search_nroff_seq);
284 mcview_hexedit_free_change_list (view);
287 /* --------------------------------------------------------------------------------------------- */
289 void
290 mcview_set_codeset (mcview_t * view)
292 #ifdef HAVE_CHARSET
293 const char *cp_id = NULL;
295 view->utf8 = TRUE;
296 cp_id =
297 get_codepage_id (mc_global.source_codepage >=
298 0 ? mc_global.source_codepage : mc_global.display_codepage);
299 if (cp_id != NULL)
301 GIConv conv;
302 conv = str_crt_conv_from (cp_id);
303 if (conv != INVALID_CONV)
305 if (view->converter != str_cnv_from_term)
306 str_close_conv (view->converter);
307 view->converter = conv;
309 view->utf8 = (gboolean) str_isutf8 (cp_id);
311 #else
312 (void) view;
313 #endif
316 /* --------------------------------------------------------------------------------------------- */
318 void
319 mcview_select_encoding (mcview_t * view)
321 #ifdef HAVE_CHARSET
322 if (do_select_codepage ())
323 mcview_set_codeset (view);
324 #else
325 (void) view;
326 #endif
329 /* --------------------------------------------------------------------------------------------- */
331 void
332 mcview_show_error (mcview_t * view, const char *msg)
334 mcview_close_datasource (view);
335 if (mcview_is_in_panel (view))
337 mcview_set_datasource_string (view, msg);
339 else
341 message (D_ERROR, MSG_ERROR, "%s", msg);
345 /* --------------------------------------------------------------------------------------------- */
346 /** returns index of the first char in the line
347 * it is constant for all line characters
350 off_t
351 mcview_bol (mcview_t * view, off_t current, off_t limit)
353 int c;
354 off_t filesize;
355 filesize = mcview_get_filesize (view);
356 if (current <= 0)
357 return 0;
358 if (current > filesize)
359 return filesize;
360 if (!mcview_get_byte (view, current, &c))
361 return current;
362 if (c == '\n')
364 if (!mcview_get_byte (view, current - 1, &c))
365 return current;
366 if (c == '\r')
367 current--;
369 while (current > 0 && current >= limit)
371 if (!mcview_get_byte (view, current - 1, &c))
372 break;
373 if (c == '\r' || c == '\n')
374 break;
375 current--;
377 return current;
380 /* --------------------------------------------------------------------------------------------- */
381 /** returns index of last char on line + width EOL
382 * mcview_eol of the current line == mcview_bol next line
385 off_t
386 mcview_eol (mcview_t * view, off_t current, off_t limit)
388 int c, prev_ch = 0;
389 off_t filesize;
390 filesize = mcview_get_filesize (view);
391 if (current < 0)
392 return 0;
393 if (current >= filesize)
394 return filesize;
395 while (current < filesize && current < limit)
397 if (!mcview_get_byte (view, current, &c))
398 break;
399 if (c == '\n')
401 current++;
402 break;
404 else if (prev_ch == '\r')
406 break;
408 current++;
409 prev_ch = c;
411 return current;
414 /* --------------------------------------------------------------------------------------------- */
416 char *
417 mcview_get_title (const Dlg_head * h, size_t len)
419 const mcview_t *view = (const mcview_t *) find_widget_type (h, mcview_callback);
420 const char *modified = view->hexedit_mode && (view->change_list != NULL) ? "(*) " : " ";
421 const char *file_label;
423 len -= 4;
425 file_label = view->filename != NULL ? view->filename :
426 view->command != NULL ? view->command : "";
427 file_label = str_term_trim (file_label, len - str_term_width1 (_("View: ")));
429 return g_strconcat (_("View: "), modified, file_label, (char *) NULL);
432 /* --------------------------------------------------------------------------------------------- */
434 gboolean
435 mcview_lock_file (mcview_t * view)
437 char *fullpath;
438 gboolean ret;
440 fullpath = mc_build_filename (view->workdir, view->filename, (char *) NULL);
441 ret = lock_file (fullpath);
442 g_free (fullpath);
444 return ret;
447 /* --------------------------------------------------------------------------------------------- */
449 gboolean
450 mcview_unlock_file (mcview_t * view)
452 char *fullpath;
453 gboolean ret;
455 fullpath = mc_build_filename (view->workdir, view->filename, (char *) NULL);
456 ret = unlock_file (fullpath);
457 g_free (fullpath);
459 return ret;
462 /* --------------------------------------------------------------------------------------------- */