unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* TODO: insert-file should warn if the file is modified
@ 2002-04-19 12:58 Pavel Janík
  2002-04-19 13:47 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Pavel Janík @ 2002-04-19 12:58 UTC (permalink / raw)


Hi,

we have this entry in TODO:

   * If you do an insert-file and that file is currently modified in
     another buffer but not written yet, print a warning.

I think that it is pretty simple:

(defun file-is-modified-somewhere (filename)
  "Check if the file FILENAME is modified inside Emacs.

Return buffer visiting the file FILENAME marked as modified.
Otherwise, return nil."
  (let ((result))
    (dolist (buffer (buffer-list) result)
      (if (and (string= filename (buffer-file-name buffer))
	       (buffer-modified-p buffer))
	  (setq result buffer)))))

(defun insert-file (filename)
  "Insert contents of file FILENAME into buffer after point.
Set mark after the inserted text.

If the file is modified in some buffer, warn user.

This function is meant for the user to run interactively.
Don't call it from programs!  Use `insert-file-contents' instead.
\(Its calling sequence is different; see its documentation)."
  (interactive "*fInsert file: ")
  (if (file-directory-p filename)
      (signal 'file-error (list "Opening input file" "file is a directory"
				filename)))
  (let ((buffer (file-is-modified-somewhere filename)))
    (if (or (not buffer)
	    (y-or-n-p (format "File %s is modified in buffer %s. Insert it? " filename buffer)))
	(let ((tem (insert-file-contents filename)))
	  (push-mark (+ (point) (car (cdr tem))))))))


What do you think?
-- 
Pavel Janík

Use recursive procedures for recursively-defined data structures.
                  --  The Elements of Programming Style (Kernighan & Plaugher)

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-19 12:58 TODO: insert-file should warn if the file is modified Pavel Janík
@ 2002-04-19 13:47 ` Eli Zaretskii
  2002-04-19 14:33   ` Andreas Schwab
  2002-04-19 15:07   ` Pavel Janík
  2002-04-19 14:16 ` Kim F. Storm
  2002-04-20 17:26 ` Richard Stallman
  2 siblings, 2 replies; 11+ messages in thread
From: Eli Zaretskii @ 2002-04-19 13:47 UTC (permalink / raw)
  Cc: emacs-devel

> From: Pavel@Janik.cz (Pavel =?iso-8859-2?q?Jan=EDk?=)
> Date: Fri, 19 Apr 2002 14:58:37 +0200
> 
>    * If you do an insert-file and that file is currently modified in
>      another buffer but not written yet, print a warning.
> 
> I think that it is pretty simple:
> 
> (defun file-is-modified-somewhere (filename)
>   "Check if the file FILENAME is modified inside Emacs.
> 
> Return buffer visiting the file FILENAME marked as modified.
> Otherwise, return nil."
>   (let ((result))
>     (dolist (buffer (buffer-list) result)
>       (if (and (string= filename (buffer-file-name buffer))
> 	       (buffer-modified-p buffer))
> 	         (setq result buffer)))))

What about buffer that visit the same file under a different name
(e.g., symlinks on Posix systems)?  Don't we want to catch those as
well?

In other words, perhaps you should base the search on file's
attributes, not just the name?

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-19 12:58 TODO: insert-file should warn if the file is modified Pavel Janík
  2002-04-19 13:47 ` Eli Zaretskii
@ 2002-04-19 14:16 ` Kim F. Storm
  2002-04-19 14:53   ` Pavel Janík
  2002-04-20 17:26 ` Richard Stallman
  2 siblings, 1 reply; 11+ messages in thread
From: Kim F. Storm @ 2002-04-19 14:16 UTC (permalink / raw)


Pavel@Janik.cz (Pavel Janík) writes:

Maybe it should offer to insert either the contents of the original
file or the contents of the modified buffer?

> Hi,
> 
> we have this entry in TODO:
> 
>    * If you do an insert-file and that file is currently modified in
>      another buffer but not written yet, print a warning.

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-19 13:47 ` Eli Zaretskii
@ 2002-04-19 14:33   ` Andreas Schwab
  2002-04-19 15:08     ` Pavel Janík
  2002-04-19 15:07   ` Pavel Janík
  1 sibling, 1 reply; 11+ messages in thread
From: Andreas Schwab @ 2002-04-19 14:33 UTC (permalink / raw)
  Cc: Pavel, emacs-devel

Eli Zaretskii <eliz@fencepost.gnu.org> writes:

|> > From: Pavel@Janik.cz (Pavel =?iso-8859-2?q?Jan=EDk?=)
|> > Date: Fri, 19 Apr 2002 14:58:37 +0200
|> > 
|> >    * If you do an insert-file and that file is currently modified in
|> >      another buffer but not written yet, print a warning.
|> > 
|> > I think that it is pretty simple:
|> > 
|> > (defun file-is-modified-somewhere (filename)
|> >   "Check if the file FILENAME is modified inside Emacs.
|> > 
|> > Return buffer visiting the file FILENAME marked as modified.
|> > Otherwise, return nil."
|> >   (let ((result))
|> >     (dolist (buffer (buffer-list) result)
|> >       (if (and (string= filename (buffer-file-name buffer))
|> > 	       (buffer-modified-p buffer))
|> > 	         (setq result buffer)))))
|> 
|> What about buffer that visit the same file under a different name
|> (e.g., symlinks on Posix systems)?  Don't we want to catch those as
|> well?
|> 
|> In other words, perhaps you should base the search on file's
|> attributes, not just the name?

You can use the buffer-file-truename for that.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE GmbH, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-19 14:16 ` Kim F. Storm
@ 2002-04-19 14:53   ` Pavel Janík
  0 siblings, 0 replies; 11+ messages in thread
From: Pavel Janík @ 2002-04-19 14:53 UTC (permalink / raw)
  Cc: emacs-devel

   From: storm@cua.dk (Kim F. Storm)
   Date: 19 Apr 2002 16:16:40 +0200

   > Maybe it should offer to insert either the contents of the original
   > file or the contents of the modified buffer?

Should it? There can be more buffers visiting that file and it could be
really hard to choose one. And in fact, the name of the function if
insert-file, not insert-buffer ;-)
-- 
Pavel Janík

Doctor, it hurts, where is my problem?
                  -- Hubert Mantel with his crystal-ball

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-19 13:47 ` Eli Zaretskii
  2002-04-19 14:33   ` Andreas Schwab
@ 2002-04-19 15:07   ` Pavel Janík
  1 sibling, 0 replies; 11+ messages in thread
From: Pavel Janík @ 2002-04-19 15:07 UTC (permalink / raw)
  Cc: emacs-devel

   From: Eli Zaretskii <eliz@fencepost.gnu.org>
   Date: Fri, 19 Apr 2002 09:47:06 -0400

Hi ELi,

   > What about buffer that visit the same file under a different name
   > (e.g., symlinks on Posix systems)?  Don't we want to catch those as
   > well?

Yes, we certainly want to catch those as well. Thank you for pointing this
out. What about the following version? I'm checking if truename of the
argument is the same as truename of any file visited:

(defun file-is-modified-somewhere (filename)
  "Check if the file FILENAME is modified inside Emacs.

Return buffer visiting the file FILENAME marked as modified.
Otherwise, return nil."
  (let ((result)
	(true-filename (file-truename filename)))
    (dolist (buffer (buffer-list) result)
      (if (and (string= true-filename
			(file-truename (or (buffer-file-name buffer) "")))
	       (buffer-modified-p buffer))
	  (setq result buffer)))))

-- 
Pavel Janík

I remember when I lost an argument.  Boy did that hurt!  ;-)
                  -- Richard Stallman in emacs-devel

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-19 14:33   ` Andreas Schwab
@ 2002-04-19 15:08     ` Pavel Janík
  0 siblings, 0 replies; 11+ messages in thread
From: Pavel Janík @ 2002-04-19 15:08 UTC (permalink / raw)
  Cc: Eli Zaretskii, emacs-devel

   From: Andreas Schwab <schwab@suse.de>
   Date: Fri, 19 Apr 2002 16:33:47 +0200

   > You can use the buffer-file-truename for that.

This is buffer-local variable. I think it is better to use file-truename
instead.
-- 
Pavel Janík

So calm down guys. And improving the benchmark might not be a bad idea.
                  -- Linus Torvalds in linux-kernel

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-19 12:58 TODO: insert-file should warn if the file is modified Pavel Janík
  2002-04-19 13:47 ` Eli Zaretskii
  2002-04-19 14:16 ` Kim F. Storm
@ 2002-04-20 17:26 ` Richard Stallman
  2002-04-20 18:12   ` Pavel Janík
  2 siblings, 1 reply; 11+ messages in thread
From: Richard Stallman @ 2002-04-20 17:26 UTC (permalink / raw)
  Cc: emacs-devel

      (let ((buffer (file-is-modified-somewhere filename)))
	(if (or (not buffer)
		(y-or-n-p (format "File %s is modified in buffer %s. Insert it? " filename buffer)))

The code looks good, but the message might be misunderstood.
"It" is ambiguous1  Be VERY careful about using pronouns
in writing, and especially in short statements like that one.

  File %s is being edited.  Insert the file anyway?

would be a clearer message.

It is better not to include the buffer name, because that will usually
give no additional information and will make the message longer.  One
possible approach is to include the buffer name only when it is
different from the file's name.

KFS suggested offering to insert the buffer.  That is a good idea
provided the questions asked seem natural and not inconvenient.

One possible approach is to ask whether to insert the file or the
buffer; a user who wants to insert neither one can type C-g instead of
answering the question.

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-20 17:26 ` Richard Stallman
@ 2002-04-20 18:12   ` Pavel Janík
  2002-04-21 13:29     ` Kim F. Storm
  2002-04-22  7:46     ` Richard Stallman
  0 siblings, 2 replies; 11+ messages in thread
From: Pavel Janík @ 2002-04-20 18:12 UTC (permalink / raw)
  Cc: emacs-devel

   From: Richard Stallman <rms@gnu.org>
   Date: Sat, 20 Apr 2002 11:26:30 -0600 (MDT)

   >       (let ((buffer (file-is-modified-somewhere filename)))
   > 	(if (or (not buffer)
   > 		(y-or-n-p (format "File %s is modified in buffer %s. Insert it? " filename buffer)))
   > 
   > The code looks good, but the message might be misunderstood.
   > "It" is ambiguous1  Be VERY careful about using pronouns
   > in writing, and especially in short statements like that one.
   > 
   >   File %s is being edited.  Insert the file anyway?
   > 
   > would be a clearer message.

Yes.

   > It is better not to include the buffer name, because that will usually
   > give no additional information and will make the message longer.  One
   > possible approach is to include the buffer name only when it is
   > different from the file's name.

I think that this is not enough. Try this:

pavel@SnowWhite:/tmp/em> LANG=C ls -lR
.:
total 8
drwxr-xr-x    2 pavel    users        4096 Apr 20 20:07 1
drwxr-xr-x    2 pavel    users        4096 Apr 20 20:07 2

./1:
total 4
-rw-r--r--    1 pavel    users           3 Apr 20 20:07 file

./2:
total 0
lrwxrwxrwx    1 pavel    users           9 Apr 20 20:07 file -> ../1/file

User will edit 2/file in Emacs in buffer `file'. He can not insert file
1/file, because 2/file is edited in the buffer with the same name as the
filename and he will not be warned of the buffer. On the other hand: the
file can be edited in more than one buffer.

   > KFS suggested offering to insert the buffer.  That is a good idea
   > provided the questions asked seem natural and not inconvenient.

Which buffer? The first one we find is editing that file? There can be
many of them...
-- 
Pavel Janík

Buying an operating system without source is like buying a self-assembly
Space Shuttle with no instructions.
                  -- Jeremy Allison in linux-kernel

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-20 18:12   ` Pavel Janík
@ 2002-04-21 13:29     ` Kim F. Storm
  2002-04-22  7:46     ` Richard Stallman
  1 sibling, 0 replies; 11+ messages in thread
From: Kim F. Storm @ 2002-04-21 13:29 UTC (permalink / raw)
  Cc: rms, emacs-devel

Pavel@Janik.cz (Pavel Janík) writes:

>    From: Richard Stallman <rms@gnu.org>
>    Date: Sat, 20 Apr 2002 11:26:30 -0600 (MDT)
> 
>    >       (let ((buffer (file-is-modified-somewhere filename)))
>    > 	(if (or (not buffer)
>    > 		(y-or-n-p (format "File %s is modified in buffer %s. Insert it? " filename buffer)))
>    > 
>    > The code looks good, but the message might be misunderstood.
>    > "It" is ambiguous1  Be VERY careful about using pronouns
>    > in writing, and especially in short statements like that one.
>    > 
>    >   File %s is being edited.  Insert the file anyway?
>    > 
>    > would be a clearer message.
> 
> Yes.
> 
>    > It is better not to include the buffer name, because that will usually
>    > give no additional information and will make the message longer.  One
>    > possible approach is to include the buffer name only when it is
>    > different from the file's name.
> 
> I think that this is not enough. Try this:
> 
> pavel@SnowWhite:/tmp/em> LANG=C ls -lR
> .:
> total 8
> drwxr-xr-x    2 pavel    users        4096 Apr 20 20:07 1
> drwxr-xr-x    2 pavel    users        4096 Apr 20 20:07 2
> 
> ./1:
> total 4
> -rw-r--r--    1 pavel    users           3 Apr 20 20:07 file
> 
> ./2:
> total 0
> lrwxrwxrwx    1 pavel    users           9 Apr 20 20:07 file -> ../1/file
> 
> User will edit 2/file in Emacs in buffer `file'. He can not insert file
> 1/file, because 2/file is edited in the buffer with the same name as the
> filename and he will not be warned of the buffer. On the other hand: the
> file can be edited in more than one buffer.
> 
>    > KFS suggested offering to insert the buffer.  That is a good idea
>    > provided the questions asked seem natural and not inconvenient.
> 
> Which buffer? The first one we find is editing that file? There can be
> many of them...

I think that in practice, there is rarely more than one buffer.

The response could be more advanced, like

        File %s is being edited.  Insert file anyway? [ynvb?]

where answering y inserts the file, n doesn't, v visits the buffer
containing the edited file, b inserts the edited buffer, and ?
explains these options.

-- 
Kim F. Storm <storm@cua.dk> http://www.cua.dk

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

* Re: TODO: insert-file should warn if the file is modified
  2002-04-20 18:12   ` Pavel Janík
  2002-04-21 13:29     ` Kim F. Storm
@ 2002-04-22  7:46     ` Richard Stallman
  1 sibling, 0 replies; 11+ messages in thread
From: Richard Stallman @ 2002-04-22  7:46 UTC (permalink / raw)
  Cc: emacs-devel

It is very rare to have two buffers editing the same file, and you
usually have to work hard to make that happen.  People who do such a
peculiar thing had better know how to cope with it.  Therefore, what
insert-file does in that case is not very important.

We should not base any major decisions on what happens in that obscure
case.  Rather, we should do the right thing for the ordinary
reasonable case, then cope with the two-buffers-same-file case as
necessary.

       > KFS suggested offering to insert the buffer.  That is a good idea
       > provided the questions asked seem natural and not inconvenient.

    Which buffer? The first one we find is editing that file? There can be
    many of them...

Pick one of them.  It doesn't matter how.

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

end of thread, other threads:[~2002-04-22  7:46 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-04-19 12:58 TODO: insert-file should warn if the file is modified Pavel Janík
2002-04-19 13:47 ` Eli Zaretskii
2002-04-19 14:33   ` Andreas Schwab
2002-04-19 15:08     ` Pavel Janík
2002-04-19 15:07   ` Pavel Janík
2002-04-19 14:16 ` Kim F. Storm
2002-04-19 14:53   ` Pavel Janík
2002-04-20 17:26 ` Richard Stallman
2002-04-20 18:12   ` Pavel Janík
2002-04-21 13:29     ` Kim F. Storm
2002-04-22  7:46     ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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