help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

(call-interactively FN) leaks prefix arg into FN when called from within


From: Göktuğ Kayaalp
Subject: (call-interactively FN) leaks prefix arg into FN when called from within an interactive command
Date: Tue, 24 Apr 2018 04:05:40 +0300
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/27.0.50 (gnu/linux)

So, I have the following function:

,----
| (defun gk-find-file (arg)
|   (interactive "P")
|   (call-interactively (if arg #'find-file #'ffap)))
`----

bound to C-x C-f.  That key sequence runs ffap, and with prefix argument
(i.e. C-u C-x C-f) it runs find-file.  But that's not what I want, I
want the inverse, so I swap find-file and ffap:

,----
| (defun gk-find-file (arg)
|   (interactive "P")
|   (call-interactively (if arg #'ffap #'find-file)))
`----

but with this version, it always runs find file.  After trying various
things I came up with this:

,----
| (defun gk-find-file (arg)
|   (interactive "P")
|   (if (null arg) (call-interactively #'find-file)
|     (let (current-prefix-arg)
|       (call-interactively #'ffap))))
`----

which worked as intended (yes I tried it w/o the let too).  So the
prefix argument to gk-find-file leaks to ffap, causing it to think it
got a prefix argument too.  Is this intentional, or a bug?
Nevertheless, this is really confusing.  And reading "(elisp) Prefix
Command Arguments", it does not really indicate this:

,----
|  -- Variable: current-prefix-arg
|      This variable holds the raw prefix argument for the _current_
|      command.  Commands may examine it directly, but the usual method
|      for accessing it is with ‘(interactive "P")’.
`----

This is a more generic example:

,----
| (defun foo (arg)
|   (interactive "P")
|   (call-interactively
|    (lambda (arg)
|      (interactive "P")
|      (message "%S %S %S" arg current-prefix-arg prefix-arg))))
| 
| M-x foo     => nil nil nil
| C-u M-x foo => (4) (4) nil
`----

But is it really the _current_ command when it's invoked by
call-interactively?  It was totally unexpected for me that the value of
current-prefix-arg leaks into a function F in (call-interactively F).  I
believe call-interactively should be, at least optionally, able to
suppress the prefix arg from the context it was called in.  Is this
expected behaviour?  If so, why is it not documented?

-- 
İ. Göktuğ Kayaalp       <https://www.gkayaalp.com/>
                         024C 30DD 597D 142B 49AC
                         40EB 465C D949 B101 2427




reply via email to

[Prev in Thread] Current Thread [Next in Thread]