Ticket #1652: 0001-Ticket-1652.patch

File 0001-Ticket-1652.patch, 10.1 KB (added by angel_il, 13 years ago)
  • src/editor/edit-impl.h

    From 713e6056862cdfe9e8ec4275ae39d1abf25c36bf Mon Sep 17 00:00:00 2001
    From: Ilia Maslakov <il.smind@gmail.com>
    Date: Mon, 28 Mar 2011 12:42:53 +0000
    Subject: [PATCH 1/2] Ticket #1652 (autodetect line-endings)
    
        * On opening file, detect line-endings used by sampling some initial content.
        * If it happen to be CR or CRLF, skip fast load path, and in edit_insert_file() convert such line endings to \n.
        * Save detected line ending type for editor.
    Signed-off-by: Ilia Maslakov <il.smind@gmail.com>
    ---
     src/editor/edit-impl.h |    2 +-
     src/editor/edit.c      |   59 +++++++++++++++++++++++++++++++++++++++++++-----
     src/editor/editcmd.c   |   12 +++++----
     src/editor/editdraw.c  |   21 ++++++++++++-----
     4 files changed, 76 insertions(+), 18 deletions(-)
    
    diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h
    index d6bb43a..1b6ac87 100644
    a b void edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, in 
    290290                                 long *start_pos, long *end_pos, int *col1, int *col2); 
    291291int edit_insert_column_of_text_from_file (WEdit * edit, int file, 
    292292                                          long *start_pos, long *end_pos, int *col1, int *col2); 
    293 long edit_insert_file (WEdit * edit, const char *filename); 
     293long edit_insert_file (WEdit * edit, const char *filename, LineBreaks lb_type); 
    294294int edit_load_back_cmd (WEdit * edit); 
    295295int edit_load_forward_cmd (WEdit * edit); 
    296296void edit_block_process_cmd (WEdit * edit, int macro_number); 
  • src/editor/edit.c

    diff --git a/src/editor/edit.c b/src/editor/edit.c
    index 226151b..0d854f9 100644
    a b check_file_access (WEdit * edit, const char *filename, struct stat *st) 
    419419 
    420420/* --------------------------------------------------------------------------------------------- */ 
    421421/** 
     422 * detect type of line breaks of loaded file by lead BUF_MEDIUM bytes 
     423 * 
     424 */ 
     425static LineBreaks 
     426detect_lb_type (char *filename) 
     427{ 
     428    char buf[BUF_MEDIUM]; 
     429    ssize_t file, sz; 
     430 
     431    file = mc_open (filename, O_RDONLY | O_BINARY); 
     432    if (file == -1) 
     433        return LB_ASIS; 
     434 
     435    memset (buf, 0, sizeof (buf)); 
     436    sz = mc_read (file, buf, sizeof (buf) - 1); 
     437    if (sz < 0) 
     438        return LB_ASIS; 
     439    mc_close (file); 
     440 
     441    buf[(size_t) sz] = '\0'; 
     442    if (strstr (buf, "\r\n") != NULL) 
     443        return LB_WIN; 
     444    if (strchr (buf, '\r') != NULL) 
     445        return LB_MAC; 
     446    return LB_ASIS; 
     447} 
     448 
     449/* --------------------------------------------------------------------------------------------- */ 
     450/** 
    422451 * Open the file and load it into the buffers, either directly or using 
    423452 * a filter.  Return 0 on success, 1 on error. 
    424453 * 
    static int 
    432461edit_load_file (WEdit * edit) 
    433462{ 
    434463    int fast_load = 1; 
     464    LineBreaks lb_type = LB_ASIS; 
    435465 
    436466    /* Cannot do fast load if a filter is used */ 
    437467    if (edit_find_filter (edit->filename) >= 0) 
    edit_load_file (WEdit * edit) 
    454484        /* If we are dealing with a real file, check that it exists */ 
    455485        if (check_file_access (edit, edit->filename, &edit->stat1)) 
    456486            return 1; 
     487        lb_type = detect_lb_type(edit->filename); 
     488 
     489        if (lb_type != LB_ASIS && lb_type != LB_UNIX) 
     490            fast_load = 0; 
    457491    } 
    458492    else 
    459493    { 
    edit_load_file (WEdit * edit) 
    476510        if (*edit->filename) 
    477511        { 
    478512            edit->undo_stack_disable = 1; 
    479             if (edit_insert_file (edit, edit->filename) == 0) 
     513            if (edit_insert_file (edit, edit->filename, lb_type) == 0) 
    480514            { 
    481515                edit_clean (edit); 
    482516                return 1; 
    483517            } 
     518            edit_set_markers (edit, 0, 0, 0, 0); 
    484519            edit->undo_stack_disable = 0; 
    485520        } 
    486521    } 
    487     edit->lb = LB_ASIS; 
     522    edit->lb = lb_type; 
    488523    return 0; 
    489524} 
    490525 
    user_menu (WEdit * edit, const char *menu_file, int selected_entry) 
    16651700        { 
    16661701            long ins_len; 
    16671702 
    1668             ins_len = edit_insert_file (edit, block_file); 
     1703            ins_len = edit_insert_file (edit, block_file, LB_ASIS); 
    16691704            if (nomark == 0 && ins_len > 0) 
    16701705                edit_set_markers (edit, start_mark, start_mark + ins_len, 0, 0); 
    16711706        } 
    edit_write_stream (WEdit * edit, FILE * f) 
    19872022/* --------------------------------------------------------------------------------------------- */ 
    19882023/** inserts a file at the cursor, returns count of inserted bytes on success */ 
    19892024long 
    1990 edit_insert_file (WEdit * edit, const char *filename) 
     2025edit_insert_file (WEdit * edit, const char *filename, LineBreaks lb_type) 
    19912026{ 
    19922027    char *p; 
    19932028    long ins_len = 0; 
    edit_insert_file (WEdit * edit, const char *filename) 
    20622097            while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) 
    20632098            { 
    20642099                for (i = 0; i < blocklen; i++) 
    2065                     edit_insert (edit, buf[i]); 
     2100                { 
     2101                    if (buf[i] == '\r') 
     2102                    { 
     2103                        if (lb_type == LB_MAC) 
     2104                            edit_insert (edit, '\n'); 
     2105                        else if (lb_type == LB_WIN) 
     2106                        /* just skip */; 
     2107                        else 
     2108                            edit_insert (edit, '\r'); 
     2109                    } 
     2110                    else 
     2111                        edit_insert (edit, buf[i]); 
     2112                } 
    20662113            } 
    20672114            /* highlight inserted text then not persistent blocks */ 
    20682115            if (!option_persistent_selections) 
    edit_init (WEdit * edit, int lines, int columns, const char *filename, long line 
    21572204    edit->redo_stack_size = START_STACK_SIZE; 
    21582205    edit->redo_stack_size_mask = START_STACK_SIZE - 1; 
    21592206    edit->redo_stack = g_malloc0 ((edit->redo_stack_size + 10) * sizeof (long)); 
    2160  
     2207    edit->highlight = 0; 
    21612208    edit->utf8 = 0; 
    21622209    edit->converter = str_cnv_from_term; 
    21632210    edit_set_codeset (edit); 
  • src/editor/editcmd.c

    diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c
    index fc027e1..15f1b45 100644
    a b edit_get_save_file_as (WEdit * edit) 
    432432    { 
    433433        char *fname; 
    434434 
    435         edit->lb = cur_lb; 
     435        /* Don't change current LB type (possibly autodetected), unless user asked to. */ 
     436        if (cur_lb != LB_ASIS) 
     437            edit->lb = cur_lb; 
    436438        fname = tilde_expand (filename); 
    437439        g_free (filename); 
    438440        return fname; 
    edit_paste_from_X_buf_cmd (WEdit * edit) 
    26382640    /* try use external clipboard utility */ 
    26392641    mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_file_from_ext_clip", NULL); 
    26402642    tmp = concat_dir_and_file (mc_config_get_cache_path (), EDIT_CLIP_FILE); 
    2641     edit_insert_file (edit, tmp); 
     2643    edit_insert_file (edit, tmp, LB_ASIS); 
    26422644    g_free (tmp); 
    26432645} 
    26442646 
    edit_insert_file_cmd (WEdit * edit) 
    27542756        } 
    27552757        else 
    27562758        { 
    2757             if (edit_insert_file (edit, exp) != 0) 
     2759            if (edit_insert_file (edit, exp, LB_ASIS) != 0) 
    27582760            { 
    27592761                g_free (exp); 
    27602762                edit->force |= REDRAW_COMPLETELY; 
    edit_sort_cmd (WEdit * edit) 
    28282830    if (edit_block_delete_cmd (edit)) 
    28292831        return 1; 
    28302832    tmp = concat_dir_and_file (mc_config_get_cache_path (), EDIT_TEMP_FILE); 
    2831     edit_insert_file (edit, tmp); 
     2833    edit_insert_file (edit, tmp, LB_ASIS); 
    28322834    g_free (tmp); 
    28332835    return 0; 
    28342836} 
    edit_ext_cmd (WEdit * edit) 
    28672869 
    28682870    edit->force |= REDRAW_COMPLETELY; 
    28692871    tmp = concat_dir_and_file (mc_config_get_cache_path (), EDIT_TEMP_FILE); 
    2870     edit_insert_file (edit, tmp); 
     2872    edit_insert_file (edit, tmp, LB_ASIS); 
    28712873    g_free (tmp); 
    28722874    return 0; 
    28732875} 
  • src/editor/editdraw.c

    diff --git a/src/editor/editdraw.c b/src/editor/editdraw.c
    index 7fbb294..d0368cf 100644
    a b status_string (WEdit * edit, char *s, int w) 
    102102    unsigned int cur_utf = 0; 
    103103    int cw = 1; 
    104104 
     105    static const char *lb_names[LB_NAMES] = { 
     106        "", 
     107        "LF", 
     108        "CRLF", 
     109        "CR" 
     110    }; 
     111 
    105112    /* 
    106113     * If we are at the end of file, print <EOF>, 
    107114     * otherwise print the current character as is (if printable), 
    status_string (WEdit * edit, char *s, int w) 
    141148    /* The field lengths just prevent the status line from shortening too much */ 
    142149    if (simple_statusbar) 
    143150        g_snprintf (s, w, 
    144                     "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s", 
     151                    "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s %s", 
    145152                    edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-', 
    146153                    edit->modified ? 'M' : '-', 
    147154                    macro_index < 0 ? '-' : 'R', 
    status_string (WEdit * edit, char *s, int w) 
    150157                    edit->curs_line + 1, 
    151158                    edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str, 
    152159#ifdef HAVE_CHARSET 
    153                     mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) : "" 
     160                    mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) : "", 
    154161#else 
    155                     "" 
     162                    "", 
    156163#endif 
     164                    lb_names[edit->lb] 
    157165            ); 
    158166    else 
    159167        g_snprintf (s, w, 
    160                     "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s", 
     168                    "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s %s", 
    161169                    edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-', 
    162170                    edit->modified ? 'M' : '-', 
    163171                    macro_index < 0 ? '-' : 'R', 
    status_string (WEdit * edit, char *s, int w) 
    168176                    edit->curs_line + 1, 
    169177                    edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str, 
    170178#ifdef HAVE_CHARSET 
    171                     mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) : "" 
     179                    mc_global.source_codepage >= 0 ? get_codepage_id (mc_global.source_codepage) : "", 
    172180#else 
    173                     "" 
     181                    "", 
    174182#endif 
     183                    lb_names[edit->lb] 
    175184            ); 
    176185} 
    177186