Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

emacs lisp listing files with glob expansion

Tags:

lisp

elisp

Are there any library or function that performs a bash-like glob expansion for emacs lisp?

For example:

(directory-files-glob "~/Desktop/*")
> ("/home/user/Desktop/file1" "/home/user/Desktop/file2")

If there isn't such a function are there any hint/suggestion on how to implement it?

EDIT:

I've found in the docs also an useful function that does quite exactly this:

file-expand-wildcards: This function expands the wildcard pattern pattern, returning a list of file names that match it.

like image 372
pygabriel Avatar asked Aug 18 '10 18:08

pygabriel


3 Answers

Check out the documentation for directory-files:

(directory-files "~/Desktop" nil ".")

Note: The third argument is a regular expression - not globbing.

It is straight forward to turn globbing patterns into regular expressions. eshell comes with a translation package which you can use:

(require 'em-glob)
(defun directory-files-glob (path)
   (directory-files (file-name-directory path) 
                    nil 
                    (eshell-glob-regexp (file-name-nondirectory path))))

And, if you want full exposure to eshell's globbing (with directories), there's probably a way to get that. The above assumes that the globbing part is in the non-directory portion of the path.

like image 77
Trey Jackson Avatar answered Nov 16 '22 03:11

Trey Jackson


Not sure why this was overlooked maybe it was not in emacs in 2010 but in a current emacs at least there is function file-expand-wildcards

(file-expand-wildcards "~/Desktop/*")

which does exactly what you want to do..

like image 23
Peer Stritzinger Avatar answered Nov 16 '22 03:11

Peer Stritzinger


Package f adds a huge amount of file and filepath manipulation functions under a consistent naming scheme. It has a function f-glob that does exactly that:

(f-glob "~/doc/*.org") ; returns a list of files ending with ".org"
(f-glob "*.org" "~/doc/") ; they all behave the same
(f-glob "*.org" "~/doc")
like image 5
Mirzhan Irkegulov Avatar answered Nov 16 '22 05:11

Mirzhan Irkegulov