I have this type of code in my init.el each loads the features in a directly and it turns out that because the file names contained the - character they were not being loaded, as the regex checks for only alphanumeric filenames which are not hidden.

(let ((default-directory (expand-file-name "wsi/lisp" user-emacs-directory)))
  (normal-top-level-add-to-load-path (directory-files default-directory nil "[^\\.][a-z0-9]*")))

By the way I copied this from someone’s init.el and I’ve suffered from it for ages, an object lesson on how one should not copy init.el files and other Emacs lisp code without fully understanding the code in them.

I’m not sure but it looks like the regex will not even match file names with capitals in them.

I guess the question I want to ask is how to add arbitrary non-alphabetical characters to a regex matching alphanumeric characters, with the proviso that they are acceptable in file names…

  • xtifr@alien.topB
    link
    fedilink
    English
    arrow-up
    1
    ·
    1 year ago

    On a POSIX filesystem (MacOS, Linux, BSD, etc.), any character except ‘/’ can be part of a filename. So why are you limiting yourself to a couple of punctuation characters? The best regex to match filenames is: “[^/]+”

    If you’re using Windows, it gets a bit trickier, but for reading the filesystem, “[^/\\\\]+” seems like it should be adequate. If you want to make sure you can create a filename on Windows, you’d have to exclude a few more characters. Nevertheless, excluding invalid characters still seems like a better approach than trying to include all the valid ones.