Search code examples
emacskey-bindingspython-mode

emacs, python-mode.el define-key map assignment


When I load the python-mode.el file in emacs, I don't get any of the key bindings specified. I started playing with the file and noticed that when I change:

(define-key map [(control c)(\#)] 'py-comment-region)

to:

(define-key global-map [(control c)(\#)] 'py-comment-region)

it works fine.

I went to look where the define the map variable and saw:

(defvar py-shell-map nil
  "Keymap used in *Python* shell buffers.")

;; used by py-completion-at-point, the way of python.el
(defvar python-shell-map
  (let ((map (copy-keymap comint-mode-map)))
    (define-key map [tab]   'py-shell-complete)
    (define-key map "\C-c-" 'py-up-exception)
    (define-key map "\C-c=" 'py-down-exception)
    map)
  "Keymap used in *Python* shell buffers.")

Is the 'map' variable defined alright? Should I do some changes to my init file? I assume that this file works to everybody else, so why I need to change the 'map' variable to 'global-map' for it to work in my computer?

I'm running in a virtual machine if that's of any help.


Solution

  • The code you quoted in which you believe map is defined is not actually the relevant portion of the code. It is a different keymap used for a python shell, and it's not the one used when you edit a python file in Emacs.

    The line you're editing appears in python-mode inside the following code block:

    (defvar python-mode-map)
    (setq python-mode-map
          (let ((map (make-sparse-keymap)))
            ;; electric keys
            (define-key map [(:)] 'py-electric-colon)
            (define-key map [(\#)] 'py-electric-comment)
            ...
    

    As you can see the variable map is first initialized as a "sparse keymap", then certain key-bindings get defined in that map, and finally the map is set as the value of python-mode-map. The latter is the keymap used in a buffer that is in python-mode.

    So the keybindings should work - but of course only in a buffer that is in python-mode. To activate python-mode in a buffer, type M-x python-mode. This works only after the file python-mode.el has been loaded.

    You can check if your current buffer is in python-mode in two ways:

    • your mode line should display the letters "Py"
    • type M-: ENTER major-mode ENTER -> this should print "python-mode" to the minibuffer