diff lisp/files.el @ 83224:7a0245dd1848

Merged in changes from CVS trunk. Patches applied: * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-653 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-654 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-655 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-656 Update from CVS: lisp/man.el (Man-xref-normal-file): Fix help-echo. * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-657 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-658 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-659 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-660 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-661 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-662 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-663 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-664 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-665 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-666 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-667 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-668 Merge from gnus--rel--5.10 * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-669 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-670 Update from CVS * miles@gnu.org--gnu-2004/emacs--cvs-trunk--0--patch-671 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-64 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-65 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-66 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-67 Update from CVS * miles@gnu.org--gnu-2004/gnus--rel--5.10--patch-68 Update from CVS git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-264
author Karoly Lorentey <lorentey@elte.hu>
date Sat, 06 Nov 2004 17:52:02 +0000
parents 4056279af756 a705018ca13b
children 3ec251523b3e
line wrap: on
line diff
--- a/lisp/files.el	Sun Oct 31 02:05:24 2004 +0000
+++ b/lisp/files.el	Sat Nov 06 17:52:02 2004 +0000
@@ -676,7 +676,7 @@
 both at the level of the file and at the level of the directories
 containing it, until no links are left at any level.
 
-\(fn FILENAME)"
+\(fn FILENAME)"  ;; Don't document the optional arguments.
   ;; COUNTER and PREV-DIRS are only used in recursive calls.
   ;; COUNTER can be a cons cell whose car is the count of how many
   ;; more links to chase before getting an error.
@@ -977,6 +977,14 @@
 	  (mapcar 'switch-to-buffer (cdr value)))
       (switch-to-buffer-other-frame value))))
 
+(defun find-file-existing (filename &optional wildcards)
+  "Edit the existing file FILENAME.
+Like \\[find-file] but only allow files that exists."
+  (interactive (find-file-read-args "Find existing file: " t))
+  (unless (file-exists-p filename) (error "%s does not exist" filename))
+  (find-file filename wildcards)
+  (current-buffer))
+
 (defun find-file-read-only (filename &optional wildcards)
   "Edit file FILENAME but don't allow changes.
 Like \\[find-file] but marks buffer as read-only.
@@ -1225,6 +1233,7 @@
 When nil, never request confirmation."
   :group 'files
   :group 'find-file
+  :version "21.4"
   :type '(choice integer (const :tag "Never request confirmation" nil)))
 
 (defun find-file-noselect (filename &optional nowarn rawfile wildcards)
@@ -1836,20 +1845,27 @@
 regular expression.  The mode is then determined as the mode associated
 with that interpreter in `interpreter-mode-alist'.")
 
-(defvar xml-based-modes '(html-mode)
-  "Modes that override an XML declaration.
-When `set-auto-mode' sees an <?xml or <!DOCTYPE declaration, that
-buffer will be in some XML mode.  If `auto-mode-alist' associates
-the file with one of the modes in this list, that mode will be
-used.  Else `xml-mode' or `sgml-mode' is used.")
+(defvar magic-mode-alist
+  '(;; The < comes before the groups (but the first) to reduce backtracking.
+    ;; Is there a nicer way of getting . including \n?
+    ;; TODO: UTF-16 <?xml may be preceded by a BOM 0xff 0xfe or 0xfe 0xff.
+    ("\\(?:<\\?xml\\s +[^>]*>\\)?\\s *<\\(?:!--\\(?:.\\|\n\\)*?-->\\s *<\\)*\\(?:!DOCTYPE\\s +[^>]*>\\s *<\\)?\\s *\\(?:!--\\(?:.\\|\n\\)*?-->\\s *<\\)*[Hh][Tt][Mm][Ll]" . html-mode)
+    ;; These two must come after html, because they are more general:
+    ("<\\?xml " . xml-mode)
+    ("\\s *<\\(?:!--\\(?:.\\|\n\\)*?-->\\s *<\\)*!DOCTYPE " . sgml-mode)
+    ("%![^V]" . ps-mode))
+  "Alist of buffer beginnings vs corresponding major mode functions.
+Each element looks like (REGEXP . FUNCTION).  FUNCTION will be
+called, unless it is nil.")
 
 (defun set-auto-mode (&optional keep-mode-if-same)
   "Select major mode appropriate for current buffer.
+
 This checks for a -*- mode tag in the buffer's text, checks the
 interpreter that runs this file against `interpreter-mode-alist',
-looks for an <?xml or <!DOCTYPE declaration (see
-`xml-based-modes'), or compares the filename against the entries
-in `auto-mode-alist'.
+compares the buffer beginning against `magic-mode-alist',
+or compares the filename against the entries in
+`auto-mode-alist'.
 
 It does not check for the `mode:' local variable in the
 Local Variables section of the file; for that, use `hack-local-variables'.
@@ -1895,7 +1911,8 @@
 	    (if (not (functionp mode))
 		(message "Ignoring unknown mode `%s'" mode)
 	      (setq done t)
-	      (or (set-auto-mode-0 mode)
+	      (or (set-auto-mode-0 mode keep-mode-if-same)
+		  ;; continuing would call minor modes again, toggling them off
 		  (throw 'nop nil)))))
       ;; If we didn't, look for an interpreter specified in the first line.
       ;; As a special case, allow for things like "#!/bin/env perl", which
@@ -1909,47 +1926,49 @@
 	    ;; same time.
 	    done (assoc (file-name-nondirectory mode)
 			interpreter-mode-alist))
-      ;; If we found an interpreter mode to use, invoke it now.
-      (if done (set-auto-mode-0 (cdr done))))
-    (if (and (not done) buffer-file-name)
-	(let ((name buffer-file-name))
-	  ;; Remove backup-suffixes from file name.
-	  (setq name (file-name-sans-versions name))
-	  (while name
-	    ;; Find first matching alist entry.
-	    (let ((case-fold-search
-		   (memq system-type '(vax-vms windows-nt cygwin))))
-	      (if (and (setq mode (assoc-default name auto-mode-alist
+      (if done
+	  (set-auto-mode-0 (cdr done) keep-mode-if-same)))
+    ;; If we found an interpreter mode to use, invoke it now.
+    (unless done
+      (if (setq done (save-excursion
+		       (goto-char (point-min))
+		       (assoc-default nil magic-mode-alist
+				      (lambda (re dummy)
+					(looking-at re)))))
+	  (set-auto-mode-0 done keep-mode-if-same)
+	(if buffer-file-name
+	    (let ((name buffer-file-name))
+	      ;; Remove backup-suffixes from file name.
+	      (setq name (file-name-sans-versions name))
+	      (while name
+		;; Find first matching alist entry.
+		(let ((case-fold-search
+		       (memq system-type '(vax-vms windows-nt cygwin))))
+		  (if (and (setq mode (assoc-default name auto-mode-alist
 						 'string-match))
-		       (consp mode)
-		       (cadr mode))
-		  (setq mode (car mode)
-			name (substring name 0 (match-beginning 0)))
-		(setq name)))
-	    (when mode
-	      (if xml (or (memq mode xml-based-modes)
-			  (setq mode 'xml-mode)))
-	      (set-auto-mode-0 mode)
-	      (setq done t)))))
-    (and xml
-	 (not done)
-	 (set-auto-mode-0 'xml-mode))))
+			   (consp mode)
+			   (cadr mode))
+		      (setq mode (car mode)
+			    name (substring name 0 (match-beginning 0)))
+		    (setq name)))
+		(when mode
+		  (set-auto-mode-0 mode keep-mode-if-same)))))))))
 
 
 ;; 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
 ;; same one we already have, don't actually reset it.  We don't want to lose
 ;; minor modes such as Font Lock.
-(defun set-auto-mode-0 (mode)
+(defun set-auto-mode-0 (mode &optional keep-mode-if-same)
   "Apply MODE and return it.
-If `keep-mode-if-same' is non-nil MODE is chased of any aliases and
-compared to current major mode.  If they are the same, do nothing
-and return nil."
+If optional arg KEEP-MODE-IF-SAME is non-nil, MODE is chased of
+any aliases and compared to current major mode.  If they are the
+same, do nothing and return nil."
   (when keep-mode-if-same
     (while (symbolp (symbol-function mode))
       (setq mode (symbol-function mode)))
     (if (eq mode major-mode)
-	(setq mode)))
+	(setq mode nil)))
   (when mode
     (funcall mode)
     mode))
@@ -3813,7 +3832,7 @@
 
 (defun kill-some-buffers (&optional list)
   "Kill some buffers.  Asks the user whether to kill each one of them.
-Non-interactively, if optional argument LIST is non-`nil', it
+Non-interactively, if optional argument LIST is non-nil, it
 specifies the list of buffers to kill, asking for approval for each one."
   (interactive)
   (if (null list)