annotate lisp/mule/mule-files.el @ 80:1ce6082ce73f r20-0b90

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