unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* local key swap? alternatives?
@ 2005-09-01 12:46 Francisco Borges
  2005-09-01 16:49 ` Kevin Rodgers
  0 siblings, 1 reply; 9+ messages in thread
From: Francisco Borges @ 2005-09-01 12:46 UTC (permalink / raw)


Hello,

I'm running Emacs 22.0.50.1 which I got from KarolyLorentey's multi-tty
branch, as I don't think this is related to multi-tty's patch I'm
posting here...

Long ago I came with the idea to swap keys in LaTeX buffers to be able
to type popular LaTeX characters without SHIFT, namely $%^&*()_{}.

So I thought of swapping ($,4), (%,5) etc but I wanted it only for
LaTeX-mode and never figured out how to perform a local key
swap. Instead I modified lisp/double.el to use doubles as (?4 "$" "4")
(?5 "%" "5") etc, e.g. hitting '4' once I get '$', hit twice I get '4'.
To get a decent compromise.

With my last compilation of emacs (22.0.50.1), this has stopped
working.

Two questions:

1. Is there an easy way for me to get this to work again?

2. Would anyone recommend another approach to the problem? Is there a
way to swap keys locally in Emacs?

                           -----

Thank you for your attention,
Francisco.

and BTW, thank you for Emacs ;-)

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

* Re: local key swap? alternatives?
  2005-09-01 12:46 local key swap? alternatives? Francisco Borges
@ 2005-09-01 16:49 ` Kevin Rodgers
  2005-09-07 13:53   ` Francisco Borges
  0 siblings, 1 reply; 9+ messages in thread
From: Kevin Rodgers @ 2005-09-01 16:49 UTC (permalink / raw)


Francisco Borges wrote:
 > Long ago I came with the idea to swap keys in LaTeX buffers to be able
 > to type popular LaTeX characters without SHIFT, namely $%^&*()_{}.
 >
 > So I thought of swapping ($,4), (%,5) etc but I wanted it only for
 > LaTeX-mode and never figured out how to perform a local key
 > swap. Instead I modified lisp/double.el to use doubles as (?4 "$" "4")
 > (?5 "%" "5") etc, e.g. hitting '4' once I get '$', hit twice I get '4'.
 > To get a decent compromise.
 >
 > With my last compilation of emacs (22.0.50.1), this has stopped
 > working.
 >
 > Two questions:
 >
 > 1. Is there an easy way for me to get this to work again?
 >
 > 2. Would anyone recommend another approach to the problem? Is there a
 > way to swap keys locally in Emacs?

How about:

(defun swap-keys (key-1 key-2 &optional keymap)
   "*Swap the bindings of KEY-1 and KEY-2.
Optional arg KEYMAP defaults to the global keymap; with a prefix arg,
the local keymap."
   (interactive (list (read-key-sequence "Swap key: ")
                      (read-key-sequence "Swap with key: ")
                      (if current-prefix-arg
                          (current-local-map)
                        (current-global-map))))
   (let ((binding-1 (lookup-key keymap key-1))
         (binding-2 (lookup-key keymap key-2)))
     (define-key keymap key-1 binding-2)
     (define-key keymap key-2 binding-1)
     (when (interactive-p)
       (message "%s runs the command %s; %s runs the command %s"
                key-1 (lookup-key keymap key-1 t)
                key-2 (lookup-key keymap key-2 t)))))

(swap-keys "$" "4" latex-mode-map)
(swap-keys "%" "5" latex-mode-map)

-- 
Kevin Rodgers

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

* Re: local key swap? alternatives?
       [not found] <mailman.5694.1125579745.20277.help-gnu-emacs@gnu.org>
@ 2005-09-05 18:13 ` Stefan Monnier
  0 siblings, 0 replies; 9+ messages in thread
From: Stefan Monnier @ 2005-09-05 18:13 UTC (permalink / raw)


> 1. Is there an easy way for me to get this to work again?

Most likely, yes, but you haven't given us much to work from to help you.


        Stefan

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

* Re: local key swap? alternatives?
  2005-09-01 16:49 ` Kevin Rodgers
@ 2005-09-07 13:53   ` Francisco Borges
  2005-09-07 17:47     ` Kevin Rodgers
  0 siblings, 1 reply; 9+ messages in thread
From: Francisco Borges @ 2005-09-07 13:53 UTC (permalink / raw)


» On Thu, Sep 01, 2005 at 10:49AM -0600, Kevin Rodgers wrote:

> Francisco Borges wrote:
> > Long ago I came with the idea to swap keys in LaTeX buffers to be able
> > to type popular LaTeX characters without SHIFT, namely $%^&*()_{}.

> How about:
>
> (defun swap-keys (key-1 key-2 &optional keymap)
>   "*Swap the bindings of KEY-1 and KEY-2.
> Optional arg KEYMAP defaults to the global keymap; with a prefix arg,
> the local keymap."
>   (interactive (list (read-key-sequence "Swap key: ")
>                      (read-key-sequence "Swap with key: ")
>                      (if current-prefix-arg
>                          (current-local-map)
>                        (current-global-map))))
>   (let ((binding-1 (lookup-key keymap key-1))
>         (binding-2 (lookup-key keymap key-2)))
>     (define-key keymap key-1 binding-2)
>     (define-key keymap key-2 binding-1)
>     (when (interactive-p)
>       (message "%s runs the command %s; %s runs the command %s"
>                key-1 (lookup-key keymap key-1 t)
>                key-2 (lookup-key keymap key-2 t)))))
> (swap-keys "$" "4" latex-mode-map)
> (swap-keys "%" "5" latex-mode-map)

That does not work because both keys are bound to
self-insert-command. That's the reason I had failed to do it myself.

I (finally) solved the problem by doing something as ugly as:

(defun my-four ()
  (interactive)
  (insert-char (string-to-char "4") 1))

(defun my-dollar ()
  (interactive)
  (insert-char (string-to-char "$") 1))

(local-set-key "$" (quote my-four))
(local-set-key "4" (quote my-dollar))

Would there be a better way to do it? I don't really know Lisp...

I think that the right thing to do is to use a function such as the one
you send and treat self-insert-command cases specially, but with my
knowledge of Lisp making these changes to the code above would take more
than the free time I have to spend on it...

Cheers,
Francisco.

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

* Re: local key swap? alternatives?
  2005-09-07 13:53   ` Francisco Borges
@ 2005-09-07 17:47     ` Kevin Rodgers
  2005-09-08 14:57       ` Francisco Borges
       [not found]       ` <mailman.6290.1126191990.20277.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: Kevin Rodgers @ 2005-09-07 17:47 UTC (permalink / raw)


Francisco Borges wrote:
 > That does not work because both keys are bound to
 > self-insert-command. That's the reason I had failed to do it myself.

Yes, that's a tricky situation.

 > I (finally) solved the problem by doing something as ugly as:
 >
 > (defun my-four ()
 >   (interactive)
 >   (insert-char (string-to-char "4") 1))
 >
 > (defun my-dollar ()
 >   (interactive)
 >   (insert-char (string-to-char "$") 1))
 >
 > (local-set-key "$" (quote my-four))
 > (local-set-key "4" (quote my-dollar))
 >
 > Would there be a better way to do it? I don't really know Lisp...

Just a few minor points: You could allow a prefix argument to insert
multiple characters, provide a doc string, and avoid converting from a
single-character string to the character:

(defun my-four (&optional arg)
"Insert \"4\" at point.
With a prefix ARG, insert that many characters."
   (interactive "p")
   (insert-char ?4 (or arg 1)))

(defun my-dollar ()
"Insert \"$\" at point.
With a prefix ARG, insert that many characters."
   (interactive "p")
   (insert-char ?$ (or arg 1)))

(local-set-key "$" 'my-four)
(local-set-key "4" 'my-dollar)

If you have many such functions, you'd want to abstract the common parts
with a function-defining macro:

(defmacro define-my-insert (name char)
   "Define the `my-NAME' command, to insert CHAR."
   `(defun ,(intern (format "my-%s" name)) (&optional arg)
      ,(format "Insert \"%c\" at point.
With a prefix ARG, insert that many characters."
               char)
      (interactive "p")
      (insert-char ,char (or arg 1))))

(define-my-insert four ?4)
(define-my-insert dollar ?$)

 > I think that the right thing to do is to use a function such as the one
 > you send and treat self-insert-command cases specially, but with my
 > knowledge of Lisp making these changes to the code above would take more
 > than the free time I have to spend on it...

If the define-my-insert macro were rewritten to evalutate the NAME and
CHAR arguments, you could add this to swap-keys:

(when (eq binding-1 'self-insert-command)
   (setq binding-1
         (define-my-insert (intern key-1) (string-to-char key-1))))
(when (eq binding-2 'self-insert-command)
   (setq binding-2
         (define-my-insert (intern key-2) (string-to-char key-2))))

-- 
Kevin Rodgers

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

* Re: local key swap? alternatives?
  2005-09-07 17:47     ` Kevin Rodgers
@ 2005-09-08 14:57       ` Francisco Borges
       [not found]       ` <mailman.6290.1126191990.20277.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 9+ messages in thread
From: Francisco Borges @ 2005-09-08 14:57 UTC (permalink / raw)


» On Wed, Sep 07, 2005 at 11:47AM -0600, Kevin Rodgers wrote:

> If you have many such functions, you'd want to abstract the common parts
> with a function-defining macro:
>
> (defmacro define-my-insert (name char)
>   "Define the `my-NAME' command, to insert CHAR."
>   `(defun ,(intern (format "my-%s" name)) (&optional arg)
>      ,(format "Insert \"%c\" at point.
> With a prefix ARG, insert that many characters."
>               char)
>      (interactive "p")
>      (insert-char ,char (or arg 1))))
>
> (define-my-insert four ?4)
> (define-my-insert dollar ?$)

That's great! Thanks a lot!

Cheers,
Francisco.

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

* Re: local key swap? alternatives?
       [not found]       ` <mailman.6290.1126191990.20277.help-gnu-emacs@gnu.org>
@ 2005-09-09  4:41         ` Stefan Monnier
  2005-09-09  9:42           ` Francisco Borges
       [not found]           ` <mailman.6364.1126259105.20277.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 9+ messages in thread
From: Stefan Monnier @ 2005-09-09  4:41 UTC (permalink / raw)


>> (defmacro define-my-insert (name char)
>> "Define the `my-NAME' command, to insert CHAR."
>> `(defun ,(intern (format "my-%s" name)) (&optional arg)
>> ,(format "Insert \"%c\" at point.
>> With a prefix ARG, insert that many characters."
>> char)
>> (interactive "p")
>> (insert-char ,char (or arg 1))))
>> 
>> (define-my-insert four ?4)
>> (define-my-insert dollar ?$)

Have you taken a look at key-translation-map?
Something like

  (define-key key-translation-map "4" "$")
  (define-key key-translation-map "$" "4")


-- Stefan

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

* Re: local key swap? alternatives?
  2005-09-09  4:41         ` Stefan Monnier
@ 2005-09-09  9:42           ` Francisco Borges
       [not found]           ` <mailman.6364.1126259105.20277.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 9+ messages in thread
From: Francisco Borges @ 2005-09-09  9:42 UTC (permalink / raw)


» On Fri, Sep 09, 2005 at 12:41AM -0400, Stefan Monnier wrote:

> Have you taken a look at key-translation-map?
> Something like
>
>   (define-key key-translation-map "4" "$")
>   (define-key key-translation-map "$" "4")

This seems to be better than using the macros but also worse...

It's better because LaTeX mode still knows that $$, () etc should match
each other but using key-translation-map, I'm mapping the keys globally,
so the whole emacs has the keys swapped.

I tried using local-key-translation-map but the local in it does not
mean "local buffer". So by now my problems are:

Using macros:
I loose blinking matches of [], {} etc
I loose using ^ to accentuate with quail, if I do swap 6^

Using key-translation-map:
I remap globally
I loose using ^ to accentuate with quail, if I do swap 6^

Any other ideas?

                       --------

Thanks for the help,
-- 
Francisco Borges
Alfa Informatica - RuG

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

* Re: local key swap? alternatives?
       [not found]           ` <mailman.6364.1126259105.20277.help-gnu-emacs@gnu.org>
@ 2005-09-09 13:18             ` Stefan Monnier
  0 siblings, 0 replies; 9+ messages in thread
From: Stefan Monnier @ 2005-09-09 13:18 UTC (permalink / raw)


>> Have you taken a look at key-translation-map?
>> Something like
>> 
>> (define-key key-translation-map "4" "$")
>> (define-key key-translation-map "$" "4")

> This seems to be better than using the macros but also worse...

> It's better because LaTeX mode still knows that $$, () etc should match
> each other but using key-translation-map, I'm mapping the keys globally,
> so the whole emacs has the keys swapped.

You probably want to do something like:

  (set (make-local-variable 'key-translation-map)
       (copy-keymap key-translation-map))

before the above two define-keys.  And put the whole thing inside
LaTeX-mode-hook (if you're using AUCTeX) or latex-mode-hook (otherwise).


        Stefan

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

end of thread, other threads:[~2005-09-09 13:18 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-01 12:46 local key swap? alternatives? Francisco Borges
2005-09-01 16:49 ` Kevin Rodgers
2005-09-07 13:53   ` Francisco Borges
2005-09-07 17:47     ` Kevin Rodgers
2005-09-08 14:57       ` Francisco Borges
     [not found]       ` <mailman.6290.1126191990.20277.help-gnu-emacs@gnu.org>
2005-09-09  4:41         ` Stefan Monnier
2005-09-09  9:42           ` Francisco Borges
     [not found]           ` <mailman.6364.1126259105.20277.help-gnu-emacs@gnu.org>
2005-09-09 13:18             ` Stefan Monnier
     [not found] <mailman.5694.1125579745.20277.help-gnu-emacs@gnu.org>
2005-09-05 18:13 ` Stefan Monnier

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 read-only IMAP folder(s) and NNTP newsgroup(s).