+static _rl_vimotion_cxt *
+_rl_mvcxt_alloc (int op, int key)
+{
+ _rl_vimotion_cxt *m;
+
+ m = xmalloc (sizeof (_rl_vimotion_cxt));
+ _rl_mvcxt_init (m, op, key);
+ return m;
+}
+
+static void
+_rl_mvcxt_dispose (_rl_vimotion_cxt *m)
+{
+ xfree (m);
+}
+
+static int
+rl_domove_motion_callback (_rl_vimotion_cxt *m)
+{
+ int c;
+
+ _rl_vi_last_motion = c = m->motion;
+
+ /* Append a blank character temporarily so that the motion routines
+ work right at the end of the line. Original value of rl_end is saved
+ as m->end. */
+ rl_line_buffer[rl_end++] = ' ';
+ rl_line_buffer[rl_end] = '\0';
+
+ _rl_dispatch (c, _rl_keymap);
+
+#if defined (READLINE_CALLBACKS)
+ if (RL_ISSTATE (RL_STATE_CALLBACK))
+ {
+ /* Messy case where char search can be vi motion command; see rest of
+ details in callback.c. vi_char_search and callback_char_search just
+ set and unset the CHARSEARCH state. This is where any vi motion
+ command that needs to set its own state should be handled, with any
+ corresponding code to manage that state in callback.c */
+ if (RL_ISSTATE (RL_STATE_CHARSEARCH))
+ return 0;
+ else
+ return (_rl_vi_domove_motion_cleanup (c, m));
+ }
+#endif
+
+ return (_rl_vi_domove_motion_cleanup (c, m));
+}
+
+int
+_rl_vi_domove_motion_cleanup (int c, _rl_vimotion_cxt *m)
+{
+ int r;
+
+ /* Remove the blank that we added in rl_domove_motion_callback. */
+ rl_end = m->end;
+ rl_line_buffer[rl_end] = '\0';
+ if (rl_point > rl_end)
+ rl_point = rl_end;
+
+ /* No change in position means the command failed. */
+ if (rl_mark == rl_point)
+ {
+ RL_UNSETSTATE (RL_STATE_VIMOTION);
+ return (-1);
+ }
+
+ /* rl_vi_f[wW]ord () leaves the cursor on the first character of the next
+ word. If we are not at the end of the line, and we are on a
+ non-whitespace character, move back one (presumably to whitespace). */
+ if ((_rl_to_upper (c) == 'W') && rl_point < rl_end && rl_point > rl_mark &&
+ !whitespace (rl_line_buffer[rl_point]))
+ rl_point--; /* XXX */
+
+ /* If cw or cW, back up to the end of a word, so the behaviour of ce
+ or cE is the actual result. Brute-force, no subtlety. */
+ if (m->key == 'c' && rl_point >= rl_mark && (_rl_to_upper (c) == 'W'))
+ {
+ /* Don't move farther back than where we started. */
+ while (rl_point > rl_mark && whitespace (rl_line_buffer[rl_point]))
+ rl_point--;