ELISP 交互函数,将前缀参数和用户输入作为可选参数

mig*_*rin 3 lisp arguments elisp function option-type

interactive在 ELISP 中,代码文档提到:

P——原始形式的前缀 arg。不进行 I/O。... s -- 任意文本,读入迷你缓冲区并作为字符串返回...提示。

我认为我可以编写一个带有可选前缀参数的函数,如下所示:

(defun some-function (&optional prefix)
    (interactive "P")
    ...
)
Run Code Online (Sandbox Code Playgroud)

或具有用户输入的函数,如下所示:

(defun some-function (user-argument)
  (interactive "sProvide an argument: ")
  ...
)
Run Code Online (Sandbox Code Playgroud)

但不是两者兼而有之。然后我找到了 Org-mode 函数org-match-sparse-tree,我可以使用它来调用C-u C-c \,其中前缀参数限制匹配以打开 org-mode 标题,并且仍然提示我进行匹配。源代码如下,我找不到变量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)))
Run Code Online (Sandbox Code Playgroud)

该函数如何同时获取前缀参数和用户输入?

phi*_*ils 5

\n

这个函数如何[交互地]接受前缀参数和用户输入?

\n
\n\n

它没有——match没有获得参数,因此是nil(org-make-tags-matcher match)您所看到的是使用该nil值作为参数进行后续调用的效果:

\n\n
(defun org-make-tags-matcher (match)\n  "..."\n  (unless match\n    ;; Get a new match request, with completion against the global\n    ;; tags table and the local tags in current buffer.\n    (let ((org-last-tags-completion-table\n           (org-tag-add-to-alist\n            (org-get-buffer-tags)\n            (org-global-tags-completion-table))))\n      (setq match\n            (completing-read\n             "Match: "\n             \'org-tags-completion-function nil nil nil \'org-tags-history))))\n  ...)\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

不过,函数可以接受多个interactive参数。

\n\n

C-hf interactive

\n\n
\n

要将多个参数传递给命令,请连接各个字符串,并用换行符分隔它们。

\n
\n\n

该帮助中的第一个示例演示了这一点:

\n\n
(defun foo (arg buf) "Doc string" (interactive "P\\nbbuffer: ") .... )\n
Run Code Online (Sandbox Code Playgroud)\n\n

(elisp)Using Interactive您链接到的文档中的上一层对此进行了详细说明:

\n\n
It may be a string; its contents are a sequence of elements\nseparated by newlines, one for each argument(1).  Each element\nconsists of a code character (*note Interactive Codes::) optionally\nfollowed by a prompt (which some code characters use and some\nignore).  Here is an example:\n\n     (interactive "P\\nbFrobnicate buffer: ")\n\nThe code letter \xe2\x80\x98P\xe2\x80\x99 sets the command\xe2\x80\x99s first argument to the raw\ncommand prefix (*note Prefix Command Arguments::).  \xe2\x80\x98bFrobnicate\nbuffer: \xe2\x80\x99 prompts the user with \xe2\x80\x98Frobnicate buffer: \xe2\x80\x99 to enter the\nname of an existing buffer, which becomes the second and final\nargument.\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

不过,您应该充分阅读该文档——您可以做更复杂的事情,包括编写任意 elisp 来生成交互式参数(可能涉及也可能不涉及提示用户)。

\n