如何在elisp中将未知的args列表映射到start-process? [英] How can I map an unknown list of args to start-process in elisp?

查看:94
本文介绍了如何在elisp中将未知的args列表映射到start-process?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我终于想学习elisp了,但是我并没有全神贯注于如何动态地将未知的参数列表映射到变量。

I'm finally trying to learn elisp but haven't wrapped my head around how to map an unknown list of arguments to variables dynamically.

这是一个有效的功能最多将三个参数传递给启动过程。但是我想将无数个args传递给该函数。

Here's a working function that passes up to three arguments to start-process. But I would like to pass an infinite number of args to the function.

(defun create-drush-buffer (command &rest a)
  (if (locate-dominating-file default-directory "includes/bootstrap.inc")
      (progn
        (setq opt1 (car a))
        (setq opt2 (cadr a))
        (setq opt3 (caddr a))
        (setq allopt (concat opt1 " " opt2 " " opt3))
        (setq b-name (concat "*drush " command " " allopt "*"))
        (if (buffer-live-p b-name)
            (switch-to-buffer b-name)
          (setq d-buffer (get-buffer-create  b-name))
          (with-current-buffer d-buffer
            (goto-char (point-min))
            (view-mode 1)
            (hl-line-mode 1)
            (if opt3
                (start-process "drush" (current-buffer) drupal-drush-program
                               command
                               opt1
                               opt2
                               opt3)
              (if opt2
                  (start-process "drush" (current-buffer)
                                 drupal-drush-program
                                 command
                                 opt1
                                 opt2)
                (if opt1
                    (start-process "drush" (current-buffer)
                                   drupal-drush-program
                                   command
                                   opt1)
                  (start-process "drush" (current-buffer)
                                 drupal-drush-program
                                 command))))
            (shrink-window-if-larger-than-buffer))
          (switch-to-buffer d-buffer)))
    (message (concat default-directory " is not a drupal project"))))

这是我调用的函数的一个示例希望使用create-drush-buffer。

Here's an example of a calling function I would like to work with create-drush-buffer.

(defun drush-sql-sync ()
  (interactive)
  (create-drush-buffer
   "sql-sync"
   "-y"
   "-d"
   "-v"
   "@cu.wstage1-education"
  "@cu.local-education"))

如何我可以做到这一点并使我的代码减少冗余吗?

How can I achieve this and make my code less redundant? Any help with code or even steering me to relevant documentation appreciated.

推荐答案

您可以使用 apply

You can use apply:


(应用功能和其余参数)

(apply FUNCTION &rest ARGUMENTS)

使用剩余的args调用FUNCTION,将最后一个args作为
args的列表。然后返回值FUNCTION返回。因此,(apply'+1 2'(3
4))返回10。

Call FUNCTION with our remaining args, using our last arg as list of args. Then return the value FUNCTION returns. Thus, (apply '+ 1 2 '(3 4)) returns 10.

这样,您可以调用启动过程使用类似的东西:

This way, you could call start-process using something like:

(apply 'start-process "drush" (current-buffer)
                             drupal-drush-program
                             command
                             a)

作为旁注,您不应使用 setq 创建临时变量,因为这会创建或修改全局变量(如果不存在名称相同的局部变量) 。而是使用 let

As a side note, you should not create temporary variables using setq, as this creates or modified global variables (if no local ones with the name exists). Instead, use let.

祝您的elisp项目好运!

Good luck with your elisp projects!

这篇关于如何在elisp中将未知的args列表映射到start-process?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆