Emacs Lisp:mapcar中的列表
我使用的函数使用mapcar将(简单)函数应用于列表的所有成员,如下所示:Emacs Lisp:mapcar中的列表,emacs,elisp,Emacs,Elisp,我使用的函数使用mapcar将(简单)函数应用于列表的所有成员,如下所示: (mapcar 'my-concat-function '( "/path/one.php" "/path/two.php")) (mapcar 'my-concat-function '( (directory-files "/path/"
(mapcar 'my-concat-function '(
"/path/one.php"
"/path/two.php"))
(mapcar 'my-concat-function '(
(directory-files "/path/" nil "\\.php$")))
但是我想使用目录文件
生成文件列表并对其进行过滤,如下所示:
(mapcar 'my-concat-function '(
"/path/one.php"
"/path/two.php"))
(mapcar 'my-concat-function '(
(directory-files "/path/" nil "\\.php$")))
但我总是得到一份工作
find-file-noselect: Wrong type argument: stringp, (directory-files "/path/" nil "\\.php$")
当我评估
(directory-files "/path/" nil "\\.php$")
它回来了
("one.php" "two.php" "three.php" ...)
(我没有添加“…”;Emacs添加了。无论列表大小,它似乎总是以“…”结尾)
问题:
如何格式化目录文件的输出
,使其生成mapcar想要的内容,一个原子列表,我真的不知道如何调用此表单:
"one.php" "two.php" "three.php"
没有括号,没有那些奇怪的“…”
编辑
当我尝试所建议的表单时(谢谢大家),作为mapcar的第一个参数引用的函数不起作用(regexp找不到任何东西,所有文件都在空(?)缓冲区中打开):(
这是完整的代码,非常感谢您的帮助,这很奇怪,这个函数只花了很少的时间编写,现在我在这个简单的列表问题上被阻止了几个小时,arg
(defun px-bpm-parse (fname)
"Extract elements. Basic Project Management."
(setq in-buf (set-buffer (find-file fname)))
(setq u1 '())
(setq u2 '())
(setq u3 '())
(setq project-dir "/var/www/html/microlabel.git/")
(beginning-of-buffer)
(while
(re-search-forward "^.*<link.*href=\"\\([^\"]+\\)\".*rel=\"stylesheet\"" nil t)
(when (match-string 0)
(setq url (match-string 1) )
(setq u3 (cons (concat "[[file:" project-dir url "][" url "]]\n") u3))))
(beginning-of-buffer)
(while
(re-search-forward "^.*<a.*href=\"\\([^\"]+\\)\"[^>]+>\\([^<]+\\)</a>" nil t)
(when (match-string 0)
(setq url (match-string 1) )
(setq title (match-string 2) )
(setq u1 (cons (concat "[[file:" project-dir url "][" title "]]\n") u1))))
(beginning-of-buffer)
(while
(re-search-forward "^.*<script.*src=\"\\([^\"]+\\)\"" nil t)
(when (match-string 0)
(setq url (match-string 1) )
(setq u2 (cons (concat "[[file:" project-dir url "][" url "]]\n") u2))))
(beginning-of-buffer)
(progn
(with-current-buffer "BPM.org"
(insert "** File: ")
;; (org-insert-link &optional COMPLETE-FILE LINK-LOCATION DEFAULT-DESCRIPTION)
(insert fname)
(insert "\n*** HREF Links (by name)\n")
(mapcar 'insert u1)
(insert "\n*** SCRIPT Links\n")
(mapcar 'insert u2)
(insert "\n*** CSS Links\n")
(mapcar 'insert u3)
(insert "\n\n"))
(switch-to-buffer "BPM.org")
(org-mode)))
(defun px-bpm ()
;; (defun px-bpm (prj-root)
"List all links"
(interactive)
;; (interactive "sEnter project root directory ")
(progn
(with-current-buffer (get-buffer-create "BPM.org")
(insert "* File dependencies\n\n"))
;; (mapcar 'px-bpm-parse '(
;; "/var/www/html/microlabel.git/add.php"
;; ))
(mapcar 'px-bpm-parse (directory-files "/var/www/html/microlabel.git/" nil "\\.php$"))
))
(defun px bpm解析(fname)
“提取元素。基本项目管理。”
(buf中的setq(设置缓冲区(查找文件fname)))
(setq u1’())
(setq u2’())
(setq u3’())
(setq项目目录“/var/www/html/microlabel.git/”)
(缓冲区的开始)
(而
(向前搜索“^.*]+>\\”([^当您计算表单并看到表单的结果(x y z…
)时,由于输出很长,所以它只是以这种方式打印出来。结果实际上是您期望的列表。例如
(list 1 2 3 4 5 6 7 8 9 10 11 12 13)
;=> (1 2 3 4 5 6 7 8 9 10 11 12 ...)
然而,列表的最后一个元素应该是:
(last (list 1 2 3 4 5 6 7 8 9 10 11 12 13))
;=> (13)
由于(directory files)/path/“nil”\\\.php$”
返回一个列表,并且mapcar的第二个参数应该是一个列表,因此可以将其设为第二个参数:
(mapcar 'my-concat-function (directory-files "/path/" nil "\\.php$"))
当您对某个内容求值时,如果求值消息很长,则会将其截断。如果您想查看所有内容,请执行以下操作:(消息“%s”(目录文件)/path/“nil”\\\.php$)
看起来您有一组额外的括号和引号,而不是”((目录文件…)
,请尝试纯旧的(目录文件…
,它将返回您需要的列表。@Dan这是正确的解决方案,但没有说明OP认为它不起作用的原因:返回的列表打印为(x y z…)并且…不是其中一个文件。函数顶部的所有setq
s都应该在let
中,否则它们将设置全局变量。(let(u1 u2 u3(project dir)/var/www/html/microlabel.git/))还有,你为什么在BUF 中,>代码> SEQ ING <代码>,尤其是因为你从不使用它?同意你应该接受这个答案,因为它回答了你原来的问题;如果你在函数中的其他地方遇到麻烦,就考虑打开一个新的问题。顺便说一下,代码>(SEQQ LST(CONS X LST))
通常写为(push x lst)
。关于这个“…”的事情,好吧;但是Dan,(目录文件)/path/“nil”\\\.php$”
=(“one.php”“two.php”“three.php”)
的输出在技术上与我实际传递给mapcar的内容不同:“one.php”“two.php”“three.php”“
所以我真的需要一个表单(可能使用lambda)来准确地返回这个。效果不同的事实似乎说明了这一点,对吗?@xaccrocheur我不是Dan,但你已经对我的答案发表了评论。(目录文件)/path/“nil”\\\\.php$”的结果
目录中以.php
结尾的文件列表。您可以检查您喜欢的任何文件(例如(长度(目录文件…)
)。当您看到(
,Emacs正在打印列表的前几个元素,然后打印…
,以显示有更多的元素。就像我列出从1到13的整数的示例一样,结果是1到13的列表,但为了方便起见,Emacs将其打印为(1 2 3 4 6 8 9 10 11 12…)
@xaccrocheur请尝试我答案中的最后一行代码;我很确定您会发现它做了您期望的事情。mapcar
的第二个参数必须是一个列表。mapcar
将第一个参数(您的函数)应用于列表的每个元素。也就是说,它将函数映射到每个后续的汽车(列表前面的项目)。@xaccrocher目录文件返回字符串列表。如果您在(mapcar'foo(目录文件…)
中遇到错误,则是因为foo
导致错误,而不是mapcar。