help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Interactively finding file in a list of directories


From: Denis Bueno
Subject: Interactively finding file in a list of directories
Date: Fri, 9 Mar 2007 10:52:13 -0500

All-

I'm seeking advice on how to improve a bit of code I've written. As
the subject says, I would like to be able to visit a file by its name
but search a set of default directories, rather than having to specify
the directory. (This ability exists in Eclipse under the name "Find
resource.")

For example, assume there are 4 different files all named "find.jsp"
in different directories, and I would like to find a particular one. I
would do M-x find-resource, type "find", hit TAB, it I would see a
list that looks something like:

,----
| foobar/find.jsp
| baz/find.jsp
| duh/find.jsp
| blah/find.jsp
`----

I would then C-x o to the completion buffer, hit Return on the file I
want, and Emacs will visit it.

I have some code that works as a first approximation, but it has a few bugs:

   1. (the major problem) The completion folds all results which have
the same filename. Assuming the above example, the completion buffer
displays "find.jsp" but I have no idea which actual file that
corresponds to. There are no other listings of "find.jsp" in the
completion buffer.

   2. It doesn't handle suffixes properly. Assuming there were a file
"baz/find.txt" in the filesystem of previous example, it would be
listed.

Does anyone have any advice on how I can fix the above bugs?

-Denis


;; --- `find-resource' kind of like in Eclipse.

;; BUGS: for some reason the completing buffer shows filenames that have an
;; extension *not* in `find-resource-suffixes'.
(defvar find-resource-paths
 (list "~/")
 "*List of paths to search when using `find-resource'; format it
 like `load-path'.")
;(make-variable-buffer-local 'find-resource-paths)

(defvar find-resource-suffixes
 (list "")
 "*List of file suffixes used when doing completion in `find-resource'.")

(defun find-resource-add-path (path)
 "Add a path used by `find-resource'."
 (interactive "DPath: ")
 (add-to-list 'find-resource-paths path))

(defun find-resource (filename)
 "Find a file `filename' in `find-resource-paths', using
`find-resource-suffixes', and visit it. The structure of
`find-resource' cribbed from `load-library'."
 (interactive
  (list (completing-read "Find resource: "
                          'locate-file-completion
                          (cons find-resource-paths
                               find-resource-suffixes))))
 (let ((f (locate-file filename
                       find-resource-paths
                       (cons "" find-resource-suffixes))))
   (if f (find-file f)
     (message "Could not find `%S' after completing completing-read (bug)"
              filename))))




reply via email to

[Prev in Thread] Current Thread [Next in Thread]