root/src/viewer/move.c

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

DEFINITIONS

This source file includes following definitions.
  1. mcview_scroll_to_cursor
  2. mcview_movement_fixups
  3. mcview_move_up
  4. mcview_move_down
  5. mcview_move_left
  6. mcview_move_right
  7. mcview_moveto_top
  8. mcview_moveto_bottom
  9. mcview_moveto_bol
  10. mcview_moveto_eol
  11. mcview_moveto_offset
  12. mcview_moveto
  13. mcview_coord_to_offset
  14. mcview_offset_to_coord
  15. mcview_place_cursor
  16. mcview_moveto_match

   1 /*
   2    Internal file viewer for the Midnight Commander
   3    Functions for handle cursor movement
   4 
   5    Copyright (C) 1994-2022
   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-2022
  18    Ilia Maslakov <il.smind@gmail.com>, 2009, 2010
  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 <http://www.gnu.org/licenses/>.
  34  */
  35 
  36 /*
  37    The following variables have to do with the current position and are
  38    updated by the cursor movement functions.
  39 
  40    In hex view and wrapped text view mode, dpy_start marks the offset of
  41    the top-left corner on the screen, in non-wrapping text mode it is
  42    the beginning of the current line.  In hex mode, hex_cursor is the
  43    offset of the cursor.  In non-wrapping text mode, dpy_text_column is
  44    the number of columns that are hidden on the left side on the screen.
  45 
  46    In hex mode, dpy_start is updated by the view_fix_cursor_position()
  47    function in order to keep the other functions simple.  In
  48    non-wrapping text mode dpy_start and dpy_text_column are normalized
  49    such that dpy_text_column < view_get_datacolumns().
  50  */
  51 
  52 #include <config.h>
  53 
  54 #include "lib/global.h"
  55 #include "lib/tty/tty.h"
  56 #include "internal.h"
  57 
  58 /*** global variables ****************************************************************************/
  59 
  60 /*** file scope macro definitions ****************************************************************/
  61 
  62 /*** file scope type declarations ****************************************************************/
  63 
  64 /*** file scope variables ************************************************************************/
  65 
  66 /* --------------------------------------------------------------------------------------------- */
  67 /*** file scope functions ************************************************************************/
  68 /* --------------------------------------------------------------------------------------------- */
  69 
  70 static void
  71 mcview_scroll_to_cursor (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
  72 {
  73     if (view->mode_flags.hex)
  74     {
  75         off_t bytes = view->bytes_per_line;
  76         off_t cursor = view->hex_cursor;
  77         off_t topleft = view->dpy_start;
  78         off_t displaysize;
  79 
  80         displaysize = view->data_area.lines * bytes;
  81         if (topleft + displaysize <= cursor)
  82             topleft = mcview_offset_rounddown (cursor, bytes) - (displaysize - bytes);
  83         if (cursor < topleft)
  84             topleft = mcview_offset_rounddown (cursor, bytes);
  85         view->dpy_start = topleft;
  86         view->dpy_paragraph_skip_lines = 0;
  87         view->dpy_wrap_dirty = TRUE;
  88     }
  89 }
  90 
  91 /* --------------------------------------------------------------------------------------------- */
  92 
  93 static void
  94 mcview_movement_fixups (WView * view, gboolean reset_search)
     /* [previous][next][first][last][top][bottom][index][help]  */
  95 {
  96     mcview_scroll_to_cursor (view);
  97     if (reset_search)
  98     {
  99         view->search_start = view->mode_flags.hex ? view->hex_cursor : view->dpy_start;
 100         view->search_end = view->search_start;
 101     }
 102     view->dirty++;
 103 }
 104 
 105 /* --------------------------------------------------------------------------------------------- */
 106 /*** public functions ****************************************************************************/
 107 /* --------------------------------------------------------------------------------------------- */
 108 
 109 void
 110 mcview_move_up (WView * view, off_t lines)
     /* [previous][next][first][last][top][bottom][index][help]  */
 111 {
 112     if (view->mode_flags.hex)
 113     {
 114         off_t bytes = lines * view->bytes_per_line;
 115 
 116         if (view->hex_cursor >= bytes)
 117         {
 118             view->hex_cursor -= bytes;
 119             if (view->hex_cursor < view->dpy_start)
 120             {
 121                 view->dpy_start = DOZ (view->dpy_start, bytes);
 122                 view->dpy_paragraph_skip_lines = 0;
 123                 view->dpy_wrap_dirty = TRUE;
 124             }
 125         }
 126         else
 127         {
 128             view->hex_cursor %= view->bytes_per_line;
 129         }
 130     }
 131     else
 132     {
 133         mcview_ascii_move_up (view, lines);
 134     }
 135     mcview_movement_fixups (view, TRUE);
 136 }
 137 
 138 /* --------------------------------------------------------------------------------------------- */
 139 
 140 void
 141 mcview_move_down (WView * view, off_t lines)
     /* [previous][next][first][last][top][bottom][index][help]  */
 142 {
 143     off_t last_byte;
 144 
 145     last_byte = mcview_get_filesize (view);
 146 
 147     if (view->mode_flags.hex)
 148     {
 149         off_t i, limit;
 150 
 151         limit = DOZ (last_byte, (off_t) view->bytes_per_line);
 152 
 153         for (i = 0; i < lines && view->hex_cursor < limit; i++)
 154         {
 155             view->hex_cursor += view->bytes_per_line;
 156             if (lines != 1)
 157             {
 158                 view->dpy_start += view->bytes_per_line;
 159                 view->dpy_paragraph_skip_lines = 0;
 160                 view->dpy_wrap_dirty = TRUE;
 161             }
 162         }
 163     }
 164     else
 165     {
 166         mcview_ascii_move_down (view, lines);
 167     }
 168     mcview_movement_fixups (view, TRUE);
 169 }
 170 
 171 /* --------------------------------------------------------------------------------------------- */
 172 
 173 void
 174 mcview_move_left (WView * view, off_t columns)
     /* [previous][next][first][last][top][bottom][index][help]  */
 175 {
 176     if (view->mode_flags.hex)
 177     {
 178         off_t old_cursor = view->hex_cursor;
 179 
 180         g_assert (columns == 1);
 181 
 182         if (view->hexview_in_text || !view->hexedit_lownibble)
 183         {
 184             if (view->hex_cursor > 0)
 185                 view->hex_cursor--;
 186         }
 187         if (!view->hexview_in_text)
 188             if (old_cursor > 0 || view->hexedit_lownibble)
 189                 view->hexedit_lownibble = !view->hexedit_lownibble;
 190     }
 191     else if (!view->mode_flags.wrap)
 192         view->dpy_text_column = DOZ (view->dpy_text_column, columns);
 193     mcview_movement_fixups (view, FALSE);
 194 }
 195 
 196 /* --------------------------------------------------------------------------------------------- */
 197 
 198 void
 199 mcview_move_right (WView * view, off_t columns)
     /* [previous][next][first][last][top][bottom][index][help]  */
 200 {
 201     if (view->mode_flags.hex)
 202     {
 203         off_t last_byte;
 204         off_t old_cursor = view->hex_cursor;
 205 
 206         last_byte = mcview_get_filesize (view);
 207         last_byte = DOZ (last_byte, 1);
 208 
 209         g_assert (columns == 1);
 210 
 211         if (view->hexview_in_text || view->hexedit_lownibble)
 212         {
 213             if (view->hex_cursor < last_byte)
 214                 view->hex_cursor++;
 215         }
 216         if (!view->hexview_in_text)
 217             if (old_cursor < last_byte || !view->hexedit_lownibble)
 218                 view->hexedit_lownibble = !view->hexedit_lownibble;
 219     }
 220     else if (!view->mode_flags.wrap)
 221     {
 222         view->dpy_text_column += columns;
 223     }
 224     mcview_movement_fixups (view, FALSE);
 225 }
 226 
 227 /* --------------------------------------------------------------------------------------------- */
 228 
 229 void
 230 mcview_moveto_top (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 231 {
 232     view->dpy_start = 0;
 233     view->dpy_paragraph_skip_lines = 0;
 234     mcview_state_machine_init (&view->dpy_state_top, 0);
 235     view->hex_cursor = 0;
 236     view->dpy_text_column = 0;
 237     mcview_movement_fixups (view, TRUE);
 238 }
 239 
 240 /* --------------------------------------------------------------------------------------------- */
 241 
 242 void
 243 mcview_moveto_bottom (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 244 {
 245     off_t filesize;
 246 
 247     mcview_update_filesize (view);
 248 
 249     if (view->growbuf_in_use)
 250         mcview_growbuf_read_all_data (view);
 251 
 252     filesize = mcview_get_filesize (view);
 253 
 254     if (view->mode_flags.hex)
 255     {
 256         view->hex_cursor = DOZ (filesize, 1);
 257         mcview_movement_fixups (view, TRUE);
 258     }
 259     else
 260     {
 261         view->dpy_start = filesize;
 262         view->dpy_paragraph_skip_lines = 0;
 263         view->dpy_wrap_dirty = TRUE;
 264         mcview_move_up (view, view->data_area.lines);
 265     }
 266 }
 267 
 268 /* --------------------------------------------------------------------------------------------- */
 269 
 270 void
 271 mcview_moveto_bol (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 272 {
 273     if (view->mode_flags.hex)
 274     {
 275         view->hex_cursor -= view->hex_cursor % view->bytes_per_line;
 276         view->dpy_text_column = 0;
 277     }
 278     else
 279     {
 280         mcview_ascii_moveto_bol (view);
 281     }
 282     mcview_movement_fixups (view, TRUE);
 283 }
 284 
 285 /* --------------------------------------------------------------------------------------------- */
 286 
 287 void
 288 mcview_moveto_eol (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 289 {
 290     off_t bol;
 291 
 292     if (view->mode_flags.hex)
 293     {
 294         off_t filesize;
 295 
 296         bol = mcview_offset_rounddown (view->hex_cursor, view->bytes_per_line);
 297         if (mcview_get_byte_indexed (view, bol, view->bytes_per_line - 1, NULL) == TRUE)
 298         {
 299             view->hex_cursor = bol + view->bytes_per_line - 1;
 300         }
 301         else
 302         {
 303             filesize = mcview_get_filesize (view);
 304             view->hex_cursor = DOZ (filesize, 1);
 305         }
 306     }
 307     else
 308     {
 309         mcview_ascii_moveto_eol (view);
 310     }
 311     mcview_movement_fixups (view, FALSE);
 312 }
 313 
 314 /* --------------------------------------------------------------------------------------------- */
 315 
 316 void
 317 mcview_moveto_offset (WView * view, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 318 {
 319     if (view->mode_flags.hex)
 320     {
 321         view->hex_cursor = offset;
 322         view->dpy_start = offset - offset % view->bytes_per_line;
 323         view->dpy_paragraph_skip_lines = 0;
 324         view->dpy_wrap_dirty = TRUE;
 325     }
 326     else
 327     {
 328         view->dpy_start = offset;
 329         view->dpy_paragraph_skip_lines = 0;
 330         view->dpy_wrap_dirty = TRUE;
 331     }
 332     mcview_movement_fixups (view, TRUE);
 333 }
 334 
 335 /* --------------------------------------------------------------------------------------------- */
 336 
 337 void
 338 mcview_moveto (WView * view, off_t line, off_t col)
     /* [previous][next][first][last][top][bottom][index][help]  */
 339 {
 340     off_t offset;
 341 
 342     mcview_coord_to_offset (view, &offset, line, col);
 343     mcview_moveto_offset (view, offset);
 344 }
 345 
 346 /* --------------------------------------------------------------------------------------------- */
 347 
 348 void
 349 mcview_coord_to_offset (WView * view, off_t * ret_offset, off_t line, off_t column)
     /* [previous][next][first][last][top][bottom][index][help]  */
 350 {
 351     coord_cache_entry_t coord;
 352 
 353     coord.cc_line = line;
 354     coord.cc_column = column;
 355     coord.cc_nroff_column = column;
 356     mcview_ccache_lookup (view, &coord, CCACHE_OFFSET);
 357     *ret_offset = coord.cc_offset;
 358 }
 359 
 360 /* --------------------------------------------------------------------------------------------- */
 361 
 362 void
 363 mcview_offset_to_coord (WView * view, off_t * ret_line, off_t * ret_column, off_t offset)
     /* [previous][next][first][last][top][bottom][index][help]  */
 364 {
 365     coord_cache_entry_t coord;
 366 
 367     coord.cc_offset = offset;
 368     mcview_ccache_lookup (view, &coord, CCACHE_LINECOL);
 369 
 370     *ret_line = coord.cc_line;
 371     *ret_column = view->mode_flags.nroff ? coord.cc_nroff_column : coord.cc_column;
 372 }
 373 
 374 /* --------------------------------------------------------------------------------------------- */
 375 
 376 void
 377 mcview_place_cursor (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 378 {
 379     const WRect *r = &view->data_area;
 380     int col = view->cursor_col;
 381 
 382     if (!view->hexview_in_text && view->hexedit_lownibble)
 383         col++;
 384     widget_gotoyx (view, r->y + view->cursor_row, r->x + col);
 385 }
 386 
 387 /* --------------------------------------------------------------------------------------------- */
 388 /** we have set view->search_start and view->search_end and must set
 389  * view->dpy_text_column and view->dpy_start
 390  * try to display maximum of match */
 391 
 392 void
 393 mcview_moveto_match (WView * view)
     /* [previous][next][first][last][top][bottom][index][help]  */
 394 {
 395     if (view->mode_flags.hex)
 396     {
 397         view->hex_cursor = view->search_start;
 398         view->hexedit_lownibble = FALSE;
 399         view->dpy_start = view->search_start - view->search_start % view->bytes_per_line;
 400         view->dpy_end = view->search_end - view->search_end % view->bytes_per_line;
 401         view->dpy_paragraph_skip_lines = 0;
 402         view->dpy_wrap_dirty = TRUE;
 403     }
 404     else
 405     {
 406         view->dpy_start = mcview_bol (view, view->search_start, 0);
 407         view->dpy_paragraph_skip_lines = 0;
 408         view->dpy_wrap_dirty = TRUE;
 409     }
 410 
 411     mcview_scroll_to_cursor (view);
 412     view->dirty++;
 413 }
 414 
 415 /* --------------------------------------------------------------------------------------------- */

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