3

I would like to use the F9 key to toggle between the modus-themes-select and ef-themes-select functions. When I call the ef-themes-select, a minibuffer pops up to select an Ef theme:

enter image description here

Now when I press the F9 one more time I want to switch to the modus-themes-select instead, and when I press the F9 again then switch back to the ef-themes-select and so on. I want to toggle between the two functions by pressing F9:

enter image description here

I would be grateful if someone could help me write a custom function for that. Thank you.

1
  • Potentially (perhaps depending on what those commands you've mentioned actually do) your custom command could act based on last-command. Refer to C-h i g (elisp)Command Loop Info. Note that it's typical to (setq this-command ...) when a command is pretending to be a different command -- so if you have a custom command which simply calls either A or B interactively, it can set that so that everything else also thinks that's the command which was called.
    – phils
    Apr 2, 2023 at 11:03

2 Answers 2

4

Here are two simple ways to do what you request (using predefined commands end|beginning-of-line, so you can see it easily).

Old school (essentially what you came up with):

(defvar bol/eol 'end-of-line "Current bol/eol command.")

(defun bol/eol ()
  "Alternately invoke `beginning-of-line' and `end-of-line'."
  (interactive)
  (funcall (prog1 (setq bol/eol  (if (eq bol/eol 'end-of-line)
                                     'beginning-of-line
                                   'end-of-line))
             bol/eol)))
(global-set-key (kbd "<f9>") 'bol/eol)

Newer school:

(define-minor-mode bol/eol-mode
  "Alternately invoke `beginning-of-line' and `end-of-line'."
  :global t
  (funcall (if bol/eol-mode 'beginning-of-line 'end-of-line)))

(global-set-key (kbd "<f9>") 'bol/eol-mode)

(define-minor-mode auto-creates a global variable named the same as the mode command, which holds the current state.)

1

My unskilled snippet, would work anyway..

(setq-default my-themes-select-flag nil) ;; this is a global flag to have only `t` or `'nil`.

(defun my-themes-select ()
  "Calls modus-themes-select and ef-themes-select alternately."
  (interactive)
  (if my-themes-select-flag
      (modus-themes-select) ;; when the flag is t.
    (ef-themes-select)) ;; when the flag is nil.
  (setq my-themes-select-flag (not my-themes-select-flag))) ;; invert the flag after every call.

(define-key global-map (kbd "<f9>") 'my-themes-select)

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.