* How to bind a key locally to a buffer (not mode!)
@ 2014-10-19 20:52 Marcin Borkowski
2014-10-19 21:12 ` John Mastro
` (3 more replies)
0 siblings, 4 replies; 8+ messages in thread
From: Marcin Borkowski @ 2014-10-19 20:52 UTC (permalink / raw)
To: help-gnu-emacs@gnu.org
Hi all,
(global-set-key ...)
binds a key globally.
(local-set-key ...)
binds a key locally, i.e., in the current major mode.
I'd like to bind a key in /one buffer/ only, so that the rebinding does
not affect other buffers in this mode. I could probably do it by
defining a minor mode, which rebinds this key to a function, which runs
a function set by a buffer-local variable, but this seems rather
convoluted. Is there a simpler way to achieve this?
The rationale is that I'd like to have a command which makes an indirect
buffer of the current buffer and sets it into another mode; I'd like to
have then a command (bound to some key) which would kill that buffer
and select the base one back. I /could/ also make this command
determine the base buffer based on the indirect buffer name (which I
derive from the bas buffer name by means of something like
(concat (buffer-name (current-buffer)) "-some-postfix")
but this doesn't seem too elegant.
Any hints?
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to bind a key locally to a buffer (not mode!)
2014-10-19 20:52 How to bind a key locally to a buffer (not mode!) Marcin Borkowski
@ 2014-10-19 21:12 ` John Mastro
2014-10-20 10:43 ` Artur Malabarba
` (2 subsequent siblings)
3 siblings, 0 replies; 8+ messages in thread
From: John Mastro @ 2014-10-19 21:12 UTC (permalink / raw)
To: help-gnu-emacs; +Cc: Marcin Borkowski
> Marcin Borkowski <mbork@wmi.amu.edu.pl> wrote:
> I'd like to bind a key in /one buffer/ only, so that the rebinding does
> not affect other buffers in this mode. I could probably do it by
> defining a minor mode, which rebinds this key to a function, which runs
> a function set by a buffer-local variable, but this seems rather
> convoluted. Is there a simpler way to achieve this?
Why not define a minor mode but, instead of the buffer-local
variable thing, only activate this mode in the buffer where you
want the binding?
--
john
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to bind a key locally to a buffer (not mode!)
[not found] <mailman.11531.1413752320.1147.help-gnu-emacs@gnu.org>
@ 2014-10-19 21:30 ` Joost Kremers
0 siblings, 0 replies; 8+ messages in thread
From: Joost Kremers @ 2014-10-19 21:30 UTC (permalink / raw)
To: help-gnu-emacs
Marcin Borkowski wrote:
> I'd like to bind a key in /one buffer/ only, so that the rebinding does
> not affect other buffers in this mode. I could probably do it by
> defining a minor mode, which rebinds this key to a function, which runs
> a function set by a buffer-local variable, but this seems rather
> convoluted. Is there a simpler way to achieve this?
Define a minor mode that binds that key and only activate it in the
buffer you want it in.
> The rationale is that I'd like to have a command which makes an indirect
> buffer of the current buffer and sets it into another mode; I'd like to
> have then a command (bound to some key) which would kill that buffer
> and select the base one back. I /could/ also make this command
> determine the base buffer based on the indirect buffer name (which I
> derive from the bas buffer name by means of something like
>
> (concat (buffer-name (current-buffer)) "-some-postfix")
>
> but this doesn't seem too elegant.
I don't see why that wouldn't be elegant. IMHO it's more elegant than
defining a special minor mode for the sole purpose of being able to
return to the correct base buffer.
BTW, are you aware of the function `buffer-base-buffer'? It tells you
what the base buffer of an indirect buffer is. No need to mess with
buffer-local key bindings or buffer names or what have you... :-)
--
Joost Kremers joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to bind a key locally to a buffer (not mode!)
2014-10-19 20:52 How to bind a key locally to a buffer (not mode!) Marcin Borkowski
2014-10-19 21:12 ` John Mastro
@ 2014-10-20 10:43 ` Artur Malabarba
2014-10-20 12:16 ` Artur Malabarba
[not found] ` <mailman.11555.1413801786.1147.help-gnu-emacs@gnu.org>
2014-10-20 11:40 ` Stefan Monnier
3 siblings, 1 reply; 8+ messages in thread
From: Artur Malabarba @ 2014-10-20 10:43 UTC (permalink / raw)
To: Marcin Borkowski; +Cc: help-gnu-emacs
> Hi all,
>
> (global-set-key ...)
>
> binds a key globally.
>
> (local-set-key ...)
>
> binds a key locally, i.e., in the current major mode.
>
> I'd like to bind a key in /one buffer/ only, so that the rebinding does
> not affect other buffers in this mode. I could probably do it by
> defining a minor mode, which rebinds this key to a function, which runs
> a function set by a buffer-local variable, but this seems rather
> convoluted. Is there a simpler way to achieve this?
There is, I used to do this in one of my org buffers. I forget the exact
code now, but I'll look for it.
You essentially split the current buffer's keymap from its major mode, and
then call local set key.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to bind a key locally to a buffer (not mode!)
[not found] ` <mailman.11555.1413801786.1147.help-gnu-emacs@gnu.org>
@ 2014-10-20 11:36 ` Pascal J. Bourguignon
0 siblings, 0 replies; 8+ messages in thread
From: Pascal J. Bourguignon @ 2014-10-20 11:36 UTC (permalink / raw)
To: help-gnu-emacs
Artur Malabarba <bruce.connor.am@gmail.com> writes:
>> Hi all,
>>
>> (global-set-key ...)
>>
>> binds a key globally.
>>
>> (local-set-key ...)
>>
>> binds a key locally, i.e., in the current major mode.
>>
>> I'd like to bind a key in /one buffer/ only, so that the rebinding does
>> not affect other buffers in this mode. I could probably do it by
>> defining a minor mode, which rebinds this key to a function, which runs
>> a function set by a buffer-local variable, but this seems rather
>> convoluted. Is there a simpler way to achieve this?
>
> There is, I used to do this in one of my org buffers. I forget the exact
> code now, but I'll look for it.
>
> You essentially split the current buffer's keymap from its major mode, and
> then call local set key.
1. C-h f local-set-key RET
2. the documentation of local-set-key tells you how to do it:
The binding goes in the current buffer's local map,
which in most cases is shared with all other buffers in the same major mode.
Therefore you should create a buffer local map distinct from the major
mode map.
3. M-x apropos RET local map RET
4. find a command that lets you create a local keymap.
5. use local-set-key with the new buffer local map.
--
__Pascal Bourguignon__ http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to bind a key locally to a buffer (not mode!)
2014-10-19 20:52 How to bind a key locally to a buffer (not mode!) Marcin Borkowski
` (2 preceding siblings ...)
[not found] ` <mailman.11555.1413801786.1147.help-gnu-emacs@gnu.org>
@ 2014-10-20 11:40 ` Stefan Monnier
3 siblings, 0 replies; 8+ messages in thread
From: Stefan Monnier @ 2014-10-20 11:40 UTC (permalink / raw)
To: help-gnu-emacs
> I'd like to bind a key in /one buffer/ only, so that the rebinding does
> not affect other buffers in this mode.
As mentioned, you can use a minor mode for that (and only enable it in
the current buffer). If you want this binding to have lower precedence
than the minor mode, you can use something like:
(use-local-map (make-composed-keymap nil (current-local-map)))
(local-set-key ...)
Where the first line changes the local map in the current buffer to use
a fresh new keymap which inherits from the previous one (the previous
one is assumed to be the one set by the major mode).
Stefan
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to bind a key locally to a buffer (not mode!)
2014-10-20 10:43 ` Artur Malabarba
@ 2014-10-20 12:16 ` Artur Malabarba
2014-10-20 14:02 ` Marcin Borkowski
0 siblings, 1 reply; 8+ messages in thread
From: Artur Malabarba @ 2014-10-20 12:16 UTC (permalink / raw)
To: Marcin Borkowski; +Cc: help-gnu-emacs
Here it is:
(use-local-map (copy-keymap (current-local-map)))
(local-set-key "x" #'some-function)
This will bind x to some-function only on the current buffer.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: How to bind a key locally to a buffer (not mode!)
2014-10-20 12:16 ` Artur Malabarba
@ 2014-10-20 14:02 ` Marcin Borkowski
0 siblings, 0 replies; 8+ messages in thread
From: Marcin Borkowski @ 2014-10-20 14:02 UTC (permalink / raw)
To: bruce.connor.am; +Cc: help-gnu-emacs
On 2014-10-20, at 14:16, Artur Malabarba wrote:
> Here it is:
>
> (use-local-map (copy-keymap (current-local-map)))
> (local-set-key "x" #'some-function)
>
> This will bind x to some-function only on the current buffer.
Cool, thanks! Though I ended up defining a minor mode (which has its
advantages, too, like the "lighter").
Best,
--
Marcin Borkowski
http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski
Adam Mickiewicz University
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2014-10-20 14:02 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-10-19 20:52 How to bind a key locally to a buffer (not mode!) Marcin Borkowski
2014-10-19 21:12 ` John Mastro
2014-10-20 10:43 ` Artur Malabarba
2014-10-20 12:16 ` Artur Malabarba
2014-10-20 14:02 ` Marcin Borkowski
[not found] ` <mailman.11555.1413801786.1147.help-gnu-emacs@gnu.org>
2014-10-20 11:36 ` Pascal J. Bourguignon
2014-10-20 11:40 ` Stefan Monnier
[not found] <mailman.11531.1413752320.1147.help-gnu-emacs@gnu.org>
2014-10-19 21:30 ` Joost Kremers
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).