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

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

Re: resolve rename-buffer conflicts


From: Hongxu Chen
Subject: Re: resolve rename-buffer conflicts
Date: Mon, 17 Jun 2013 08:07:28 +0800
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

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



reply via email to

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