1

I'm developing a set of minor modes that I use in my LaTeX copy-editing work.

I have, e.g.. a list of three minor modes stored in a variable:

(defvar latex-editing-mode
  '(preamble-editing-mode
    biblio-editing-mode
    proofs-editing-mode))

I need them to be active one at a time. E.g. when I turn-on biblio-editing-mode the other two modes must be turned-off.

I'm trying to write a function:

(defun activate-mode-in-list (MINOR_MODE MINOR_MODE_LIST)      
  (interactive)
  (let ((MINOR_MODE_LIST (delete MINOR_MODE MINOR_MODE_LIST)))

(mapcar (lambda (mode) (setq mode nil)) MINOR_MODE_LIST)
(setq MINOR_MODE t)))

I need to use it this way:

(activate-mode-in-list biblio-editing-mode latex-editing-mode)

because I want to use it to write a set of functions to be called with dedicated and consistent keybindings. Eg.

(defun activate-biblio-editing-mode ()
  "Activate `biblio-editing-mode` and deactivate the other ones
setted in the `latex-editing-mode` variable"
  (interactive)
  (activate-mode-in-list biblio-editing-mode latex-editing-mode))

And then:

(require 'preamble-editing-mode)
(global-set-key (kbd "<C-M-kp-0>") 'activate-preamble-editing-mode)

(require 'biblio-editing-mode)
(global-set-key (kbd "<C-M-kp-1>") 'activate-biblio-editing-mode)

;; ...

(require 'proofs-editing-mode)
(global-set-key (kbd "<C-M-kp-9>") 'activate-proofs-editing-mode)

But my activate-mode-in-list function doesn't work as I wrote it. I guess I'm getting lost in quoting stuff because evaluating:

(setq preamble-editing-mode nil)
(setq biblio-editing-mode t)

works as expected.

  1. Where am I doing wrong?
  2. Is there a better way to achieve my goal?

All I need is a function that activates a specific minor-mode and deactivate some other minor modes. I can store this set of minor modes in a variable.. In my workflow, "in most cases", only one minor mode of this set of minor modes should be active at a time (but in some cases I would have more then one active at the same time. In this case I'd open them in the standard way).

1 Answer 1

2

Compare this. The essential difference is using set instead of setq.

(defun activate-mode-in-list (minor-mode minor-mode-list)
  (dolist (mm  minor-mode-list)
    (set mm (eq mm minor-mode))))

Your feeling that "I'm getting lost in quoting stuff because evaluating" was not far off. You don't want to set the values of the local variables mode (bound by lambda) and minor-mode (bound by let). You want to set the values of the variables that those local variables are bound to.

The code I show here uses set to set the value of the value of local variable mm, which is one of your minor-mode variables, to t or nil, depending on whether that minor-mode variable is the input value, minor-mode.

E.g., for input minor-mode = proofs-editing-mode, during iteration, when mm is proofs-editing-mode we set the value of the symbol that is the value of mm, that is, proofs-editing-mode, to t (which is (eq proofs-editing-mode proofs-editing-mode)). Then when mm is, say, biblio-editing-mode, we set the value of mm, that is, biblio-editing-mode, to nil (which is (eq biblio-editing-mode proofs-editing-mode)).

14
  • I get this error: let: Attempt to set a constant symbol: nil
    – Gabriele
    Commented Mar 8, 2021 at 6:48
  • All I need is a function that activates a specific minor-mode and deactivate some other minor modes. I can store this set of minor modes in a variable.. In my workflow, "in most cases", only one minor mode of this set of minor modes should be active at time (but in some cases I would have more then one active at the same time. In this case i'd open them in the standard way).
    – Gabriele
    Commented Mar 8, 2021 at 6:53
  • I restored my original title because it is the very topic of my question. I tried to solve with the exaple i posted but maybe I'm doing the wrong way.
    – Gabriele
    Commented Mar 8, 2021 at 7:01
  • I get no error, using your defvar and inputting at the prompts (1) the name of one of your (preamble|biblio|proofs)-editing-mode and (2) latex-editing-mode. M-x activate... then does exactly what you requested.
    – Drew
    Commented Mar 8, 2021 at 16:11
  • My foult. I don't need to call it interactively but in this way: (activate-mode-in-list biblio-editing-mode latex-editing-mode). I update my question to better explain my purpose.
    – Gabriele
    Commented Mar 8, 2021 at 19:39

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.