unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Post-Advice for file-move (rename-file()) but not upon save-buffer
@ 2007-05-06  8:57 Nordlöw
  2007-05-06 11:00 ` Tim X
  2007-05-16  3:40 ` Kevin Rodgers
  0 siblings, 2 replies; 3+ messages in thread
From: Nordlöw @ 2007-05-06  8:57 UTC (permalink / raw)
  To: help-gnu-emacs

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?


Thanks in advance,
Nordlöw

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

* Re: Post-Advice for file-move (rename-file()) but not upon save-buffer
  2007-05-06  8:57 Post-Advice for file-move (rename-file()) but not upon save-buffer Nordlöw
@ 2007-05-06 11:00 ` Tim X
  2007-05-16  3:40 ` Kevin Rodgers
  1 sibling, 0 replies; 3+ messages in thread
From: Tim X @ 2007-05-06 11:00 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw <per.nordlow@gmail.com> 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
etc.

Tim



-- 
tcross (at) rapttech dot com dot au

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

* Re: Post-Advice for file-move (rename-file()) but not upon save-buffer
  2007-05-06  8:57 Post-Advice for file-move (rename-file()) but not upon save-buffer Nordlöw
  2007-05-06 11:00 ` Tim X
@ 2007-05-16  3:40 ` Kevin Rodgers
  1 sibling, 0 replies; 3+ messages in thread
From: Kevin Rodgers @ 2007-05-16  3:40 UTC (permalink / raw)
  To: help-gnu-emacs

Nordlöw wrote:
> 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?

,----[ C-h f interactive-p RET ]
| interactive-p is a built-in function in `C source code'.
| (interactive-p)
|
| Return t if the function was run directly by user input.
| This means that the function was called with `call-interactively'
| (which includes being called as the binding of a key)
| and input is currently coming from the keyboard (not in keyboard macro),
| and Emacs is not running in batch mode (`noninteractive' is nil).
|
| The only known proper use of `interactive-p' is in deciding whether to
| display a helpful message, or how to display it.  If you're thinking
| of using it for any other purpose, it is quite likely that you're
| making a mistake.  Think: what do you want to do when the command is
| called from a keyboard macro?
|
| If you want to test whether your function was called with
| `call-interactively', the way to do that is by adding an extra
| optional argument, and making the `interactive' spec specify non-nil
| unconditionally for that argument.  (`p' is a good way to do this.)
|
| [back]
`----

-- 
Kevin Rodgers
Denver, Colorado, USA

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

end of thread, other threads:[~2007-05-16  3:40 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-05-06  8:57 Post-Advice for file-move (rename-file()) but not upon save-buffer Nordlöw
2007-05-06 11:00 ` Tim X
2007-05-16  3:40 ` Kevin Rodgers

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