2

Say I'm developing a minor mode A that happens to need minor mode B enabled during its lifetime.

The most straightforward way is to do something like:

(define-minor-mode A-mode
  :global t
  :lighter " A"
  (if A-mode
      (B-mode 1)
    (B-mode 0))) ; hooks here are added/removed...

Or alternatively:

(add-hook 'A-mode-hook 'B-mode)

The problem with these approaches is that the original status is not preserved. I could simply use a global variable to keep track of it but I was hoping to find a more elegant solution.

For example I can solve a similar issue involving variables like this:

(progn
  (setq foo 10)
  (print foo)     ; 10
  (let ((foo 42))
    (print foo))  ; 42
  (print foo))    ; 10

There is one more problem: the user could manually disable minor mode B while minor mode A is still on; and this shouldn't happen.

Probably is just bad practice to even require this behavior...

(FYI in my case minor mode B is temp-buffer-resize-mode.)

  • What about buffer-local variables? – DoMiNeLa10 Oct 17 '17 at 16:10
  • I'm unsure about how they can be useful in this case, can you provide a minimal example? – cYrus Oct 17 '17 at 16:15
  • I haven't noticed you were defining a global minor mode. That changes things a little bit. – DoMiNeLa10 Oct 17 '17 at 16:37
2

The problem with these approaches is that the original status is not preserved. I could simply use a global variable to keep track of it but I was hoping to find a more elegant solution.

Well I think you'll essentially need to do that, but it needn't actually be a defvar'd variable.

If you're using lexical binding, then you can probably just use that.

Alternatively, as it's a global value with no other use-case, you could store it as a symbol property on A-mode.

(put 'A-mode 'B-mode-enabled (bound-and-true-p B-mode))

and then when disabling A-mode:

(unless (get 'A-mode 'B-mode-enabled)
  (B-mode 0))

There is one more problem: the user could manually disable minor mode B while minor mode A is still on; and this shouldn't happen.

A-mode can always add to B-mode-hook to at least identify when that happens, even if it doesn't try to actively counter-act it.

Probably is just bad practice to even require this behavior...

Perhaps. temp-buffer-resize-mode is just acting on temp-buffer-show-hook to call resize-temp-buffer-window, which is something you could probably adapt for A-mode's specific needs.

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.