Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binding a key in a map for a different, not-yet-loaded mode? #1063

Open
hdeyoung opened this issue Oct 19, 2023 · 1 comment
Open

Binding a key in a map for a different, not-yet-loaded mode? #1063

hdeyoung opened this issue Oct 19, 2023 · 1 comment

Comments

@hdeyoung
Copy link

Binding a key in a map for a different mode?

I'm trying to use citar with both AUCTeX and org-mode.

(use-package org
  :ensure t
  :mode ("\\.org\\'" . org-mode))

(use-package latex
  :ensure auctex
  :mode ("\\.tex\\'" . LaTeX-mode))

(use-package citar
  :after (:any latex org)
  :demand t
  :bind (:map LaTeX-mode-map ("C-c [" . citar-insert-citation)))

Problem: This works fine if, after starting Emacs, I happen to open a .tex file before opening a .org file. However, if I happen to first open a .org file, I get a (void-variable LaTeX-mode-map) error in the *Messages* buffer and, worse than that, when I later open a .tex file, the binding is not set up. From what I can tell, the problem is that org-mode triggers the use-package citar, but the binding fails because LaTeX-mode-map does not exist until LaTeX-mode is used.

Question: What is the idiomatic way to handle this kind of situation with use-package? Should I be splitting the clauses for citar into two calls to use-package? The following does work, but I feel slightly unsettled by using two use-package citar. What is the recommended idiom?

(use-package org
  :ensure t
  :mode ("\\.org\\'" . org-mode))

(use-package latex
  :ensure auctex
  :mode ("\\.tex\\'" . LaTeX-mode))

(use-package citar
  :after (:any latex org))

(use-package citar
  :after latex
  :bind (:map LaTeX-mode-map ("C-c [" . citar-insert-citation)))

Also, if I expand this last use-package citar, I see that the bind-keys has :package citar, and I sort of expected to see :package latex... Can someone please explain this to me?

@mmarshall540
Copy link

The default value for :package is the package being configured by the current use-package form.

However, you can set the :package value yourself after the :map keyword, and this will override the default behavior, setting up an eval-after-load for the package you specify instead.

    (use-package citar
      :after (:any latex org)
      :demand t
      :bind 
      ( :map LaTeX-mode-map 
        :package latex
        ("C-c [" . citar-insert-citation)))

This will cause it to wait for latex.el to load (and define the keymap) before attempting to set the binding in LaTeX-mode-map.

A minor caveat is that the :package keyword does not seem to be documented. So there's no guarantee that use-package/bind-key will continue to use it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants