Emacs - 如何提取列表的所有元素

时间:2014-07-03 18:20:49

标签: emacs elisp

我正在寻找一些帮助,请提取已在dired-mode中标记的文件和/或目录列表的所有元素。从本质上讲,如果有某种方法可以从(mapcar (lambda (filename) (file-name-nondirectory filename)) (dired-get-marked-files))的结果中删除括号,那么这就可以了。

(start-process
  "name-of-process"
  "*output-buffer*"
  "/usr/bin/zip"
  "zip-file-name.zip"
  (mapcar
    (lambda (filename) (file-name-nondirectory filename))
    (dired-get-marked-files)) )

我寻求的结果将如下所示:

(start-process
  "name-of-process"
  "*output-buffer*"
  "/usr/bin/zip"
  "zip-file-name.zip"
  "filename-number-one"
  "filename-number-two"
  "filename-number-three" )

修改

start-process函数通常不接受单个连接的参数字符串。相反,每个参数必须单独拼写(每个参数周围都有引号),参数可以是变量。

以上是上面第一个示例中的调试器消息 - 发生错误,因为文件名周围有一个括号 - 即它不能是列表。

Debugger entered--Lisp error: (wrong-type-argument stringp ("file-name-number-one" "file-name-number-two" "file-name-number-three"))
  start-process("name-of-process" "*output-buffer*" "/usr/bin/zip" "zip-file-name.zip" ("file-name-number-one" "file-name-number-two" "file-name-number-three"))
  eval((start-process "name-of-process" "*output-buffer*" "/usr/bin/zip" "zip-file-name.zip" (mapcar (lambda (filename) (file-name-nondirectory filename)) (dired-get-marked-files))) nil)
  (cons (eval exp lexical-binding) values)
  (setq values (cons (eval exp lexical-binding) values))
  (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error))
  (let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value)))
  (if (null eval-expression-debug-on-error) (setq values (cons (eval exp lexical-binding) values)) (let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value))))
  (let ((exp (if exp exp (read--expression "Eval: ")))) (if (null eval-expression-debug-on-error) (setq values (cons (eval exp lexical-binding) values)) (let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value)))) (let ((print-length (and (not (= 0 (prefix-numeric-value insert-value))) eval-expression-print-length)) (print-level (and (not (= 0 (prefix-numeric-value insert-value))) eval-expression-print-level)) (deactivate-mark)) (if insert-value (with-no-warnings (let ((standard-output (current-buffer))) (prog1 (prin1 (car values)) (if (= 0 ...) (progn ...))))) (prog1 (prin1 (car values) t) (let ((str (eval-expression-print-format ...))) (if str (princ str t)))))))
  (if (active-minibuffer-window) nil (let ((exp (if exp exp (read--expression "Eval: ")))) (if (null eval-expression-debug-on-error) (setq values (cons (eval exp lexical-binding) values)) (let ((old-value (make-symbol "t")) new-value) (let ((debug-on-error old-value)) (setq values (cons (eval exp lexical-binding) values)) (setq new-value debug-on-error)) (if (eq old-value new-value) nil (setq debug-on-error new-value)))) (let ((print-length (and (not (= 0 ...)) eval-expression-print-length)) (print-level (and (not (= 0 ...)) eval-expression-print-level)) (deactivate-mark)) (if insert-value (with-no-warnings (let ((standard-output ...)) (prog1 (prin1 ...) (if ... ...)))) (prog1 (prin1 (car values) t) (let ((str ...)) (if str (princ str t))))))))
  lawlist-eval-expression()
  funcall-interactively(lawlist-eval-expression)
  call-interactively(lawlist-eval-expression nil nil)
  command-execute(lawlist-eval-expression)

2 个答案:

答案 0 :(得分:5)

您想要的是apply使用(mapcar ...)作为其最后一个参数:

(apply 'start-process
       "name-of-process"
       "*output-buffer*"
       "/usr/bin/zip"
       "zip-file-name.zip"
       (mapcar #'file-name-nondirectory (dired-get-marked-files)))

请注意,(mapcar #'function list)拼写的缩写为(mapcar (lambda (arg) (function arg)) list)

答案 1 :(得分:0)

combine-and-quote-strings就是你想要的:

(combine-and-quote-strings (mapcar (lambda (x)
                                     (file-name-nondirectory x))
                                   (dired-get-marked-files)))

编辑:以下内容将为您提供带引号的单引号字符串。不确定它是否能与start-process完美搭配:

(mapconcat
    (lambda (x)
      (concat "\"" (file-name-nondirectory x) "\""))
    (dired-get-marked-files) " ")

编辑:Righty-o,让我们试试吧。使用,@拼接反引号列表,然后eval整个事项:

(eval `(start-process
        "name-of-process"
        "*output-buffer*"
        "/usr/bin/zip"
        "zip-file-name.zip"
        ,@(mapcar
           (lambda (x)
             (file-name-nondirectory x))
           (dired-get-marked-files))))