[comp.emacs] If you REALLY don't like the arrangement of C-h and DEL in GNU...

Karl.Kleinpaste@cbstr1.att.com (05/28/87)

I have been asked this question 4 times in the last 2 weeks.

Many people are used to typing C-h as a delete-backward-char (aka,
"stty erase") and tend to use DEL as a SIGINT key.  SysV users in
particular have this tendency.  Fine.  Except GNU Emacs has a really
serious prejudice toward C-h as the Help character, and DEL as
delete-backward-char.

Ecumenical differences abound.  Religious wars begin.

Yes, one could ask that SysV users (and all who have the tendency to
use C-h for stty erase and DEL as SIGINT) to convert to using DEL and
C-c, respectively.  I converted just so, about 2 years ago.  But
that's really just another religious war.

Many people seem not to be aware of a very valuable variable inside
GNU Emacs called the keyboard-translate-table.  When you type any char
at GNU Emacs, GNU Emacs takes its ASCII value as index into this
string, takes the char found there, and substitutes as though you'd
typed that instead.

This variable's existence allows the use of:

(setq keyboard-translate-table "\000\001\002\003\004\005\006\007
\177\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027
\030\031\032\033\034\035\036\037 !\042#$%&'()*+,-./0123456789:;<
=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\134]^_`abcdefghijklmnopqrstuvwxy
z{|}~\010")

which you can put in your .emacs file.  This has the effect of
interchanging all uses of C-h and DEL after the execution of the setq.
(Note that the string should all be on one line; I broke it into
pieces to prevent mailer/rnews stupidity.)  Note that the position
where \010 ought to be has \177 in it, and conversely.

This translation happens at a very low level inside GNU Emacs.  It is
so low that, when you type C-h after executing the setq, GNU Emacs
will *think* you've typed DEL.  When you type DEL, GNU Emacs will
think you typed C-h.  Hit 2 DELs and you'll get the minibuffer help
menu.  Hit a 3rd DEL and you'll get the full-buffer help menu.  Ask
GNU Emacs to describe-key-briefly on C-h, and it'll tell you that DEL
runs the command delete-backward-char.  You get the idea.

So you can avoid all the ecumenical and religious problems associated
with The Right Way To Set Up Your Terminal with simple, judicious use
of the keyboard-translate-table.  (I know a person who uses k-t-t to
interchange the use of C-g and C-u - icktooey - dangerous magic.)
This works far better than a mess of global-set-key commands because
it can't be fubar'd by some major mode coming along and screwing
around with your keymaps.  k-t-t conversion happens before keymap
interpretations.

Try it out and see if it prevents an argument with your officemate
about The Proper Use of DEL.

Enjoy,
Karl

pdg@ihdev.ATT.COM (Joe Isuzu) (06/18/87)

In article <247@cbstr1.att.com> Karl.Kleinpaste@cbstr1.att.com writes:
>Many people are used to typing C-h as a delete-backward-char (aka,
>"stty erase") and tend to use DEL as a SIGINT key.  
>(setq keyboard-translate-table "\000\001\002\003\004\005\006\007
>\177\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027
>\030\031\032\033\034\035\036\037 !\042#$%&'()*+,-./0123456789:;<
>=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\134]^_`abcdefghijklmnopqrstuvwxy
>z{|}~\010")


A little cleaner (from my .env, originally stolen from one of the
tty init files suplied with GNUmacs) and easier to read/modify.

;;; switch ^H and ^?
(let ((the-table (make-string 128 0)))
  (let ((i 0))
    (while (< i 128)
     (aset the-table i i)
      (setq i (1+ i))))
  ;; Swap ^H and DEL
  (aset the-table ?\177 ?\^h)
  (aset the-table ?\^h ?\177)
  (setq keyboard-translate-table the-table))

I agree with Karl, the k-t-t is a great feature.

-- 

Paul Guthrie				"Another day, another Jaguar"
ihnp4!ihdev!pdg				    -- Pat Sajak

walton@tybalt.caltech.edu (Steve Walton) (06/18/87)

Karl describes keyboard-translate table in the referenced message for
swapping C-H and DEL.  One real anamoly of doing it this way is that
quoted-insert (C-Q by default) will insert a DEL if you type C-Q C-H
at your keyboard;  similarly, C-Q followed by the DEL key will insert
a C-H.  There's gotta be a better way...

    Steve Walton, guest as walton@tybalt.caltech.edu
    AMETEK Computer Research Division, ametek!walton@csvax.caltech.edu
"Long signatures are definitely frowned upon"--USENET posting rules

ram-ashwin@YALE.ARPA (Ashwin Ram) (06/18/87)

>    This translation happens at a very low level inside GNU Emacs.  It is
>    so low that, when you type C-h after executing the setq, GNU Emacs
>    will *think* you've typed DEL.  When you type DEL, GNU Emacs will
>    think you typed C-h.  Hit 2 DELs and you'll get the minibuffer help
>    menu.  Hit a 3rd DEL and you'll get the full-buffer help menu.  Ask
>    GNU Emacs to describe-key-briefly on C-h, and it'll tell you that DEL
>    runs the command delete-backward-char.  You get the idea.

And so low that GNU Emacs will happily tell you to type C-h to get more help.
The latter is a bug, not a feature, of ktt's.  I think C-h was an extremely
poor choice of help key (especially given that the BACKSPACE key on most
terminals emits exactly the same code as C-h, and therefore Emacs can't
distinguish between them).  At the very least there should be a variable such
as "help-char" to allow customization of this function at a useful level.

-- Ashwin Ram --

ARPA:    Ram-Ashwin@yale
UUCP:    {decvax,linus,seismo}!yale!Ram-Ashwin
BITNET:  Ram@yalecs

drw@cullvax.UUCP (Dale Worley) (06/19/87)

Cleaner still,

;;; switch ^H and ^?
(aset keyboard-translate-table ?\177 ?\^h)
(aset keyboard-translate-table ?\^h ?\177)

Dale
-- 
Dale Worley	Cullinet Software		ARPA: cullvax!drw@eddie.mit.edu
UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw
If you light a match, how much mass does it convert into energy?

danny@calgary.UUCP (06/19/87)

In article <3038@cit-vax.Caltech.Edu>, walton@tybalt.caltech.edu (Steve Walton) writes:
>
> Karl describes keyboard-translate table in the referenced message for
> swapping C-H and DEL.  One real anamoly of doing it this way is that
> quoted-insert (C-Q by default) will insert a DEL if you type C-Q C-H
> at your keyboard;  similarly, C-Q followed by the DEL key will insert
> a C-H.  There's gotta be a better way...

There is:

	(aset global-map 8 'delete-backward-char)

overrides the binding of C-h to 'help-command while bypassing the checking
for a keymap performed by define-key.  Thenceforth, both C-h and DEL will
delete characters backwards, although, in some modes, DEL may get locally
bound to 'backward-delete-char-untabify.

You can also bind 'help-command to anything you want.  I bind it to a
function key transmitting a multi-character sequence (esc-[-208z on the sun).
Unfortunately, I like to have a double hit of the help key do a
'describe-key-briefly (aka help-c).  This is tricky as, although
global-set-key and local-set-key create extra keymaps as required, define-key
doesn't.  The code I am forced to is:

	(setq special-help-map1 (make-sparse-keymap))
	(setq special-help-map2 (make-sparse-keymap))
	(setq special-help-map3 (make-sparse-keymap))
	(setq special-help-map4 (make-sparse-keymap))
	(setq special-help-map5 (make-sparse-keymap))
	(define-key help-map "\e" special-help-map1)
	(define-key help-map "\e[" special-help-map2)
	(define-key help-map "\e[2" special-help-map3)
	(define-key help-map "\e[20" special-help-map4)
	(define-key help-map "\e[208" special-help-map5)
	(define-key help-map "\e[208z" 'describe-key-briefly)

On a related topic, I have had a lot of difficulty binding
'universal-argument (simple.el) to a multicharacter function key.  Eventually
I came up with the following alternative function:

(defun my-universal-argument ()
  "Begin a numeric argument for the following command.
Digits or minus sign following this command make up the numeric argument.
If no digits or minus sign follow, this command by itself provides 4 as argument.
Used more than once, this command multiplies the argument by 4 each time."
Fixed for tying to a function (multi-char sequence) key."
  (interactive nil)
  (let ((c-u 4)
	key-seq)
    (setq key-seq (read-key-sequence nil))
    (while (eq (key-binding key-seq) 'my-universal-argument)
      (setq c-u (* 4 c-u))
      (setq key-seq (read-key-sequence nil)))
    (if (equal (length key-seq) 1)
	(prefix-arg-internal (string-to-char key-seq) c-u nil)
      (setq prefix-arg (list c-u))
      (command-execute (key-binding key-seq)))))

Note: the else part of the if statement is needed because
'prefix-arg-internal uses "unread-command-char" to dispose of its first
argument if it is not a digit or "-".  I don't guarantee that my fix will
work in all circumstances but I have not mayself found any problems.  Of
course, the clean way to handle it (HINT, HINT) would be for gnu emacs to
permit pushing back (i.e. unreading) more than a single character as gosmacs
does.  Oh well, perfection is not achieved in a day.

-- danny levinson			   ...ihnp4!alberta!calgary!danny (?)
   university of calgary

[Seen on a local door: "So many pedestrians, so little time"]