1

I need to prompt the user for a file which is 1) not a directory 2) if already exists, is writable.

I know I can use read-file-name, but I'm not sure how to filter out the improper files. Specifically, I need to filter out those are either 1) a directory or 2) read-only (not writable).

1

The 6th argument to read-file-name is a PREDICATE that filters completion candidates (file names). C-h f read-file-name says, about it:

Sixth arg PREDICATE, if non-nil, should be a function of one argument; then a file name is considered an acceptable completion alternative only if PREDICATE returns non-nil with the file name as its argument.

The file name passed to the predicate is a relative name, so your predicate function may need to use expand-file-name to obtain an absolute file name. Then you will want to apply function file-directory-p to each candidate, to see if it's a directory, and apply function file-attribute-modes to each candidate, to see if it is writable.

See also (elisp) File Attributes for info about function file-attributes (search for file-attribute-modes in that manual node).

  • Thanks, I thought there will be a hidden pre-baked solution somewhere given this need seems to be common. – Yuan Fu Oct 18 at 13:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.