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

* RE: Interactively finding file in a list of directories
  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
  1 sibling, 0 replies; 6+ messages in thread
From: Drew Adams @ 2007-03-09 16:20 UTC (permalink / raw)
  To: Denis Bueno, help-gnu-emacs

> 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...
>
> 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 don't have a suggestion for your code, but you can do what you want with
Icicles, if you like, using command `icicle-locate-file'. Just use a regexp
that matches the directories and files you want to find. In this case, use
`find.jsp' (or `/find.jsp$', to be sure not to match files, such as
foofind.jsp or find.jsp2, whose names contain `find.jsp'). You will see
exactly the list of 4 completion choices you show above.

The search for matching files is limited to those under the current
directory (and subdirs etc.) or, if you use `C-u', to those under a
directory you name (which could be the root). See
http://www.emacswiki.org/cgi-bin/wiki/Icicles_-_File-Name_Input.

Besides Icicles, there are other packages that let you do something similar.
See http://www.emacswiki.org/cgi-bin/wiki/LocateFilesAnywhere.

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

* Re: Interactively finding file in a list of directories
  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
  1 sibling, 1 reply; 6+ messages in thread
From: Peter Dyballa @ 2007-03-09 20:51 UTC (permalink / raw)
  To: Denis Bueno; +Cc: help-gnu-emacs


Am 09.03.2007 um 16:52 schrieb Denis Bueno:

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

There is an interface to the UNIX locate database (M-x locate RET  
pattern RET). Regularly, once a day or once a weekly, the whole disk  
is searched and all path names, files and directories, but no  
"special files," are recorded in a database or hash file. The command  
locate takes as argument a search pattern that allows a few  
metacharacters and looks up this pattern in the database, so it's  
much faster than find. Executing locate as a shell command, you could  
filter a bit more with grep what locate returns ...

--
Greetings

   Pete

Think of XML as Lisp for COBOL programmers.
                               -- Tony-A (some guy on /.)

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

* RE: Interactively finding file in a list of directories
  2007-03-09 20:51 ` Peter Dyballa
@ 2007-03-09 21:25   ` Drew Adams
  2007-03-09 22:13     ` Denis Bueno
  0 siblings, 1 reply; 6+ messages in thread
From: Drew Adams @ 2007-03-09 21:25 UTC (permalink / raw)
  To: Peter Dyballa, Denis Bueno; +Cc: help-gnu-emacs

> > 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:
>
> There is an interface to the UNIX locate database (M-x locate RET
> pattern RET). Regularly, once a day or once a weekly, the whole disk
> is searched and all path names, files and directories, but no
> "special files," are recorded in a database or hash file. The command
> locate takes as argument a search pattern that allows a few
> metacharacters and looks up this pattern in the database, so it's
> much faster than find. Executing locate as a shell command, you could
> filter a bit more with grep what locate returns ...

The link I gave earlier provides info on several such Emacs interfaces to a
UNIX or GNU/Linux `locate' database:
http://www.emacswiki.org/cgi-bin/wiki/LocateFilesAnywhere.

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

* Re: Interactively finding file in a list of directories
  2007-03-09 21:25   ` Drew Adams
@ 2007-03-09 22:13     ` Denis Bueno
  2007-03-09 22:59       ` Peter Dyballa
  0 siblings, 1 reply; 6+ messages in thread
From: Denis Bueno @ 2007-03-09 22:13 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

On 3/9/07, Drew Adams <drew.adams@oracle.com> wrote:
> > > 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:
> >
> > There is an interface to the UNIX locate database (M-x locate RET
> > pattern RET). Regularly, once a day or once a weekly, the whole disk
> > is searched and all path names, files and directories, but no
> > "special files," are recorded in a database or hash file. The command
> > locate takes as argument a search pattern that allows a few
> > metacharacters and looks up this pattern in the database, so it's
> > much faster than find. Executing locate as a shell command, you could
> > filter a bit more with grep what locate returns ...
>
> The link I gave earlier provides info on several such Emacs interfaces to a
> UNIX or GNU/Linux `locate' database:
> http://www.emacswiki.org/cgi-bin/wiki/LocateFilesAnywhere.

Thanks. I've given up on rolling my own solution (indeed, the many
solutions I've seen thus far are much more feature-complete). I'm
current maintaining a filecache cache persistently -- using
instructions from the link above -- and that's working well.

Later I'll try to fix the only annoyances I've found -- I can't find
the scroll-in-the-reverse-direction counterpart to C-x TAB, and I'd
like to by able to find SomeFileName.java by typing So*F*.java. (I'm
not using icicles because I wanted a more lightweight solution, and
I'm not using GlobalFF because I'd like new files to show up more
often than the interval in which locate database is automatically
updated. I may yet  decide that these are stupid reasons not to use
these libraries.)

Thank you all for the help (both here and on emacs-devel, which was
admittedly the wrong list).

-Denis

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

* Re: Interactively finding file in a list of directories
  2007-03-09 22:13     ` Denis Bueno
@ 2007-03-09 22:59       ` Peter Dyballa
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Dyballa @ 2007-03-09 22:59 UTC (permalink / raw)
  To: Denis Bueno; +Cc: help-gnu-emacs


Am 09.03.2007 um 23:13 schrieb Denis Bueno:

> I'd like to by able to find SomeFileName.java by typing So*F*.java.

As shell-command:

	locate .java | egrep '/So[^/]*F[^/]*\.java$'

This would also find SoF.java.

--
Greetings

   Pete

“I hope to die before I *have* to use Microsoft Word.”
      - Donald E. Knuth, 2001-10-02 in Tübingen.

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