Ticket #3068: mcedit-vim-modeline-4.patch
File mcedit-vim-modeline-4.patch, 19.4 KB (added by twasilczyk, 10 years ago) |
---|
-
.gitignore
diff --git a/.gitignore b/.gitignore index 310c62f..9e8b9e7 100644
a b make.log 46 46 make.clang 47 47 make.gcc 48 48 make.tcc 49 misc/ext.d/doc.sh 50 misc/ext.d/misc.sh 51 misc/ext.d/text.sh 52 misc/ext.d/web.sh 53 misc/syntax/Syntax 54 src/man2hlp/man2hlp 55 src/vfs/extfs/helpers/uc1541 56 src/vfs/extfs/helpers/ulib 57 tests/src/editor/test-data.txt -
src/editor/edit-impl.h
diff --git a/src/editor/edit-impl.h b/src/editor/edit-impl.h index e840d30..5eb417e 100644
a b 59 59 #define MARK_CURS 1000000000 60 60 #define KEY_PRESS 1500000000 61 61 62 /* Tabs spaces: (sofar only HALF_TAB_SIZE is used: */63 #define TAB_SIZE option_tab_spacing64 #define HALF_TAB_SIZE ((int) option_tab_spacing / 2)65 66 62 /* max count stack files */ 67 63 #define MAX_HISTORY_MOVETO 50 68 64 #define LINE_STATE_WIDTH 8 … … void format_paragraph (WEdit * edit, gboolean force); 286 282 /* either command or char_for_insertion must be passed as -1 */ 287 283 void edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion); 288 284 285 int edit_tab_spacing(const WEdit *edit, gboolean half); 286 289 287 /*** inline functions ****************************************************************************/ 290 288 291 289 /** -
src/editor/edit.c
diff --git a/src/editor/edit.c b/src/editor/edit.c index 1c80466..f48bac1 100644
a b static const off_t option_filesize_default_threshold = 64 * 1024 * 1024; 142 142 /* --------------------------------------------------------------------------------------------- */ 143 143 /*** file scope functions ************************************************************************/ 144 144 /* --------------------------------------------------------------------------------------------- */ 145 146 static gboolean 147 edit_fake_half_tabs(WEdit *edit) 148 { 149 if (edit->force_halftabs == -1) 150 return option_fake_half_tabs; 151 return edit->force_halftabs; 152 } 153 154 static gboolean 155 edit_fill_tabs_with_spaces(WEdit *edit) 156 { 157 if (edit->force_fill_tabs_with_spaces == -1) 158 return option_fill_tabs_with_spaces; 159 return edit->force_fill_tabs_with_spaces; 160 } 161 162 int 163 edit_tab_spacing(const WEdit *edit, gboolean half) 164 { 165 int spacing = option_tab_spacing; 166 if (edit->force_tab_spacing > 0) 167 spacing = edit->force_tab_spacing; 168 if (half) 169 spacing /= 2; 170 return spacing; 171 } 172 145 173 /** 146 174 * Load file OR text into buffers. Set cursor to the beginning of file. 147 175 * … … is_aligned_on_a_tab (WEdit * edit) 1302 1330 long curs_col; 1303 1331 1304 1332 edit_update_curs_col (edit); 1305 curs_col = edit->curs_col % ( TAB_SIZE* space_width);1306 return (curs_col == 0 || curs_col == ( HALF_TAB_SIZE* space_width));1333 curs_col = edit->curs_col % (edit_tab_spacing(edit, FALSE) * space_width); 1334 return (curs_col == 0 || curs_col == (edit_tab_spacing(edit, TRUE) * space_width)); 1307 1335 } 1308 1336 1309 1337 /* --------------------------------------------------------------------------------------------- */ … … right_of_four_spaces (WEdit * edit) 1313 1341 { 1314 1342 int i, ch = 0; 1315 1343 1316 for (i = 1; i <= HALF_TAB_SIZE; i++)1344 for (i = 1; i <= edit_tab_spacing(edit, TRUE); i++) 1317 1345 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 - i); 1318 1346 1319 1347 return (ch == ' ' && is_aligned_on_a_tab (edit)); … … left_of_four_spaces (WEdit * edit) 1326 1354 { 1327 1355 int i, ch = 0; 1328 1356 1329 for (i = 0; i < HALF_TAB_SIZE; i++)1357 for (i = 0; i < edit_tab_spacing(edit, TRUE); i++) 1330 1358 ch |= edit_buffer_get_byte (&edit->buffer, edit->buffer.curs1 + i); 1331 1359 1332 1360 return (ch == ' ' && is_aligned_on_a_tab (edit)); … … insert_spaces_tab (WEdit * edit, gboolean half) 1374 1402 long i; 1375 1403 1376 1404 edit_update_curs_col (edit); 1377 i = option_tab_spacing * space_width; 1378 if (half) 1379 i /= 2; 1405 i = edit_tab_spacing(edit, half) * space_width; 1380 1406 if (i != 0) 1381 1407 { 1382 1408 i = ((edit->curs_col / i) + 1) * i - edit->curs_col; … … insert_spaces_tab (WEdit * edit, gboolean half) 1393 1419 static inline void 1394 1420 edit_tab_cmd (WEdit * edit) 1395 1421 { 1396 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer))1422 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer)) 1397 1423 { 1398 1424 /* insert a half tab (usually four spaces) unless there is a 1399 1425 half tab already behind, then delete it and insert a 1400 1426 full tab. */ 1401 if ( option_fill_tabs_with_spaces|| !right_of_four_spaces (edit))1427 if (edit_fill_tabs_with_spaces(edit) || !right_of_four_spaces (edit)) 1402 1428 insert_spaces_tab (edit, TRUE); 1403 1429 else 1404 1430 { 1405 1431 int i; 1406 1432 1407 for (i = 1; i <= HALF_TAB_SIZE; i++)1433 for (i = 1; i <= edit_tab_spacing(edit, TRUE); i++) 1408 1434 edit_backspace (edit, TRUE); 1409 1435 edit_insert (edit, '\t'); 1410 1436 } 1411 1437 } 1412 else if ( option_fill_tabs_with_spaces)1438 else if (edit_fill_tabs_with_spaces(edit)) 1413 1439 insert_spaces_tab (edit, FALSE); 1414 1440 else 1415 1441 edit_insert (edit, '\t'); … … edit_move_block_to_right (WEdit * edit) 1545 1571 edit_cursor_move (edit, cur_bol - edit->buffer.curs1); 1546 1572 if (!edit_line_is_blank (edit, edit->buffer.curs_line)) 1547 1573 { 1548 if ( option_fill_tabs_with_spaces)1549 insert_spaces_tab (edit, option_fake_half_tabs);1574 if (edit_fill_tabs_with_spaces(edit)) 1575 insert_spaces_tab (edit, edit_fake_half_tabs(edit)); 1550 1576 else 1551 1577 edit_insert (edit, '\t'); 1552 1578 edit_cursor_move (edit, … … edit_move_block_to_left (WEdit * edit) 1585 1611 1586 1612 edit_cursor_move (edit, cur_bol - edit->buffer.curs1); 1587 1613 1588 if (option_fake_half_tabs) 1589 del_tab_width = HALF_TAB_SIZE; 1590 else 1591 del_tab_width = option_tab_spacing; 1614 del_tab_width = edit_tab_spacing(edit, edit_fake_half_tabs(edit)); 1592 1615 1593 1616 next_char = edit_buffer_get_current_byte (&edit->buffer); 1594 1617 if (next_char == '\t') … … edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath) 2017 2040 * cursor on that line and show it in the middle of the screen. 2018 2041 */ 2019 2042 2043 static off_t 2044 mc_findinfile(int handle, const gchar **searchstrs) 2045 { 2046 off_t offset_bak, offset_cur, filesize; 2047 char buff[10240]; 2048 ssize_t got; 2049 const char *found; 2050 size_t i, smallest_searchstr_len, searchstrs_count; 2051 2052 searchstrs_count = 0; 2053 smallest_searchstr_len = strlen(searchstrs[0]); 2054 while (searchstrs[searchstrs_count] != NULL) { 2055 size_t len = strlen(searchstrs[searchstrs_count]); 2056 if (smallest_searchstr_len > len) 2057 smallest_searchstr_len = len; 2058 searchstrs_count++; 2059 } 2060 2061 offset_bak = mc_lseek(handle, 0, SEEK_CUR); 2062 if (offset_bak < 0) 2063 return -1; 2064 2065 filesize = mc_lseek(handle, 0, SEEK_END); 2066 offset_cur = mc_lseek(handle, offset_bak, SEEK_SET); 2067 2068 do { 2069 got = mc_read(handle, buff, sizeof(buff) - 1); 2070 if (got <= 0) 2071 return -1; 2072 buff[got] = '\0'; 2073 2074 for (i = 0; i < searchstrs_count; i++) { 2075 /* this could be done in O(n), not O(n*m) */ 2076 found = strstr(buff, searchstrs[i]); 2077 if (found) { 2078 return mc_lseek(handle, offset_cur + (found - buff), SEEK_SET) + 2079 strlen(searchstrs[i]); 2080 } 2081 } 2082 2083 offset_cur = mc_lseek(handle, 2084 offset_cur + (sizeof(buff) - 1) - smallest_searchstr_len, SEEK_SET); 2085 } while (offset_cur + smallest_searchstr_len < filesize); 2086 2087 return -1; 2088 } 2089 2090 static void 2091 scan_modeline(WEdit *edit, const vfs_path_t *filename) 2092 { 2093 int fd; 2094 int conf_tabstop = -1; 2095 int conf_softtabstop = -1; 2096 int conf_expandtab = -1; 2097 int dst_halftabs = -1; 2098 int dst_fill_tabs_with_spaces = -1; 2099 int dst_force_tab_spacing = -1; 2100 const gchar *searchstrs[] = { 2101 "\n/*", "\n//", "\n#", "\ndnl", NULL 2102 }; 2103 2104 fd = mc_open(filename, O_RDONLY | O_BINARY); 2105 while (1) { 2106 off_t foundat; 2107 char buff[200]; 2108 const char *s; 2109 ssize_t got; 2110 const char *foundstr; 2111 2112 foundat = mc_findinfile(fd, searchstrs); 2113 if (foundat < 0) 2114 return; 2115 2116 mc_lseek(fd, foundat, SEEK_SET); 2117 2118 got = mc_read(fd, buff, sizeof(buff) - 1); 2119 if (got <= 0) 2120 return; 2121 buff[got] = '\0'; 2122 2123 s = buff; 2124 while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') 2125 s++; 2126 if (strncmp(s, "vim:", 4) != 0) 2127 continue; 2128 s += 4; 2129 2130 foundstr = strstr(s, "tabstop="); 2131 if (foundstr) 2132 conf_tabstop=atoi(foundstr + 8); 2133 foundstr = strstr(s, "softtabstop="); 2134 if (foundstr) 2135 conf_softtabstop=atoi(foundstr + 12); 2136 foundstr = strstr(s, "expandtab"); 2137 if (foundstr) 2138 conf_expandtab = 1; 2139 foundstr = strstr(s, "noexpandtab"); 2140 if (foundstr) 2141 conf_expandtab = 0; 2142 2143 break; 2144 } 2145 2146 if (conf_softtabstop > 0 && conf_tabstop > 0 && 2147 conf_softtabstop * 2 == conf_tabstop) 2148 { 2149 dst_halftabs = 1; 2150 } 2151 2152 dst_fill_tabs_with_spaces = conf_expandtab; 2153 2154 dst_force_tab_spacing = conf_tabstop; 2155 2156 edit->force_halftabs = dst_halftabs; 2157 edit->force_fill_tabs_with_spaces = dst_fill_tabs_with_spaces; 2158 edit->force_tab_spacing = dst_force_tab_spacing; 2159 2160 mc_close(fd); 2161 } 2162 2020 2163 WEdit * 2021 2164 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath, 2022 2165 long line) … … edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * f 2054 2197 edit_save_size (edit); 2055 2198 } 2056 2199 2200 edit->force_halftabs = -1; 2201 edit->force_fill_tabs_with_spaces = -1; 2202 edit->force_tab_spacing = -1; 2203 2204 scan_modeline(edit, filename_vpath); 2205 2057 2206 edit->drag_state = MCEDIT_DRAG_NORMAL; 2058 2207 2059 2208 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; … … edit_move_forward3 (const WEdit * edit, off_t current, long cols, off_t upto) 2755 2904 2756 2905 if (c == '\n') 2757 2906 return (upto != 0 ? (off_t) col : p); 2758 if (c == '\t') 2759 col += TAB_SIZE - col % TAB_SIZE; 2907 if (c == '\t') { 2908 int tab_size = edit_tab_spacing(edit, FALSE); 2909 col += tab_size - col % tab_size; 2910 } 2760 2911 else if ((c < 32 || c == 127) && (orig_c == c 2761 2912 #ifdef HAVE_CHARSET 2762 2913 || (!mc_global.utf8_display && !edit->utf8) … … edit_move_to_prev_col (WEdit * edit, off_t p) 2909 3060 else 2910 3061 { 2911 3062 edit->over_col = 0; 2912 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer))3063 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer)) 2913 3064 { 2914 3065 long fake_half_tabs; 2915 3066 2916 3067 edit_update_curs_col (edit); 2917 3068 2918 fake_half_tabs = HALF_TAB_SIZE* space_width;3069 fake_half_tabs = edit_tab_spacing(edit, TRUE) * space_width; 2919 3070 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0) 2920 3071 { 2921 3072 int q; … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3402 3553 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0) 3403 3554 edit_backspace (edit, TRUE); 3404 3555 } 3405 else if ( option_fake_half_tabs&& is_in_indent (&edit->buffer)3556 else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) 3406 3557 && right_of_four_spaces (edit)) 3407 3558 { 3408 3559 int i; 3409 3560 3410 for (i = 0; i < HALF_TAB_SIZE; i++)3561 for (i = 0; i < edit_tab_spacing(edit, TRUE); i++) 3411 3562 edit_backspace (edit, TRUE); 3412 3563 } 3564 else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) 3565 && edit_buffer_get_byte(&edit->buffer, edit->buffer.curs1 - 1) == '\t') { 3566 int i; 3567 3568 edit_backspace (edit, TRUE); 3569 for (i = 0; i < edit_tab_spacing(edit, TRUE); i++) 3570 edit_insert (edit, ' '); 3571 /* TODO: if (right_of_four_spaces(edit) && left_of_four_spaces(edit) 3572 * - replace spaces with a tab */ 3573 } 3413 3574 else 3414 3575 edit_backspace (edit, FALSE); 3415 3576 break; … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3422 3583 if (option_cursor_beyond_eol && edit->over_col > 0) 3423 3584 edit_insert_over (edit); 3424 3585 3425 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && left_of_four_spaces (edit))3586 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit)) 3426 3587 { 3427 3588 int i; 3428 3589 3429 for (i = 1; i <= HALF_TAB_SIZE; i++)3590 for (i = 1; i <= edit_tab_spacing(edit, TRUE); i++) 3430 3591 edit_delete (edit, TRUE); 3431 3592 } 3432 3593 else … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3488 3649 edit->column_highlight = 1; 3489 3650 case CK_Left: 3490 3651 case CK_MarkLeft: 3491 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && right_of_four_spaces (edit))3652 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && right_of_four_spaces (edit)) 3492 3653 { 3493 3654 if (option_cursor_beyond_eol && edit->over_col > 0) 3494 3655 edit->over_col--; 3495 3656 else 3496 edit_cursor_move (edit, - HALF_TAB_SIZE);3657 edit_cursor_move (edit, -edit_tab_spacing(edit, TRUE)); 3497 3658 edit->force &= (0xFFF - REDRAW_CHAR_ONLY); 3498 3659 } 3499 3660 else … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3503 3664 edit->column_highlight = 1; 3504 3665 case CK_Right: 3505 3666 case CK_MarkRight: 3506 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && left_of_four_spaces (edit))3667 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit)) 3507 3668 { 3508 edit_cursor_move (edit, HALF_TAB_SIZE);3669 edit_cursor_move (edit, edit_tab_spacing(edit, TRUE)); 3509 3670 edit->force &= (0xFFF - REDRAW_CHAR_ONLY); 3510 3671 } 3511 3672 else … … edit_move_down (WEdit * edit, long i, gboolean do_scroll) 3964 4125 } 3965 4126 3966 4127 /* --------------------------------------------------------------------------------------------- */ 4128 4129 /* vim: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */ -
src/editor/editcmd.c
diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 4347fd7..a51839c 100644
a b 67 67 #endif 68 68 69 69 #include "src/history.h" 70 #include "src/setup.h" /* option_tab_spacing */70 #include "src/setup.h" 71 71 #ifdef HAVE_CHARSET 72 72 #include "src/selcodepage.h" 73 73 #endif -
src/editor/editdraw.c
diff --git a/src/editor/editdraw.c b/src/editor/editdraw.c index cd084de..c66b675 100644
a b edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_c 584 584 int tab_over = 0; 585 585 gboolean wide_width_char = FALSE; 586 586 gboolean control_char = FALSE; 587 int tab_size = edit_tab_spacing(edit, FALSE); 587 588 588 589 p->ch = 0; 589 590 p->style = 0; … … edit_draw_this_line (WEdit * edit, off_t b, long row, long start_col, long end_c 632 633 col = end_col - edit->start_col + 1; /* quit */ 633 634 break; 634 635 case '\t': 635 i = TAB_SIZE - ((int) col % TAB_SIZE);636 i = tab_size - ((int) col % tab_size); 636 637 tab_over = (end_col - edit->start_col) - (col + i - 1); 637 638 if (tab_over < 0) 638 639 i += tab_over; -
src/editor/editwidget.h
diff --git a/src/editor/editwidget.h b/src/editor/editwidget.h index 90c73f6..d58e197 100644
a b struct WEdit 163 163 /* line break */ 164 164 LineBreaks lb; 165 165 gboolean extmod; 166 167 int force_halftabs; 168 int force_fill_tabs_with_spaces; 169 int force_tab_spacing; 166 170 }; 167 171 168 172 /*** global variables defined in .c file *********************************************************/ … … struct WEdit 171 175 172 176 /*** inline functions ****************************************************************************/ 173 177 #endif /* MC__EDIT_WIDGET_H */ 178 179 /* vim: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */ -
src/editor/wordproc.c
diff --git a/src/editor/wordproc.c b/src/editor/wordproc.c index 8c42ce9..5358dab 100644
a b 50 50 51 51 #include "lib/global.h" 52 52 53 #include "src/setup.h" /* option_tab_spacing */54 55 53 #include "edit-impl.h" 56 54 #include "editwidget.h" 57 55 … … 59 57 60 58 /*** file scope macro definitions ****************************************************************/ 61 59 62 #define tab_width option_tab_spacing63 64 60 #define NO_FORMAT_CHARS_START "-+*\\,.;:&>" 65 61 #define FONT_MEAN_WIDTH 1 66 62 … … strip_newlines (unsigned char *t, off_t size) 205 201 */ 206 202 207 203 static inline off_t 208 next_tab_pos (off_t x )204 next_tab_pos (off_t x, int tab_width) 209 205 { 210 206 x += tab_width - x % tab_width; 211 207 return x; … … next_tab_pos (off_t x) 214 210 /* --------------------------------------------------------------------------------------------- */ 215 211 216 212 static inline off_t 217 line_pixel_length (unsigned char *t, off_t b, off_t l, gboolean utf8)213 line_pixel_length (unsigned char *t, off_t b, off_t l, int tab_width, gboolean utf8) 218 214 { 219 215 off_t xn, x; /* position conters */ 220 216 off_t cw; /* character width in bytes */ … … line_pixel_length (unsigned char *t, off_t b, off_t l, gboolean utf8) 235 231 case '\n': 236 232 return b; 237 233 case '\t': 238 xn = next_tab_pos (x );234 xn = next_tab_pos (x, tab_width); 239 235 break; 240 236 default: 241 237 #ifdef HAVE_CHARSET … … word_start (unsigned char *t, off_t q, off_t size) 324 320 /** replaces ' ' with '\n' to properly format a paragraph */ 325 321 326 322 static inline void 327 format_this (unsigned char *t, off_t size, long indent, gboolean utf8)323 format_this (unsigned char *t, off_t size, long indent, int tab_width, gboolean utf8) 328 324 { 329 325 off_t q = 0, ww; 330 326 … … format_this (unsigned char *t, off_t size, long indent, gboolean utf8) 337 333 { 338 334 off_t p; 339 335 340 q = line_pixel_length (t, q, ww, utf8);336 q = line_pixel_length (t, q, ww, tab_width, utf8); 341 337 if (q > size) 342 338 break; 343 339 if (t[q] == '\n') … … edit_indent_width (const WEdit * edit, off_t p) 386 382 static void 387 383 edit_insert_indent (WEdit * edit, long indent) 388 384 { 385 int tab_size = edit_tab_spacing(edit, FALSE); 389 386 if (!option_fill_tabs_with_spaces) 390 while (indent >= TAB_SIZE)387 while (indent >= tab_size) 391 388 { 392 389 edit_insert (edit, '\t'); 393 indent -= TAB_SIZE;390 indent -= tab_size; 394 391 } 395 392 396 393 while (indent-- > 0) … … format_paragraph (WEdit * edit, gboolean force) 513 510 #ifdef HAVE_CHARSET 514 511 utf8 = edit->utf8; 515 512 #endif 516 format_this (t2, q - p, indent, utf8);513 format_this (t2, q - p, indent, edit_tab_spacing(edit, FALSE), utf8); 517 514 put_paragraph (edit, t2, p, indent, size); 518 515 g_free ((char *) t2); 519 516 … … format_paragraph (WEdit * edit, gboolean force) 522 519 } 523 520 524 521 /* --------------------------------------------------------------------------------------------- */ 522 523 /* vim: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */