8

I was reading through the projectile source file and they do the following:

(defvar projectile-command-map
  (let ((map (make-sparse-keymap)))
    ; define-keys
    map)
  "Keymap for Projectile commands after `projectile-keymap-prefix'.")
(fset 'projectile-command-map projectile-command-map)

Why do they do this? Is it for backwards compatibility?

1
  • 1
    I'm very much aware of how keymaps work, yet I couldn't tell you why they do this. You might like to ask them.
    – Stefan
    May 15, 2017 at 4:09

1 Answer 1

10

fset sets a symbol's function definition.

Here, projectile-command-map is used as a prefix command. A prefix command is a symbol whose function definition is a keymap.

The definition of a prefix key is usually the keymap to use for looking up the following event. The definition can also be a Lisp symbol whose function definition is the following keymap; the effect is the same, but it provides a command name for the prefix key that can be used as a description of what the prefix key is for.

This can also be done with define-prefix-command.

What's a bit confusing here is that the same symbol, projectile-command-map, is being used for two purposes:

  • As a keymap
  • As a prefix command

Another example may be clearer:

(fset 'help-command help-map)

Here, help-command is clearly a prefix command, while help-map is a keymap.

8
  • See also C-h f defalias (similar to fset). See (elisp)Prefix Keys for info about prefix commands.
    – Drew
    May 12, 2017 at 22:44
  • What's a bit confusing here is that the same symbol is being used for two purposes: prefix keymap and prefix command. It'd be clearer if they were two different symbols, as with help-command and help-map. May 12, 2017 at 22:59
  • Maybe so, but that's the most common way to do it (a cliche) - use the same symbol. (grep for fset in the Lisp sources, then M-x flush offset and search for map.)
    – Drew
    May 12, 2017 at 23:47
  • 1
    It's common and I'd say the recommended way in order to avoid defining extra symbols, but it's confusing to those who don't have a good understanding of how keymaps work. I just made the comment to be more explicit for OP. May 13, 2017 at 0:35
  • Yes, I understand why you made the comment, and I agree it can be confusing. I don't think the reason is to avoid extra symbols. The reason is to have the same symbol enjoy both behaviors. It's a programming cliche, but the relation between the two (whether the same name or not) is documented as such. It is a command, so it can be bound to a key. But it is also a variable, so it can act as a keymap. This is Lisp-2. ;-)
    – Drew
    May 13, 2017 at 4:31

Your Answer

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

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