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); |
289 | 289 | int edit_insert_file_cmd (WEdit * edit); |
290 | 290 | void edit_insert_column_of_text (WEdit * edit, unsigned char *data, int size, int width); |
291 | 291 | int edit_insert_column_of_text_from_file (WEdit * edit, int file); |
292 | | int edit_insert_file (WEdit * edit, const char *filename); |
| 292 | int edit_insert_file (WEdit * edit, const char *filename, LineBreaks lb_type); |
293 | 293 | int edit_load_back_cmd (WEdit * edit); |
294 | 294 | int edit_load_forward_cmd (WEdit * edit); |
295 | 295 | void edit_block_process_cmd (WEdit * edit, const char *shell_cmd, int block); |
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) |
421 | 421 | return 0; |
422 | 422 | } |
423 | 423 | |
| 424 | static LineBreaks |
| 425 | detect_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 | |
424 | 448 | /* --------------------------------------------------------------------------------------------- */ |
425 | 449 | /** |
426 | 450 | * Open the file and load it into the buffers, either directly or using |
… |
… |
static int |
436 | 460 | edit_load_file (WEdit * edit) |
437 | 461 | { |
438 | 462 | int fast_load = 1; |
| 463 | LineBreaks lb_type = LB_ASIS; |
439 | 464 | |
440 | 465 | /* Cannot do fast load if a filter is used */ |
441 | 466 | if (edit_find_filter (edit->filename) >= 0) |
… |
… |
edit_load_file (WEdit * edit) |
458 | 483 | /* If we are dealing with a real file, check that it exists */ |
459 | 484 | if (check_file_access (edit, edit->filename, &edit->stat1)) |
460 | 485 | 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; |
461 | 490 | } |
462 | 491 | else |
463 | 492 | { |
… |
… |
edit_load_file (WEdit * edit) |
480 | 509 | if (*edit->filename) |
481 | 510 | { |
482 | 511 | edit->undo_stack_disable = 1; |
483 | | if (!edit_insert_file (edit, edit->filename)) |
| 512 | if (!edit_insert_file (edit, edit->filename, lb_type)) |
484 | 513 | { |
485 | 514 | edit_clean (edit); |
486 | 515 | return 1; |
… |
… |
edit_load_file (WEdit * edit) |
488 | 517 | edit->undo_stack_disable = 0; |
489 | 518 | } |
490 | 519 | } |
491 | | edit->lb = LB_ASIS; |
| 520 | edit->lb = lb_type; |
492 | 521 | return 0; |
493 | 522 | } |
494 | 523 | |
… |
… |
user_menu (WEdit * edit) |
1699 | 1728 | } |
1700 | 1729 | |
1701 | 1730 | if (rc == 0) |
1702 | | edit_insert_file (edit, block_file); |
| 1731 | edit_insert_file (edit, block_file, LB_ASIS); |
1703 | 1732 | |
1704 | 1733 | /* truncate block file */ |
1705 | 1734 | fd = fopen (block_file, "w"); |
… |
… |
edit_write_stream (WEdit * edit, FILE * f) |
2014 | 2043 | /* --------------------------------------------------------------------------------------------- */ |
2015 | 2044 | /** inserts a file at the cursor, returns 1 on success */ |
2016 | 2045 | int |
2017 | | edit_insert_file (WEdit * edit, const char *filename) |
| 2046 | edit_insert_file (WEdit * edit, const char *filename, LineBreaks lb_type) |
2018 | 2047 | { |
2019 | 2048 | char *p; |
2020 | 2049 | |
… |
… |
edit_insert_file (WEdit * edit, const char *filename) |
2073 | 2102 | else |
2074 | 2103 | while ((blocklen = mc_read (file, (char *) buf, TEMP_BUF_LEN)) > 0) |
2075 | 2104 | 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 | } |
2077 | 2118 | edit_cursor_move (edit, current - edit->curs1); |
2078 | 2119 | g_free (buf); |
2079 | 2120 | mc_close (file); |
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) |
433 | 433 | { |
434 | 434 | char *fname; |
435 | 435 | |
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; |
437 | 439 | fname = tilde_expand (filename); |
438 | 440 | g_free (filename); |
439 | 441 | return fname; |
… |
… |
edit_paste_from_X_buf_cmd (WEdit * edit) |
2501 | 2503 | /* try use external clipboard utility */ |
2502 | 2504 | paste_to_file_from_ext_clip (); |
2503 | 2505 | tmp = concat_dir_and_file (home_dir, EDIT_CLIP_FILE); |
2504 | | edit_insert_file (edit, tmp); |
| 2506 | edit_insert_file (edit, tmp, LB_ASIS); |
2505 | 2507 | g_free (tmp); |
2506 | 2508 | } |
2507 | 2509 | |
… |
… |
edit_insert_file_cmd (WEdit * edit) |
2617 | 2619 | } |
2618 | 2620 | else |
2619 | 2621 | { |
2620 | | if (edit_insert_file (edit, exp)) |
| 2622 | if (edit_insert_file (edit, exp, LB_ASIS)) |
2621 | 2623 | { |
2622 | 2624 | g_free (exp); |
2623 | 2625 | edit->force |= REDRAW_COMPLETELY; |
… |
… |
edit_sort_cmd (WEdit * edit) |
2689 | 2691 | if (edit_block_delete_cmd (edit)) |
2690 | 2692 | return 1; |
2691 | 2693 | tmp = concat_dir_and_file (home_dir, EDIT_TEMP_FILE); |
2692 | | edit_insert_file (edit, tmp); |
| 2694 | edit_insert_file (edit, tmp, LB_ASIS); |
2693 | 2695 | g_free (tmp); |
2694 | 2696 | return 0; |
2695 | 2697 | } |
… |
… |
edit_ext_cmd (WEdit * edit) |
2726 | 2728 | |
2727 | 2729 | edit->force |= REDRAW_COMPLETELY; |
2728 | 2730 | tmp = concat_dir_and_file (home_dir, EDIT_TEMP_FILE); |
2729 | | edit_insert_file (edit, tmp); |
| 2731 | edit_insert_file (edit, tmp, LB_ASIS); |
2730 | 2732 | g_free (tmp); |
2731 | 2733 | return 0; |
2732 | 2734 | } |
… |
… |
edit_block_process_cmd (WEdit * edit, const char *shell_cmd, int block) |
2846 | 2848 | /* insert result block */ |
2847 | 2849 | if (block && !edit_block_delete_cmd (edit)) |
2848 | 2850 | { |
2849 | | edit_insert_file (edit, b); |
| 2851 | edit_insert_file (edit, b, LB_ASIS); |
2850 | 2852 | block_file = fopen (b, "w"); |
2851 | 2853 | if (block_file != NULL) |
2852 | 2854 | fclose (block_file); |
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) |
103 | 103 | unsigned int cur_utf = 0; |
104 | 104 | int cw = 1; |
105 | 105 | |
| 106 | static const char *lb_names[LB_NAMES] = { |
| 107 | "", |
| 108 | "LF", |
| 109 | "CRLF", |
| 110 | "CR" |
| 111 | }; |
| 112 | |
106 | 113 | /* |
107 | 114 | * If we are at the end of file, print <EOF>, |
108 | 115 | * otherwise print the current character as is (if printable), |
… |
… |
status_string (WEdit * edit, char *s, int w) |
142 | 149 | /* The field lengths just prevent the status line from shortening too much */ |
143 | 150 | if (simple_statusbar) |
144 | 151 | 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", |
146 | 153 | edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-', |
147 | 154 | edit->modified ? 'M' : '-', |
148 | 155 | edit->macro_i < 0 ? '-' : 'R', |
… |
… |
status_string (WEdit * edit, char *s, int w) |
151 | 158 | edit->curs_line + 1, |
152 | 159 | edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str, |
153 | 160 | #ifdef HAVE_CHARSET |
154 | | source_codepage >= 0 ? get_codepage_id (source_codepage) : "" |
| 161 | source_codepage >= 0 ? get_codepage_id (source_codepage) : "", |
155 | 162 | #else |
156 | | "" |
| 163 | "", |
157 | 164 | #endif |
| 165 | lb_names[edit->lb] |
158 | 166 | ); |
159 | 167 | else |
160 | 168 | 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", |
162 | 170 | edit->mark1 != edit->mark2 ? (edit->column_highlight ? 'C' : 'B') : '-', |
163 | 171 | edit->modified ? 'M' : '-', |
164 | 172 | edit->macro_i < 0 ? '-' : 'R', |
… |
… |
status_string (WEdit * edit, char *s, int w) |
169 | 177 | edit->curs_line + 1, |
170 | 178 | edit->total_lines + 1, edit->curs1, edit->last_byte, byte_str, |
171 | 179 | #ifdef HAVE_CHARSET |
172 | | source_codepage >= 0 ? get_codepage_id (source_codepage) : "" |
| 180 | source_codepage >= 0 ? get_codepage_id (source_codepage) : "", |
173 | 181 | #else |
174 | | "" |
| 182 | "", |
175 | 183 | #endif |
| 184 | lb_names[edit->lb] |
176 | 185 | ); |
177 | 186 | } |
178 | 187 | |