all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* 4 minor suggestions for files.el
@ 2003-04-14 20:22 Stefan Monnier
  2003-04-15 19:14 ` Kai Großjohann
                   ` (2 more replies)
  0 siblings, 3 replies; 28+ messages in thread
From: Stefan Monnier @ 2003-04-14 20:22 UTC (permalink / raw)



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

^ permalink raw reply	[flat|nested] 28+ messages in thread

end of thread, other threads:[~2003-05-09 11:20 UTC | newest]

Thread overview: 28+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-04-14 20:22 4 minor suggestions for files.el Stefan Monnier
2003-04-15 19:14 ` Kai Großjohann
2003-04-15 20:16 ` Kevin Rodgers
2003-04-16  4:40 ` Richard Stallman
2003-04-17 21:39   ` Stefan Monnier
2003-04-18 11:28     ` Richard Stallman
2003-04-18 13:24       ` Andre Spiegel
2003-04-18 14:23         ` Stefan Monnier
2003-04-18 14:36           ` Andre Spiegel
2003-04-18 14:40             ` Stefan Monnier
2003-04-19 19:11             ` Stefan Monnier
2003-04-22  0:45               ` Richard Stallman
2003-04-19 13:35         ` Richard Stallman
2003-04-29 21:07           ` Stefan Monnier
2003-05-05 14:32             ` Richard Stallman
2003-05-05 14:52               ` Andre Spiegel
2003-05-06 10:14                 ` Richard Stallman
2003-05-06 10:56                   ` Andre Spiegel
2003-05-07 11:51                     ` Richard Stallman
2003-05-07 12:31                       ` Andre Spiegel
2003-05-07 14:26                         ` Stefan Monnier
2003-05-09 11:20                         ` Richard Stallman
2003-05-06 13:00                   ` Stefan Monnier
2003-05-07 11:50                     ` Richard Stallman
2003-05-05 14:57               ` Stefan Monnier
2003-05-06 10:14                 ` Richard Stallman
2003-05-06 12:54                   ` Stefan Monnier
2003-05-07 11:50                     ` Richard Stallman

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.