[Top][All Lists]

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

TODO: insert-file should warn if the file is modified

From: Pavel Janík
Subject: TODO: insert-file should warn if the file is modified
Date: Fri, 19 Apr 2002 14:58:37 +0200
User-agent: Gnus/5.090006 (Oort Gnus v0.06) Emacs/21.2.50 (i386-suse-linux-gnu)


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"
  (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)

reply via email to

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