]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Merge from emacs-23
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007, 2008, 2009, 2010 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 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
22
23 ;;; Commentary:
24
25 ;; Tramp's main Emacs version for development is GNU Emacs 24. This
26 ;; package provides compatibility functions for GNU Emacs 22, GNU
27 ;; Emacs 23 and XEmacs 21.4+.
28
29 ;;; Code:
30
31 (eval-when-compile
32
33 ;; Pacify byte-compiler.
34 (require 'cl))
35
36 (eval-and-compile
37
38 (require 'custom)
39
40 ;; Load the appropriate timer package.
41 (if (featurep 'xemacs)
42 (require 'timer-funcs)
43 (require 'timer))
44
45 (autoload 'tramp-tramp-file-p "tramp")
46 (autoload 'tramp-file-name-handler "tramp")
47 (autoload 'tramp-handle-file-remote-p "tramp")
48
49 ;; tramp-util offers integration into other (X)Emacs packages like
50 ;; compile.el, gud.el etc. Not necessary in Emacs 23.
51 (eval-after-load "tramp"
52 ;; We check whether `start-file-process' is an alias.
53 '(when (or (not (fboundp 'start-file-process))
54 (symbolp (symbol-function 'start-file-process)))
55 (require 'tramp-util)
56 (add-hook 'tramp-unload-hook
57 '(lambda ()
58 (when (featurep 'tramp-util)
59 (unload-feature 'tramp-util 'force))))))
60
61 ;; Make sure that we get integration with the VC package. When it
62 ;; is loaded, we need to pull in the integration module. Not
63 ;; necessary in Emacs 23.
64 (eval-after-load "vc"
65 (eval-after-load "tramp"
66 ;; We check whether `start-file-process' is an alias.
67 '(when (or (not (fboundp 'start-file-process))
68 (symbolp (symbol-function 'start-file-process)))
69 (require 'tramp-vc)
70 (add-hook 'tramp-unload-hook
71 '(lambda ()
72 (when (featurep 'tramp-vc)
73 (unload-feature 'tramp-vc 'force)))))))
74
75 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
76 ;; Currently, XEmacs supports this.
77 (when (featurep 'xemacs)
78 (unless (boundp 'byte-compile-default-warnings)
79 (defvar byte-compile-default-warnings nil))
80 (delq 'unused-vars byte-compile-default-warnings))
81
82 ;; `last-coding-system-used' is unknown in XEmacs.
83 (unless (boundp 'last-coding-system-used)
84 (defvar last-coding-system-used nil))
85
86 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
87 ;; used in XEmacs, so we set it here and there. The following is
88 ;; needed to pacify Emacs byte-compiler.
89 (unless (boundp 'byte-compile-not-obsolete-var)
90 (defvar byte-compile-not-obsolete-var nil))
91 (setq byte-compile-not-obsolete-var 'directory-sep-char)
92 (if (boundp 'byte-compile-not-obsolete-vars) ; Emacs 23.2
93 (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
94
95 ;; `with-temp-message' does not exists in XEmacs.
96 (condition-case nil
97 (with-temp-message (current-message) nil)
98 (error (defmacro with-temp-message (message &rest body) `(progn ,@body))))
99
100 ;; `set-buffer-multibyte' comes from Emacs Leim.
101 (unless (fboundp 'set-buffer-multibyte)
102 (defalias 'set-buffer-multibyte 'ignore))
103
104 ;; `font-lock-add-keywords' does not exist in XEmacs.
105 (unless (fboundp 'font-lock-add-keywords)
106 (defalias 'font-lock-add-keywords 'ignore))
107
108 ;; The following functions cannot be aliases of the corresponding
109 ;; `tramp-handle-*' functions, because this would bypass the locking
110 ;; mechanism.
111
112 ;; `file-remote-p' has been introduced with Emacs 22. The version
113 ;; of XEmacs is not a magic file name function (yet); this is
114 ;; corrected in tramp-util.el. Here it is sufficient if the
115 ;; function exists.
116 (unless (fboundp 'file-remote-p)
117 (defalias 'file-remote-p
118 (lambda (file &optional identification connected)
119 (when (tramp-tramp-file-p file)
120 (tramp-file-name-handler
121 'file-remote-p file identification connected)))))
122
123 ;; `process-file' does not exist in XEmacs.
124 (unless (fboundp 'process-file)
125 (defalias 'process-file
126 (lambda (program &optional infile buffer display &rest args)
127 (when (tramp-tramp-file-p default-directory)
128 (apply
129 'tramp-file-name-handler
130 'process-file program infile buffer display args)))))
131
132 ;; `start-file-process' is new in Emacs 23.
133 (unless (fboundp 'start-file-process)
134 (defalias 'start-file-process
135 (lambda (name buffer program &rest program-args)
136 (when (tramp-tramp-file-p default-directory)
137 (apply
138 'tramp-file-name-handler
139 'start-file-process name buffer program program-args)))))
140
141 ;; `set-file-times' is also new in Emacs 23.
142 (unless (fboundp 'set-file-times)
143 (defalias 'set-file-times
144 (lambda (filename &optional time)
145 (when (tramp-tramp-file-p filename)
146 (tramp-file-name-handler
147 'set-file-times filename time)))))
148
149 ;; We currently use "[" and "]" in the filename format for IPv6
150 ;; hosts of GNU Emacs. This means, that Emacs wants to expand
151 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
152 ;; because no expansion could be found. We detect this situation
153 ;; and do something really awful: we have `file-expand-wildcards'
154 ;; return the original filename if it can't expand anything. Let's
155 ;; just hope that this doesn't break anything else.
156 ;; It is not needed anymore since GNU Emacs 23.2.
157 (unless (or (featurep 'xemacs)
158 ;; `featurep' has only one argument in XEmacs.
159 (funcall 'featurep 'files 'remote-wildcards))
160 (defadvice file-expand-wildcards
161 (around tramp-advice-file-expand-wildcards activate)
162 (let ((name (ad-get-arg 0)))
163 ;; If it's a Tramp file, look if wildcards need to be expanded
164 ;; at all.
165 (if (and
166 (tramp-tramp-file-p name)
167 (not (string-match
168 "[[*?]" (tramp-handle-file-remote-p name 'localname))))
169 (setq ad-return-value (list name))
170 ;; Otherwise, just run the original function.
171 ad-do-it)))
172 (add-hook
173 'tramp-unload-hook
174 (lambda ()
175 (ad-remove-advice
176 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
177 (ad-activate 'file-expand-wildcards)))))
178
179 (defsubst tramp-compat-line-beginning-position ()
180 "Return point at beginning of line (compat function).
181 Calls `line-beginning-position' or `point-at-bol' if defined, else
182 own implementation."
183 (cond
184 ((fboundp 'line-beginning-position)
185 (funcall (symbol-function 'line-beginning-position)))
186 ((fboundp 'point-at-bol) (funcall (symbol-function 'point-at-bol)))
187 (t (save-excursion (beginning-of-line) (point)))))
188
189 (defsubst tramp-compat-line-end-position ()
190 "Return point at end of line (compat function).
191 Calls `line-end-position' or `point-at-eol' if defined, else
192 own implementation."
193 (cond
194 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
195 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
196 (t (save-excursion (end-of-line) (point)))))
197
198 (defsubst tramp-compat-temporary-file-directory ()
199 "Return name of directory for temporary files (compat function).
200 For Emacs, this is the variable `temporary-file-directory', for XEmacs
201 this is the function `temp-directory'."
202 (cond
203 ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
204 ((fboundp 'temp-directory) (funcall (symbol-function 'temp-directory)))
205 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
206 (file-name-as-directory (getenv "TEMP")))
207 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
208 (file-name-as-directory (getenv "TMP")))
209 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
210 (file-name-as-directory (getenv "TMPDIR")))
211 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
212 (t (message (concat "Neither `temporary-file-directory' nor "
213 "`temp-directory' is defined -- using /tmp."))
214 (file-name-as-directory "/tmp"))))
215
216 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
217 ;; implementation with `make-temp-name', creating the temporary file
218 ;; immediately in order to avoid a security hole.
219 (defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
220 "Create a temporary file (compat function).
221 Add the extension of FILENAME, if existing."
222 (let* (file-name-handler-alist
223 (prefix (expand-file-name
224 (symbol-value 'tramp-temp-name-prefix)
225 (tramp-compat-temporary-file-directory)))
226 (extension (file-name-extension filename t))
227 result)
228 (if (fboundp 'make-temp-file)
229 (setq result
230 (funcall
231 (symbol-function 'make-temp-file) prefix dir-flag extension))
232 ;; We use our own implementation, taken from files.el.
233 (while
234 (condition-case ()
235 (progn
236 (setq result (concat (make-temp-name prefix) extension))
237 (if dir-flag
238 (make-directory result)
239 (write-region "" nil result nil 'silent))
240 nil)
241 (file-already-exists t))
242 ;; The file was somehow created by someone else between
243 ;; `make-temp-name' and `write-region', let's try again.
244 nil))
245 result))
246
247 ;; `most-positive-fixnum' does not exist in XEmacs.
248 (defsubst tramp-compat-most-positive-fixnum ()
249 "Return largest positive integer value (compat function)."
250 (cond
251 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
252 ;; Default value in XEmacs.
253 (t 134217727)))
254
255 ;; ID-FORMAT does not exists in XEmacs.
256 (defun tramp-compat-file-attributes (filename &optional id-format)
257 "Like `file-attributes' for Tramp files (compat function)."
258 (cond
259 ((or (null id-format) (eq id-format 'integer))
260 (file-attributes filename))
261 ((tramp-tramp-file-p filename)
262 (tramp-file-name-handler 'file-attributes filename id-format))
263 (t (condition-case nil
264 (funcall (symbol-function 'file-attributes) filename id-format)
265 (error (file-attributes filename))))))
266
267 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
268 ;; hurt to ignore it for other (X)Emacs versions.
269 (defun tramp-compat-copy-file
270 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
271 "Like `copy-file' for Tramp files (compat function)."
272 (if preserve-uid-gid
273 (funcall
274 (symbol-function 'copy-file)
275 filename newname ok-if-already-exists keep-date preserve-uid-gid)
276 (copy-file filename newname ok-if-already-exists keep-date)))
277
278 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
279 ;; is taken from there.
280 (defun tramp-compat-copy-directory
281 (directory newname &optional keep-time parents)
282 "Make a copy of DIRECTORY (compat function)."
283 (if (fboundp 'copy-directory)
284 (funcall
285 (symbol-function 'copy-directory) directory newname keep-time parents)
286
287 ;; If `default-directory' is a remote directory, make sure we find
288 ;; its `copy-directory' handler.
289 (let ((handler (or (find-file-name-handler directory 'copy-directory)
290 (find-file-name-handler newname 'copy-directory))))
291 (if handler
292 (funcall handler 'copy-directory directory newname keep-time parents)
293
294 ;; Compute target name.
295 (setq directory (directory-file-name (expand-file-name directory))
296 newname (directory-file-name (expand-file-name newname)))
297 (if (and (file-directory-p newname)
298 (not (string-equal (file-name-nondirectory directory)
299 (file-name-nondirectory newname))))
300 (setq newname
301 (expand-file-name
302 (file-name-nondirectory directory) newname)))
303 (if (not (file-directory-p newname)) (make-directory newname parents))
304
305 ;; Copy recursively.
306 (mapc
307 (lambda (file)
308 (if (file-directory-p file)
309 (tramp-compat-copy-directory file newname keep-time parents)
310 (copy-file file newname t keep-time)))
311 ;; We do not want to delete "." and "..".
312 (directory-files
313 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
314
315 ;; Set directory attributes.
316 (set-file-modes newname (file-modes directory))
317 (if keep-time
318 (set-file-times newname (nth 5 (file-attributes directory))))))))
319
320 ;; RECURSIVE has been introduced with Emacs 23.2.
321 (defun tramp-compat-delete-directory (directory &optional recursive)
322 "Like `delete-directory' for Tramp files (compat function)."
323 (if (null recursive)
324 (delete-directory directory)
325 (condition-case nil
326 (funcall (symbol-function 'delete-directory) directory recursive)
327 ;; This Emacs version does not support the RECURSIVE flag. We
328 ;; use the implementation from Emacs 23.2.
329 (error
330 (setq directory (directory-file-name (expand-file-name directory)))
331 (if (not (file-symlink-p directory))
332 (mapc (lambda (file)
333 (if (eq t (car (file-attributes file)))
334 (tramp-compat-delete-directory file recursive)
335 (delete-file file)))
336 (directory-files
337 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
338 (delete-directory directory)))))
339
340 ;; `number-sequence' does not exist in XEmacs. Implementation is
341 ;; taken from Emacs 23.
342 (defun tramp-compat-number-sequence (from &optional to inc)
343 "Return a sequence of numbers from FROM to TO as a list (compat function)."
344 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
345 (funcall (symbol-function 'number-sequence) from to inc)
346 (if (or (not to) (= from to))
347 (list from)
348 (or inc (setq inc 1))
349 (when (zerop inc) (error "The increment can not be zero"))
350 (let (seq (n 0) (next from))
351 (if (> inc 0)
352 (while (<= next to)
353 (setq seq (cons next seq)
354 n (1+ n)
355 next (+ from (* n inc))))
356 (while (>= next to)
357 (setq seq (cons next seq)
358 n (1+ n)
359 next (+ from (* n inc)))))
360 (nreverse seq)))))
361
362 (defun tramp-compat-split-string (string pattern)
363 "Like `split-string' but omit empty strings.
364 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
365 This is, the first, empty, element is omitted. In XEmacs, the first
366 element is not omitted."
367 (delete "" (split-string string pattern)))
368
369 (defun tramp-compat-process-running-p (process-name)
370 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
371 (when (stringp process-name)
372 (cond
373 ;; GNU Emacs 22 on w32.
374 ((fboundp 'w32-window-exists-p)
375 (funcall (symbol-function 'w32-window-exists-p)
376 process-name process-name))
377
378 ;; GNU Emacs 23.
379 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
380 (let (result)
381 (dolist (pid (funcall (symbol-function 'list-system-processes)) result)
382 (let ((attributes
383 (funcall (symbol-function 'process-attributes) pid)))
384 (when (and (string-equal
385 (cdr (assoc 'user attributes)) (user-login-name))
386 (let ((comm (cdr (assoc 'comm attributes))))
387 ;; The returned command name could be truncated
388 ;; to 15 characters. Therefore, we cannot check
389 ;; for `string-equal'.
390 (and comm (string-match
391 (concat "^" (regexp-quote comm))
392 process-name))))
393 (setq result t))))))
394
395 ;; Fallback, if there is no Lisp support yet.
396 (t (let ((default-directory
397 (if (file-remote-p default-directory)
398 (tramp-compat-temporary-file-directory)
399 default-directory))
400 (unix95 (getenv "UNIX95"))
401 result)
402 (setenv "UNIX95" "1")
403 (when (member
404 (user-login-name)
405 (tramp-compat-split-string
406 (shell-command-to-string
407 (format "ps -C %s -o user=" process-name))
408 "[ \f\t\n\r\v]+"))
409 (setq result t))
410 (setenv "UNIX95" unix95)
411 result)))))
412
413 (provide 'tramp-compat)
414
415 ;;; TODO:
416
417 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
418 ;;; tramp-compat.el ends here