Advertisement
tomkiewicz

mcedit-vim-modeline

Aug 31st, 2013
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 9.81 KB | None | 0 0
  1. diff --git a/.gitignore b/.gitignore
  2. index 310c62f..9e8b9e7 100644
  3. --- a/.gitignore
  4. +++ b/.gitignore
  5. @@ -46,3 +46,12 @@ make.log
  6.  make.clang
  7.  make.gcc
  8.  make.tcc
  9. +misc/ext.d/doc.sh
  10. +misc/ext.d/misc.sh
  11. +misc/ext.d/text.sh
  12. +misc/ext.d/web.sh
  13. +misc/syntax/Syntax
  14. +src/man2hlp/man2hlp
  15. +src/vfs/extfs/helpers/uc1541
  16. +src/vfs/extfs/helpers/ulib
  17. +tests/src/editor/test-data.txt
  18. diff --git a/src/editor/edit.c b/src/editor/edit.c
  19. index 1c80466..1fe0a53 100644
  20. --- a/src/editor/edit.c
  21. +++ b/src/editor/edit.c
  22. @@ -142,6 +142,23 @@ static const off_t option_filesize_default_threshold = 64 * 1024 * 1024;
  23.  /* --------------------------------------------------------------------------------------------- */
  24.  /*** file scope functions ************************************************************************/
  25.  /* --------------------------------------------------------------------------------------------- */
  26. +
  27. +static gboolean
  28. +edit_fake_half_tabs(WEdit *edit)
  29. +{
  30. +    if (edit->force_halftabs == -1)
  31. +        return option_fake_half_tabs;
  32. +    return edit->force_halftabs;
  33. +}
  34. +
  35. +static gboolean
  36. +edit_fill_tabs_with_spaces(WEdit *edit)
  37. +{
  38. +    if (edit->force_fill_tabs_with_spaces == -1)
  39. +        return option_fill_tabs_with_spaces;
  40. +    return edit->force_fill_tabs_with_spaces;
  41. +}
  42. +
  43.  /**
  44.   * Load file OR text into buffers.  Set cursor to the beginning of file.
  45.   *
  46. @@ -1393,12 +1410,12 @@ insert_spaces_tab (WEdit * edit, gboolean half)
  47.  static inline void
  48.  edit_tab_cmd (WEdit * edit)
  49.  {
  50. -    if (option_fake_half_tabs && is_in_indent (&edit->buffer))
  51. +    if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer))
  52.      {
  53.          /* insert a half tab (usually four spaces) unless there is a
  54.             half tab already behind, then delete it and insert a
  55.             full tab. */
  56. -        if (option_fill_tabs_with_spaces || !right_of_four_spaces (edit))
  57. +        if (edit_fill_tabs_with_spaces(edit) || !right_of_four_spaces (edit))
  58.              insert_spaces_tab (edit, TRUE);
  59.          else
  60.          {
  61. @@ -1409,7 +1426,7 @@ edit_tab_cmd (WEdit * edit)
  62.              edit_insert (edit, '\t');
  63.          }
  64.      }
  65. -    else if (option_fill_tabs_with_spaces)
  66. +    else if (edit_fill_tabs_with_spaces(edit))
  67.          insert_spaces_tab (edit, FALSE);
  68.      else
  69.          edit_insert (edit, '\t');
  70. @@ -1545,8 +1562,8 @@ edit_move_block_to_right (WEdit * edit)
  71.          edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
  72.          if (!edit_line_is_blank (edit, edit->buffer.curs_line))
  73.          {
  74. -            if (option_fill_tabs_with_spaces)
  75. -                insert_spaces_tab (edit, option_fake_half_tabs);
  76. +            if (edit_fill_tabs_with_spaces(edit))
  77. +                insert_spaces_tab (edit, edit_fake_half_tabs(edit));
  78.              else
  79.                  edit_insert (edit, '\t');
  80.              edit_cursor_move (edit,
  81. @@ -1585,7 +1602,7 @@ edit_move_block_to_left (WEdit * edit)
  82.  
  83.          edit_cursor_move (edit, cur_bol - edit->buffer.curs1);
  84.  
  85. -        if (option_fake_half_tabs)
  86. +        if (edit_fake_half_tabs(edit))
  87.              del_tab_width = HALF_TAB_SIZE;
  88.          else
  89.              del_tab_width = option_tab_spacing;
  90. @@ -2017,6 +2034,108 @@ edit_insert_file (WEdit * edit, const vfs_path_t * filename_vpath)
  91.   * cursor on that line and show it in the middle of the screen.
  92.   */
  93.  
  94. +static off_t
  95. +mc_findinfile(int handle, const char *searchstr)
  96. +{
  97. +    off_t offset_bak, offset_cur, filesize;
  98. +    char buff[10240];
  99. +    ssize_t got;
  100. +    const char *found;
  101. +    size_t searchstr_len;
  102. +
  103. +    searchstr_len = strlen(searchstr);
  104. +
  105. +    offset_bak = mc_lseek(handle, 0, SEEK_CUR);
  106. +    if (offset_bak < 0)
  107. +        return -1;
  108. +
  109. +    filesize = mc_lseek(handle, 0, SEEK_END);
  110. +    offset_cur = mc_lseek(handle, offset_bak, SEEK_SET);
  111. +
  112. +    do {
  113. +        got = mc_read(handle, buff, sizeof(buff) - 1);
  114. +        if (got <= 0)
  115. +            return -1;
  116. +        buff[got] = '\0';
  117. +
  118. +        found = strstr(buff, searchstr);
  119. +        if (found)
  120. +            return mc_lseek(handle, offset_cur + (found - buff), SEEK_SET);
  121. +
  122. +        offset_cur = mc_lseek(handle,
  123. +            offset_cur + (sizeof(buff) - 1) - searchstr_len, SEEK_SET);
  124. +    } while (offset_cur + searchstr_len < filesize);
  125. +
  126. +    return -1;
  127. +}
  128. +
  129. +static void
  130. +scan_modeline(WEdit *edit, const vfs_path_t *filename)
  131. +{
  132. +    int fd;
  133. +    int conf_tabstop = -1;
  134. +    int conf_softtabstop = -1;
  135. +    int conf_expandtab = -1;
  136. +    int dst_halftabs = -1;
  137. +    int dst_fill_tabs_with_spaces = -1;
  138. +
  139. +    fd = mc_open(filename, O_RDONLY | O_BINARY);
  140. +    while (1) {
  141. +        off_t foundat;
  142. +        char buff[200];
  143. +        char *s;
  144. +        ssize_t got;
  145. +        const char *foundstr;
  146. +
  147. +        foundat = mc_findinfile(fd, "\n/*");
  148. +        if (foundat < 0)
  149. +            return;
  150. +
  151. +        got = mc_read(fd, buff, sizeof(buff) - 1);
  152. +        if (got <= 0)
  153. +            return;
  154. +        buff[got] = '\0';
  155. +
  156. +        mc_lseek(fd, foundat + 3, SEEK_SET);
  157. +
  158. +        s = &buff[3];
  159. +        while (*s == ' ' || *s == '\t' || *s == '\n' || *s == '\r')
  160. +            s++;
  161. +
  162. +        if (strncmp(s, "vim:", 4) != 0)
  163. +            continue;
  164. +        s += 4;
  165. +
  166. +        foundstr = strstr(s, "tabstop=");
  167. +        if (foundstr)
  168. +            conf_tabstop=atoi(foundstr + 8);
  169. +        foundstr = strstr(s, "softtabstop=");
  170. +        if (foundstr)
  171. +            conf_softtabstop=atoi(foundstr + 12);
  172. +        foundstr = strstr(s, "expandtab");
  173. +        if (foundstr)
  174. +            conf_expandtab = 1;
  175. +        foundstr = strstr(s, "noexpandtab");
  176. +        if (foundstr)
  177. +            conf_expandtab = 0;
  178. +
  179. +        break;
  180. +    }
  181. +
  182. +    if (conf_softtabstop > 0 && conf_tabstop > 0 &&
  183. +        conf_softtabstop * 2 == conf_tabstop)
  184. +    {
  185. +        dst_halftabs = 1;
  186. +    }
  187. +
  188. +    dst_fill_tabs_with_spaces = conf_expandtab;
  189. +
  190. +    edit->force_halftabs = dst_halftabs;
  191. +    edit->force_fill_tabs_with_spaces = dst_fill_tabs_with_spaces;
  192. +
  193. +    mc_close(fd);
  194. +}
  195. +
  196.  WEdit *
  197.  edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * filename_vpath,
  198.             long line)
  199. @@ -2054,6 +2173,11 @@ edit_init (WEdit * edit, int y, int x, int lines, int cols, const vfs_path_t * f
  200.          edit_save_size (edit);
  201.      }
  202.  
  203. +    edit->force_halftabs = -1;
  204. +    edit->force_fill_tabs_with_spaces = -1;
  205. +
  206. +    scan_modeline(edit, filename_vpath);
  207. +
  208.      edit->drag_state = MCEDIT_DRAG_NORMAL;
  209.  
  210.      edit->stat1.st_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
  211. @@ -2909,7 +3033,7 @@ edit_move_to_prev_col (WEdit * edit, off_t p)
  212.      else
  213.      {
  214.          edit->over_col = 0;
  215. -        if (option_fake_half_tabs && is_in_indent (&edit->buffer))
  216. +        if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer))
  217.          {
  218.              long fake_half_tabs;
  219.  
  220. @@ -3402,7 +3526,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
  221.              while (edit_buffer_get_previous_byte (&edit->buffer) != '\n' && edit->buffer.curs1 > 0)
  222.                  edit_backspace (edit, TRUE);
  223.          }
  224. -        else if (option_fake_half_tabs && is_in_indent (&edit->buffer)
  225. +        else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer)
  226.                   && right_of_four_spaces (edit))
  227.          {
  228.              int i;
  229. @@ -3410,6 +3534,16 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
  230.              for (i = 0; i < HALF_TAB_SIZE; i++)
  231.                  edit_backspace (edit, TRUE);
  232.          }
  233. +        else if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer)
  234. +                 && is_aligned_on_a_tab(edit)) {
  235. +            int i;
  236. +
  237. +            edit_backspace (edit, TRUE);
  238. +            for (i = 0; i < HALF_TAB_SIZE; i++)
  239. +                edit_insert (edit, ' ');
  240. +            /* TODO: if (right_of_four_spaces(edit) && left_of_four_spaces(edit)
  241. +             * - replace spaces with a tab */
  242. +        }
  243.          else
  244.              edit_backspace (edit, FALSE);
  245.          break;
  246. @@ -3422,7 +3556,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
  247.              if (option_cursor_beyond_eol && edit->over_col > 0)
  248.                  edit_insert_over (edit);
  249.  
  250. -            if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
  251. +            if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
  252.              {
  253.                  int i;
  254.  
  255. @@ -3488,7 +3622,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
  256.          edit->column_highlight = 1;
  257.      case CK_Left:
  258.      case CK_MarkLeft:
  259. -        if (option_fake_half_tabs && is_in_indent (&edit->buffer) && right_of_four_spaces (edit))
  260. +        if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && right_of_four_spaces (edit))
  261.          {
  262.              if (option_cursor_beyond_eol && edit->over_col > 0)
  263.                  edit->over_col--;
  264. @@ -3503,7 +3637,7 @@ edit_execute_cmd (WEdit * edit, unsigned long command, int char_for_insertion)
  265.          edit->column_highlight = 1;
  266.      case CK_Right:
  267.      case CK_MarkRight:
  268. -        if (option_fake_half_tabs && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
  269. +        if (edit_fake_half_tabs(edit) && is_in_indent (&edit->buffer) && left_of_four_spaces (edit))
  270.          {
  271.              edit_cursor_move (edit, HALF_TAB_SIZE);
  272.              edit->force &= (0xFFF - REDRAW_CHAR_ONLY);
  273. @@ -3964,3 +4098,5 @@ edit_move_down (WEdit * edit, long i, gboolean do_scroll)
  274.  }
  275.  
  276.  /* --------------------------------------------------------------------------------------------- */
  277. +
  278. +/* vim: set tabstop=8 softtabstop=4 shiftwidth=4 expandtab: */
  279. diff --git a/src/editor/editwidget.h b/src/editor/editwidget.h
  280. index 90c73f6..1b45cfb 100644
  281. --- a/src/editor/editwidget.h
  282. +++ b/src/editor/editwidget.h
  283. @@ -163,6 +163,9 @@ struct WEdit
  284.      /* line break */
  285.      LineBreaks lb;
  286.      gboolean extmod;
  287. +
  288. +    int force_halftabs;
  289. +    int force_fill_tabs_with_spaces;
  290.  };
  291.  
  292.  /*** global variables defined in .c file *********************************************************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement