* Dmitrij D. Czarkoff <czark...@gmail.com> [110721 14:49]:
> Hello!
> 
> I tried both documentation and google, and I could only figure out how to
> enable arrow keys in emacs mode of ksh.
> 
> Is there a way to enable them in vi mode? Same goes for HOME and END buttons.

Try the patch below. 

You probably wonder why cursor movement is done in such a strange way,
reimplementing both domove() and vi_cmd(), but I couldn't find a better
way to go to "end of line + one".

diff --git a/vi.c b/vi.c
index 0bac6be..c1a7b7d 100644
--- a/vi.c
+++ b/vi.c
@@ -247,6 +247,48 @@ x_vi(char *buf, size_t len)
        return es->linelen;
 }
 
+static void keypad(int ch) {
+       int cur = 0;
+       char cmd;
+
+       switch (ch) {
+               case 'D': /* left */
+                       cur--;
+                       break;
+               case 'C': /* right */
+                       cur++;
+                       break;
+               case 'A': /* up */
+                       cmd = 'k';
+                       vi_cmd(1, &cmd);
+                       break;
+               case 'B': /* down */
+                       cmd = 'j';
+                       vi_cmd(1, &cmd);
+                       break;
+               case 'H': /* home */
+                       es->cursor = 0;
+                       break;
+               case 'F': /* end */
+                       es->cursor = es->linelen;
+                       break;
+               default:
+                       vi_error();
+                       return;
+       }
+       if ((cur += es->cursor) >= 0) {
+               if (cur > es->linelen && cur != 0) {
+                       cur--;
+                       vi_error();
+               }
+               es->cursor = cur;
+       } else
+               vi_error();
+
+       refresh(0);
+       return;
+}
+
 static int
 vi_hook(int ch)
 {
@@ -261,6 +303,15 @@ vi_hook(int ch)
                                state = VLIT;
                                ch = '^';
                        }
+
+                       if (ch == '\033') {
+                              if (x_getc() == '[')
+                                      keypad(x_getc());
+                              else /* unknown sequence */
+                                      vi_error();
+                              return 0;
+                       }
+                                                       
                        switch (vi_insert(ch)) {
                        case -1:
                                vi_error();

-- 
Alexander Polakov | plhk.ru

Reply via email to