tags:

views:

909

answers:

3

I use shell-command a lots (default binded to M-!), but often I'm doing stuff to the buffer I'm currently editing. Instead of typing the buffer name (and no filename completion is available in shell-command, alas!), it'd be nice to have a shortcut key, say f3, to insert that name for me whenever I press the key.

The problem is I don't want to bind the key globally (I use f3 for other things in other context), only in minibuffer when shell-command is prompting. It's easy to write a lisp function to insert the current buffer name, but what mode's keymap should I modify to bind a key to that function?

Alternatively, is there any lisp code/package that provide filename completion in shell-command, similar to how bash does it? I know the normal M-x shell does completion, but the convenience of entering a command in minibuffer is hard to give up ;)

Edit:

here is what I wanted, taken from huaiyuan's answer with some fixes inspired by / stolen from http://osdir.com/ml/emacs.sources/2002-04/msg00022.html

(define-key minibuffer-local-map
  [f3] (lambda () (interactive) 
       (insert (buffer-name (current-buffer-not-mini)))))

(defun current-buffer-not-mini ()
  "Return current-buffer if current buffer is not the *mini-buffer*
  else return buffer before minibuf is activated."
  (if (not (window-minibuffer-p)) (current-buffer)
      (if (eq (get-lru-window) (next-window))
       (window-buffer (previous-window)) (window-buffer (next-window)))))
+4  A: 
(define-key minibuffer-local-map
  [f3] (lambda () (interactive) (insert (buffer-name))))

Edit:

As pointed out in the comment section and elsewhere, the above code snippet doesn't work. (Sorry, I should have tested it before posting. :) Some fixes have been posted; here is another:

(define-key minibuffer-local-map [f3]
  (lambda () (interactive) 
     (insert (buffer-name (window-buffer (minibuffer-selected-window))))))

Regarding filename completion while issuing shell-command, perhaps this kludge would work (it works for me on Emacs 23.0.60):

(require 'shell)
(define-key minibuffer-local-map (kbd "C-i") 'comint-dynamic-complete)
huaiyuan
That ALMOST does it, except buffer-name is *Minibuf-1*, not what I wanted. I tried (buffer-file-name (other-buffer)), but that gives the OTHER buffer, not the current-buffer before I entered minibuf. Is there other way to get the real current-buffer?
polyglot
Nevermind, there seems to be no such function coming with emacs; I'll see if I can hack one.
polyglot
+1  A: 

I'll defer to huaiyuan about exactly what you asked, but you could also check out http://stackoverflow.com/questions/206806/filtering-text-through-a-shell-command-in-emacs as a possible alternative approach.

dmckee
+4  A: 

Here's how to fix huaiyuan's solution:

(define-key minibuffer-local-map [f3]
  (lambda() (interactive) (insert (buffer-file-name (nth 1 (buffer-list))))))

You probably should add some sort of error checking in case the "other" buffer has no file name set and (buffer-file-name) returns nil.

David Hanak