I want to write an Emacs Lisp function that will turn on flyspell-mode
regardless of the current state of the mode. Function flyspell-mode-on
is deprecated. The documentation suggests that a positive prefix argument will turn flyspell-mode
, but unfortunately running
(flyspell-mode 1)
results in an error message:
Wrong number of arguments: (lambda (flyspell-mode 1)), 0
If I could figure out how to call flyspell-mode
with a prefix argument, I believe I could solve this problem.
The most relevant section I can find in the Emacs Lisp manual is the section entitled "Interactive Call", which describes such commands as call-interactively
. This is emphatically not what I want.
(The ultimate problem I am trying to solve is to create a mode hook that turns on the mode regardless of its current state.)
N.B. The title of the question emacs lisp call function with prefix argument programmatically makes it appear to be related, but that question was asking about how to create an interactive command, and the issue was ultimately resolved by using call-interactively
.
EDIT: This question is moot; I have found an alternate solution to my original problem:
(add-hook 'text-mode-hook
(function (lambda ()
(require 'flyspell)
(if flyspell-mode nil (flyspell-mode)))))
But I would still like to know how to call an Emacs Lisp function with a prefix argument, from another Emacs Lisp function, with nothing interactive.
UPDATE: Perhaps I should have asked why I was getting that error message...
If you are not calling a function interactively, then the
(interactive)
declaration is not used to obtain the arguments.In the vast majority of cases, you do not need to worry about whether an argument can be a "prefix argument" for non-interactive calls; just check the function documentation, and pass the value you need for whatever it is you want to do.
If for some reason you do need to replicate sending a prefix argument in a non-interactive context, you will need to check that function's
(interactive)
declaration and determine exactly how it is using that argument, and ensure that you replicate that behaviour for the argument you do pass.For full details, see:
interactive
RET(info "(elisp) Prefix Command Arguments")
RETIn more complex cases where the function changes its behaviour based on the
current-prefix-arg
variable, you may be able to set that variable directly.It looks like your version of Flyspell mode does not follow the minor mode conventions, which require that you can turn on a minor mode with
(name-of-mode t)
or any positive prefix argument, turn it off with(name-of-mode 0)
any negative prefix argument, and toggle it with(name-of-mode nil)
.If you have the latest version of Flyspell, a bug report might be in order. I have the version shipped with GNU Emacs 23.2 on my machine, and it respects the convention. My version also defines two functions
turn-on-flyspell
andturn-off-flyspell
, both trivial wrappers aroundflyspell-mode
; functions with such names are common, but not official conventions. The functionsflyspell-mode-on
andflyspell-mode-off
are apparently intended for internal use.As a general matter, commands read the current prefix argument from the
current-prefix-arg
variable. Don't confuse that withprefix-arg
, which is the value for the next command (only a few commands likeuniversal-argument
touch this variable). Thus, if you need to pass a prefix argument when calling a function, bind or setcurrent-prefix-arg
.I can think of this.. Should be more better
UPDATE: I can run this directly.. what am i missing from the question.?
EDITED: Removed quote for lambda expression (I added this note because SX forces an edit to be at least six characters long, so this can be deleted).
I'm not Emacs and Elisp master (yet ;)) but I think in this case you may use Ctrl-u
1
Alt-xflyspell-mode
.See my comment for the fix to the source of your problem. As for the answer to your question, the way the prefix arg is turned into some kind of Lisp argument depends on the
interactive
spec, so the only way to do it reliably (i.e. without prior knowledge such as the fact that it's a minor mode function) is to call the function interactively:FWIW, the `flyspell-mode' function has accepted an argument (as in "(flyspell-mode 1)") at least since Emacs-21, so I don't know how you got that error.
But while I'm here, I might as well point out that (add-hook 'text-mode-hook 'flyspell-mode) has changed meaning in Emacs-24: instead of meaning "toggle flyspell-mode in text modes" it will now mean "enable flyspell-mode in text modes". It's a backward-incompatible change, but I believe it will fix more latent bugs than it will introduce.