Manual pages: mcmcdiffmceditmcview

root/src/viewer/nroff.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. mcview_nroff_get_char
  2. mcview__get_nroff_real_len
  3. mcview_nroff_seq_new_num
  4. mcview_nroff_seq_new
  5. mcview_nroff_seq_free
  6. mcview_nroff_seq_info
  7. mcview_nroff_seq_next
  8. mcview_nroff_seq_prev

   1 /*
   2    Internal file viewer for the Midnight Commander
   3    Functions for searching in nroff-like view
   4 
   5    Copyright (C) 1994-2025
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Miguel de Icaza, 1994, 1995, 1998
  10    Janne Kukonlehto, 1994, 1995
  11    Jakub Jelinek, 1995
  12    Joseph M. Hinkle, 1996
  13    Norbert Warmuth, 1997
  14    Pavel Machek, 1998
  15    Roland Illig <roland.illig@gmx.de>, 2004, 2005
  16    Slava Zanko <slavazanko@google.com>, 2009
  17    Andrew Borodin <aborodin@vmail.ru>, 2009
  18    Ilia Maslakov <il.smind@gmail.com>, 2009
  19 
  20    This file is part of the Midnight Commander.
  21 
  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 3 of the License,
  25    or (at your option) any later version.
  26 
  27    The Midnight Commander is distributed in the hope that it will be useful,
  28    but WITHOUT ANY WARRANTY; without even the implied warranty of
  29    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  30    GNU General Public License for more details.
  31 
  32    You should have received a copy of the GNU General Public License
  33    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  34  */
  35 
  36 #include <config.h>
  37 
  38 #include "lib/global.h"
  39 #include "lib/tty/tty.h"
  40 #include "lib/skin.h"
  41 #include "lib/charsets.h"
  42 
  43 #include "internal.h"
  44 
  45 /*** global variables ****************************************************************************/
  46 
  47 /*** file scope macro definitions ****************************************************************/
  48 
  49 /*** file scope type declarations ****************************************************************/
  50 
  51 /*** forward declarations (file scope functions) *************************************************/
  52 
  53 /*** file scope variables ************************************************************************/
  54 
  55 /* --------------------------------------------------------------------------------------------- */
  56 /*** file scope functions ************************************************************************/
  57 /* --------------------------------------------------------------------------------------------- */
  58 
  59 static gboolean
  60 mcview_nroff_get_char (mcview_nroff_t *nroff, int *ret_val, off_t nroff_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
  61 {
  62     int c = 0;
  63 
  64     if (nroff->view->utf8)
  65     {
  66         if (!mcview_get_utf (nroff->view, nroff_index, &c, &nroff->char_length))
  67         {
  68             // we need got symbol in any case
  69             nroff->char_length = 1;
  70             if (!mcview_get_byte (nroff->view, nroff_index, &c) || !g_ascii_isprint (c))
  71                 return FALSE;
  72         }
  73     }
  74     else
  75     {
  76         nroff->char_length = 1;
  77         if (!mcview_get_byte (nroff->view, nroff_index, &c))
  78             return FALSE;
  79     }
  80 
  81     *ret_val = c;
  82 
  83     return g_unichar_isprint (c);
  84 }
  85 
  86 /* --------------------------------------------------------------------------------------------- */
  87 /*** public functions ****************************************************************************/
  88 /* --------------------------------------------------------------------------------------------- */
  89 
  90 int
  91 mcview__get_nroff_real_len (WView *view, off_t start, off_t length)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     mcview_nroff_t *nroff;
  94     int ret = 0;
  95     off_t i = 0;
  96 
  97     if (!view->mode_flags.nroff)
  98         return 0;
  99 
 100     nroff = mcview_nroff_seq_new_num (view, start);
 101     if (nroff == NULL)
 102         return 0;
 103     while (i < length)
 104     {
 105         switch (nroff->type)
 106         {
 107         case NROFF_TYPE_BOLD:
 108             ret += 1 + nroff->char_length;  // real char length and 0x8
 109             break;
 110         case NROFF_TYPE_UNDERLINE:
 111             ret += 2;  // underline symbol and ox8
 112             break;
 113         default:
 114             break;
 115         }
 116         i += nroff->char_length;
 117         mcview_nroff_seq_next (nroff);
 118     }
 119 
 120     mcview_nroff_seq_free (&nroff);
 121     return ret;
 122 }
 123 
 124 /* --------------------------------------------------------------------------------------------- */
 125 
 126 mcview_nroff_t *
 127 mcview_nroff_seq_new_num (WView *view, off_t lc_index)
     /* [previous][next][first][last][top][bottom][index][help]  */
 128 {
 129     mcview_nroff_t *nroff;
 130 
 131     nroff = g_try_malloc0 (sizeof (mcview_nroff_t));
 132     if (nroff != NULL)
 133     {
 134         nroff->index = lc_index;
 135         nroff->view = view;
 136         mcview_nroff_seq_info (nroff);
 137     }
 138     return nroff;
 139 }
 140 
 141 /* --------------------------------------------------------------------------------------------- */
 142 
 143 mcview_nroff_t *
 144 mcview_nroff_seq_new (WView *view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 145 {
 146     return mcview_nroff_seq_new_num (view, (off_t) 0);
 147 }
 148 
 149 /* --------------------------------------------------------------------------------------------- */
 150 
 151 void
 152 mcview_nroff_seq_free (mcview_nroff_t **nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 153 {
 154     if (nroff == NULL || *nroff == NULL)
 155         return;
 156     MC_PTR_FREE (*nroff);
 157 }
 158 
 159 /* --------------------------------------------------------------------------------------------- */
 160 
 161 nroff_type_t
 162 mcview_nroff_seq_info (mcview_nroff_t *nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 163 {
 164     int next, next2;
 165 
 166     if (nroff == NULL)
 167         return NROFF_TYPE_NONE;
 168     nroff->type = NROFF_TYPE_NONE;
 169 
 170     if (!mcview_nroff_get_char (nroff, &nroff->current_char, nroff->index))
 171         return nroff->type;
 172 
 173     if (!mcview_get_byte (nroff->view, nroff->index + nroff->char_length, &next) || next != '\b')
 174         return nroff->type;
 175 
 176     if (!mcview_nroff_get_char (nroff, &next2, nroff->index + 1 + nroff->char_length))
 177         return nroff->type;
 178 
 179     if (nroff->current_char == '_' && next2 == '_')
 180     {
 181         nroff->type =
 182             (nroff->prev_type == NROFF_TYPE_BOLD) ? NROFF_TYPE_BOLD : NROFF_TYPE_UNDERLINE;
 183     }
 184     else if (nroff->current_char == next2)
 185     {
 186         nroff->type = NROFF_TYPE_BOLD;
 187     }
 188     else if (nroff->current_char == '_')
 189     {
 190         nroff->current_char = next2;
 191         nroff->type = NROFF_TYPE_UNDERLINE;
 192     }
 193     else if (nroff->current_char == '+' && next2 == 'o')
 194     {
 195         // ???
 196     }
 197     return nroff->type;
 198 }
 199 
 200 /* --------------------------------------------------------------------------------------------- */
 201 
 202 int
 203 mcview_nroff_seq_next (mcview_nroff_t *nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 204 {
 205     if (nroff == NULL)
 206         return -1;
 207 
 208     nroff->prev_type = nroff->type;
 209 
 210     switch (nroff->type)
 211     {
 212     case NROFF_TYPE_BOLD:
 213         nroff->index += 1 + nroff->char_length;
 214         break;
 215     case NROFF_TYPE_UNDERLINE:
 216         nroff->index += 2;
 217         break;
 218     default:
 219         break;
 220     }
 221 
 222     nroff->index += nroff->char_length;
 223 
 224     mcview_nroff_seq_info (nroff);
 225     return nroff->current_char;
 226 }
 227 
 228 /* --------------------------------------------------------------------------------------------- */
 229 
 230 int
 231 mcview_nroff_seq_prev (mcview_nroff_t *nroff)
     /* [previous][next][first][last][top][bottom][index][help]  */
 232 {
 233     int prev;
 234     off_t prev_index, prev_index2;
 235 
 236     if (nroff == NULL)
 237         return -1;
 238 
 239     nroff->prev_type = NROFF_TYPE_NONE;
 240 
 241     if (nroff->index == 0)
 242         return -1;
 243 
 244     prev_index = nroff->index - 1;
 245 
 246     while (prev_index != 0)
 247     {
 248         if (mcview_nroff_get_char (nroff, &nroff->current_char, prev_index))
 249             break;
 250         prev_index--;
 251     }
 252     if (prev_index == 0)
 253     {
 254         nroff->index--;
 255         mcview_nroff_seq_info (nroff);
 256         return nroff->current_char;
 257     }
 258 
 259     prev_index--;
 260 
 261     if (!mcview_get_byte (nroff->view, prev_index, &prev) || prev != '\b')
 262     {
 263         nroff->index = prev_index;
 264         mcview_nroff_seq_info (nroff);
 265         return nroff->current_char;
 266     }
 267     prev_index2 = prev_index - 1;
 268 
 269     while (prev_index2 != 0)
 270     {
 271         if (mcview_nroff_get_char (nroff, &prev, prev_index))
 272             break;
 273         prev_index2--;
 274     }
 275 
 276     nroff->index = (prev_index2 == 0) ? prev_index : prev_index2;
 277     mcview_nroff_seq_info (nroff);
 278     return nroff->current_char;
 279 }
 280 
 281 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */