This source file includes following definitions.
- mcview_toggle_magic_mode
- mcview_toggle_wrap_mode
- mcview_toggle_nroff_mode
- mcview_toggle_hex_mode
- mcview_init
- mcview_done
- mcview_set_codeset
- mcview_select_encoding
- mcview_show_error
- mcview_bol
- mcview_eol
- mcview_get_title
- mcview_calc_percent
- mcview_clear_mode_flags
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36 #include <config.h>
37
38 #include <string.h>
39 #include <sys/types.h>
40
41 #include "lib/global.h"
42 #include "lib/vfs/vfs.h"
43 #include "lib/strutil.h"
44 #include "lib/util.h"
45 #include "lib/widget.h"
46 #ifdef HAVE_CHARSET
47 #include "lib/charsets.h"
48 #endif
49
50 #ifdef HAVE_CHARSET
51 #include "src/selcodepage.h"
52 #endif
53
54 #include "internal.h"
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71 void
72 mcview_toggle_magic_mode (WView *view)
73 {
74 char *filename, *command;
75 dir_list *dir;
76 int *dir_idx;
77
78 mcview_altered_flags.magic = TRUE;
79 view->mode_flags.magic = !view->mode_flags.magic;
80
81
82 filename = g_strdup (vfs_path_as_str (view->filename_vpath));
83 command = g_strdup (view->command);
84 dir = view->dir;
85 dir_idx = view->dir_idx;
86 view->dir = NULL;
87 view->dir_idx = NULL;
88 mcview_done (view);
89 mcview_init (view);
90 mcview_load (view, command, filename, 0, 0, 0);
91 view->dir = dir;
92 view->dir_idx = dir_idx;
93 g_free (filename);
94 g_free (command);
95
96 view->dpy_bbar_dirty = TRUE;
97 view->dirty++;
98 }
99
100
101
102 void
103 mcview_toggle_wrap_mode (WView *view)
104 {
105 view->mode_flags.wrap = !view->mode_flags.wrap;
106 view->dpy_wrap_dirty = TRUE;
107 view->dpy_bbar_dirty = TRUE;
108 view->dirty++;
109 }
110
111
112
113 void
114 mcview_toggle_nroff_mode (WView *view)
115 {
116 view->mode_flags.nroff = !view->mode_flags.nroff;
117 mcview_altered_flags.nroff = TRUE;
118 view->dpy_wrap_dirty = TRUE;
119 view->dpy_bbar_dirty = TRUE;
120 view->dirty++;
121 }
122
123
124
125 void
126 mcview_toggle_hex_mode (WView *view)
127 {
128 view->mode_flags.hex = !view->mode_flags.hex;
129
130 if (view->mode_flags.hex)
131 {
132 view->hex_cursor = view->dpy_start;
133 view->dpy_start = mcview_offset_rounddown (view->dpy_start, view->bytes_per_line);
134 widget_want_cursor (WIDGET (view), TRUE);
135 }
136 else
137 {
138 view->dpy_start = mcview_bol (view, view->hex_cursor, 0);
139 view->hex_cursor = view->dpy_start;
140 widget_want_cursor (WIDGET (view), FALSE);
141 }
142 mcview_altered_flags.hex = TRUE;
143 view->dpy_paragraph_skip_lines = 0;
144 view->dpy_wrap_dirty = TRUE;
145 view->dpy_bbar_dirty = TRUE;
146 view->dirty++;
147 }
148
149
150
151 void
152 mcview_init (WView *view)
153 {
154 size_t i;
155
156 view->filename_vpath = NULL;
157 view->workdir_vpath = NULL;
158 view->command = NULL;
159 view->search_nroff_seq = NULL;
160
161 mcview_set_datasource_none (view);
162
163 view->growbuf_in_use = FALSE;
164
165
166 view->hexedit_lownibble = FALSE;
167 view->locked = FALSE;
168 view->coord_cache = NULL;
169
170 view->dpy_start = 0;
171 view->dpy_paragraph_skip_lines = 0;
172 mcview_state_machine_init (&view->dpy_state_top, 0);
173 view->dpy_wrap_dirty = FALSE;
174 view->force_max = -1;
175 view->dpy_text_column = 0;
176 view->dpy_end = 0;
177 view->hex_cursor = 0;
178 view->cursor_col = 0;
179 view->cursor_row = 0;
180 view->change_list = NULL;
181
182
183
184 view->dirty = 0;
185 view->dpy_bbar_dirty = TRUE;
186 view->bytes_per_line = 1;
187
188 view->search_start = 0;
189 view->search_end = 0;
190
191 view->marker = 0;
192 for (i = 0; i < G_N_ELEMENTS (view->marks); i++)
193 view->marks[i] = 0;
194
195 view->update_steps = 0;
196 view->update_activate = 0;
197
198 view->saved_bookmarks = NULL;
199 }
200
201
202
203 void
204 mcview_done (WView *view)
205 {
206
207 if (mcview_remember_file_position && view->filename_vpath != NULL)
208 {
209 save_file_position (view->filename_vpath, -1, 0,
210 view->mode_flags.hex ? view->hex_cursor : view->dpy_start,
211 view->saved_bookmarks);
212 view->saved_bookmarks = NULL;
213 }
214
215
216 mcview_global_flags = view->mode_flags;
217
218
219
220 vfs_path_free (view->filename_vpath, TRUE);
221 view->filename_vpath = NULL;
222 vfs_path_free (view->workdir_vpath, TRUE);
223 view->workdir_vpath = NULL;
224 MC_PTR_FREE (view->command);
225
226 mcview_close_datasource (view);
227
228
229 if (view->coord_cache != NULL)
230 {
231 g_ptr_array_free (view->coord_cache, TRUE);
232 view->coord_cache = NULL;
233 }
234
235 if (view->converter == INVALID_CONV)
236 view->converter = str_cnv_from_term;
237
238 if (view->converter != str_cnv_from_term)
239 {
240 str_close_conv (view->converter);
241 view->converter = str_cnv_from_term;
242 }
243
244 mcview_search_deinit (view);
245 view->search = NULL;
246 view->last_search_string = NULL;
247 mcview_hexedit_free_change_list (view);
248
249 if (mc_global.mc_run_mode == MC_RUN_VIEWER && view->dir != NULL)
250 {
251
252 dir_list_free_list (view->dir);
253 g_free (view->dir);
254 g_free (view->dir_idx);
255 }
256
257 view->dir = NULL;
258 }
259
260
261
262 #ifdef HAVE_CHARSET
263 void
264 mcview_set_codeset (WView *view)
265 {
266 const char *cp_id = NULL;
267
268 view->utf8 = TRUE;
269 cp_id =
270 get_codepage_id (mc_global.source_codepage >=
271 0 ? mc_global.source_codepage : mc_global.display_codepage);
272 if (cp_id != NULL)
273 {
274 GIConv conv;
275 conv = str_crt_conv_from (cp_id);
276 if (conv != INVALID_CONV)
277 {
278 if (view->converter != str_cnv_from_term)
279 str_close_conv (view->converter);
280 view->converter = conv;
281 }
282 view->utf8 = (gboolean) str_isutf8 (cp_id);
283 view->dpy_wrap_dirty = TRUE;
284 }
285 }
286
287
288
289 void
290 mcview_select_encoding (WView *view)
291 {
292 if (do_select_codepage ())
293 mcview_set_codeset (view);
294 }
295 #endif
296
297
298
299 void
300 mcview_show_error (WView *view, const char *msg)
301 {
302 if (mcview_is_in_panel (view))
303 mcview_set_datasource_string (view, msg);
304 else
305 message (D_ERROR, MSG_ERROR, "%s", msg);
306 }
307
308
309
310
311
312
313 off_t
314 mcview_bol (WView *view, off_t current, off_t limit)
315 {
316 int c;
317 off_t filesize;
318 filesize = mcview_get_filesize (view);
319 if (current <= 0)
320 return 0;
321 if (current > filesize)
322 return filesize;
323 if (!mcview_get_byte (view, current, &c))
324 return current;
325 if (c == '\n')
326 {
327 if (!mcview_get_byte (view, current - 1, &c))
328 return current;
329 if (c == '\r')
330 current--;
331 }
332 while (current > 0 && current > limit)
333 {
334 if (!mcview_get_byte (view, current - 1, &c))
335 break;
336 if (c == '\r' || c == '\n')
337 break;
338 current--;
339 }
340 return current;
341 }
342
343
344
345
346
347
348 off_t
349 mcview_eol (WView *view, off_t current)
350 {
351 int c, prev_ch = 0;
352
353 if (current < 0)
354 return 0;
355
356 while (TRUE)
357 {
358 if (!mcview_get_byte (view, current, &c))
359 break;
360 if (c == '\n')
361 {
362 current++;
363 break;
364 }
365 else if (prev_ch == '\r')
366 {
367 break;
368 }
369 current++;
370 prev_ch = c;
371 }
372 return current;
373 }
374
375
376
377 char *
378 mcview_get_title (const WDialog *h, size_t len)
379 {
380 const WView *view;
381 const char *modified;
382 const char *file_label;
383 const char *view_filename;
384 char *ret_str;
385
386 view = (const WView *) widget_find_by_type (CONST_WIDGET (h), mcview_callback);
387 modified = view->hexedit_mode && (view->change_list != NULL) ? "(*) " : " ";
388 view_filename = vfs_path_as_str (view->filename_vpath);
389
390 len -= 4;
391
392 file_label = view_filename != NULL ? view_filename : view->command != NULL ? view->command : "";
393 file_label = str_term_trim (file_label, len - str_term_width1 (_("View: ")));
394
395 ret_str = g_strconcat (_("View: "), modified, file_label, (char *) NULL);
396 return ret_str;
397 }
398
399
400
401 int
402 mcview_calc_percent (WView *view, off_t p)
403 {
404 off_t filesize;
405 int percent;
406
407 if (view->status_area.cols < 1 || (view->status_area.x + view->status_area.cols) < 4)
408 return (-1);
409 if (mcview_may_still_grow (view))
410 return (-1);
411
412 filesize = mcview_get_filesize (view);
413 if (view->mode_flags.hex && filesize > 0)
414 {
415
416 filesize--;
417 }
418
419 if (filesize == 0 || p >= filesize)
420 percent = 100;
421 else if (p > (INT_MAX / 100))
422 percent = p / (filesize / 100);
423 else
424 percent = p * 100 / filesize;
425
426 return percent;
427 }
428
429
430
431 void
432 mcview_clear_mode_flags (mcview_mode_flags_t *flags)
433 {
434 memset (flags, 0, sizeof (*flags));
435 }
436
437