On Wed, Jun 20, 2012 at 05:13:53PM -0700, Tor Perkins wrote: > > [ ... ] > > I've attached a patch that I think works a bit better. It's a bit > of a band-aid, because ideally, I'd like to see the comment case act > like the non-comment case, e.g. both like so (my "ideal" scenario): > > 1 xxxxx > foobar > > # 1 xxxxx > # foobar > > [ ... ]
I've attached my preferred patch and please disregard the "band-aid" patch found in my previous message... This patch delivers the "ideal" behavior described above and it's not just a quick fix. There are three parts to the patch. First, the 7.3.552 update left get_number_indent() in a bit of a messy state because it added a new "comment aware" section, but retained the previous code as well. In fact, the problem at the core of this "Issue 65" was caused by using the new code at the wrong time... I believe that the new code in get_number_indent() can handle all cases, so this allowed for a major cleanup of that function... Second, the code in internal_format() that handles auto-wrap for numbered lists is now "comment aware". The change is small and pretty good I think (feedback welcome). Finally, I've added a new test case for auto-wrap with numbered lists (both with and without comments). - Tor -- You received this message from the "vim_dev" maillist. Do not top-post! Type your reply below the text you are replying to. For more information, visit http://www.vim.org/maillist.php
diff -p ./src/edit.c.org ./src/edit.c *** ./src/edit.c.org Wed Jun 13 05:44:48 2012 --- ./src/edit.c Wed Jun 13 07:22:04 2012 *************** internal_format(textwidth, second_indent *** 6320,6331 **** if (!(flags & INSCHAR_COM_LIST)) { /* ! * This section is for numeric lists w/o comments. If comment ! * indents are needed with numeric lists (formatoptions=nq), ! * then the INSCHAR_COM_LIST flag will cause the corresponding ! * OPENLINE_COM_LIST flag to be passed through to open_line() ! * (as seen above)... ! */ if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) second_indent = get_number_indent(curwin->w_cursor.lnum -1); if (second_indent >= 0) --- 6320,6330 ---- if (!(flags & INSCHAR_COM_LIST)) { /* ! * This section is for auto-wrap of numeric lists. When not in ! * insert mode (i.e. format_lines()), the INSCHAR_COM_LIST flag ! * will be set and open_line() will handle it (as seen above). ! * The code here (and in get_number_indent()) will recognize ! * comments if needed... */ if (second_indent < 0 && has_format_option(FO_Q_NUMBER)) second_indent = get_number_indent(curwin->w_cursor.lnum -1); if (second_indent >= 0) *************** internal_format(textwidth, second_indent *** 6336,6342 **** FALSE, NUL, TRUE); else #endif ! (void)set_indent(second_indent, SIN_CHANGED); } } first_line = FALSE; --- 6335,6363 ---- FALSE, NUL, TRUE); else #endif ! #ifdef FEAT_COMMENTS ! if ( ( leader_len > 0 ) && ! ( second_indent - leader_len > 0 ) ) { ! int i; ! int padding = second_indent - leader_len; ! ! /* We started at the first_line of a numbered list that has ! * a comment. the open_line() function has inserted the ! * proper comment leader and positioned the cursor at the ! * end of the split line. Now we add the additional ! * whitespace needed after the comment leader for the ! * numbered list. */ ! for (i = 0; i < padding; i++) ! { ! ins_str(" "); ! changed_bytes(curwin->w_cursor.lnum, leader_len); ! } ! } else { ! #endif ! (void)set_indent(second_indent, SIN_CHANGED); ! #ifdef FEAT_COMMENTS ! } ! #endif } } first_line = FALSE; diff -p ./src/misc1.c.org ./src/misc1.c *** ./src/misc1.c.org Wed Jun 13 05:44:48 2012 --- ./src/misc1.c Wed Jun 13 07:05:02 2012 *************** get_number_indent(lnum) *** 424,494 **** colnr_T col; pos_T pos; if (lnum > curbuf->b_ml.ml_line_count) return -1; pos.lnum = 0; #ifdef FEAT_COMMENTS ! if (has_format_option(FO_Q_COMS) && has_format_option(FO_Q_NUMBER)) { ! regmatch_T regmatch; ! int lead_len; /* length of comment leader */ ! ! lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); ! regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); ! if (regmatch.regprog != NULL) ! { ! regmatch.rm_ic = FALSE; ! ! /* vim_regexec() expects a pointer to a line. This lets us ! * start matching for the flp beyond any comment leader... */ ! if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) ! { ! pos.lnum = lnum; ! pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); ! #ifdef FEAT_VIRTUALEDIT ! pos.coladd = 0; ! #endif ! } ! } ! vim_free(regmatch.regprog); } - else - { - /* - * What follows is the orig code that is not "comment aware"... - * - * I'm not sure if regmmatch_T (multi-match) is needed in this case. - * It may be true that this section would work properly using the - * regmatch_T code above, in which case, these two separate sections - * should be consolidated w/ FEAT_COMMENTS making lead_len > 0... - */ #endif ! regmmatch_T regmatch; ! ! regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); ! if (regmatch.regprog != NULL) ! { ! regmatch.rmm_ic = FALSE; ! regmatch.rmm_maxcol = 0; ! if (vim_regexec_multi(®match, curwin, curbuf, ! lnum, (colnr_T)0, NULL)) ! { ! pos.lnum = regmatch.endpos[0].lnum + lnum; ! pos.col = regmatch.endpos[0].col; #ifdef FEAT_VIRTUALEDIT ! pos.coladd = 0; #endif ! } ! vim_free(regmatch.regprog); ! } ! #ifdef FEAT_COMMENTS } ! #endif if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) ! return -1; getvcol(curwin, &pos, &col, NULL, NULL); return (int)col; } --- 424,463 ---- colnr_T col; pos_T pos; + regmatch_T regmatch; + int lead_len = 0; /* length of comment leader */ + if (lnum > curbuf->b_ml.ml_line_count) return -1; pos.lnum = 0; #ifdef FEAT_COMMENTS ! /* In format_lines() (i.e. not insert mode), fo+=q is needed too... */ ! if ((State & INSERT) || has_format_option(FO_Q_COMS)) { ! lead_len = get_leader_len(ml_get(lnum), NULL, FALSE, TRUE); } #endif ! regmatch.regprog = vim_regcomp(curbuf->b_p_flp, RE_MAGIC); ! if (regmatch.regprog != NULL) ! { ! regmatch.rm_ic = FALSE; ! /* vim_regexec() expects a pointer to a line. This lets us ! * start matching for the flp beyond any comment leader... */ ! if (vim_regexec(®match, ml_get(lnum) + lead_len, (colnr_T)0)) ! { ! pos.lnum = lnum; ! pos.col = (colnr_T)(*regmatch.endp - ml_get(lnum)); #ifdef FEAT_VIRTUALEDIT ! pos.coladd = 0; #endif ! } } ! vim_free(regmatch.regprog); if (pos.lnum == 0 || *ml_get_pos(&pos) == NUL) ! return -1; getvcol(curwin, &pos, &col, NULL, NULL); return (int)col; } diff -p ./src/testdir/test68.in.org ./src/testdir/test68.in *** ./src/testdir/test68.in.org Mon Jun 11 12:24:11 2012 --- ./src/testdir/test68.in Wed Jun 13 05:45:34 2012 *************** a b *** 52,57 **** --- 52,68 ---- STARTTEST /^{/+1 + :set tw=5 fo=tcn comments=:# + A bjA b + ENDTEST + + { + 1 a + # 1 a + } + + STARTTEST + /^{/+1 :set tw=5 fo=qn comments=:# gwap ENDTEST diff -p ./src/testdir/test68.ok.org ./src/testdir/test68.ok *** ./src/testdir/test68.ok.org Mon Jun 11 12:24:11 2012 --- ./src/testdir/test68.ok Wed Jun 13 05:45:34 2012 *************** a b *** 35,40 **** --- 35,48 ---- { + 1 a + b + # 1 a + # b + } + + + { # 1 a # b }