Discussion:
reload modified files in buffer
Rajinder Yadav
2010-12-14 06:55:47 UTC
Permalink
How can I get emacs to automatically reload files in its buffer that
have been modified externally?

When editing a rails app, if I run a rails generate command, I don't
want to have to reload a file manually. Sometimes I start to edit a file
that is out of sync in the buffer and then get into a mess. I would like
this to happen transparently.
--
Kind Regards,
Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-23-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.3
suvayu ali
2010-12-14 07:57:19 UTC
Permalink
How can I get emacs to automatically reload files in its buffer that have
been modified externally?
When editing a rails app, if I run a rails generate command, I don't want to
have to reload a file manually. Sometimes I start to edit a file that is out
of sync in the buffer and then get into a mess. I would like this to happen
transparently.
`M-x auto-revert-mode'
--
Suvayu

Open source is the future. It sets us free.
Deniz Dogan
2010-12-14 09:28:03 UTC
Permalink
How can I get emacs to automatically reload files in its buffer that have
been modified externally?
When editing a rails app, if I run a rails generate command, I don't want to
have to reload a file manually. Sometimes I start to edit a file that is out
of sync in the buffer and then get into a mess. I would like this to happen
transparently.
If the buffer file has been modified, Emacs should warn you about
modifying it before it lets you. What version of Emacs are you using?
--
Deniz Dogan
Andrea Crotti
2010-12-14 15:19:13 UTC
Permalink
Post by Rajinder Yadav
How can I get emacs to automatically reload files in its buffer that
have been modified externally?
When editing a rails app, if I run a rails generate command, I don't
want to have to reload a file manually. Sometimes I start to edit a
file that is out of sync in the buffer and then get into a mess. I
would like this to happen transparently.
I use this
--8<---------------cut here---------------start------------->8---
(defun is-git-file ()
"Return nil unless the file is in the git files"
(if
(member (file-name-nondirectory buffer-file-name)
(split-string (shell-command-to-string "git ls-files")))
(auto-revert-mode t)))

(add-hook 'find-file-hook 'is-git-file)
--8<---------------cut here---------------end--------------->8---

to set files in auto-revert mode if they're part of a git repository.
Maybe you can adapt something like this to your needs.
Having everything in auto-revert mode might slow down too much...
suvayu ali
2010-12-15 15:43:59 UTC
Permalink
Hi Andrea,

On Tue, Dec 14, 2010 at 7:19 AM, Andrea Crotti
Post by Andrea Crotti
I use this
--8<---------------cut here---------------start------------->8---
 (defun is-git-file ()
   "Return nil unless the file is in the git files"
   (if
       (member (file-name-nondirectory buffer-file-name)
               (split-string  (shell-command-to-string "git ls-files")))
       (auto-revert-mode t)))
 (add-hook 'find-file-hook 'is-git-file)
--8<---------------cut here---------------end--------------->8---
to set files in auto-revert mode if they're part of a git repository.
Maybe you can adapt something like this to your needs.
Having everything in auto-revert mode might slow down too much...
Any particular reason to write your own function? I simply use this,

(lambda ()
(if (vc-working-revision (buffer-file-name))
(auto-revert-mode t)))

--
Suvayu

Open source is the future. It sets us free.
Andrea Crotti
2010-12-15 16:58:07 UTC
Permalink
Post by suvayu ali
Any particular reason to write your own function? I simply use this,
(lambda ()
(if (vc-working-revision (buffer-file-name))
(auto-revert-mode t)))
--
Suvayu
Open source is the future. It sets us free.
One very good reason, I didn't know about it :D
I also looked for something like that but then I gave up, thanks it's
much more general like this...
Sven Joachim
2010-12-14 07:12:35 UTC
Permalink
Post by Rajinder Yadav
How can I get emacs to automatically reload files in its buffer that
have been modified externally?
Use global-auto-revert-mode.
Post by Rajinder Yadav
When editing a rails app, if I run a rails generate command, I don't
want to have to reload a file manually. Sometimes I start to edit a
file that is out of sync in the buffer and then get into a mess.
Doesn't Emacs warn you when you do this?

Sven
Peter Dyballa
2010-12-15 15:42:56 UTC
Permalink
Post by Sven Joachim
Post by Rajinder Yadav
When editing a rails app, if I run a rails generate command, I don't
want to have to reload a file manually. Sometimes I start to edit a
file that is out of sync in the buffer and then get into a mess.
Doesn't Emacs warn you when you do this?
It does! But wouldn't it be better GNU Emacs would reload the file
instead of warning/asking things?

--
Mit friedvollen Grüßen

Pete

Es macht schon Spaß, Leute zu hassen: Nie gehen die Ideen aus!
Andrea Crotti
2010-12-15 19:14:41 UTC
Permalink
Post by Peter Dyballa
It does! But wouldn't it be better GNU Emacs would reload the file
instead of warning/asking things?
--
Mit friedvollen Grüßen
Pete
Es macht schon Spaß, Leute zu hassen: Nie gehen die Ideen aus!
not correct, what if you modified that file?
You would lose your modification without even knowing, not a very good idea...
Peter Dyballa
2010-12-15 19:26:02 UTC
Permalink
Post by Andrea Crotti
not correct, what if you modified that file?
Then it would ask whether to use diff-mode to see the differences I
made to the previous version and that to the new version.

--
Greetings

Pete

Only useless documentation transcends the first two laws.
– Arnold's Third Law of Documentation
suvayu ali
2010-12-16 00:39:11 UTC
Permalink
On Wed, Dec 15, 2010 at 11:14 AM, Andrea Crotti
Post by Andrea Crotti
Post by Peter Dyballa
It does! But wouldn't it be better GNU Emacs would reload the file
instead of warning/asking things?
not correct, what if you modified that file?
You would lose your modification without even knowing, not a very good idea...
I think auto-revert-mode doesn't revert the buffer either if it has
been modified. I think that is a good thing, but then tastes may vary.
:)
--
Suvayu

Open source is the future. It sets us free.
Rajinder Yadav
2010-12-16 11:42:37 UTC
Permalink
Post by suvayu ali
On Wed, Dec 15, 2010 at 11:14 AM, Andrea Crotti
Post by Andrea Crotti
Post by Peter Dyballa
It does! But wouldn't it be better GNU Emacs would reload the file
instead of warning/asking things?
not correct, what if you modified that file?
You would lose your modification without even knowing, not a very good idea...
I think auto-revert-mode doesn't revert the buffer either if it has
been modified. I think that is a good thing, but then tastes may vary.
:)
it's good to know that auto-revert mode will not revert a modify buffer.
however afaik Emacs will not let you save a modified buffer if the file
has changed.

i like Peter's idea of Emacs having the ability to diff-merge a conflict
when you've modified a buffer and the file has changed.

Emacs should be smart enough to auto-reload a buffer if the file has
changed and the buffer has not been edited, this would be a welcomed
enhancement =)
--
Kind Regards,
Rajinder Yadav | DevMentor.org | Do Good! ~ Share Freely

GNU/Linux: 2.6.35-23-generic
Kubuntu x86_64 10.10 | KDE 4.5.1
Ruby 1.9.2p0 | Rails 3.0.3
PJ Weisberg
2010-12-16 16:32:01 UTC
Permalink
Emacs should be smart enough to auto-reload a buffer if the file has changed
and the buffer has not been edited, this would be a welcomed enhancement =)
This would be annoying (to me) if it weren't off by default. Of
course, I could turn it off by default in my .emacs. But my point is,
you can enable this (and have it saved in your .emacs) by doing:

M-x customize-save-variable
global-auto-revert-mode
y

-PJ
suvayu ali
2010-12-16 18:08:47 UTC
Permalink
Emacs should be smart enough to auto-reload a buffer if the file has changed
and the buffer has not been edited, this would be a welcomed enhancement =)
It is, that is what auto-revert-mode does. But it is for the user to
turn on. I am a firm believer of each user customising there setup as
per their needs. Features should not be forced on anyone. If you have
a preference then customise. Emacs even has the wonderful "customize"
interface to help you with everything.
--
Suvayu

Open source is the future. It sets us free.
Andrea Crotti
2010-12-17 23:09:38 UTC
Permalink
Post by Rajinder Yadav
it's good to know that auto-revert mode will not revert a modify
buffer. however afaik Emacs will not let you save a modified buffer if
the file has changed.
i like Peter's idea of Emacs having the ability to diff-merge a
conflict when you've modified a buffer and the file has changed.
Emacs should be smart enough to auto-reload a buffer if the file has
changed and the buffer has not been edited, this would be a welcomed
enhancement =)
Try to enable globally auto-revert-mode and keep 200 buffers open on a
slow machine, you'll understand why is not a default ;)

I think is a good idea to be off by default, after all why do you need
to revert buffers so often?

For me it only happens when I push/pull with some revision control
systems...

Sven Joachim
2010-12-15 16:04:08 UTC
Permalink
Post by Peter Dyballa
Post by Sven Joachim
Post by Rajinder Yadav
When editing a rails app, if I run a rails generate command, I don't
want to have to reload a file manually. Sometimes I start to edit a
file that is out of sync in the buffer and then get into a mess.
Doesn't Emacs warn you when you do this?
It does! But wouldn't it be better GNU Emacs would reload the file
instead of warning/asking things?
I would not appreciate if Emacs did that without asking first. And
reverting the buffer (= reloading the file) is among the offered options
in such a situation.

Cheers,
Sven
Continue reading on narkive:
Loading...