该函数为主目录(directory-files-and-attributes "~/" 'full nil t)
创建未排序的文件和目录列表.结果似乎是一种类似的格式file-attributes
,其文档可在以下链接中查看:https: //www.gnu.org/software/emacs/manual/html_node/elisp/File-Attributes.html
此线程的目标是创建一个按修改日期/时间排序的列表 - 列表开头的最新列表,列表末尾的最旧列表.
最后,我想将该详细列表转换为文件/目录的绝对路径的简单列表 - 保持与上述排序相同的顺序.
directory-files-and-attributes
返回一个列表.值得庆幸的是,有很多Lisp函数可以转换列表.
首先,您希望通过比较每个条目的第6个元素来对列表进行排序.您可以使用本机Emacs Lisp sort
函数来实现,该函数将比较函数作为第二个元素:
(sort (directory-files-and-attributes "~")
#'(lambda (x y) (time-less-p (nth 6 x) (nth 6 y))))
Run Code Online (Sandbox Code Playgroud)
使用Common Lisp排序函数可以更清楚地实现相同的目的:
(cl-sort (directory-files-and-attributes "~")
#'time-less-p
:key #'(lambda (x) (nth 6 x)))
Run Code Online (Sandbox Code Playgroud)
现在,您只想提取每个条目的第一个元素 - 用于mapcar
将函数应用于列表的所有元素:
(mapcar #'car
(sort (directory-files-and-attributes "~")
#'(lambda (x y) (time-less-p (nth 6 x) (nth 6 y)))))
Run Code Online (Sandbox Code Playgroud)