1

When I press the F9 key I want to call the ef-themes-select function and when I press F9 a second time to call the modus-themes-select function. I want to toggle between them each time I press the F9 key.

Both functions bring up a minibuffer completion in which a new theme can be selected. So, the first time I press F9, ef-themes-select will display a minibuffer to select a different Ef theme. The second time I press F9, modus-themes-select should be called. However, we need to take into consideration the second time F9 is pressed it is issued in the minibuffer that opened first.

    (use-package ef-themes
      :init
      (load-theme 'ef-summer t)
      :bind ("<f9>" . my/select-themes))
    
    (use-package modus-themes
      :bind ("<f9>" . my/select-themes))

Can someone help me write this function to do the behavior I'm looking for?

    (defun my/select-themes ()
    ;; ...
    )

The function doesn't need to remember the theme that is currently active, just call the functions in a toggling manner.

4
  • Are you sure you'd prefer this 'design'? Because the common approach for calling two different variants of a function is by using a universal argument. Feb 3, 2023 at 12:50
  • And if it's just one variant, can the F key be used? Then I'll use two different F keys instead.
    – Zoli
    Feb 3, 2023 at 12:57
  • Well @sds provided a nice answer already, of course. If always calling modus-themes-select first, and if calling ef-themes-select directly after it causes no further problems, e.g. that a second minibuffer gets opened or you see a message 'attempt to open another minibuffer from minibuffer', then the repeated F9 key solution is fine; it could even get adapted so that it closes the first mini-buffer before opening the second (which is probably what your comment about it is hinting at). I was just indicating the common alternative approach (which does not automatically mean the best approach) Feb 3, 2023 at 16:08
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Feb 3, 2023 at 16:38

1 Answer 1

2

Use last-command:

(defun my/select-themes ()
  (interactive)
  (if (eq last-command 'my/select-themes)
      (modus-themes-select)
    (ef-themes-select)))

If this design (F9 for ef-themes-select and F9 F9 for modus-themes-select) is what you want, this is the way to go.

If you want F9 to run ef-themes-select and C-u F9 to run modus-themes-select, you can use Prefix Command Arguments:

(defun my/select-themes (arg)
  (interactive "P")
  (if arg
      (modus-themes-select)
    (ef-themes-select)))
3
  • Thank you. A user mentioned above that using universal argument is better. I'm not sure what he meant by that. Is it a problem that I bind the function to an F key? As far as I know F5-F9 is free for the user to bind.
    – Zoli
    Feb 3, 2023 at 15:03
  • @Zoli: see edit
    – sds
    Feb 3, 2023 at 15:08
  • The first is the one I want. Thank you.
    – Zoli
    Feb 3, 2023 at 15:18

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.