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

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

RE: Passing current buffer to compile command


From: Drew Adams
Subject: RE: Passing current buffer to compile command
Date: Thu, 20 Jan 2011 17:08:37 -0800

Rustom, Le, and others have provided precise solutions for this.

FWIW, here is some additional info that can be of general help, but as such is
nowhere near as convenient as a specific, dedicated command such as those
suggested.  Just a few general tools that can sometimes be handy.


1. At the heart of the solutions the others have provided is this Lisp sexp:

(buffer-file-name (window-buffer (minibuffer-selected-window)))

It returns the name of the file associated with the buffer selected before the
minibuffer was activated, as follows:

* `minibuffer-selected-window' (probably not a great name for what it does)
returns the window that was selected before activating the minibuffer.
`window-buffer' then returns that window's buffer.

* `buffer-file-name' returns the name of the file that its buffer arg is
visiting (or nil if not visiting a file).

If you happen to know these Lisp functions then you could evaluate this long
sexp yourself and insert the result in the minibuffer.  No, it's not convenient
to do this kind of thing, but it can be made a little more convenient and more
useful.


2. You can do this while in the minibuffer.  `M-:' prompts for a Lisp sexp to
evaluate, and you can use `M-:' from the minibuffer provided
`enable-recursive-minibuffers' is non-nil.

In Icicles: `M-:' in the minibuffer automatically binds
`enable-recursive-minibuffers' to non-nil.  With a prefix arg, `M-:' inserts the
result of the evaluation into the minibuffer.  With a negative prefix arg (e.g.
`M-') it removes the double-quotes from a string value.

So, for example, to insert the buffer's file name you could do the following
whenever you are in the minibuffer (regardless of the context: `M-x', `C-x C-f',
etc.):

M- M-: (buffer-file-name
        (window-buffer (minibuffer-selected-window)))

If you were in buffer `foo.c' before activating the minibuffer, then the text
`foo.c' (without quotes) would be inserted in the minibuffer at point.  This is
equivalent, BTW:

M- M-: (buffer-file-name icicle-pre-minibuffer-buffer)

This insertion does not change any other text already in the minibuffer.  You
can use `M- M-:' as many times as you like, to insert the values of multiple
Lisp sexps.


3. That's a long sexp to type just to get the current file name.  But 
you can save it and reuse it.  Assign it as a string value to a variable or a
register, then insert the value quickly whenever you want.  `C-=' inserts the
value from a variable.  `C-x r i' inserts it from a register.

You would then type just `M- M-: C-=' to insert what you need.  Not as useful as
a dedicated command, obviously, but maybe good to know.

http://www.emacswiki.org/emacs/Icicles_-_Inserting_a_Regexp_from_a_Variable_or_R
egister.


---

Off-topic but related:

* You can use `C-M-F' (i.e., `C-M-S-f') in any minibuffer to insert one or more
file names on demand, using completion.

* You can use `M-.' in the minibuffer to grab various things at or near point
and insert them into the minibuffer.  When grabbing a sexp you can use a prefix
arg to eval it and insert the result instead.

http://www.emacswiki.org/emacs/Icicles_-_Inserting_Text_from_Cursor




reply via email to

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