zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: (2) Re: History Up key
       [not found] <000601c0aae0$efafb580$21c9ca95@mow.siemens.ru>
@ 2001-03-12 17:36 ` Peter Stephenson
  2001-03-12 18:37   ` Bart Schaefer
  2001-03-13  7:30   ` PATCH: (2) " Andrej Borsenkow
  0 siblings, 2 replies; 4+ messages in thread
From: Peter Stephenson @ 2001-03-12 17:36 UTC (permalink / raw)
  To: Zsh hackers list

> why not use termcap/terminfo to find esc for arrow keys?

Here we go (instead of the previous patch).

I don't have an exotic enough terminal to test these fully; it works for a
bog-standard xterm.  Note the feature that, as I mentioned, if the
sequences look like \e[? or \eO?, bind the other one as well.

I'm quite prepared to be told that some machines use exotic cursor key
sequences in their termcap values which they don't actually send.

I'd like some indication there are non-standard terminals that this doesn't
wreck horribly before committing it.

Index: Doc/Zsh/zle.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/zle.yo,v
retrieving revision 1.9
diff -u -r1.9 zle.yo
--- Doc/Zsh/zle.yo	2000/09/20 06:58:54	1.9
+++ Doc/Zsh/zle.yo	2001/03/12 17:32:35
@@ -222,7 +222,7 @@
 Move backward one character.
 )
 tindex(vi-backward-char)
-item(tt(vi-backward-char) (unbound) (^H h ^?) (unbound))(
+item(tt(vi-backward-char) (unbound) (^H h ^?) (ESC-[D))(
 Move backward one character, without changing lines.
 )
 tindex(backward-word)
@@ -273,7 +273,7 @@
 Move forward one character.
 )
 tindex(vi-forward-char)
-item(tt(vi-forward-char) (unbound) (space l) (unbound))(
+item(tt(vi-forward-char) (unbound) (space l) (ESC-[C))(
 Move forward one character.
 )
 tindex(vi-find-next-char)
@@ -357,7 +357,7 @@
 Move to the first event in the history list.
 )
 tindex(down-line-or-history)
-item(tt(down-line-or-history) (^N ESC-[B) (j) (unbound))(
+item(tt(down-line-or-history) (^N ESC-[B) (j) (ESC-[B))(
 Move down a line in the buffer, or if already at the bottom line,
 move to the next event in the history list.
 )
@@ -546,7 +546,7 @@
 Repeat the last vi history search, but in reverse.
 )
 tindex(up-line-or-history)
-item(tt(up-line-or-history) (^P ESC-[A) (k) (unbound))(
+item(tt(up-line-or-history) (^P ESC-[A) (k) (ESC-[A))(
 Move up a line in the buffer, or if already at the top line,
 move to the previous event in the history list.
 )
Index: Src/init.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/init.c,v
retrieving revision 1.16
diff -u -r1.16 init.c
--- Src/init.c	2001/01/16 13:44:20	1.16
+++ Src/init.c	2001/03/12 17:32:35
@@ -497,7 +497,8 @@
 static char *tccapnams[TC_COUNT] = {
     "cl", "le", "LE", "nd", "RI", "up", "UP", "do",
     "DO", "dc", "DC", "ic", "IC", "cd", "ce", "al", "dl", "ta",
-    "md", "so", "us", "me", "se", "ue", "ch"
+    "md", "so", "us", "me", "se", "ue", "ch",
+    "ku", "kd", "kl", "kr"
 };
 
 /* Initialise termcap */
Index: Src/zsh.h
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/zsh.h,v
retrieving revision 1.26
diff -u -r1.26 zsh.h
--- Src/zsh.h	2001/03/07 12:58:41	1.26
+++ Src/zsh.h	2001/03/12 17:32:35
@@ -1524,7 +1524,11 @@
 #define TCSTANDOUTEND  22
 #define TCUNDERLINEEND 23
 #define TCHORIZPOS     24
-#define TC_COUNT       25
+#define TCUPCURSOR     25
+#define TCDOWNCURSOR   26
+#define TCLEFTCURSOR   27
+#define TCRIGHTCURSOR  28
+#define TC_COUNT       29
 
 #define tccan(X) (tclen[X])
 
Index: Src/Zle/zle_keymap.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_keymap.c,v
retrieving revision 1.3
diff -u -r1.3 zle_keymap.c
--- Src/Zle/zle_keymap.c	2000/12/18 02:14:57	1.3
+++ Src/Zle/zle_keymap.c	2001/03/12 17:32:35
@@ -1011,6 +1011,52 @@
     zfree(keybuf, keybufsz);
 }
 
+static char *cursorptr;
+
+/* utility function for termcap output routine to add to string */
+
+static int 
+add_cursor_char(int c)
+{
+    *cursorptr++ = c;
+    return 0;
+}
+
+/* interrogate termcap for cursor keys and add bindings to keymap */
+
+/**/
+static void
+add_cursor_key(Keymap km, int tccode, Thingy thingy, int defchar)
+{
+    char buf[2048];
+
+    if (tccan(tccode)) {
+	/*
+	 * We can use the real termcap sequence.  We need to
+	 * persuade termcap to output `move cursor 1 char' and capture it.
+	 */
+	cursorptr = buf;
+	tputs(tcstr[tccode], 1, add_cursor_char);
+	*cursorptr = '\0';
+    } else {
+	/* Assume the normal VT100-like values. */
+	sprintf(buf, "\33[%c", defchar);
+    }
+    bindkey(km, buf, refthingy(thingy), NULL);
+
+    /*
+     * If the string looked like \e[? or \eO?, bind the other one, too.
+     * This is necessary to make cursor keys work on many xterms with
+     * both normal and application modes.
+     */
+    if (buf[0] == '\33' && (buf[1] == '[' || buf[1] == 'O') && 
+	buf[2] && !buf[3])
+    {
+	buf[1] = (buf[1] == '[') ? 'O' : '[';
+	bindkey(km, buf, refthingy(thingy), NULL);
+    }
+}
+
 /* Create the default keymaps.  For efficiency reasons, this function   *
  * assigns directly to the km->first array.  It knows that there are no *
  * prefix bindings in the way, and that it is using a simple keymap.    */
@@ -1023,6 +1069,7 @@
     Keymap emap = newkeymap(NULL, "emacs");
     Keymap amap = newkeymap(NULL, "vicmd");
     Keymap smap = newkeymap(NULL, ".safe");
+    Keymap vimaps[2], kptr;
     char buf[3], *ed;
     int i;
 
@@ -1065,26 +1112,23 @@
 
     /* vt100 arrow keys are bound by default, for historical reasons. *
      * Both standard and keypad modes are supported.                  */
+
+    vimaps[0] = vmap;
+    vimaps[1] = amap;
+    for (i = 0; i < 2; i++) {
+	kptr = vimaps[i];
+	/* vi command and insert modes: arrow keys */
+	add_cursor_key(kptr, TCUPCURSOR, t_uplineorhistory, 'A');
+	add_cursor_key(kptr, TCDOWNCURSOR, t_downlineorhistory, 'B');
+	add_cursor_key(kptr, TCLEFTCURSOR, t_vibackwardchar, 'D');
+	add_cursor_key(kptr, TCRIGHTCURSOR, t_viforwardchar, 'C');
+    }
 
-    /* vi command mode: arrow keys */
-    bindkey(amap, "\33[A",  refthingy(t_uplineorhistory), NULL);
-    bindkey(amap, "\33[B",  refthingy(t_downlineorhistory), NULL);
-    bindkey(amap, "\33[C",  refthingy(t_viforwardchar), NULL);
-    bindkey(amap, "\33[D",  refthingy(t_vibackwardchar), NULL);
-    bindkey(amap, "\33OA",  refthingy(t_uplineorhistory), NULL);
-    bindkey(amap, "\33OB",  refthingy(t_downlineorhistory), NULL);
-    bindkey(amap, "\33OC",  refthingy(t_viforwardchar), NULL);
-    bindkey(amap, "\33OD",  refthingy(t_vibackwardchar), NULL);
-
-    /* emacs mode: arrow keys */
-    bindkey(emap, "\33[A",  refthingy(t_uplineorhistory), NULL);
-    bindkey(emap, "\33[B",  refthingy(t_downlineorhistory), NULL);
-    bindkey(emap, "\33[C",  refthingy(t_forwardchar), NULL);
-    bindkey(emap, "\33[D",  refthingy(t_backwardchar), NULL);
-    bindkey(emap, "\33OA",  refthingy(t_uplineorhistory), NULL);
-    bindkey(emap, "\33OB",  refthingy(t_downlineorhistory), NULL);
-    bindkey(emap, "\33OC",  refthingy(t_forwardchar), NULL);
-    bindkey(emap, "\33OD",  refthingy(t_backwardchar), NULL);
+    /* emacs mode: arrow keys */ 
+    add_cursor_key(emap, TCUPCURSOR, t_uplineorhistory, 'A');
+    add_cursor_key(emap, TCDOWNCURSOR, t_downlineorhistory, 'B');
+    add_cursor_key(emap, TCLEFTCURSOR, t_backwardchar, 'D');
+    add_cursor_key(emap, TCRIGHTCURSOR, t_forwardchar, 'C');
    
     /* emacs mode: ^X sequences */
     bindkey(emap, "\30*",   refthingy(t_expandword), NULL);

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: PATCH: (2) Re: History Up key
  2001-03-12 17:36 ` PATCH: (2) Re: History Up key Peter Stephenson
@ 2001-03-12 18:37   ` Bart Schaefer
  2001-03-13 11:40     ` PATCH: (2b) " Peter Stephenson
  2001-03-13  7:30   ` PATCH: (2) " Andrej Borsenkow
  1 sibling, 1 reply; 4+ messages in thread
From: Bart Schaefer @ 2001-03-12 18:37 UTC (permalink / raw)
  To: Peter Stephenson, Zsh hackers list

On Mar 12,  5:36pm, Peter Stephenson wrote:
} Subject: PATCH: (2) Re: History Up key
}
} > why not use termcap/terminfo to find esc for arrow keys?
} 
} I'd like some indication there are non-standard terminals that this doesn't
} wreck horribly before committing it.

There are some ancient terminals (I think Qume terminals are an example)
for which the termcap bindings are (or at least, were)

	kd => newline
	kl => backspace
	kr => space

with ku not bound, of course.  Probably an adequate solution is to avoid
binding the "arrow" keys in those circumstances where zsh automatically
enters single-line-zle mode.

Otherwise, there are of course broken termcap/terminfo database, but zsh
will be no worse off than any other program that trusts them.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 4+ messages in thread

* RE: PATCH: (2) Re: History Up key
  2001-03-12 17:36 ` PATCH: (2) Re: History Up key Peter Stephenson
  2001-03-12 18:37   ` Bart Schaefer
@ 2001-03-13  7:30   ` Andrej Borsenkow
  1 sibling, 0 replies; 4+ messages in thread
From: Andrej Borsenkow @ 2001-03-13  7:30 UTC (permalink / raw)
  To: Zsh hackers list


> 
> I don't have an exotic enough terminal to test these fully; it works for a
> bog-standard xterm.  Note the feature that, as I mentioned, if the
> sequences look like \e[? or \eO?, bind the other one as well.
> 

Works here under dtterm with emacs bindings.

> -item(tt(vi-backward-char) (unbound) (^H h ^?) (unbound))(
> +item(tt(vi-backward-char) (unbound) (^H h ^?) (ESC-[D))(
... et al

A word that zsh tries first to get ESC from termcap?

-andrej


^ permalink raw reply	[flat|nested] 4+ messages in thread

* PATCH: (2b) Re: History Up key
  2001-03-12 18:37   ` Bart Schaefer
@ 2001-03-13 11:40     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2001-03-13 11:40 UTC (permalink / raw)
  To: Zsh hackers list

Bart wrote:
> There are some ancient terminals (I think Qume terminals are an example)
> for which the termcap bindings are (or at least, were)
> 
> 	kd => newline
> 	kl => backspace
> 	kr => space
> 
> with ku not bound, of course.  Probably an adequate solution is to avoid
> binding the "arrow" keys in those circumstances where zsh automatically
> enters single-line-zle mode.

Andrej wrote:
> A word that zsh tries first to get ESC from termcap?

This goes on top of the immediately previous patch, not the one before
that.  I'll probably commit both of these later if there are no more
comments.

The consequence of the first hunk is that the VT100 sequences are bound if
the shell doesn't like the look of the terminal.  I don't think this is a
bad fallback position (it's consistent with what happened before, apart
from the additional viins bindings, of course).

--- Src/Zle/zle_keymap.c.yeah	Tue Mar 13 10:51:13 2001
+++ Src/Zle/zle_keymap.c	Tue Mar 13 10:52:55 2001
@@ -1030,7 +1030,11 @@
 {
     char buf[2048];
 
-    if (tccan(tccode)) {
+    /*
+     * Be careful not to try too hard with bindings for dubious or
+     * dysfunctional terminals.
+     */
+    if (tccan(tccode) && !(termflags & (TERM_NOUP|TERM_BAD|TERM_UNKNOWN))) {
 	/*
 	 * We can use the real termcap sequence.  We need to
 	 * persuade termcap to output `move cursor 1 char' and capture it.
--- Doc/Zsh/zle.yo.yeah	Tue Mar 13 10:55:12 2001
+++ Doc/Zsh/zle.yo	Tue Mar 13 11:28:35 2001
@@ -201,6 +201,16 @@
 and their default bindings in emacs mode,
 vi command mode and vi insert mode
 (the `tt(emacs)', `tt(vicmd)' and `tt(viins)' keymaps, respectively).
+
+Note that cursor keys are bound to movement keys in all three keymaps;
+the shell assumes that the cursor keys send the key sequences reported
+by the terminal-handling library (termcap or terminfo).  The key sequences
+shown in the list are those based on the VT100, common on many modern
+terminals, but in fact these are not necessarily bound.  In the case of the
+tt(viins) keymap, the initial escape character of the sequences serves also
+to return to the tt(vicmd) keymap: whether this happens is determined by
+the tt(KEYTIMEOUT) parameter, see ifzman(zmanref(zshparam))\
+ifnzman(noderef(Parameters)).
 startmenu()
 menu(Movement)
 menu(History Control)

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR Ltd., Unit 300, Science Park, Milton Road,
Cambridge, CB4 0XL, UK                          Tel: +44 (0)1223 392070


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2001-03-13 11:41 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <000601c0aae0$efafb580$21c9ca95@mow.siemens.ru>
2001-03-12 17:36 ` PATCH: (2) Re: History Up key Peter Stephenson
2001-03-12 18:37   ` Bart Schaefer
2001-03-13 11:40     ` PATCH: (2b) " Peter Stephenson
2001-03-13  7:30   ` PATCH: (2) " Andrej Borsenkow

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).