Merge from trunk.
[bpt/emacs.git] / lisp / files.el
index 5bdb26f..f15c523 100644 (file)
@@ -1,6 +1,6 @@
 ;;; files.el --- file input and output commands for Emacs
 
-;; Copyright (C) 1985-1987, 1992-2011 Free Software Foundation, Inc.
+;; Copyright (C) 1985-1987, 1992-2012 Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
 ;; Package: emacs
@@ -917,24 +917,41 @@ See `load-file' for a different interface to `load'."
 
 (defun file-remote-p (file &optional identification connected)
   "Test whether FILE specifies a location on a remote system.
-Returns nil or a string identifying the remote connection (ideally
-a prefix of FILE).  For example, the remote identification for filename
-\"/user@host:/foo\" could be \"/user@host:\".
-A file is considered \"remote\" if accessing it is likely to be slower or
-less reliable than accessing local files.
-Furthermore, relative file names do not work across remote connections.
-
-IDENTIFICATION specifies which part of the identification shall
-be returned as string.  IDENTIFICATION can be the symbol
-`method', `user', `host' or `localname'; any other value is
-handled like nil and means to return the complete identification
-string.
-
-If CONNECTED is non-nil, the function returns an identification only
-if FILE is located on a remote system, and a connection is established
-to that remote system.
-
-`file-remote-p' will never open a connection on its own."
+A file is considered remote if accessing it is likely to
+be slower or less reliable than accessing local files.
+
+`file-remote-p' never opens a new remote connection.  It can
+only reuse a connection that is already open.
+
+Return nil or a string identifying the remote connection
+\(ideally a prefix of FILE).  Return nil if FILE is a relative
+file name.
+
+When IDENTIFICATION is nil, the returned string is a complete
+remote identifier: with components method, user, and host.  The
+components are those present in FILE, with defaults filled in for
+any that are missing.
+
+IDENTIFICATION can specify which part of the identification to
+return.  IDENTIFICATION can be the symbol `method', `user',
+`host', or `localname'.  Any other value is handled like nil and
+means to return the complete identification.  The string returned
+for IDENTIFICATION `localname' can differ depending on whether
+there is an existing connection.
+
+If CONNECTED is non-nil, return an identification only if FILE is
+located on a remote system and a connection is established to
+that remote system.
+
+Tip: You can use this expansion of remote identifier components
+     to derive a new remote file name from an existing one.  For
+     example, if FILE is \"/sudo::/path/to/file\" then
+
+       \(concat \(file-remote-p FILE) \"/bin/sh\")
+
+     returns a remote file name for file \"/bin/sh\" that has the
+     same remote identifier as FILE but expanded; a name such as
+     \"/sudo:root@myhost:/bin/sh\"."
   (let ((handler (find-file-name-handler file 'file-remote-p)))
     (if handler
        (funcall handler 'file-remote-p file identification connected)
@@ -1639,11 +1656,6 @@ home directory is a root directory) and removes automounter prefixes
                        (substring filename (match-end 0)))))
       filename)))
 
-(defcustom find-file-not-true-dirname-list nil
-  "List of logical names for which visiting shouldn't save the true dirname."
-  :type '(repeat (string :tag "Name"))
-  :group 'find-file)
-
 (defun find-buffer-visiting (filename &optional predicate)
   "Return the buffer visiting file FILENAME (a string).
 This is like `get-file-buffer', except that it checks for any buffer
@@ -2466,7 +2478,9 @@ of a script, mode MODE is enabled.
 
 See also `auto-mode-alist'.")
 
-(defvar inhibit-first-line-modes-regexps (mapcar 'purecopy '("\\.tar\\'" "\\.tgz\\'"))
+(defvar inhibit-first-line-modes-regexps
+  (mapcar 'purecopy '("\\.tar\\'" "\\.tgz\\'" "\\.tiff?\\'"
+                     "\\.gif\\'" "\\.png\\'" "\\.jpe?g\\'"))
   "List of regexps; if one matches a file name, don't look for `-*-'.")
 
 (defvar inhibit-first-line-modes-suffixes nil
@@ -2606,7 +2620,7 @@ we don't actually set it to the same mode the buffer already has."
                   (if (looking-at auto-mode-interpreter-regexp)
                       (match-string 2)
                     ""))
-           ;; Map interpreter name to a mode, signalling we're done at the
+           ;; Map interpreter name to a mode, signaling we're done at the
            ;; same time.
            done (assoc (file-name-nondirectory mode)
                        interpreter-mode-alist))
@@ -2632,12 +2646,12 @@ we don't actually set it to the same mode the buffer already has."
       (if buffer-file-name
          (let ((name buffer-file-name)
                (remote-id (file-remote-p buffer-file-name)))
+           ;; Remove backup-suffixes from file name.
+           (setq name (file-name-sans-versions name))
            ;; Remove remote file name identification.
            (when (and (stringp remote-id)
                       (string-match (regexp-quote remote-id) name))
              (setq name (substring name (match-end 0))))
-           ;; Remove backup-suffixes from file name.
-           (setq name (file-name-sans-versions name))
            (while name
              ;; Find first matching alist entry.
              (setq mode
@@ -2957,60 +2971,62 @@ Returns an alist of elements (VAR . VAL), where VAR is a variable
 and VAL is the specified value.  Ignores any specification for
 `mode:' and `coding:' (which should have already been handled
 by `set-auto-mode' and `set-auto-coding', respectively).
-Throws an error if the -*- line is malformed.
+Return nil if the -*- line is malformed.
 
 If MODE-ONLY is non-nil, just returns the symbol specifying the
 mode, if there is one, otherwise nil."
-  (save-excursion
-    (goto-char (point-min))
-    (let ((end (set-auto-mode-1))
-         result)
-      (cond ((not end)
-            nil)
-           ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
-            ;; Simple form: "-*- MODENAME -*-".
-            (if mode-only
-                (intern (concat (match-string 1) "-mode"))))
-           (t
-            ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
-            ;; (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))
-              (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
-                  (error "Malformed -*- line"))
-              (goto-char (match-end 0))
-              ;; There used to be a downcase here,
-              ;; but the manual didn't say so,
-              ;; and people want to set var names that aren't all lc.
-              (let* ((key (intern (match-string 1)))
-                     (val (save-restriction
-                            (narrow-to-region (point) end)
-                            (let ((read-circle nil))
-                              (read (current-buffer)))))
-                     ;; It is traditional to ignore
-                     ;; case when checking for `mode' in set-auto-mode,
-                     ;; so we must do that here as well.
-                     ;; That is inconsistent, but we're stuck with it.
-                     ;; The same can be said for `coding' in set-auto-coding.
-                     (keyname (downcase (symbol-name key))))
-                (if mode-only
-                    (and (equal keyname "mode")
-                         (setq result
-                               (intern (concat (downcase (symbol-name val))
-                                               "-mode"))))
-                  (or (equal keyname "coding")
-                      (condition-case nil
-                          (push (cons (cond ((eq key 'eval) 'eval)
-                                            ;; Downcase "Mode:".
-                                            ((equal keyname "mode") 'mode)
-                                            (t (indirect-variable key)))
-                                      val) result)
-                        (error nil))))
-                (skip-chars-forward " \t;")))
-            result)))))
+  (catch 'malformed-line
+    (save-excursion
+      (goto-char (point-min))
+      (let ((end (set-auto-mode-1))
+           result)
+       (cond ((not end)
+              nil)
+             ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
+              ;; Simple form: "-*- MODENAME -*-".
+              (if mode-only
+                  (intern (concat (match-string 1) "-mode"))))
+             (t
+              ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
+              ;; (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")
+                  (throw 'malformed-line nil))
+                (goto-char (match-end 0))
+                ;; There used to be a downcase here,
+                ;; but the manual didn't say so,
+                ;; and people want to set var names that aren't all lc.
+                (let* ((key (intern (match-string 1)))
+                       (val (save-restriction
+                              (narrow-to-region (point) end)
+                              (let ((read-circle nil))
+                                (read (current-buffer)))))
+                       ;; It is traditional to ignore
+                       ;; case when checking for `mode' in set-auto-mode,
+                       ;; so we must do that here as well.
+                       ;; That is inconsistent, but we're stuck with it.
+                       ;; The same can be said for `coding' in set-auto-coding.
+                       (keyname (downcase (symbol-name key))))
+                  (if mode-only
+                      (and (equal keyname "mode")
+                           (setq result
+                                 (intern (concat (downcase (symbol-name val))
+                                                 "-mode"))))
+                    (or (equal keyname "coding")
+                        (condition-case nil
+                            (push (cons (cond ((eq key 'eval) 'eval)
+                                              ;; Downcase "Mode:".
+                                              ((equal keyname "mode") 'mode)
+                                              (t (indirect-variable key)))
+                                        val) result)
+                          (error nil))))
+                  (skip-chars-forward " \t;")))
+              result))))))
 
 (defun hack-local-variables-filter (variables dir-name)
   "Filter local variable settings, querying the user if necessary.
@@ -3278,7 +3294,7 @@ It is dangerous if either of these conditions are met:
       (and (symbolp (car exp))
           ;; Allow (minor)-modes calls with no arguments.
           ;; This obsoletes the use of "mode:" for such things.  (Bug#8613)
-          (or (and (member (cdr exp) '(nil (1) (-1)))
+          (or (and (member (cdr exp) '(nil (1) (0) (-1)))
                    (string-match "-mode\\'" (symbol-name (car exp))))
               (let ((prop (get (car exp) 'safe-local-eval-function)))
                 (cond ((eq prop t)
@@ -3561,6 +3577,10 @@ and `file-local-variables-alist', without applying them."
            (hack-local-variables-filter variables dir-name)))))))
 
 (defun hack-dir-local-variables-non-file-buffer ()
+  "Apply directory-local variables to a non-file buffer.
+For non-file buffers, such as Dired buffers, directory-local
+variables are looked for in `default-directory' and its parent
+directories."
   (hack-dir-local-variables)
   (hack-local-variables-apply))
 
@@ -3683,7 +3703,11 @@ the old visited file has been renamed to the new name FILENAME."
          (get major-mode 'mode-class)
          ;; Don't change the mode if the local variable list specifies it.
          (hack-local-variables t)
-         (set-auto-mode t))
+         ;; TODO consider making normal-mode handle this case.
+         (let ((old major-mode))
+           (set-auto-mode t)
+           (or (eq old major-mode)
+               (hack-local-variables))))
     (error nil)))
 
 (defun write-file (filename &optional confirm)
@@ -4683,7 +4707,15 @@ prints a message in the minibuffer.  Instead, use `set-buffer-modified-p'."
   "Change whether this buffer is read-only.
 With prefix argument ARG, make the buffer read-only if ARG is
 positive, otherwise make it writable.  If buffer is read-only
-and `view-read-only' is non-nil, enter view mode."
+and `view-read-only' is non-nil, enter view mode.
+
+This function is usually the wrong thing to use in a Lisp program.
+It can have side-effects beyond changing the read-only status of a buffer
+\(e.g., enabling view mode), and does not affect read-only regions that
+are caused by text properties.  To make a buffer read-only in Lisp code,
+set `buffer-read-only'.  To ignore read-only status (whether due to text
+properties or buffer state) and make changes, temporarily bind
+`inhibit-read-only'."
   (interactive "P")
   (if (and arg
            (if (> (prefix-numeric-value arg) 0) buffer-read-only
@@ -4701,11 +4733,7 @@ and `view-read-only' is non-nil, enter view mode."
            (not (eq (get major-mode 'mode-class) 'special)))
       (view-mode-enter))
      (t (setq buffer-read-only (not buffer-read-only))
-        (force-mode-line-update)))
-    (if (memq (vc-backend buffer-file-name) '(RCS SCCS))
-        (message "%s" (substitute-command-keys
-                  (concat "File is under version-control; "
-                          "use \\[vc-next-action] to check in/out"))))))
+        (force-mode-line-update)))))
 
 (defun insert-file (filename)
   "Insert contents of file FILENAME into buffer after point.
@@ -4859,7 +4887,7 @@ given.  With a prefix argument, TRASH is nil."
                directory 'full directory-files-no-dot-files-regexp))
          (error "Directory is not empty, not moving to trash")
        (move-file-to-trash directory)))
-     ;; Otherwise, call outselves recursively if needed.
+     ;; Otherwise, call ourselves recursively if needed.
      (t
       (if (and recursive (not (file-symlink-p directory)))
          (mapc (lambda (file)
@@ -4942,9 +4970,10 @@ directly into NEWNAME instead."
              (copy-file file target t keep-time)))))
 
       ;; Set directory attributes.
-      (set-file-modes newname (file-modes directory))
-      (if keep-time
-         (set-file-times newname (nth 5 (file-attributes directory)))))))
+      (let ((modes (file-modes directory))
+           (times (and keep-time (nth 5 (file-attributes directory)))))
+       (if modes (set-file-modes newname modes))
+       (if times (set-file-times newname times))))))
 \f
 (put 'revert-buffer-function 'permanent-local t)
 (defvar revert-buffer-function nil
@@ -5782,7 +5811,7 @@ returns nil."
 
          ;; vc dired listings provide the state or blanks between file
          ;; permissions and date.  The state is always surrounded by
-         ;; parantheses:
+         ;; parentheses:
          ;; -rw-r--r-- (modified) 2005-10-22 21:25 files.el
          ;; This is not supported yet.
     (purecopy (concat "\\([0-9][BkKMGTPEZY]? " iso
@@ -6102,7 +6131,7 @@ message to that effect instead of signaling an error."
 
 (defvar kill-emacs-query-functions nil
   "Functions to call with no arguments to query about killing Emacs.
-If any of these functions returns nil, killing Emacs is cancelled.
+If any of these functions returns nil, killing Emacs is canceled.
 `save-buffers-kill-emacs' calls these functions, but `kill-emacs',
 the low level primitive, does not.  See also `kill-emacs-hook'.")
 
@@ -6306,7 +6335,7 @@ as in \"og+rX-w\"."
   "Convert symbolic file modes to numeric file modes.
 MODES is the string to convert, it should match
 \"[ugoa]*([+-=][rwxXstugo]*)+,...\".
-See (info \"(coreutils)File permissions\") for more information on this
+See Info node `(coreutils)File permissions' for more information on this
 notation.
 FROM (or 0 if nil) gives the mode bits on which to base permissions if
 MODES request to add, remove, or set permissions based on existing ones,
@@ -6432,12 +6461,14 @@ Otherwise, trash FILENAME using the freedesktop.org conventions,
 
           ;; Ensure that the trash directory exists; otherwise, create it.
           (let ((saved-default-file-modes (default-file-modes)))
-            (set-default-file-modes ?\700)
-            (unless (file-exists-p trash-files-dir)
-              (make-directory trash-files-dir t))
-            (unless (file-exists-p trash-info-dir)
-              (make-directory trash-info-dir t))
-            (set-default-file-modes saved-default-file-modes))
+            (unwind-protect
+                (progn
+                  (set-default-file-modes #o700)
+                  (unless (file-exists-p trash-files-dir)
+                    (make-directory trash-files-dir t))
+                  (unless (file-exists-p trash-info-dir)
+                    (make-directory trash-info-dir t)))
+              (set-default-file-modes saved-default-file-modes)))
 
           ;; Try to move to trash with .trashinfo undo information
           (save-excursion
@@ -6492,7 +6523,7 @@ Otherwise, trash FILENAME using the freedesktop.org conventions,
                                 (setq tries 0 success t))
                             (file-already-exists nil))
                     (setq tries (1- tries))
-                    ;; Uniqify new-fn.  (Some file managers do not
+                    ;; Uniquify new-fn.  (Some file managers do not
                     ;; like Emacs-style backup file names---e.g. bug
                     ;; 170956 in Konqueror bug tracker.)
                     (setq new-fn (make-temp-name (concat base-fn "_")))))