At some point I thought I had understood what the OP wanted and edited the question to reflect that understanding, but the OP demurred and changed it back a bit, so I don't know if this will answer his current question. It is really an answer to the previous form of the question after my edit, but I still think there is a chance that this is really what the OP is after.
The basic idea is that we want a function that handles its argument exactly the way a minor mode does, so we figure out what a "real" minor mode function does and strip way all the minor mode paraphernalia, leaving only the argument handling. But the argument is supposed to enable or disable something, so we emulate this enabling/disabling by defining a variable and set it to t
to "enable" or to nil
to "disable" whatever it is we are enabling or disabling. For a "real" minor mode, the variable is a buffer-local variable with the same name as the mode function (e.g. abbrev-mode
is the name of the mode function and also the name of the mode variable), so we follow that convention.
With that as explanation, here's a minimal function that handles its argument the way the quoted paragraph in the question describes and "enables"/"disables" whatever it is we are trying to enable/disable by setting the corresponding variable. Here's the minimal code:
(defvar-local foo-bar nil)
(defun foo-bar (&optional arg)
(interactive
(list
(if current-prefix-arg
(prefix-numeric-value current-prefix-arg)
'toggle)))
(setq foo-bar
(cond
((eq arg 'toggle) (not foo-bar))
((and (numberp arg) (< arg 1)) nil)
(t t)))
foo-bar)
I assume that the variable should be buffer-local here, but it could be made global: just change the defvar-local
to defvar
. Initially, foo-bar
is nil
, so it is "disabled".
The tests that @antonio describes in his answer can be used to verify that everything is as it should be: after every test, you can evaluate the foo-bar
variable in the appropriate buffer to make sure its value is as expected.
BTW, I did this by macroexpanding a fictitious (define-minor-mode foo-bar "foo-bar docstring")
and ruthlessly eliminating everything but the argument handling and the state variable.
Although I hope this will answer the OP's "real" question, I am not holding my breath. Depending on how it goes, I might keep it, amend it or delete it.
abbrev-mode
(or really any minor mode function`, since they all implement the behavior you describe) implements its argument handling. Is that correct? – NickD Aug 06 '22 at 02:15M-x roto-abbrev
or I do(roto-abbrev 'toggle)
from lisp? Ifroto-abbrev
were a minor-mode function then the minor-mode would be toggled: what's the behavior you expect in this case? Similarly for all the other cases. – NickD Aug 06 '22 at 03:06pp-macroexpand-last-sexp
. Go to the definition ofabbrev-mode
(e.g. usingxref-find-definitions
). Then place your cursor after the definition and doM-x pp-macroexpand-last-sexp
. Now you can study yourself how the functionality has been implemented. – dalanicolai Aug 09 '22 at 08:44