[英]ELISP interactive function with both prefix argument and user input as optional arguments
In ELISP, the documentation for interactive
codes mentions:在 ELISP 中, interactive
代码的文档提到:
P -- Prefix arg in raw form. P - 原始形式的前缀 arg。 Does not do I/O.不做 I/O。 ... s -- Arbitrary text, read in the minibuffer and returned as a string ... Prompt. ... s -- 任意文本,读入微型缓冲区并作为字符串返回 ... 提示。
I presumed that I could write a function with an optional prefix argument, as in:我假设我可以编写一个带有可选前缀参数的函数,如下所示:
(defun some-function (&optional prefix)
(interactive "P")
...
)
or a function with user input, as in:或具有用户输入的函数,如:
(defun some-function (user-argument)
(interactive "sProvide an argument: ")
...
)
but not both.但不是两者兼而有之。 Then I found the Org-mode function org-match-sparse-tree
, which I can call with Cu Cc \\
, where the prefix argument restricts the match to open org-mode headings and I am still prompted for a match.然后我找到了 Org-mode 函数org-match-sparse-tree
,我可以用Cu Cc \\
调用它,其中前缀参数将匹配限制为打开 org-mode 标题,并且仍然提示我进行匹配。 The source code is below and I cannot find how the variable match
is assigned:源代码如下,我找不到变量match
的分配方式:
(defun org-match-sparse-tree (&optional todo-only match)
"..."
(interactive "P")
(org-agenda-prepare-buffers (list (current-buffer)))
(let ((org--matcher-tags-todo-only todo-only))
(org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match))
org--matcher-tags-todo-only)))
How does this function take both prefix argument and user input?这个函数如何同时接受前缀参数和用户输入?
How does this function [interactively] take both prefix argument and user input?这个函数如何[交互地]接受前缀参数和用户输入?
It doesn't -- the match
argument is not obtained, and is therefore nil
.它没有 - 没有获得match
参数,因此是nil
。 What you're seeing is the effect of the subsequent call to (org-make-tags-matcher match)
with that nil
value as the argument:您所看到的是随后调用(org-make-tags-matcher match)
并使用该nil
值作为参数的效果:
(defun org-make-tags-matcher (match)
"..."
(unless match
;; Get a new match request, with completion against the global
;; tags table and the local tags in current buffer.
(let ((org-last-tags-completion-table
(org-tag-add-to-alist
(org-get-buffer-tags)
(org-global-tags-completion-table))))
(setq match
(completing-read
"Match: "
'org-tags-completion-function nil nil nil 'org-tags-history))))
...)
Functions can take multiple interactive
arguments, though.不过,函数可以采用多个interactive
参数。
See Ch f interactive
请参阅Ch f interactive
To pass several arguments to the command, concatenate the individual strings, separating them by newline characters.要将多个参数传递给命令,请连接各个字符串,并用换行符分隔它们。
The very first example in that help demonstrates this:该帮助中的第一个示例演示了这一点:
(defun foo (arg buf) "Doc string" (interactive "P\nbbuffer: ") .... )
This is elaborated upon at (elisp)Using Interactive
-- up one level in the documentation you'd linked to:这在(elisp)Using Interactive
有详细说明——在您链接到的文档中上一层:
It may be a string; its contents are a sequence of elements
separated by newlines, one for each argument(1). Each element
consists of a code character (*note Interactive Codes::) optionally
followed by a prompt (which some code characters use and some
ignore). Here is an example:
(interactive "P\nbFrobnicate buffer: ")
The code letter ‘P’ sets the command’s first argument to the raw
command prefix (*note Prefix Command Arguments::). ‘bFrobnicate
buffer: ’ prompts the user with ‘Frobnicate buffer: ’ to enter the
name of an existing buffer, which becomes the second and final
argument.
You should read that documentation fully, though -- there are more sophisticated things you can do, including writing arbitrary elisp to produce the interactive arguments (which may or may not involve prompting the user).不过,您应该完整地阅读该文档——您可以做更复杂的事情,包括编写任意 elisp 来生成交互式参数(这可能涉及也可能不涉及提示用户)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.