Mercurial > emacs
annotate lisp/files.el @ 2375:94e135f2d64d
(local-write-file-hooks): New variable.
(set-visited-file-name): Kill local-write-file-hooks as local var.
(basic-save-buffer): Use local-write-file-hooks.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 26 Mar 1993 07:28:20 +0000 |
parents | e5a37be50b36 |
children | 808b03396f33 |
rev | line source |
---|---|
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
1 ;;; files.el --- file input and output commands for Emacs |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
2 |
846
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
844
diff
changeset
|
3 ;; Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. |
20674ae6bf52
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
844
diff
changeset
|
4 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
5 ;; Maintainer: FSF |
337 | 6 |
7 ;; This file is part of GNU Emacs. | |
8 | |
9 ;; GNU Emacs is free software; you can redistribute it and/or modify | |
10 ;; it under the terms of the GNU General Public License as published by | |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
11 ;; the Free Software Foundation; either version 2, or (at your option) |
337 | 12 ;; any later version. |
13 | |
14 ;; GNU Emacs is distributed in the hope that it will be useful, | |
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 ;; GNU General Public License for more details. | |
18 | |
19 ;; You should have received a copy of the GNU General Public License | |
20 ;; along with GNU Emacs; see the file COPYING. If not, write to | |
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
22 | |
2307
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2108
diff
changeset
|
23 ;;; Commentary: |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2108
diff
changeset
|
24 |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2108
diff
changeset
|
25 ;; Defines most of Emacs's file- and directory-handling functions, |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2108
diff
changeset
|
26 ;; including basic file visiting, backup generation, link handling, |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2108
diff
changeset
|
27 ;; ITS-id version control, load- and write-hook handling, and the like. |
10e417efb12a
Added or corrected Commentary sections
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
2108
diff
changeset
|
28 |
807
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
29 ;;; Code: |
4f28bd14272c
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
778
diff
changeset
|
30 |
337 | 31 (defconst delete-auto-save-files t |
32 "*Non-nil means delete a buffer's auto-save file when the buffer is saved.") | |
33 | |
34 (defconst directory-abbrev-alist | |
35 nil | |
36 "*Alist of abbreviations for file directories. | |
37 A list of elements of the form (FROM . TO), each meaning to replace | |
38 FROM with TO when it appears in a directory name. This replacement is | |
39 done when setting up the default directory of a newly visited file. | |
40 *Every* FROM string should start with `^'. | |
41 | |
42 Use this feature when you have directories which you normally refer to | |
43 via absolute symbolic links. Make TO the name of the link, and FROM | |
44 the name it is linked to.") | |
45 | |
46 ;;; Turn off backup files on VMS since it has version numbers. | |
47 (defconst make-backup-files (not (eq system-type 'vax-vms)) | |
48 "*Create a backup of each file when it is saved for the first time. | |
49 This can be done by renaming the file or by copying. | |
50 | |
51 Renaming means that Emacs renames the existing file so that it is a | |
52 backup file, then writes the buffer into a new file. Any other names | |
53 that the old file had will now refer to the backup file. The new file | |
54 is owned by you and its group is defaulted. | |
55 | |
56 Copying means that Emacs copies the existing file into the backup | |
57 file, then writes the buffer on top of the existing file. Any other | |
58 names that the old file had will now refer to the new (edited) file. | |
59 The file's owner and group are unchanged. | |
60 | |
61 The choice of renaming or copying is controlled by the variables | |
62 `backup-by-copying', `backup-by-copying-when-linked' and | |
63 `backup-by-copying-when-mismatch'.") | |
64 | |
65 ;; Do this so that local variables based on the file name | |
66 ;; are not overridden by the major mode. | |
67 (defvar backup-inhibited nil | |
68 "Non-nil means don't make a backup file for this buffer.") | |
69 (put 'backup-inhibited 'permanent-local t) | |
70 | |
71 (defconst backup-by-copying nil | |
72 "*Non-nil means always use copying to create backup files. | |
73 See documentation of variable `make-backup-files'.") | |
74 | |
75 (defconst backup-by-copying-when-linked nil | |
76 "*Non-nil means use copying to create backups for files with multiple names. | |
77 This causes the alternate names to refer to the latest version as edited. | |
78 This variable is relevant only if `backup-by-copying' is nil.") | |
79 | |
80 (defconst backup-by-copying-when-mismatch nil | |
81 "*Non-nil means create backups by copying if this preserves owner or group. | |
82 Renaming may still be used (subject to control of other variables) | |
83 when it would not result in changing the owner or group of the file; | |
84 that is, for files which are owned by you and whose group matches | |
85 the default for a new file created there by you. | |
86 This variable is relevant only if `backup-by-copying' is nil.") | |
87 | |
88 (defvar backup-enable-predicate | |
89 '(lambda (name) | |
90 (or (< (length name) 5) | |
91 (not (string-equal "/tmp/" (substring name 0 5))))) | |
92 "Predicate that looks at a file name and decides whether to make backups. | |
93 Called with an absolute file name as argument, it returns t to enable backup.") | |
94 | |
95 (defconst buffer-offer-save nil | |
96 "*Non-nil in a buffer means offer to save the buffer on exit | |
97 even if the buffer is not visiting a file. | |
98 Automatically local in all buffers.") | |
99 (make-variable-buffer-local 'buffer-offer-save) | |
100 | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
101 (defconst find-file-existing-other-name nil |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
102 "*Non-nil means find a file under alternative names, in existing buffers. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
103 This means if any existing buffer is visiting the file you want |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
104 under another name, you get the existing buffer instead of a new buffer.") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
105 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
106 (defconst find-file-visit-truename nil |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
107 "*Non-nil means visit a file under its truename. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
108 The truename of a file is found by chasing all links |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
109 both at the file level and at the levels of the containing directories.") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
110 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
111 (defvar buffer-file-truename nil |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
112 "The truename of the file visited in the current buffer. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
113 This variable is automatically local in all buffers, when non-nil.") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
114 (make-variable-buffer-local 'buffer-file-truename) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
115 (put 'buffer-file-truename 'permanent-local t) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
116 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
117 (defvar buffer-file-number nil |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
118 "The device number and file number of the file visited in the current buffer. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
119 The value is a list of the form (FILENUM DEVNUM). |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
120 This pair of numbers uniquely identifies the file. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
121 If the buffer is visiting a new file, the value is nil.") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
122 (make-variable-buffer-local 'buffer-file-number) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
123 (put 'buffer-file-number 'permanent-local t) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
124 |
337 | 125 (defconst file-precious-flag nil |
126 "*Non-nil means protect against I/O errors while saving files. | |
127 Some modes set this non-nil in particular buffers.") | |
128 | |
129 (defvar version-control nil | |
130 "*Control use of version numbers for backup files. | |
131 t means make numeric backup versions unconditionally. | |
132 nil means make them for files that have some already. | |
133 never means do not make them.") | |
134 | |
135 (defvar dired-kept-versions 2 | |
136 "*When cleaning directory, number of versions to keep.") | |
137 | |
138 (defvar trim-versions-without-asking nil | |
844
bf829a2d63b4
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
819
diff
changeset
|
139 "*If t, deletes excess backup versions silently. |
bf829a2d63b4
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
819
diff
changeset
|
140 If nil, asks confirmation. Any other value prevents any trimming.") |
337 | 141 |
142 (defvar kept-old-versions 2 | |
143 "*Number of oldest versions to keep when a new numbered backup is made.") | |
144 | |
145 (defvar kept-new-versions 2 | |
146 "*Number of newest versions to keep when a new numbered backup is made. | |
147 Includes the new backup. Must be > 0") | |
148 | |
149 (defconst require-final-newline nil | |
150 "*Value of t says silently ensure a file ends in a newline when it is saved. | |
151 Non-nil but not t says ask user whether to add a newline when there isn't one. | |
152 nil means don't add newlines.") | |
153 | |
154 (defconst auto-save-default t | |
155 "*Non-nil says by default do auto-saving of every file-visiting buffer.") | |
156 | |
157 (defconst auto-save-visited-file-name nil | |
158 "*Non-nil says auto-save a buffer in the file it is visiting, when practical. | |
159 Normally auto-save files are written under other names.") | |
160 | |
161 (defconst save-abbrevs nil | |
162 "*Non-nil means save word abbrevs too when files are saved. | |
163 Loading an abbrev file sets this to t.") | |
164 | |
165 (defconst find-file-run-dired t | |
166 "*Non-nil says run dired if find-file is given the name of a directory.") | |
167 | |
1879
c9b4ece292cc
(find-file-hooks): Delete permanent-local property.
Richard M. Stallman <rms@gnu.org>
parents:
1799
diff
changeset
|
168 ;;;It is not useful to make this a local variable. |
c9b4ece292cc
(find-file-hooks): Delete permanent-local property.
Richard M. Stallman <rms@gnu.org>
parents:
1799
diff
changeset
|
169 ;;;(put 'find-file-not-found-hooks 'permanent-local t) |
337 | 170 (defvar find-file-not-found-hooks nil |
171 "List of functions to be called for `find-file' on nonexistent file. | |
172 These functions are called as soon as the error is detected. | |
173 `buffer-file-name' is already set up. | |
174 The functions are called in the order given until one of them returns non-nil.") | |
175 | |
1879
c9b4ece292cc
(find-file-hooks): Delete permanent-local property.
Richard M. Stallman <rms@gnu.org>
parents:
1799
diff
changeset
|
176 ;;;It is not useful to make this a local variable. |
c9b4ece292cc
(find-file-hooks): Delete permanent-local property.
Richard M. Stallman <rms@gnu.org>
parents:
1799
diff
changeset
|
177 ;;;(put 'find-file-hooks 'permanent-local t) |
337 | 178 (defvar find-file-hooks nil |
179 "List of functions to be called after a buffer is loaded from a file. | |
180 The buffer's local variables (if any) will have been processed before the | |
181 functions are called.") | |
182 | |
2375
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
183 ;;; In case someone does make it local. |
337 | 184 (put 'write-file-hooks 'permanent-local t) |
185 (defvar write-file-hooks nil | |
186 "List of functions to be called before writing out a buffer to a file. | |
187 If one of them returns non-nil, the file is considered already written | |
605 | 188 and the rest are not called. |
189 These hooks are considered to pertain to the visited file. | |
190 So this list is cleared if you change the visited file name. | |
2375
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
191 See also `write-contents-hooks'. |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
192 Don't make this variable buffer-local; instead, use `local-write-file-hooks'.") |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
193 |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
194 (put 'local-write-file-hooks 'permanent-local t) |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
195 (defvar local-write-file-hooks nil |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
196 "Just like `write-file-hooks', except intended for per-buffer use. |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
197 The functions in this list are called before the ones in |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
198 `write-file-hooks'.") |
605 | 199 |
200 (defvar write-contents-hooks nil | |
201 "List of functions to be called before writing out a buffer to a file. | |
202 If one of them returns non-nil, the file is considered already written | |
203 and the rest are not called. | |
204 These hooks are considered to pertain to the buffer's contents, | |
205 not to the particular visited file; thus, `set-visited-file-name' does | |
206 not clear this variable, but changing the major mode does clear it. | |
207 See also `write-file-hooks'.") | |
337 | 208 |
209 (defconst enable-local-variables t | |
210 "*Control use of local-variables lists in files you visit. | |
211 The value can be t, nil or something else. | |
212 A value of t means local-variables lists are obeyed; | |
213 nil means they are ignored; anything else means query. | |
214 | |
215 The command \\[normal-mode] always obeys local-variables lists | |
216 and ignores this variable.") | |
217 | |
1066
9b788095582b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
941
diff
changeset
|
218 (defconst enable-local-eval 'maybe |
722
0a2391511b46
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
705
diff
changeset
|
219 "*Control processing of the \"variable\" `eval' in a file's local variables. |
0a2391511b46
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
705
diff
changeset
|
220 The value can be t, nil or something else. |
0a2391511b46
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
705
diff
changeset
|
221 A value of t means obey `eval' variables; |
0a2391511b46
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
705
diff
changeset
|
222 nil means ignore them; anything else means query. |
0a2391511b46
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
705
diff
changeset
|
223 |
0a2391511b46
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
705
diff
changeset
|
224 The command \\[normal-mode] always obeys local-variables lists |
0a2391511b46
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
705
diff
changeset
|
225 and ignores this variable.") |
337 | 226 |
227 ;; Avoid losing in versions where CLASH_DETECTION is disabled. | |
228 (or (fboundp 'lock-buffer) | |
229 (fset 'lock-buffer 'ignore)) | |
230 (or (fboundp 'unlock-buffer) | |
231 (fset 'unlock-buffer 'ignore)) | |
232 | |
233 (defun pwd () | |
234 "Show the current default directory." | |
235 (interactive nil) | |
236 (message "Directory %s" default-directory)) | |
237 | |
238 (defun cd (dir) | |
239 "Make DIR become the current buffer's default directory." | |
240 (interactive "DChange default directory: ") | |
241 (setq dir (expand-file-name dir)) | |
242 (if (not (eq system-type 'vax-vms)) | |
243 (setq dir (file-name-as-directory dir))) | |
244 (if (not (file-directory-p dir)) | |
245 (error "%s is not a directory" dir) | |
246 (if (file-executable-p dir) | |
247 (setq default-directory dir) | |
248 (error "Cannot cd to %s: Permission denied" dir))) | |
423 | 249 ;; We used to call pwd at this point. That's not terribly helpful |
250 ;; when we're invoking cd interactively, and the new cmushell-based | |
251 ;; shell has its own (better) facilities for this. | |
252 ) | |
337 | 253 |
254 (defun load-file (file) | |
255 "Load the Lisp file named FILE." | |
256 (interactive "fLoad file: ") | |
257 (load (expand-file-name file) nil nil t)) | |
258 | |
259 (defun load-library (library) | |
260 "Load the library named LIBRARY. | |
261 This is an interface to the function `load'." | |
262 (interactive "sLoad library: ") | |
263 (load library)) | |
1134
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
264 |
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
265 ;; OTHER is the other file to be compared. |
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
266 (defun file-local-copy (file) |
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
267 "Copy the file FILE into a temporary file on this machine. |
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
268 Returns the name of the local copy, or nil, if FILE is directly |
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
269 accessible." |
1672
ecf43116a845
The find-file-name-handler function in ../src/fileio.c is now
Jim Blandy <jimb@redhat.com>
parents:
1537
diff
changeset
|
270 (let ((handler (find-file-name-handler file))) |
1134
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
271 (if handler |
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
272 (funcall handler 'file-local-copy file) |
05c961416bb5
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1109
diff
changeset
|
273 nil))) |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
274 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
275 (defun file-truename (filename) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
276 "Return the truename of FILENAME, which should be absolute. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
277 The truename of a file name is found by chasing symbolic links |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
278 both at the level of the file and at the level of the directories |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
279 containing it, until no links are left at any level." |
1406
62dae4a3c7fb
* files.el (file-truename): The variable ~ should be considered an
Jim Blandy <jimb@redhat.com>
parents:
1395
diff
changeset
|
280 (if (string= filename "~") |
62dae4a3c7fb
* files.el (file-truename): The variable ~ should be considered an
Jim Blandy <jimb@redhat.com>
parents:
1395
diff
changeset
|
281 (setq filename (expand-file-name filename))) |
1672
ecf43116a845
The find-file-name-handler function in ../src/fileio.c is now
Jim Blandy <jimb@redhat.com>
parents:
1537
diff
changeset
|
282 (let ((handler (find-file-name-handler filename))) |
1453
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
283 ;; For file name that has a special handler, call handler. |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
284 ;; This is so that ange-ftp can save time by doing a no-op. |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
285 (if handler |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
286 (funcall handler 'file-truename filename) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
287 (let ((dir (file-name-directory filename)) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
288 target dirfile) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
289 ;; Get the truename of the directory. |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
290 (setq dirfile (directory-file-name dir)) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
291 ;; If these are equal, we have the (or a) root directory. |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
292 (or (string= dir dirfile) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
293 (setq dir (file-name-as-directory (file-truename dirfile)))) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
294 ;; Put it back on the file name. |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
295 (setq filename (concat dir (file-name-nondirectory filename))) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
296 ;; Is the file name the name of a link? |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
297 (setq target (file-symlink-p filename)) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
298 (if target |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
299 ;; Yes => chase that link, then start all over |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
300 ;; since the link may point to a directory name that uses links. |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
301 (file-truename (expand-file-name target dir)) |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
302 ;; No, we are done! |
fb2d16111e4b
(file-truename): Check for root by seeing if
Richard M. Stallman <rms@gnu.org>
parents:
1406
diff
changeset
|
303 filename))))) |
1672
ecf43116a845
The find-file-name-handler function in ../src/fileio.c is now
Jim Blandy <jimb@redhat.com>
parents:
1537
diff
changeset
|
304 |
337 | 305 |
306 (defun switch-to-buffer-other-window (buffer) | |
307 "Select buffer BUFFER in another window." | |
308 (interactive "BSwitch to buffer in other window: ") | |
309 (let ((pop-up-windows t)) | |
310 (pop-to-buffer buffer t))) | |
311 | |
778 | 312 (defun switch-to-buffer-other-frame (buffer) |
313 "Switch to buffer BUFFER in another frame." | |
314 (interactive "BSwitch to buffer in other frame: ") | |
315 (let ((pop-up-frames t)) | |
1799
8e52d9366e6c
(switch-to-buffer-other-frame): Pass t to pop-to-buffer.
Roland McGrath <roland@gnu.org>
parents:
1725
diff
changeset
|
316 (pop-to-buffer buffer t))) |
423 | 317 |
337 | 318 (defun find-file (filename) |
319 "Edit file FILENAME. | |
320 Switch to a buffer visiting file FILENAME, | |
321 creating one if none already exists." | |
322 (interactive "FFind file: ") | |
323 (switch-to-buffer (find-file-noselect filename))) | |
324 | |
325 (defun find-file-other-window (filename) | |
326 "Edit file FILENAME, in another window. | |
327 May create a new window, or reuse an existing one. | |
328 See the function `display-buffer'." | |
329 (interactive "FFind file in other window: ") | |
330 (switch-to-buffer-other-window (find-file-noselect filename))) | |
331 | |
778 | 332 (defun find-file-other-frame (filename) |
333 "Edit file FILENAME, in another frame. | |
334 May create a new frame, or reuse an existing one. | |
423 | 335 See the function `display-buffer'." |
778 | 336 (interactive "FFind file in other frame: ") |
337 (switch-to-buffer-other-frame (find-file-noselect filename))) | |
423 | 338 |
337 | 339 (defun find-file-read-only (filename) |
340 "Edit file FILENAME but don't allow changes. | |
341 Like \\[find-file] but marks buffer as read-only. | |
342 Use \\[toggle-read-only] to permit editing." | |
343 (interactive "fFind file read-only: ") | |
344 (find-file filename) | |
345 (setq buffer-read-only t)) | |
346 | |
347 (defun find-file-read-only-other-window (filename) | |
348 "Edit file FILENAME in another window but don't allow changes. | |
349 Like \\[find-file-other-window] but marks buffer as read-only. | |
350 Use \\[toggle-read-only] to permit editing." | |
351 (interactive "fFind file read-only other window: ") | |
352 (find-file filename) | |
353 (setq buffer-read-only t)) | |
354 | |
778 | 355 (defun find-file-read-only-other-frame (filename) |
356 "Edit file FILENAME in another frame but don't allow changes. | |
357 Like \\[find-file-other-frame] but marks buffer as read-only. | |
423 | 358 Use \\[toggle-read-only] to permit editing." |
778 | 359 (interactive "fFind file read-only other frame: ") |
360 (find-file-other-frame filename) | |
423 | 361 (setq buffer-read-only t)) |
362 | |
337 | 363 (defun find-alternate-file (filename) |
364 "Find file FILENAME, select its buffer, kill previous buffer. | |
365 If the current buffer now contains an empty file that you just visited | |
366 \(presumably by mistake), use this command to visit the file you really want." | |
367 (interactive | |
368 (let ((file buffer-file-name) | |
369 (file-name nil) | |
370 (file-dir nil)) | |
371 (and file | |
372 (setq file-name (file-name-nondirectory file) | |
373 file-dir (file-name-directory file))) | |
941 | 374 (list (read-file-name |
375 "Find alternate file: " file-dir nil nil file-name)))) | |
337 | 376 (and (buffer-modified-p) |
377 ;; (not buffer-read-only) | |
378 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? " | |
379 (buffer-name)))) | |
380 (error "Aborted")) | |
381 (let ((obuf (current-buffer)) | |
382 (ofile buffer-file-name) | |
383 (oname (buffer-name))) | |
384 (rename-buffer " **lose**") | |
385 (setq buffer-file-name nil) | |
386 (unwind-protect | |
387 (progn | |
388 (unlock-buffer) | |
389 (find-file filename)) | |
390 (cond ((eq obuf (current-buffer)) | |
391 (setq buffer-file-name ofile) | |
392 (lock-buffer) | |
393 (rename-buffer oname)))) | |
394 (or (eq (current-buffer) obuf) | |
395 (kill-buffer obuf)))) | |
396 | |
397 (defun create-file-buffer (filename) | |
398 "Create a suitably named buffer for visiting FILENAME, and return it. | |
399 FILENAME (sans directory) is used unchanged if that name is free; | |
400 otherwise a string <2> or <3> or ... is appended to get an unused name." | |
401 (let ((lastname (file-name-nondirectory filename))) | |
402 (if (string= lastname "") | |
403 (setq lastname filename)) | |
404 (generate-new-buffer lastname))) | |
405 | |
423 | 406 (defun generate-new-buffer (name) |
407 "Create and return a buffer with a name based on NAME. | |
1461 | 408 Choose the buffer's name using `generate-new-buffer-name'." |
423 | 409 (get-buffer-create (generate-new-buffer-name name))) |
410 | |
817 | 411 (defconst automount-dir-prefix "^/tmp_mnt/" |
412 "Regexp to match the automounter prefix in a directory name.") | |
413 | |
1725
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
414 (defvar abbreviated-home-dir nil |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
415 "The the user's homedir abbreviated according to `directory-abbrev-list'.") |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
416 |
423 | 417 (defun abbreviate-file-name (filename) |
1461 | 418 "Return a version of FILENAME shortened using `directory-abbrev-alist'. |
423 | 419 This also substitutes \"~\" for the user's home directory. |
1461 | 420 Type \\[describe-variable] directory-abbrev-alist RET for more information." |
817 | 421 ;; Get rid of the prefixes added by the automounter. |
422 (if (and (string-match automount-dir-prefix filename) | |
423 (file-exists-p (file-name-directory | |
424 (substring filename (1- (match-end 0)))))) | |
425 (setq filename (substring filename (1- (match-end 0))))) | |
423 | 426 (let ((tail directory-abbrev-alist)) |
1725
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
427 ;; If any elt of directory-abbrev-alist matches this name, |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
428 ;; abbreviate accordingly. |
423 | 429 (while tail |
430 (if (string-match (car (car tail)) filename) | |
431 (setq filename | |
432 (concat (cdr (car tail)) (substring filename (match-end 0))))) | |
433 (setq tail (cdr tail))) | |
1725
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
434 ;; Compute and save the abbreviated homedir name. |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
435 ;; We defer computing this until the first time it's needed, to |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
436 ;; give time for directory-abbrev-alist to be set properly. |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
437 (or abbreviated-home-dir |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
438 (setq abbreviated-home-dir |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
439 (let ((abbreviated-home-dir "$foo")) |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
440 (concat "^" (abbreviate-file-name (expand-file-name "~")))))) |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
441 ;; If FILENAME starts with the abbreviated homedir, |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
442 ;; make it start with `~' instead. |
ac890f97e78b
(abbreviated-home-dir): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1672
diff
changeset
|
443 (if (string-match abbreviated-home-dir filename) |
423 | 444 (setq filename |
445 (concat "~" (substring filename (match-end 0))))) | |
446 filename)) | |
447 | |
337 | 448 (defun find-file-noselect (filename &optional nowarn) |
449 "Read file FILENAME into a buffer and return the buffer. | |
450 If a buffer exists visiting FILENAME, return that one, but | |
451 verify that the file has not changed since visited or saved. | |
452 The buffer is not selected, just returned to the caller." | |
817 | 453 (setq filename |
454 (abbreviate-file-name | |
455 (expand-file-name filename))) | |
337 | 456 (if (file-directory-p filename) |
457 (if find-file-run-dired | |
458 (dired-noselect filename) | |
459 (error "%s is a directory." filename)) | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
460 (let* ((buf (get-file-buffer filename)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
461 (truename (abbreviate-file-name (file-truename filename))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
462 (number (nthcdr 10 (file-attributes truename))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
463 ;; Find any buffer for a file which has same truename. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
464 (same-truename |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
465 (or buf ; Shortcut |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
466 (let (found |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
467 (list (buffer-list))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
468 (while (and (not found) list) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
469 (save-excursion |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
470 (set-buffer (car list)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
471 (if (string= buffer-file-truename truename) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
472 (setq found (car list)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
473 (setq list (cdr list))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
474 found))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
475 (same-number |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
476 (or buf ; Shortcut |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
477 (and number |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
478 (let (found |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
479 (list (buffer-list))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
480 (while (and (not found) list) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
481 (save-excursion |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
482 (set-buffer (car list)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
483 (if (equal buffer-file-number number) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
484 (setq found (car list)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
485 (setq list (cdr list))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
486 found)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
487 error) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
488 ;; Let user know if there is a buffer with the same truename. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
489 (if (and (not buf) same-truename (not nowarn)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
490 (message "%s and %s are the same file (%s)" |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
491 filename (buffer-file-name same-truename) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
492 truename) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
493 (if (and (not buf) same-number (not nowarn)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
494 (message "%s and %s are the same file" |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
495 filename (buffer-file-name same-number)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
496 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
497 ;; Optionally also find that buffer. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
498 (if (or find-file-existing-other-name find-file-visit-truename) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
499 (setq buf (or same-truename same-number))) |
337 | 500 (if buf |
501 (or nowarn | |
502 (verify-visited-file-modtime buf) | |
503 (cond ((not (file-exists-p filename)) | |
504 (error "File %s no longer exists!" filename)) | |
505 ((yes-or-no-p | |
506 (format | |
507 (if (buffer-modified-p buf) | |
508 "File %s changed on disk. Discard your edits? " | |
509 "File %s changed on disk. Read the new version? ") | |
510 (file-name-nondirectory filename))) | |
511 (save-excursion | |
512 (set-buffer buf) | |
513 (revert-buffer t t))))) | |
514 (save-excursion | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
515 ;;; The truename stuff makes this obsolete. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
516 ;;; (let* ((link-name (car (file-attributes filename))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
517 ;;; (linked-buf (and (stringp link-name) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
518 ;;; (get-file-buffer link-name)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
519 ;;; (if (bufferp linked-buf) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
520 ;;; (message "Symbolic link to file in buffer %s" |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
521 ;;; (buffer-name linked-buf)))) |
337 | 522 (setq buf (create-file-buffer filename)) |
523 (set-buffer buf) | |
524 (erase-buffer) | |
525 (condition-case () | |
526 (insert-file-contents filename t) | |
527 (file-error | |
528 (setq error t) | |
529 ;; Run find-file-not-found-hooks until one returns non-nil. | |
530 (let ((hooks find-file-not-found-hooks)) | |
531 (while (and hooks | |
532 (not (funcall (car hooks)))) | |
533 (setq hooks (cdr hooks)))))) | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
534 ;; Find the file's truename, and maybe use that as visited name. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
535 (setq buffer-file-truename (abbreviate-file-name truename)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
536 (setq buffer-file-number number) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
537 (if find-file-visit-truename (setq filename buffer-file-truename)) |
337 | 538 ;; Set buffer's default directory to that of the file. |
539 (setq default-directory (file-name-directory filename)) | |
540 ;; Turn off backup files for certain file names. Since | |
541 ;; this is a permanent local, the major mode won't eliminate it. | |
542 (and (not (funcall backup-enable-predicate buffer-file-name)) | |
543 (progn | |
544 (make-local-variable 'backup-inhibited) | |
545 (setq backup-inhibited t))) | |
546 (after-find-file error (not nowarn)))) | |
547 buf))) | |
548 | |
1212
de70f50101c0
(after-find-file): New arg NOAUTO.
Richard M. Stallman <rms@gnu.org>
parents:
1182
diff
changeset
|
549 (defun after-find-file (&optional error warn noauto) |
337 | 550 "Called after finding a file and by the default revert function. |
551 Sets buffer mode, parses local variables. | |
1212
de70f50101c0
(after-find-file): New arg NOAUTO.
Richard M. Stallman <rms@gnu.org>
parents:
1182
diff
changeset
|
552 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an |
337 | 553 error in reading the file. WARN non-nil means warn if there |
554 exists an auto-save file more recent than the visited file. | |
1212
de70f50101c0
(after-find-file): New arg NOAUTO.
Richard M. Stallman <rms@gnu.org>
parents:
1182
diff
changeset
|
555 NOAUTO means don't mess with auto-save mode. |
337 | 556 Finishes by calling the functions in `find-file-hooks'." |
557 (setq buffer-read-only (not (file-writable-p buffer-file-name))) | |
558 (if noninteractive | |
559 nil | |
560 (let* (not-serious | |
561 (msg | |
562 (cond ((and error (file-attributes buffer-file-name)) | |
563 (setq buffer-read-only t) | |
564 "File exists, but is read-protected.") | |
565 ((not buffer-read-only) | |
566 (if (and warn | |
567 (file-newer-than-file-p (make-auto-save-file-name) | |
568 buffer-file-name)) | |
569 "Auto save file is newer; consider M-x recover-file" | |
570 (setq not-serious t) | |
571 (if error "(New file)" nil))) | |
572 ((not error) | |
573 (setq not-serious t) | |
574 "Note: file is write protected") | |
575 ((file-attributes (directory-file-name default-directory)) | |
576 "File not found and directory write-protected") | |
1537
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
577 ((file-exists-p (file-name-directory buffer-file-name)) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
578 (setq buffer-read-only nil)) |
337 | 579 (t |
423 | 580 (setq buffer-read-only nil) |
1537
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
581 (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name)))) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
582 "Use M-x make-dir RET RET to create the directory" |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
583 "Use C-u M-x make-dir RET RET to create directory and its parents"))))) |
337 | 584 (if msg |
585 (progn | |
586 (message msg) | |
587 (or not-serious (sit-for 1 nil t))))) | |
1212
de70f50101c0
(after-find-file): New arg NOAUTO.
Richard M. Stallman <rms@gnu.org>
parents:
1182
diff
changeset
|
588 (if (and auto-save-default (not noauto)) |
337 | 589 (auto-save-mode t))) |
590 (normal-mode t) | |
591 (mapcar 'funcall find-file-hooks)) | |
592 | |
593 (defun normal-mode (&optional find-file) | |
594 "Choose the major mode for this buffer automatically. | |
595 Also sets up any specified local variables of the file. | |
596 Uses the visited file name, the -*- line, and the local variables spec. | |
597 | |
598 This function is called automatically from `find-file'. In that case, | |
599 we may set up specified local variables depending on the value of | |
600 `enable-local-variables': if it is t, we do; if it is nil, we don't; | |
601 otherwise, we query. `enable-local-variables' is ignored if you | |
602 run `normal-mode' explicitly." | |
603 (interactive) | |
604 (or find-file (funcall (or default-major-mode 'fundamental-mode))) | |
605 (condition-case err | |
606 (set-auto-mode) | |
607 (error (message "File mode specification error: %s" | |
608 (prin1-to-string err)))) | |
609 (condition-case err | |
566 | 610 (let ((enable-local-variables (or (not find-file) |
611 enable-local-variables))) | |
612 (hack-local-variables)) | |
337 | 613 (error (message "File local-variables error: %s" |
614 (prin1-to-string err))))) | |
615 | |
566 | 616 (defvar auto-mode-alist (mapcar 'purecopy |
617 '(("\\.text\\'" . text-mode) | |
618 ("\\.c\\'" . c-mode) | |
619 ("\\.h\\'" . c-mode) | |
620 ("\\.tex\\'" . TeX-mode) | |
621 ("\\.ltx\\'" . LaTeX-mode) | |
622 ("\\.el\\'" . emacs-lisp-mode) | |
623 ("\\.mm\\'" . nroff-mode) | |
624 ("\\.me\\'" . nroff-mode) | |
625 ("\\.scm\\'" . scheme-mode) | |
626 ("\\.l\\'" . lisp-mode) | |
627 ("\\.lisp\\'" . lisp-mode) | |
628 ("\\.f\\'" . fortran-mode) | |
629 ("\\.for\\'" . fortran-mode) | |
630 ("\\.mss\\'" . scribe-mode) | |
631 ("\\.pl\\'" . prolog-mode) | |
632 ("\\.cc\\'" . c++-mode) | |
633 ("\\.C\\'" . c++-mode) | |
634 ;;; Less common extensions come here | |
635 ;;; so more common ones above are found faster. | |
636 ("\\.s\\'" . asm-mode) | |
637 ("ChangeLog\\'" . change-log-mode) | |
1090
676310a31777
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1066
diff
changeset
|
638 ("ChangeLog.[0-9]+\\'" . change-log-mode) |
566 | 639 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode) |
1090
676310a31777
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1066
diff
changeset
|
640 ;; The following should come after the ChangeLog pattern |
676310a31777
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1066
diff
changeset
|
641 ;; for the sake of ChangeLog.1, etc. |
676310a31777
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1066
diff
changeset
|
642 ("\\.[12345678]\\'" . nroff-mode) |
566 | 643 ("\\.TeX\\'" . TeX-mode) |
644 ("\\.sty\\'" . LaTeX-mode) | |
645 ("\\.bbl\\'" . LaTeX-mode) | |
646 ("\\.bib\\'" . bibtex-mode) | |
647 ("\\.article\\'" . text-mode) | |
648 ("\\.letter\\'" . text-mode) | |
649 ("\\.texinfo\\'" . texinfo-mode) | |
819
5bbabfcef929
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
817
diff
changeset
|
650 ("\\.texi\\'" . texinfo-mode) |
566 | 651 ("\\.lsp\\'" . lisp-mode) |
652 ("\\.awk\\'" . awk-mode) | |
653 ("\\.prolog\\'" . prolog-mode) | |
654 ;; Mailer puts message to be edited in | |
655 ;; /tmp/Re.... or Message | |
656 ("^/tmp/Re" . text-mode) | |
657 ("/Message[0-9]*\\'" . text-mode) | |
658 ;; some news reader is reported to use this | |
659 ("^/tmp/fol/" . text-mode) | |
660 ("\\.y\\'" . c-mode) | |
661 ("\\.oak\\'" . scheme-mode) | |
662 ("\\.scm.[0-9]*\\'" . scheme-mode) | |
663 ;; .emacs following a directory delimiter | |
664 ;; in either Unix or VMS syntax. | |
665 ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode) | |
666 ("\\.ml\\'" . lisp-mode))) | |
667 "\ | |
668 Alist of filename patterns vs corresponding major mode functions. | |
669 Each element looks like (REGEXP . FUNCTION). | |
670 Visiting a file whose name matches REGEXP causes FUNCTION to be called.") | |
671 | |
337 | 672 (defun set-auto-mode () |
673 "Select major mode appropriate for current buffer. | |
566 | 674 This checks for a -*- mode tag in the buffer's text, or |
675 compares the filename against the entries in auto-mode-alist. It does | |
676 not check for the \"mode:\" local variable in the Local Variables | |
677 section of the file; for that, use `hack-local-variables'. | |
678 | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
679 If `enable-local-variables' is nil, this function does not check for a |
566 | 680 -*- mode tag." |
337 | 681 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*- |
682 (let (beg end mode) | |
683 (save-excursion | |
684 (goto-char (point-min)) | |
685 (skip-chars-forward " \t\n") | |
566 | 686 (if (and enable-local-variables |
687 (search-forward "-*-" (save-excursion (end-of-line) (point)) t) | |
337 | 688 (progn |
689 (skip-chars-forward " \t") | |
690 (setq beg (point)) | |
566 | 691 (search-forward "-*-" |
692 (save-excursion (end-of-line) (point)) | |
693 t)) | |
337 | 694 (progn |
695 (forward-char -3) | |
696 (skip-chars-backward " \t") | |
697 (setq end (point)) | |
698 (goto-char beg) | |
699 (if (search-forward ":" end t) | |
700 (progn | |
701 (goto-char beg) | |
702 (if (let ((case-fold-search t)) | |
703 (search-forward "mode:" end t)) | |
704 (progn | |
705 (skip-chars-forward " \t") | |
706 (setq beg (point)) | |
707 (if (search-forward ";" end t) | |
708 (forward-char -1) | |
709 (goto-char end)) | |
710 (skip-chars-backward " \t") | |
711 (setq mode (buffer-substring beg (point)))))) | |
712 (setq mode (buffer-substring beg end))))) | |
713 (setq mode (intern (concat (downcase mode) "-mode"))) | |
714 (let ((alist auto-mode-alist) | |
715 (name buffer-file-name)) | |
716 (let ((case-fold-search (eq system-type 'vax-vms))) | |
717 ;; Remove backup-suffixes from file name. | |
718 (setq name (file-name-sans-versions name)) | |
719 ;; Find first matching alist entry. | |
720 (while (and (not mode) alist) | |
721 (if (string-match (car (car alist)) name) | |
722 (setq mode (cdr (car alist)))) | |
723 (setq alist (cdr alist))))))) | |
724 (if mode (funcall mode)))) | |
725 | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
726 (defun hack-local-variables-prop-line () |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
727 ;; Set local variables specified in the -*- line. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
728 ;; Returns t if mode was set. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
729 (save-excursion |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
730 (goto-char (point-min)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
731 (skip-chars-forward " \t\n\r") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
732 (let ((result '()) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
733 (end (save-excursion (end-of-line) (point))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
734 mode-p) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
735 ;; Parse the -*- line into the `result' alist. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
736 (cond ((not (search-forward "-*-" end t)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
737 ;; doesn't have one. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
738 nil) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
739 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
740 ;; Simple form: "-*- MODENAME -*-". |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
741 (setq result |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
742 (list (cons 'mode |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
743 (intern (buffer-substring |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
744 (match-beginning 1) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
745 (match-end 1))))))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
746 (t |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
747 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-' |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
748 ;; (last ";" is optional). |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
749 (save-excursion |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
750 (if (search-forward "-*-" end t) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
751 (setq end (- (point) 3)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
752 (error "-*- not terminated before end of line"))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
753 (while (< (point) end) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
754 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
755 (error "malformed -*- line")) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
756 (goto-char (match-end 0)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
757 (let ((key (intern (downcase (buffer-substring |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
758 (match-beginning 1) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
759 (match-end 1))))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
760 (val (save-restriction |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
761 (narrow-to-region (point) end) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
762 (read (current-buffer))))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
763 (setq result (cons (cons key val) result)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
764 (skip-chars-forward " \t;"))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
765 (setq result (nreverse result)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
766 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
767 ;; Mode is magic. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
768 (let (mode) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
769 (while (setq mode (assq 'mode result)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
770 (setq mode-p t result (delq mode result)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
771 (funcall (intern (concat (downcase (symbol-name (cdr mode))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
772 "-mode"))))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
773 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
774 (if (and result |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
775 (or (eq enable-local-variables t) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
776 (and enable-local-variables |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
777 (save-window-excursion |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
778 (switch-to-buffer (current-buffer)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
779 (y-or-n-p (format "Set local variables as specified in -*- line of %s? " |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
780 (file-name-nondirectory buffer-file-name))))))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
781 (while result |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
782 (let ((key (car (car result))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
783 (val (cdr (car result)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
784 ;; 'mode has already been removed from this list. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
785 (hack-one-local-variable key val)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
786 (setq result (cdr result)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
787 mode-p))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
788 |
566 | 789 (defun hack-local-variables () |
1371 | 790 "Parse and put into effect this buffer's local variables spec." |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
791 (hack-local-variables-prop-line) |
337 | 792 ;; Look for "Local variables:" line in last page. |
793 (save-excursion | |
794 (goto-char (point-max)) | |
795 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move) | |
796 (if (let ((case-fold-search t)) | |
797 (and (search-forward "Local Variables:" nil t) | |
566 | 798 (or (eq enable-local-variables t) |
337 | 799 (and enable-local-variables |
800 (save-window-excursion | |
801 (switch-to-buffer (current-buffer)) | |
802 (save-excursion | |
803 (beginning-of-line) | |
804 (set-window-start (selected-window) (point))) | |
805 (y-or-n-p (format "Set local variables as specified at end of %s? " | |
806 (file-name-nondirectory buffer-file-name)))))))) | |
807 (let ((continue t) | |
1066
9b788095582b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
941
diff
changeset
|
808 prefix prefixlen suffix beg |
9b788095582b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
941
diff
changeset
|
809 (enable-local-eval enable-local-eval)) |
337 | 810 ;; The prefix is what comes before "local variables:" in its line. |
811 ;; The suffix is what comes after "local variables:" in its line. | |
812 (skip-chars-forward " \t") | |
813 (or (eolp) | |
814 (setq suffix (buffer-substring (point) | |
815 (progn (end-of-line) (point))))) | |
816 (goto-char (match-beginning 0)) | |
817 (or (bolp) | |
818 (setq prefix | |
819 (buffer-substring (point) | |
820 (progn (beginning-of-line) (point))))) | |
566 | 821 |
337 | 822 (if prefix (setq prefixlen (length prefix) |
823 prefix (regexp-quote prefix))) | |
824 (if suffix (setq suffix (concat (regexp-quote suffix) "$"))) | |
825 (while continue | |
826 ;; Look at next local variable spec. | |
827 (if selective-display (re-search-forward "[\n\C-m]") | |
828 (forward-line 1)) | |
829 ;; Skip the prefix, if any. | |
830 (if prefix | |
831 (if (looking-at prefix) | |
832 (forward-char prefixlen) | |
833 (error "Local variables entry is missing the prefix"))) | |
834 ;; Find the variable name; strip whitespace. | |
835 (skip-chars-forward " \t") | |
836 (setq beg (point)) | |
837 (skip-chars-forward "^:\n") | |
838 (if (eolp) (error "Missing colon in local variables entry")) | |
839 (skip-chars-backward " \t") | |
840 (let* ((str (buffer-substring beg (point))) | |
841 (var (read str)) | |
842 val) | |
843 ;; Setting variable named "end" means end of list. | |
844 (if (string-equal (downcase str) "end") | |
845 (setq continue nil) | |
846 ;; Otherwise read the variable value. | |
847 (skip-chars-forward "^:") | |
848 (forward-char 1) | |
849 (setq val (read (current-buffer))) | |
850 (skip-chars-backward "\n") | |
851 (skip-chars-forward " \t") | |
852 (or (if suffix (looking-at suffix) (eolp)) | |
853 (error "Local variables entry is terminated incorrectly")) | |
854 ;; Set the variable. "Variables" mode and eval are funny. | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
855 (hack-one-local-variable var val)))))))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
856 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
857 (defconst ignored-local-variables |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
858 '(enable-local-eval) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
859 "Variables to be ignored in a file's local variable spec.") |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
860 |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
861 ;; "Set" one variable in a local variables spec. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
862 ;; A few variable names are treated specially. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
863 (defun hack-one-local-variable (var val) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
864 (cond ((eq var 'mode) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
865 (funcall (intern (concat (downcase (symbol-name val)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
866 "-mode")))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
867 ((memq var ignored-local-variables) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
868 nil) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
869 ;; "Setting" eval means either eval it or do nothing. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
870 ((eq var 'eval) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
871 (if (and (not (string= (user-login-name) "root")) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
872 (or (eq enable-local-eval t) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
873 (and enable-local-eval |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
874 (save-window-excursion |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
875 (switch-to-buffer (current-buffer)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
876 (save-excursion |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
877 (beginning-of-line) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
878 (set-window-start (selected-window) (point))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
879 (setq enable-local-eval |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
880 (y-or-n-p (format "Process `eval' local variable in file %s? " |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
881 (file-name-nondirectory buffer-file-name)))))))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
882 (save-excursion (eval val)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
883 (message "Ignoring `eval:' in file's local variables"))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
884 ;; Ordinary variable, really set it. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
885 (t (make-local-variable var) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
886 (set var val)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
887 |
337 | 888 |
889 (defun set-visited-file-name (filename) | |
890 "Change name of file visited in current buffer to FILENAME. | |
891 The next time the buffer is saved it will go in the newly specified file. | |
892 nil or empty string as argument means make buffer not be visiting any file. | |
893 Remember to delete the initial contents of the minibuffer | |
894 if you wish to pass an empty string as the argument." | |
895 (interactive "FSet visited file name: ") | |
896 (if filename | |
897 (setq filename | |
898 (if (string-equal filename "") | |
899 nil | |
900 (expand-file-name filename)))) | |
901 (or (equal filename buffer-file-name) | |
902 (null filename) | |
903 (progn | |
904 (lock-buffer filename) | |
905 (unlock-buffer))) | |
906 (setq buffer-file-name filename) | |
907 (if filename ; make buffer name reflect filename. | |
423 | 908 (let ((new-name (file-name-nondirectory buffer-file-name))) |
337 | 909 (if (string= new-name "") |
910 (error "Empty file name")) | |
911 (if (eq system-type 'vax-vms) | |
912 (setq new-name (downcase new-name))) | |
913 (setq default-directory (file-name-directory buffer-file-name)) | |
423 | 914 (rename-buffer new-name t))) |
337 | 915 (setq buffer-backed-up nil) |
916 (clear-visited-file-modtime) | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
917 (if filename |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
918 (progn |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
919 (setq buffer-file-truename |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
920 (abbreviate-file-name (file-truename buffer-file-name))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
921 (if find-file-visit-truename |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
922 (setq buffer-file-name buffer-file-truename)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
923 (setq buffer-file-number (nth 10 (file-attributes buffer-file-name)))) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
924 (setq buffer-file-truename nil buffer-file-number nil)) |
337 | 925 ;; write-file-hooks is normally used for things like ftp-find-file |
926 ;; that visit things that are not local files as if they were files. | |
927 ;; Changing to visit an ordinary local file instead should flush the hook. | |
928 (kill-local-variable 'write-file-hooks) | |
2375
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
929 (kill-local-variable 'local-write-file-hooks) |
337 | 930 (kill-local-variable 'revert-buffer-function) |
931 (kill-local-variable 'backup-inhibited) | |
932 ;; Turn off backup files for certain file names. | |
933 ;; Since this is a permanent local, the major mode won't eliminate it. | |
934 (and (not (funcall backup-enable-predicate buffer-file-name)) | |
935 (progn | |
936 (make-local-variable 'backup-inhibited) | |
937 (setq backup-inhibited t))) | |
938 ;; If auto-save was not already on, turn it on if appropriate. | |
939 (if (not buffer-auto-save-file-name) | |
1471
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
940 (auto-save-mode (and buffer-file-name auto-save-default)) |
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
941 ;; If auto save is on, start using a new name. |
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
942 ;; We deliberately don't rename or delete the old auto save |
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
943 ;; for the old visited file name. This is because perhaps |
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
944 ;; the user wants to save the new state and then compare with the |
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
945 ;; previous state from the auto save file. |
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
946 (setq buffer-auto-save-file-name |
2e29ad2afc42
(set-visited-file-name): Change the auto save file name.
Richard M. Stallman <rms@gnu.org>
parents:
1461
diff
changeset
|
947 (make-auto-save-file-name))) |
337 | 948 (if buffer-file-name |
949 (set-buffer-modified-p t))) | |
950 | |
951 (defun write-file (filename) | |
952 "Write current buffer into file FILENAME. | |
2003
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
953 Makes buffer visit that file, and marks it not modified. |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
954 If the buffer is already visiting a file, you can specify |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
955 a directory name as FILENAME, to write a file of the same |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
956 old name in that directory." |
337 | 957 ;; (interactive "FWrite file: ") |
958 (interactive | |
959 (list (if buffer-file-name | |
960 (read-file-name "Write file: " | |
961 nil nil nil nil) | |
962 (read-file-name "Write file: " | |
963 (cdr (assq 'default-directory | |
964 (buffer-local-variables))) | |
965 nil nil (buffer-name))))) | |
966 (or (null filename) (string-equal filename "") | |
2003
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
967 (progn |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
968 ;; If arg is just a directory, |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
969 ;; use same file name, but in that directory. |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
970 (if (and (file-directory-p filename) buffer-file-name) |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
971 (setq filename (concat (file-name-as-directory filename) |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
972 (file-name-nondirectory buffer-file-name)))) |
bff31af6c721
(write-file): Handle directory name as arg.
Richard M. Stallman <rms@gnu.org>
parents:
1905
diff
changeset
|
973 (set-visited-file-name filename))) |
337 | 974 (set-buffer-modified-p t) |
975 (save-buffer)) | |
976 | |
977 (defun backup-buffer () | |
978 "Make a backup of the disk file visited by the current buffer, if appropriate. | |
979 This is normally done before saving the buffer the first time. | |
980 If the value is non-nil, it is the result of `file-modes' on the original | |
981 file; this means that the caller, after saving the buffer, should change | |
982 the modes of the new file to agree with the old modes." | |
983 (if (and make-backup-files (not backup-inhibited) | |
984 (not buffer-backed-up) | |
985 (file-exists-p buffer-file-name) | |
986 (memq (aref (elt (file-attributes buffer-file-name) 8) 0) | |
987 '(?- ?l))) | |
988 (let ((real-file-name buffer-file-name) | |
989 backup-info backupname targets setmodes) | |
990 ;; If specified name is a symbolic link, chase it to the target. | |
991 ;; Thus we make the backups in the directory where the real file is. | |
992 (while (let ((tem (file-symlink-p real-file-name))) | |
993 (if tem | |
994 (setq real-file-name | |
995 (expand-file-name tem | |
996 (file-name-directory real-file-name)))) | |
997 tem)) | |
998 (setq backup-info (find-backup-file-name real-file-name) | |
999 backupname (car backup-info) | |
1000 targets (cdr backup-info)) | |
1001 ;;; (if (file-directory-p buffer-file-name) | |
1002 ;;; (error "Cannot save buffer in directory %s" buffer-file-name)) | |
1003 (condition-case () | |
1004 (let ((delete-old-versions | |
1005 ;; If have old versions to maybe delete, | |
1006 ;; ask the user to confirm now, before doing anything. | |
1007 ;; But don't actually delete til later. | |
1008 (and targets | |
844
bf829a2d63b4
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
819
diff
changeset
|
1009 (or (eq trim-versions-without-asking t) (eq trim-versions-without-asking nil)) |
337 | 1010 (or trim-versions-without-asking |
1011 (y-or-n-p (format "Delete excess backup versions of %s? " | |
1012 real-file-name)))))) | |
1013 ;; Actually write the back up file. | |
1014 (condition-case () | |
1015 (if (or file-precious-flag | |
1016 ; (file-symlink-p buffer-file-name) | |
1017 backup-by-copying | |
1018 (and backup-by-copying-when-linked | |
1019 (> (file-nlinks real-file-name) 1)) | |
1020 (and backup-by-copying-when-mismatch | |
1021 (let ((attr (file-attributes real-file-name))) | |
1022 (or (nth 9 attr) | |
1023 (/= (nth 2 attr) (user-uid)))))) | |
1103
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1024 (condition-case () |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1025 (copy-file real-file-name backupname t t) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1026 (file-error |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1027 ;; If copying fails because file BACKUPNAME |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1028 ;; is not writable, delete that file and try again. |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1029 (if (and (file-exists-p backupname) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1030 (not (file-writable-p backupname))) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1031 (delete-file backupname)) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1032 (copy-file real-file-name backupname t t))) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1033 ;; rename-file should delete old backup. |
337 | 1034 (rename-file real-file-name backupname t) |
1035 (setq setmodes (file-modes backupname))) | |
1036 (file-error | |
1037 ;; If trouble writing the backup, write it in ~. | |
1038 (setq backupname (expand-file-name "~/%backup%~")) | |
1039 (message "Cannot write backup file; backing up in ~/%%backup%%~") | |
1040 (sleep-for 1) | |
1103
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1041 (condition-case () |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1042 (copy-file real-file-name backupname t t) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1043 (file-error |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1044 ;; If copying fails because file BACKUPNAME |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1045 ;; is not writable, delete that file and try again. |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1046 (if (and (file-exists-p backupname) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1047 (not (file-writable-p backupname))) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1048 (delete-file backupname)) |
ecaf2b70cd45
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1090
diff
changeset
|
1049 (copy-file real-file-name backupname t t))))) |
337 | 1050 (setq buffer-backed-up t) |
1051 ;; Now delete the old versions, if desired. | |
1052 (if delete-old-versions | |
1053 (while targets | |
1054 (condition-case () | |
1055 (delete-file (car targets)) | |
1056 (file-error nil)) | |
1057 (setq targets (cdr targets)))) | |
1058 setmodes) | |
1059 (file-error nil))))) | |
1060 | |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1061 (defun file-name-sans-versions (name &optional keep-backup-version) |
337 | 1062 "Return FILENAME sans backup versions or strings. |
1063 This is a separate procedure so your site-init or startup file can | |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1064 redefine it. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1065 If the optional argument KEEP-BACKUP-VERSION is non-nil, |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1066 we do not remove backup version numbers, only true file version numbers." |
1672
ecf43116a845
The find-file-name-handler function in ../src/fileio.c is now
Jim Blandy <jimb@redhat.com>
parents:
1537
diff
changeset
|
1067 (let ((handler (find-file-name-handler name))) |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1068 (if handler |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1069 (funcall handler 'file-name-sans-versions name keep-backup-version) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1070 (substring name 0 |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1071 (if (eq system-type 'vax-vms) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1072 ;; VMS version number is (a) semicolon, optional |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1073 ;; sign, zero or more digits or (b) period, option |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1074 ;; sign, zero or more digits, provided this is the |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1075 ;; second period encountered outside of the |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1076 ;; device/directory part of the file name. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1077 (or (string-match ";[---+]?[0-9]*\\'" name) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1078 (if (string-match "\\.[^]>:]*\\(\\.[---+]?[0-9]*\\)\\'" |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1079 name) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1080 (match-beginning 1)) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1081 (length name)) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1082 (if keep-backup-version |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1083 (length name) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1084 (or (string-match "\\.~[0-9]+~\\'" name) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1085 (string-match "~\\'" name) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1086 (length name)))))))) |
337 | 1087 |
1088 (defun make-backup-file-name (file) | |
1089 "Create the non-numeric backup file name for FILE. | |
1090 This is a separate function so you can redefine it for customization." | |
1091 (concat file "~")) | |
1092 | |
1093 (defun backup-file-name-p (file) | |
1094 "Return non-nil if FILE is a backup file name (numeric or not). | |
1095 This is a separate function so you can redefine it for customization. | |
1096 You may need to redefine `file-name-sans-versions' as well." | |
1097 (string-match "~$" file)) | |
1098 | |
890
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1099 ;; This is used in various files. |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1100 ;; The usage of bv-length is not very clean, |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1101 ;; but I can't see a good alternative, |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1102 ;; so as of now I am leaving it alone. |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1103 (defun backup-extract-version (fn) |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1104 "Given the name of a numeric backup file, return the backup number. |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1105 Uses the free variable `bv-length', whose value should be |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1106 the index in the name where the version number begins." |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1107 (if (and (string-match "[0-9]+~$" fn bv-length) |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1108 (= (match-beginning 0) bv-length)) |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1109 (string-to-int (substring fn bv-length -1)) |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1110 0)) |
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1111 |
337 | 1112 ;; I believe there is no need to alter this behavior for VMS; |
1113 ;; since backup files are not made on VMS, it should not get called. | |
1114 (defun find-backup-file-name (fn) | |
1115 "Find a file name for a backup file, and suggestions for deletions. | |
1116 Value is a list whose car is the name for the backup file | |
1117 and whose cdr is a list of old versions to consider deleting now." | |
1118 (if (eq version-control 'never) | |
1119 (list (make-backup-file-name fn)) | |
1120 (let* ((base-versions (concat (file-name-nondirectory fn) ".~")) | |
1121 (bv-length (length base-versions)) | |
1122 (possibilities (file-name-all-completions | |
1123 base-versions | |
1124 (file-name-directory fn))) | |
755 | 1125 (versions (sort (mapcar |
890
bad1b9af86a1
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
854
diff
changeset
|
1126 (function backup-extract-version) |
755 | 1127 possibilities) |
337 | 1128 '<)) |
423 | 1129 (high-water-mark (apply 'max 0 versions)) |
337 | 1130 (deserve-versions-p |
1131 (or version-control | |
1132 (> high-water-mark 0))) | |
1133 (number-to-delete (- (length versions) | |
1134 kept-old-versions kept-new-versions -1))) | |
1135 (if (not deserve-versions-p) | |
1136 (list (make-backup-file-name fn)) | |
1137 (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~") | |
1138 (if (> number-to-delete 0) | |
1139 (mapcar (function (lambda (n) | |
1140 (concat fn ".~" (int-to-string n) "~"))) | |
1141 (let ((v (nthcdr kept-old-versions versions))) | |
1142 (rplacd (nthcdr (1- number-to-delete) v) ()) | |
1143 v)))))))) | |
1144 | |
1145 (defun file-nlinks (filename) | |
1146 "Return number of names file FILENAME has." | |
1147 (car (cdr (file-attributes filename)))) | |
1229
d4324e885a41
(file-relative-name): New function.
Roland McGrath <roland@gnu.org>
parents:
1212
diff
changeset
|
1148 |
1246
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1149 (defun file-relative-name-1 (directory) |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1150 (cond ((string= directory "/") |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1151 filename) |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1152 ((string-match (concat "^" (regexp-quote directory)) |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1153 filename) |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1154 (substring filename (match-end 0))) |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1155 (t |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1156 (file-relative-name-1 |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1157 (file-name-directory (substring directory 0 -1)))))) |
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1158 |
1229
d4324e885a41
(file-relative-name): New function.
Roland McGrath <roland@gnu.org>
parents:
1212
diff
changeset
|
1159 (defun file-relative-name (filename &optional directory) |
d4324e885a41
(file-relative-name): New function.
Roland McGrath <roland@gnu.org>
parents:
1212
diff
changeset
|
1160 "Convert FILENAME to be relative to DIRECTORY (default: default-directory)." |
d4324e885a41
(file-relative-name): New function.
Roland McGrath <roland@gnu.org>
parents:
1212
diff
changeset
|
1161 (setq filename (expand-file-name filename) |
1230
f882f7a19c60
(file-relative-name): Don't lose when DIRECTORY is nil.
Roland McGrath <roland@gnu.org>
parents:
1229
diff
changeset
|
1162 directory (file-name-as-directory (if directory |
f882f7a19c60
(file-relative-name): Don't lose when DIRECTORY is nil.
Roland McGrath <roland@gnu.org>
parents:
1229
diff
changeset
|
1163 (expand-file-name directory) |
1229
d4324e885a41
(file-relative-name): New function.
Roland McGrath <roland@gnu.org>
parents:
1212
diff
changeset
|
1164 default-directory))) |
1246
5e25dd40a160
(file-relative-name-1): New function split out.
Richard M. Stallman <rms@gnu.org>
parents:
1231
diff
changeset
|
1165 (file-relative-name-1 directory)) |
337 | 1166 |
1167 (defun save-buffer (&optional args) | |
1168 "Save current buffer in visited file if modified. Versions described below. | |
1169 By default, makes the previous version into a backup file | |
1170 if previously requested or if this is the first save. | |
1171 With 1 or 3 \\[universal-argument]'s, marks this version | |
1172 to become a backup when the next save is done. | |
1173 With 2 or 3 \\[universal-argument]'s, | |
1174 unconditionally makes the previous version into a backup file. | |
1175 With argument of 0, never makes the previous version into a backup file. | |
1176 | |
1177 If a file's name is FOO, the names of its numbered backup versions are | |
1178 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~. | |
1179 Numeric backups (rather than FOO~) will be made if value of | |
1180 `version-control' is not the atom `never' and either there are already | |
1181 numeric versions of the file being backed up, or `version-control' is | |
1182 non-nil. | |
1183 We don't want excessive versions piling up, so there are variables | |
1184 `kept-old-versions', which tells Emacs how many oldest versions to keep, | |
1185 and `kept-new-versions', which tells how many newest versions to keep. | |
1186 Defaults are 2 old versions and 2 new. | |
1187 `dired-kept-versions' controls dired's clean-directory (.) command. | |
1188 If `trim-versions-without-asking' is nil, system will query user | |
1189 before trimming versions. Otherwise it does it silently." | |
1190 (interactive "p") | |
1191 (let ((modp (buffer-modified-p)) | |
1192 (large (> (buffer-size) 50000)) | |
1193 (make-backup-files (and make-backup-files (not (eq args 0))))) | |
1194 (and modp (memq args '(16 64)) (setq buffer-backed-up nil)) | |
1195 (if (and modp large) (message "Saving file %s..." (buffer-file-name))) | |
1196 (basic-save-buffer) | |
1197 (and modp (memq args '(4 64)) (setq buffer-backed-up nil)))) | |
1198 | |
1199 (defun delete-auto-save-file-if-necessary (&optional force) | |
1200 "Delete auto-save file for current buffer if `delete-auto-save-files' is t. | |
1201 Normally delete only if the file was written by this Emacs since | |
1202 the last real save, but optional arg FORCE non-nil means delete anyway." | |
1203 (and buffer-auto-save-file-name delete-auto-save-files | |
1204 (not (string= buffer-file-name buffer-auto-save-file-name)) | |
1205 (or force (recent-auto-save-p)) | |
1206 (progn | |
1207 (condition-case () | |
1208 (delete-file buffer-auto-save-file-name) | |
1209 (file-error nil)) | |
1210 (set-buffer-auto-saved)))) | |
1211 | |
1212 (defun basic-save-buffer () | |
1213 "Save the current buffer in its visited file, if it has been modified." | |
1214 (interactive) | |
1215 (if (buffer-modified-p) | |
1216 (let ((recent-save (recent-auto-save-p)) | |
1217 setmodes tempsetmodes) | |
1218 ;; On VMS, rename file and buffer to get rid of version number. | |
1219 (if (and (eq system-type 'vax-vms) | |
1220 (not (string= buffer-file-name | |
1221 (file-name-sans-versions buffer-file-name)))) | |
1222 (let (buffer-new-name) | |
1223 ;; Strip VMS version number before save. | |
1224 (setq buffer-file-name | |
1225 (file-name-sans-versions buffer-file-name)) | |
1226 ;; Construct a (unique) buffer name to correspond. | |
1227 (let ((buf (create-file-buffer (downcase buffer-file-name)))) | |
1228 (setq buffer-new-name (buffer-name buf)) | |
1229 (kill-buffer buf)) | |
1230 (rename-buffer buffer-new-name))) | |
1231 ;; If buffer has no file name, ask user for one. | |
1232 (or buffer-file-name | |
1233 (progn | |
1234 (setq buffer-file-name | |
1235 (expand-file-name (read-file-name "File to save in: ") nil) | |
1236 default-directory (file-name-directory buffer-file-name)) | |
1237 (auto-save-mode auto-save-default))) | |
1238 (or (verify-visited-file-modtime (current-buffer)) | |
1239 (not (file-exists-p buffer-file-name)) | |
1240 (yes-or-no-p | |
1241 (format "%s has changed since visited or saved. Save anyway? " | |
1242 (file-name-nondirectory buffer-file-name))) | |
1243 (error "Save not confirmed")) | |
1244 (save-restriction | |
1245 (widen) | |
1246 (and (> (point-max) 1) | |
1247 (/= (char-after (1- (point-max))) ?\n) | |
1248 (or (eq require-final-newline t) | |
1249 (and require-final-newline | |
1250 (y-or-n-p | |
1251 (format "Buffer %s does not end in newline. Add one? " | |
1252 (buffer-name))))) | |
1253 (save-excursion | |
1254 (goto-char (point-max)) | |
1255 (insert ?\n))) | |
2375
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
1256 (let ((hooks (append write-contents-hooks local-write-file-hooks |
94e135f2d64d
(local-write-file-hooks): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
2335
diff
changeset
|
1257 write-file-hooks)) |
337 | 1258 (done nil)) |
1259 (while (and hooks | |
1260 (not (setq done (funcall (car hooks))))) | |
1261 (setq hooks (cdr hooks))) | |
1262 ;; If a hook returned t, file is already "written". | |
1263 (cond ((not done) | |
1264 (if (not (file-writable-p buffer-file-name)) | |
1265 (let ((dir (file-name-directory buffer-file-name))) | |
1266 (if (not (file-directory-p dir)) | |
1267 (error "%s is not a directory" dir) | |
1268 (if (not (file-exists-p buffer-file-name)) | |
1269 (error "Directory %s write-protected" dir) | |
1270 (if (yes-or-no-p | |
1271 (format "File %s is write-protected; try to save anyway? " | |
1272 (file-name-nondirectory | |
1273 buffer-file-name))) | |
1274 (setq tempsetmodes t) | |
1275 (error "Attempt to save to a file which you aren't allowed to write")))))) | |
1276 (or buffer-backed-up | |
1277 (setq setmodes (backup-buffer))) | |
1278 (if file-precious-flag | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1279 ;; If file is precious, write temp name, then rename it. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1280 (let ((dir (file-name-directory buffer-file-name)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1281 (realname buffer-file-name) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1282 tempname temp nogood i succeed) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1283 (setq i 0) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1284 (setq nogood t) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1285 ;; Find the temporary name to write under. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1286 (while nogood |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1287 (setq tempname (format "%s#tmp#%d" dir i)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1288 (setq nogood (file-exists-p tempname)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1289 (setq i (1+ i))) |
337 | 1290 (unwind-protect |
1291 (progn (clear-visited-file-modtime) | |
1292 (write-region (point-min) (point-max) | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1293 tempname nil realname) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1294 (setq succeed t)) |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1295 ;; If writing the temp file fails, |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1296 ;; delete the temp file. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1297 (or succeed (delete-file tempname))) |
2108
baec460e28c7
* files.el (basic-save-buffer): If file-precious-flag is set, and
Jim Blandy <jimb@redhat.com>
parents:
2003
diff
changeset
|
1298 ;; Since we have created an entirely new file |
baec460e28c7
* files.el (basic-save-buffer): If file-precious-flag is set, and
Jim Blandy <jimb@redhat.com>
parents:
2003
diff
changeset
|
1299 ;; and renamed it, make sure it gets the |
baec460e28c7
* files.el (basic-save-buffer): If file-precious-flag is set, and
Jim Blandy <jimb@redhat.com>
parents:
2003
diff
changeset
|
1300 ;; right permission bits set. |
baec460e28c7
* files.el (basic-save-buffer): If file-precious-flag is set, and
Jim Blandy <jimb@redhat.com>
parents:
2003
diff
changeset
|
1301 (setq setmodes (file-modes buffer-file-name)) |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1302 ;; We succeeded in writing the temp file, |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1303 ;; so rename it. |
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1304 (rename-file tempname buffer-file-name t)) |
337 | 1305 ;; If file not writable, see if we can make it writable |
1306 ;; temporarily while we write it. | |
1307 ;; But no need to do so if we have just backed it up | |
1308 ;; (setmodes is set) because that says we're superseding. | |
1309 (cond ((and tempsetmodes (not setmodes)) | |
1310 ;; Change the mode back, after writing. | |
1311 (setq setmodes (file-modes buffer-file-name)) | |
1312 (set-file-modes buffer-file-name 511))) | |
1313 (write-region (point-min) (point-max) | |
1314 buffer-file-name nil t))))) | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1315 (setq buffer-file-number (nth 10 (file-attributes buffer-file-name))) |
337 | 1316 (if setmodes |
1317 (condition-case () | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1318 (set-file-modes buffer-file-name setmodes) |
337 | 1319 (error nil)))) |
1320 ;; If the auto-save file was recent before this command, | |
1321 ;; delete it now. | |
1322 (delete-auto-save-file-if-necessary recent-save) | |
1323 (run-hooks 'after-save-hooks)) | |
1324 (message "(No changes need to be saved)"))) | |
1325 | |
1326 (defun save-some-buffers (&optional arg exiting) | |
1327 "Save some modified file-visiting buffers. Asks user about each one. | |
423 | 1328 Optional argument (the prefix) non-nil means save all with no questions. |
1329 Optional second argument EXITING means ask about certain non-file buffers | |
1330 as well as about file buffers." | |
337 | 1331 (interactive "P") |
891 | 1332 (save-window-excursion |
632 | 1333 (if (zerop (map-y-or-n-p |
1334 (function | |
1335 (lambda (buffer) | |
1336 (and (buffer-modified-p buffer) | |
1337 (or | |
1338 (buffer-file-name buffer) | |
1339 (and exiting | |
1340 (progn | |
1341 (set-buffer buffer) | |
1342 (and buffer-offer-save (> (buffer-size) 0))))) | |
1343 (if arg | |
1344 t | |
1345 (if (buffer-file-name buffer) | |
1346 (format "Save file %s? " | |
1347 (buffer-file-name buffer)) | |
1348 (format "Save buffer %s? " | |
1349 (buffer-name buffer))))))) | |
1350 (function | |
1351 (lambda (buffer) | |
638 | 1352 (set-buffer buffer) |
1353 (save-buffer))) | |
632 | 1354 (buffer-list) |
891 | 1355 '("buffer" "buffers" "save") |
908
94eb4344341b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
891
diff
changeset
|
1356 (list (list ?\C-r (lambda (buf) |
94eb4344341b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
891
diff
changeset
|
1357 (view-buffer buf) |
909
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
908
diff
changeset
|
1358 (setq view-exit-action |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
908
diff
changeset
|
1359 '(lambda (ignore) |
4c6cdb66c74c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
908
diff
changeset
|
1360 (exit-recursive-edit))) |
908
94eb4344341b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
891
diff
changeset
|
1361 (recursive-edit) |
94eb4344341b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
891
diff
changeset
|
1362 ;; Return nil to ask about BUF again. |
94eb4344341b
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
891
diff
changeset
|
1363 nil) |
891 | 1364 "display the current buffer")) |
1365 )) | |
632 | 1366 (message "(No files need saving)")))) |
337 | 1367 |
1368 (defun not-modified (&optional arg) | |
1369 "Mark current buffer as unmodified, not needing to be saved. | |
1370 With prefix arg, mark buffer as modified, so \\[save-buffer] will save." | |
1371 (interactive "P") | |
1372 (message (if arg "Modification-flag set" | |
1373 "Modification-flag cleared")) | |
1374 (set-buffer-modified-p arg)) | |
1375 | |
1376 (defun toggle-read-only (&optional arg) | |
1377 "Change whether this buffer is visiting its file read-only. | |
1378 With arg, set read-only iff arg is positive." | |
1379 (interactive "P") | |
1380 (setq buffer-read-only | |
1381 (if (null arg) | |
1382 (not buffer-read-only) | |
1383 (> (prefix-numeric-value arg) 0))) | |
1384 ;; Force mode-line redisplay | |
1385 (set-buffer-modified-p (buffer-modified-p))) | |
1386 | |
1387 (defun insert-file (filename) | |
1388 "Insert contents of file FILENAME into buffer after point. | |
1389 Set mark after the inserted text. | |
1390 | |
1391 This function is meant for the user to run interactively. | |
1392 Don't call it from programs! Use `insert-file-contents' instead. | |
1393 \(Its calling sequence is different; see its documentation)." | |
1394 (interactive "fInsert file: ") | |
1395 (let ((tem (insert-file-contents filename))) | |
1396 (push-mark (+ (point) (car (cdr tem)))))) | |
1397 | |
1398 (defun append-to-file (start end filename) | |
1399 "Append the contents of the region to the end of file FILENAME. | |
1400 When called from a function, expects three arguments, | |
1401 START, END and FILENAME. START and END are buffer positions | |
1402 saying what text to write." | |
1403 (interactive "r\nFAppend to file: ") | |
1404 (write-region start end filename t)) | |
1405 | |
1406 (defun file-newest-backup (filename) | |
1407 "Return most recent backup file for FILENAME or nil if no backups exist." | |
1408 (let* ((filename (expand-file-name filename)) | |
1409 (file (file-name-nondirectory filename)) | |
1410 (dir (file-name-directory filename)) | |
1411 (comp (file-name-all-completions file dir)) | |
1412 newest) | |
1413 (while comp | |
1414 (setq file (concat dir (car comp)) | |
1415 comp (cdr comp)) | |
1416 (if (and (backup-file-name-p file) | |
1417 (or (null newest) (file-newer-than-file-p file newest))) | |
1418 (setq newest file))) | |
1419 newest)) | |
1420 | |
1421 (defun rename-uniquely () | |
1422 "Rename current buffer to a similar name not already taken. | |
1423 This function is useful for creating multiple shell process buffers | |
1424 or multiple mail buffers, etc." | |
1425 (interactive) | |
1426 (let* ((new-buf (generate-new-buffer (buffer-name))) | |
1427 (name (buffer-name new-buf))) | |
1428 (kill-buffer new-buf) | |
1429 (rename-buffer name) | |
1430 (set-buffer-modified-p (buffer-modified-p)))) ; force mode line update | |
423 | 1431 |
1537
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1432 (defun make-directory (dir &optional parents) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1433 "Create the directory DIR and any nonexistent parent dirs." |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1434 (interactive "FMake directory: \nP") |
1672
ecf43116a845
The find-file-name-handler function in ../src/fileio.c is now
Jim Blandy <jimb@redhat.com>
parents:
1537
diff
changeset
|
1435 (let ((handler (find-file-name-handler dir))) |
1537
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1436 (if handler |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1437 (funcall handler 'make-directory dir parents) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1438 (if (not parents) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1439 (make-directory-internal dir) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1440 (let ((dir (directory-file-name (expand-file-name dir))) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1441 create-list) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1442 (while (not (file-exists-p dir)) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1443 (setq create-list (cons dir create-list) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1444 dir (directory-file-name (file-name-directory dir)))) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1445 (while create-list |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1446 (make-directory-internal (car create-list)) |
8531c6d96e5c
(make-directory): Renamed from make-directory-path.
Richard M. Stallman <rms@gnu.org>
parents:
1471
diff
changeset
|
1447 (setq create-list (cdr create-list)))))))) |
337 | 1448 |
1449 (put 'revert-buffer-function 'permanent-local t) | |
1450 (defvar revert-buffer-function nil | |
1451 "Function to use to revert this buffer, or nil to do the default.") | |
1452 | |
1453 (put 'revert-buffer-insert-file-contents-function 'permanent-local t) | |
1454 (defvar revert-buffer-insert-file-contents-function nil | |
1455 "Function to use to insert contents when reverting this buffer. | |
1456 Gets two args, first the nominal file name to use, | |
1457 and second, t if reading the auto-save file.") | |
1458 | |
1905
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1459 (defun revert-buffer (&optional ignore-auto noconfirm) |
337 | 1460 "Replace the buffer text with the text of the visited file on disk. |
1461 This undoes all changes since the file was visited or saved. | |
605 | 1462 With a prefix argument, offer to revert from latest auto-save file, if |
1463 that is more recent than the visited file. | |
1905
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1464 |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1465 When called from lisp, the first argument is IGNORE-AUTO; only offer |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1466 to revert from the auto-save file when this is nil. Note that the |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1467 sense of this argument is the reverse of the prefix argument, for the |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1468 sake of backward compatibility. IGNORE-AUTO is optional, defaulting |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1469 to nil. |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1470 |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1471 Optional second argument NOCONFIRM means don't ask for confirmation at |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1472 all. |
337 | 1473 |
605 | 1474 If the value of `revert-buffer-function' is non-nil, it is called to |
1475 do the work." | |
1905
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1476 ;; I admit it's odd to reverse the sense of the prefix argument, but |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1477 ;; there is a lot of code out there which assumes that the first |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1478 ;; argument should be t to avoid consulting the auto-save file, and |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1479 ;; there's no straightforward way to encourage authors to notice a |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1480 ;; reversal of the argument sense. So I'm just changing the user |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1481 ;; interface, but leaving the programmatic interface the same. |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1482 (interactive (list (not prefix-arg))) |
337 | 1483 (if revert-buffer-function |
1905
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1484 (funcall revert-buffer-function ignore-auto noconfirm) |
337 | 1485 (let* ((opoint (point)) |
1905
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1486 (auto-save-p (and (not ignore-auto) |
494dd05d460c
* files.el (revert-buffer): Reverse the sense of the first
Jim Blandy <jimb@redhat.com>
parents:
1892
diff
changeset
|
1487 (recent-auto-save-p) |
337 | 1488 buffer-auto-save-file-name |
1489 (file-readable-p buffer-auto-save-file-name) | |
1490 (y-or-n-p | |
1491 "Buffer has been auto-saved recently. Revert from auto-save file? "))) | |
1492 (file-name (if auto-save-p | |
1493 buffer-auto-save-file-name | |
1494 buffer-file-name))) | |
1495 (cond ((null file-name) | |
1496 (error "Buffer does not seem to be associated with any file")) | |
1497 ((or noconfirm | |
1498 (yes-or-no-p (format "Revert buffer from file %s? " | |
1499 file-name))) | |
1500 ;; If file was backed up but has changed since, | |
1501 ;; we shd make another backup. | |
1502 (and (not auto-save-p) | |
423 | 1503 (not (verify-visited-file-modtime (current-buffer))) |
337 | 1504 (setq buffer-backed-up nil)) |
1505 ;; Get rid of all undo records for this buffer. | |
1506 (or (eq buffer-undo-list t) | |
1507 (setq buffer-undo-list nil)) | |
1508 (let ((buffer-read-only nil) | |
1509 ;; Don't make undo records for the reversion. | |
1510 (buffer-undo-list t)) | |
1511 (if revert-buffer-insert-file-contents-function | |
1512 (funcall revert-buffer-insert-file-contents-function | |
1513 file-name auto-save-p) | |
1514 (if (not (file-exists-p file-name)) | |
1515 (error "File %s no longer exists!" file-name)) | |
1516 ;; Bind buffer-file-name to nil | |
1517 ;; so that we don't try to lock the file. | |
1518 (let ((buffer-file-name nil)) | |
1519 (or auto-save-p | |
1520 (unlock-buffer)) | |
1521 (erase-buffer)) | |
1522 (insert-file-contents file-name (not auto-save-p)))) | |
1523 (goto-char (min opoint (point-max))) | |
1212
de70f50101c0
(after-find-file): New arg NOAUTO.
Richard M. Stallman <rms@gnu.org>
parents:
1182
diff
changeset
|
1524 (after-find-file nil nil t) |
337 | 1525 t))))) |
1526 | |
1527 (defun recover-file (file) | |
1528 "Visit file FILE, but get contents from its last auto-save file." | |
1529 (interactive | |
1530 (let ((prompt-file buffer-file-name) | |
1531 (file-name nil) | |
1532 (file-dir nil)) | |
1533 (and prompt-file | |
1534 (setq file-name (file-name-nondirectory prompt-file) | |
1535 file-dir (file-name-directory prompt-file))) | |
1536 (list (read-file-name "Recover file: " | |
1537 file-dir nil nil file-name)))) | |
1538 (setq file (expand-file-name file)) | |
1539 (if (auto-save-file-name-p file) (error "%s is an auto-save file" file)) | |
1540 (let ((file-name (let ((buffer-file-name file)) | |
1541 (make-auto-save-file-name)))) | |
1542 (cond ((not (file-newer-than-file-p file-name file)) | |
1543 (error "Auto-save file %s not current" file-name)) | |
1544 ((save-window-excursion | |
1545 (if (not (eq system-type 'vax-vms)) | |
1546 (with-output-to-temp-buffer "*Directory*" | |
1547 (buffer-disable-undo standard-output) | |
1548 (call-process "ls" nil standard-output nil | |
736
18b892513cb7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
735
diff
changeset
|
1549 (if (file-symlink-p file) "-lL" "-l") |
18b892513cb7
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
735
diff
changeset
|
1550 file file-name))) |
337 | 1551 (yes-or-no-p (format "Recover auto save file %s? " file-name))) |
1552 (switch-to-buffer (find-file-noselect file t)) | |
1553 (let ((buffer-read-only nil)) | |
1554 (erase-buffer) | |
1555 (insert-file-contents file-name nil)) | |
1212
de70f50101c0
(after-find-file): New arg NOAUTO.
Richard M. Stallman <rms@gnu.org>
parents:
1182
diff
changeset
|
1556 (after-find-file nil nil t)) |
337 | 1557 (t (error "Recover-file cancelled."))))) |
1558 | |
1559 (defun kill-some-buffers () | |
1560 "For each buffer, ask whether to kill it." | |
1561 (interactive) | |
1562 (let ((list (buffer-list))) | |
1563 (while list | |
1564 (let* ((buffer (car list)) | |
1565 (name (buffer-name buffer))) | |
1566 (and (not (string-equal name "")) | |
1567 (/= (aref name 0) ? ) | |
1568 (yes-or-no-p | |
1569 (format "Buffer %s %s. Kill? " | |
1570 name | |
1571 (if (buffer-modified-p buffer) | |
1572 "HAS BEEN EDITED" "is unmodified"))) | |
1573 (kill-buffer buffer))) | |
1574 (setq list (cdr list))))) | |
1575 | |
1576 (defun auto-save-mode (arg) | |
1577 "Toggle auto-saving of contents of current buffer. | |
1395
f6f838c4a26e
(buffer-file-number): New variable.
Richard M. Stallman <rms@gnu.org>
parents:
1371
diff
changeset
|
1578 With prefix argument ARG, turn auto-saving on if positive, else off." |
337 | 1579 (interactive "P") |
1580 (setq buffer-auto-save-file-name | |
1581 (and (if (null arg) | |
1582 (not buffer-auto-save-file-name) | |
1583 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0)))) | |
1584 (if (and buffer-file-name auto-save-visited-file-name | |
1585 (not buffer-read-only)) | |
1586 buffer-file-name | |
1587 (make-auto-save-file-name)))) | |
1588 (if (interactive-p) | |
1589 (message "Auto-save %s (in this buffer)" | |
1590 (if buffer-auto-save-file-name "on" "off"))) | |
1591 buffer-auto-save-file-name) | |
1592 | |
1593 (defun rename-auto-save-file () | |
1594 "Adjust current buffer's auto save file name for current conditions. | |
1595 Also rename any existing auto save file, if it was made in this session." | |
1596 (let ((osave buffer-auto-save-file-name)) | |
1597 (setq buffer-auto-save-file-name | |
1598 (make-auto-save-file-name)) | |
1599 (if (and osave buffer-auto-save-file-name | |
1600 (not (string= buffer-auto-save-file-name buffer-file-name)) | |
1601 (not (string= buffer-auto-save-file-name osave)) | |
1602 (file-exists-p osave) | |
1603 (recent-auto-save-p)) | |
1604 (rename-file osave buffer-auto-save-file-name t)))) | |
1605 | |
1606 (defun make-auto-save-file-name () | |
1607 "Return file name to use for auto-saves of current buffer. | |
1608 Does not consider `auto-save-visited-file-name' as that variable is checked | |
1609 before calling this function. You can redefine this for customization. | |
1610 See also `auto-save-file-name-p'." | |
1611 (if buffer-file-name | |
1612 (concat (file-name-directory buffer-file-name) | |
1613 "#" | |
1614 (file-name-nondirectory buffer-file-name) | |
1615 "#") | |
1616 ;; For non-file bfr, use bfr name and Emacs pid. | |
1617 (expand-file-name (format "#%s#%s#" (buffer-name) (make-temp-name ""))))) | |
1618 | |
1619 (defun auto-save-file-name-p (filename) | |
1620 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'. | |
1621 FILENAME should lack slashes. You can redefine this for customization." | |
1622 (string-match "^#.*#$" filename)) | |
1623 | |
1624 (defconst list-directory-brief-switches | |
1625 (if (eq system-type 'vax-vms) "" "-CF") | |
1626 "*Switches for list-directory to pass to `ls' for brief listing,") | |
1627 | |
1628 (defconst list-directory-verbose-switches | |
1629 (if (eq system-type 'vax-vms) | |
1630 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)" | |
1631 "-l") | |
1632 "*Switches for list-directory to pass to `ls' for verbose listing,") | |
1633 | |
1634 (defun list-directory (dirname &optional verbose) | |
1635 "Display a list of files in or matching DIRNAME, a la `ls'. | |
1636 DIRNAME is globbed by the shell if necessary. | |
1637 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'. | |
1638 Actions controlled by variables `list-directory-brief-switches' | |
1639 and `list-directory-verbose-switches'." | |
1640 (interactive (let ((pfx current-prefix-arg)) | |
1641 (list (read-file-name (if pfx "List directory (verbose): " | |
1642 "List directory (brief): ") | |
1643 nil default-directory nil) | |
1644 pfx))) | |
1645 (let ((switches (if verbose list-directory-verbose-switches | |
1646 list-directory-brief-switches))) | |
1647 (or dirname (setq dirname default-directory)) | |
1648 (setq dirname (expand-file-name dirname)) | |
1649 (with-output-to-temp-buffer "*Directory*" | |
1650 (buffer-disable-undo standard-output) | |
1651 (princ "Directory ") | |
1652 (princ dirname) | |
1653 (terpri) | |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1654 (save-excursion |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1655 (set-buffer "*Directory*") |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1656 (let ((wildcard (not (file-directory-p dirname)))) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1657 (insert-directory dirname switches wildcard (not wildcard))))))) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1658 |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1659 (defvar insert-directory-program "ls" |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1660 "Absolute or relative name of the `ls' program used by `insert-directory'.") |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1661 |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1662 ;; insert-directory |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1663 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1664 ;; FULL-DIRECTORY-P is nil. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1665 ;; The single line of output must display FILE's name as it was |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1666 ;; given, namely, an absolute path name. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1667 ;; - must insert exactly one line for each file if WILDCARD or |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1668 ;; FULL-DIRECTORY-P is t, plus one optional "total" line |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1669 ;; before the file lines, plus optional text after the file lines. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1670 ;; Lines are delimited by "\n", so filenames containing "\n" are not |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1671 ;; allowed. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1672 ;; File lines should display the basename. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1673 ;; - must be consistent with |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1674 ;; - functions dired-move-to-filename, (these two define what a file line is) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1675 ;; dired-move-to-end-of-filename, |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1676 ;; dired-between-files, (shortcut for (not (dired-move-to-filename))) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1677 ;; dired-insert-headerline |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1678 ;; dired-after-subdir-garbage (defines what a "total" line is) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1679 ;; - variable dired-subdir-regexp |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1680 (defun insert-directory (file switches &optional wildcard full-directory-p) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1681 "Insert directory listing for of FILE, formatted according to SWITCHES. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1682 Leaves point after the inserted text. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1683 Optional third arg WILDCARD means treat FILE as shell wildcard. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1684 Optional fourth arg FULL-DIRECTORY-P means file is a directory and |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1685 switches do not contain `d', so that a full listing is expected. |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1686 |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1687 This works by running a directory listing program |
1892
c4ff65277bb3
(insert-directory): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
1879
diff
changeset
|
1688 whose name is in the variable `insert-directory-program'. |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1689 If WILDCARD, it also runs the shell specified by `shell-file-name'." |
1672
ecf43116a845
The find-file-name-handler function in ../src/fileio.c is now
Jim Blandy <jimb@redhat.com>
parents:
1537
diff
changeset
|
1690 (let ((handler (find-file-name-handler file))) |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1691 (if handler |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1692 (funcall handler 'insert-directory file switches |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1693 wildcard full-directory-p) |
337 | 1694 (if (eq system-type 'vax-vms) |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1695 (vms-read-directory file switches (current-buffer)) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1696 (if wildcard |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1697 (let ((default-directory (file-name-directory file))) |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1698 (call-process shell-file-name nil t nil |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1699 "-c" (concat insert-directory-program |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1700 " -d " switches " " |
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1701 (file-name-nondirectory file)))) |
2335
e5a37be50b36
* files.el (insert-directory): Do chase symlinks before passing
Jim Blandy <jimb@redhat.com>
parents:
2307
diff
changeset
|
1702 ;; Chase links till we reach a non-link. |
e5a37be50b36
* files.el (insert-directory): Do chase symlinks before passing
Jim Blandy <jimb@redhat.com>
parents:
2307
diff
changeset
|
1703 ;; This used to be commented out, but Barry Margolin says: |
e5a37be50b36
* files.el (insert-directory): Do chase symlinks before passing
Jim Blandy <jimb@redhat.com>
parents:
2307
diff
changeset
|
1704 ;; SunOS 4.1.3 (and SV and POSIX?) lists the link |
e5a37be50b36
* files.el (insert-directory): Do chase symlinks before passing
Jim Blandy <jimb@redhat.com>
parents:
2307
diff
changeset
|
1705 ;; if we give a link to a directory - yuck! |
e5a37be50b36
* files.el (insert-directory): Do chase symlinks before passing
Jim Blandy <jimb@redhat.com>
parents:
2307
diff
changeset
|
1706 (let (symlink) |
e5a37be50b36
* files.el (insert-directory): Do chase symlinks before passing
Jim Blandy <jimb@redhat.com>
parents:
2307
diff
changeset
|
1707 (while (setq symlink (file-symlink-p file)) |
e5a37be50b36
* files.el (insert-directory): Do chase symlinks before passing
Jim Blandy <jimb@redhat.com>
parents:
2307
diff
changeset
|
1708 (setq file symlink))) |
1109
c9feb3e64805
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
1103
diff
changeset
|
1709 (call-process insert-directory-program nil t nil switches file)))))) |
337 | 1710 |
1711 (defun save-buffers-kill-emacs (&optional arg) | |
1712 "Offer to save each buffer, then kill this Emacs process. | |
1713 With prefix arg, silently save all file-visiting buffers, then kill." | |
1714 (interactive "P") | |
1715 (save-some-buffers arg t) | |
1716 (and (or (not (memq t (mapcar (function | |
1717 (lambda (buf) (and (buffer-file-name buf) | |
1718 (buffer-modified-p buf)))) | |
1719 (buffer-list)))) | |
1720 (yes-or-no-p "Modified buffers exist; exit anyway? ")) | |
1721 (or (not (fboundp 'process-list)) | |
1722 ;; process-list is not defined on VMS. | |
1723 (let ((processes (process-list)) | |
1724 active) | |
1725 (while processes | |
739
0bb85f26b79c
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
736
diff
changeset
|
1726 (and (memq (process-status (car processes)) '(run stop open)) |
337 | 1727 (let ((val (process-kill-without-query (car processes)))) |
1728 (process-kill-without-query (car processes) val) | |
1729 val) | |
1730 (setq active t)) | |
1731 (setq processes (cdr processes))) | |
1732 (or (not active) | |
1733 (yes-or-no-p "Active processes exist; kill them and exit anyway? ")))) | |
1734 (kill-emacs))) | |
1735 | |
1736 (define-key ctl-x-map "\C-f" 'find-file) | |
1737 (define-key ctl-x-map "\C-q" 'toggle-read-only) | |
1738 (define-key ctl-x-map "\C-r" 'find-file-read-only) | |
1739 (define-key ctl-x-map "\C-v" 'find-alternate-file) | |
1740 (define-key ctl-x-map "\C-s" 'save-buffer) | |
1741 (define-key ctl-x-map "s" 'save-some-buffers) | |
1742 (define-key ctl-x-map "\C-w" 'write-file) | |
1743 (define-key ctl-x-map "i" 'insert-file) | |
1744 (define-key esc-map "~" 'not-modified) | |
1745 (define-key ctl-x-map "\C-d" 'list-directory) | |
1746 (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs) | |
1747 | |
1748 (define-key ctl-x-4-map "f" 'find-file-other-window) | |
1749 (define-key ctl-x-4-map "r" 'find-file-read-only-other-window) | |
1750 (define-key ctl-x-4-map "\C-f" 'find-file-other-window) | |
1751 (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window) | |
854
0b4f3a91f207
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
846
diff
changeset
|
1752 (define-key ctl-x-4-map "\C-o" 'display-buffer) |
423 | 1753 |
778 | 1754 (define-key ctl-x-5-map "b" 'switch-to-buffer-other-frame) |
1755 (define-key ctl-x-5-map "f" 'find-file-other-frame) | |
1756 (define-key ctl-x-5-map "\C-f" 'find-file-other-frame) | |
1757 (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame) | |
662
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
1758 |
8a533acedb77
*** empty log message ***
Eric S. Raymond <esr@snark.thyrsus.com>
parents:
638
diff
changeset
|
1759 ;;; files.el ends here |