Use-package: Multiple keymaps in bind-key macros

Created on 10 Oct 2014  路  20Comments  路  Source: jwiegley/use-package

It'd be nice if bind-key(s) macros could handle multiple keymaps. For the moment, I need to do:

(dolist (map '(my-map1 my-map2))
  (bind-keys :map (symbol-value map)
             ("key"  . fn)
             ("key2" . fn2)))

It would be neater to just do:

(bind-keys :map '(my-map1 my-map2)
           ("key"  . fn)
           ("key2" . fn2))
bind-key enhancement

All 20 comments

This would be very neat.

Probably linked to #88

I'm not sure how easy it would be though to distinguish the two data structures.

There is, of course, keymapp :wink:

@lionel- I agree, and will be adding a feature like this shortly. I like your introduction of a keyword for this.

This now connects to #121.

Sweet! Thanks a lot John!

@lionel- I realized last night that this implementation isn't quite what I want yet. The :map argument is only handled once, meaning you can't switch keymaps, or have both global and mode-map bindings.

Instead, since bind-keys is already treating its argument list specially, I'd like to allow for multiple occurrences of :map, where any bindings that follow apply to the immediately preceding map.

Could anyone confirm that

(use-package expand-region
  :bind (("M-m" . er/expand-region))
  :bind (:map html-mode-map
          ("s-m" . er/mark-inner-tag)
          ("s-M" . er/mark-outer-tag))
  :config
  (progn
    (setq expand-region-contract-fast-key "M")))

should work?

I get error Debugger entered--Lisp error: (void-variable html-mode-map).

As of today, that should not work. In future, it should.

:map can now occur multiple times, and is properly lazy. I don't know about having it accept a list argument, however; that seems like a fairly rare usage.

that seems like a fairly rare usage

One usage is to bind keys in both a major mode and a related inferior mode.

True.

Reading the code, this should actually work in the manner originally requested. Please reopen if not.

Thanks John!

After upgrading to latest use-package, I get an error with this backtrace:

Debugger entered--Lisp error: (void-function ess-mode-map)
  (ess-mode-map inferior-ess-mode-map)
  (or (ess-mode-map inferior-ess-mode-map) global-map)
  (lookup-key (or (ess-mode-map inferior-ess-mode-map) global-map) key)

Triggered with:

    (bind-keys :map (ess-mode-map inferior-ess-mode-map)
               ...)

which used to work.

PS: I can't reopen the issue, should I file a new one?

@lionel- I've disabled this feature because it creates complexities in the implementation of bind-keys that I don't want to deal with at the moment. The alternative is to use code that looks like this:

(dolist (mm '(ess-mode-map inferior-ess-mode-map))
  (bind-keys :map mm ...))

I'm not going to support multiple keymaps to :map, when there is a workaround available using dolist.

In case others see this in future, the code sample mentioned by @jwiegley should be like:

(dolist (mm '(ess-mode-map inferior-ess-mode-map))
  (bind-keys :map (symbol-value mm) ...))

Note the (symbol-value mm) part. Otherwise you will get Wrong type argument error, which I met.

Note that I personally use a custom function for this:

(defsubst hook-into-modes (func &rest modes)
  (dolist (mode-hook modes) (add-hook mode-hook func)))

(hook-into-modes #'abbrev-mode
                 'text-mode-hook
                 'prog-mode-hook
                 'erc-mode-hook
                 'LaTeX-mode-hook)

I created an add-hooks package to make it more convenient to add multiple functions to a hook and vice versa, with some extra syntax sugar. I personally use this in individual use-package :config calls, though I would be also interested in having this syntax in use-package or a plugin package (#444).

(add-hooks-pair '(text-mode prog-mode erc-mode LaTeX-mode) 'abbrev-mode)

If you'd rather me not advertise other projects/issues here feel free to delete this comment.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

DamienCassou picture DamienCassou  路  9Comments

sheepduke picture sheepduke  路  3Comments

DrWaleedAYousef picture DrWaleedAYousef  路  11Comments

kpbochenek picture kpbochenek  路  8Comments

Yevgnen picture Yevgnen  路  5Comments