2

I want to search through a fixed set of files and I use the tags-search command for it:

(defvar my-files-form
  '(list "/path/to/one/file"
         "/path/to/another/file"))

(defun search-my-files (regexp)
  (interactive "sSearch my files (regexp): ")
  (tags-search regexp my-files-form))

This works great when all files in my-files-form exist, but if, for instance, /path/to/one/file does not exist, then the command fails mid-search and will never look through the existing second file /path/to/another/file.

How can I ignore non-existing files in the search?

1 Answer 1

2

Filter the list with file-exists-p before passing it to tag-search to ignore non-existent files.

(defvar my-files-form
  '("/path/to/one/file"
    "/path/to/another/file"))

(defun search-my-files (regexp)
  (interactive "sSearch myfiles (regexp): ")
  (tags-search regexp (seq-filter 'file-exists-p my-files-form)))

Note that I got rid of the initial list in my-files-form. It's not needed, because you're already quoting the list of files.

References

  • seq-filter - This function returns a list of all the elements in sequence for which predicate returns non-nil.
  • file-exists-p - This function returns t if a file named filename appears to exist.
1
  • 1
    Works brilliantly. Thanks!
    – halloleo
    Commented Oct 14, 2022 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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