Subject: Re: bug#13522: 24.2; save-buffer removes edited file under some conditions

From: Glenn Morris <[email-masked]>
Date: Wed, 30 Jan 2013 03:59:25 -0500

Glenn Morris wrote:

> Maybe the right solution is to have the select-safe-coding-system check
> in basic-save-buffer-2 before backup-buffer, then pass the resulting
> coding system to write-region somehow so it does not need to query
> again.

Very lightly tested patch:

*** lisp/files.el	2013-01-10 15:50:04 +0000
--- lisp/files.el	2013-01-30 08:53:30 +0000
***************
*** 4656,4662 ****
  ;; This returns a value (MODES EXTENDED-ATTRIBUTES BACKUPNAME), like
  ;; backup-buffer.
  (defun basic-save-buffer-2 ()
!   (let (tempsetmodes setmodes)
      (if (not (file-writable-p buffer-file-name))
  	(let ((dir (file-name-directory buffer-file-name)))
  	  (if (not (file-directory-p dir))
--- 4656,4662 ----
  ;; This returns a value (MODES EXTENDED-ATTRIBUTES BACKUPNAME), like
  ;; backup-buffer.
  (defun basic-save-buffer-2 ()
!   (let (tempsetmodes setmodes writecoding)
      (if (not (file-writable-p buffer-file-name))
  	(let ((dir (file-name-directory buffer-file-name)))
  	  (if (not (file-directory-p dir))
***************
*** 4672,4677 ****
--- 4672,4680 ----
  		     buffer-file-name)))
  		  (setq tempsetmodes t)
  		(error "Attempt to save to a file which you aren't allowed to write"))))))
+     (setq writecoding
+ 	  (choose-write-coding-system nil nil buffer-file-name nil t
+ 				      buffer-file-truename))
      (or buffer-backed-up
  	(setq setmodes (backup-buffer)))
      (let* ((dir (file-name-directory buffer-file-name))
***************
*** 4753,4762 ****
  				 (logior (car setmodes) 128))))))
  	(let (success)
  	  (unwind-protect
- 	      (progn
                  ;; Pass in nil&nil rather than point-min&max to indicate
                  ;; we're saving the buffer rather than just a region.
                  ;; write-region-annotate-functions may make us of it.
  		(write-region nil nil
  			      buffer-file-name nil t buffer-file-truename)
  		(setq success t))
--- 4756,4765 ----
  				 (logior (car setmodes) 128))))))
  	(let (success)
  	  (unwind-protect
  	      ;; Pass in nil&nil rather than point-min&max to indicate
  	      ;; we're saving the buffer rather than just a region.
  	      ;; write-region-annotate-functions may make us of it.
+ 	      (let ((write-region-coding-system writecoding))
  		(write-region nil nil
  			      buffer-file-name nil t buffer-file-truename)
  		(setq success t))

=== modified file 'src/fileio.c'
*** src/fileio.c	2013-01-23 20:07:28 +0000
--- src/fileio.c	2013-01-30 08:55:45 +0000
***************
*** 249,254 ****
--- 249,255 ----
  static Lisp_Object Qset_file_acl;
  static Lisp_Object Qfile_newer_than_file_p;
  Lisp_Object Qinsert_file_contents;
+ Lisp_Object Qchoose_write_coding_system;
  Lisp_Object Qwrite_region;
  static Lisp_Object Qverify_visited_file_modtime;
  static Lisp_Object Qset_visited_file_modtime;
***************
*** 4615,4628 ****
  
  /* Decide the coding-system to encode the data with.  */
  
! static Lisp_Object
! choose_write_coding_system (Lisp_Object start, Lisp_Object end, Lisp_Object filename,
! 			    Lisp_Object append, Lisp_Object visit, Lisp_Object lockname,
! 			    struct coding_system *coding)
  {
    Lisp_Object val;
    Lisp_Object eol_parent = Qnil;
  
    if (auto_saving
        && NILP (Fstring_equal (BVAR (current_buffer, filename),
  			      BVAR (current_buffer, auto_save_file_name))))
--- 4616,4637 ----
  
  /* Decide the coding-system to encode the data with.  */
  
! DEFUN ("choose-write-coding-system", Fchoose_write_coding_system,
!        Schoose_write_coding_system, 3, 6, 0,
!        doc: /* Choose coding system for write.
! Arguments as for `write-region'.  */ )
!   (Lisp_Object start, Lisp_Object end, Lisp_Object filename,
!    Lisp_Object append, Lisp_Object visit, Lisp_Object lockname)
  {
    Lisp_Object val;
    Lisp_Object eol_parent = Qnil;
  
+   if (NILP (start))
+     {
+       XSETFASTINT (start, BEGV);
+       XSETFASTINT (end, ZV);
+     }
+ 
    if (auto_saving
        && NILP (Fstring_equal (BVAR (current_buffer, filename),
  			      BVAR (current_buffer, auto_save_file_name))))
***************
*** 4715,4724 ****
      }
  
    val = coding_inherit_eol_type (val, eol_parent);
-   setup_coding_system (val, coding);
- 
-   if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display)))
-     coding->mode |= CODING_MODE_SELECTIVE_DISPLAY;
    return val;
  }
  
--- 4724,4729 ----
***************
*** 4874,4882 ****
       We used to make this choice before calling build_annotations, but that
       leads to problems when a write-annotate-function takes care of
       unsavable chars (as was the case with X-Symbol).  */
!   Vlast_coding_system_used
!     = choose_write_coding_system (start, end, filename,
! 				  append, visit, lockname, &coding);
  
  #ifdef CLASH_DETECTION
    if (!auto_saving)
--- 4879,4893 ----
       We used to make this choice before calling build_annotations, but that
       leads to problems when a write-annotate-function takes care of
       unsavable chars (as was the case with X-Symbol).  */
!   Vlast_coding_system_used = NILP (Vwrite_region_coding_system) ?
!     Fchoose_write_coding_system (start, end, filename,
!                                 append, visit, lockname) :
!     Vwrite_region_coding_system;
! 
!   setup_coding_system (Vlast_coding_system_used, &coding);
! 
!   if (!STRINGP (start) && !NILP (BVAR (current_buffer, selective_display)))
!     coding.mode |= CODING_MODE_SELECTIVE_DISPLAY;
  
  #ifdef CLASH_DETECTION
    if (!auto_saving)
***************
*** 5861,5866 ****
--- 5872,5878 ----
    DEFSYM (Qset_file_acl, "set-file-acl");
    DEFSYM (Qfile_newer_than_file_p, "file-newer-than-file-p");
    DEFSYM (Qinsert_file_contents, "insert-file-contents");
+   DEFSYM (Qchoose_write_coding_system, "choose-write-coding-system");
    DEFSYM (Qwrite_region, "write-region");
    DEFSYM (Qverify_visited_file_modtime, "verify-visited-file-modtime");
    DEFSYM (Qset_visited_file_modtime, "set-visited-file-modtime");
***************
*** 5890,5895 ****
--- 5902,5912 ----
  of file names regardless of the current language environment.  */);
    Vdefault_file_name_coding_system = Qnil;
  
+   DEFVAR_LISP ("write-region-coding-system", Vwrite_region_coding_system,
+ 	       doc: /* If non-nil, coding system for `write-region'.
+ You should only ever `let'-bind this around a `write-region' call.  */);
+   Vwrite_region_coding_system = Qnil;
+ 
    DEFSYM (Qformat_decode, "format-decode");
    DEFSYM (Qformat_annotate_function, "format-annotate-function");
    DEFSYM (Qafter_insert_file_set_coding, "after-insert-file-set-coding");
***************
*** 6085,6090 ****
--- 6102,6108 ----
    defsubr (&Sdefault_file_modes);
    defsubr (&Sfile_newer_than_file_p);
    defsubr (&Sinsert_file_contents);
+   defsubr (&Schoose_write_coding_system);
    defsubr (&Swrite_region);
    defsubr (&Scar_less_than_car);
    defsubr (&Sverify_visited_file_modtime);