tags:

views:

249

answers:

3

In emacs when you type M-x find-name-dired it will give you two prompts. The first is what directory path you want to search in, and the second is what the file pattern you would like to search for.

How do I change it (in my .emacs) so the first prompt is always a specific project directory?

Also, how do I make it so the file name search is case insensitive?

+5  A: 

To get a different starting directory, you can advise the function to read the arguments using a different starting directory like so:

(defadvice find-name-dired (before find-name-dired-with-default-directory activate)
  "change the argument reading"
  (interactive
   (let ((default-directory "/some/path"))
     (call-interactively 'get-args-for-my-find-name-dired))))

(defun get-args-for-my-find-name-dired (dir pattern)
  (interactive "DFind-name (directory): \nsFind-name (filename wildcard): ")
  (list dir pattern))

And then you just call my-find-name-dired.

Regarding case insensitivity, you can customize the variable find-name-arg to be the case insensitive version:

(setq find-name-arg "-iname")
Trey Jackson
Thanks Trey, that worked great
RobKohr
+1  A: 

I suspect Trey's answer is probably elegant and preferred for some reasons that hurts my brain whenever I try to grok (defadvice) but I would take the brute force simple approach and use the following:

(setq my-dired-default-dir "/home/fred/lib")

(defun my-find-name-dired (pattern)
  "My version of find-name-dired that always starts in my chosen folder"
  (interactive "Find Name (file name wildcard): ")
  (find-name-dired my-dired-default-dir pattern))

I'm guessing that I lose history with this approach so if that is important to you Trey's approach is better.

One of these days I have to wrap my head around (defadvice)

pajato0
The best way to understand how to use advice is to go through the tutorial at the beginning (in the comments) of the advice library itself. Either `M-x find-library advice RET` or look at the source here: http://git.savannah.gnu.org/cgit/emacs.git/tree/lisp/emacs-lisp/advice.el
Trey Jackson
I agree, Trey's was a little beyond me, and yours made more sense, though for some reason the pattern yours defaulted to was ~/ (this was not asking for the directory, but the actual file pattern).Pajato0, can you do a minor edit to your answer so I can up up-vote it. Voting seems to be locked on your answer.
RobKohr
A: 

After I posted this I discovered another way of resolving this issue:

(add-hook 'find file-hook
 (lambda ()
  (setq default-directory command-line-default-directory)))

This left as a default the directory that I started emacs in (which is useful so you don't have to change your project directory in your emacs all the time).

In reality I think it is saying that every time you run find file, change the default directory to your starting command line directory.

Another thing could be to set some default directory: (setq my-default-directory "/home/rob/whatever")

and replace command-line-default-directory with my-default-directory/

RobKohr