;;; files.el --- file input and output commands for Emacs
-;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-;; 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1986, 1987, 1992, 1993, 1994, 1995, 1996,
+;; 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
+;; 2006 Free Software Foundation, Inc.
;; Maintainer: FSF
;;; Code:
+(defvar font-lock-keywords)
+
+
(defgroup backup nil
"Backups of edited data files."
:group 'files)
(setq count (1+ count))))
newname))
+(defun make-temp-file (prefix &optional dir-flag suffix)
+ "Create a temporary file.
+The returned file name (created by appending some random characters at the end
+of PREFIX, and expanding against `temporary-file-directory' if necessary),
+is guaranteed to point to a newly created empty file.
+You can then use `write-region' to write new data into the file.
+
+If DIR-FLAG is non-nil, create a new empty directory instead of a file.
+
+If SUFFIX is non-nil, add that at the end of the file name."
+ (let ((umask (default-file-modes))
+ file)
+ (unwind-protect
+ (progn
+ ;; Create temp files with strict access rights. It's easy to
+ ;; loosen them later, whereas it's impossible to close the
+ ;; time-window of loose permissions otherwise.
+ (set-default-file-modes ?\700)
+ (while (condition-case ()
+ (progn
+ (setq file
+ (make-temp-name
+ (expand-file-name prefix temporary-file-directory)))
+ (if suffix
+ (setq file (concat file suffix)))
+ (if dir-flag
+ (make-directory file)
+ (write-region "" nil file nil 'silent nil 'excl))
+ nil)
+ (file-already-exists t))
+ ;; the file was somehow created by someone else between
+ ;; `make-temp-name' and `write-region', let's try again.
+ nil)
+ file)
+ ;; Reset the umask.
+ (set-default-file-modes umask))))
+
(defun recode-file-name (file coding new-coding &optional ok-if-already-exists)
"Change the encoding of FILE's name from CODING to NEW-CODING.
The value is a new name of FILE.
(not (or buf nowarn))
(> (nth 7 attributes) large-file-warning-threshold)
(not (y-or-n-p
- (format "File %s is large (%sMB), really open? "
+ (format "File %s is large (%dMB), really open? "
(file-name-nondirectory filename)
(/ (nth 7 attributes) 1048576)))))
(error "Aborted"))
(hack-local-variables)))
;; Turn font lock off and on, to make sure it takes account of
;; whatever file local variables are relevant to it.
- (when (and font-lock-mode (eq (car font-lock-keywords) t))
+ (when (and font-lock-mode
+ ;; Font-lock-mode (now in font-core.el) can be ON when
+ ;; font-lock.el still hasn't been loaded.
+ (boundp 'font-lock-keywords)
+ (eq (car font-lock-keywords) t))
(setq font-lock-keywords (cadr font-lock-keywords))
(font-lock-mode 1))
("\\.ad[abs]\\'" . ada-mode)
("\\.ad[bs].dg\\'" . ada-mode)
("\\.\\([pP]\\([Llm]\\|erl\\|od\\)\\|al\\)\\'" . perl-mode)
+ ("Imakefile\\'" . makefile-imake-mode)
,@(if (memq system-type '(berkeley-unix next-mach darwin))
'(("\\.mk\\'" . makefile-bsdmake-mode)
("GNUmakefile\\'" . makefile-gmake-mode)
;; /tmp/Re.... or Message
("\\`/tmp/Re" . text-mode)
("/Message[0-9]*\\'" . text-mode)
- ("/drafts/[0-9]+\\'" . mh-letter-mode)
("\\.zone\\'" . zone-mode)
;; some news reader is reported to use this
("\\`/tmp/fol/" . text-mode)
If FUNCTION is nil, then it is not called. (That is a way of saying
\"allow `auto-mode-alist' to decide for these files.\")")
+(defvar magic-mode-regexp-match-limit 4000
+ "Upper limit on `magic-mode-alist' regexp matches.")
+
(defun set-auto-mode (&optional keep-mode-if-same)
"Select major mode appropriate for current buffer.
(setq done t)
(or (set-auto-mode-0 mode keep-mode-if-same)
;; continuing would call minor modes again, toggling them off
- (throw 'nop nil)))))
+ (throw 'nop nil))))))
+ (unless done
;; 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
;; finds the interpreter anywhere in $PATH.
(unless done
(if (setq done (save-excursion
(goto-char (point-min))
- (assoc-default nil magic-mode-alist
- (lambda (re dummy)
- (looking-at re)))))
+ (save-restriction
+ (narrow-to-region (point-min)
+ (min (point-max)
+ (+ (point-min) magic-mode-regexp-match-limit)))
+ (assoc-default nil magic-mode-alist
+ (lambda (re dummy)
+ (looking-at re))))))
(set-auto-mode-0 done keep-mode-if-same)
;; Compare the filename against the entries in auto-mode-alist.
(if buffer-file-name
"Non-nil if SYM could be dangerous as a file-local variable with value VAL.
If VAL is nil or omitted, the question is whether any value might be
dangerous."
+ ;; If this is an alias, check the base name.
+ (condition-case nil
+ (setq sym (indirect-variable sym))
+ (error nil))
(let ((safep (get sym 'safe-local-variable)))
(or (get sym 'risky-local-variable)
(and (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-commands?$\\|-predicates?$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$"
(defun set-visited-file-name (filename &optional no-query along-with-file)
"Change name of file visited in current buffer to FILENAME.
+This also renames the buffer to correspond to the new file.
The next time the buffer is saved it will go in the newly specified file.
-FILENAME nil or an empty string means make buffer not be visiting any file.
+FILENAME nil or an empty string means mark buffer as not visiting any file.
Remember to delete the initial contents of the minibuffer
if you wish to pass an empty string as the argument.
(and buffer-file-name
(file-writable-p buffer-file-name)
(setq buffer-read-only nil))
- (save-buffer))
+ (save-buffer)
+ ;; It's likely that the VC status at the new location is different from
+ ;; the one at the old location.
+ (vc-find-file-hook))
\f
(defun backup-buffer ()
"Make a backup of the disk file visited by the current buffer, if appropriate.
This function returns a relative file name which is equivalent to FILENAME
when used with that default directory as the default.
If FILENAME and DIRECTORY lie on different machines or on different drives
-on a DOS/Windows machine, it returns FILENAME on expanded form."
+on a DOS/Windows machine, it returns FILENAME in expanded form."
(save-match-data
(setq directory
(file-name-as-directory (expand-file-name (or directory
ancestor))))))
\f
(defun save-buffer (&optional args)
- "Save current buffer in visited file if modified. Variations are described below.
+ "Save current buffer in visited file if modified.
+Variations are described below.
+
By default, makes the previous version into a backup file
if previously requested or if this is the first save.
Prefixed with one \\[universal-argument], marks this version
;; If we get an error writing the new file, and we made
;; the backup by renaming, undo the backing-up.
(and setmodes (not success)
- (rename-file (cdr setmodes) buffer-file-name))))))
+ (progn
+ (rename-file (cdr setmodes) buffer-file-name t)
+ (setq buffer-backed-up nil)))))))
setmodes))
(defun diff-buffer-with-file (&optional buffer)
(t (setq buffer-read-only (not buffer-read-only))
(force-mode-line-update)))
(if (vc-backend buffer-file-name)
- (message (substitute-command-keys
+ (message "%s" (substitute-command-keys
(concat "File is under version-control; "
"use \\[vc-next-action] to check in/out"))))))
(forward-word -1)
(buffer-substring (point) end)))))))))
+;; The following expression replaces `dired-move-to-filename-regexp'.
+(defvar directory-listing-before-filename-regexp
+ (let* ((l "\\([A-Za-z]\\|[^\0-\177]\\)")
+ (l-or-quote "\\([A-Za-z']\\|[^\0-\177]\\)")
+ ;; In some locales, month abbreviations are as short as 2 letters,
+ ;; and they can be followed by ".".
+ ;; In Breton, a month name can include a quote character.
+ (month (concat l-or-quote l-or-quote "+\\.?"))
+ (s " ")
+ (yyyy "[0-9][0-9][0-9][0-9]")
+ (dd "[ 0-3][0-9]")
+ (HH:MM "[ 0-2][0-9][:.][0-5][0-9]")
+ (seconds "[0-6][0-9]\\([.,][0-9]+\\)?")
+ (zone "[-+][0-2][0-9][0-5][0-9]")
+ (iso-mm-dd "[01][0-9]-[0-3][0-9]")
+ (iso-time (concat HH:MM "\\(:" seconds "\\( ?" zone "\\)?\\)?"))
+ (iso (concat "\\(\\(" yyyy "-\\)?" iso-mm-dd "[ T]" iso-time
+ "\\|" yyyy "-" iso-mm-dd "\\)"))
+ (western (concat "\\(" month s "+" dd "\\|" dd "\\.?" s month "\\)"
+ s "+"
+ "\\(" HH:MM "\\|" yyyy "\\)"))
+ (western-comma (concat month s "+" dd "," s "+" yyyy))
+ ;; Japanese MS-Windows ls-lisp has one-digit months, and
+ ;; omits the Kanji characters after month and day-of-month.
+ ;; On Mac OS X 10.3, the date format in East Asian locales is
+ ;; day-of-month digits followed by month digits.
+ (mm "[ 0-1]?[0-9]")
+ (east-asian
+ (concat "\\(" mm l "?" s dd l "?" s "+"
+ "\\|" dd s mm s "+" "\\)"
+ "\\(" HH:MM "\\|" yyyy l "?" "\\)")))
+ ;; The "[0-9]" below requires the previous column to end in a digit.
+ ;; This avoids recognizing `1 may 1997' as a date in the line:
+ ;; -r--r--r-- 1 may 1997 1168 Oct 19 16:49 README
+
+ ;; The "[BkKMGTPEZY]?" below supports "ls -alh" output.
+ ;; The ".*" below finds the last match if there are multiple matches.
+ ;; This avoids recognizing `jservice 10 1024' as a date in the line:
+ ;; drwxr-xr-x 3 jservice 10 1024 Jul 2 1997 esg-host
+
+ ;; vc dired listings provide the state or blanks between file
+ ;; permissions and date. The state is always surrounded by
+ ;; parantheses:
+ ;; -rw-r--r-- (modified) 2005-10-22 21:25 files.el
+ ;; This is not supported yet.
+ (concat ".*[0-9][BkKMGTPEZY]?" s
+ "\\(" western "\\|" western-comma "\\|" east-asian "\\|" iso "\\)"
+ s "+"))
+ "Regular expression to match up to the file name in a directory listing.
+The default value is designed to recognize dates and times
+regardless of the language.")
(defvar insert-directory-ls-version 'unknown)