X-Git-Url: https://git.hcoop.net/bpt/emacs.git/blobdiff_plain/3f9549e874035d963eb5ca02f24d51dc935e6326..797c9e3df26496debbfb9bf506ad5049b503e98c:/lisp/files.el diff --git a/lisp/files.el b/lisp/files.el index ca5a415fb8..871a4b0548 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -206,7 +206,7 @@ have fast storage with limited space, such as a RAM disk." (declare-function msdos-long-file-names "msdos.c") (declare-function w32-long-file-name "w32proc.c") (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep)) -(declare-function dired-unmark "dired" (arg)) +(declare-function dired-unmark "dired" (arg &optional interactive)) (declare-function dired-do-flagged-delete "dired" (&optional nomessage)) (declare-function dos-8+3-filename "dos-fns" (filename)) (declare-function dosified-file-name "dos-fns" (file-name)) @@ -507,6 +507,11 @@ and ignores this variable." (other :tag "Query" other)) :group 'find-file) +(defvar enable-dir-local-variables t + "Non-nil means enable use of directory-local variables. +Some modes may wish to set this to nil to prevent directory-local +settings being applied, but still respect file-local ones.") + ;; This is an odd variable IMO. ;; You might wonder why it is needed, when we could just do: ;; (set (make-local-variable 'enable-local-variables) nil) @@ -1511,7 +1516,10 @@ expand wildcards (if any) and replace the file with multiple files." (defvar kill-buffer-hook nil "Hook run when a buffer is killed. The buffer being killed is current while the hook is running. -See `kill-buffer'.") +See `kill-buffer'. + +Note: Be careful with let-binding this hook considering it is +frequently used for cleanup.") (defun find-alternate-file (filename &optional wildcards) "Find file FILENAME, select its buffer, kill previous buffer. @@ -1978,13 +1986,10 @@ Do you want to revisit the file normally now? ") (set-buffer-multibyte nil) (setq buffer-file-coding-system 'no-conversion) (set-buffer-major-mode buf) - (make-local-variable 'find-file-literally) - (setq find-file-literally t)) + (setq-local find-file-literally t)) (after-find-file error (not nowarn))) (current-buffer)))) -(defvar file-name-buffer-file-type-alist) ;From dos-w32.el. - (defun insert-file-contents-literally (filename &optional visit beg end replace) "Like `insert-file-contents', but only reads in the file literally. A buffer may be modified in several ways after reading into the buffer, @@ -1996,7 +2001,6 @@ This function ensures that none of these modifications will take place." (after-insert-file-functions nil) (coding-system-for-read 'no-conversion) (coding-system-for-write 'no-conversion) - (file-name-buffer-file-type-alist '(("" . t))) (inhibit-file-name-handlers ;; FIXME: Yuck!! We should turn insert-file-contents-literally ;; into a file operation instead! @@ -2170,7 +2174,7 @@ not set local variables (though we do notice a mode specified with -*-.) or from Lisp without specifying the optional argument FIND-FILE; in that case, this function acts as if `enable-local-variables' were t." (interactive) - (funcall (or (default-value 'major-mode) 'fundamental-mode)) + (fundamental-mode) (let ((enable-local-variables (or (not find-file) enable-local-variables))) ;; FIXME this is less efficient than it could be, since both ;; s-a-m and h-l-v may parse the same regions, looking for "mode:". @@ -2308,7 +2312,7 @@ since only a single case-insensitive search through the alist is made." ("\\.\\(\ arc\\|zip\\|lzh\\|lha\\|zoo\\|[jew]ar\\|xpi\\|rar\\|7z\\|\ ARC\\|ZIP\\|LZH\\|LHA\\|ZOO\\|[JEW]AR\\|XPI\\|RAR\\|7Z\\)\\'" . archive-mode) - ("\\.\\(sx[dmicw]\\|od[fgpst]\\|oxt\\)\\'" . archive-mode) ;OpenOffice.org + ("\\.oxt\\'" . archive-mode) ;(Open|Libre)Office extensions. ("\\.\\(deb\\|[oi]pk\\)\\'" . archive-mode) ; Debian/Opkg packages. ;; Mailer puts message to be edited in ;; /tmp/Re.... or Message @@ -2357,7 +2361,7 @@ ARC\\|ZIP\\|LZH\\|LHA\\|ZOO\\|[JEW]AR\\|XPI\\|RAR\\|7Z\\)\\'" . archive-mode) ("\\.\\(diffs?\\|patch\\|rej\\)\\'" . diff-mode) ("\\.\\(dif\\|pat\\)\\'" . diff-mode) ; for MSDOG ("\\.[eE]?[pP][sS]\\'" . ps-mode) - ("\\.\\(?:PDF\\|DVI\\|OD[FGPST]\\|DOCX?\\|XLSX?\\|PPTX?\\|pdf\\|dvi\\|od[fgpst]\\|docx?\\|xlsx?\\|pptx?\\)\\'" . doc-view-mode-maybe) + ("\\.\\(?:PDF\\|DVI\\|OD[FGPST]\\|DOCX?\\|XLSX?\\|PPTX?\\|pdf\\|djvu\\|dvi\\|od[fgpst]\\|docx?\\|xlsx?\\|pptx?\\)\\'" . doc-view-mode-maybe) ("configure\\.\\(ac\\|in\\)\\'" . autoconf-mode) ("\\.s\\(v\\|iv\\|ieve\\)\\'" . sieve-mode) ("BROWSE\\'" . ebrowse-tree-mode) @@ -2496,6 +2500,7 @@ See also `auto-mode-alist'.") "\\.zoo\\'" "\\.[jew]ar\\'" "\\.xpi\\'" "\\.rar\\'" "\\.7z\\'" "\\.sx[dmicw]\\'" "\\.odt\\'" + "\\.diff\\'" "\\.patch\\'" "\\.tiff?\\'" "\\.gif\\'" "\\.png\\'" "\\.jpe?g\\'")) "List of regexps matching file names in which to ignore local variables. This includes `-*-' lines as well as trailing \"Local Variables\" sections. @@ -2753,7 +2758,9 @@ we don't actually set it to the same mode the buffer already has." (if (functionp re) (funcall re) (looking-at re))))))) - (set-auto-mode-0 done keep-mode-if-same))))) + (set-auto-mode-0 done keep-mode-if-same))) + (unless done + (set-buffer-major-mode (current-buffer))))) ;; 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 @@ -3023,6 +3030,9 @@ n -- to ignore the local variables list.") (prog1 (memq char '(?! ?\s ?y)) (quit-window t))))))) +(defconst hack-local-variable-regexp + "[ \t]*\\([^][;\"'?()\\ \t\n]+\\)[ \t]*:[ \t]*") + (defun hack-local-variables-prop-line (&optional mode-only) "Return local variables specified in the -*- line. Returns an alist of elements (VAR . VAL), where VAR is a variable @@ -3049,11 +3059,11 @@ mode, if there is one, otherwise nil." ;; (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") + (while (not (or (and mode-only result) + (>= (point) end))) + (unless (looking-at hack-local-variable-regexp) + (message "Malformed mode-line: %S" + (buffer-substring-no-properties (point) end)) (throw 'malformed-line nil)) (goto-char (match-end 0)) ;; There used to be a downcase here, @@ -3205,8 +3215,7 @@ local variables, but directory-local variables may still be applied." (prefix (concat "^" (regexp-quote (buffer-substring (line-beginning-position) - (match-beginning 0))))) - beg) + (match-beginning 0)))))) (forward-line 1) (let ((startpos (point)) @@ -3241,18 +3250,16 @@ local variables, but directory-local variables may still be applied." (forward-line 1)) (goto-char (point-min)) - (while (and (not (eobp)) - (or (not mode-only) - (not result))) - ;; Find the variable name; strip whitespace. - (skip-chars-forward " \t") - (setq beg (point)) - (skip-chars-forward "^:\n") - (if (eolp) (error "Missing colon in local variables entry")) - (skip-chars-backward " \t") - (let* ((str (buffer-substring beg (point))) - (var (let ((read-circle nil)) - (read str))) + (while (not (or (eobp) + (and mode-only result))) + ;; Find the variable name; + (unless (looking-at hack-local-variable-regexp) + (error "Malformed local variable line: %S" + (buffer-substring-no-properties + (point) (line-end-position)))) + (goto-char (match-end 1)) + (let* ((str (match-string 1)) + (var (intern str)) val val2) (and (equal (downcase (symbol-name var)) "mode") (setq var 'mode)) @@ -3662,8 +3669,12 @@ is found. Returns the new class name." (defun hack-dir-local-variables () "Read per-directory local variables for the current buffer. Store the directory-local variables in `dir-local-variables-alist' -and `file-local-variables-alist', without applying them." +and `file-local-variables-alist', without applying them. + +This does nothing if either `enable-local-variables' or +`enable-dir-local-variables' are nil." (when (and enable-local-variables + enable-dir-local-variables (or enable-remote-dir-locals (not (file-remote-p (or (buffer-file-name) default-directory))))) @@ -4170,23 +4181,31 @@ ignored." "Default `backup-enable-predicate' function. Checks for files in `temporary-file-directory', `small-temporary-file-directory', and /tmp." - (not (or (let ((comp (compare-strings temporary-file-directory 0 nil - name 0 nil))) - ;; Directory is under temporary-file-directory. - (and (not (eq comp t)) - (< comp (- (length temporary-file-directory))))) - (let ((comp (compare-strings "/tmp" 0 nil - name 0 nil))) - ;; Directory is under /tmp. - (and (not (eq comp t)) - (< comp (- (length "/tmp"))))) - (if small-temporary-file-directory - (let ((comp (compare-strings small-temporary-file-directory - 0 nil - name 0 nil))) - ;; Directory is under small-temporary-file-directory. - (and (not (eq comp t)) - (< comp (- (length small-temporary-file-directory))))))))) + (let ((temporary-file-directory temporary-file-directory) + caseless) + ;; On MS-Windows, file-truename will convert short 8+3 aliases to + ;; their long file-name equivalents, so compare-strings does TRT. + (if (memq system-type '(ms-dos windows-nt)) + (setq temporary-file-directory (file-truename temporary-file-directory) + name (file-truename name) + caseless t)) + (not (or (let ((comp (compare-strings temporary-file-directory 0 nil + name 0 nil caseless))) + ;; Directory is under temporary-file-directory. + (and (not (eq comp t)) + (< comp (- (length temporary-file-directory))))) + (let ((comp (compare-strings "/tmp" 0 nil + name 0 nil))) + ;; Directory is under /tmp. + (and (not (eq comp t)) + (< comp (- (length "/tmp"))))) + (if small-temporary-file-directory + (let ((comp (compare-strings small-temporary-file-directory + 0 nil + name 0 nil caseless))) + ;; Directory is under small-temporary-file-directory. + (and (not (eq comp t)) + (< comp (- (length small-temporary-file-directory)))))))))) (defun make-backup-file-name (file) "Create the non-numeric backup file name for FILE. @@ -4557,28 +4576,21 @@ Before and after saving the buffer, this function runs (not (file-exists-p buffer-file-name)))) (let ((recent-save (recent-auto-save-p)) setmodes) - ;; If buffer has no file name, ask user for one. + ;; If buffer has no file name, ask user for one. (or buffer-file-name - (let ((filename - (expand-file-name - (read-file-name "File to save in: " - nil (expand-file-name (buffer-name)))))) - (if (file-exists-p filename) - (if (file-directory-p filename) - ;; Signal an error if the user specified the name of an - ;; existing directory. - (error "%s is a directory" filename) - (unless (y-or-n-p (format "File `%s' exists; overwrite? " - filename)) - (error "Canceled"))) - ;; Signal an error if the specified name refers to a - ;; non-existing directory. - (let ((dir (file-name-directory filename))) - (unless (file-directory-p dir) - (if (file-exists-p dir) - (error "%s is not a directory" dir) - (error "%s: no such directory" dir))))) - (set-visited-file-name filename))) + (let ((filename + (expand-file-name + (read-file-name "File to save in: " + nil (expand-file-name (buffer-name)))))) + (if (file-exists-p filename) + (if (file-directory-p filename) + ;; Signal an error if the user specified the name of an + ;; existing directory. + (error "%s is a directory" filename) + (unless (y-or-n-p (format "File `%s' exists; overwrite? " + filename)) + (error "Canceled")))) + (set-visited-file-name filename))) (or (verify-visited-file-modtime (current-buffer)) (not (file-exists-p buffer-file-name)) (yes-or-no-p @@ -4605,13 +4617,21 @@ Before and after saving the buffer, this function runs (insert ?\n)))) ;; Support VC version backups. (vc-before-save) - (run-hooks 'before-save-hook) + ;; Don't let errors prevent saving the buffer. + (with-demoted-errors (run-hooks 'before-save-hook)) (or (run-hook-with-args-until-success 'write-contents-functions) (run-hook-with-args-until-success 'local-write-file-hooks) (run-hook-with-args-until-success 'write-file-functions) ;; If a hook returned t, file is already "written". ;; Otherwise, write it the usual way now. - (setq setmodes (basic-save-buffer-1))) + (let ((dir (file-name-directory + (expand-file-name buffer-file-name)))) + (unless (file-exists-p dir) + (if (y-or-n-p + (format "Directory `%s' does not exist; create? " dir)) + (make-directory dir t) + (error "Canceled"))) + (setq setmodes (basic-save-buffer-1)))) ;; Now we have saved the current buffer. Let's make sure ;; that buffer-file-coding-system is fixed to what ;; actually used for saving by binding it locally. @@ -4649,14 +4669,12 @@ Before and after saving the buffer, this function runs (basic-save-buffer-2)) (basic-save-buffer-2)) (if buffer-file-coding-system-explicit - (setcar buffer-file-coding-system-explicit last-coding-system-used) - (setq buffer-file-coding-system-explicit - (cons last-coding-system-used nil))))) + (setcar buffer-file-coding-system-explicit last-coding-system-used)))) ;; This returns a value (MODES EXTENDED-ATTRIBUTES BACKUPNAME), like ;; backup-buffer. (defun basic-save-buffer-2 () - (let (tempsetmodes setmodes) + (let (tempsetmodes setmodes writecoding) (if (not (file-writable-p buffer-file-name)) (let ((dir (file-name-directory buffer-file-name))) (if (not (file-directory-p dir)) @@ -4672,6 +4690,14 @@ Before and after saving the buffer, this function runs buffer-file-name))) (setq tempsetmodes t) (error "Attempt to save to a file which you aren't allowed to write")))))) + ;; This may involve prompting, so do it now before backing up the file. + ;; Otherwise there can be a delay while the user answers the + ;; prompt during which the original file has been renamed. (Bug#13522) + (setq writecoding + ;; Args here should match write-region call below around + ;; which we use writecoding. + (choose-write-coding-system nil nil buffer-file-name nil t + buffer-file-truename)) (or buffer-backed-up (setq setmodes (backup-buffer))) (let* ((dir (file-name-directory buffer-file-name)) @@ -4753,10 +4779,11 @@ Before and after saving the buffer, this function runs (logior (car setmodes) 128)))))) (let (success) (unwind-protect - (progn ;; Pass in nil&nil rather than point-min&max to indicate ;; we're saving the buffer rather than just a region. ;; write-region-annotate-functions may make us of it. + (let ((coding-system-for-write writecoding) + (coding-system-require-warning nil)) (write-region nil nil buffer-file-name nil t buffer-file-truename) (setq success t))