emacs-devel
[Top][All Lists]
Advanced

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

4 minor suggestions for files.el


From: Stefan Monnier
Subject: 4 minor suggestions for files.el
Date: Mon, 14 Apr 2003 16:22:17 -0400

1 - Don't allow viewing non-existent files.  The interactive spec already
    ensures that's the case, but it doesn't work when the function
    is called from dired or somesuch (the file might have existed
    when the dired buffer was created).

@@ -928,6 +928,7 @@
 Like \\[find-file] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
   (interactive (find-file-read-args "Find file read-only: " t))
+  (unless (file-exists-p filename) (error "%s does not exist" filename))
   (find-file filename wildcards)
   (toggle-read-only 1)
   (current-buffer))
@@ -937,6 +938,7 @@
 Like \\[find-file-other-window] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
   (interactive (find-file-read-args "Find file read-only other window: " t))
+  (unless (file-exists-p filename) (error "%s does not exist" filename))
   (find-file-other-window filename wildcards)
   (toggle-read-only 1)
   (current-buffer))
@@ -946,6 +948,7 @@
 Like \\[find-file-other-frame] but marks buffer as read-only.
 Use \\[toggle-read-only] to permit editing."
   (interactive (find-file-read-args "Find file read-only other frame: " t))
+  (unless (file-exists-p filename) (error "%s does not exist" filename))
   (find-file-other-frame filename wildcards)
   (toggle-read-only 1)
   (current-buffer))


2 - Prompt the user when trying to open a very large file.  My Emacs crawls
    to a near halt when I try to open a 16MB file, so if I ever try to
    open such a file, I'd rather be warned.

@@ -1163,6 +1166,9 @@
   :version "21.1"
   :type 'boolean)
 
+(defcustom large-file-warning-threshold 10000000
+  "Maximum size of file above which a confirmation is requested.")
+
 (defun find-file-noselect (filename &optional nowarn rawfile wildcards)
   "Read file FILENAME into a buffer and return the buffer.
 If a buffer exists visiting FILENAME, return that one, but
@@ -1198,7 +1204,8 @@
            (mapcar #'find-file-noselect files)))
       (let* ((buf (get-file-buffer filename))
             (truename (abbreviate-file-name (file-truename filename)))
-            (number (nthcdr 10 (file-attributes truename)))
+            (attributes (file-attributes truename))
+            (number (nthcdr 10 attributes))
             ;; Find any buffer for a file which has same truename.
             (other (and (not buf) (find-buffer-visiting filename))))
        ;; Let user know if there is a buffer with the same truename.
@@ -1212,6 +1219,17 @@
              ;; Optionally also find that buffer.
              (if (or find-file-existing-other-name find-file-visit-truename)
                  (setq buf other))))
+       ;; Check to see if the file looks uncommonly large.
+       (when (and large-file-warning-threshold (nth 7 attributes)
+                  ;; Don't ask again if we already have the file or
+                  ;; if we're asked to be quiet.
+                  (not (or buf nowarn))
+                  (> (nth 7 attributes) large-file-warning-threshold)
+                  (not (y-or-n-p
+                        (format "File %s is large (%sMB), really open? "
+                                (file-name-nondirectory filename)
+                                  (/ (nth 7 attributes) 1048576)))))
+         (error "Aborted"))
        (if buf
            ;; We are using an existing buffer.
            (progn


3 - Don't catch errors to turn them into messages when debug-on-error
    is set to t.  I wish I could solve it more generically.

@@ -1521,6 +1539,17 @@
       (view-mode-enter))
     (run-hooks 'find-file-hook)))
 
+(defmacro with-errors-caught (format &rest body)
+  "Eval BODY and turn any error into a FORMAT message.
+FORMAT can have a %s escape which will be replaced with the actual error.
+If `debug-on-error' is set, errors are not caught, so that you can
+debug them."
+  `(if debug-on-error
+       (progn . ,body)
+     (condition-case err
+        (progn . ,body)
+       (error (message ,format (prin1-to-string err))))))
+
 (defun normal-mode (&optional find-file)
   "Choose the major mode for this buffer automatically.
 Also sets up any specified local variables of the file.
@@ -1538,16 +1567,11 @@
 in that case, this function acts as if `enable-local-variables' were t."
   (interactive)
   (or find-file (funcall (or default-major-mode 'fundamental-mode)))
-  (condition-case err
-      (set-auto-mode)
-    (error (message "File mode specification error: %s"
-                   (prin1-to-string err))))
-  (condition-case err
-      (let ((enable-local-variables (or (not find-file)
-                                       enable-local-variables)))
-       (hack-local-variables))
-    (error (message "File local-variables error: %s"
-                   (prin1-to-string err))))
+  (with-errors-caught "File mode specification error: %s"
+    (set-auto-mode))
+  (with-errors-caught "File local-variables error: %s"
+    (let ((enable-local-variables (or (not find-file) enable-local-variables)))
+      (hack-local-variables)))
   (if (fboundp 'ucs-set-table-for-input) ; don't lose when building
       (ucs-set-table-for-input)))
 

4 - Don't ask for confirmation when the user has just gone through the
    trouble of typing the whole M-x revert-buffer RET thing.

@@ -3449,7 +3475,10 @@
   ;; there's no straightforward way to encourage authors to notice a
   ;; reversal of the argument sense.  So I'm just changing the user
   ;; interface, but leaving the programmatic interface the same.
-  (interactive (list (not current-prefix-arg)))
+  (interactive (list (not current-prefix-arg)
+                    ;; Don't request confirmation if the user just hit
+                    ;; M-x revert-buffer RET.
+                    (eq last-command-char ?\r)))
   (if revert-buffer-function
       (funcall revert-buffer-function ignore-auto noconfirm)
     (let* ((auto-save-p (and (not ignore-auto)


5 - Don't junk the buffer-undo-list when reverting while preserving modes.
    This is convenient with things like auto-revert or VC, especially
    combined with the undo-in-region feature.  It does require changes
    in the C code for insert-file-contents as well, since it
    currently always junks the undo list.  These changes aren't
    included here, but they are not needed for the code to work
    as well as before.

@@ -3481,20 +3510,22 @@
                  (not (verify-visited-file-modtime (current-buffer)))
                  (setq buffer-backed-up nil))
             ;; Get rid of all undo records for this buffer.
-            (or (eq buffer-undo-list t)
+            (or (eq buffer-undo-list t) preserve-modes
                 (setq buffer-undo-list nil))
             ;; Effectively copy the after-revert-hook status,
             ;; since after-find-file will clobber it.
-            (let ((global-hook (default-value 'after-revert-hook))
+            (let* ((global-hook (default-value 'after-revert-hook))
                   (local-hook-p (local-variable-p 'after-revert-hook))
-                  (local-hook (and (local-variable-p 'after-revert-hook)
-                                   after-revert-hook)))
-              (let (buffer-read-only
-                    ;; Don't make undo records for the reversion.
-                    (buffer-undo-list t))
+                   (local-hook (and local-hook-p after-revert-hook)))
+              (let ((inhibit-read-only t))
                 (if revert-buffer-insert-file-contents-function
+                    (if preserve-modes
                     (funcall revert-buffer-insert-file-contents-function
                              file-name auto-save-p)
+                      ;; Don't make undo records for the reversion.
+                      (let ((buffer-undo-list t))
+                        (funcall revert-buffer-insert-file-contents-function
+                                 file-name auto-save-p)))
                   (if (not (file-exists-p file-name))
                       (error (if buffer-file-number
                                  "File %s no longer exists!"
@@ -3523,8 +3554,10 @@
                         (let ((buffer-file-format buffer-file-format))
                           (insert-file-contents file-name (not auto-save-p)
                                                 nil nil t))
+                      ;; Don't make undo records for the reversion.
+                      (let ((buffer-undo-list t))
                       (insert-file-contents file-name (not auto-save-p)
-                                            nil nil t)))))
+                                              nil nil t))))))
               ;; Recompute the truename in case changes in symlinks
               ;; have changed the truename.
               (setq buffer-file-truename


Any comment ?


        Stefan





reply via email to

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