【问题标题】:Emacs -- sort list of directories / files by modification dateEmacs -- 按修改日期排序目录/文件列表
【发布时间】:2014-10-23 04:02:09
【问题描述】:

函数(directory-files-and-attributes "~/" 'full nil t) 为主目录创建一个未排序 文件和目录列表。结果似乎采用类似于file-attributes 的格式,其文档可以在以下链接中查看:https://www.gnu.org/software/emacs/manual/html_node/elisp/File-Attributes.html

这个线程的目标是创建一个按修改日期/时间排序的列表——最新的在列表的开头,最旧的在列表的结尾。

最后,我想将该详细列表转换为仅包含文件/目录的绝对路径的简单列表——保持与上述排序相同的顺序。

【问题讨论】:

    标签: emacs elisp


    【解决方案1】:

    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))))
    

    使用 Common Lisp 排序功能可能更清楚地实现同样的效果:

    (cl-sort (directory-files-and-attributes "~")
             #'time-less-p
             :key #'(lambda (x) (nth 6 x)))
    

    现在您只想提取每个条目的第一个元素 - 使用 mapcar 将函数应用于列表的所有元素:

    (mapcar #'car
            (sort (directory-files-and-attributes "~")
                  #'(lambda (x y) (time-less-p (nth 6 x) (nth 6 y)))))
    

    【讨论】:

      猜你喜欢
      • 2016-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-01
      • 2011-12-25
      • 2012-08-09
      • 1970-01-01
      相关资源
      最近更新 更多