[Top][All Lists]

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

Re: Post-Advice for file-move (rename-file()) but not upon save-buffer

From: Tim X
Subject: Re: Post-Advice for file-move (rename-file()) but not upon save-buffer
Date: Sun, 06 May 2007 21:00:08 +1000
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.99 (gnu/linux)

Nordlöw <address@hidden> writes:

> Hey there again, Emacs Hackers!
> I want to make Emacs run a command every time a file is moved/renamed
> (from Emacs)
> Here is the essential part of my code:
> (defadvice rename-file (after update-c-includes activate)
>   "Update C include statements upon rename of file."
>   (let ((file (file-name-nondirectory (ad-get-arg 0)))
>       (newname (file-name-nondirectory (ad-get-arg 1))))
>     (if (or (file-is-C-header file)
>           (file-is-C-header newname))
>       (if (y-or-n-p "Update C,C++ #includes accordingly ")
>           ;; TODO: We need to strip parts of the path from file and newname
>           (c-rename-includes file newname))
>       )))
> This works as intended for rename-file but it also gets called when I
> save a buffer to file, using save-buffer(), which is *not* what I
> want. I guess it's because save-file() is implemented using rename-
> file().
> How can I make it run *solely* when I rename a file?

You cannot make it run solely when you rename a file because it is used by
other functions, such as save-buffer etc. So, what you need to do is make it
only do what you want using some other criteria. Maybe regquire both files to
be ending in .h (because I suspect the rename-file called when saving a buffer
is probably related to temporary files and/or backup files etc and they *may*
not both end in .h, even when saving a .h file for the first time. 

In the end, it is likely you won't be able to use defadvice here. I love
defadvice and use it all the time, but in your case, you may be better off
writing a specific function that does what you want and binding that to a key


tcross (at) rapttech dot com dot au

reply via email to

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