unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Interactively finding file in a list of directories
@ 2007-03-09 15:52 Denis Bueno
  2007-03-09 16:20 ` Drew Adams
  2007-03-09 20:51 ` Peter Dyballa
  0 siblings, 2 replies; 6+ messages in thread
From: Denis Bueno @ 2007-03-09 15:52 UTC (permalink / raw)
  To: help-gnu-emacs

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))))

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2007-03-09 22:59 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-03-09 15:52 Interactively finding file in a list of directories Denis Bueno
2007-03-09 16:20 ` Drew Adams
2007-03-09 20:51 ` Peter Dyballa
2007-03-09 21:25   ` Drew Adams
2007-03-09 22:13     ` Denis Bueno
2007-03-09 22:59       ` Peter Dyballa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).