Search code examples
macosemacskey-bindingsspacemacsevil-mode

What is proper way to define define-key in emacs


I am trying to make toggle insert/normal mode in evil in spacemacs. I have successfully accomplished that with code down there. But LED light next to Caps-lock on my macbook pro 2018 13' stoped working. So i am trying to switch led on my caps-lock with script.

I am using karabiner-elements to remap caps-lock to f13. When emacs records f13 it changes state to evil-normal-state or evil-insert-state.

The problem starts, when i want to add another command to run when f13 aka caps-lock ist pressed. Its (shell-command "/Users/atrumoram/setleds +caps"). Which turns the light on caps-lock or turns it off. I was trying to make my own function defun. But i really cannot make it work together. In the end i would like to have something like this.

This is code that toggles insert/normal mode in evil using capslock

  (define-key evil-insert-state-map (kbd "<f13>") 'evil-normal-state)
  (define-key evil-normal-state-map (kbd "<f13>") 'evil-insert-state)

In the end i would like to have something like this.

  (define-key evil-insert-state-map (kbd "<f13>") 'evil-normal-state (shell-command "/Users/atrumoram/setleds +caps"))
  (define-key evil-normal-state-map (kbd "<f13>") 'evil-insert-state (shell-command "/Users/atrumoram/setleds -caps"))

Is there some way somebody can help me out? Looking forward for your ideas.


Solution

  • A key can only be bound to one function at a time, so in order for two things to happen when you press the key, you need to create a function, make it "interactive", and bind that to the key you want.

    This function performs the two actions, one after the other:

    (defun my-evil-normal-state-and-set-caps-led ()
      (interactive)
      (evil-normal-state)
      (shell-command "/Users/atrumoram/setleds +caps"))
    

    Since it's declared as an interactive function, you can test it with M-x my-evil-normal-state-and-set-caps-led.

    Then it's just a matter of:

    (define-key evil-insert-state-map (kbd "<f13>") 'my-evil-normal-state-and-set-caps-led)
    

    And vice versa for switching to insert state.