0

Setting up bindings for Python Mode (and its Tree Sitter variant) with the following use-package form mysteriously fails:

(use-package python
  :bind (:map python-mode-map
              ("C-x *" . #'whitespace-mode)
              ("C-c C-v" . #'bci/python-check)))

When editing Python Mode buffers, I have python-mode remapped to python-ts-mode via major-mode-remap-alist.

(Note: this question is edited from a previous version which asked more specifically about a failing invocation of keymap-local-set, which turned out to involve a simple mistake involving quoting. However, as my original question was an instance of the XY Problem, the question has been updated to ask about my initial, more general problem.)

M-x version: GNU Emacs 31.0.50 (build 3, x86_64-pc-linux-gnu, GTK+ Version 3.24.38, cairo version 1.16.0) of 2024-11-21

6
  • This question is similar to: How to evaluate the variables before adding them to a list?. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem.
    – Drew
    Commented Nov 23 at 17:23
  • This is one of the most common duplicate questions.
    – Drew
    Commented Nov 23 at 17:25
  • I see what you mean; this feels a bit tricky, since quoting only gets involved in this case incidentally (it turned out to be the nature of the issue, which I, by hypothesis of having trouble in the first place, wasn't aware of). It looks like jirassimok already contributed an answer which is helpful to the greater issue of keybindings/use-package/etc. as a whole, which is more the spirit of my question anyway. Regardless, if you feel I should delete my question, I'll do it.
    – bci_
    Commented Nov 23 at 22:46
  • You can rephrase your question, if relevant, not starting with the code that uses the quoted sexp. IOW, if there's a question and answer that have nothing to do with that, then please change the question to do that. Thx.
    – Drew
    Commented Nov 24 at 5:41
  • 1
    Thanks..........
    – Drew
    Commented Nov 25 at 1:51

1 Answer 1

1

You seem to have already solved the specific issue you were having, but there might be an easier way to approach the general task you are trying to solve: bind-keys.

(bind-keys :map python-base-mode-map
           ("C-x *" . whitespace-mode)
           ("<remap> <python-check>" . bci/python-check))

(C-c C-v is already bound in python-mode-map rather than python-base-mode-map, so if you try to bind the key in the latter map, it won't work.)

If you don't want to remap every binding of python-check, you can bind the specific keys in whichever of the more-specific maps you'd like:

(bind-keys :map python-base-mode-map
           ("C-x *" . whitespace-mode)
           :map python-mode-map
           ("C-c C-v" . bci/python-check)
           :map python-ts-mode-map
           ("C-c C-v" . bci/python-check))

(You can also write [remap python-check] instead of "<remap> <python-check>", but the documentation uses the string form.)


On its own in init.el, those will cause an error (because the Python-specific variables aren't defined by default), so you can wrap it using one of these:

(use-package python
  :bind (:map python-base-mode-map
         ("C-x *" . whitespace-mode)
         ("<remap> <python-check>" . bci/python-check)))
(with-eval-after-load 'python
  (bind-keys :map python-base-mode-map
             ("C-x *" . whitespace-mode)
             ("<remap> <python-check>" . bci/python-check)))

(I'd personally recommend use-package, as it also provides other useful utility for package-specific customization, like setting hooks.)

2
  • Thank you for your helpful suggestion, @jirrassimok. I tried this before (and just now, to be sure), with no success. This time around, (with your use-package snippet), I was able to bind whitespace-mode but not my custom bci/python-check command. However, when I use my hook-based solution (which I have under the :config section of my python-mode use-package block), everything gets bound without fail. I think what makes the difference is that for my Python buffers I'm using python-ts-mode, which I have as a major-mode remap from python-mode.
    – bci_
    Commented Nov 23 at 22:35
  • 1
    @bci_ Oh, I see what the problem was; C-c C-v is already bound, so binding it in the parent map doesn't help. I've updated the answer to show two different ways to avoid that problem.
    – jirassimok
    Commented Nov 24 at 3:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.