]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Merge from emacs--rel--22
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007, 2008 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 3, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, see
22 ;; <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Tramp's main Emacs version for development is GNU Emacs 23. This
27 ;; package provides compatibility functions for GNU Emacs 21, GNU
28 ;; Emacs 22 and XEmacs 21.4+.
29
30 ;;; Code:
31
32 (eval-when-compile
33
34 ;; Pacify byte-compiler.
35 (require 'cl))
36
37 (eval-and-compile
38
39 (require 'custom)
40
41 ;; Load the appropriate timer package.
42 (if (featurep 'xemacs)
43 (require 'timer-funcs)
44 (require 'timer))
45
46 ;; tramp-util offers integration into other (X)Emacs packages like
47 ;; compile.el, gud.el etc. Not necessary in Emacs 23.
48 (eval-after-load "tramp"
49 ;; We check whether `start-file-process' is an alias.
50 '(when (or (not (fboundp 'start-file-process))
51 (symbolp (symbol-function 'start-file-process)))
52 (require 'tramp-util)
53 (add-hook 'tramp-unload-hook
54 '(lambda ()
55 (when (featurep 'tramp-util)
56 (unload-feature 'tramp-util 'force))))))
57
58 ;; Make sure that we get integration with the VC package. When it
59 ;; is loaded, we need to pull in the integration module. Not
60 ;; necessary in Emacs 23.
61 (eval-after-load "vc"
62 (eval-after-load "tramp"
63 ;; We check whether `start-file-process' is an alias.
64 '(when (or (not (fboundp 'start-file-process))
65 (symbolp (symbol-function 'start-file-process)))
66 (require 'tramp-vc)
67 (add-hook 'tramp-unload-hook
68 '(lambda ()
69 (when (featurep 'tramp-vc)
70 (unload-feature 'tramp-vc 'force)))))))
71
72 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
73 ;; Currently, XEmacs supports this.
74 (when (featurep 'xemacs)
75 (unless (boundp 'byte-compile-default-warnings)
76 (defvar byte-compile-default-warnings nil))
77 (delq 'unused-vars byte-compile-default-warnings))
78
79 ;; `last-coding-system-used' is unknown in XEmacs.
80 (unless (boundp 'last-coding-system-used)
81 (defvar last-coding-system-used nil))
82
83 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
84 ;; used in XEmacs, so we set it here and there. The following is
85 ;; needed to pacify Emacs byte-compiler.
86 (unless (boundp 'byte-compile-not-obsolete-var)
87 (defvar byte-compile-not-obsolete-var nil))
88 (setq byte-compile-not-obsolete-var 'directory-sep-char)
89
90 ;; `with-temp-message' does not exists in XEmacs.
91 (condition-case nil
92 (with-temp-message (current-message) nil)
93 (error (defmacro with-temp-message (message &rest body) `(progn ,@body))))
94
95 ;; `set-buffer-multibyte' comes from Emacs Leim.
96 (unless (fboundp 'set-buffer-multibyte)
97 (defalias 'set-buffer-multibyte 'ignore))
98
99 ;; `font-lock-add-keywords' does not exist in XEmacs.
100 (unless (fboundp 'font-lock-add-keywords)
101 (defalias 'font-lock-add-keywords 'ignore))
102
103 ;; `file-remote-p' has been introduced with Emacs 22. The version
104 ;; of XEmacs is not a magic file name function (yet); this is
105 ;; corrected in tramp-util.el. Here it is sufficient if the
106 ;; function exists.
107 (unless (fboundp 'file-remote-p)
108 (defalias 'file-remote-p 'tramp-handle-file-remote-p))
109
110 ;; `process-file' exists since Emacs 22.
111 (unless (fboundp 'process-file)
112 (defalias 'process-file 'tramp-handle-process-file))
113
114 ;; `start-file-process' is new in Emacs 23.
115 (unless (fboundp 'start-file-process)
116 (defalias 'start-file-process 'tramp-handle-start-file-process))
117
118 ;; `set-file-times' is also new in Emacs 23.
119 (unless (fboundp 'set-file-times)
120 (defalias 'set-file-times 'tramp-handle-set-file-times)))
121
122 (defsubst tramp-compat-line-end-position ()
123 "Return point at end of line (compat function).
124 Calls `line-end-position' or `point-at-eol' if defined, else
125 own implementation."
126 (cond
127 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
128 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
129 (t (save-excursion (end-of-line) (point)))))
130
131 (defsubst tramp-compat-temporary-file-directory ()
132 "Return name of directory for temporary files (compat function).
133 For Emacs, this is the variable `temporary-file-directory', for XEmacs
134 this is the function `temp-directory'."
135 (cond
136 ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
137 ((fboundp 'temp-directory) (funcall (symbol-function 'temp-directory)))
138 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
139 (file-name-as-directory (getenv "TEMP")))
140 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
141 (file-name-as-directory (getenv "TMP")))
142 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
143 (file-name-as-directory (getenv "TMPDIR")))
144 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
145 (t (message (concat "Neither `temporary-file-directory' nor "
146 "`temp-directory' is defined -- using /tmp."))
147 (file-name-as-directory "/tmp"))))
148
149 ;; `make-temp-file' exists in Emacs only. The third parameter SUFFIX
150 ;; has been introduced with Emacs 22. We try it, if it fails, we fall
151 ;; back to `make-temp-name', creating the temporary file immediately
152 ;; in order to avoid a security hole.
153 (defsubst tramp-compat-make-temp-file (filename)
154 "Create a temporary file (compat function).
155 Add the extension of FILENAME, if existing."
156 (let ((prefix (expand-file-name
157 (symbol-value 'tramp-temp-name-prefix)
158 (tramp-compat-temporary-file-directory)))
159 (extension (file-name-extension filename t))
160 result)
161 (condition-case nil
162 (setq result
163 (funcall (symbol-function 'make-temp-file) prefix nil extension))
164 (error
165 ;; We use our own implementation, taken from files.el.
166 (while
167 (condition-case ()
168 (progn
169 (setq result (concat (make-temp-name prefix) extension))
170 (write-region
171 "" nil result nil 'silent nil
172 ;; 7th parameter is MUSTBENEW in Emacs, and
173 ;; CODING-SYSTEM in XEmacs. It is not a security
174 ;; hole in XEmacs if we cannot use this parameter,
175 ;; because XEmacs uses a user-specific subdirectory
176 ;; with 0700 permissions.
177 (when (not (featurep 'xemacs)) 'excl))
178 nil)
179 (file-already-exists t))
180 ;; The file was somehow created by someone else between
181 ;; `make-temp-name' and `write-region', let's try again.
182 nil)))
183 result))
184
185 ;; `most-positive-fixnum' arrived in Emacs 22. Before, and in XEmacs,
186 ;; it is a fixed value.
187 (defsubst tramp-compat-most-positive-fixnum ()
188 "Return largest positive integer value (compat function)."
189 (cond
190 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
191 ;; Default value in XEmacs and Emacs 21.
192 (t 134217727)))
193
194 ;; ID-FORMAT exists since Emacs 22.
195 (defun tramp-compat-file-attributes (filename &optional id-format)
196 "Like `file-attributes' for Tramp files (compat function)."
197 (cond
198 ((or (null id-format) (eq id-format 'integer))
199 (file-attributes filename))
200 ;; FIXME: shouldn't that be tramp-file-p or somesuch?
201 ((file-remote-p filename)
202 (funcall (symbol-function 'tramp-handle-file-attributes)
203 filename id-format))
204 (t (condition-case nil
205 (funcall (symbol-function 'file-attributes) filename id-format)
206 (error (file-attributes filename))))))
207
208 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
209 ;; hurt to ignore it for other (X)Emacs versions.
210 (defun tramp-compat-copy-file
211 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
212 "Like `copy-file' for Tramp files (compat function)."
213 (if preserve-uid-gid
214 (funcall
215 (symbol-function 'copy-file)
216 filename newname ok-if-already-exists keep-date preserve-uid-gid)
217 (copy-file filename newname ok-if-already-exists keep-date)))
218
219 ;; `copy-tree' is a built-in function in XEmacs. In Emacs 21, it is
220 ;; an autoloaded function in cl-extra.el. Since Emacs 22, it is part
221 ;; of subr.el. There are problems when autoloading, therefore we test
222 ;; for `subrp' and `symbol-file'. Implementation is taken from Emacs23.
223 (defun tramp-compat-copy-tree (tree)
224 "Make a copy of TREE (compat function)."
225 (if (or (subrp 'copy-tree) (symbol-file 'copy-tree))
226 (funcall (symbol-function 'copy-tree) tree)
227 (let (result)
228 (while (consp tree)
229 (let ((newcar (car tree)))
230 (if (consp (car tree))
231 (setq newcar (tramp-compat-copy-tree (car tree))))
232 (push newcar result))
233 (setq tree (cdr tree)))
234 (nconc (nreverse result) tree))))
235
236 (provide 'tramp-compat)
237
238 ;;; TODO:
239
240 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
241 ;;; tramp-compat.el ends here