Ticket #1585: bol_eol.diff

File bol_eol.diff, 6.1 KB (added by himikof, 10 years ago)

Patch for mcview_bol/mcview_eol

  • src/viewer/internal.h

    diff --git a/src/viewer/internal.h b/src/viewer/internal.h
    index fdabc79..6c229df 100644
    a b void mcview_done (mcview_t *view); 
    274274void mcview_select_encoding (mcview_t *view); 
    275275void mcview_set_codeset (mcview_t *view); 
    276276void mcview_show_error (mcview_t *view, const char *error); 
    277 off_t mcview_bol (mcview_t *view, off_t current, int *eol_width); 
    278 off_t mcview_eol (mcview_t *view, off_t current, int *eol_width); 
     277off_t mcview_bol (mcview_t *view, off_t current); 
     278off_t mcview_eol (mcview_t *view, off_t current); 
    279279 
    280280/* move.c */ 
    281281void mcview_move_up (mcview_t *, off_t); 
  • src/viewer/lib.c

    diff --git a/src/viewer/lib.c b/src/viewer/lib.c
    index a01a8ff..142e24c 100644
    a b mcview_show_error (mcview_t * view, const char *msg) 
    244244 
    245245/* --------------------------------------------------------------------------------------------- */ 
    246246 
    247 /* returns index of first char on line */ 
     247/* returns index of the first char in the line */ 
     248/* it is constant for all line characters */ 
    248249off_t 
    249 mcview_bol (mcview_t * view, off_t current, int * eol_width) 
     250mcview_bol (mcview_t * view, off_t current) 
    250251{ 
    251     int c, prev_ch = 0; 
    252  
    253     *eol_width = 1; 
     252    int c; 
     253    off_t filesize; 
     254    filesize = mcview_get_filesize (view); 
     255    if (current <= 0) 
     256        return 0; 
     257    if (current > filesize) 
     258        return filesize; 
     259    if (!mcview_get_byte (view, current, &c)) 
     260        return current; 
     261    if (c == '\n') { 
     262        if (!mcview_get_byte (view, current - 1, &c)) 
     263            return current; 
     264        if (c == '\r') 
     265            current--; 
     266    } 
    254267    while (current > 0) { 
    255268        if (!mcview_get_byte (view, current - 1, &c)) 
    256269            break; 
    257         if (c == '\r') { 
    258             if (prev_ch == '\n') { 
    259                 /* Windows/DOS (CR LF) width of eol = 2 bytes */ 
    260                 if (current > 0) 
    261                     current++; 
    262                 *eol_width = 2; 
    263             } 
    264             /* Macintosh (CR) width of eol = 1 byte */ 
    265             break; 
    266         } else if (prev_ch == '\n') { 
    267             /* UNIX (LF)  width of eol = 1 byte */ 
    268             if (current > 0) 
    269                 current++; 
     270        if (c == '\r' || c == '\n') 
    270271            break; 
    271         } 
    272272        current--; 
    273         prev_ch = c; 
    274273    } 
    275     if (current < 0) 
    276         return 0; 
    277274    return current; 
    278275} 
    279276 
    280277/* returns index of last char on line + width EOL */ 
    281278/* mcview_eol of the current line == mcview_bol next line */ 
    282279off_t 
    283 mcview_eol (mcview_t * view, off_t current, int * eol_width) 
     280mcview_eol (mcview_t * view, off_t current) 
    284281{ 
    285282    int c, prev_ch = 0; 
    286283    off_t filesize; 
    287     *eol_width = 1; 
    288284    filesize = mcview_get_filesize (view); 
     285    if (current < 0) 
     286        return 0; 
     287    if (current >= filesize) 
     288        return filesize; 
    289289    while (current < filesize) { 
    290290        if (!mcview_get_byte (view, current, &c)) 
    291291            break; 
    292292        if (c == '\n') { 
    293             if (prev_ch == '\r') { 
    294                 /* Windows/DOS (CR LF) width of eol = 2 bytes */ 
    295                 *eol_width = 2; 
    296             } 
    297             /* Macintosh (CR) width of eol = 1 byte */ 
     293            current++; 
    298294            break; 
    299295        } else if (prev_ch == '\r') { 
    300             /* UNIX (LF)  width of eol = 1 byte */ 
    301296            break; 
    302297        } 
    303298        current++; 
    304299        prev_ch = c; 
    305300    } 
    306     current += *eol_width; 
    307     if (current > filesize) 
    308         return filesize; 
    309301    return current; 
    310302} 
  • src/viewer/mcviewer.c

    diff --git a/src/viewer/mcviewer.c b/src/viewer/mcviewer.c
    index b8fc167..a520d17 100644
    a b mcview_load (mcview_t * view, const char *command, const char *file, int start_l 
    381381    if (mcview_remember_file_position && view->filename != NULL) { 
    382382        long line, col; 
    383383        off_t new_offset; 
    384         int eol_len; 
    385384        canon_fname = vfs_canon (view->filename); 
    386385        load_file_position (canon_fname, &line, &col, &new_offset); 
    387386        new_offset = min (new_offset, mcview_get_filesize (view)); 
    388         view->dpy_start = mcview_bol (view, new_offset, &eol_len); 
     387        view->dpy_start = mcview_bol (view, new_offset); 
    389388        g_free (canon_fname); 
    390389    } 
    391390 
  • src/viewer/move.c

    diff --git a/src/viewer/move.c b/src/viewer/move.c
    index b4d0d5a..9e9e47e 100644
    a b mcview_move_up (mcview_t * view, off_t lines) 
    100100    } else { 
    101101        const screen_dimen width = view->data_area.width; 
    102102        off_t i; 
    103         int eol_len = 1; 
    104103        for (i = 0; i < lines; i++) { 
    105             new_offset = mcview_bol (view, view->dpy_start, &eol_len); 
     104            new_offset = mcview_bol (view, view->dpy_start); 
    106105            if (new_offset > 0) 
    107                 new_offset -= eol_len; 
    108                 new_offset = mcview_bol (view, new_offset, &eol_len); 
     106                new_offset--; 
     107            new_offset = mcview_bol (view, new_offset); 
    109108            if (new_offset < 0) 
    110109                new_offset = 0; 
    111110            view->dpy_start = new_offset; 
    mcview_move_down (mcview_t * view, off_t lines) 
    141140        int c; 
    142141        for (i = 0; i < lines; i++) { 
    143142            off_t new_offset; 
    144             int eol_len; 
    145             new_offset = mcview_eol(view, view->dpy_start, &eol_len); 
     143            new_offset = mcview_eol(view, view->dpy_start); 
    146144            view->dpy_start = new_offset; 
    147145        } 
    148146    } 
    mcview_moveto_bol (mcview_t * view) 
    258256    if (view->hex_mode) { 
    259257        view->hex_cursor -= view->hex_cursor % view->bytes_per_line; 
    260258    } else if (!view->text_wrap_mode) { 
    261         int eol_len; 
    262         view->dpy_start = mcview_bol (view, view->dpy_start, &eol_len); 
     259        view->dpy_start = mcview_bol (view, view->dpy_start); 
    263260    } 
    264261    mcview_movement_fixups (view, TRUE); 
    265262} 
    mcview_moveto_match (mcview_t * view) 
    366363        view->hex_cursor = offset; 
    367364        view->dpy_start = offset - offset % view->bytes_per_line; 
    368365    } else { 
    369         int eol_len; 
    370         view->dpy_start = mcview_bol (view, offset, &eol_len); 
     366        view->dpy_start = mcview_bol (view, offset); 
    371367    } 
    372368 
    373369    mcview_scroll_to_cursor (view);