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