unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to get current filename and absolute path of the file in emacs?
@ 2007-04-16  3:37 QinGW
  2007-04-16  4:30 ` Eric Hanchrow
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: QinGW @ 2007-04-16  3:37 UTC (permalink / raw)
  To: help-gnu-emacs

I want to put current filename and file path to clipboard and I cant
get it by short key.how can I implement it?
Thank a lot.

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

* Re: How to get current filename and absolute path of the file in emacs?
  2007-04-16  3:37 How to get current filename and absolute path of the file in emacs? QinGW
@ 2007-04-16  4:30 ` Eric Hanchrow
  2007-04-16 19:36   ` Eli Zaretskii
  2007-04-16  5:07 ` Pascal Bourguignon
       [not found] ` <mailman.2113.1176698595.7795.help-gnu-emacs@gnu.org>
  2 siblings, 1 reply; 10+ messages in thread
From: Eric Hanchrow @ 2007-04-16  4:30 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> "QinGW" == QinGW  <dealover@21cn.com> writes:

    QinGW> I want to put current filename and file path to clipboard
    QinGW> and I cant get it by short key.how can I implement it?
    QinGW> Thank a lot.

This is what I use:
  
(setq x-select-enable-clipboard t)

(defun copy-buffer-file-name (use-backslashes)
  "Puts the file name of the current buffer (or the current directory,
if the buffer isn't visiting a file) onto the kill ring, so that it
can be retrieved with \\[yank], or by another program.  With argument,
uses backslashes instead of forward slashes."
  (interactive "P")
  (let ((fn (subst-char-in-string
             ?/
             (if use-backslashes ?\\ ?/)
             (or
              (buffer-file-name (current-buffer))
              ;; Perhaps the buffer isn't visiting a file at all.  In
              ;; that case, let's return the directory.
              (expand-file-name default-directory)))))
    (when (null fn)
      (error "Buffer doesn't appear to be associated with any file or directory."))
    (kill-new fn)
    (message "%s" fn)
    fn))

(global-set-key (kbd "<f8>") 'copy-buffer-file-name)
-- 

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

* Re: How to get current filename and absolute path of the file in emacs?
  2007-04-16  3:37 How to get current filename and absolute path of the file in emacs? QinGW
  2007-04-16  4:30 ` Eric Hanchrow
@ 2007-04-16  5:07 ` Pascal Bourguignon
  2007-04-16  8:57   ` Matthew Flaschen
                     ` (3 more replies)
       [not found] ` <mailman.2113.1176698595.7795.help-gnu-emacs@gnu.org>
  2 siblings, 4 replies; 10+ messages in thread
From: Pascal Bourguignon @ 2007-04-16  5:07 UTC (permalink / raw)
  To: help-gnu-emacs

QinGW <dealover@21cn.com> writes:

> I want to put current filename and file path to clipboard and I cant
> get it by short key.how can I implement it?
> Thank a lot.

M-x apropos RET file name RET

We find a buffer-file-name function... interesting!

Now, how to put that in the clipboard?  We know that when you type
C-w, the selection is put in the clipboard, so:

C-h k C-w

then click on simple.el (or type C-x o TAB RET) and read the source of
kill-region.  We see that it calls kill-new; we move the cursor on
kill-new and type C-h f RET

(global-set-key (kbd "M-z") 
   (lambda ()
     (interactive)
     (kill-new (buffer-file-name))))

-- 
__Pascal Bourguignon__

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

* Re: How to get current filename and absolute path of the file in emacs?
  2007-04-16  5:07 ` Pascal Bourguignon
@ 2007-04-16  8:57   ` Matthew Flaschen
  2007-04-16  9:32   ` qingwuking
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 10+ messages in thread
From: Matthew Flaschen @ 2007-04-16  8:57 UTC (permalink / raw)
  To: emacs

Pascal Bourguignon wrote:
> QinGW <dealover@21cn.com> writes:
> 
>> I want to put current filename and file path to clipboard and I cant
>> get it by short key.how can I implement it?
>> Thank a lot.
> 
> M-x apropos RET file name RET
> 
> We find a buffer-file-name function... interesting!
> 
I'd kind of just like to make buffer-file-name an interactive function
(not create a wrapper function with a different name).  Anyone know how
to do this?

Matt Flaschen

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

* Re: How to get current filename and absolute path of the file in emacs?
  2007-04-16  5:07 ` Pascal Bourguignon
  2007-04-16  8:57   ` Matthew Flaschen
@ 2007-04-16  9:32   ` qingwuking
  2007-04-16  9:33   ` qingwuking
       [not found]   ` <mailman.2121.1176714130.7795.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 10+ messages in thread
From: qingwuking @ 2007-04-16  9:32 UTC (permalink / raw)
  To: help-gnu-emacs

On 4月16日, 下午1时07分, Pascal Bourguignon <p...@informatimago.com> wrote:
> QinGW <dealo...@21cn.com> writes:
> > I want to put current filename and file path to clipboard and I cant
> > get it by short key.how can I implement it?
> > Thank a lot.
>
> M-x apropos RET file name RET
>
> We find a buffer-file-name function... interesting!
>
> Now, how to put that in the clipboard?  We know that when you type
> C-w, the selection is put in the clipboard, so:
>
> C-h k C-w
>
> then click on simple.el (or type C-x o TAB RET) and read the source of
> kill-region.  We see that it calls kill-new; we move the cursor on
> kill-new and type C-h f RET
>
> (global-set-key (kbd "M-z")
>    (lambda ()
>      (interactive)
>      (kill-new (buffer-file-name))))
>
> --
> __Pascal Bourguignon__

Thank you very much, this is very simple and useful.

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

* Re: How to get current filename and absolute path of the file in emacs?
  2007-04-16  5:07 ` Pascal Bourguignon
  2007-04-16  8:57   ` Matthew Flaschen
  2007-04-16  9:32   ` qingwuking
@ 2007-04-16  9:33   ` qingwuking
       [not found]   ` <mailman.2121.1176714130.7795.help-gnu-emacs@gnu.org>
  3 siblings, 0 replies; 10+ messages in thread
From: qingwuking @ 2007-04-16  9:33 UTC (permalink / raw)
  To: help-gnu-emacs

On 4月16日, 下午1时07分, Pascal Bourguignon <p...@informatimago.com> wrote:
> QinGW <dealo...@21cn.com> writes:
> > I want to put current filename and file path to clipboard and I cant
> > get it by short key.how can I implement it?
> > Thank a lot.
>
> M-x apropos RET file name RET
>
> We find a buffer-file-name function... interesting!
>
> Now, how to put that in the clipboard?  We know that when you type
> C-w, the selection is put in the clipboard, so:
>
> C-h k C-w
>
> then click on simple.el (or type C-x o TAB RET) and read the source of
> kill-region.  We see that it calls kill-new; we move the cursor on
> kill-new and type C-h f RET
>
> (global-set-key (kbd "M-z")
>    (lambda ()
>      (interactive)
>      (kill-new (buffer-file-name))))
>
> --
> __Pascal Bourguignon__

Thanks, That's great!

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

* Re: How to get current filename and absolute path of the file in emacs?
       [not found] ` <mailman.2113.1176698595.7795.help-gnu-emacs@gnu.org>
@ 2007-04-16  9:35   ` qingwuking
  0 siblings, 0 replies; 10+ messages in thread
From: qingwuking @ 2007-04-16  9:35 UTC (permalink / raw)
  To: help-gnu-emacs

On 4月16日, 下午12时30分, Eric Hanchrow <off...@blarg.net> wrote:
> >>>>> "QinGW" == QinGW  <dealo...@21cn.com> writes:
>
>     QinGW> I want to put current filename and file path to clipboard
>     QinGW> and I cant get it by short key.how can I implement it?
>     QinGW> Thank a lot.
>
> This is what I use:
>
> (setq x-select-enable-clipboard t)
>
> (defun copy-buffer-file-name (use-backslashes)
>   "Puts the file name of the current buffer (or the current directory,
> if the buffer isn't visiting a file) onto the kill ring, so that it
> can be retrieved with \\[yank], or by another program.  With argument,
> uses backslashes instead of forward slashes."
>   (interactive "P")
>   (let ((fn (subst-char-in-string
>              ?/
>              (if use-backslashes ?\\ ?/)
>              (or
>               (buffer-file-name (current-buffer))
>               ;; Perhaps the buffer isn't visiting a file at all.  In
>               ;; that case, let's return the directory.
>               (expand-file-name default-directory)))))
>     (when (null fn)
>       (error "Buffer doesn't appear to be associated with any file or directory."))
>     (kill-new fn)
>     (message "%s" fn)
>     fn))
>
> (global-set-key (kbd "<f8>") 'copy-buffer-file-name)
> --

Thanks very much.

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

* Re: How to get current filename and absolute path of the file in emacs?
       [not found]   ` <mailman.2121.1176714130.7795.help-gnu-emacs@gnu.org>
@ 2007-04-16 11:39     ` Pascal Bourguignon
  0 siblings, 0 replies; 10+ messages in thread
From: Pascal Bourguignon @ 2007-04-16 11:39 UTC (permalink / raw)
  To: help-gnu-emacs

Matthew Flaschen <matthew.flaschen@gatech.edu> writes:

> Pascal Bourguignon wrote:
>> QinGW <dealover@21cn.com> writes:
>> 
>>> I want to put current filename and file path to clipboard and I cant
>>> get it by short key.how can I implement it?
>>> Thank a lot.
>> 
>> M-x apropos RET file name RET
>> 
>> We find a buffer-file-name function... interesting!
>> 
> I'd kind of just like to make buffer-file-name an interactive function
> (not create a wrapper function with a different name).  Anyone know how
> to do this?

Well, you can call all function with: M-: (fun args...) RET
like:

M-: (buffer-file-name) RET

So it's hardly worth it.
If you want to bind it to a key:

(global-set-key (kbd "<f5>") (lambda ()
                                (interactive)
                                (buffer-file-name)))

If you really want a command:

(defun command-buffer-file-name ()
    (interactive)
    (buffer-file-name))

and:

M-x command-buffer-file-name RET

or:

(global-set-key (kbd "<f5>") 'command-buffer-file-name)

and:

<f5>


If the function takes arguments read the doc of interactive and add
them to the command.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

Pour moi, la grande question n'a jamais été: «Qui suis-je? Où vais-je?» 
comme l'a formulé si adroitement notre ami Pascal, mais plutôt: 
«Comment vais-je m'en tirer?» -- Jean Yanne

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

* Re: How to get current filename and absolute path of the file in emacs?
  2007-04-16  4:30 ` Eric Hanchrow
@ 2007-04-16 19:36   ` Eli Zaretskii
  2007-04-16 20:09     ` Eric Hanchrow
  0 siblings, 1 reply; 10+ messages in thread
From: Eli Zaretskii @ 2007-04-16 19:36 UTC (permalink / raw)
  To: help-gnu-emacs

> From: Eric Hanchrow <offby1@blarg.net>
> Date: Sun, 15 Apr 2007 21:30:19 -0700
> 
> (defun copy-buffer-file-name (use-backslashes)
>   "Puts the file name of the current buffer (or the current directory,
> if the buffer isn't visiting a file) onto the kill ring, so that it
> can be retrieved with \\[yank], or by another program.  With argument,
> uses backslashes instead of forward slashes."
>   (interactive "P")
>   (let ((fn (subst-char-in-string
>              ?/
>              (if use-backslashes ?\\ ?/)

It's easier to use convert-standard-filename to mirror slashes into
backslashes.  The nice side effect is that the code that calls
copy-buffer-file-name will now become 100% portable, because you no
longer need the use-backslashes flag to DTRT.

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

* Re: How to get current filename and absolute path of the file in emacs?
  2007-04-16 19:36   ` Eli Zaretskii
@ 2007-04-16 20:09     ` Eric Hanchrow
  0 siblings, 0 replies; 10+ messages in thread
From: Eric Hanchrow @ 2007-04-16 20:09 UTC (permalink / raw)
  To: help-gnu-emacs

>>>>> "Eli" == Eli Zaretskii <eliz@gnu.org> writes:

    ...
    >> (interactive "P") (let ((fn (subst-char-in-string ?/ (if
    >> use-backslashes ?\\ ?/)

    Eli> It's easier to use convert-standard-filename to mirror
    Eli> slashes into backslashes.  

Fabulous!  Thanks for pointing that out.

-- 

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

end of thread, other threads:[~2007-04-16 20:09 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-04-16  3:37 How to get current filename and absolute path of the file in emacs? QinGW
2007-04-16  4:30 ` Eric Hanchrow
2007-04-16 19:36   ` Eli Zaretskii
2007-04-16 20:09     ` Eric Hanchrow
2007-04-16  5:07 ` Pascal Bourguignon
2007-04-16  8:57   ` Matthew Flaschen
2007-04-16  9:32   ` qingwuking
2007-04-16  9:33   ` qingwuking
     [not found]   ` <mailman.2121.1176714130.7795.help-gnu-emacs@gnu.org>
2007-04-16 11:39     ` Pascal Bourguignon
     [not found] ` <mailman.2113.1176698595.7795.help-gnu-emacs@gnu.org>
2007-04-16  9:35   ` qingwuking

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