[comp.emacs] Question/Suggestion on C-s/C-q question . . .

randy@mit-eddie.UUCP (03/04/87)

  Does anyone have an idea how possible/easy it would be to define a
key as a prefix for control?  In other words, to allow a two key
substitution for any control key?  (Example: 'C-v s' substitutes for
'C-s'.  C-v would be a bad choice, but you get the idea).  Loosely, to
define an equivalent of the escape key for Control?  I remember seeing
this in some earlier emacs (I think the original twenex version).  The
manual describes the key bindings, but doesn't give any general method
to get at a key's corresponding control definition.

  This strikes me as being an easy and general solution to the problem
of terminal servers etc. catching ^S and ^Q.  Redefinition of C-^ and
C-\ relies on your terminal being able to produce them, and my home pc
(Macintosh) can't.  Suggestions/questions/comments/flames?

					-- Randy Smith

 UsMail:  Randy Smith		Uucp:  . . .!seismo!elsie!ncifcrf!randy
       	  c/o PRI, Inc.
	  PO Box B, Bldng. 430
          Frederick, MD 21701  	

matt@oddjob.UUCP (03/05/87)

I don't believe the addresses that seismo is sticking on, but...
randy@seismo.CSS.GOV@ncifcrf.UUCP (Randy Smith) writes:
)   Does anyone have an idea how possible/easy it would be to define a
) key as a prefix for control?  In other words, to allow a two key
) substitution for any control key?  (Example: 'C-v s' substitutes for
) 'C-s'.  C-v would be a bad choice, but you get the idea).

TVI terminals have a key "FUNCT" which, if held while typing
some other key x, sends C-A x C-M.  I made this act as a meta
key by using:

(defun funct-key ()
  "Make the TVI's FUNCT key act like a META key"
  (interactive)
  (let ((save-key (read-char)))
    (if (/= (read-char) ?\^M)
	(error "bad C-A or FUNCT key usage")
      (setq prefix-arg current-prefix-arg
	    this-command last-command
	    unread-command-char (logior save-key 128)))))

(define-key global-map "\^A" 'funct-key)

Speaking off the top of my head, you have two main choices.  You
can emulate and simplify the above to:

(defun prefix-control ()
  "Make the net character act like the corresponing control character"
  (interactive)
  (setq prefix-arg current-prefix-arg
	this-command last-command
	unread-command-char (logand (read-char) 31)))))
;; A more sophisticated version would map ? to DEL

The other choice is to make a new keymap that does indirection
through whatever keymap is in effect.  Binding a key sequence to
a string has this effect - it's essentially a keyboard macro.

(setq control-map (make-keymap))
(fset 'prefix-control control-map)
(setq i 0)
(while (< i 32)
  (define-key control-map (char-to-string (+ i ?@)) (char-to-string i))
  (define-key control-map (char-to-string (+ i ?`)) (char-to-string i))
  (setq i (1+ i)))
(define-key control-map "?" "\^?")
(define-key global-map "\^V" 'prefix-control) ;or whatever key you want


In either case, the extension to "prefix-control-meta" is left
as an exercise for the reader.

Keep out of trouble, Randy.
					Matt

rbj@ICST-CMR.ARPA.UUCP (03/05/87)

> Speaking off the top of my head, you have two main choices.  You
> can emulate and simplify the above to:
> 
> (defun prefix-control ()
>   "Make the next character act like the corresponding control character"
>   (interactive)
>   (setq prefix-arg current-prefix-arg
> 	this-command last-command
> 	unread-command-char (logand (read-char) 31)))))
> ;; A more sophisticated version would map ? to DEL

Here's where you become more sophisticated. You have just illustrated
one of my pet peeves: special casing `?' in control code functions.
Why do you think DEL is `^?'? There is a function that will map both
letters AND the apparant special case `?': char XOR 64. Of course,
this forces you to type ^A instead of ^a, so it is more useful as an
output transformation than as an input one: ^a maps to `!'!

It is amazing how many people don't know this trick, so don't feel bad.
 
> 					Matt

	(Root Boy) Jim "Just Say Yes" Cottrell	<rbj@icst-cmr.arpa>
	I think I am an overnight sensation right now!!

earleh@dartvax.UUCP (Earle R. Horton) (03/06/87)

In article <8703042025.AA12061@ncifcrf.ncifcrf.uucp>, randy@seismo.CSS.GOV@ncifcrf.UUCP (Randy Smith) writes:
> 
> 
>   Does anyone have an idea how possible/easy it would be to define a
> key as a prefix for control?
...
>                                               Redefinition of C-^ and
> C-\ relies on your terminal being able to produce them, and my home pc
> (Macintosh) can't.  Suggestions/questions/comments/flames?
> 
> 					-- Randy Smith

Lest anyone get the idea that the Macintosh lacks the ability to generate
control characters, or is an unsatisfactory substitute for a "real" terminal
in any way, allow me to correct that impression.  Mr. Smith suffers from one
of two possible ailments, a) inferior terminal emulator, or b) forgot to read
the user's manual.  In order to avoid messing around with lisp code for hours,
I would recommend that you throw out your present terminal emulator and get a)
MacKermit, b) uw, or c) VersaTerm.  The first two are free and allow
generation of any character you can think of, even eight- bit jobs.  VersaTerm
provides the opportunity to spend money, but has more features, although the
programmer seems to have neglected eight-bit characters.  uw is nice for UNIX,
since you can run a server on the host that services 7 (!) pseudo ttys in
separate windows on the Macintosh.  Also, if you have BSD 4.3, window size
changes in uw are communicated to programs running on the host (!)  Also, uw
encodes ^S and ^Q into two-byte sequences, which are decoded by the server and
passed to emacs or whatever as ^S or ^Q.  This means that when running uw,
nobody can touch my ^S or ^Q but emacs!  MacKermit comes with a separate
utility program that allows mapping of keys to anything: strange control
characters, eight-bit characters, whole sentences, anything you want, and it
emulates a vt102.  uw emulates a vt52, Ann Arbor Ambassador (ANSI), adm31, or
tek 4012.  VersaTerm does VT100, VT52, and tek 4012 (4105 in the more
expensive version).  I would suggest this to Mr. Smith: throw away that
terminal program you are using if it can't do ^^ and ^\, and get a real one!

I realize that this article has very little to do with emacs, but I 
don't want people to go around thinking I can't generate all the
^\'s or ^^'s I want to with my Mac!