Ticket #1652: detect-linebreaks.patch

File detect-linebreaks.patch, 9.3 KB (added by pfalcon, 14 years ago)
  • src/editor/edit-impl.h

    commit 15106b094279bf972ef7a64165fa9e5c0601214c
    Author: Paul Sokolovsky <pfalcon@users.sourceforge.net>
    Date:   Mon Jan 10 21:11:20 2011 +0200
    
        Autodetect line break type in editor.
        
        On opening a file, sample what line break it uses, remember that, and load file
        in such a way that no special characters are shown. File saved in with its original
        line break type, unless overriden with Save As (which was tweaked to treat "don't
        change" option as such).
    
    diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h
    index 55a5707..d4428c4 100644
    a b int edit_save_block_cmd (WEdit * edit); 
    289289int edit_insert_file_cmd (WEdit * edit); 
    290290void edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width); 
    291291int edit_insert_column_of_text_from_file (WEdit * edit, int file); 
    292 int edit_insert_file (WEdit * edit, const char *filename); 
     292int edit_insert_file (WEdit * edit, const char *filename, LineBreaks lb_type); 
    293293int edit_load_back_cmd (WEdit * edit); 
    294294int edit_load_forward_cmd (WEdit * edit); 
    295295void edit_block_process_cmd (WEdit * edit, const char *shell_cmd, int block); 
  • src/editor/edit.c

    diff --git a/src/editor/edit.c b/src/editor/edit.c
    index 8241e6d..8d6f529 100644
    a b check_file_access (WEdit * edit, const char *filename, struct stat *st) 
    421421    return 0; 
    422422} 
    423423 
     424static LineBreaks 
     425detect_lb_type (char *filename) 
     426{ 
     427    char buf[256]; 
     428    int file, sz; 
     429 
     430    file = mc_open (filename, O_RDONLY | O_BINARY); 
     431    if (file == -1) 
     432        return LB_ASIS; 
     433 
     434    memset(buf, 0, sizeof(buf)); 
     435    sz = mc_read (file, buf, sizeof(buf) - 1); 
     436    if (sz < 0) 
     437        return LB_ASIS; 
     438    mc_close(file); 
     439 
     440    buf[sz] = 0; 
     441    if (strstr(buf, "\r\n")) 
     442        return LB_WIN; 
     443    if (strchr(buf, '\r')) 
     444        return LB_MAC; 
     445    return LB_ASIS; 
     446} 
     447 
    424448/* --------------------------------------------------------------------------------------------- */ 
    425449/** 
    426450 * Open the file and load it into the buffers, either directly or using 
    static int 
    436460edit_load_file (WEdit * edit) 
    437461{ 
    438462    int fast_load = 1; 
     463    LineBreaks lb_type = LB_ASIS; 
    439464 
    440465    /* Cannot do fast load if a filter is used */ 
    441466    if (edit_find_filter (edit->filename) >= 0) 
    edit_load_file (WEdit * edit) 
    458483        /* If we are dealing with a real file, check that it exists */ 
    459484        if (check_file_access (edit, edit->filename, &edit->stat1)) 
    460485            return 1; 
     486        lb_type = detect_lb_type(edit->filename); 
     487 
     488        if (lb_type != LB_ASIS && lb_type != LB_UNIX) 
     489            fast_load = 0; 
    461490    } 
    462491    else 
    463492    { 
    edit_load_file (WEdit * edit) 
    480509        if (*edit->filename) 
    481510        { 
    482511            edit->undo_stack_disable = 1; 
    483             if (!edit_insert_file (edit, edit->filename)) 
     512            if (!edit_insert_file (edit, edit->filename, lb_type)) 
    484513            { 
    485514                edit_clean (edit); 
    486515                return 1; 
    edit_load_file (WEdit * edit) 
    488517            edit->undo_stack_disable = 0; 
    489518        } 
    490519    } 
    491     edit->lb = LB_ASIS; 
     520    edit->lb = lb_type; 
    492521    return 0; 
    493522} 
    494523 
    user_menu (WEdit * edit) 
    16991728        } 
    17001729 
    17011730        if (rc == 0) 
    1702             edit_insert_file (edit, block_file); 
     1731            edit_insert_file (edit, block_file, LB_ASIS); 
    17031732 
    17041733        /* truncate block file */ 
    17051734        fd = fopen (block_file, "w"); 
    edit_write_stream (WEdit * edit, FILE * f) 
    20142043/* --------------------------------------------------------------------------------------------- */ 
    20152044/** inserts a file at the cursor, returns 1 on success */ 
    20162045int 
    2017 edit_insert_file (WEdit * edit, const char *filename) 
     2046edit_insert_file (WEdit * edit, const char *filename, LineBreaks lb_type) 
    20182047{ 
    20192048    char *p; 
    20202049 
    edit_insert_file (WEdit * edit, const char *filename) 
    20732102        else 
    20742103            while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) 
    20752104                for (i = 0; i < blocklen; i++) 
    2076                     edit_insert (edit, buf[i]); 
     2105                { 
     2106                    if (buf[i] == '\r') 
     2107                    { 
     2108                        if (lb_type == LB_MAC) 
     2109                            edit_insert (edit, '\n'); 
     2110                        else if (lb_type == LB_WIN) 
     2111                            /* just skip */; 
     2112                        else 
     2113                            edit_insert (edit, '\r'); 
     2114                    } 
     2115                    else 
     2116                        edit_insert (edit, buf[i]); 
     2117                } 
    20772118        edit_cursor_move (edit, current - edit->curs1); 
    20782119        g_free (buf); 
    20792120        mc_close (file); 
  • src/editor/editcmd.c

    diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c
    index d916191..704384c 100644
    a b edit_get_save_file_as (WEdit * edit) 
    433433    { 
    434434        char *fname; 
    435435 
    436         edit->lb = cur_lb; 
     436        /* Don't change current LB type (possibly autodetected), unless user asked to. */ 
     437        if (cur_lb != LB_ASIS) 
     438            edit->lb = cur_lb; 
    437439        fname = tilde_expand (filename); 
    438440        g_free (filename); 
    439441        return fname; 
    edit_paste_from_X_buf_cmd (WEdit * edit) 
    25012503    /* try use external clipboard utility */ 
    25022504    paste_to_file_from_ext_clip (); 
    25032505    tmp = concat_dir_and_file (home_dir, EDIT_CLIP_FILE); 
    2504     edit_insert_file (edit, tmp); 
     2506    edit_insert_file (edit, tmp, LB_ASIS); 
    25052507    g_free (tmp); 
    25062508} 
    25072509 
    edit_insert_file_cmd (WEdit * edit) 
    26172619        } 
    26182620        else 
    26192621        { 
    2620             if (edit_insert_file (edit, exp)) 
     2622            if (edit_insert_file (edit, exp, LB_ASIS)) 
    26212623            { 
    26222624                g_free (exp); 
    26232625                edit->force |= REDRAW_COMPLETELY; 
    edit_sort_cmd (WEdit * edit) 
    26892691    if (edit_block_delete_cmd (edit)) 
    26902692        return 1; 
    26912693    tmp = concat_dir_and_file (home_dir, EDIT_TEMP_FILE); 
    2692     edit_insert_file (edit, tmp); 
     2694    edit_insert_file (edit, tmp, LB_ASIS); 
    26932695    g_free (tmp); 
    26942696    return 0; 
    26952697} 
    edit_ext_cmd (WEdit * edit) 
    27262728 
    27272729    edit->force |= REDRAW_COMPLETELY; 
    27282730    tmp = concat_dir_and_file (home_dir, EDIT_TEMP_FILE); 
    2729     edit_insert_file (edit, tmp); 
     2731    edit_insert_file (edit, tmp, LB_ASIS); 
    27302732    g_free (tmp); 
    27312733    return 0; 
    27322734} 
    edit_block_process_cmd (WEdit * edit, const char *shell_cmd, int block) 
    28462848        /* insert result block */ 
    28472849        if (block && !edit_block_delete_cmd (edit)) 
    28482850        { 
    2849             edit_insert_file (edit, b); 
     2851            edit_insert_file (edit, b, LB_ASIS); 
    28502852            block_file = fopen (b, "w"); 
    28512853            if (block_file != NULL) 
    28522854                fclose (block_file); 
  • src/editor/editdraw.c

    commit 37ad5c8472c857253ff03022383227ebf7a83622
    Author: Paul Sokolovsky <pfalcon@users.sourceforge.net>
    Date:   Sun Jan 16 01:16:02 2011 +0200
    
        Show line breaks type in status line.
        
        Shown after all existing info, after codepage, only if explicit one is
        set (!= LB_ASIS).
    
    diff --git a/src/editor/editdraw.c b/src/editor/editdraw.c
    index 06d7a0f..3f059f9 100644
    a b status_string (WEdit * edit, char *s, int w) 
    103103    unsigned int cur_utf = 0; 
    104104    int cw = 1; 
    105105 
     106    static const char *lb_names[LB_NAMES] = { 
     107        "", 
     108        "LF", 
     109        "CRLF", 
     110        "CR" 
     111    }; 
     112 
    106113    /* 
    107114     * If we are at the end of file, print <EOF>, 
    108115     * otherwise print the current character as is (if printable), 
    status_string (WEdit * edit, char *s, int w) 
    142149    /* The field lengths just prevent the status line from shortening too much */ 
    143150    if (simple_statusbar) 
    144151        g_snprintf (s, w, 
    145                     "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s", 
     152                    "%c%c%c%c %3ld %5ld/%ld %6ld/%ld %s %s %s", 
    146153                    edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-', 
    147154                    edit->modified ? 'M' : '-', 
    148155                    edit->macro_i < 0 ? '-' : 'R', 
    status_string (WEdit * edit, char *s, int w) 
    151158                    edit->curs_line + 1, 
    152159                    edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str, 
    153160#ifdef HAVE_CHARSET 
    154                     source_codepage >= 0 ? get_codepage_id (source_codepage) : "" 
     161                    source_codepage >= 0 ? get_codepage_id (source_codepage) : "", 
    155162#else 
    156                     "" 
     163                    "", 
    157164#endif 
     165                    lb_names[edit->lb] 
    158166            ); 
    159167    else 
    160168        g_snprintf (s, w, 
    161                     "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s", 
     169                    "[%c%c%c%c] %2ld L:[%3ld+%2ld %3ld/%3ld] *(%-4ld/%4ldb) %s %s %s", 
    162170                    edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-', 
    163171                    edit->modified ? 'M' : '-', 
    164172                    edit->macro_i < 0 ? '-' : 'R', 
    status_string (WEdit * edit, char *s, int w) 
    169177                    edit->curs_line + 1, 
    170178                    edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str, 
    171179#ifdef HAVE_CHARSET 
    172                     source_codepage >= 0 ? get_codepage_id (source_codepage) : "" 
     180                    source_codepage >= 0 ? get_codepage_id (source_codepage) : "", 
    173181#else 
    174                     "" 
     182                    "", 
    175183#endif 
     184                    lb_names[edit->lb] 
    176185            ); 
    177186} 
    178187