annotate lisp/code-files.el @ 288:e11d67e05968 r21-0b42

Import from CVS: tag r21-0b42
author cvs
date Mon, 13 Aug 2007 10:35:54 +0200
parents ca9a9ec9c1c1
children 70ad99077275
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
1 ;;; code-files.el --- File I/O functions for XEmacs.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
2
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
3 ;; Copyright (C) 1992,93,94,95 Free Software Foundation, Inc.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
4 ;; Copyright (C) 1995 Amdahl Corporation.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
5 ;; Copyright (C) 1995 Sun Microsystems.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
6
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
7 ;; This file is part of XEmacs.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
8
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
9 ;; This file is very similar to mule-files.el
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
10
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
11 ;; XEmacs is free software; you can redistribute it and/or modify it
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
12 ;; under the terms of the GNU General Public License as published by
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
13 ;; the Free Software Foundation; either version 2, or (at your option)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
14 ;; any later version.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
15
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
16 ;; XEmacs is distributed in the hope that it will be useful, but
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
19 ;; General Public License for more details.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
20
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
21 ;; You should have received a copy of the GNU General Public License
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
22 ;; along with XEmacs; see the file COPYING. If not, write to the
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
24 ;; Boston, MA 02111-1307, USA.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
25
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
26 ;;; Commentary:
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
27
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
28 ;;; Derived from mule.el in the original Mule but heavily modified
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
29 ;;; by Ben Wing.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
30
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
31 ;; 1997/3/11 modified by MORIOKA Tomohiko to sync with Emacs 20 API.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
32
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
33 ;;; Code:
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
34
263
727739f917cb Import from CVS: tag r20-5b30
cvs
parents: 259
diff changeset
35 (setq-default buffer-file-coding-system 'no-conversion)
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
36 (put 'buffer-file-coding-system 'permanent-local t)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
37
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
38 (define-obsolete-variable-alias
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
39 'file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
40 'buffer-file-coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
41
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
42 (define-obsolete-variable-alias
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
43 'overriding-file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
44 'coding-system-for-read)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
45
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
46 (defvar buffer-file-coding-system-for-read 'undecided
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
47 "Coding system used when reading a file.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
48 This provides coarse-grained control; for finer-grained control, use
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
49 `file-coding-system-alist'. From a Lisp program, if you wish to
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
50 unilaterally specify the coding system used for one particular
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
51 operation, you should bind the variable `coding-system-for-read'
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
52 rather than setting this variable, which is intended to be used for
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
53 global environment specification.")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
54
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
55 (define-obsolete-variable-alias
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
56 'file-coding-system-for-read
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
57 'buffer-file-coding-system-for-read)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
58
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
59 (defvar file-coding-system-alist
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
60 '(("\\.elc$" . (binary . binary))
274
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents: 263
diff changeset
61 ;; This must not be neccessary, slb suggests -kkm
ca9a9ec9c1c1 Import from CVS: tag r21-0b35
cvs
parents: 263
diff changeset
62 ;; ("loaddefs.el$" . (binary . binary))
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
63 ("\\.tar$" . (binary . binary))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
64 ("\\.\\(tif\\|tiff\\)$" . (binary . binary))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
65 ("\\.png$" . (binary . binary))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
66 ("\\.gif$" . (binary . binary))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
67 ("\\.\\(jpeg\\|jpg\\)$" . (binary . binary))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
68 ("TUTORIAL\\.hr$" . iso-8859-2)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
69 ("TUTORIAL\\.pl$" . iso-8859-2)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
70 ;; ("\\.\\(el\\|emacs\\|info\\(-[0-9]+\\)?\\|texi\\)$" . iso-2022-8)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
71 ;; ("\\(ChangeLog\\|CHANGES-beta\\)$" . iso-2022-8)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
72 ("\\.\\(gz\\|Z\\)$" . binary)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
73 ("/spool/mail/.*$" . convert-mbox-coding-system))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
74 "Alist to decide a coding system to use for a file I/O operation.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
75 The format is ((PATTERN . VAL) ...),
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
76 where PATTERN is a regular expression matching a file name,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
77 VAL is a coding system, a cons of coding systems, or a function symbol.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
78 If VAL is a coding system, it is used for both decoding and encoding
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
79 the file contents.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
80 If VAL is a cons of coding systems, the car part is used for decoding,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
81 and the cdr part is used for encoding.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
82 If VAL is a function symbol, the function must return a coding system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
83 or a cons of coding systems which are used as above.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
84
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
85 This overrides the more general specification in
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
86 `buffer-file-coding-system-for-read', but is overridden by
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
87 `coding-system-for-read'.")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
88
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
89 (defun set-buffer-file-coding-system (coding-system &optional force)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
90 "Set buffer-file-coding-system of the current buffer to CODING-SYSTEM.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
91 If optional argument FORCE (interactively, the prefix argument) is not
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
92 given, attempt to match the EOL type of the new coding system to
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
93 the current value of `buffer-file-coding-system'."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
94 (interactive "zFile coding system: \nP")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
95 (get-coding-system coding-system) ;; correctness check
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
96 (if (not force)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
97 (setq coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
98 (subsidiary-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
99 coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
100 (coding-system-eol-type buffer-file-coding-system))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
101 (setq buffer-file-coding-system coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
102 (redraw-modeline t))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
103
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
104 (defun toggle-buffer-file-coding-system ()
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
105 "Set EOL type of buffer-file-coding-system of the current buffer to
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
106 something other than what it is at the moment."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
107 (interactive)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
108 (let ((eol-type
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
109 (coding-system-eol-type buffer-file-coding-system)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
110 (setq buffer-file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
111 (subsidiary-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
112 (coding-system-base buffer-file-coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
113 (cond ((eq eol-type 'lf) 'crlf)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
114 ((eq eol-type 'crlf) 'lf)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
115 ((eq eol-type 'cr) 'lf))))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
116
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
117 (define-obsolete-function-alias
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
118 'set-file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
119 'set-buffer-file-coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
120
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
121 (defun set-buffer-file-coding-system-for-read (coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
122 "Set the coding system used when reading in a file.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
123 This is equivalent to setting the variable
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
124 `buffer-file-coding-system-for-read'. You can also use
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
125 `file-coding-system-alist' to specify the coding system for
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
126 particular files."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
127 (interactive "zFile coding system for read: ")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
128 (get-coding-system coding-system) ;; correctness check
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
129 (setq buffer-file-coding-system-for-read coding-system))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
130
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
131 (define-obsolete-function-alias
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
132 'set-file-coding-system-for-read
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
133 'set-buffer-file-coding-system-for-read)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
134
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
135 (defun set-default-buffer-file-coding-system (coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
136 "Set the default value of `buffer-file-coding-system' to CODING-SYSTEM.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
137 The default value is used both for buffers without associated files
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
138 and for files with no apparent coding system (i.e. primarily ASCII).
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
139 See `buffer-file-coding-system' for more information."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
140 (interactive "zDefault file coding system: ")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
141 (setq-default buffer-file-coding-system coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
142 (redraw-modeline t))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
143
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
144 (define-obsolete-function-alias
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
145 'set-default-file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
146 'set-default-buffer-file-coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
147
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
148 (defun find-file-coding-system-for-read-from-filename (filename)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
149 "Look up coding system to read a file in `file-coding-system-alist'.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
150 The return value will be nil (no applicable entry) or a coding system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
151 object (the entry specified a coding system)."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
152 (let ((alist file-coding-system-alist)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
153 (found nil)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
154 (codesys nil))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
155 (let ((case-fold-search (eq system-type 'vax-vms)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
156 (setq filename (file-name-sans-versions filename))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
157 (while (and (not found) alist)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
158 (if (string-match (car (car alist)) filename)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
159 (setq codesys (cdr (car alist))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
160 found t))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
161 (setq alist (cdr alist))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
162 (when codesys
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
163 (if (functionp codesys)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
164 (setq codesys (funcall codesys 'insert-file-contents filename))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
165 )
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
166 (cond ((consp codesys) (find-coding-system (car codesys)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
167 ((find-coding-system codesys))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
168 ))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
169
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
170 (define-obsolete-function-alias
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
171 'find-file-coding-system-from-filename
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
172 'find-file-coding-system-for-read-from-filename)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
173
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
174 (defun find-file-coding-system-for-write-from-filename (filename)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
175 "Look up coding system to write a file in `file-coding-system-alist'.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
176 The return value will be nil (no applicable entry) or a coding system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
177 object (the entry specified a coding system)."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
178 (let ((alist file-coding-system-alist)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
179 (found nil)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
180 (codesys nil))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
181 (let ((case-fold-search (eq system-type 'vax-vms)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
182 (setq filename (file-name-sans-versions filename))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
183 (while (and (not found) alist)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
184 (if (string-match (car (car alist)) filename)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
185 (setq codesys (cdr (car alist))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
186 found t))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
187 (setq alist (cdr alist))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
188 (when codesys
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
189 (if (functionp codesys)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
190 (setq codesys (funcall codesys 'write-region filename))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
191 )
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
192 (cond ((consp codesys) (find-coding-system (cdr codesys)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
193 ((find-coding-system codesys))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
194 ))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
195
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
196 (defun convert-mbox-coding-system (filename visit start end)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
197 "Decoding function for Unix mailboxes.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
198 Does separate detection and decoding on each message, since each
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
199 message might be in a different encoding."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
200 (let ((buffer-read-only nil))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
201 (save-restriction
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
202 (narrow-to-region start end)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
203 (goto-char (point-min))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
204 (while (not (eobp))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
205 (let ((start (point))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
206 end)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
207 (forward-char 1)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
208 (if (re-search-forward "^From" nil 'move)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
209 (beginning-of-line))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
210 (setq end (point))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
211 (decode-coding-region start end 'undecided))))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
212
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
213 (defun find-coding-system-magic-cookie ()
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
214 "Look for the coding-system magic cookie in the current buffer.\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
215 "The coding-system magic cookie is the exact string\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
216 "\";;;###coding system: \" followed by a valid coding system symbol,\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
217 "somewhere within the first 3000 characters of the file. If found,\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
218 "the coding system symbol is returned; otherwise nil is returned.\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
219 "Note that it is extremely unlikely that such a string would occur\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
220 "coincidentally as the result of encoding some characters in a non-ASCII\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
221 "charset, and that the spaces make it even less likely since the space\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
222 "character is not a valid octet in any ISO 2022 encoding of most non-ASCII\n"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
223 "charsets."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
224 (save-excursion
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
225 (goto-char (point-min))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
226 (or (and (looking-at
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
227 "^[^\n]*-\\*-[^\n]*coding: \\([^ \t\n;]+\\)[^\n]*-\\*-")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
228 (let ((codesys (intern (buffer-substring
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
229 (match-beginning 1)(match-end 1)))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
230 (if (find-coding-system codesys) codesys)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
231 ;; (save-excursion
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
232 ;; (let (start end)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
233 ;; (and (re-search-forward "^;+[ \t]*Local Variables:" nil t)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
234 ;; (setq start (match-end 0))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
235 ;; (re-search-forward "\n;+[ \t]*End:")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
236 ;; (setq end (match-beginning 0))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
237 ;; (save-restriction
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
238 ;; (narrow-to-region start end)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
239 ;; (goto-char start)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
240 ;; (re-search-forward "^;;; coding: \\([^\n]+\\)$" nil t)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
241 ;; )
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
242 ;; (let ((codesys
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
243 ;; (intern (buffer-substring
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
244 ;; (match-beginning 1)(match-end 1)))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
245 ;; (if (find-coding-system codesys) codesys))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
246 ;; )))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
247 (let ((case-fold-search nil))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
248 (if (search-forward
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
249 ";;;###coding system: " (+ (point-min) 3000) t)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
250 (let ((start (point))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
251 (end (progn
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
252 (skip-chars-forward "^ \t\n\r")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
253 (point))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
254 (if (> end start)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
255 (let ((codesys (intern (buffer-substring start end))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
256 (if (find-coding-system codesys) codesys)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
257 )))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
258 )))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
259
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
260 (defun load (file &optional noerror nomessage nosuffix)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
261 "Execute a file of Lisp code named FILE.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
262 First tries FILE with .elc appended, then tries with .el,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
263 then tries FILE unmodified. Searches directories in load-path.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
264 If optional second arg NOERROR is non-nil,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
265 report no error if FILE doesn't exist.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
266 Print messages at start and end of loading unless
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
267 optional third arg NOMESSAGE is non-nil.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
268 If optional fourth arg NOSUFFIX is non-nil, don't try adding
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
269 suffixes .elc or .el to the specified name FILE.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
270 Return t if file exists."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
271 (let* ((filename (substitute-in-file-name file))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
272 (handler (find-file-name-handler filename 'load))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
273 (path nil))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
274 (if handler
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
275 (funcall handler 'load filename noerror nomessage nosuffix)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
276 (if (or (<= (length filename) 0)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
277 (null (setq path
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
278 (locate-file filename load-path
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
279 (and (not nosuffix) ".elc:.el:")))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
280 (and (null noerror)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
281 (signal 'file-error (list "Cannot open load file" filename)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
282 ;; now use the internal load to actually load the file.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
283 (load-internal
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
284 file noerror nomessage nosuffix
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
285 (let ((elc ; use string= instead of string-match to keep match-data.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
286 (string= ".elc" (downcase (substring path -4)))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
287 (or (and (not elc) coding-system-for-read) ; prefer for source file
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
288 ;; find magic-cookie
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
289 (save-excursion
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
290 (set-buffer (get-buffer-create " *load*"))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
291 (erase-buffer)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
292 (let ((coding-system-for-read 'no-conversion))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
293 (insert-file-contents path nil 1 3001))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
294 (find-coding-system-magic-cookie))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
295 (if elc
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
296 ;; if reading a byte-compiled file and we didn't find
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
297 ;; a coding-system magic cookie, then use `binary'.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
298 ;; We need to guarantee that we never do autodetection
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
299 ;; on byte-compiled files because confusion here would
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
300 ;; be a very bad thing. Pre-existing byte-compiled
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
301 ;; files are always in the `binary' coding system.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
302 ;; Also, byte-compiled files always use `lf' to terminate
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
303 ;; a line; don't risk confusion here either.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
304 'binary
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
305 (or (find-file-coding-system-for-read-from-filename path)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
306 ;; looking up in `file-coding-system-alist'.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
307 ;; otherwise use `buffer-file-coding-system-for-read',
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
308 ;; as normal
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
309 buffer-file-coding-system-for-read)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
310 )))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
311 )))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
312
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
313 (defvar insert-file-contents-access-hook nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
314 "A hook to make a file accessible before reading it.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
315 `insert-file-contents' calls this hook before doing anything else.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
316 Called with two arguments: FILENAME and VISIT, the same as the
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
317 corresponding arguments in the call to `insert-file-contents'.")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
318
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
319 (defvar insert-file-contents-pre-hook nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
320 "A special hook to decide the coding system used for reading in a file.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
321
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
322 Before reading a file, `insert-file-contents' calls the functions on
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
323 this hook with arguments FILENAME and VISIT, the same as the
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
324 corresponding arguments in the call to `insert-file-contents'. In
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
325 these functions, you may refer to the global variable
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
326 `buffer-file-coding-system-for-read'.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
327
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
328 The return value of the functions should be either
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
329
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
330 -- nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
331 -- A coding system or a symbol denoting it, indicating the coding system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
332 to be used for reading the file
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
333 -- A list of two elements (absolute pathname and length of data inserted),
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
334 which is used as the return value to `insert-file-contents'. In this
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
335 case, `insert-file-contents' assumes that the function has inserted
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
336 the file for itself and suppresses further reading.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
337
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
338 If any function returns non-nil, the remaining functions are not called.")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
339
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
340 (defvar insert-file-contents-error-hook nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
341 "A hook to set `buffer-file-coding-system' when a read error has occurred.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
342
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
343 When a file error (e.g. nonexistent file) occurs while read a file,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
344 `insert-file-contents' calls the functions on this hook with three
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
345 arguments: FILENAME and VISIT (the same as the corresponding arguments
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
346 in the call to `insert-file-contents') and a cons (SIGNALED-CONDITIONS
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
347 . SIGNAL-DATA).
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
348
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
349 After calling this hook, the error is signalled for real and
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
350 propagates to the caller of `insert-file-contents'.")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
351
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
352 (defvar insert-file-contents-post-hook nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
353 "A hook to set `buffer-file-coding-system' for the current buffer.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
354
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
355 After successful reading, `insert-file-contents' calls the functions
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
356 on this hook with four arguments: FILENAME and VISIT (the same as the
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
357 corresponding arguments in the call to `insert-file-contents'),
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
358 CODING-SYSTEM (the actual coding system used to decode the file), and
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
359 a cons of absolute pathname and length of data inserted (the same
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
360 thing as will be returned from `insert-file-contents').")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
361
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
362 (defun insert-file-contents (filename &optional visit beg end replace)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
363 "Insert contents of file FILENAME after point.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
364 Returns list of absolute file name and length of data inserted.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
365 If second argument VISIT is non-nil, the buffer's visited filename
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
366 and last save file modtime are set, and it is marked unmodified.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
367 If visiting and the file does not exist, visiting is completed
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
368 before the error is signaled.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
369
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
370 The optional third and fourth arguments BEG and END
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
371 specify what portion of the file to insert.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
372 If VISIT is non-nil, BEG and END must be nil.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
373 If optional fifth argument REPLACE is non-nil,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
374 it means replace the current buffer contents (in the accessible portion)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
375 with the file contents. This is better than simply deleting and inserting
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
376 the whole thing because (1) it preserves some marker positions
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
377 and (2) it puts less data in the undo list.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
378
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
379 NOTE: When Mule support is enabled, the REPLACE argument is
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
380 currently ignored.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
381
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
382 The coding system used for decoding the file is determined as follows:
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
383
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
384 1. `coding-system-for-read', if non-nil.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
385 2. The result of `insert-file-contents-pre-hook', if non-nil.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
386 3. The matching value for this filename from
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
387 `file-coding-system-alist', if any.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
388 4. `buffer-file-coding-system-for-read', if non-nil.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
389 5. The coding system 'no-conversion.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
390
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
391 If a local value for `buffer-file-coding-system' in the current buffer
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
392 does not exist, it is set to the coding system which was actually used
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
393 for reading.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
394
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
395 See also `insert-file-contents-access-hook',
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
396 `insert-file-contents-pre-hook', `insert-file-contents-error-hook',
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
397 and `insert-file-contents-post-hook'."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
398 (let (return-val coding-system used-codesys conversion-func)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
399 ;; OK, first load the file.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
400 (condition-case err
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
401 (progn
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
402 (run-hook-with-args 'insert-file-contents-access-hook
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
403 filename visit)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
404 ;; determine the coding system to use, as described above.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
405 (setq coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
406 (or
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
407 ;; #1.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
408 coding-system-for-read
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
409 ;; #2.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
410 (run-special-hook-with-args 'insert-file-contents-pre-hook
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
411 filename visit)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
412 ;; #3.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
413 (find-file-coding-system-for-read-from-filename filename)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
414 ;; #4.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
415 buffer-file-coding-system-for-read
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
416 ;; #5.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
417 'no-conversion))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
418 (if (consp coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
419 (setq return-val coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
420 (if (null (find-coding-system coding-system))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
421 (progn
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
422 (message
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
423 "Invalid coding-system (%s), using 'undecided"
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
424 coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
425 (setq coding-system 'undecided)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
426 (setq return-val
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
427 (insert-file-contents-internal filename visit beg end
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
428 replace coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
429 ;; store here!
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
430 'used-codesys))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
431 ))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
432 (file-error
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
433 (run-hook-with-args 'insert-file-contents-error-hook
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
434 filename visit err)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
435 (signal (car err) (cdr err))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
436 (setq coding-system used-codesys)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
437 ;; call any `post-read-conversion' for the coding system that
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
438 ;; was used ...
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
439 (let ((func
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
440 (coding-system-property coding-system 'post-read-conversion))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
441 (endmark (make-marker)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
442 (set-marker endmark (+ (point) (nth 1 return-val)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
443 (if func
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
444 (unwind-protect
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
445 (save-excursion
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
446 (let (buffer-read-only)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
447 (funcall func (point) (marker-position endmark))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
448 (if visit
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
449 (progn
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
450 (set-buffer-auto-saved)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
451 (set-buffer-modified-p nil)))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
452 (setcar (cdr return-val) (- (marker-position endmark) (point))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
453 ;; now finally set the buffer's `buffer-file-coding-system'.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
454 (if (run-special-hook-with-args 'insert-file-contents-post-hook
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
455 filename visit return-val)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
456 nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
457 (if (local-variable-p 'buffer-file-coding-system (current-buffer))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
458 ;; if buffer-file-coding-system is already local, just
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
459 ;; set its eol type to what was found, if it wasn't
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
460 ;; set already.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
461 (set-buffer-file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
462 (subsidiary-coding-system buffer-file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
463 (coding-system-eol-type coding-system)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
464 ;; otherwise actually set buffer-file-coding-system.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
465 (set-buffer-file-coding-system coding-system)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
466 return-val))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
467
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
468 (defvar write-region-pre-hook nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
469 "A special hook to decide the coding system used for writing out a file.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
470
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
471 Before writing a file, `write-region' calls the functions on this hook
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
472 with arguments START, END, FILENAME, APPEND, VISIT, and CODING-SYSTEM,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
473 the same as the corresponding arguments in the call to
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
474 `write-region'.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
475
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
476 The return value of the functions should be either
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
477
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
478 -- nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
479 -- A coding system or a symbol denoting it, indicating the coding system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
480 to be used for reading the file
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
481 -- A list of two elements (absolute pathname and length of data written),
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
482 which is used as the return value to `write-region'. In this
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
483 case, `write-region' assumes that the function has written
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
484 the file for itself and suppresses further writing.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
485
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
486 If any function returns non-nil, the remaining functions are not called.")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
487
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
488 (defvar write-region-post-hook nil
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
489 "A hook called by `write-region' after a file has been written out.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
490
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
491 The functions on this hook are called with arguments START, END,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
492 FILENAME, APPEND, VISIT, and CODING-SYSTEM, the same as the
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
493 corresponding arguments in the call to `write-region'.")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
494
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
495 (defun write-region (start end filename &optional append visit lockname coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
496 "Write current region into specified file.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
497 By default the file's existing contents are replaced by the specified region.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
498 When called from a program, takes three arguments:
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
499 START, END and FILENAME. START and END are buffer positions.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
500 Optional fourth argument APPEND if non-nil means
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
501 append to existing file contents (if any).
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
502 Optional fifth argument VISIT if t means
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
503 set last-save-file-modtime of buffer to this file's modtime
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
504 and mark buffer not modified.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
505 If VISIT is a string, it is a second file name;
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
506 the output goes to FILENAME, but the buffer is marked as visiting VISIT.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
507 VISIT is also the file name to lock and unlock for clash detection.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
508 If VISIT is neither t nor nil nor a string,
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
509 that means do not print the \"Wrote file\" message.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
510 The optional sixth arg LOCKNAME, if non-nil, specifies the name to
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
511 use for locking and unlocking, overriding FILENAME and VISIT.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
512 Kludgy feature: if START is a string, then that string is written
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
513 to the file, instead of any buffer contents, and END is ignored.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
514 Optional seventh argument CODING-SYSTEM specifies the coding system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
515 used to encode the text when it is written out, and defaults to
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
516 the value of `buffer-file-coding-system' in the current buffer.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
517 Interactively, with a prefix arg, you will be prompted for the
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
518 coding system.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
519 See also `write-region-pre-hook' and `write-region-post-hook'."
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
520 (interactive "r\nFWrite region to file: \ni\ni\ni\nZCoding-system: ")
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
521 (setq coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
522 (or coding-system-for-write
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
523 (run-special-hook-with-args
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
524 'write-region-pre-hook start end filename append visit lockname)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
525 coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
526 buffer-file-coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
527 (find-file-coding-system-for-write-from-filename filename)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
528 ))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
529 (if (consp coding-system)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
530 coding-system
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
531 (let ((func
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
532 (coding-system-property coding-system 'pre-write-conversion)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
533 (if func
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
534 (let ((curbuf (current-buffer))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
535 (tempbuf (generate-new-buffer " *temp-write-buffer*"))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
536 (modif (buffer-modified-p)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
537 (unwind-protect
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
538 (save-excursion
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
539 (set-buffer tempbuf)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
540 (erase-buffer)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
541 (insert-buffer-substring curbuf start end)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
542 (funcall func (point-min) (point-max))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
543 (write-region-internal (point-min) (point-max) filename
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
544 append
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
545 (if (eq visit t) nil visit)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
546 lockname
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
547 coding-system))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
548 ;; leaving a buffer associated with file will cause problems
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
549 ;; when next visiting.
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
550 (kill-buffer tempbuf)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
551 (if (or visit (null modif))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
552 (progn
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
553 (set-buffer-auto-saved)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
554 (set-buffer-modified-p nil)
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
555 (if (buffer-file-name) (set-visited-file-modtime))))))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
556 (write-region-internal start end filename append visit lockname
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
557 coding-system)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
558 (run-hook-with-args 'write-region-post-hook
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
559 start end filename append visit lockname
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
560 coding-system)))
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
561
11cf20601dec Import from CVS: tag r20-5b28
cvs
parents:
diff changeset
562 ;;; mule-files.el ends here