2

I have a minor mode that declare a customizable variable as its keymap prefix:

(defcustom mymode-keymap-prefix "C-c *"
  "Prefix for `mymode-mode'."
  :group 'mymode)

(defvar mymode-mode-map
  (let ((map (make-sparse-keymap)))
    (define-key map (kbd mymode-keymap-prefix) some-mymode-command-map)
    map))

(define-minor-mode mymode-mode
  "Mymode."
  nil
  " mymode"
  mymode-mode-map
  :group 'mymode)

In my init.el somewhere I have (require 'mymode-mode). I want to make this keymap prefix customizable for user.
This customization must be done before mode is loaded, after that it's too late, mode keymap is already made.
I've tried to open customization buffer with customize-variable mymode-keymap-prefix and change it but it doesn't work in my case. When I query the variable after all is loaded it really have value I've put in it but my mode keymap still starts with old prefix as if nothing had happened. Only approach that work for me is to put (setq mymode-keymap-prefix <new_value>) before (require 'mymode-mode) in init.el.

3

Customizations can be loaded at different times, depending on what your users have in their init file. By default, the customizations are appended to the end of the init file (.emacs or .emacs.d/init.el). If this is the case, you'll need to add your require after the customization code in this file.

Customizations may also be stored in a separate file, set in the variable custom-file. If this is the case, they will be loaded explicitly with the command (load custom-file). In this case, your require should come after the (load custom-file) line.

Given different users will have different setups, it might be easiest to not to use the customization system for this variable, and just put (setq mymode-keymap-prefix <value>) in their init before the (require 'mymode-mode) - as you've already discovered.

  • Thanks! Does Emacs have something like pre-mode hooks - hooks that run at the beginning of mode loading? I know that regular hooks are run at the end of loading of specific mode. – foki Apr 6 '16 at 21:24
  • 1
    You can create a hook and run it anytime you want. Note that the macro define-minor-mode automatically creates your mode hooks and places them where they're supposed to go - one of the last things to happen when the mode is started. There's nothing to stop you from adding a new hook at the beginning of the mode definition (although it's not conventional). However, any hook you put in the body of the mode definition will be run too late here - the value of mymode-keymap-prefix is applied when you define mymode-mode-map, which happens before the mode is even defined let alone called. – Tyler Apr 7 '16 at 0:23
2

By loading packages with use-package instead of require you can specify code to run before the package is loaded:

(use-package mymode-mode
  :init ;; Runs before loading
  (setq mymode-keymap-prefix <new_value>)
)

Essentially all this does is put (setq mymode-keymap-prefix <new_value>) before the require as the other answer suggests, but it's a convienty way to bundle the "pre-loading" config with the the rest.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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