Friday, January 16, 2015

Re: Any automatic bracket-insertion plugins not breaking undo?

diff --git a/runtime/doc/insert.txt b/runtime/doc/insert.txt
--- a/runtime/doc/insert.txt
+++ b/runtime/doc/insert.txt
@@ -352,8 +352,10 @@ CTRL-G CTRL-J cursor one line down, inse
<Right> cursor one character right *i_<Right>*
<S-Left> cursor one word back (like "b" command) *i_<S-Left>*
<C-Left> cursor one word back (like "b" command) *i_<C-Left>*
+<M-Left> cursor one char left (without breaking undo) *i_<M-Left>*
<S-Right> cursor one word forward (like "w" command) *i_<S-Right>*
<C-Right> cursor one word forward (like "w" command) *i_<C-Right>*
+<M-Right> cursor one char right (without breaking undo)*i_<M-Right>*
<Home> cursor to first char in the line *i_<Home>*
<End> cursor to after last char in the line *i_<End>*
<C-Home> cursor to first char in the file *i_<C-Home>*
diff --git a/src/edit.c b/src/edit.c
--- a/src/edit.c
+++ b/src/edit.c
@@ -193,7 +193,7 @@ static void insert_special __ARGS((int,
static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
static void check_auto_format __ARGS((int));
static void redo_literal __ARGS((int c));
-static void start_arrow __ARGS((pos_T *end_insert_pos));
+static void start_arrow __ARGS((pos_T *end_insert_pos, int change));
#ifdef FEAT_SPELL
static void check_spell_redraw __ARGS((void));
static void spell_back_to_badword __ARGS((void));
@@ -233,11 +233,11 @@ static void ins_mousescroll __ARGS((int
#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
static void ins_tabline __ARGS((int c));
#endif
-static void ins_left __ARGS((void));
+static void ins_left __ARGS((int change));
static void ins_home __ARGS((int c));
static void ins_end __ARGS((int c));
static void ins_s_left __ARGS((void));
-static void ins_right __ARGS((void));
+static void ins_right __ARGS((int change));
static void ins_s_right __ARGS((void));
static void ins_up __ARGS((int startcol));
static void ins_pageup __ARGS((void));
@@ -1228,7 +1228,7 @@ doESCkey:
if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
ins_s_left();
else
- ins_left();
+ ins_left(!(mod_mask & (MOD_MASK_ALT)));
break;

case K_S_LEFT: /* <S-Left> */
@@ -1240,7 +1240,7 @@ doESCkey:
if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
ins_s_right();
else
- ins_right();
+ ins_right(!(mod_mask & (MOD_MASK_ALT)));
break;

case K_S_RIGHT: /* <S-Right> */
@@ -6721,10 +6721,11 @@ redo_literal(c)
* For undo/redo it resembles hitting the <ESC> key.
*/
static void
-start_arrow(end_insert_pos)
+start_arrow(end_insert_pos, change)
pos_T *end_insert_pos; /* can be NULL */
-{
- if (!arrow_used) /* something has been inserted */
+ int change; /* if true, start a new change */
+{
+ if (!arrow_used && change) /* something has been inserted */
{
AppendToRedobuff(ESC_STR);
stop_insert(end_insert_pos, FALSE, FALSE);
@@ -6763,7 +6764,7 @@ spell_back_to_badword()

spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
if (curwin->w_cursor.col != tpos.col)
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
}
#endif

@@ -9176,7 +9177,7 @@ ins_mouse(c)
curbuf = curwin->w_buffer;
}
#endif
- start_arrow(curwin == old_curwin ? &tpos : NULL);
+ start_arrow(curwin == old_curwin ? &tpos : NULL, TRUE);
#ifdef FEAT_WINDOWS
if (curwin != new_curwin && win_valid(new_curwin))
{
@@ -9280,7 +9281,7 @@ ins_mousescroll(dir)

if (!equalpos(curwin->w_cursor, tpos))
{
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
@@ -9298,7 +9299,7 @@ ins_tabline(c)
|| (current_tab != 0 && current_tab != tabpage_index(curtab)))
{
undisplay_dollar();
- start_arrow(&curwin->w_cursor);
+ start_arrow(&curwin->w_cursor, TRUE);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
@@ -9324,7 +9325,7 @@ ins_scroll()
tpos = curwin->w_cursor;
if (gui_do_scroll())
{
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
@@ -9340,7 +9341,7 @@ ins_horscroll()
tpos = curwin->w_cursor;
if (gui_do_horiz_scroll(scrollbar_value, FALSE))
{
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
# ifdef FEAT_CINDENT
can_cindent = TRUE;
# endif
@@ -9349,7 +9350,8 @@ ins_horscroll()
#endif

static void
-ins_left()
+ins_left(change)
+ int change; /* start a new change */
{
pos_T tpos;

@@ -9366,7 +9368,14 @@ ins_left()
* break undo. K_LEFT is inserted in im_correct_cursor(). */
if (!im_is_preediting())
#endif
- start_arrow(&tpos);
+ {
+ start_arrow(&tpos, change);
+ if (!change)
+ {
+ int modifier = MOD_MASK_ALT;
+ AppendCharToRedobuff(simplify_key(K_LEFT, &modifier));
+ }
+ }
#ifdef FEAT_RIGHTLEFT
/* If exit reversed string, position is fixed */
if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
@@ -9381,7 +9390,7 @@ ins_left()
*/
else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
{
- start_arrow(&tpos);
+ start_arrow(&tpos, change);
--(curwin->w_cursor.lnum);
coladvance((colnr_T)MAXCOL);
curwin->w_set_curswant = TRUE; /* so we stay at the end */
@@ -9409,7 +9418,7 @@ ins_home(c)
curwin->w_cursor.coladd = 0;
#endif
curwin->w_curswant = 0;
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
}

static void
@@ -9429,7 +9438,7 @@ ins_end(c)
coladvance((colnr_T)MAXCOL);
curwin->w_curswant = MAXCOL;

- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
}

static void
@@ -9442,7 +9451,7 @@ ins_s_left()
undisplay_dollar();
if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
{
- start_arrow(&curwin->w_cursor);
+ start_arrow(&curwin->w_cursor, TRUE);
(void)bck_word(1L, FALSE, FALSE);
curwin->w_set_curswant = TRUE;
}
@@ -9451,7 +9460,8 @@ ins_s_left()
}

static void
-ins_right()
+ins_right(change)
+ int change; /* start a new change */
{
#ifdef FEAT_FOLDING
if ((fdo_flags & FDO_HOR) && KeyTyped)
@@ -9464,7 +9474,12 @@ ins_right()
#endif
)
{
- start_arrow(&curwin->w_cursor);
+ start_arrow(&curwin->w_cursor, change);
+ if (!change)
+ {
+ int modifier = MOD_MASK_ALT;
+ AppendCharToRedobuff(simplify_key(K_RIGHT, &modifier));
+ }
curwin->w_set_curswant = TRUE;
#ifdef FEAT_VIRTUALEDIT
if (virtual_active())
@@ -9491,7 +9506,7 @@ ins_right()
else if (vim_strchr(p_ww, ']') != NULL
&& curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
{
- start_arrow(&curwin->w_cursor);
+ start_arrow(&curwin->w_cursor, change);
curwin->w_set_curswant = TRUE;
++curwin->w_cursor.lnum;
curwin->w_cursor.col = 0;
@@ -9511,7 +9526,7 @@ ins_s_right()
if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
|| gchar_cursor() != NUL)
{
- start_arrow(&curwin->w_cursor);
+ start_arrow(&curwin->w_cursor, TRUE);
(void)fwd_word(1L, FALSE, 0);
curwin->w_set_curswant = TRUE;
}
@@ -9541,7 +9556,7 @@ ins_up(startcol)
#endif
)
redraw_later(VALID);
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
@@ -9563,7 +9578,7 @@ ins_pageup()
/* <C-PageUp>: tab page back */
if (first_tabpage->tp_next != NULL)
{
- start_arrow(&curwin->w_cursor);
+ start_arrow(&curwin->w_cursor, TRUE);
goto_tabpage(-1);
}
return;
@@ -9573,7 +9588,7 @@ ins_pageup()
tpos = curwin->w_cursor;
if (onepage(BACKWARD, 1L) == OK)
{
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
@@ -9604,7 +9619,7 @@ ins_down(startcol)
#endif
)
redraw_later(VALID);
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
@@ -9626,7 +9641,7 @@ ins_pagedown()
/* <C-PageDown>: tab page forward */
if (first_tabpage->tp_next != NULL)
{
- start_arrow(&curwin->w_cursor);
+ start_arrow(&curwin->w_cursor, TRUE);
goto_tabpage(0);
}
return;
@@ -9636,7 +9651,7 @@ ins_pagedown()
tpos = curwin->w_cursor;
if (onepage(FORWARD, 1L) == OK)
{
- start_arrow(&tpos);
+ start_arrow(&tpos, TRUE);
#ifdef FEAT_CINDENT
can_cindent = TRUE;
#endif
Hi Ben!

On Fr, 16 Jan 2015, Ben Fritz wrote:

> The problem is, there is no way to move the cursor in insert mode,
> without breaking the undo sequence. Such a capability would allow both
> of these mappings:
>
> inore ( ()<Left> inore <expr> ) GetNextChar()==")" ? "\<Right>" : ")"
>
> Alternatively, there is no way in insert mode to insert a character
> after the cursor, or delete the character after the cursor. Such a
> capability would allow:
>
> inore <expr> ( ")".PutCharAfter(")") inore <expr> ) GetNextChar()==")"
> ? DeleteNextChar().")" : ")"
>
> At one point, there was a workaround that exploited a bug in setline()
> that allowed the undo/redo to work. You could use setline() to change
> the line without breaking undo sequence. I don't remember how repeat
> worked, but I think it involved an <Esc> mapping.
>
> If either the abilities above (moving the cursor without breaking
> undo, or insert/delete after the cursor) were implemented as Vim
> insert-mode commands, I would consider dropping the use of a plugin
> and just writing my own mappings.
>
> As it is, I tried the pull request I mentioned on delimitMate, and it
> seems to work for simple cases, but either visual-block mode messes it
> up. I have an unsatisfactory workaround I posted as a comment on that
> pull request that I'm using for the present.

Hm, we already have special cases <C-[Left|Right]> and <S-[Left|Right]>
to move by word|WORD. So how about making <M-Left> and <M-Right> move
without breaking undo?
https://github.com/chrisbra/vim-mq-patches/blob/master/move_cursor_without_breaking_undo


Best,
Christian
--
Wer tugendhaft lebt und handelt, der legt seinen Adel an den Tag.
-- Giovanni Boccaccio, Das Dekameron

--
--
You received this message from the "vim_use" 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

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment