Subject: Re: Backspace vs Delete on Console in 1.5.3
To: Christian Hattemer <chris@heaven.riednet.wh.tu-darmstadt.de>
From: Greg A. Woods <woods@weird.com>
List: port-i386
Date: 09/03/2002 13:21:45
[ On Monday, September 2, 2002 at 17:40:38 (+0200), Christian Hattemer wrote: ]
> Subject: Re: Backspace vs Delete on Console in 1.5.3
>
> I found that also more and csh do this. Perhaps I should change back and try
> to give the delete key another sequence (the one from X)?

What you need to do is to have _all_ of your interactive programs agree
with you _and_ your TTY driver on which key you'll press to delete
characters (and which key you'll press to send an interrupt signal to
the foreground process, etc.).  Most programs honour the TTY driver
settings for special input characters.  With the "wscons" console driver
there's a layer of translation between the low-level keyboard codes sent
by the hardware and the character codes which are sent through to the
TTY driver.

Forget about X Windows if you're not using it.  All it does is add
another alternate key translation layer that seems to continually
confuse people, and of course X applications may also have their own
ways to interpret key codes too, with the canonical example of "xterm"
being no exception (though generally it doesn't mess up the "erase" key).

Now what I mean by "interactive programs" are those which bypass the TTY
driver and which do their own special key handling.  Normally the TTY
driver deletes characters when you press the key which transmits the
character it knows as the "erase" key.  However interactive programs
which allow command-line editing, such as your shell, text editors,
etc. will put the TTY into a mode where all (or at least most special)
characters are sent directly to the program with no interpretation.  If
you've explicitly configured your shell to treat a certain character as
the one which will delete a previous character on the current line, then
it may not match what the TTY driver would do in normal mode.  You can
check by running "stty -a" and looking at what it reports as the "erase"
character value.  You can test this directly by typing and erasing
characters first at the shell prompt, and then to a non-interactive
program such as "cat".

If your keyboard had a "backspace" key that sent an ASCII DEL character
(i.e. a character with decimal value 127, or <CTRL-?>, usually displayed
as `^?') through to the TTY layer, then you want to set your TTY "erase"
character to match:

	stty erase '^?'

However if your keyboard has a proper "backspace" key that sends an
ASCII <CTRL-H> character (i.e. a character with a decimal value of 8,
`^H') then you want to set want to set your TTY "erase" character to
match it instead:

	stty erase '^h'

You would put whichever of those commands is appropriate into your
~/.profile to be run every time you login.  Ideally you write a tiny
script in there to make an intelligent decision about what "stty"
settings to use depending on what TTY you're logging in from.

Now if you're using the "wscons" console driver on i386 with a
"standard" 101-key PC-style keyboard then you want to arrange for wscons
to map the keyboard code into a proper backspace (^h) value so that your
TTY driver will see it as distinct from the cursor-pad and number-pad
'DEL' key.  To do this you can add the following (perhaps without the
swapctrlcaps line) to near the end of /etc/rc.d/wscons:

	if [ $(uname -m) = "i386" ]; then
		# fix the backspace key so it sends a backspace....
		/sbin/wsconsctl -w map+='keycode 14 = Cmd_ResetEmul BackSpace Delete' > /dev/null

		echo -n 'fixing keyboard map... '
		# PC Keyboard layouts are *all* insane....
		/sbin/wsconsctl -w encoding=us.swapctrlcaps
	fi

Put these lines just before the closing brace of the wscons_start() function.

A better fix is to patch the poorly designed keycode mapping tables in
the kernel, but the run-time patch above is easier.  :-)

I'm not sure which key you'll find most natural to use for sending
interrupts.  I prefer DEL, which in the old days was "rubout" on a real
TTY.  Lots of other people prefer <CTRL-C> (^C), which is about the best
alternate choice if you don't have separate backspace and delete keys
(eg. on an old VT220 terminal or any of the clones which don't have
easily re-mappable keys).

	stty intr '^?'

Anyway once you have you, your console driver, and your TTY driver all
agreeing on what each key means, then you can see how well your programs
work.  I don't know about zsh any more, but I believe /bin/sh and
/bin/ksh will both use the TTY's "erase" character by default.  The only
major program I continually fight with is 'emacs' (and that's primarily
because RMS grew up using a terminal with only a "delete" key).  If
you're an emacs user let me know and I'll point you to the important
parts of my ~/.emacs.el which re-map the help and delete-backward-char
bindings to match a sane world where there are separate backspace and
delete keys.

-- 
								Greg A. Woods

+1 416 218-0098;            <g.a.woods@ieee.org>;           <woods@robohack.ca>
Planix, Inc. <woods@planix.com>; VE3TCP; Secrets of the Weird <woods@weird.com>