Ticket #3068: mcedit-vim-modeline-1.patch
File mcedit-vim-modeline-1.patch, 9.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.c
diff --git a/src/editor/edit.c b/src/editor/edit.c index 1c80466..1fe0a53 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 145 162 /** 146 163 * Load file OR text into buffers. Set cursor to the beginning of file. 147 164 * … … insert_spaces_tab (WEdit * edit, gboolean half) 1393 1410 static inline void 1394 1411 edit_tab_cmd (WEdit * edit) 1395 1412 { 1396 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer))1413 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer)) 1397 1414 { 1398 1415 /* insert a half tab (usually four spaces) unless there is a 1399 1416 half tab already behind, then delete it and insert a 1400 1417 full tab. */ 1401 if ( option_fill_tabs_with_spaces|| !right_of_four_spaces (edit))1418 if (edit_fill_tabs_with_spaces(edit) || !right_of_four_spaces (edit)) 1402 1419 insert_spaces_tab (edit, TRUE); 1403 1420 else 1404 1421 { … … edit_tab_cmd (WEdit * edit) 1409 1426 edit_insert (edit, '\t'); 1410 1427 } 1411 1428 } 1412 else if ( option_fill_tabs_with_spaces)1429 else if (edit_fill_tabs_with_spaces(edit)) 1413 1430 insert_spaces_tab (edit, FALSE); 1414 1431 else 1415 1432 edit_insert (edit, '\t'); … … edit_move_block_to_right (WEdit * edit) 1545 1562 edit_cursor_move (edit, cur_bol - edit->buffer.curs1); 1546 1563 if (!edit_line_is_blank (edit, edit->buffer.curs_line)) 1547 1564 { 1548 if ( option_fill_tabs_with_spaces)1549 insert_spaces_tab (edit, option_fake_half_tabs);1565 if (edit_fill_tabs_with_spaces(edit)) 1566 insert_spaces_tab (edit, edit_fake_half_tabs(edit)); 1550 1567 else 1551 1568 edit_insert (edit, '\t'); 1552 1569 edit_cursor_move (edit, … … edit_move_block_to_left (WEdit * edit) 1585 1602 1586 1603 edit_cursor_move (edit, cur_bol - edit->buffer.curs1); 1587 1604 1588 if ( option_fake_half_tabs)1605 if (edit_fake_half_tabs(edit)) 1589 1606 del_tab_width = HALF_TAB_SIZE; 1590 1607 else 1591 1608 del_tab_width = option_tab_spacing; … … edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath) 2017 2034 * cursor on that line and show it in the middle of the screen. 2018 2035 */ 2019 2036 2037 static off_t 2038 mc_findinfile(int handle, const char *searchstr) 2039 { 2040 off_t offset_bak, offset_cur, filesize; 2041 char buff[10240]; 2042 ssize_t got; 2043 const char *found; 2044 size_t searchstr_len; 2045 2046 searchstr_len = strlen(searchstr); 2047 2048 offset_bak = mc_lseek(handle, 0, SEEK_CUR); 2049 if (offset_bak < 0) 2050 return -1; 2051 2052 filesize = mc_lseek(handle, 0, SEEK_END); 2053 offset_cur = mc_lseek(handle, offset_bak, SEEK_SET); 2054 2055 do { 2056 got = mc_read(handle, buff, sizeof(buff) - 1); 2057 if (got <= 0) 2058 return -1; 2059 buff[got] = '\0'; 2060 2061 found = strstr(buff, searchstr); 2062 if (found) 2063 return mc_lseek(handle, offset_cur + (found - buff), SEEK_SET); 2064 2065 offset_cur = mc_lseek(handle, 2066 offset_cur + (sizeof(buff) - 1) - searchstr_len, SEEK_SET); 2067 } while (offset_cur + searchstr_len < filesize); 2068 2069 return -1; 2070 } 2071 2072 static void 2073 scan_modeline(WEdit *edit, const vfs_path_t *filename) 2074 { 2075 int fd; 2076 int conf_tabstop = -1; 2077 int conf_softtabstop = -1; 2078 int conf_expandtab = -1; 2079 int dst_halftabs = -1; 2080 int dst_fill_tabs_with_spaces = -1; 2081 2082 fd = mc_open(filename, O_RDONLY | O_BINARY); 2083 while (1) { 2084 off_t foundat; 2085 char buff[200]; 2086 char *s; 2087 ssize_t got; 2088 const char *foundstr; 2089 2090 foundat = mc_findinfile(fd, "\n/*"); 2091 if (foundat < 0) 2092 return; 2093 2094 got = mc_read(fd, buff, sizeof(buff) - 1); 2095 if (got <= 0) 2096 return; 2097 buff[got] = '\0'; 2098 2099 mc_lseek(fd, foundat + 3, SEEK_SET); 2100 2101 s = &buff[3]; 2102 while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r') 2103 s++; 2104 2105 if (strncmp(s, "vim:", 4) != 0) 2106 continue; 2107 s += 4; 2108 2109 foundstr = strstr(s, "tabstop="); 2110 if (foundstr) 2111 conf_tabstop=atoi(foundstr + 8); 2112 foundstr = strstr(s, "softtabstop="); 2113 if (foundstr) 2114 conf_softtabstop=atoi(foundstr + 12); 2115 foundstr = strstr(s, "expandtab"); 2116 if (foundstr) 2117 conf_expandtab = 1; 2118 foundstr = strstr(s, "noexpandtab"); 2119 if (foundstr) 2120 conf_expandtab = 0; 2121 2122 break; 2123 } 2124 2125 if (conf_softtabstop > 0 && conf_tabstop > 0 && 2126 conf_softtabstop * 2 == conf_tabstop) 2127 { 2128 dst_halftabs = 1; 2129 } 2130 2131 dst_fill_tabs_with_spaces = conf_expandtab; 2132 2133 edit->force_halftabs = dst_halftabs; 2134 edit->force_fill_tabs_with_spaces = dst_fill_tabs_with_spaces; 2135 2136 mc_close(fd); 2137 } 2138 2020 2139 WEdit * 2021 2140 edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath, 2022 2141 long line) … … edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * f 2054 2173 edit_save_size (edit); 2055 2174 } 2056 2175 2176 edit->force_halftabs = -1; 2177 edit->force_fill_tabs_with_spaces = -1; 2178 2179 scan_modeline(edit, filename_vpath); 2180 2057 2181 edit->drag_state = MCEDIT_DRAG_NORMAL; 2058 2182 2059 2183 edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH; … … edit_move_to_prev_col (WEdit * edit, off_t p) 2909 3033 else 2910 3034 { 2911 3035 edit->over_col = 0; 2912 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer))3036 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer)) 2913 3037 { 2914 3038 long fake_half_tabs; 2915 3039 … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3402 3526 while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0) 3403 3527 edit_backspace (edit, TRUE); 3404 3528 } 3405 else if ( option_fake_half_tabs&& is_in_indent (&edit->buffer)3529 else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) 3406 3530 && right_of_four_spaces (edit)) 3407 3531 { 3408 3532 int i; … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3410 3534 for (i = 0; i < HALF_TAB_SIZE; i++) 3411 3535 edit_backspace (edit, TRUE); 3412 3536 } 3537 else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) 3538 && is_aligned_on_a_tab(edit)) { 3539 int i; 3540 3541 edit_backspace (edit, TRUE); 3542 for (i = 0; i < HALF_TAB_SIZE; i++) 3543 edit_insert (edit, ' '); 3544 /* TODO: if (right_of_four_spaces(edit) && left_of_four_spaces(edit) 3545 * - replace spaces with a tab */ 3546 } 3413 3547 else 3414 3548 edit_backspace (edit, FALSE); 3415 3549 break; … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3422 3556 if (option_cursor_beyond_eol && edit->over_col > 0) 3423 3557 edit_insert_over (edit); 3424 3558 3425 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && left_of_four_spaces (edit))3559 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit)) 3426 3560 { 3427 3561 int i; 3428 3562 … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3488 3622 edit->column_highlight = 1; 3489 3623 case CK_Left: 3490 3624 case CK_MarkLeft: 3491 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && right_of_four_spaces (edit))3625 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && right_of_four_spaces (edit)) 3492 3626 { 3493 3627 if (option_cursor_beyond_eol && edit->over_col > 0) 3494 3628 edit->over_col--; … … edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion) 3503 3637 edit->column_highlight = 1; 3504 3638 case CK_Right: 3505 3639 case CK_MarkRight: 3506 if ( option_fake_half_tabs&& is_in_indent (&edit->buffer) && left_of_four_spaces (edit))3640 if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit)) 3507 3641 { 3508 3642 edit_cursor_move (edit, HALF_TAB_SIZE); 3509 3643 edit->force &= (0xFFF - REDRAW_CHAR_ONLY); … … edit_move_down (WEdit * edit, long i, gboolean do_scroll) 3964 4098 } 3965 4099 3966 4100 /* --------------------------------------------------------------------------------------------- */ 4101 4102 /* vim: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */ -
src/editor/editwidget.h
diff --git a/src/editor/editwidget.h b/src/editor/editwidget.h index 90c73f6..1b45cfb 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; 166 169 }; 167 170 168 171 /*** global variables defined in .c file *********************************************************/