2 Internal file viewer for the Midnight Commander
3 Function for nroff-like view
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,
40 #include "lib/global.h"
41 #include "lib/tty/tty.h"
43 #include "lib/charsets.h"
45 #include "src/main.h" /* utf8_display */
46 #include "src/setup.h" /* option_tab_spacing */
50 /*** global variables ****************************************************************************/
52 /*** file scope macro definitions ****************************************************************/
54 /*** file scope type declarations ****************************************************************/
56 /*** file scope variables ************************************************************************/
58 /*** file scope functions ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
62 mcview_nroff_get_char (mcview_nroff_t
* nroff
, int *ret_val
, off_t nroff_index
)
66 if (nroff
->view
->utf8
)
69 c
= mcview_get_utf (nroff
->view
, nroff_index
, &nroff
->char_width
, &utf_result
);
72 /* we need got symbol in any case */
73 nroff
->char_width
= 1;
74 if (!mcview_get_byte (nroff
->view
, nroff_index
, &c
) || !g_ascii_isprint (c
))
81 nroff
->char_width
= 1;
82 if (!mcview_get_byte (nroff
->view
, nroff_index
, &c
))
88 return g_unichar_isprint (c
);
91 /* --------------------------------------------------------------------------------------------- */
92 /*** public functions ****************************************************************************/
93 /* --------------------------------------------------------------------------------------------- */
96 mcview_display_nroff (mcview_t
* view
)
98 const screen_dimen left
= view
->data_area
.left
;
99 const screen_dimen top
= view
->data_area
.top
;
100 const screen_dimen width
= view
->data_area
.width
;
101 const screen_dimen height
= view
->data_area
.height
;
102 screen_dimen row
, col
;
108 struct hexedit_change_node
*curr
= view
->change_list
;
110 mcview_display_clean (view
);
111 mcview_display_ruler (view
);
113 /* Find the first displayable changed byte */
114 from
= view
->dpy_start
;
115 while (curr
&& (curr
->offset
< from
))
120 tty_setcolor (NORMAL_COLOR
);
121 for (row
= 0, col
= 0; row
< height
;)
126 gboolean read_res
= TRUE
;
127 c
= mcview_get_utf (view
, from
, &cw
, &read_res
);
134 if (!mcview_get_byte (view
, from
, &c
))
149 c_next
= mcview_get_utf (view
, from
, &cw
, &read_res
);
153 mcview_get_byte (view
, from
, &c_next
);
155 if (g_unichar_isprint (c_prev
) && g_unichar_isprint (c_next
)
156 && (c_prev
== c_next
|| c_prev
== '_' || (c_prev
== '+' && c_next
== 'o')))
162 /* We're inside an nroff character sequence at the
163 * beginning of the screen -- just skip the
164 * backspace and continue with the next character. */
172 && (c_next
!= '_' || mcview_count_backspaces (view
, from
+ 1) == 1))
173 tty_setcolor (VIEW_UNDERLINED_COLOR
);
175 tty_setcolor (VIEW_BOLD_COLOR
);
180 if ((c
== '\n') || (col
>= width
&& view
->text_wrap_mode
))
184 if (c
== '\n' || row
>= height
)
190 mcview_get_byte_indexed (view
, from
, 1, &c
);
191 if (c
== '\r' || c
== '\n')
201 mcview_offset_to_coord (view
, &line
, &column
, from
);
202 col
+= (option_tab_spacing
- col
% option_tab_spacing
);
203 if (view
->text_wrap_mode
&& col
>= width
&& width
!= 0)
211 if (view
->search_start
<= from
&& from
< view
->search_end
)
213 tty_setcolor (SELECTED_COLOR
);
218 if ((off_t
) col
>= view
->dpy_text_column
219 && (off_t
) col
- view
->dpy_text_column
< (off_t
) width
)
221 widget_move (view
, top
+ row
, left
+ ((off_t
) col
- view
->dpy_text_column
));
227 c
= convert_from_8bit_to_utf_c ((unsigned char) c
, view
->converter
);
229 if (!g_unichar_isprint (c
))
236 c
= convert_from_utf_to_current_c (c
, view
->converter
);
241 c
= convert_to_display_c (c
);
246 tty_print_anychar (c
);
252 if (g_unichar_iswide (c
))
254 else if (g_unichar_iszerowidth (c
))
258 tty_setcolor (NORMAL_COLOR
);
260 view
->dpy_end
= from
;
263 /* --------------------------------------------------------------------------------------------- */
266 mcview__get_nroff_real_len (mcview_t
* view
, off_t start
, off_t length
)
268 mcview_nroff_t
*nroff
;
272 if (!view
->text_nroff_mode
)
275 nroff
= mcview_nroff_seq_new_num (view
, start
);
282 case NROFF_TYPE_BOLD
:
283 ret
+= 1 + nroff
->char_width
; /* real char width and 0x8 */
285 case NROFF_TYPE_UNDERLINE
:
286 ret
+= 2; /* underline symbol and ox8 */
291 i
+= nroff
->char_width
;
292 mcview_nroff_seq_next (nroff
);
295 mcview_nroff_seq_free (&nroff
);
299 /* --------------------------------------------------------------------------------------------- */
302 mcview_nroff_seq_new_num (mcview_t
* view
, off_t lc_index
)
304 mcview_nroff_t
*nroff
;
306 nroff
= g_try_malloc0 (sizeof (mcview_nroff_t
));
309 nroff
->index
= lc_index
;
311 mcview_nroff_seq_info (nroff
);
316 /* --------------------------------------------------------------------------------------------- */
319 mcview_nroff_seq_new (mcview_t
* view
)
321 return mcview_nroff_seq_new_num (view
, (off_t
) 0);
325 /* --------------------------------------------------------------------------------------------- */
328 mcview_nroff_seq_free (mcview_nroff_t
** nroff
)
330 if (nroff
== NULL
|| *nroff
== NULL
)
336 /* --------------------------------------------------------------------------------------------- */
339 mcview_nroff_seq_info (mcview_nroff_t
* nroff
)
344 return NROFF_TYPE_NONE
;
345 nroff
->type
= NROFF_TYPE_NONE
;
347 if (!mcview_nroff_get_char (nroff
, &nroff
->current_char
, nroff
->index
))
350 if (!mcview_get_byte (nroff
->view
, nroff
->index
+ nroff
->char_width
, &next
) || next
!= '\b')
353 if (!mcview_nroff_get_char (nroff
, &next2
, nroff
->index
+ 1 + nroff
->char_width
))
356 if (nroff
->current_char
== '_' && next2
== '_')
358 nroff
->type
= (nroff
->prev_type
== NROFF_TYPE_BOLD
)
359 ? NROFF_TYPE_BOLD
: NROFF_TYPE_UNDERLINE
;
362 else if (nroff
->current_char
== next2
)
364 nroff
->type
= NROFF_TYPE_BOLD
;
366 else if (nroff
->current_char
== '_')
368 nroff
->current_char
= next2
;
369 nroff
->type
= NROFF_TYPE_UNDERLINE
;
371 else if (nroff
->current_char
== '+' && next2
== 'o')
378 /* --------------------------------------------------------------------------------------------- */
381 mcview_nroff_seq_next (mcview_nroff_t
* nroff
)
386 nroff
->prev_type
= nroff
->type
;
390 case NROFF_TYPE_BOLD
:
391 nroff
->index
+= 1 + nroff
->char_width
;
393 case NROFF_TYPE_UNDERLINE
:
400 nroff
->index
+= nroff
->char_width
;
402 mcview_nroff_seq_info (nroff
);
403 return nroff
->current_char
;
406 /* --------------------------------------------------------------------------------------------- */