From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: "Stefan Monnier" Newsgroups: gmane.emacs.devel Subject: 4 minor suggestions for files.el Date: Mon, 14 Apr 2003 16:22:17 -0400 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: <200304142022.h3EKMHRu026814@rum.cs.yale.edu> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: main.gmane.org 1050352640 571 80.91.224.249 (14 Apr 2003 20:37:20 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 14 Apr 2003 20:37:20 +0000 (UTC) Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Mon Apr 14 22:37:18 2003 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 195Aha-00008r-00 for ; Mon, 14 Apr 2003 22:37:18 +0200 Original-Received: from monty-python.gnu.org ([199.232.76.173]) by quimby.gnus.org with esmtp (Exim 3.12 #1 (Debian)) id 195Aj7-00083K-00 for ; Mon, 14 Apr 2003 22:38:53 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 195AgI-0006xf-02 for emacs-devel@quimby.gnus.org; Mon, 14 Apr 2003 16:35:58 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10.13) id 195AZR-00057T-00 for emacs-devel@gnu.org; Mon, 14 Apr 2003 16:28:53 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10.13) id 195ATq-0003iz-00 for emacs-devel@gnu.org; Mon, 14 Apr 2003 16:23:08 -0400 Original-Received: from rum.cs.yale.edu ([128.36.229.169]) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 195AT4-0003Zs-00 for emacs-devel@gnu.org; Mon, 14 Apr 2003 16:22:18 -0400 Original-Received: from rum.cs.yale.edu (localhost [127.0.0.1]) by rum.cs.yale.edu (8.12.8/8.12.8) with ESMTP id h3EKMHx6026816 for ; Mon, 14 Apr 2003 16:22:17 -0400 Original-Received: (from monnier@localhost) by rum.cs.yale.edu (8.12.8/8.12.8/Submit) id h3EKMHRu026814; Mon, 14 Apr 2003 16:22:17 -0400 X-Mailer: exmh version 2.4 06/23/2000 with nmh-1.0.4 Original-To: emacs-devel@gnu.org X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Emacs development discussions. List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:13230 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:13230 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