unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* resolve rename-buffer conflicts
@ 2013-06-14  1:54 Hongxu Chen
  2013-06-16 18:29 ` Michael Heerdegen
  0 siblings, 1 reply; 3+ messages in thread
From: Hongxu Chen @ 2013-06-14  1:54 UTC (permalink / raw)
  To: help-gnu-emacs


Hi,

  I have followed some tips to rename file(and the corresponding buffer
  name) like this below:

,----------[ rename-this-file ]
|   (defun rename-this-file (new-name)
|   "Renames both current buffer and file it's visiting to NEW-NAME."
|   (interactive "sNew name: ")
|   (let ((name (buffer-name))
|         (filename (buffer-file-name)))
|     (unless filename
|       (error "Buffer '%s' is not visiting a file!" name))
|     (if (get-buffer new-name)
|         (message "A buffer named '%s' already exists!" new-name)
|       (progn
|         (rename-file filename new-name t)
|         (rename-buffer new-name t)
|         (set-visited-file-name new-name)
|         (set-buffer-modified-p nil)))))
`----------

Since there might be a buffer name conflict I am using `(rename-buffer
new-name t)' inside this function instead(according to the docstrings,
if the last argument is non-nil it is supposed to generate a new buffer
name when there is a conflict). However it DOESN'T work at all. It
reports like below:

    A buffer named 'test.cpp' already exists!

But it works fine when invoking `rename-buffer' interactively like `C-u
M-x rename-buffer'.

So Why it fails for the snippet above?
(I also use uniquify.el but I don't believe it is a problem since it
defines an advice `rename-buffer-uniquify')

-- 
Regards,
Hongxu Chen



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

* Re: resolve rename-buffer conflicts
  2013-06-14  1:54 resolve rename-buffer conflicts Hongxu Chen
@ 2013-06-16 18:29 ` Michael Heerdegen
  2013-06-17  0:07   ` Hongxu Chen
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Heerdegen @ 2013-06-16 18:29 UTC (permalink / raw)
  To: Hongxu Chen; +Cc: help-gnu-emacs

Hi Hongxu,

> ,----------[ rename-this-file ]
> |   (defun rename-this-file (new-name)
> |   "Renames both current buffer and file it's visiting to NEW-NAME."
> |   (interactive "sNew name: ")
> |   (let ((name (buffer-name))
> |         (filename (buffer-file-name)))
> |     (unless filename
> |       (error "Buffer '%s' is not visiting a file!" name))
> |     (if (get-buffer new-name)
> |         (message "A buffer named '%s' already exists!" new-name)
> |       (progn
> |         (rename-file filename new-name t)
> |         (rename-buffer new-name t)
> |         (set-visited-file-name new-name)
> |         (set-buffer-modified-p nil)))))
> `----------

>     A buffer named 'test.cpp' already exists!
>
> So Why it fails for the snippet above?

I tested your code in a recent build of trunk, and it works fine here.

Can you reproduce your problem with emacs -Q?  Which Emacs version are
you using?  Maybe try to follow what's happening with the debugger.  You
probably want to (debug-on-entry 'rename-this-file).  Load file sources
to avoid debugging compiled code.


Regards,

Michael.



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

* Re: resolve rename-buffer conflicts
  2013-06-16 18:29 ` Michael Heerdegen
@ 2013-06-17  0:07   ` Hongxu Chen
  0 siblings, 0 replies; 3+ messages in thread
From: Hongxu Chen @ 2013-06-17  0:07 UTC (permalink / raw)
  To: help-gnu-emacs

Michael Heerdegen <michael_heerdegen@web.de> writes:

> Hi Hongxu,
>
>> ,----------[ rename-this-file ]
>> |   (defun rename-this-file (new-name)
>> |   "Renames both current buffer and file it's visiting to NEW-NAME."
>> |   (interactive "sNew name: ")
>> |   (let ((name (buffer-name))
>> |         (filename (buffer-file-name)))
>> |     (unless filename
>> |       (error "Buffer '%s' is not visiting a file!" name))
>> |     (if (get-buffer new-name)
>> |         (message "A buffer named '%s' already exists!" new-name)
>> |       (progn
>> |         (rename-file filename new-name t)
>> |         (rename-buffer new-name t)
>> |         (set-visited-file-name new-name)
>> |         (set-buffer-modified-p nil)))))
>> `----------
>
>>     A buffer named 'test.cpp' already exists!
>>
>> So Why it fails for the snippet above?
>
> I tested your code in a recent build of trunk, and it works fine here.
>
> Can you reproduce your problem with emacs -Q?  Which Emacs version are
> you using?  Maybe try to follow what's happening with the debugger.  You
> probably want to (debug-on-entry 'rename-this-file).  Load file sources
> to avoid debugging compiled code.

Thanks for your advice, Michael. I now figure out that I made a silly
mistake here, the `if' condition here should be stripped as well. I
didn't even notice that! The fixed version is simpler:

,----------[ rename-this-file ]
| (defun rename-this-file (new-name)
|   "Renames both current buffer and file it's visiting to NEW-NAME."
|   (interactive "sNew name: ")
|   (let ((name (buffer-name))
|         (filename (buffer-file-name)))
|     (unless filename
|       (error "Buffer '%s' is not visiting a file!" name))
|         (rename-file filename new-name t)
|         (rename-buffer new-name t)
|         (set-visited-file-name new-name)
|         (set-buffer-modified-p nil)))
`----------


>
>
> Regards,
>
> Michael.

-- 
Regards,
Hongxu Chen



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

end of thread, other threads:[~2013-06-17  0:07 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-06-14  1:54 resolve rename-buffer conflicts Hongxu Chen
2013-06-16 18:29 ` Michael Heerdegen
2013-06-17  0:07   ` Hongxu Chen

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