[gnu.emacs.bug] write-file-hooks bug?

sasdjb@MCNC.ORG (David Biesack) (06/27/89)

There may be a bug with write-file-hooks...

GNU Emacs 18.52.6 of Thu Feb 16 1989 on liszt (hpux)
GNU EMacs 18.54.2 of Mon Jun 12 1989 on sdcapol2 (Domain/OS)

I believe there is a bug in emacs such that when a write-file-hooks
hook function signals an error, not only does the file not get saved
(this is correct behavior), the file is "deleted" (this is not correct
behavior!)

You can try this easily with the following:

0) In the *scratch* buffer of an emacs started with -q, evaluate:

(defun error-hook ()
  (if (y-or-n-p "Signal an error? ")
      (error "Error hook")))

(setq write-file-hooks '(error-hook))

1) C-x C-f to edit a new file, "xyzzy", and enter some arbitrary text.
2) C-x C-s to save save it, answering 'n' to te error hook prompt.
   (the hook function does not signal the first time).
3) modify the buffer
4) C-x C-s to save it again, but this time answer 'y' so that the hook
   function signals an error.
5) xyzzy no longer exists, although the backup file xyzzy~ does exist.

I would expect xyzzy to still exist and contain the contents saved in
step 2. It may be that the "bug" is really that write-file-hooks are
not well documented:

Documentation:
List of functions to be called before writing out a buffer to a file.
If one of them returns non-nil, the file is considered already written
and the rest are not called.

It's as if emacs creates the backup file by renaming BEFORE calling
the hooks, then skips the actual saving if an error was signaled.

Our local portability standards limits C source file to lines no
longer than 72 characts, so I have written a write-file-hook which
scans the file for lines longer than a buffer local variable; my
c-mode-hook sets the local variable to 72. The hook signaled an error
if the buffer contains such long lines. I would like this behavior
because I have a function which does (save-some-buffers t) before
calling compile, but I don't want the compilation to continue if a
write-file-hooks hook "fails".

I rewrote my hook function so that it no longer signals an error.
However, this can catch catch unsuspecting users unaware...

djb

David J. Biesack
rti!sas!sasdjb
SAS Institute, Inc.
SAS Circle, Box 8000
Cary, NC 27512-8000
(919) 467-8000