Ticket #3068: mcedit-vim-modeline-3.patch
File mcedit-vim-modeline-3.patch, 18.8 KB (added by twasilczyk, 11 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..4de00f0 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 char *searchstr) 2045 { 2046 off_t offset_bak, offset_cur, filesize; 2047 char buff[10240]; 2048 ssize_t got; 2049 const char *found; 2050 size_t searchstr_len; 2051 2052 searchstr_len = strlen(searchstr); 2053 2054 offset_bak = mc_lseek(handle, 0, SEEK_CUR); 2055 if (offset_bak < 0) 2056 return -1; 2057 2058 filesize = mc_lseek(handle, 0, SEEK_END); 2059 offset_cur = mc_lseek(handle, offset_bak, SEEK_SET); 2060 2061 do { 2062 got = mc_read(handle, buff, sizeof(buff) - 1); 2063 if (got <= 0) 2064 return -1; 2065 buff[got] = '\0'; 2066 2067 found = strstr(buff, searchstr); 2068 if (found) 2069 return mc_lseek(handle, offset_cur + (found - buff), SEEK_SET); 2070 2071 offset_cur = mc_lseek(handle, 2072 offset_cur + (sizeof(buff) - 1) - searchstr_len, SEEK_SET); 2073 } while (offset_cur + searchstr_len < filesize); 2074 2075 return -1; 2076 } 2077 2078 static void 2079 scan_modeline(WEdit *edit, const vfs_path_t *filename) 2080 { 2081 int fd; 2082 int conf_tabstop = -1; 2083 int conf_softtabstop = -1; 2084 int conf_expandtab = -1; 2085 int dst_halftabs = -1; 2086 int dst_fill_tabs_with_spaces = -1; 2087 int dst_force_tab_spacing = -1; 2088 2089 fd = mc_open(filename, O_RDONLY | O_BINARY); 2090 while (1) { 2091 off_t foundat; 2092 char buff[200]; 2093 char *s; 2094 ssize_t got; 2095 const char *foundstr; 2096 2097 foundat = mc_findinfile(fd, "\n/*"); 2098 if (foundat < 0) 2099 return; 2100 2101 got = mc_read(fd, buff, sizeof(buff) - 1); 2102 if (got <= 0) 2103 return; 2104 buff[got] = '\0'; 2105 2106 mc_lseek(fd, foundat + 3, SEEK_SET); 2107 2108 s = &buff[3]; 2109 while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') 2110 s++; 2111 2112 if (strncmp(s, "vim:", 4) != 0) 2113 continue; 2114 s += 4; 2115 2116 foundstr = strstr(s, "tabstop="); 2117 if (foundstr) 2118 conf_tabstop=atoi(foundstr + 8); 2119 foundstr = strstr(s, "softtabstop="); 2120 if (foundstr) 2121 conf_softtabstop=atoi(foundstr + 12); 2122 foundstr = strstr(s, "expandtab"); 2123 if (foundstr) 2124 conf_expandtab = 1; 2125 foundstr = strstr(s, "noexpandtab"); 2126 if (foundstr) 2127 conf_expandtab = 0; 2128 2129 break; 2130 } 2131 2132 if (conf_softtabstop > 0 && conf_tabstop > 0 && 2133 conf_softtabstop * 2 == conf_tabstop) 2134 { 2135 dst_halftabs = 1; 2136 } 2137 2138 dst_fill_tabs_with_spaces = conf_expandtab; 2139 2140 dst_force_tab_spacing = conf_tabstop; 2141 2142 edit->force_halftabs = dst_halftabs; 2143 edit->force_fill_tabs_with_spaces = dst_fill_tabs_with_spaces; 2144 edit->force_tab_spacing = dst_force_tab_spacing; 2145 2146 mc_close(fd); 2147 } 2148 2020 2149 WEdit * 2021 2150 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath, 2022 2151 long line) … … edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * f 2054 2183 edit_save_size (edit); 2055 2184 } 2056 2185 2186 edit->force_halftabs = -1; 2187 edit->force_fill_tabs_with_spaces = -1; 2188 edit->force_tab_spacing = -1; 2189 2190 scan_modeline(edit, filename_vpath); 2191 2057 2192 edit->drag_state = MCEDIT_DRAG_NORMAL; 2058 2193 2059 2194 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 2890 2756 2891 if (c == '\n') 2757 2892 return (upto != 0 ? (off_t) col : p); 2758 if (c == '\t') 2759 col += TAB_SIZE - col % TAB_SIZE; 2893 if (c == '\t') { 2894 int tab_size = edit_tab_spacing(edit, FALSE); 2895 col += tab_size - col % tab_size; 2896 } 2760 2897 else if ((c < 32 || c == 127) && (orig_c == c 2761 2898 #ifdef HAVE_CHARSET 2762 2899 || (!mc_global.utf8_display && !edit->utf8) … … edit_move_to_prev_col (WEdit * edit, off_t p) 2909 3046 else 2910 3047 { 2911 3048 edit->over_col = 0; 2912 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer))3049 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer)) 2913 3050 { 2914 3051 long fake_half_tabs; 2915 3052 2916 3053 edit_update_curs_col (edit); 2917 3054 2918 fake_half_tabs = HALF_TAB_SIZE* space_width;3055 fake_half_tabs = edit_tab_spacing(edit, TRUE) * space_width; 2919 3056 if (fake_half_tabs != 0 && edit->curs_col % fake_half_tabs != 0) 2920 3057 { 2921 3058 int q; … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3402 3539 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0) 3403 3540 edit_backspace (edit, TRUE); 3404 3541 } 3405 else if ( option_fake_half_tabs&& is_in_indent (&edit->buffer)3542 else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) 3406 3543 && right_of_four_spaces (edit)) 3407 3544 { 3408 3545 int i; 3409 3546 3410 for (i = 0; i < HALF_TAB_SIZE; i++)3547 for (i = 0; i < edit_tab_spacing(edit, TRUE); i++) 3411 3548 edit_backspace (edit, TRUE); 3412 3549 } 3550 else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) 3551 && edit_buffer_get_byte(&edit->buffer, edit->buffer.curs1 - 1) == '\t') { 3552 int i; 3553 3554 edit_backspace (edit, TRUE); 3555 for (i = 0; i < edit_tab_spacing(edit, TRUE); i++) 3556 edit_insert (edit, ' '); 3557 /* TODO: if (right_of_four_spaces(edit) && left_of_four_spaces(edit) 3558 * - replace spaces with a tab */ 3559 } 3413 3560 else 3414 3561 edit_backspace (edit, FALSE); 3415 3562 break; … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3422 3569 if (option_cursor_beyond_eol && edit->over_col > 0) 3423 3570 edit_insert_over (edit); 3424 3571 3425 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && left_of_four_spaces (edit))3572 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit)) 3426 3573 { 3427 3574 int i; 3428 3575 3429 for (i = 1; i <= HALF_TAB_SIZE; i++)3576 for (i = 1; i <= edit_tab_spacing(edit, TRUE); i++) 3430 3577 edit_delete (edit, TRUE); 3431 3578 } 3432 3579 else … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3488 3635 edit->column_highlight = 1; 3489 3636 case CK_Left: 3490 3637 case CK_MarkLeft: 3491 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && right_of_four_spaces (edit))3638 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && right_of_four_spaces (edit)) 3492 3639 { 3493 3640 if (option_cursor_beyond_eol && edit->over_col > 0) 3494 3641 edit->over_col--; 3495 3642 else 3496 edit_cursor_move (edit, - HALF_TAB_SIZE);3643 edit_cursor_move (edit, -edit_tab_spacing(edit, TRUE)); 3497 3644 edit->force &= (0xFFF - REDRAW_CHAR_ONLY); 3498 3645 } 3499 3646 else … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3503 3650 edit->column_highlight = 1; 3504 3651 case CK_Right: 3505 3652 case CK_MarkRight: 3506 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && left_of_four_spaces (edit))3653 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit)) 3507 3654 { 3508 edit_cursor_move (edit, HALF_TAB_SIZE);3655 edit_cursor_move (edit, edit_tab_spacing(edit, TRUE)); 3509 3656 edit->force &= (0xFFF - REDRAW_CHAR_ONLY); 3510 3657 } 3511 3658 else … … edit_move_down (WEdit * edit, long i, gboolean do_scroll) 3964 4111 } 3965 4112 3966 4113 /* --------------------------------------------------------------------------------------------- */ 4114 4115 /* 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: */