Brian Z
Brian Z

Reputation: 953

How do I prevent emacs from overwriting files without warning?

On two different occasions, I have overwritten important files with emacs, without even realizing it until later. This happens because various commands (specifically org-agenda-write and org-export) will simply replace an existing file without a warning that a file with that name already exists. Is there way to configure emacs so that this won't happen?

Upvotes: 1

Views: 291

Answers (1)

Tyler
Tyler

Reputation: 10032

org-agenda-write uses write-file to save your agenda. This function, when called from a program, will over-write existing files without confirmation. write-file is buried pretty deep inside org-agenda-write, and modifying it directly is likely to cause surprising bugs elsewhere in Emacs. However, you can wrap org-agenda-write in an around advice. This is a neat way to add a check for the existence of the file, and alert the user before over-writing it.

(defadvice org-agenda-write (around my-file-check)
  "Check if a file exists before writing the agenda to it."
  (if (file-exists-p file)
      (if (y-or-n-p (format "Overwrite %s?" file))
          ad-do-it)
    ad-do-it))

(ad-activate 'org-agenda-write)

This might qualify as a bug in org-agenda-write, and if you ask nicely on the orgmode mailing list they might be convinced to make this check the default behaviour.

The file writing behaviour of org-export looks a little more involved, and may draw on external programs to complete. Still, you could probably use a similar wrapper on that if you like.

Upvotes: 1

Related Questions