Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Can one define global-set-key to override mode-based keymap?

0 views
Skip to first unread message

Liu Fung Sin

unread,
Nov 12, 2006, 9:19:11 PM11/12/06
to
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

Kevin Rodgers

unread,
Nov 13, 2006, 11:25:55 AM11/13/06
to help-gn...@gnu.org
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

rgb

unread,
Nov 13, 2006, 11:52:43 AM11/13/06
to
> 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)

rgb

unread,
Nov 13, 2006, 12:01:40 PM11/13/06
to
> 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?

Kevin Rodgers

unread,
Nov 13, 2006, 12:59:46 PM11/13/06
to help-gn...@gnu.org

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

martin rudalics

unread,
Nov 14, 2006, 1:46:50 AM11/14/06
to fungs...@gmail.com, help-gn...@gnu.org
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)


Daniel Engeler

unread,
Dec 18, 2006, 2:31:20 AM12/18/06
to

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

0 new messages