file - Emacs 口齿不清 : Concise way to get `directory-files` without "." and ".."?

标签 file emacs directory elisp interactive

函数directory-files返回 ...条目也是如此。虽然在某种意义上确实如此,只有这样函数才能返回所有现有条目,但我还没有看到包含这些条目的用途。另一方面,每次使用 directory-files我也写类似的东西

(unless (string-match-p "^\\.\\.?$" ... 

或者为了更好的效率
(unless (or (string= "." entry)
            (string= ".." entry))
   ..)

特别是在交互式使用( M-: )中,额外的代码是不可取的。

是否有一些预定义的函数可以有效地仅返回目录的实际子条目?

最佳答案

您可以将其作为原始函数调用的一部分来执行。

(directory-files DIRECTORY &optional FULL MATCH NOSORT)

If MATCH is non-nil, mention only file names that match the regexp MATCH.

所以:

(directory-files (expand-file-name "~/") nil "^\\([^.]\\|\\.[^.]\\|\\.\\..\\)")

或者:

(defun my-directory-files (directory &optional full nosort)
  "Like `directory-files' with MATCH hard-coded to exclude \".\" and \"..\"."
  (directory-files directory full "^\\([^.]\\|\\.[^.]\\|\\.\\..\\)" nosort))

虽然更类似于您自己的方法可能会产生更有效的包装器,真的。

(defun my-directory-files (directory &optional full match nosort)
  "Like `directory-files', but excluding \".\" and \"..\"."
  (delete "." (delete ".." (directory-files directory full match nosort))))

尽管这对列表进行了两次处理,并且我们知道我们希望排除的每个名称只有一个实例(并且它们很有可能首先出现),因此如果您是这样的事情,则可能是一个很好的解决方案希望经常处理大型目录:

(defun my-directory-files (directory &optional full match nosort)
  "Like `directory-files', but excluding \".\" and \"..\"."
  (let* ((files (cons nil (directory-files directory full match nosort)))
         (parent files)
         (current (cdr files))
         (exclude (list "." ".."))
         (file nil))
    (while (and current exclude)
      (setq file (car current))
      (if (not (member file exclude))
          (setq parent current)
        (setcdr parent (cdr current))
        (setq exclude (delete file exclude)))
      (setq current (cdr current)))
    (cdr files)))

关于file - Emacs 口齿不清 : Concise way to get `directory-files` without "." and ".."?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17164767/

相关文章:

java - 从文件读取时如何修复仅检查数组最后一行的用户名和密码

java - 如何像在 Netbeans IDE 中一样在 Emacs 中自动格式化代码

java - 探索文件所在文件夹的跨平台方式

java 如何在同名目录中创建.txt

angular - 在 Angular 中使用 TypeScript 对文件大小进行 Jasmine 单元测试

file - 如何使用 SSIS 包仅遍历目标中不存在的文件?

python - Emacs 中的颜色 Python 关键字参数

关闭缓冲区后显示的 Emacs 缓冲区

PHP For 循环不读取文件

c# - 是否可以每次读取一定数量的文件?