215 |
"[\000-\031]\\|" ; control characters |
"[\000-\031]\\|" ; control characters |
216 |
"\\(/\\.\\.?[^/]\\)\\|" ; leading dots |
"\\(/\\.\\.?[^/]\\)\\|" ; leading dots |
217 |
"\\(/[^/.]+\\.[^/.]*\\.\\)")) ; more than a single dot |
"\\(/[^/.]+\\.[^/.]*\\.\\)")) ; more than a single dot |
218 |
((memq system-type '(ms-dos windows-nt)) |
((memq system-type '(ms-dos windows-nt cygwin)) |
219 |
(concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive |
(concat "^\\([^A-Z[-`a-z]\\|..+\\)?:\\|" ; colon except after drive |
220 |
"[|<>\"?*\000-\031]")) ; invalid characters |
"[|<>\"?*\000-\031]")) ; invalid characters |
221 |
(t "[\000]")) |
(t "[\000]")) |
344 |
|
|
345 |
;;;It is not useful to make this a local variable. |
;;;It is not useful to make this a local variable. |
346 |
;;;(put 'find-file-not-found-hooks 'permanent-local t) |
;;;(put 'find-file-not-found-hooks 'permanent-local t) |
347 |
(defvar find-file-not-found-hooks nil |
(defvar find-file-not-found-functions nil |
348 |
"List of functions to be called for `find-file' on nonexistent file. |
"List of functions to be called for `find-file' on nonexistent file. |
349 |
These functions are called as soon as the error is detected. |
These functions are called as soon as the error is detected. |
350 |
Variable `buffer-file-name' is already set up. |
Variable `buffer-file-name' is already set up. |
351 |
The functions are called in the order given until one of them returns non-nil.") |
The functions are called in the order given until one of them returns non-nil.") |
352 |
|
(defvaralias 'find-file-not-found-hooks 'find-file-not-found-functions) |
353 |
|
(make-obsolete-variable |
354 |
|
'find-file-not-found-hooks 'find-file-not-found-functions "21.4") |
355 |
|
|
356 |
;;;It is not useful to make this a local variable. |
;;;It is not useful to make this a local variable. |
357 |
;;;(put 'find-file-hooks 'permanent-local t) |
;;;(put 'find-file-hooks 'permanent-local t) |
358 |
(defvar find-file-hooks nil |
(defvar find-file-hook nil |
359 |
"List of functions to be called after a buffer is loaded from a file. |
"List of functions to be called after a buffer is loaded from a file. |
360 |
The buffer's local variables (if any) will have been processed before the |
The buffer's local variables (if any) will have been processed before the |
361 |
functions are called.") |
functions are called.") |
362 |
|
(defvaralias 'find-file-hooks 'find-file-hook) |
363 |
|
(make-obsolete-variable 'find-file-hooks 'find-file-hook "21.4") |
364 |
|
|
365 |
(defvar write-file-hooks nil |
(defvar write-file-functions nil |
366 |
"List of functions to be called before writing out a buffer to a file. |
"List of functions to be called before writing out a buffer to a file. |
367 |
If one of them returns non-nil, the file is considered already written |
If one of them returns non-nil, the file is considered already written |
368 |
and the rest are not called. |
and the rest are not called. |
369 |
These hooks are considered to pertain to the visited file. |
These hooks are considered to pertain to the visited file. |
370 |
So any buffer-local binding of `write-file-hooks' is |
So any buffer-local binding of this variable is discarded if you change |
371 |
discarded if you change the visited file name with \\[set-visited-file-name]. |
the visited file name with \\[set-visited-file-name], but not when you |
372 |
|
change the major mode. |
373 |
|
|
374 |
|
See also `write-contents-functions'.") |
375 |
|
(put 'write-file-functions 'permanent-local t) |
376 |
|
(defvaralias 'write-file-hooks 'write-file-functions) |
377 |
|
(make-obsolete-variable 'write-file-hooks 'write-file-functions "21.4") |
378 |
|
|
379 |
Don't make this variable buffer-local; instead, use `local-write-file-hooks'. |
(defvar local-write-file-hooks nil) |
|
See also `write-contents-hooks'.") |
|
|
;;; However, in case someone does make it local... |
|
|
(put 'write-file-hooks 'permanent-local t) |
|
|
|
|
|
(defvar local-write-file-hooks nil |
|
|
"Just like `write-file-hooks', except intended for per-buffer use. |
|
|
The functions in this list are called before the ones in |
|
|
`write-file-hooks'. |
|
|
|
|
|
This variable is meant to be used for hooks that have to do with a |
|
|
particular visited file. Therefore, it is a permanent local, so that |
|
|
changing the major mode does not clear it. However, calling |
|
|
`set-visited-file-name' does clear it.") |
|
380 |
(make-variable-buffer-local 'local-write-file-hooks) |
(make-variable-buffer-local 'local-write-file-hooks) |
381 |
(put 'local-write-file-hooks 'permanent-local t) |
(put 'local-write-file-hooks 'permanent-local t) |
382 |
|
(make-obsolete-variable 'local-write-file-hooks 'write-file-functions "21.4") |
383 |
|
|
384 |
(defvar write-contents-hooks nil |
(defvar write-contents-functions nil |
385 |
"List of functions to be called before writing out a buffer to a file. |
"List of functions to be called before writing out a buffer to a file. |
386 |
If one of them returns non-nil, the file is considered already written |
If one of them returns non-nil, the file is considered already written |
387 |
and the rest are not called. |
and the rest are not called. |
391 |
`set-visited-file-name' does not clear this variable; but changing the |
`set-visited-file-name' does not clear this variable; but changing the |
392 |
major mode does clear it. |
major mode does clear it. |
393 |
|
|
394 |
This variable automatically becomes buffer-local whenever it is set. |
See also `write-file-functions'.") |
395 |
If you use `add-hook' to add elements to the list, use nil for the |
(make-variable-buffer-local 'write-contents-functions) |
396 |
LOCAL argument. |
(defvaralias 'write-contents-hooks 'write-contents-functions) |
397 |
|
(make-obsolete-variable 'write-contents-hooks 'write-contents-functions "21.4") |
|
See also `write-file-hooks'.") |
|
|
(make-variable-buffer-local 'write-contents-hooks) |
|
398 |
|
|
399 |
(defcustom enable-local-variables t |
(defcustom enable-local-variables t |
400 |
"*Control use of local variables in files you visit. |
"*Control use of local variables in files you visit. |
445 |
(defvar view-read-only nil |
(defvar view-read-only nil |
446 |
"*Non-nil means buffers visiting files read-only, do it in view mode.") |
"*Non-nil means buffers visiting files read-only, do it in view mode.") |
447 |
|
|
448 |
|
(put 'ange-ftp-completion-hook-function 'safe-magic t) |
449 |
(defun ange-ftp-completion-hook-function (op &rest args) |
(defun ange-ftp-completion-hook-function (op &rest args) |
450 |
"Provides support for ange-ftp host name completion. |
"Provides support for ange-ftp host name completion. |
451 |
Runs the usual ange-ftp hook, but only for completion operations." |
Runs the usual ange-ftp hook, but only for completion operations." |
600 |
(when (string-match suffix file) |
(when (string-match suffix file) |
601 |
(setq file (substring file 0 (match-beginning 0))) |
(setq file (substring file 0 (match-beginning 0))) |
602 |
(push (if string-dir (concat string-dir file) file) names))))) |
(push (if string-dir (concat string-dir file) file) names))))) |
603 |
(if action |
(cond |
604 |
(all-completions string (mapcar 'list names)) |
((eq action t) (all-completions string names)) |
605 |
(try-completion string (mapcar 'list names)))))) |
((null action) (try-completion string names)) |
606 |
|
(t (test-completion string names)))))) |
607 |
|
|
608 |
(defun load-library (library) |
(defun load-library (library) |
609 |
"Load the library named LIBRARY. |
"Load the library named LIBRARY. |
614 |
(cons load-path load-suffixes)))) |
(cons load-path load-suffixes)))) |
615 |
(load library)) |
(load library)) |
616 |
|
|
617 |
|
(defun file-remote-p (file) |
618 |
|
"Test whether FILE specifies a location on a remote system." |
619 |
|
(let ((handler (find-file-name-handler file 'file-local-copy))) |
620 |
|
(if handler |
621 |
|
(get handler 'file-remote-p)))) |
622 |
|
|
623 |
(defun file-local-copy (file) |
(defun file-local-copy (file) |
624 |
"Copy the file FILE into a temporary file on this machine. |
"Copy the file FILE into a temporary file on this machine. |
625 |
Returns the name of the local copy, or nil, if FILE is directly |
Returns the name of the local copy, or nil, if FILE is directly |
675 |
;; it is stored on disk (expanding short name aliases with the full |
;; it is stored on disk (expanding short name aliases with the full |
676 |
;; name in the process). |
;; name in the process). |
677 |
(if (eq system-type 'windows-nt) |
(if (eq system-type 'windows-nt) |
678 |
(let ((handler (find-file-name-handler filename 'file-truename)) |
(let ((handler (find-file-name-handler filename 'file-truename))) |
|
newname) |
|
679 |
;; For file name that has a special handler, call handler. |
;; For file name that has a special handler, call handler. |
680 |
;; This is so that ange-ftp can save time by doing a no-op. |
;; This is so that ange-ftp can save time by doing a no-op. |
681 |
(if handler |
(if handler |
682 |
(setq filename (funcall handler 'file-truename filename)) |
(setq filename (funcall handler 'file-truename filename)) |
683 |
;; If filename contains a wildcard, newname will be the old name. |
;; If filename contains a wildcard, newname will be the old name. |
684 |
(if (string-match "[[*?]" filename) |
(unless (string-match "[[*?]" filename) |
685 |
(setq newname filename) |
;; If filename exists, use the long name |
686 |
;; If filename doesn't exist, newname will be nil. |
(setq filename (or (w32-long-file-name filename) filename)))) |
|
(setq newname (w32-long-file-name filename))) |
|
|
(setq filename (or newname filename))) |
|
687 |
(setq done t))) |
(setq done t))) |
688 |
|
|
689 |
;; If this file directly leads to a link, process that iteratively |
;; If this file directly leads to a link, process that iteratively |
766 |
(setq newname (expand-file-name tem (file-name-directory newname))) |
(setq newname (expand-file-name tem (file-name-directory newname))) |
767 |
(setq count (1- count)))) |
(setq count (1- count)))) |
768 |
newname)) |
newname)) |
769 |
|
|
770 |
|
(defun recode-file-name (file coding new-coding &optional ok-if-already-exists) |
771 |
|
"Change the encoding of FILE's name from CODING to NEW-CODING. |
772 |
|
The value is a new name of FILE. |
773 |
|
Signals a `file-already-exists' error if a file of the new name |
774 |
|
already exists unless optional third argument OK-IF-ALREADY-EXISTS |
775 |
|
is non-nil. A number as third arg means request confirmation if |
776 |
|
the new name already exists. This is what happens in interactive |
777 |
|
use with M-x." |
778 |
|
(interactive |
779 |
|
(let ((default-coding (or file-name-coding-system |
780 |
|
default-file-name-coding-system)) |
781 |
|
(filename (read-file-name "Recode filename: " nil nil t)) |
782 |
|
from-coding to-coding) |
783 |
|
(if (and default-coding |
784 |
|
;; We provide the default coding only when it seems that |
785 |
|
;; the filename is correctly decoded by the default |
786 |
|
;; coding. |
787 |
|
(let ((charsets (find-charset-string filename))) |
788 |
|
(and (not (memq 'eight-bit-control charsets)) |
789 |
|
(not (memq 'eight-bit-graphic charsets))))) |
790 |
|
(setq from-coding (read-coding-system |
791 |
|
(format "Recode filename %s from (default %s): " |
792 |
|
filename default-coding) |
793 |
|
default-coding)) |
794 |
|
(setq from-coding (read-coding-system |
795 |
|
(format "Recode filename %s from: " filename)))) |
796 |
|
|
797 |
|
;; We provide the default coding only when a user is going to |
798 |
|
;; change the encoding not from the default coding. |
799 |
|
(if (eq from-coding default-coding) |
800 |
|
(setq to-coding (read-coding-system |
801 |
|
(format "Recode filename %s from %s to: " |
802 |
|
filename from-coding))) |
803 |
|
(setq to-coding (read-coding-system |
804 |
|
(format "Recode filename %s from %s to (default %s): " |
805 |
|
filename from-coding default-coding) |
806 |
|
default-coding))) |
807 |
|
(list filename from-coding to-coding))) |
808 |
|
|
809 |
|
(let* ((default-coding (or file-name-coding-system |
810 |
|
default-file-name-coding-system)) |
811 |
|
;; FILE should have been decoded by DEFAULT-CODING. |
812 |
|
(encoded (encode-coding-string file default-coding)) |
813 |
|
(newname (decode-coding-string encoded coding)) |
814 |
|
(new-encoded (encode-coding-string newname new-coding)) |
815 |
|
;; Suppress further encoding. |
816 |
|
(file-name-coding-system nil) |
817 |
|
(default-file-name-coding-system nil) |
818 |
|
(locale-coding-system nil)) |
819 |
|
(rename-file encoded new-encoded ok-if-already-exists) |
820 |
|
newname)) |
821 |
|
|
822 |
(defun switch-to-buffer-other-window (buffer &optional norecord) |
(defun switch-to-buffer-other-window (buffer &optional norecord) |
823 |
"Select buffer BUFFER in another window. |
"Select buffer BUFFER in another window. |
845 |
(defvar find-file-default nil |
(defvar find-file-default nil |
846 |
"Used within `find-file-read-args'.") |
"Used within `find-file-read-args'.") |
847 |
|
|
848 |
(defun find-file-read-args (prompt) |
(defun find-file-read-args (prompt mustmatch) |
849 |
(list (let ((find-file-default |
(list (let ((find-file-default |
850 |
(and buffer-file-name |
(and buffer-file-name |
851 |
(abbreviate-file-name buffer-file-name))) |
(abbreviate-file-name buffer-file-name))) |
858 |
(minibuffer-setup-hook |
(minibuffer-setup-hook |
859 |
minibuffer-setup-hook)) |
minibuffer-setup-hook)) |
860 |
(add-hook 'minibuffer-setup-hook munge-default-fun) |
(add-hook 'minibuffer-setup-hook munge-default-fun) |
861 |
(read-file-name prompt nil default-directory)) |
(read-file-name prompt nil default-directory mustmatch)) |
862 |
current-prefix-arg)) |
t)) |
863 |
|
|
864 |
(defun find-file (filename &optional wildcards) |
(defun find-file (filename &optional wildcards) |
865 |
"Edit file FILENAME. |
"Edit file FILENAME. |
873 |
expand wildcards (if any) and visit multiple files. Wildcard expansion |
expand wildcards (if any) and visit multiple files. Wildcard expansion |
874 |
can be suppressed by setting `find-file-wildcards'." |
can be suppressed by setting `find-file-wildcards'." |
875 |
(interactive |
(interactive |
876 |
(find-file-read-args "Find file: ")) |
(find-file-read-args "Find file: " nil)) |
877 |
(let ((value (find-file-noselect filename nil nil wildcards))) |
(let ((value (find-file-noselect filename nil nil wildcards))) |
878 |
(if (listp value) |
(if (listp value) |
879 |
(mapcar 'switch-to-buffer (nreverse value)) |
(mapcar 'switch-to-buffer (nreverse value)) |
890 |
|
|
891 |
Interactively, or if WILDCARDS is non-nil in a call from Lisp, |
Interactively, or if WILDCARDS is non-nil in a call from Lisp, |
892 |
expand wildcards (if any) and visit multiple files." |
expand wildcards (if any) and visit multiple files." |
893 |
(interactive (find-file-read-args "FFind file in other window: ")) |
(interactive (find-file-read-args "Find file in other window: " nil)) |
894 |
(let ((value (find-file-noselect filename nil nil wildcards))) |
(let ((value (find-file-noselect filename nil nil wildcards))) |
895 |
(if (listp value) |
(if (listp value) |
896 |
(progn |
(progn |
910 |
|
|
911 |
Interactively, or if WILDCARDS is non-nil in a call from Lisp, |
Interactively, or if WILDCARDS is non-nil in a call from Lisp, |
912 |
expand wildcards (if any) and visit multiple files." |
expand wildcards (if any) and visit multiple files." |
913 |
(interactive (find-file-read-args "FFind file in other frame: ")) |
(interactive (find-file-read-args "Find file in other frame: " nil)) |
914 |
(let ((value (find-file-noselect filename nil nil wildcards))) |
(let ((value (find-file-noselect filename nil nil wildcards))) |
915 |
(if (listp value) |
(if (listp value) |
916 |
(progn |
(progn |
923 |
"Edit file FILENAME but don't allow changes. |
"Edit file FILENAME but don't allow changes. |
924 |
Like \\[find-file] but marks buffer as read-only. |
Like \\[find-file] but marks buffer as read-only. |
925 |
Use \\[toggle-read-only] to permit editing." |
Use \\[toggle-read-only] to permit editing." |
926 |
(interactive (find-file-read-args "fFind file read-only: ")) |
(interactive (find-file-read-args "Find file read-only: " t)) |
927 |
(find-file filename wildcards) |
(find-file filename wildcards) |
928 |
(toggle-read-only 1) |
(toggle-read-only 1) |
929 |
(current-buffer)) |
(current-buffer)) |
932 |
"Edit file FILENAME in another window but don't allow changes. |
"Edit file FILENAME in another window but don't allow changes. |
933 |
Like \\[find-file-other-window] but marks buffer as read-only. |
Like \\[find-file-other-window] but marks buffer as read-only. |
934 |
Use \\[toggle-read-only] to permit editing." |
Use \\[toggle-read-only] to permit editing." |
935 |
(interactive (find-file-read-args "fFind file read-only other window: ")) |
(interactive (find-file-read-args "Find file read-only other window: " t)) |
936 |
(find-file-other-window filename wildcards) |
(find-file-other-window filename wildcards) |
937 |
(toggle-read-only 1) |
(toggle-read-only 1) |
938 |
(current-buffer)) |
(current-buffer)) |
941 |
"Edit file FILENAME in another frame but don't allow changes. |
"Edit file FILENAME in another frame but don't allow changes. |
942 |
Like \\[find-file-other-frame] but marks buffer as read-only. |
Like \\[find-file-other-frame] but marks buffer as read-only. |
943 |
Use \\[toggle-read-only] to permit editing." |
Use \\[toggle-read-only] to permit editing." |
944 |
(interactive (find-file-read-args "fFind file read-only other frame: ")) |
(interactive (find-file-read-args "Find file read-only other frame: " t)) |
945 |
(find-file-other-frame filename wildcards) |
(find-file-other-frame filename wildcards) |
946 |
(toggle-read-only 1) |
(toggle-read-only 1) |
947 |
(current-buffer)) |
(current-buffer)) |
979 |
file-dir (file-name-directory file))) |
file-dir (file-name-directory file))) |
980 |
(list (read-file-name |
(list (read-file-name |
981 |
"Find alternate file: " file-dir nil nil file-name)))) |
"Find alternate file: " file-dir nil nil file-name)))) |
982 |
(and (buffer-modified-p) (buffer-file-name) |
(unless (run-hook-with-args-until-failure 'kill-buffer-query-functions) |
983 |
;; (not buffer-read-only) |
(error "Aborted")) |
984 |
(not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " |
(when (and (buffer-modified-p) (buffer-file-name)) |
985 |
(buffer-name)))) |
(if (yes-or-no-p (format "Buffer %s is modified; save it first? " |
986 |
(error "Aborted")) |
(buffer-name))) |
987 |
|
(save-buffer) |
988 |
|
(unless (yes-or-no-p "Kill and replace the buffer without saving it? ") |
989 |
|
(error "Aborted")))) |
990 |
(let ((obuf (current-buffer)) |
(let ((obuf (current-buffer)) |
991 |
(ofile buffer-file-name) |
(ofile buffer-file-name) |
992 |
(onum buffer-file-number) |
(onum buffer-file-number) |
993 |
|
(odir dired-directory) |
994 |
(otrue buffer-file-truename) |
(otrue buffer-file-truename) |
995 |
(oname (buffer-name))) |
(oname (buffer-name))) |
996 |
(if (get-buffer " **lose**") |
(if (get-buffer " **lose**") |
999 |
(unwind-protect |
(unwind-protect |
1000 |
(progn |
(progn |
1001 |
(unlock-buffer) |
(unlock-buffer) |
1002 |
|
;; This prevents us from finding the same buffer |
1003 |
|
;; if we specified the same file again. |
1004 |
(setq buffer-file-name nil) |
(setq buffer-file-name nil) |
1005 |
(setq buffer-file-number nil) |
(setq buffer-file-number nil) |
1006 |
(setq buffer-file-truename nil) |
(setq buffer-file-truename nil) |
1007 |
|
;; Likewise for dired buffers. |
1008 |
|
(setq dired-directory nil) |
1009 |
(find-file filename)) |
(find-file filename)) |
1010 |
(cond ((eq obuf (current-buffer)) |
(when (eq obuf (current-buffer)) |
1011 |
(setq buffer-file-name ofile) |
;; This executes if find-file gets an error |
1012 |
(setq buffer-file-number onum) |
;; and does not really find anything. |
1013 |
(setq buffer-file-truename otrue) |
;; We put things back as they were. |
1014 |
(lock-buffer) |
;; If find-file actually finds something, we kill obuf below. |
1015 |
(rename-buffer oname)))) |
(setq buffer-file-name ofile) |
1016 |
(or (eq (current-buffer) obuf) |
(setq buffer-file-number onum) |
1017 |
(kill-buffer obuf)))) |
(setq buffer-file-truename otrue) |
1018 |
|
(setq dired-directory odir) |
1019 |
|
(lock-buffer) |
1020 |
|
(rename-buffer oname))) |
1021 |
|
(unless (eq (current-buffer) obuf) |
1022 |
|
(with-current-buffer obuf |
1023 |
|
;; We already asked; don't ask again. |
1024 |
|
(let ((kill-buffer-query-functions)) |
1025 |
|
(kill-buffer obuf)))))) |
1026 |
|
|
1027 |
(defun create-file-buffer (filename) |
(defun create-file-buffer (filename) |
1028 |
"Create a suitably named buffer for visiting FILENAME, and return it. |
"Create a suitably named buffer for visiting FILENAME, and return it. |
1048 |
|
|
1049 |
(defun abbreviate-file-name (filename) |
(defun abbreviate-file-name (filename) |
1050 |
"Return a version of FILENAME shortened using `directory-abbrev-alist'. |
"Return a version of FILENAME shortened using `directory-abbrev-alist'. |
1051 |
This also substitutes \"~\" for the user's home directory. |
This also substitutes \"~\" for the user's home directory and |
1052 |
Type \\[describe-variable] directory-abbrev-alist RET for more information." |
removes automounter prefixes (see the variable `automount-dir-prefix')." |
1053 |
;; Get rid of the prefixes added by the automounter. |
;; Get rid of the prefixes added by the automounter. |
1054 |
(if (and automount-dir-prefix |
(if (and automount-dir-prefix |
1055 |
(string-match automount-dir-prefix filename) |
(string-match automount-dir-prefix filename) |
1084 |
;; MS-DOS root directories can come with a drive letter; |
;; MS-DOS root directories can come with a drive letter; |
1085 |
;; Novell Netware allows drive letters beyond `Z:'. |
;; Novell Netware allows drive letters beyond `Z:'. |
1086 |
(not (and (or (eq system-type 'ms-dos) |
(not (and (or (eq system-type 'ms-dos) |
1087 |
|
(eq system-type 'cygwin) |
1088 |
(eq system-type 'windows-nt)) |
(eq system-type 'windows-nt)) |
1089 |
(save-match-data |
(save-match-data |
1090 |
(string-match "^[a-zA-`]:/$" filename))))) |
(string-match "^[a-zA-`]:/$" filename))))) |
1103 |
:type '(repeat (string :tag "Name")) |
:type '(repeat (string :tag "Name")) |
1104 |
:group 'find-file) |
:group 'find-file) |
1105 |
|
|
1106 |
(defun find-buffer-visiting (filename) |
(defun find-buffer-visiting (filename &optional predicate) |
1107 |
"Return the buffer visiting file FILENAME (a string). |
"Return the buffer visiting file FILENAME (a string). |
1108 |
This is like `get-file-buffer', except that it checks for any buffer |
This is like `get-file-buffer', except that it checks for any buffer |
1109 |
visiting the same file, possibly under a different name. |
visiting the same file, possibly under a different name. |
1110 |
|
If PREDICATE is non-nil, only a buffer satisfying it can be returned. |
1111 |
If there is no such live buffer, return nil." |
If there is no such live buffer, return nil." |
1112 |
(let ((buf (get-file-buffer filename)) |
(let ((predicate (or predicate #'identity)) |
1113 |
(truename (abbreviate-file-name (file-truename filename)))) |
(truename (abbreviate-file-name (file-truename filename)))) |
1114 |
(or buf |
(or (let ((buf (get-file-buffer filename))) |
1115 |
(let ((list (buffer-list)) found) |
(when (and buf (funcall predicate buf)) buf)) |
1116 |
(while (and (not found) list) |
(let ((list (buffer-list)) found) |
1117 |
(save-excursion |
(while (and (not found) list) |
1118 |
(set-buffer (car list)) |
(save-excursion |
1119 |
(if (and buffer-file-name |
(set-buffer (car list)) |
1120 |
(string= buffer-file-truename truename)) |
(if (and buffer-file-name |
1121 |
(setq found (car list)))) |
(string= buffer-file-truename truename) |
1122 |
(setq list (cdr list))) |
(funcall predicate (current-buffer))) |
1123 |
found) |
(setq found (car list)))) |
1124 |
(let* ((attributes (file-attributes truename)) |
(setq list (cdr list))) |
1125 |
(number (nthcdr 10 attributes)) |
found) |
1126 |
(list (buffer-list)) found) |
(let* ((attributes (file-attributes truename)) |
1127 |
(and buffer-file-numbers-unique |
(number (nthcdr 10 attributes)) |
1128 |
number |
(list (buffer-list)) found) |
1129 |
(while (and (not found) list) |
(and buffer-file-numbers-unique |
1130 |
(with-current-buffer (car list) |
number |
1131 |
(if (and buffer-file-name |
(while (and (not found) list) |
1132 |
(equal buffer-file-number number) |
(with-current-buffer (car list) |
1133 |
;; Verify this buffer's file number |
(if (and buffer-file-name |
1134 |
;; still belongs to its file. |
(equal buffer-file-number number) |
1135 |
(file-exists-p buffer-file-name) |
;; Verify this buffer's file number |
1136 |
(equal (file-attributes buffer-file-name) |
;; still belongs to its file. |
1137 |
attributes)) |
(file-exists-p buffer-file-name) |
1138 |
(setq found (car list)))) |
(equal (file-attributes buffer-file-truename) |
1139 |
(setq list (cdr list)))) |
attributes) |
1140 |
found)))) |
(funcall predicate (current-buffer))) |
1141 |
|
(setq found (car list)))) |
1142 |
|
(setq list (cdr list)))) |
1143 |
|
found)))) |
1144 |
|
|
1145 |
(defcustom find-file-wildcards t |
(defcustom find-file-wildcards t |
1146 |
"*Non-nil means file-visiting commands should handle wildcards. |
"*Non-nil means file-visiting commands should handle wildcards. |
1307 |
;; Needed in case we are re-visiting the file with a different |
;; Needed in case we are re-visiting the file with a different |
1308 |
;; text representation. |
;; text representation. |
1309 |
(kill-local-variable 'buffer-file-coding-system) |
(kill-local-variable 'buffer-file-coding-system) |
1310 |
|
(kill-local-variable 'cursor-type) |
1311 |
(erase-buffer) |
(erase-buffer) |
1312 |
(and (default-value 'enable-multibyte-characters) |
(and (default-value 'enable-multibyte-characters) |
1313 |
(not rawfile) |
(not rawfile) |
1332 |
(signal 'file-error (list "File is not readable" |
(signal 'file-error (list "File is not readable" |
1333 |
filename))) |
filename))) |
1334 |
;; Run find-file-not-found-hooks until one returns non-nil. |
;; Run find-file-not-found-hooks until one returns non-nil. |
1335 |
(or (run-hook-with-args-until-success 'find-file-not-found-hooks) |
(or (run-hook-with-args-until-success 'find-file-not-found-functions) |
1336 |
;; If they fail too, set error. |
;; If they fail too, set error. |
1337 |
(setq error t))))) |
(setq error t))))) |
1338 |
;; Record the file's truename, and maybe use that as visited name. |
;; Record the file's truename, and maybe use that as visited name. |
1375 |
"Like `insert-file-contents', but only reads in the file literally. |
"Like `insert-file-contents', but only reads in the file literally. |
1376 |
A buffer may be modified in several ways after reading into the buffer, |
A buffer may be modified in several ways after reading into the buffer, |
1377 |
to Emacs features such as format decoding, character code |
to Emacs features such as format decoding, character code |
1378 |
conversion, `find-file-hooks', automatic uncompression, etc. |
conversion, `find-file-hook', automatic uncompression, etc. |
1379 |
|
|
1380 |
This function ensures that none of these modifications will take place." |
This function ensures that none of these modifications will take place." |
1381 |
(let ((format-alist nil) |
(let ((format-alist nil) |
1396 |
(fset 'find-buffer-file-type find-buffer-file-type-function) |
(fset 'find-buffer-file-type find-buffer-file-type-function) |
1397 |
(fmakunbound 'find-buffer-file-type))))) |
(fmakunbound 'find-buffer-file-type))))) |
1398 |
|
|
1399 |
|
(defun insert-file-1 (filename insert-func) |
1400 |
|
(if (file-directory-p filename) |
1401 |
|
(signal 'file-error (list "Opening input file" "file is a directory" |
1402 |
|
filename))) |
1403 |
|
(let* ((buffer (find-buffer-visiting (abbreviate-file-name (file-truename filename)) |
1404 |
|
#'buffer-modified-p)) |
1405 |
|
(tem (funcall insert-func filename))) |
1406 |
|
(push-mark (+ (point) (car (cdr tem)))) |
1407 |
|
(when buffer |
1408 |
|
(message "File %s already visited and modified in buffer %s" |
1409 |
|
filename (buffer-name buffer))))) |
1410 |
|
|
1411 |
(defun insert-file-literally (filename) |
(defun insert-file-literally (filename) |
1412 |
"Insert contents of file FILENAME into buffer after point with no conversion. |
"Insert contents of file FILENAME into buffer after point with no conversion. |
1413 |
|
|
1415 |
Don't call it from programs! Use `insert-file-contents-literally' instead. |
Don't call it from programs! Use `insert-file-contents-literally' instead. |
1416 |
\(Its calling sequence is different; see its documentation)." |
\(Its calling sequence is different; see its documentation)." |
1417 |
(interactive "*fInsert file literally: ") |
(interactive "*fInsert file literally: ") |
1418 |
(if (file-directory-p filename) |
(insert-file-1 filename #'insert-file-contents-literally)) |
|
(signal 'file-error (list "Opening input file" "file is a directory" |
|
|
filename))) |
|
|
(let ((tem (insert-file-contents-literally filename))) |
|
|
(push-mark (+ (point) (car (cdr tem)))))) |
|
1419 |
|
|
1420 |
(defvar find-file-literally nil |
(defvar find-file-literally nil |
1421 |
"Non-nil if this buffer was made by `find-file-literally' or equivalent. |
"Non-nil if this buffer was made by `find-file-literally' or equivalent. |
1456 |
Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil |
Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil |
1457 |
means this call was from `revert-buffer'. |
means this call was from `revert-buffer'. |
1458 |
Fifth arg NOMODES non-nil means don't alter the file's modes. |
Fifth arg NOMODES non-nil means don't alter the file's modes. |
1459 |
Finishes by calling the functions in `find-file-hooks' |
Finishes by calling the functions in `find-file-hook' |
1460 |
unless NOMODES is non-nil." |
unless NOMODES is non-nil." |
1461 |
(setq buffer-read-only (not (file-writable-p buffer-file-name))) |
(setq buffer-read-only (not (file-writable-p buffer-file-name))) |
1462 |
(if noninteractive |
(if noninteractive |
1477 |
(file-newer-than-file-p (or buffer-auto-save-file-name |
(file-newer-than-file-p (or buffer-auto-save-file-name |
1478 |
(make-auto-save-file-name)) |
(make-auto-save-file-name)) |
1479 |
buffer-file-name)) |
buffer-file-name)) |
1480 |
(format "%s has auto save data; consider M-x recover-file" |
(format "%s has auto save data; consider M-x recover-this-file" |
1481 |
(file-name-nondirectory buffer-file-name)) |
(file-name-nondirectory buffer-file-name)) |
1482 |
(setq not-serious t) |
(setq not-serious t) |
1483 |
(if error "(New file)" nil))) |
(if error "(New file)" nil))) |
1502 |
;; before altering a backup file. |
;; before altering a backup file. |
1503 |
(when (backup-file-name-p buffer-file-name) |
(when (backup-file-name-p buffer-file-name) |
1504 |
(setq buffer-read-only t)) |
(setq buffer-read-only t)) |
1505 |
|
;; When a file is marked read-only, |
1506 |
|
;; make the buffer read-only even if root is looking at it. |
1507 |
|
(when (and (file-modes (buffer-file-name)) |
1508 |
|
(zerop (logand (file-modes (buffer-file-name)) #o222))) |
1509 |
|
(setq buffer-read-only t)) |
1510 |
(unless nomodes |
(unless nomodes |
1511 |
(when (and view-read-only view-mode) |
(when (and view-read-only view-mode) |
1512 |
(view-mode-disable)) |
(view-mode-disable)) |
1515 |
view-read-only |
view-read-only |
1516 |
(not (eq (get major-mode 'mode-class) 'special))) |
(not (eq (get major-mode 'mode-class) 'special))) |
1517 |
(view-mode-enter)) |
(view-mode-enter)) |
1518 |
(run-hooks 'find-file-hooks))) |
(run-hooks 'find-file-hook))) |
1519 |
|
|
1520 |
(defun normal-mode (&optional find-file) |
(defun normal-mode (&optional find-file) |
1521 |
"Choose the major mode for this buffer automatically. |
"Choose the major mode for this buffer automatically. |
1543 |
enable-local-variables))) |
enable-local-variables))) |
1544 |
(hack-local-variables)) |
(hack-local-variables)) |
1545 |
(error (message "File local-variables error: %s" |
(error (message "File local-variables error: %s" |
1546 |
(prin1-to-string err))))) |
(prin1-to-string err)))) |
1547 |
|
(if (fboundp 'ucs-set-table-for-input) ; don't lose when building |
1548 |
|
(ucs-set-table-for-input))) |
1549 |
|
|
1550 |
(defvar auto-mode-alist |
(defvar auto-mode-alist |
1551 |
(mapc |
(mapc |
1552 |
(lambda (elt) |
(lambda (elt) |
1553 |
(cons (purecopy (car elt)) (cdr elt))) |
(cons (purecopy (car elt)) (cdr elt))) |
1554 |
'(("\\.in\\'" nil t) |
'(("\\.te?xt\\'" . text-mode) |
|
("\\.te?xt\\'" . text-mode) |
|
1555 |
("\\.c\\'" . c-mode) |
("\\.c\\'" . c-mode) |
1556 |
("\\.h\\'" . c-mode) |
("\\.h\\'" . c-mode) |
1557 |
("\\.tex\\'" . tex-mode) |
("\\.tex\\'" . tex-mode) |
1601 |
("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode) |
("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode) |
1602 |
("\\.scm\\.[0-9]*\\'" . scheme-mode) |
("\\.scm\\.[0-9]*\\'" . scheme-mode) |
1603 |
("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode) |
("\\.[ck]?sh\\'\\|\\.shar\\'\\|/\\.z?profile\\'" . sh-mode) |
1604 |
|
("\\.bash\\'" . sh-mode) |
1605 |
("\\(/\\|\\`\\)\\.\\(bash_profile\\|z?login\\|bash_login\\|z?logout\\)\\'" . sh-mode) |
("\\(/\\|\\`\\)\\.\\(bash_profile\\|z?login\\|bash_login\\|z?logout\\)\\'" . sh-mode) |
1606 |
("\\(/\\|\\`\\)\\.\\(bash_logout\\|shrc\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode) |
("\\(/\\|\\`\\)\\.\\(bash_logout\\|shrc\\|[kz]shrc\\|bashrc\\|t?cshrc\\|esrc\\)\\'" . sh-mode) |
1607 |
("\\(/\\|\\`\\)\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode) |
("\\(/\\|\\`\\)\\.\\([kz]shenv\\|xinitrc\\|startxrc\\|xsession\\)\\'" . sh-mode) |
1640 |
("\\.awk\\'" . awk-mode) |
("\\.awk\\'" . awk-mode) |
1641 |
("\\.prolog\\'" . prolog-mode) |
("\\.prolog\\'" . prolog-mode) |
1642 |
("\\.tar\\'" . tar-mode) |
("\\.tar\\'" . tar-mode) |
1643 |
("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\)\\'" . archive-mode) |
("\\.\\(arc\\|zip\\|lzh\\|zoo\\|ear\\|jar\\|war\\)\\'" . archive-mode) |
1644 |
("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|JAR\\)\\'" . archive-mode) |
("\\.\\(ARC\\|ZIP\\|LZH\\|ZOO\\|EAR\\|JAR\\|WAR\\)\\'" . archive-mode) |
1645 |
|
("\\.sx[dmicw]\\'" . archive-mode) ; OpenOffice.org |
1646 |
;; Mailer puts message to be edited in |
;; Mailer puts message to be edited in |
1647 |
;; /tmp/Re.... or Message |
;; /tmp/Re.... or Message |
1648 |
("\\`/tmp/Re" . text-mode) |
("\\`/tmp/Re" . text-mode) |
1656 |
("\\.oak\\'" . scheme-mode) |
("\\.oak\\'" . scheme-mode) |
1657 |
("\\.sgml?\\'" . sgml-mode) |
("\\.sgml?\\'" . sgml-mode) |
1658 |
("\\.xml\\'" . sgml-mode) |
("\\.xml\\'" . sgml-mode) |
1659 |
|
("\\.xsl\\'" . sgml-mode) |
1660 |
("\\.dtd\\'" . sgml-mode) |
("\\.dtd\\'" . sgml-mode) |
1661 |
("\\.ds\\(ss\\)?l\\'" . dsssl-mode) |
("\\.ds\\(ss\\)?l\\'" . dsssl-mode) |
1662 |
("\\.idl\\'" . idl-mode) |
("\\.idl\\'" . idl-mode) |
1663 |
;; .emacs following a directory delimiter |
;; .emacs or .gnus or .viper following a directory delimiter in |
1664 |
;; in Unix, MSDOG or VMS syntax. |
;; Unix, MSDOG or VMS syntax. |
1665 |
("[]>:/\\]\\..*emacs\\'" . emacs-lisp-mode) |
("[]>:/\\]\\..*\\(emacs\\|gnus\\|viper\\)\\'" . emacs-lisp-mode) |
1666 |
("\\`\\..*emacs\\'" . emacs-lisp-mode) |
("\\`\\..*emacs\\'" . emacs-lisp-mode) |
1667 |
;; _emacs following a directory delimiter |
;; _emacs following a directory delimiter |
1668 |
;; in MsDos syntax |
;; in MsDos syntax |
1688 |
;; for the sake of ChangeLog.1, etc. |
;; for the sake of ChangeLog.1, etc. |
1689 |
;; and after the .scm.[0-9] and CVS' <file>.<rev> patterns too. |
;; and after the .scm.[0-9] and CVS' <file>.<rev> patterns too. |
1690 |
("\\.[1-9]\\'" . nroff-mode) |
("\\.[1-9]\\'" . nroff-mode) |
1691 |
("\\.g\\'" . antlr-mode))) |
("\\.g\\'" . antlr-mode) |
1692 |
|
("\\.ses\\'" . ses-mode) |
1693 |
|
("\\.in\\'" nil t))) |
1694 |
"Alist of filename patterns vs corresponding major mode functions. |
"Alist of filename patterns vs corresponding major mode functions. |
1695 |
Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL). |
Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL). |
1696 |
\(NON-NIL stands for anything that is not nil; the value does not matter.) |
\(NON-NIL stands for anything that is not nil; the value does not matter.) |
1832 |
(mode nil)) |
(mode nil)) |
1833 |
;; Find first matching alist entry. |
;; Find first matching alist entry. |
1834 |
(let ((case-fold-search |
(let ((case-fold-search |
1835 |
(memq system-type '(vax-vms windows-nt)))) |
(memq system-type '(vax-vms windows-nt cygwin)))) |
1836 |
(while (and (not mode) alist) |
(while (and (not mode) alist) |
1837 |
(if (string-match (car (car alist)) name) |
(if (string-match (car (car alist)) name) |
1838 |
(if (and (consp (cdr (car alist))) |
(if (and (consp (cdr (car alist))) |
1917 |
(goto-char beg) |
(goto-char beg) |
1918 |
end)))) |
end)))) |
1919 |
|
|
1920 |
(defun hack-local-variables-prop-line () |
(defun hack-local-variables-prop-line (&optional mode-only) |
1921 |
"Set local variables specified in the -*- line. |
"Set local variables specified in the -*- line. |
1922 |
Ignore any specification for `mode:' and `coding:'; |
Ignore any specification for `mode:' and `coding:'; |
1923 |
`set-auto-mode' should already have handled `mode:', |
`set-auto-mode' should already have handled `mode:', |
1924 |
`set-auto-coding' should already have handled `coding:'." |
`set-auto-coding' should already have handled `coding:'. |
1925 |
|
If MODE-ONLY is non-nil, all we do is check whether the major mode |
1926 |
|
is specified, returning t if it is specified." |
1927 |
(save-excursion |
(save-excursion |
1928 |
(goto-char (point-min)) |
(goto-char (point-min)) |
1929 |
(let ((result nil) |
(let ((result nil) |
1930 |
(end (set-auto-mode-1)) |
(end (set-auto-mode-1)) |
1931 |
|
mode-specified |
1932 |
(enable-local-variables |
(enable-local-variables |
1933 |
(and local-enable-local-variables enable-local-variables))) |
(and local-enable-local-variables enable-local-variables))) |
1934 |
;; Parse the -*- line into the `result' alist. |
;; Parse the -*- line into the RESULT alist. |
1935 |
|
;; Also set MODE-SPECIFIED if we see a spec or `mode'. |
1936 |
(cond ((not end) |
(cond ((not end) |
1937 |
nil) |
nil) |
1938 |
((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)") |
((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)") |
1939 |
;; Simple form: "-*- MODENAME -*-". Already handled. |
;; Simple form: "-*- MODENAME -*-". Already handled. |
1940 |
|
(setq mode-specified t) |
1941 |
nil) |
nil) |
1942 |
(t |
(t |
1943 |
;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' |
;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' |
1963 |
(or (equal (downcase (symbol-name key)) "mode") |
(or (equal (downcase (symbol-name key)) "mode") |
1964 |
(equal (downcase (symbol-name key)) "coding") |
(equal (downcase (symbol-name key)) "coding") |
1965 |
(setq result (cons (cons key val) result))) |
(setq result (cons (cons key val) result))) |
1966 |
|
(if (equal (downcase (symbol-name key)) "mode") |
1967 |
|
(setq mode-specified t)) |
1968 |
(skip-chars-forward " \t;"))) |
(skip-chars-forward " \t;"))) |
1969 |
(setq result (nreverse result)))) |
(setq result (nreverse result)))) |
1970 |
|
|
1971 |
(if (and result |
(if mode-only mode-specified |
1972 |
(or (eq enable-local-variables t) |
(if (and result |
1973 |
(and enable-local-variables |
(or mode-only |
1974 |
(save-window-excursion |
(eq enable-local-variables t) |
1975 |
(condition-case nil |
(and enable-local-variables |
1976 |
(switch-to-buffer (current-buffer)) |
(save-window-excursion |
1977 |
(error |
(condition-case nil |
1978 |
;; If we fail to switch in the selected window, |
(switch-to-buffer (current-buffer)) |
1979 |
;; it is probably a minibuffer. |
(error |
1980 |
;; So try another window. |
;; If we fail to switch in the selected window, |
1981 |
(condition-case nil |
;; it is probably a minibuffer. |
1982 |
(switch-to-buffer-other-window (current-buffer)) |
;; So try another window. |
1983 |
(error |
(condition-case nil |
1984 |
(switch-to-buffer-other-frame (current-buffer)))))) |
(switch-to-buffer-other-window (current-buffer)) |
1985 |
(y-or-n-p (format "Set local variables as specified in -*- line of %s? " |
(error |
1986 |
(file-name-nondirectory buffer-file-name))))))) |
(switch-to-buffer-other-frame (current-buffer)))))) |
1987 |
(let ((enable-local-eval enable-local-eval)) |
(y-or-n-p (format "Set local variables as specified in -*- line of %s? " |
1988 |
(while result |
(file-name-nondirectory buffer-file-name))))))) |
1989 |
(hack-one-local-variable (car (car result)) (cdr (car result))) |
(let ((enable-local-eval enable-local-eval)) |
1990 |
(setq result (cdr result)))))))) |
(while result |
1991 |
|
(hack-one-local-variable (car (car result)) (cdr (car result))) |
1992 |
|
(setq result (cdr result))))) |
1993 |
|
nil)))) |
1994 |
|
|
1995 |
(defvar hack-local-variables-hook nil |
(defvar hack-local-variables-hook nil |
1996 |
"Normal hook run after processing a file's local variables specs. |
"Normal hook run after processing a file's local variables specs. |
2001 |
"Parse and put into effect this buffer's local variables spec. |
"Parse and put into effect this buffer's local variables spec. |
2002 |
If MODE-ONLY is non-nil, all we do is check whether the major mode |
If MODE-ONLY is non-nil, all we do is check whether the major mode |
2003 |
is specified, returning t if it is specified." |
is specified, returning t if it is specified." |
2004 |
(unless mode-only |
(let ((mode-specified |
2005 |
(hack-local-variables-prop-line)) |
;; If MODE-ONLY is t, we check here for specifying the mode |
2006 |
;; Look for "Local variables:" line in last page. |
;; in the -*- line. If MODE-ONLY is nil, we process |
2007 |
(let (mode-specified |
;; the -*- line here. |
2008 |
|
(hack-local-variables-prop-line mode-only)) |
2009 |
(enable-local-variables |
(enable-local-variables |
2010 |
(and local-enable-local-variables enable-local-variables))) |
(and local-enable-local-variables enable-local-variables))) |
2011 |
|
;; Look for "Local variables:" line in last page. |
2012 |
(save-excursion |
(save-excursion |
2013 |
(goto-char (point-max)) |
(goto-char (point-max)) |
2014 |
(search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) |
(search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) |
2095 |
(put 'ignored-local-variables 'risky-local-variable t) |
(put 'ignored-local-variables 'risky-local-variable t) |
2096 |
(put 'eval 'risky-local-variable t) |
(put 'eval 'risky-local-variable t) |
2097 |
(put 'file-name-handler-alist 'risky-local-variable t) |
(put 'file-name-handler-alist 'risky-local-variable t) |
2098 |
|
(put 'inhibit-quit 'risky-local-variable t) |
2099 |
(put 'minor-mode-alist 'risky-local-variable t) |
(put 'minor-mode-alist 'risky-local-variable t) |
2100 |
(put 'minor-mode-map-alist 'risky-local-variable t) |
(put 'minor-mode-map-alist 'risky-local-variable t) |
2101 |
(put 'minor-mode-overriding-map-alist 'risky-local-variable t) |
(put 'minor-mode-overriding-map-alist 'risky-local-variable t) |
2139 |
(put 'mode-line-buffer-identification 'risky-local-variable t) |
(put 'mode-line-buffer-identification 'risky-local-variable t) |
2140 |
(put 'mode-line-modes 'risky-local-variable t) |
(put 'mode-line-modes 'risky-local-variable t) |
2141 |
(put 'mode-line-position 'risky-local-variable t) |
(put 'mode-line-position 'risky-local-variable t) |
2142 |
|
(put 'mode-line-process 'risky-local-variable t) |
2143 |
|
(put 'mode-name 'risky-local-variable t) |
2144 |
(put 'display-time-string 'risky-local-variable t) |
(put 'display-time-string 'risky-local-variable t) |
2145 |
|
(put 'parse-time-rules 'risky-local-variable t) |
2146 |
|
|
2147 |
;; This one is safe because the user gets to check it before it is used. |
;; This case is safe because the user gets to check it before it is used. |
2148 |
(put 'compile-command 'safe-local-variable t) |
(put 'compile-command 'safe-local-variable 'stringp) |
2149 |
|
|
2150 |
|
(defun risky-local-variable-p (sym val) |
2151 |
|
"Non-nil if SYM could be dangerous as a file-local variable with value VAL. |
2152 |
|
If VAL is nil, the question is whether any value might be dangerous." |
2153 |
|
(let ((safep (get sym 'safe-local-variable))) |
2154 |
|
(or (memq sym ignored-local-variables) |
2155 |
|
(get sym 'risky-local-variable) |
2156 |
|
(and (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$" |
2157 |
|
(symbol-name sym)) |
2158 |
|
(not safep)) |
2159 |
|
;; If the safe-local-variable property isn't t or nil, |
2160 |
|
;; then it must return non-nil on the proposed value to be safe. |
2161 |
|
(and (not (memq safep '(t nil))) |
2162 |
|
(or (null val) |
2163 |
|
(not (funcall safep val))))))) |
2164 |
|
|
2165 |
|
(defcustom safe-local-eval-forms nil |
2166 |
|
"*Expressions that are considered \"safe\" in an `eval:' local variable. |
2167 |
|
Add expressions to this list if you want Emacs to evaluate them, when |
2168 |
|
they appear in an `eval' local variable specification, without first |
2169 |
|
asking you for confirmation." |
2170 |
|
:group 'find-file |
2171 |
|
:version "21.4" |
2172 |
|
:type '(repeat sexp)) |
2173 |
|
|
2174 |
|
(put 'c-set-style 'safe-local-eval-function t) |
2175 |
|
|
2176 |
(defun hack-one-local-variable-quotep (exp) |
(defun hack-one-local-variable-quotep (exp) |
2177 |
(and (consp exp) (eq (car exp) 'quote) (consp (cdr exp)))) |
(and (consp exp) (eq (car exp) 'quote) (consp (cdr exp)))) |
2178 |
|
|
2179 |
|
(defun hack-one-local-variable-constantp (exp) |
2180 |
|
(or (and (not (symbolp exp)) (not (consp exp))) |
2181 |
|
(memq exp '(t nil)) |
2182 |
|
(keywordp exp) |
2183 |
|
(hack-one-local-variable-quotep exp))) |
2184 |
|
|
2185 |
|
(defun hack-one-local-variable-eval-safep (exp) |
2186 |
|
"Return t if it is safe to eval EXP when it is found in a file." |
2187 |
|
(or (not (consp exp)) |
2188 |
|
;; Detect certain `put' expressions. |
2189 |
|
(and (eq (car exp) 'put) |
2190 |
|
(hack-one-local-variable-quotep (nth 1 exp)) |
2191 |
|
(hack-one-local-variable-quotep (nth 2 exp)) |
2192 |
|
(memq (nth 1 (nth 2 exp)) |
2193 |
|
'(lisp-indent-hook)) |
2194 |
|
;; Only allow safe values of lisp-indent-hook; |
2195 |
|
;; not functions. |
2196 |
|
(or (numberp (nth 3 exp)) |
2197 |
|
(equal (nth 3 exp) ''defun))) |
2198 |
|
;; Allow expressions that the user requested. |
2199 |
|
(member exp safe-local-eval-forms) |
2200 |
|
;; Certain functions can be allowed with safe arguments |
2201 |
|
;; or can specify verification functions to try. |
2202 |
|
(and (symbolp (car exp)) |
2203 |
|
(let ((prop (get (car exp) 'safe-local-eval-function))) |
2204 |
|
(cond ((eq prop t) |
2205 |
|
(let ((ok t)) |
2206 |
|
(dolist (arg (cdr exp)) |
2207 |
|
(unless (hack-one-local-variable-constantp arg) |
2208 |
|
(setq ok nil))) |
2209 |
|
ok)) |
2210 |
|
((functionp prop) |
2211 |
|
(funcall prop exp)) |
2212 |
|
((listp prop) |
2213 |
|
(let ((ok nil)) |
2214 |
|
(dolist (function prop) |
2215 |
|
(if (funcall function exp) |
2216 |
|
(setq ok t))) |
2217 |
|
ok))))))) |
2218 |
|
|
2219 |
(defun hack-one-local-variable (var val) |
(defun hack-one-local-variable (var val) |
2220 |
"\"Set\" one variable in a local variables spec. |
"\"Set\" one variable in a local variables spec. |
2221 |
A few patterns are specified so that any name which matches one |
A few patterns are specified so that any name which matches one |
2226 |
((eq var 'coding) |
((eq var 'coding) |
2227 |
;; We have already handled coding: tag in set-auto-coding. |
;; We have already handled coding: tag in set-auto-coding. |
2228 |
nil) |
nil) |
|
((memq var ignored-local-variables) |
|
|
nil) |
|
2229 |
;; "Setting" eval means either eval it or do nothing. |
;; "Setting" eval means either eval it or do nothing. |
2230 |
;; Likewise for setting hook variables. |
;; Likewise for setting hook variables. |
2231 |
((or (get var 'risky-local-variable) |
((risky-local-variable-p var val) |
|
(and |
|
|
(string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$\\|-predicate$\\|font-lock-keywords$\\|font-lock-keywords-[0-9]+$\\|font-lock-syntactic-keywords$\\|-frame-alist$\\|-mode-alist$\\|-map$\\|-map-alist$" |
|
|
(symbol-name var)) |
|
|
(not (get var 'safe-local-variable)))) |
|
2232 |
;; Permit evalling a put of a harmless property. |
;; Permit evalling a put of a harmless property. |
2233 |
;; if the args do nothing tricky. |
;; if the args do nothing tricky. |
2234 |
(if (or (and (eq var 'eval) |
(if (or (and (eq var 'eval) |
2235 |
(consp val) |
(hack-one-local-variable-eval-safep val)) |
|
(eq (car val) 'put) |
|
|
(hack-one-local-variable-quotep (nth 1 val)) |
|
|
(hack-one-local-variable-quotep (nth 2 val)) |
|
|
;; Only allow safe values of lisp-indent-hook; |
|
|
;; not functions. |
|
|
(or (numberp (nth 3 val)) |
|
|
(equal (nth 3 val) ''defun)) |
|
|
(memq (nth 1 (nth 2 val)) |
|
|
'(lisp-indent-hook))) |
|
2236 |
;; Permit eval if not root and user says ok. |
;; Permit eval if not root and user says ok. |
2237 |
(and (not (zerop (user-uid))) |
(and (not (zerop (user-uid))) |
2238 |
(or (eq enable-local-eval t) |
(or (eq enable-local-eval t) |
2251 |
(save-excursion (eval val)) |
(save-excursion (eval val)) |
2252 |
(make-local-variable var) |
(make-local-variable var) |
2253 |
(set var val)) |
(set var val)) |
2254 |
(message "Ignoring `eval:' in the local variables list"))) |
(message "Ignoring risky spec in the local variables list"))) |
2255 |
;; Ordinary variable, really set it. |
;; Ordinary variable, really set it. |
2256 |
(t (make-local-variable var) |
(t (make-local-variable var) |
2257 |
;; Make sure the string has no text properties. |
;; Make sure the string has no text properties. |
2331 |
(if filename |
(if filename |
2332 |
(nthcdr 10 (file-attributes buffer-file-name)) |
(nthcdr 10 (file-attributes buffer-file-name)) |
2333 |
nil))) |
nil))) |
2334 |
;; write-file-hooks is normally used for things like ftp-find-file |
;; write-file-functions is normally used for things like ftp-find-file |
2335 |
;; that visit things that are not local files as if they were files. |
;; that visit things that are not local files as if they were files. |
2336 |
;; Changing to visit an ordinary local file instead should flush the hook. |
;; Changing to visit an ordinary local file instead should flush the hook. |
2337 |
(kill-local-variable 'write-file-hooks) |
(kill-local-variable 'write-file-functions) |
2338 |
(kill-local-variable 'local-write-file-hooks) |
(kill-local-variable 'local-write-file-hooks) |
2339 |
(kill-local-variable 'revert-buffer-function) |
(kill-local-variable 'revert-buffer-function) |
2340 |
(kill-local-variable 'backup-inhibited) |
(kill-local-variable 'backup-inhibited) |
2427 |
(defun backup-buffer () |
(defun backup-buffer () |
2428 |
"Make a backup of the disk file visited by the current buffer, if appropriate. |
"Make a backup of the disk file visited by the current buffer, if appropriate. |
2429 |
This is normally done before saving the buffer the first time. |
This is normally done before saving the buffer the first time. |
|
If the value is non-nil, it is the result of `file-modes' on the original |
|
|
file; this means that the caller, after saving the buffer, should change |
|
|
the modes of the new file to agree with the old modes. |
|
2430 |
|
|
2431 |
A backup may be done by renaming or by copying; see documentation of |
A backup may be done by renaming or by copying; see documentation of |
2432 |
variable `make-backup-files'. If it's done by renaming, then the file is |
variable `make-backup-files'. If it's done by renaming, then the file is |
2433 |
no longer accessible under its old name." |
no longer accessible under its old name. |
2434 |
|
|
2435 |
|
The value is non-nil after a backup was made by renaming. |
2436 |
|
It has the form (MODES . BACKUPNAME). |
2437 |
|
MODES is the result of `file-modes' on the original |
2438 |
|
file; this means that the caller, after saving the buffer, should change |
2439 |
|
the modes of the new file to agree with the old modes. |
2440 |
|
BACKUPNAME is the backup file name, which is the old file renamed." |
2441 |
(if (and make-backup-files (not backup-inhibited) |
(if (and make-backup-files (not backup-inhibited) |
2442 |
(not buffer-backed-up) |
(not buffer-backed-up) |
2443 |
(file-exists-p buffer-file-name) |
(file-exists-p buffer-file-name) |
2463 |
(or (eq delete-old-versions t) (eq delete-old-versions nil)) |
(or (eq delete-old-versions t) (eq delete-old-versions nil)) |
2464 |
(or delete-old-versions |
(or delete-old-versions |
2465 |
(y-or-n-p (format "Delete excess backup versions of %s? " |
(y-or-n-p (format "Delete excess backup versions of %s? " |
2466 |
real-file-name)))))) |
real-file-name))))) |
2467 |
|
(modes (file-modes buffer-file-name))) |
2468 |
;; Actually write the back up file. |
;; Actually write the back up file. |
2469 |
(condition-case () |
(condition-case () |
2470 |
(if (or file-precious-flag |
(if (or file-precious-flag |
2471 |
; (file-symlink-p buffer-file-name) |
; (file-symlink-p buffer-file-name) |
2472 |
backup-by-copying |
backup-by-copying |
2473 |
|
;; Don't rename a suid or sgid file. |
2474 |
|
(and modes (< 0 (logand modes #o6000))) |
2475 |
(and backup-by-copying-when-linked |
(and backup-by-copying-when-linked |
2476 |
(> (file-nlinks real-file-name) 1)) |
(> (file-nlinks real-file-name) 1)) |
2477 |
(and (or backup-by-copying-when-mismatch |
(and (or backup-by-copying-when-mismatch |
2483 |
(<= (nth 2 attr) backup-by-copying-when-privileged-mismatch))) |
(<= (nth 2 attr) backup-by-copying-when-privileged-mismatch))) |
2484 |
(or (nth 9 attr) |
(or (nth 9 attr) |
2485 |
(not (file-ownership-preserved-p real-file-name))))))) |
(not (file-ownership-preserved-p real-file-name))))))) |
2486 |
(condition-case () |
(backup-buffer-copy real-file-name backupname modes) |
|
(copy-file real-file-name backupname t t) |
|
|
(file-error |
|
|
;; If copying fails because file BACKUPNAME |
|
|
;; is not writable, delete that file and try again. |
|
|
(if (and (file-exists-p backupname) |
|
|
(not (file-writable-p backupname))) |
|
|
(delete-file backupname)) |
|
|
(copy-file real-file-name backupname t t))) |
|
2487 |
;; rename-file should delete old backup. |
;; rename-file should delete old backup. |
2488 |
(rename-file real-file-name backupname t) |
(rename-file real-file-name backupname t) |
2489 |
(setq setmodes (file-modes backupname))) |
(setq setmodes (cons modes backupname))) |
2490 |
(file-error |
(file-error |
2491 |
;; If trouble writing the backup, write it in ~. |
;; If trouble writing the backup, write it in ~. |
2492 |
(setq backupname (expand-file-name |
(setq backupname (expand-file-name |
2495 |
(message "Cannot write backup file; backing up in %s" |
(message "Cannot write backup file; backing up in %s" |
2496 |
(file-name-nondirectory backupname)) |
(file-name-nondirectory backupname)) |
2497 |
(sleep-for 1) |
(sleep-for 1) |
2498 |
(condition-case () |
(backup-buffer-copy real-file-name backupname modes))) |
|
(copy-file real-file-name backupname t t) |
|
|
(file-error |
|
|
;; If copying fails because file BACKUPNAME |
|
|
;; is not writable, delete that file and try again. |
|
|
(if (and (file-exists-p backupname) |
|
|
(not (file-writable-p backupname))) |
|
|
(delete-file backupname)) |
|
|
(copy-file real-file-name backupname t t))))) |
|
2499 |
(setq buffer-backed-up t) |
(setq buffer-backed-up t) |
2500 |
;; Now delete the old versions, if desired. |
;; Now delete the old versions, if desired. |
2501 |
(if delete-old-versions |
(if delete-old-versions |
2507 |
setmodes) |
setmodes) |
2508 |
(file-error nil)))))) |
(file-error nil)))))) |
2509 |
|
|
2510 |
|
(defun backup-buffer-copy (from-name to-name modes) |
2511 |
|
(condition-case () |
2512 |
|
(copy-file from-name to-name t t) |
2513 |
|
(file-error |
2514 |
|
;; If copying fails because file TO-NAME |
2515 |
|
;; is not writable, delete that file and try again. |
2516 |
|
(if (and (file-exists-p to-name) |
2517 |
|
(not (file-writable-p to-name))) |
2518 |
|
(delete-file to-name)) |
2519 |
|
(copy-file from-name to-name t t))) |
2520 |
|
(set-file-modes to-name (logand modes #o1777))) |
2521 |
|
|
2522 |
(defun file-name-sans-versions (name &optional keep-backup-version) |
(defun file-name-sans-versions (name &optional keep-backup-version) |
2523 |
"Return file NAME sans backup versions or strings. |
"Return file NAME sans backup versions or strings. |
2524 |
This is a separate procedure so your site-init or startup file can |
This is a separate procedure so your site-init or startup file can |
2567 |
(if (and (string-match "\\.[^.]*\\'" file) |
(if (and (string-match "\\.[^.]*\\'" file) |
2568 |
(not (eq 0 (match-beginning 0)))) |
(not (eq 0 (match-beginning 0)))) |
2569 |
(if (setq directory (file-name-directory filename)) |
(if (setq directory (file-name-directory filename)) |
2570 |
(expand-file-name (substring file 0 (match-beginning 0)) |
;; Don't use expand-file-name here; if DIRECTORY is relative, |
2571 |
directory) |
;; we don't want to expand it. |
2572 |
|
(concat directory (substring file 0 (match-beginning 0))) |
2573 |
(substring file 0 (match-beginning 0))) |
(substring file 0 (match-beginning 0))) |
2574 |
filename)))) |
filename)))) |
2575 |
|
|
2671 |
(defun make-backup-file-name-1 (file) |
(defun make-backup-file-name-1 (file) |
2672 |
"Subroutine of `make-backup-file-name' and `find-backup-file-name'." |
"Subroutine of `make-backup-file-name' and `find-backup-file-name'." |
2673 |
(let ((alist backup-directory-alist) |
(let ((alist backup-directory-alist) |
2674 |
elt backup-directory dir-sep-string) |
elt backup-directory failed) |
2675 |
(while alist |
(while alist |
2676 |
(setq elt (pop alist)) |
(setq elt (pop alist)) |
2677 |
(if (string-match (car elt) file) |
(if (string-match (car elt) file) |
2678 |
(setq backup-directory (cdr elt) |
(setq backup-directory (cdr elt) |
2679 |
alist nil))) |
alist nil))) |
2680 |
(if (null backup-directory) |
(if (and backup-directory (not (file-exists-p backup-directory))) |
|
file |
|
|
(unless (file-exists-p backup-directory) |
|
2681 |
(condition-case nil |
(condition-case nil |
2682 |
(make-directory backup-directory 'parents) |
(make-directory backup-directory 'parents) |
2683 |
(file-error file))) |
(file-error (setq backup-directory nil)))) |
2684 |
|
(if (null backup-directory) |
2685 |
|
file |
2686 |
(if (file-name-absolute-p backup-directory) |
(if (file-name-absolute-p backup-directory) |
2687 |
(progn |
(progn |
2688 |
(when (memq system-type '(windows-nt ms-dos)) |
(when (memq system-type '(windows-nt ms-dos cygwin)) |
2689 |
;; Normalize DOSish file names: convert all slashes to |
;; Normalize DOSish file names: downcase the drive |
2690 |
;; directory-sep-char, downcase the drive letter, if any, |
;; letter, if any, and replace the leading "x:" with |
2691 |
;; and replace the leading "x:" with "/drive_x". |
;; "/drive_x". |
2692 |
(or (file-name-absolute-p file) |
(or (file-name-absolute-p file) |
2693 |
(setq file (expand-file-name file))) ; make defaults explicit |
(setq file (expand-file-name file))) ; make defaults explicit |
2694 |
;; Replace any invalid file-name characters (for the |
;; Replace any invalid file-name characters (for the |
2695 |
;; case of backing up remote files). |
;; case of backing up remote files). |
2696 |
(setq file (expand-file-name (convert-standard-filename file))) |
(setq file (expand-file-name (convert-standard-filename file))) |
|
(setq dir-sep-string (char-to-string directory-sep-char)) |
|
2697 |
(if (eq (aref file 1) ?:) |
(if (eq (aref file 1) ?:) |
2698 |
(setq file (concat dir-sep-string |
(setq file (concat "/" |
2699 |
"drive_" |
"drive_" |
2700 |
(char-to-string (downcase (aref file 0))) |
(char-to-string (downcase (aref file 0))) |
2701 |
(if (eq (aref file 2) directory-sep-char) |
(if (eq (aref file 2) ?/) |
2702 |
"" |
"" |
2703 |
dir-sep-string) |
"/") |
2704 |
(substring file 2))))) |
(substring file 2))))) |
2705 |
;; Make the name unique by substituting directory |
;; Make the name unique by substituting directory |
2706 |
;; separators. It may not really be worth bothering about |
;; separators. It may not really be worth bothering about |
2707 |
;; doubling `!'s in the original name... |
;; doubling `!'s in the original name... |
2708 |
(expand-file-name |
(expand-file-name |
2709 |
(subst-char-in-string |
(subst-char-in-string |
2710 |
directory-sep-char ?! |
?/ ?! |
2711 |
(replace-regexp-in-string "!" "!!" file)) |
(replace-regexp-in-string "!" "!!" file)) |
2712 |
backup-directory)) |
backup-directory)) |
2713 |
(expand-file-name (file-name-nondirectory file) |
(expand-file-name (file-name-nondirectory file) |
2793 |
"Return number of names file FILENAME has." |
"Return number of names file FILENAME has." |
2794 |
(car (cdr (file-attributes filename)))) |
(car (cdr (file-attributes filename)))) |
2795 |
|
|
2796 |
|
;; (defun file-relative-name (filename &optional directory) |
2797 |
|
;; "Convert FILENAME to be relative to DIRECTORY (default: `default-directory'). |
2798 |
|
;; This function returns a relative file name which is equivalent to FILENAME |
2799 |
|
;; when used with that default directory as the default. |
2800 |
|
;; If this is impossible (which can happen on MSDOS and Windows |
2801 |
|
;; when the file name and directory use different drive names) |
2802 |
|
;; then it returns FILENAME." |
2803 |
|
;; (save-match-data |
2804 |
|
;; (let ((fname (expand-file-name filename))) |
2805 |
|
;; (setq directory (file-name-as-directory |
2806 |
|
;; (expand-file-name (or directory default-directory)))) |
2807 |
|
;; ;; On Microsoft OSes, if FILENAME and DIRECTORY have different |
2808 |
|
;; ;; drive names, they can't be relative, so return the absolute name. |
2809 |
|
;; (if (and (or (eq system-type 'ms-dos) |
2810 |
|
;; (eq system-type 'cygwin) |
2811 |
|
;; (eq system-type 'windows-nt)) |
2812 |
|
;; (not (string-equal (substring fname 0 2) |
2813 |
|
;; (substring directory 0 2)))) |
2814 |
|
;; filename |
2815 |
|
;; (let ((ancestor ".") |
2816 |
|
;; (fname-dir (file-name-as-directory fname))) |
2817 |
|
;; (while (and (not (string-match (concat "^" (regexp-quote directory)) fname-dir)) |
2818 |
|
;; (not (string-match (concat "^" (regexp-quote directory)) fname))) |
2819 |
|
;; (setq directory (file-name-directory (substring directory 0 -1)) |
2820 |
|
;; ancestor (if (equal ancestor ".") |
2821 |
|
;; ".." |
2822 |
|
;; (concat "../" ancestor)))) |
2823 |
|
;; ;; Now ancestor is empty, or .., or ../.., etc. |
2824 |
|
;; (if (string-match (concat "^" (regexp-quote directory)) fname) |
2825 |
|
;; ;; We matched within FNAME's directory part. |
2826 |
|
;; ;; Add the rest of FNAME onto ANCESTOR. |
2827 |
|
;; (let ((rest (substring fname (match-end 0)))) |
2828 |
|
;; (if (and (equal ancestor ".") |
2829 |
|
;; (not (equal rest ""))) |
2830 |
|
;; ;; But don't bother with ANCESTOR if it would give us `./'. |
2831 |
|
;; rest |
2832 |
|
;; (concat (file-name-as-directory ancestor) rest))) |
2833 |
|
;; ;; We matched FNAME's directory equivalent. |
2834 |
|
;; ancestor)))))) |
2835 |
|
|
2836 |
(defun file-relative-name (filename &optional directory) |
(defun file-relative-name (filename &optional directory) |
2837 |
"Convert FILENAME to be relative to DIRECTORY (default: `default-directory'). |
"Convert FILENAME to be relative to DIRECTORY (default: `default-directory'). |
2838 |
This function returns a relative file name which is equivalent to FILENAME |
This function returns a relative file name which is equivalent to FILENAME |
2839 |
when used with that default directory as the default. |
when used with that default directory as the default. |
2840 |
If this is impossible (which can happen on MSDOS and Windows |
If FILENAME and DIRECTORY lie on different machines or on different drives |
2841 |
when the file name and directory use different drive names) |
on a DOS/Windows machine, it returns FILENAME on expanded form." |
|
then it returns FILENAME." |
|
2842 |
(save-match-data |
(save-match-data |
2843 |
(let ((fname (expand-file-name filename))) |
(setq directory |
2844 |
(setq directory (file-name-as-directory |
(file-name-as-directory (expand-file-name (or directory |
2845 |
(expand-file-name (or directory default-directory)))) |
default-directory)))) |
2846 |
;; On Microsoft OSes, if FILENAME and DIRECTORY have different |
(setq filename (expand-file-name filename)) |
2847 |
;; drive names, they can't be relative, so return the absolute name. |
(let ((hf (find-file-name-handler filename 'file-local-copy)) |
2848 |
(if (and (or (eq system-type 'ms-dos) |
(hd (find-file-name-handler directory 'file-local-copy))) |
2849 |
(eq system-type 'windows-nt)) |
(when (and hf (not (get hf 'file-remote-p))) (setq hf nil)) |
2850 |
(not (string-equal (substring fname 0 2) |
(when (and hd (not (get hd 'file-remote-p))) (setq hd nil)) |
2851 |
(substring directory 0 2)))) |
(if ;; Conditions for separate trees |
2852 |
|
(or |
2853 |
|
;; Test for different drives on DOS/Windows |
2854 |
|
(and |
2855 |
|
(memq system-type '(ms-dos cygwin windows-nt)) |
2856 |
|
(not (string-equal (substring filename 0 2) |
2857 |
|
(substring directory 0 2)))) |
2858 |
|
;; Test for different remote file handlers |
2859 |
|
(not (eq hf hd)) |
2860 |
|
;; Test for different remote file system identification |
2861 |
|
(and |
2862 |
|
hf |
2863 |
|
(let ((re (car (rassq hf file-name-handler-alist)))) |
2864 |
|
(not |
2865 |
|
(equal |
2866 |
|
(and |
2867 |
|
(string-match re filename) |
2868 |
|
(substring filename 0 (match-end 0))) |
2869 |
|
(and |
2870 |
|
(string-match re directory) |
2871 |
|
(substring directory 0 (match-end 0)))))))) |
2872 |
filename |
filename |
2873 |
(let ((ancestor ".") |
(unless (eq (aref filename 0) ?/) |
2874 |
(fname-dir (file-name-as-directory fname))) |
(setq filename (concat "/" filename))) |
2875 |
(while (and (not (string-match (concat "^" (regexp-quote directory)) fname-dir)) |
(unless (eq (aref directory 0) ?/) |
2876 |
(not (string-match (concat "^" (regexp-quote directory)) fname))) |
(setq directory (concat "/" directory))) |
2877 |
(setq directory (file-name-directory (substring directory 0 -1)) |
(let ((ancestor ".") |
2878 |
|
(filename-dir (file-name-as-directory filename))) |
2879 |
|
(while |
2880 |
|
(and |
2881 |
|
(not (string-match (concat "^" (regexp-quote directory)) |
2882 |
|
filename-dir)) |
2883 |
|
(not (string-match (concat "^" (regexp-quote directory)) |
2884 |
|
filename))) |
2885 |
|
(setq directory (file-name-directory (substring directory 0 -1)) |
2886 |
ancestor (if (equal ancestor ".") |
ancestor (if (equal ancestor ".") |
2887 |
".." |
".." |
2888 |
(concat "../" ancestor)))) |
(concat "../" ancestor)))) |
2889 |
;; Now ancestor is empty, or .., or ../.., etc. |
;; Now ancestor is empty, or .., or ../.., etc. |
2890 |
(if (string-match (concat "^" (regexp-quote directory)) fname) |
(if (string-match (concat "^" (regexp-quote directory)) filename) |
2891 |
;; We matched within FNAME's directory part. |
;; We matched within FILENAME's directory part. |
2892 |
;; Add the rest of FNAME onto ANCESTOR. |
;; Add the rest of FILENAME onto ANCESTOR. |
2893 |
(let ((rest (substring fname (match-end 0)))) |
(let ((rest (substring filename (match-end 0)))) |
2894 |
(if (and (equal ancestor ".") |
(if (and (equal ancestor ".") (not (equal rest ""))) |
|
(not (equal rest ""))) |
|
2895 |
;; But don't bother with ANCESTOR if it would give us `./'. |
;; But don't bother with ANCESTOR if it would give us `./'. |
2896 |
rest |
rest |
2897 |
(concat (file-name-as-directory ancestor) rest))) |
(concat (file-name-as-directory ancestor) rest))) |
2898 |
;; We matched FNAME's directory equivalent. |
;; We matched FILENAME's directory equivalent. |
2899 |
ancestor)))))) |
ancestor)))))) |
2900 |
|
|
2901 |
(defun save-buffer (&optional args) |
(defun save-buffer (&optional args) |
2902 |
"Save current buffer in visited file if modified. Versions described below. |
"Save current buffer in visited file if modified. Versions described below. |
2977 |
|
|
2978 |
(defun basic-save-buffer () |
(defun basic-save-buffer () |
2979 |
"Save the current buffer in its visited file, if it has been modified. |
"Save the current buffer in its visited file, if it has been modified. |
2980 |
The hooks `write-contents-hooks', `local-write-file-hooks' and |
The hooks `write-contents-functions' and `write-file-functions' get a chance |
2981 |
`write-file-hooks' get a chance to do the job of saving; if they do not, |
to do the job of saving; if they do not, then the buffer is saved in |
2982 |
then the buffer is saved in the visited file file in the usual way. |
the visited file file in the usual way. |
2983 |
After saving the buffer, this function runs `after-save-hook'." |
After saving the buffer, this function runs `after-save-hook'." |
2984 |
(interactive) |
(interactive) |
2985 |
(save-current-buffer |
(save-current-buffer |
3021 |
(save-restriction |
(save-restriction |
3022 |
(widen) |
(widen) |
3023 |
(save-excursion |
(save-excursion |
3024 |
(and (> (point-max) 1) |
(and (> (point-max) (point-min)) |
3025 |
(not find-file-literally) |
(not find-file-literally) |
3026 |
(/= (char-after (1- (point-max))) ?\n) |
(/= (char-after (1- (point-max))) ?\n) |
3027 |
(not (and (eq selective-display t) |
(not (and (eq selective-display t) |
3036 |
(insert ?\n)))) |
(insert ?\n)))) |
3037 |
;; Support VC version backups. |
;; Support VC version backups. |
3038 |
(vc-before-save) |
(vc-before-save) |
3039 |
(or (run-hook-with-args-until-success 'write-contents-hooks) |
(or (run-hook-with-args-until-success 'write-contents-functions) |
3040 |
(run-hook-with-args-until-success 'local-write-file-hooks) |
(run-hook-with-args-until-success 'local-write-file-hooks) |
3041 |
(run-hook-with-args-until-success 'write-file-hooks) |
(run-hook-with-args-until-success 'write-file-functions) |
3042 |
;; If a hook returned t, file is already "written". |
;; If a hook returned t, file is already "written". |
3043 |
;; Otherwise, write it the usual way now. |
;; Otherwise, write it the usual way now. |
3044 |
(setq setmodes (basic-save-buffer-1))) |
(setq setmodes (basic-save-buffer-1))) |
3052 |
(nthcdr 10 (file-attributes buffer-file-name))) |
(nthcdr 10 (file-attributes buffer-file-name))) |
3053 |
(if setmodes |
(if setmodes |
3054 |
(condition-case () |
(condition-case () |
3055 |
(set-file-modes buffer-file-name setmodes) |
(set-file-modes buffer-file-name (car setmodes)) |
3056 |
(error nil)))) |
(error nil)))) |
3057 |
;; If the auto-save file was recent before this command, |
;; If the auto-save file was recent before this command, |
3058 |
;; delete it now. |
;; delete it now. |
3064 |
|
|
3065 |
;; This does the "real job" of writing a buffer into its visited file |
;; This does the "real job" of writing a buffer into its visited file |
3066 |
;; and making a backup file. This is what is normally done |
;; and making a backup file. This is what is normally done |
3067 |
;; but inhibited if one of write-file-hooks returns non-nil. |
;; but inhibited if one of write-file-functions returns non-nil. |
3068 |
;; It returns a value to store in setmodes. |
;; It returns a value (MODES . BACKUPNAME), like backup-buffer. |
3069 |
(defun basic-save-buffer-1 () |
(defun basic-save-buffer-1 () |
3070 |
(if save-buffer-coding-system |
(if save-buffer-coding-system |
3071 |
(let ((coding-system-for-write save-buffer-coding-system)) |
(let ((coding-system-for-write save-buffer-coding-system)) |
3072 |
(basic-save-buffer-2)) |
(basic-save-buffer-2)) |
3073 |
(basic-save-buffer-2))) |
(basic-save-buffer-2))) |
3074 |
|
|
3075 |
|
;; This returns a value (MODES . BACKUPNAME), like backup-buffer. |
3076 |
(defun basic-save-buffer-2 () |
(defun basic-save-buffer-2 () |
3077 |
(let (tempsetmodes setmodes) |
(let (tempsetmodes setmodes) |
3078 |
(if (not (file-writable-p buffer-file-name)) |
(if (not (file-writable-p buffer-file-name)) |
3131 |
;; Since we have created an entirely new file |
;; Since we have created an entirely new file |
3132 |
;; and renamed it, make sure it gets the |
;; and renamed it, make sure it gets the |
3133 |
;; right permission bits set. |
;; right permission bits set. |
3134 |
(setq setmodes (file-modes buffer-file-name)) |
(setq setmodes (or setmodes (cons (file-modes buffer-file-name) |
3135 |
|
buffer-file-name))) |
3136 |
;; We succeeded in writing the temp file, |
;; We succeeded in writing the temp file, |
3137 |
;; so rename it. |
;; so rename it. |
3138 |
(rename-file tempname buffer-file-name t)) |
(rename-file tempname buffer-file-name t)) |
3142 |
;; (setmodes is set) because that says we're superseding. |
;; (setmodes is set) because that says we're superseding. |
3143 |
(cond ((and tempsetmodes (not setmodes)) |
(cond ((and tempsetmodes (not setmodes)) |
3144 |
;; Change the mode back, after writing. |
;; Change the mode back, after writing. |
3145 |
(setq setmodes (file-modes buffer-file-name)) |
(setq setmodes (cons (file-modes buffer-file-name) buffer-file-name)) |
3146 |
(set-file-modes buffer-file-name (logior setmodes 128)))) |
(set-file-modes buffer-file-name (logior (car setmodes) 128)))) |
3147 |
(write-region (point-min) (point-max) |
(let (success) |
3148 |
buffer-file-name nil t buffer-file-truename))) |
(unwind-protect |
3149 |
|
(progn |
3150 |
|
(write-region (point-min) (point-max) |
3151 |
|
buffer-file-name nil t buffer-file-truename) |
3152 |
|
(setq success t)) |
3153 |
|
;; If we get an error writing the new file, and we made |
3154 |
|
;; the backup by renaming, undo the backing-up. |
3155 |
|
(and setmodes (not success) |
3156 |
|
(rename-file (cdr setmodes) buffer-file-name)))))) |
3157 |
setmodes)) |
setmodes)) |
3158 |
|
|
3159 |
|
(defun diff-buffer-with-file (&optional buffer) |
3160 |
|
"View the differences between BUFFER and its associated file. |
3161 |
|
This requires the external program `diff' to be in your `exec-path'." |
3162 |
|
(interactive "bBuffer: ") |
3163 |
|
(with-current-buffer (get-buffer (or buffer (current-buffer))) |
3164 |
|
(if (and buffer-file-name |
3165 |
|
(file-exists-p buffer-file-name)) |
3166 |
|
(let ((tempfile (make-temp-file "buffer-content-"))) |
3167 |
|
(unwind-protect |
3168 |
|
(save-restriction |
3169 |
|
(widen) |
3170 |
|
(write-region (point-min) (point-max) tempfile nil 'nomessage) |
3171 |
|
(diff buffer-file-name tempfile nil t) |
3172 |
|
(sit-for 0)) |
3173 |
|
(when (file-exists-p tempfile) |
3174 |
|
(delete-file tempfile)))) |
3175 |
|
(message "Buffer %s has no associated file on disc" (buffer-name)) |
3176 |
|
;; Display that message for 1 second so that user can read it |
3177 |
|
;; in the minibuffer. |
3178 |
|
(sit-for 1))) |
3179 |
|
;; return always nil, so that save-buffers-kill-emacs will not move |
3180 |
|
;; over to the next unsaved buffer when calling `d'. |
3181 |
|
nil) |
3182 |
|
|
3183 |
|
(defvar save-some-buffers-action-alist |
3184 |
|
'((?\C-r |
3185 |
|
(lambda (buf) |
3186 |
|
(view-buffer buf |
3187 |
|
(lambda (ignore) |
3188 |
|
(exit-recursive-edit))) |
3189 |
|
(recursive-edit) |
3190 |
|
;; Return nil to ask about BUF again. |
3191 |
|
nil) |
3192 |
|
"display the current buffer") |
3193 |
|
(?d diff-buffer-with-file |
3194 |
|
"show difference to last saved version")) |
3195 |
|
"ACTION-ALIST argument used in call to `map-y-or-n-p'.") |
3196 |
|
(put 'save-some-buffers-action-alist 'risky-local-variable t) |
3197 |
|
|
3198 |
(defun save-some-buffers (&optional arg pred) |
(defun save-some-buffers (&optional arg pred) |
3199 |
"Save some modified file-visiting buffers. Asks user about each one. |
"Save some modified file-visiting buffers. Asks user about each one. |
3200 |
|
You can answer `y' to save, `n' not to save, `C-r' to look at the |
3201 |
|
buffer in question with `view-buffer' before deciding or `d' to |
3202 |
|
view the differences using `diff-buffer-to-file'. |
3203 |
|
|
3204 |
Optional argument (the prefix) non-nil means save all with no questions. |
Optional argument (the prefix) non-nil means save all with no questions. |
3205 |
Optional second argument PRED determines which buffers are considered: |
Optional second argument PRED determines which buffers are considered: |
3206 |
If PRED is nil, all the file-visiting buffers are considered. |
If PRED is nil, all the file-visiting buffers are considered. |
3207 |
If PRED is t, then certain non-file buffers will also be considered. |
If PRED is t, then certain non-file buffers will also be considered. |
3208 |
If PRED is a zero-argument function, it indicates for each buffer whether |
If PRED is a zero-argument function, it indicates for each buffer whether |
3209 |
to consider it or not when called with that buffer current." |
to consider it or not when called with that buffer current. |
3210 |
|
|
3211 |
|
See `save-some-buffers-action-alist' if you want to |
3212 |
|
change the additional actions you can take on files." |
3213 |
(interactive "P") |
(interactive "P") |
3214 |
(save-window-excursion |
(save-window-excursion |
3215 |
(let* ((queried nil) |
(let* ((queried nil) |
3241 |
(save-buffer))) |
(save-buffer))) |
3242 |
(buffer-list) |
(buffer-list) |
3243 |
'("buffer" "buffers" "save") |
'("buffer" "buffers" "save") |
3244 |
(list (list ?\C-r (lambda (buf) |
save-some-buffers-action-alist)) |
|
(view-buffer buf |
|
|
(function |
|
|
(lambda (ignore) |
|
|
(exit-recursive-edit)))) |
|
|
(recursive-edit) |
|
|
;; Return nil to ask about BUF again. |
|
|
nil) |
|
|
"display the current buffer")))) |
|
3245 |
(abbrevs-done |
(abbrevs-done |
3246 |
(and save-abbrevs abbrevs-changed |
(and save-abbrevs abbrevs-changed |
3247 |
(progn |
(progn |
3272 |
With arg, set read-only iff arg is positive. |
With arg, set read-only iff arg is positive. |
3273 |
If visiting file read-only and `view-read-only' is non-nil, enter view mode." |
If visiting file read-only and `view-read-only' is non-nil, enter view mode." |
3274 |
(interactive "P") |
(interactive "P") |
3275 |
(cond |
(if (and arg |
3276 |
((and arg (if (> (prefix-numeric-value arg) 0) buffer-read-only |
(if (> (prefix-numeric-value arg) 0) buffer-read-only |
3277 |
(not buffer-read-only))) ; If buffer-read-only is set correctly, |
(not buffer-read-only))) ; If buffer-read-only is set correctly, |
3278 |
nil) ; do nothing. |
nil ; do nothing. |
3279 |
;; Toggle. |
;; Toggle. |
3280 |
((and buffer-read-only view-mode) |
(cond |
3281 |
(View-exit-and-edit) |
((and buffer-read-only view-mode) |
3282 |
(make-local-variable 'view-read-only) |
(View-exit-and-edit) |
3283 |
(setq view-read-only t)) ; Must leave view mode. |
(make-local-variable 'view-read-only) |
3284 |
((and (not buffer-read-only) view-read-only |
(setq view-read-only t)) ; Must leave view mode. |
3285 |
(not (eq (get major-mode 'mode-class) 'special))) |
((and (not buffer-read-only) view-read-only |
3286 |
(view-mode-enter)) |
(not (eq (get major-mode 'mode-class) 'special))) |
3287 |
(t (setq buffer-read-only (not buffer-read-only)) |
(view-mode-enter)) |
3288 |
(force-mode-line-update)))) |
(t (setq buffer-read-only (not buffer-read-only)) |
3289 |
|
(force-mode-line-update))) |
3290 |
|
(if (vc-backend buffer-file-name) |
3291 |
|
(message (substitute-command-keys |
3292 |
|
(concat "File is under version-control; " |
3293 |
|
"use \\[vc-next-action] to check in/out")))))) |
3294 |
|
|
3295 |
(defun insert-file (filename) |
(defun insert-file (filename) |
3296 |
"Insert contents of file FILENAME into buffer after point. |
"Insert contents of file FILENAME into buffer after point. |
3300 |
Don't call it from programs! Use `insert-file-contents' instead. |
Don't call it from programs! Use `insert-file-contents' instead. |
3301 |
\(Its calling sequence is different; see its documentation)." |
\(Its calling sequence is different; see its documentation)." |
3302 |
(interactive "*fInsert file: ") |
(interactive "*fInsert file: ") |
3303 |
(if (file-directory-p filename) |
(insert-file-1 filename #'insert-file-contents)) |
|
(signal 'file-error (list "Opening input file" "file is a directory" |
|
|
filename))) |
|
|
(let ((tem (insert-file-contents filename))) |
|
|
(push-mark (+ (point) (car (cdr tem)))))) |
|
3304 |
|
|
3305 |
(defun append-to-file (start end filename) |
(defun append-to-file (start end filename) |
3306 |
"Append the contents of the region to the end of file FILENAME. |
"Append the contents of the region to the end of file FILENAME. |
3362 |
(list (read-file-name "Make directory: " default-directory default-directory |
(list (read-file-name "Make directory: " default-directory default-directory |
3363 |
nil nil) |
nil nil) |
3364 |
t)) |
t)) |
3365 |
|
;; If default-directory is a remote directory, |
3366 |
|
;; make sure we find its make-directory handler. |
3367 |
|
(setq dir (expand-file-name dir)) |
3368 |
(let ((handler (find-file-name-handler dir 'make-directory))) |
(let ((handler (find-file-name-handler dir 'make-directory))) |
3369 |
(if handler |
(if handler |
3370 |
(funcall handler 'make-directory dir parents) |
(funcall handler 'make-directory dir parents) |
3492 |
(funcall revert-buffer-insert-file-contents-function |
(funcall revert-buffer-insert-file-contents-function |
3493 |
file-name auto-save-p) |
file-name auto-save-p) |
3494 |
(if (not (file-exists-p file-name)) |
(if (not (file-exists-p file-name)) |
3495 |
(error "File %s no longer exists!" file-name)) |
(error (if buffer-file-number |
3496 |
|
"File %s no longer exists!" |
3497 |
|
"Cannot revert nonexistent file %s") |
3498 |
|
file-name)) |
3499 |
;; Bind buffer-file-name to nil |
;; Bind buffer-file-name to nil |
3500 |
;; so that we don't try to lock the file. |
;; so that we don't try to lock the file. |
3501 |
(let ((buffer-file-name nil)) |
(let ((buffer-file-name nil)) |
3506 |
;; Auto-saved file shoule be read without |
;; Auto-saved file shoule be read without |
3507 |
;; any code conversion. |
;; any code conversion. |
3508 |
(if auto-save-p 'emacs-mule-unix |
(if auto-save-p 'emacs-mule-unix |
3509 |
coding-system-for-read))) |
(or coding-system-for-read |
3510 |
|
buffer-file-coding-system)))) |
3511 |
|
;; This force |
3512 |
|
;; after-insert-file-set-buffer-file-coding-system |
3513 |
|
;; (called from insert-file-contents) to set |
3514 |
|
;; buffer-file-coding-system to a proper value. |
3515 |
|
(kill-local-variable 'buffer-file-coding-system) |
3516 |
|
|
3517 |
;; Note that this preserves point in an intelligent way. |
;; Note that this preserves point in an intelligent way. |
3518 |
(if preserve-modes |
(if preserve-modes |
3519 |
(let ((buffer-file-format buffer-file-format)) |
(let ((buffer-file-format buffer-file-format)) |
3536 |
(run-hooks 'revert-buffer-internal-hook)) |
(run-hooks 'revert-buffer-internal-hook)) |
3537 |
t))))) |
t))))) |
3538 |
|
|
3539 |
|
(defun recover-this-file () |
3540 |
|
"Recover the visited file--get contents from its last auto-save file." |
3541 |
|
(interactive) |
3542 |
|
(recover-file buffer-file-name)) |
3543 |
|
|
3544 |
(defun recover-file (file) |
(defun recover-file (file) |
3545 |
"Visit file FILE, but get contents from its last auto-save file." |
"Visit file FILE, but get contents from its last auto-save file." |
3546 |
;; Actually putting the file name in the minibuffer should be used |
;; Actually putting the file name in the minibuffer should be used |
3596 |
(error "You set `auto-save-list-file-prefix' to disable making session files")) |
(error "You set `auto-save-list-file-prefix' to disable making session files")) |
3597 |
(let ((dir (file-name-directory auto-save-list-file-prefix))) |
(let ((dir (file-name-directory auto-save-list-file-prefix))) |
3598 |
(unless (file-directory-p dir) |
(unless (file-directory-p dir) |
3599 |
(make-directory dir t))) |
(make-directory dir t)) |
3600 |
|
(unless (directory-files dir nil |
3601 |
|
(concat "\\`" (regexp-quote |
3602 |
|
(file-name-nondirectory |
3603 |
|
auto-save-list-file-prefix))) |
3604 |
|
t) |
3605 |
|
(error "No previous sessions to recover"))) |
3606 |
(let ((ls-lisp-support-shell-wildcards t)) |
(let ((ls-lisp-support-shell-wildcards t)) |
3607 |
(dired (concat auto-save-list-file-prefix "*") |
(dired (concat auto-save-list-file-prefix "*") |
3608 |
(concat dired-listing-switches "t"))) |
(concat dired-listing-switches "t"))) |
3768 |
(setq filename (concat |
(setq filename (concat |
3769 |
(file-name-directory result) |
(file-name-directory result) |
3770 |
(subst-char-in-string |
(subst-char-in-string |
3771 |
directory-sep-char ?! |
?/ ?! |
3772 |
(replace-regexp-in-string "!" "!!" |
(replace-regexp-in-string "!" "!!" |
3773 |
filename)))) |
filename)))) |
3774 |
(setq filename result))) |
(setq filename result))) |
4026 |
PATTERN that already quotes some of the special characters." |
PATTERN that already quotes some of the special characters." |
4027 |
(save-match-data |
(save-match-data |
4028 |
(cond |
(cond |
4029 |
((memq system-type '(ms-dos windows-nt)) |
((memq system-type '(ms-dos windows-nt cygwin)) |
4030 |
;; DOS/Windows don't allow `"' in file names. So if the |
;; DOS/Windows don't allow `"' in file names. So if the |
4031 |
;; argument has quotes, we can safely assume it is already |
;; argument has quotes, we can safely assume it is already |
4032 |
;; quoted by the caller. |
;; quoted by the caller. |
4076 |
:type '(choice (string :tag "Program") (const :tag "None" nil)) |
:type '(choice (string :tag "Program") (const :tag "None" nil)) |
4077 |
:group 'dired) |
:group 'dired) |
4078 |
|
|
4079 |
(defcustom directory-free-space-args "-Pk" |
(defcustom directory-free-space-args |
4080 |
|
(if (eq system-type 'darwin) "-k" "-Pk") |
4081 |
"*Options to use when running `directory-free-space-program'." |
"*Options to use when running `directory-free-space-program'." |
4082 |
:type 'string |
:type 'string |
4083 |
:group 'dired) |
:group 'dired) |
4136 |
;; dired-insert-headerline |
;; dired-insert-headerline |
4137 |
;; dired-after-subdir-garbage (defines what a "total" line is) |
;; dired-after-subdir-garbage (defines what a "total" line is) |
4138 |
;; - variable dired-subdir-regexp |
;; - variable dired-subdir-regexp |
4139 |
|
;; - may be passed "--dired" as the first argument in SWITCHES. |
4140 |
|
;; Filename handlers might have to remove this switch if their |
4141 |
|
;; "ls" command does not support it. |
4142 |
(defun insert-directory (file switches &optional wildcard full-directory-p) |
(defun insert-directory (file switches &optional wildcard full-directory-p) |
4143 |
"Insert directory listing for FILE, formatted according to SWITCHES. |
"Insert directory listing for FILE, formatted according to SWITCHES. |
4144 |
Leaves point after the inserted text. |
Leaves point after the inserted text. |
4158 |
wildcard full-directory-p) |
wildcard full-directory-p) |
4159 |
(if (eq system-type 'vax-vms) |
(if (eq system-type 'vax-vms) |
4160 |
(vms-read-directory file switches (current-buffer)) |
(vms-read-directory file switches (current-buffer)) |
4161 |
(let (result available) |
(let (result available (beg (point))) |
4162 |
|
|
4163 |
;; Read the actual directory using `insert-directory-program'. |
;; Read the actual directory using `insert-directory-program'. |
4164 |
;; RESULT gets the status code. |
;; RESULT gets the status code. |
4165 |
(let ((coding-system-for-read |
(let* (;; We at first read by no-conversion, then after |
4166 |
(and enable-multibyte-characters |
;; putting text property `dired-filename, decode one |
4167 |
(or file-name-coding-system |
;; bunch by one to preserve that property. |
4168 |
default-file-name-coding-system))) |
(coding-system-for-read 'no-conversion) |
4169 |
;; This is to control encoding the arguments in call-process. |
;; This is to control encoding the arguments in call-process. |
4170 |
(coding-system-for-write coding-system-for-read)) |
(coding-system-for-write |
4171 |
|
(and enable-multibyte-characters |
4172 |
|
(or file-name-coding-system |
4173 |
|
default-file-name-coding-system)))) |
4174 |
(setq result |
(setq result |
4175 |
(if wildcard |
(if wildcard |
4176 |
;; Run ls in the directory part of the file pattern |
;; Run ls in the directory part of the file pattern |
4238 |
(access-file file "Reading directory") |
(access-file file "Reading directory") |
4239 |
(error "Listing directory failed but `access-file' worked"))) |
(error "Listing directory failed but `access-file' worked"))) |
4240 |
|
|
4241 |
;; Try to insert the amount of free space. |
(when (string-match "--dired\\>" switches) |
4242 |
(save-excursion |
(forward-line -2) |
4243 |
(goto-char (point-min)) |
(when (looking-at "//SUBDIRED//") |
4244 |
;; First find the line to put it on. |
(delete-region (point) (progn (forward-line 1) (point))) |
4245 |
(when (re-search-forward "^total" nil t) |
(forward-line -1)) |
4246 |
(let ((available (get-free-disk-space "."))) |
(let ((end (line-end-position))) |
4247 |
(when available |
(forward-word 1) |
4248 |
;; Replace "total" with "used", to avoid confusion. |
(forward-char 3) |
4249 |
(replace-match "total used in directory") |
(while (< (point) end) |
4250 |
(end-of-line) |
(let ((start (+ beg (read (current-buffer)))) |
4251 |
(insert " available " available)))))))))) |
(end (+ beg (read (current-buffer))))) |
4252 |
|
(if (= (char-after end) ?\n) |
4253 |
|
(put-text-property start end 'dired-filename t) |
4254 |
|
;; It seems that we can't trust ls's output as to |
4255 |
|
;; byte positions of filenames. |
4256 |
|
(put-text-property beg (point) 'dired-filename nil) |
4257 |
|
(end-of-line)))) |
4258 |
|
(goto-char end) |
4259 |
|
(beginning-of-line) |
4260 |
|
(delete-region (point) (progn (forward-line 2) (point))))) |
4261 |
|
|
4262 |
|
;; Now decode what read if necessary. |
4263 |
|
(let ((coding (or coding-system-for-read |
4264 |
|
file-name-coding-system |
4265 |
|
default-file-name-coding-system |
4266 |
|
'undecided)) |
4267 |
|
val pos) |
4268 |
|
(when (and enable-multibyte-characters |
4269 |
|
(not (memq (coding-system-base coding) |
4270 |
|
'(raw-text no-conversion)))) |
4271 |
|
;; If no coding system is specified or detection is |
4272 |
|
;; requested, detect the coding. |
4273 |
|
(if (eq (coding-system-base coding) 'undecided) |
4274 |
|
(setq coding (detect-coding-region beg (point) t))) |
4275 |
|
(if (not (eq (coding-system-base coding) 'undecided)) |
4276 |
|
(save-restriction |
4277 |
|
(narrow-to-region beg (point)) |
4278 |
|
(goto-char (point-min)) |
4279 |
|
(while (not (eobp)) |
4280 |
|
(setq pos (point) |
4281 |
|
val (get-text-property (point) 'dired-filename)) |
4282 |
|
(goto-char (next-single-property-change |
4283 |
|
(point) 'dired-filename nil (point-max))) |
4284 |
|
(decode-coding-region pos (point) coding) |
4285 |
|
(if val |
4286 |
|
(put-text-property pos (point) |
4287 |
|
'dired-filename t))))))) |
4288 |
|
|
4289 |
|
(if full-directory-p |
4290 |
|
;; Try to insert the amount of free space. |
4291 |
|
(save-excursion |
4292 |
|
(goto-char beg) |
4293 |
|
;; First find the line to put it on. |
4294 |
|
(when (re-search-forward "^ *\\(total\\)" nil t) |
4295 |
|
(let ((available (get-free-disk-space "."))) |
4296 |
|
(when available |
4297 |
|
;; Replace "total" with "used", to avoid confusion. |
4298 |
|
(replace-match "total used in directory" nil nil nil 1) |
4299 |
|
(end-of-line) |
4300 |
|
(insert " available " available))))))))))) |
4301 |
|
|
4302 |
(defun insert-directory-safely (file switches |
(defun insert-directory-safely (file switches |
4303 |
&optional wildcard full-directory-p) |
&optional wildcard full-directory-p) |
4426 |
(define-key esc-map "~" 'not-modified) |
(define-key esc-map "~" 'not-modified) |
4427 |
(define-key ctl-x-map "\C-d" 'list-directory) |
(define-key ctl-x-map "\C-d" 'list-directory) |
4428 |
(define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs) |
(define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs) |
4429 |
|
(define-key ctl-x-map "\C-q" 'toggle-read-only) |
4430 |
|
|
4431 |
(define-key ctl-x-4-map "f" 'find-file-other-window) |
(define-key ctl-x-4-map "f" 'find-file-other-window) |
4432 |
(define-key ctl-x-4-map "r" 'find-file-read-only-other-window) |
(define-key ctl-x-4-map "r" 'find-file-read-only-other-window) |