Timestamp fixes for undo.
[bpt/emacs.git] / lisp / files.el
index f9ed65b..26f229e 100644 (file)
@@ -206,7 +206,7 @@ have fast storage with limited space, such as a RAM disk."
 (declare-function msdos-long-file-names "msdos.c")
 (declare-function w32-long-file-name "w32proc.c")
 (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
-(declare-function dired-unmark "dired" (arg))
+(declare-function dired-unmark "dired" (arg &optional interactive))
 (declare-function dired-do-flagged-delete "dired" (&optional nomessage))
 (declare-function dos-8+3-filename "dos-fns" (filename))
 (declare-function dosified-file-name "dos-fns" (file-name))
@@ -316,12 +316,14 @@ A value of nil means don't add newlines.
 
 Certain major modes set this locally to the value obtained
 from `mode-require-final-newline'."
+  :safe #'symbolp
   :type '(choice (const :tag "When visiting" visit)
                 (const :tag "When saving" t)
                 (const :tag "When visiting or saving" visit-save)
                 (const :tag "Don't add newlines" nil)
                 (other :tag "Ask each time" ask))
-  :group 'editing-basics)
+  :group 'editing-basics
+  :version "24.4")
 
 (defcustom mode-require-final-newline t
   "Whether to add a newline at end of file, in certain major modes.
@@ -1516,7 +1518,10 @@ expand wildcards (if any) and replace the file with multiple files."
 (defvar kill-buffer-hook nil
   "Hook run when a buffer is killed.
 The buffer being killed is current while the hook is running.
-See `kill-buffer'.")
+See `kill-buffer'.
+
+Note: Be careful with let-binding this hook considering it is
+frequently used for cleanup.")
 
 (defun find-alternate-file (filename &optional wildcards)
   "Find file FILENAME, select its buffer, kill previous buffer.
@@ -1856,13 +1861,12 @@ the various files."
                      (setq buffer-read-only read-only)))
                  (setq buffer-file-read-only read-only))
 
-               (when (and (not (eq (not (null rawfile))
-                                   (not (null find-file-literally))))
-                          (not nonexistent)
-                          ;; It is confusing to ask whether to visit
-                          ;; non-literally if they have the file in
-                          ;; hexl-mode or image-mode.
-                          (not (memq major-mode '(hexl-mode image-mode))))
+               (unless (or (eq (null rawfile) (null find-file-literally))
+                           nonexistent
+                           ;; It is confusing to ask whether to visit
+                           ;; non-literally if they have the file in
+                           ;; hexl-mode or image-mode.
+                           (memq major-mode '(hexl-mode image-mode)))
                  (if (buffer-modified-p)
                      (if (y-or-n-p
                           (format
@@ -1983,8 +1987,7 @@ Do you want to revisit the file normally now? ")
            (set-buffer-multibyte nil)
            (setq buffer-file-coding-system 'no-conversion)
            (set-buffer-major-mode buf)
-           (make-local-variable 'find-file-literally)
-           (setq find-file-literally t))
+           (setq-local find-file-literally t))
        (after-find-file error (not nowarn)))
       (current-buffer))))
 \f
@@ -2172,7 +2175,7 @@ not set local variables (though we do notice a mode specified with -*-.)
 or from Lisp without specifying the optional argument FIND-FILE;
 in that case, this function acts as if `enable-local-variables' were t."
   (interactive)
-  (funcall (or (default-value 'major-mode) 'fundamental-mode))
+  (fundamental-mode)
   (let ((enable-local-variables (or (not find-file) enable-local-variables)))
     ;; FIXME this is less efficient than it could be, since both
     ;; s-a-m and h-l-v may parse the same regions, looking for "mode:".
@@ -2310,7 +2313,7 @@ since only a single case-insensitive search through the alist is made."
      ("\\.\\(\
 arc\\|zip\\|lzh\\|lha\\|zoo\\|[jew]ar\\|xpi\\|rar\\|7z\\|\
 ARC\\|ZIP\\|LZH\\|LHA\\|ZOO\\|[JEW]AR\\|XPI\\|RAR\\|7Z\\)\\'" . archive-mode)
-     ("\\.\\(sx[dmicw]\\|od[fgpst]\\|oxt\\)\\'" . archive-mode) ;OpenOffice.org
+     ("\\.oxt\\'" . archive-mode) ;(Open|Libre)Office extensions.
      ("\\.\\(deb\\|[oi]pk\\)\\'" . archive-mode) ; Debian/Opkg packages.
      ;; Mailer puts message to be edited in
      ;; /tmp/Re.... or Message
@@ -2498,6 +2501,7 @@ See also `auto-mode-alist'.")
                      "\\.zoo\\'" "\\.[jew]ar\\'" "\\.xpi\\'" "\\.rar\\'"
                      "\\.7z\\'"
                      "\\.sx[dmicw]\\'" "\\.odt\\'"
+                     "\\.diff\\'" "\\.patch\\'"
                      "\\.tiff?\\'" "\\.gif\\'" "\\.png\\'" "\\.jpe?g\\'"))
   "List of regexps matching file names in which to ignore local variables.
 This includes `-*-' lines as well as trailing \"Local Variables\" sections.
@@ -2755,7 +2759,9 @@ we don't actually set it to the same mode the buffer already has."
                                          (if (functionp re)
                                              (funcall re)
                                            (looking-at re)))))))
-         (set-auto-mode-0 done keep-mode-if-same)))))
+         (set-auto-mode-0 done keep-mode-if-same)))
+    (unless done
+      (set-buffer-major-mode (current-buffer)))))
 
 ;; When `keep-mode-if-same' is set, we are working on behalf of
 ;; set-visited-file-name.  In that case, if the major mode specified is the
@@ -3025,6 +3031,9 @@ n  -- to ignore the local variables list.")
          (prog1 (memq char '(?! ?\s ?y))
            (quit-window t)))))))
 
+(defconst hack-local-variable-regexp
+  "[ \t]*\\([^][;\"'?()\\ \t\n]+\\)[ \t]*:[ \t]*")
+
 (defun hack-local-variables-prop-line (&optional mode-only)
   "Return local variables specified in the -*- line.
 Returns an alist of elements (VAR . VAL), where VAR is a variable
@@ -3051,11 +3060,11 @@ mode, if there is one, otherwise nil."
               ;; (last ";" is optional).
               ;; If MODE-ONLY, just check for `mode'.
               ;; Otherwise, parse the -*- line into the RESULT alist.
-              (while (and (or (not mode-only)
-                              (not result))
-                          (< (point) end))
-                (unless (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
-                  (message "Malformed mode-line")
+              (while (not (or (and mode-only result)
+                               (>= (point) end)))
+                (unless (looking-at hack-local-variable-regexp)
+                  (message "Malformed mode-line: %S"
+                            (buffer-substring-no-properties (point) end))
                   (throw 'malformed-line nil))
                 (goto-char (match-end 0))
                 ;; There used to be a downcase here,
@@ -3207,8 +3216,7 @@ local variables, but directory-local variables may still be applied."
                  (prefix
                   (concat "^" (regexp-quote
                                (buffer-substring (line-beginning-position)
-                                                 (match-beginning 0)))))
-                 beg)
+                                                 (match-beginning 0))))))
 
              (forward-line 1)
              (let ((startpos (point))
@@ -3243,18 +3251,16 @@ local variables, but directory-local variables may still be applied."
                    (forward-line 1))
                  (goto-char (point-min))
 
-                 (while (and (not (eobp))
-                             (or (not mode-only)
-                                 (not result)))
-                   ;; Find the variable name; strip whitespace.
-                   (skip-chars-forward " \t")
-                   (setq beg (point))
-                   (skip-chars-forward "^:\n")
-                   (if (eolp) (error "Missing colon in local variables entry"))
-                   (skip-chars-backward " \t")
-                   (let* ((str (buffer-substring beg (point)))
-                          (var (let ((read-circle nil))
-                                 (read str)))
+                 (while (not (or (eobp)
+                                  (and mode-only result)))
+                   ;; Find the variable name;
+                   (unless (looking-at hack-local-variable-regexp)
+                      (error "Malformed local variable line: %S"
+                             (buffer-substring-no-properties
+                              (point) (line-end-position))))
+                    (goto-char (match-end 1))
+                   (let* ((str (match-string 1))
+                          (var (intern str))
                           val val2)
                      (and (equal (downcase (symbol-name var)) "mode")
                           (setq var 'mode))
@@ -3874,6 +3880,10 @@ Interactively, confirmation is required unless you supply a prefix argument."
                                    (or buffer-file-name (buffer-name))))))
        (and confirm
             (file-exists-p filename)
+            ;; NS does its own confirm dialog.
+            (not (and (eq (framep-on-display) 'ns)
+                      (listp last-nonmenu-event)
+                      use-dialog-box))
             (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
                 (error "Canceled")))
        (set-visited-file-name filename (not confirm))))
@@ -4176,23 +4186,31 @@ ignored."
   "Default `backup-enable-predicate' function.
 Checks for files in `temporary-file-directory',
 `small-temporary-file-directory', and /tmp."
-  (not (or (let ((comp (compare-strings temporary-file-directory 0 nil
-                                       name 0 nil)))
-            ;; Directory is under temporary-file-directory.
-            (and (not (eq comp t))
-                 (< comp (- (length temporary-file-directory)))))
-          (let ((comp (compare-strings "/tmp" 0 nil
-                                       name 0 nil)))
-            ;; Directory is under /tmp.
-            (and (not (eq comp t))
-                 (< comp (- (length "/tmp")))))
-          (if small-temporary-file-directory
-              (let ((comp (compare-strings small-temporary-file-directory
-                                           0 nil
-                                           name 0 nil)))
-                ;; Directory is under small-temporary-file-directory.
-                (and (not (eq comp t))
-                     (< comp (- (length small-temporary-file-directory)))))))))
+  (let ((temporary-file-directory temporary-file-directory)
+       caseless)
+    ;; On MS-Windows, file-truename will convert short 8+3 aliases to
+    ;; their long file-name equivalents, so compare-strings does TRT.
+    (if (memq system-type '(ms-dos windows-nt))
+       (setq temporary-file-directory (file-truename temporary-file-directory)
+             name (file-truename name)
+             caseless t))
+    (not (or (let ((comp (compare-strings temporary-file-directory 0 nil
+                                         name 0 nil caseless)))
+              ;; Directory is under temporary-file-directory.
+              (and (not (eq comp t))
+                   (< comp (- (length temporary-file-directory)))))
+            (let ((comp (compare-strings "/tmp" 0 nil
+                                         name 0 nil)))
+              ;; Directory is under /tmp.
+              (and (not (eq comp t))
+                   (< comp (- (length "/tmp")))))
+            (if small-temporary-file-directory
+                (let ((comp (compare-strings small-temporary-file-directory
+                                             0 nil
+                                             name 0 nil caseless)))
+                  ;; Directory is under small-temporary-file-directory.
+                  (and (not (eq comp t))
+                       (< comp (- (length small-temporary-file-directory))))))))))
 
 (defun make-backup-file-name (file)
   "Create the non-numeric backup file name for FILE.
@@ -4563,39 +4581,21 @@ Before and after saving the buffer, this function runs
                 (not (file-exists-p buffer-file-name))))
        (let ((recent-save (recent-auto-save-p))
              setmodes)
-         (if buffer-file-name
-             (let ((dir (file-name-directory
-                         (expand-file-name buffer-file-name))))
-               (unless (file-exists-p dir)
-                 (if (y-or-n-p
-                      (format "Directory `%s' does not exist; create? " dir))
-                     (make-directory dir t)
-                   (error "Canceled"))))
-           ;; If buffer has no file name, ask user for one.
-           (let ((filename
-                  (expand-file-name
-                   (read-file-name "File to save in: "
-                                   nil (expand-file-name (buffer-name))))))
-             (if (file-exists-p filename)
-                 (if (file-directory-p filename)
-                     ;; Signal an error if the user specified the name of an
-                     ;; existing directory.
-                     (error "%s is a directory" filename)
-                   (unless (y-or-n-p (format "File `%s' exists; overwrite? "
-                                             filename))
-                     (error "Canceled")))
-               ;; Signal an error if the specified name refers to a
-               ;; non-existing directory.
-               (let ((dir (file-name-directory filename)))
-                 (unless (file-directory-p dir)
-                   (if (file-exists-p dir)
-                       (error "%s is not a directory" dir)
-                     (if (y-or-n-p
-                          (format "Directory `%s' does not exist; create? "
-                                  dir))
-                         (make-directory dir t)
-                       (error "Canceled"))))))
-             (set-visited-file-name filename)))
+          ;; If buffer has no file name, ask user for one.
+         (or buffer-file-name
+              (let ((filename
+                     (expand-file-name
+                      (read-file-name "File to save in: "
+                                      nil (expand-file-name (buffer-name))))))
+                (if (file-exists-p filename)
+                    (if (file-directory-p filename)
+                        ;; Signal an error if the user specified the name of an
+                        ;; existing directory.
+                        (error "%s is a directory" filename)
+                      (unless (y-or-n-p (format "File `%s' exists; overwrite? "
+                                                filename))
+                        (error "Canceled"))))
+                (set-visited-file-name filename)))
          (or (verify-visited-file-modtime (current-buffer))
              (not (file-exists-p buffer-file-name))
              (yes-or-no-p
@@ -4622,13 +4622,21 @@ Before and after saving the buffer, this function runs
                     (insert ?\n))))
            ;; Support VC version backups.
            (vc-before-save)
-           (run-hooks 'before-save-hook)
+           ;; Don't let errors prevent saving the buffer.
+           (with-demoted-errors (run-hooks 'before-save-hook))
            (or (run-hook-with-args-until-success 'write-contents-functions)
                (run-hook-with-args-until-success 'local-write-file-hooks)
                (run-hook-with-args-until-success 'write-file-functions)
                ;; If a hook returned t, file is already "written".
                ;; Otherwise, write it the usual way now.
-               (setq setmodes (basic-save-buffer-1)))
+               (let ((dir (file-name-directory
+                           (expand-file-name buffer-file-name))))
+                 (unless (file-exists-p dir)
+                   (if (y-or-n-p
+                        (format "Directory `%s' does not exist; create? " dir))
+                       (make-directory dir t)
+                     (error "Canceled")))
+                 (setq setmodes (basic-save-buffer-1))))
            ;; Now we have saved the current buffer.  Let's make sure
            ;; that buffer-file-coding-system is fixed to what
            ;; actually used for saving by binding it locally.
@@ -4910,6 +4918,11 @@ change the additional actions you can take on files."
                     (length autosaved-buffers)
                     (mapconcat 'identity autosaved-buffers ", "))))))))
 \f
+(defun clear-visited-file-modtime ()
+  "Clear out records of last mod time of visited file.
+Next attempt to save will certainly not complain of a discrepancy."
+  (set-visited-file-modtime 0))
+
 (defun not-modified (&optional arg)
   "Mark current buffer as unmodified, not needing to be saved.
 With prefix ARG, mark buffer as modified, so \\[save-buffer] will save.