unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Can one define global-set-key to override mode-based keymap?
@ 2006-11-13  2:19 Liu Fung Sin
  2006-11-13 16:25 ` Kevin Rodgers
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Liu Fung Sin @ 2006-11-13  2:19 UTC (permalink / raw)


Hi,

I like using the arrow keys to move around so I can free up Ctrl-F
Ctrl-B Ctrl-N and Ctrl-P and rebind them for some commands that I use
often.

For instance, I have

(global-set-key "\C-n" 'other-window)

in my .emacs

This works great until I invoke another major mode.

Like in dired, the dired mode map has C-n bound to dired-next-line.

C-n runs the command dired-next-line
   which is an interactive compiled Lisp function in `dired'.
(dired-next-line ARG)


Is there a way to define a key as special using global-set-key (or
otherwise) so that no other modes can override my .emacs setting?

Thanks.
-- fungsin

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

* Re: Can one define global-set-key to override mode-based keymap?
  2006-11-13  2:19 Liu Fung Sin
@ 2006-11-13 16:25 ` Kevin Rodgers
  2006-11-13 16:52 ` rgb
       [not found] ` <mailman.501.1163435231.2155.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: Kevin Rodgers @ 2006-11-13 16:25 UTC (permalink / raw)


Liu Fung Sin wrote:
> I like using the arrow keys to move around so I can free up Ctrl-F
> Ctrl-B Ctrl-N and Ctrl-P and rebind them for some commands that I use
> often.
> 
> For instance, I have
> 
> (global-set-key "\C-n" 'other-window)
> 
> in my .emacs

Down that road lies madness.

> This works great until I invoke another major mode.

See?  :-)

> Like in dired, the dired mode map has C-n bound to dired-next-line.
> 
> C-n runs the command dired-next-line
>    which is an interactive compiled Lisp function in `dired'.
> (dired-next-line ARG)
> 
> 
> Is there a way to define a key as special using global-set-key (or
> otherwise) so that no other modes can override my .emacs setting?

Hmmm, maybe you could bind `C-n' etc. to some other character not likely
to be bound to a command (e.g. an ISO-8859/Unicode C1 control character
or a Unicode private use character) via keyboard-translate, then bind
the key sequence (vector) of that unused character to `other-window'.

-- 
Kevin

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

* Re: Can one define global-set-key to override mode-based keymap?
  2006-11-13  2:19 Liu Fung Sin
  2006-11-13 16:25 ` Kevin Rodgers
@ 2006-11-13 16:52 ` rgb
       [not found] ` <mailman.501.1163435231.2155.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 8+ messages in thread
From: rgb @ 2006-11-13 16:52 UTC (permalink / raw)


> Is there a way to define a key as special using global-set-key (or
> otherwise) so that no other modes can override my .emacs setting?

The simple answer is no (afaik).
If you want to override mode specific keys you need to do it in a mode
hook using local-set-key.
Some people recommend this syntax (define-key X-key-map 'fcn) where X
is the mode name rather than local-set-key.  Both have their advantages
& dissadvantages.
Either way, modes often override the global key because, for example
moving to the next line in this particular buffer involves doing other
things besides moving point.  There is no guarantee that the mode
writer put that functionality on the arrow key (for example) and so
that functionality may need to be moved by you before you override the
mode's key definition.   Meaning you may need to do something like this

(local-set-key [down] (lookup-key (current-local-map) [?\C-n]))

before you do this

(local-set-key [?\C-n] 'my-custom-fcn)

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

* Re: Can one define global-set-key to override mode-based keymap?
       [not found] ` <mailman.501.1163435231.2155.help-gnu-emacs@gnu.org>
@ 2006-11-13 17:01   ` rgb
  2006-11-13 17:59     ` Kevin Rodgers
  0 siblings, 1 reply; 8+ messages in thread
From: rgb @ 2006-11-13 17:01 UTC (permalink / raw)


> Hmmm, maybe you could bind `C-n' etc. to some other character not likely
> to be bound to a command (e.g. an ISO-8859/Unicode C1 control character
> or a Unicode private use character) via keyboard-translate, then bind
> the key sequence (vector) of that unused character to `other-window'.

I've wanted to do this but can't figure out how.  Perhaps it's not
possible?   I want to use keyboard-translate on named events such as
<down> <apps> <end> etc.
Can this be done?
If so, I must be using the wrong syntax.  Could you give an example?

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

* Re: Can one define global-set-key to override mode-based keymap?
  2006-11-13 17:01   ` rgb
@ 2006-11-13 17:59     ` Kevin Rodgers
  0 siblings, 0 replies; 8+ messages in thread
From: Kevin Rodgers @ 2006-11-13 17:59 UTC (permalink / raw)


rgb wrote:
>> Hmmm, maybe you could bind `C-n' etc. to some other character not likely
>> to be bound to a command (e.g. an ISO-8859/Unicode C1 control character
>> or a Unicode private use character) via keyboard-translate, then bind
>> the key sequence (vector) of that unused character to `other-window'.
> 
> I've wanted to do this but can't figure out how.  Perhaps it's not
> possible?   I want to use keyboard-translate on named events such as
> <down> <apps> <end> etc.
> Can this be done?
> If so, I must be using the wrong syntax.  Could you give an example?

keyboard-translate only works on characters, not arbitrary events.
Maybe you can use function-key-map or key-translation-map to map such
events (more precisely, a sequence) to a different event sequence.

-- 
Kevin

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

* Re: Can one define global-set-key to override mode-based keymap?
@ 2006-11-14  6:46 martin rudalics
  0 siblings, 0 replies; 8+ messages in thread
From: martin rudalics @ 2006-11-14  6:46 UTC (permalink / raw)
  Cc: help-gnu-emacs

Not really recommended:

(defvar my-map (make-sparse-keymap)
   "My keymap.")

(defvar my-map-alist `((t . ,my-map))
   "My keymap alist.")

(add-to-ordered-list 'emulation-mode-map-alists 'my-map-alist 100)

(define-key my-map [(control n)] 'other-window)

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

* Re: Can one define global-set-key to override mode-based keymap?
       [not found] <mailman.551.1163488292.2155.help-gnu-emacs@gnu.org>
@ 2006-12-18  7:31 ` Daniel Engeler
  0 siblings, 0 replies; 8+ messages in thread
From: Daniel Engeler @ 2006-12-18  7:31 UTC (permalink / raw)


martin rudalics wrote:
> Not really recommended:
>
> (defvar my-map (make-sparse-keymap)
>    "My keymap.")
>
> (defvar my-map-alist `((t . ,my-map))
>    "My keymap alist.")
>
> (add-to-ordered-list 'emulation-mode-map-alists 'my-map-alist 100)
>
> (define-key my-map [(control n)] 'other-window)

I tried this and it works great so far. Why is it not recommended?

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

* Re: Can one define global-set-key to override mode-based keymap?
@ 2006-12-21 13:17 martin rudalics
  0 siblings, 0 replies; 8+ messages in thread
From: martin rudalics @ 2006-12-21 13:17 UTC (permalink / raw)
  Cc: help-gnu-emacs

 >> Not really recommended:
 >>
 >> (defvar my-map (make-sparse-keymap)
 >>    "My keymap.")
 >>
 >> (defvar my-map-alist `((t . ,my-map))
 >>    "My keymap alist.")
 >>
 >> (add-to-ordered-list 'emulation-mode-map-alists 'my-map-alist 100)
 >>
 >> (define-key my-map [(control n)] 'other-window)
 >
 > I tried this and it works great so far. Why is it not recommended?

It might interact badly with other modes using this - like cua-mode.
If you use them, you should study their keymaps in order to avoid any
bad interactions.  Alternatively you could try `overriding-local-map'.

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

end of thread, other threads:[~2006-12-21 13:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-11-14  6:46 Can one define global-set-key to override mode-based keymap? martin rudalics
  -- strict thread matches above, loose matches on Subject: below --
2006-12-21 13:17 martin rudalics
     [not found] <mailman.551.1163488292.2155.help-gnu-emacs@gnu.org>
2006-12-18  7:31 ` Daniel Engeler
2006-11-13  2:19 Liu Fung Sin
2006-11-13 16:25 ` Kevin Rodgers
2006-11-13 16:52 ` rgb
     [not found] ` <mailman.501.1163435231.2155.help-gnu-emacs@gnu.org>
2006-11-13 17:01   ` rgb
2006-11-13 17:59     ` Kevin Rodgers

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).