]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Merge from emacs-24; up to 2012-04-25T15:23:19Z!sdl.web@gmail.com
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007-2012 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Tramp's main Emacs version for development is Emacs 24. This
27 ;; package provides compatibility functions for Emacs 22, Emacs 23,
28 ;; XEmacs 21.4+ and SXEmacs 22.
29
30 ;;; Code:
31
32 (require 'tramp-loaddefs)
33
34 (eval-when-compile
35
36 ;; Pacify byte-compiler.
37 (require 'cl))
38
39 (eval-and-compile
40
41 (require 'advice)
42 (require 'custom)
43 (require 'format-spec)
44 (require 'shell)
45
46 ;; As long as password.el is not part of (X)Emacs, it shouldn't be
47 ;; mandatory.
48 (if (featurep 'xemacs)
49 (load "password" 'noerror)
50 (or (require 'password-cache nil 'noerror)
51 (require 'password nil 'noerror))) ; Part of contrib.
52
53 ;; auth-source is relatively new.
54 (if (featurep 'xemacs)
55 (load "auth-source" 'noerror)
56 (require 'auth-source nil 'noerror))
57
58 ;; Load the appropriate timer package.
59 (if (featurep 'xemacs)
60 (require 'timer-funcs)
61 (require 'timer))
62
63 ;; We check whether `start-file-process' is bound.
64 (unless (fboundp 'start-file-process)
65
66 ;; tramp-util offers integration into other (X)Emacs packages like
67 ;; compile.el, gud.el etc. Not necessary in Emacs 23.
68 (eval-after-load "tramp"
69 '(require 'tramp-util))
70
71 ;; Make sure that we get integration with the VC package. When it
72 ;; is loaded, we need to pull in the integration module. Not
73 ;; necessary in Emacs 23.
74 (eval-after-load "vc"
75 (eval-after-load "tramp"
76 '(require 'tramp-vc))))
77
78 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
79 ;; Currently, XEmacs supports this.
80 (when (featurep 'xemacs)
81 (unless (boundp 'byte-compile-default-warnings)
82 (defvar byte-compile-default-warnings nil))
83 (delq 'unused-vars byte-compile-default-warnings))
84
85 ;; `last-coding-system-used' is unknown in XEmacs.
86 (unless (boundp 'last-coding-system-used)
87 (defvar last-coding-system-used nil))
88
89 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
90 ;; used in XEmacs, so we set it here and there. The following is
91 ;; needed to pacify Emacs byte-compiler.
92 ;; Note that it was removed altogether in Emacs 24.1.
93 (when (boundp 'directory-sep-char)
94 (defvar byte-compile-not-obsolete-var nil)
95 (setq byte-compile-not-obsolete-var 'directory-sep-char)
96 ;; Emacs 23.2.
97 (defvar byte-compile-not-obsolete-vars nil)
98 (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
99
100 ;; `remote-file-name-inhibit-cache' has been introduced with Emacs 24.1.
101 ;; Besides `t', `nil', and integer, we use also timestamps (as
102 ;; returned by `current-time') internally.
103 (defvar remote-file-name-inhibit-cache nil)
104
105 ;; For not existing functions, or functions with a changed argument
106 ;; list, there are compiler warnings. We want to avoid them in
107 ;; cases we know what we do.
108 (defmacro tramp-compat-funcall (function &rest arguments)
109 (if (featurep 'xemacs)
110 `(funcall (symbol-function ,function) ,@arguments)
111 `(when (or (subrp ,function) (functionp ,function))
112 (with-no-warnings (funcall ,function ,@arguments)))))
113
114 ;; `set-buffer-multibyte' comes from Emacs Leim.
115 (unless (fboundp 'set-buffer-multibyte)
116 (defalias 'set-buffer-multibyte 'ignore))
117
118 ;; The following functions cannot be aliases of the corresponding
119 ;; `tramp-handle-*' functions, because this would bypass the locking
120 ;; mechanism.
121
122 ;; `file-remote-p' has been introduced with Emacs 22. The version
123 ;; of XEmacs is not a magic file name function (yet); this is
124 ;; corrected in tramp-util.el. Here it is sufficient if the
125 ;; function exists.
126 (unless (fboundp 'file-remote-p)
127 (defalias 'file-remote-p
128 (lambda (file &optional identification connected)
129 (when (tramp-tramp-file-p file)
130 (tramp-file-name-handler
131 'file-remote-p file identification connected)))))
132
133 ;; `process-file' does not exist in XEmacs.
134 (unless (fboundp 'process-file)
135 (defalias 'process-file
136 (lambda (program &optional infile buffer display &rest args)
137 (when (tramp-tramp-file-p default-directory)
138 (apply
139 'tramp-file-name-handler
140 'process-file program infile buffer display args)))))
141
142 ;; `start-file-process' is new in Emacs 23.
143 (unless (fboundp 'start-file-process)
144 (defalias 'start-file-process
145 (lambda (name buffer program &rest program-args)
146 (when (tramp-tramp-file-p default-directory)
147 (apply
148 'tramp-file-name-handler
149 'start-file-process name buffer program program-args)))))
150
151 ;; `set-file-times' is also new in Emacs 23.
152 (unless (fboundp 'set-file-times)
153 (defalias 'set-file-times
154 (lambda (filename &optional time)
155 (when (tramp-tramp-file-p filename)
156 (tramp-file-name-handler
157 'set-file-times filename time)))))
158
159 ;; We currently use "[" and "]" in the filename format for IPv6
160 ;; hosts of GNU Emacs. This means that Emacs wants to expand
161 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
162 ;; because no expansion could be found. We detect this situation
163 ;; and do something really awful: we have `file-expand-wildcards'
164 ;; return the original filename if it can't expand anything. Let's
165 ;; just hope that this doesn't break anything else.
166 ;; It is not needed anymore since GNU Emacs 23.2.
167 (unless (or (featurep 'xemacs)
168 ;; `featurep' has only one argument in XEmacs.
169 (funcall 'featurep 'files 'remote-wildcards))
170 (defadvice file-expand-wildcards
171 (around tramp-advice-file-expand-wildcards activate)
172 (let ((name (ad-get-arg 0)))
173 ;; If it's a Tramp file, look if wildcards need to be expanded
174 ;; at all.
175 (if (and
176 (tramp-tramp-file-p name)
177 (not (string-match
178 "[[*?]" (tramp-compat-funcall
179 'file-remote-p name 'localname))))
180 (setq ad-return-value (list name))
181 ;; Otherwise, just run the original function.
182 ad-do-it)))
183 (add-hook
184 'tramp-unload-hook
185 (lambda ()
186 (ad-remove-advice
187 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
188 (ad-activate 'file-expand-wildcards)))))
189
190 ;; `with-temp-message' does not exists in XEmacs.
191 (if (fboundp 'with-temp-message)
192 (defalias 'tramp-compat-with-temp-message 'with-temp-message)
193 (defmacro tramp-compat-with-temp-message (message &rest body)
194 "Display MESSAGE temporarily if non-nil while BODY is evaluated."
195 `(progn ,@body)))
196
197 ;; `font-lock-add-keywords' does not exist in XEmacs.
198 (defun tramp-compat-font-lock-add-keywords (mode keywords &optional how)
199 "Add highlighting KEYWORDS for MODE."
200 (ignore-errors
201 (tramp-compat-funcall 'font-lock-add-keywords mode keywords how)))
202
203 (defsubst tramp-compat-temporary-file-directory ()
204 "Return name of directory for temporary files (compat function).
205 For Emacs, this is the variable `temporary-file-directory', for XEmacs
206 this is the function `temp-directory'."
207 (let (file-name-handler-alist)
208 (cond
209 ;; We must return a local directory. If it is remote, we could
210 ;; run into an infloop.
211 ((boundp 'temporary-file-directory)
212 (eval (car (get 'temporary-file-directory 'standard-value))))
213 ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
214 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
215 (file-name-as-directory (getenv "TEMP")))
216 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
217 (file-name-as-directory (getenv "TMP")))
218 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
219 (file-name-as-directory (getenv "TMPDIR")))
220 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
221 (t (message (concat "Neither `temporary-file-directory' nor "
222 "`temp-directory' is defined -- using /tmp."))
223 (file-name-as-directory "/tmp")))))
224
225 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
226 ;; implementation with `make-temp-name', creating the temporary file
227 ;; immediately in order to avoid a security hole.
228 (defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
229 "Create a temporary file (compat function).
230 Add the extension of FILENAME, if existing."
231 (let* (file-name-handler-alist
232 (prefix (expand-file-name
233 (symbol-value 'tramp-temp-name-prefix)
234 (tramp-compat-temporary-file-directory)))
235 (extension (file-name-extension filename t))
236 result)
237 (condition-case nil
238 (setq result
239 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
240 (error
241 ;; We use our own implementation, taken from files.el.
242 (while
243 (condition-case ()
244 (progn
245 (setq result (concat (make-temp-name prefix) extension))
246 (if dir-flag
247 (make-directory result)
248 (write-region "" nil result nil 'silent))
249 nil)
250 (file-already-exists t))
251 ;; The file was somehow created by someone else between
252 ;; `make-temp-name' and `write-region', let's try again.
253 nil)))
254 result))
255
256 ;; `most-positive-fixnum' does not exist in XEmacs.
257 (defsubst tramp-compat-most-positive-fixnum ()
258 "Return largest positive integer value (compat function)."
259 (cond
260 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
261 ;; Default value in XEmacs.
262 (t 134217727)))
263
264 (defun tramp-compat-decimal-to-octal (i)
265 "Return a string consisting of the octal digits of I.
266 Not actually used. Use `(format \"%o\" i)' instead?"
267 (cond ((< i 0) (error "Cannot convert negative number to octal"))
268 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
269 ((zerop i) "0")
270 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
271 (number-to-string (% i 8))))))
272
273 ;; Kudos to Gerd Moellmann for this suggestion.
274 (defun tramp-compat-octal-to-decimal (ostr)
275 "Given a string of octal digits, return a decimal number."
276 (let ((x (or ostr "")))
277 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
278 (unless (string-match "\\`[0-7]*\\'" x)
279 (error "Non-octal junk in string `%s'" x))
280 (string-to-number ostr 8)))
281
282 ;; ID-FORMAT does not exists in XEmacs.
283 (defun tramp-compat-file-attributes (filename &optional id-format)
284 "Like `file-attributes' for Tramp files (compat function)."
285 (cond
286 ((or (null id-format) (eq id-format 'integer))
287 (file-attributes filename))
288 ((tramp-tramp-file-p filename)
289 (tramp-file-name-handler 'file-attributes filename id-format))
290 (t (condition-case nil
291 (tramp-compat-funcall 'file-attributes filename id-format)
292 (wrong-number-of-arguments (file-attributes filename))))))
293
294 ;; PRESERVE-UID-GID does not exist in XEmacs.
295 ;; PRESERVE-SELINUX-CONTEXT has been introduced with Emacs 24.1.
296 (defun tramp-compat-copy-file
297 (filename newname &optional ok-if-already-exists keep-date
298 preserve-uid-gid preserve-selinux-context)
299 "Like `copy-file' for Tramp files (compat function)."
300 (cond
301 (preserve-selinux-context
302 (tramp-compat-funcall
303 'copy-file filename newname ok-if-already-exists keep-date
304 preserve-uid-gid preserve-selinux-context))
305 (preserve-uid-gid
306 (tramp-compat-funcall
307 'copy-file filename newname ok-if-already-exists keep-date
308 preserve-uid-gid))
309 (t
310 (copy-file filename newname ok-if-already-exists keep-date))))
311
312 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
313 ;; is taken from there.
314 (defun tramp-compat-copy-directory
315 (directory newname &optional keep-time parents)
316 "Make a copy of DIRECTORY (compat function)."
317 (if (fboundp 'copy-directory)
318 (tramp-compat-funcall 'copy-directory directory newname keep-time parents)
319
320 ;; If `default-directory' is a remote directory, make sure we find
321 ;; its `copy-directory' handler.
322 (let ((handler (or (find-file-name-handler directory 'copy-directory)
323 (find-file-name-handler newname 'copy-directory))))
324 (if handler
325 (funcall handler 'copy-directory directory newname keep-time parents)
326
327 ;; Compute target name.
328 (setq directory (directory-file-name (expand-file-name directory))
329 newname (directory-file-name (expand-file-name newname)))
330 (if (and (file-directory-p newname)
331 (not (string-equal (file-name-nondirectory directory)
332 (file-name-nondirectory newname))))
333 (setq newname
334 (expand-file-name
335 (file-name-nondirectory directory) newname)))
336 (if (not (file-directory-p newname)) (make-directory newname parents))
337
338 ;; Copy recursively.
339 (mapc
340 (lambda (file)
341 (if (file-directory-p file)
342 (tramp-compat-copy-directory file newname keep-time parents)
343 (copy-file file newname t keep-time)))
344 ;; We do not want to delete "." and "..".
345 (directory-files
346 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
347
348 ;; Set directory attributes.
349 (set-file-modes newname (file-modes directory))
350 (if keep-time
351 (set-file-times newname (nth 5 (file-attributes directory))))))))
352
353 ;; TRASH has been introduced with Emacs 24.1.
354 (defun tramp-compat-delete-file (filename &optional trash)
355 "Like `delete-file' for Tramp files (compat function)."
356 (condition-case nil
357 (tramp-compat-funcall 'delete-file filename trash)
358 ;; This Emacs version does not support the TRASH flag.
359 (wrong-number-of-arguments
360 (let ((delete-by-moving-to-trash
361 (and (boundp 'delete-by-moving-to-trash)
362 (symbol-value 'delete-by-moving-to-trash)
363 trash)))
364 (delete-file filename)))))
365
366 ;; RECURSIVE has been introduced with Emacs 23.2.
367 (defun tramp-compat-delete-directory (directory &optional recursive)
368 "Like `delete-directory' for Tramp files (compat function)."
369 (if (null recursive)
370 (delete-directory directory)
371 (condition-case nil
372 (tramp-compat-funcall 'delete-directory directory recursive)
373 ;; This Emacs version does not support the RECURSIVE flag. We
374 ;; use the implementation from Emacs 23.2.
375 (wrong-number-of-arguments
376 (setq directory (directory-file-name (expand-file-name directory)))
377 (if (not (file-symlink-p directory))
378 (mapc (lambda (file)
379 (if (eq t (car (file-attributes file)))
380 (tramp-compat-delete-directory file recursive)
381 (delete-file file)))
382 (directory-files
383 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
384 (delete-directory directory)))))
385
386 ;; `number-sequence' does not exist in XEmacs. Implementation is
387 ;; taken from Emacs 23.
388 (defun tramp-compat-number-sequence (from &optional to inc)
389 "Return a sequence of numbers from FROM to TO as a list (compat function)."
390 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
391 (tramp-compat-funcall 'number-sequence from to inc)
392 (if (or (not to) (= from to))
393 (list from)
394 (or inc (setq inc 1))
395 (when (zerop inc) (error "The increment can not be zero"))
396 (let (seq (n 0) (next from))
397 (if (> inc 0)
398 (while (<= next to)
399 (setq seq (cons next seq)
400 n (1+ n)
401 next (+ from (* n inc))))
402 (while (>= next to)
403 (setq seq (cons next seq)
404 n (1+ n)
405 next (+ from (* n inc)))))
406 (nreverse seq)))))
407
408 (defun tramp-compat-split-string (string pattern)
409 "Like `split-string' but omit empty strings.
410 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
411 This is, the first, empty, element is omitted. In XEmacs, the first
412 element is not omitted."
413 (delete "" (split-string string pattern)))
414
415 (defun tramp-compat-call-process
416 (program &optional infile destination display &rest args)
417 "Calls `call-process' on the local host.
418 This is needed because for some Emacs flavors Tramp has
419 defadvised `call-process' to behave like `process-file'. The
420 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
421 (let ((default-directory
422 (if (file-remote-p default-directory)
423 (tramp-compat-temporary-file-directory)
424 default-directory)))
425 (if (executable-find program)
426 (apply 'call-process program infile destination display args)
427 1)))
428
429 (defun tramp-compat-process-running-p (process-name)
430 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
431 (when (stringp process-name)
432 (cond
433 ;; GNU Emacs 22 on w32.
434 ((fboundp 'w32-window-exists-p)
435 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
436
437 ;; GNU Emacs 23.
438 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
439 (let (result)
440 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
441 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
442 (when (and (string-equal
443 (cdr (assoc 'user attributes)) (user-login-name))
444 (let ((comm (cdr (assoc 'comm attributes))))
445 ;; The returned command name could be truncated
446 ;; to 15 characters. Therefore, we cannot check
447 ;; for `string-equal'.
448 (and comm (string-match
449 (concat "^" (regexp-quote comm))
450 process-name))))
451 (setq result t))))))
452
453 ;; Fallback, if there is no Lisp support yet.
454 (t (let ((default-directory
455 (if (file-remote-p default-directory)
456 (tramp-compat-temporary-file-directory)
457 default-directory))
458 (unix95 (getenv "UNIX95"))
459 result)
460 (setenv "UNIX95" "1")
461 (when (member
462 (user-login-name)
463 (tramp-compat-split-string
464 (shell-command-to-string
465 (format "ps -C %s -o user=" process-name))
466 "[ \f\t\n\r\v]+"))
467 (setq result t))
468 (setenv "UNIX95" unix95)
469 result)))))
470
471 ;; The following functions do not exist in XEmacs. We ignore this;
472 ;; they are used for checking a remote tty.
473 (defun tramp-compat-process-get (process propname)
474 "Return the value of PROCESS' PROPNAME property.
475 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
476 (ignore-errors (tramp-compat-funcall 'process-get process propname)))
477
478 (defun tramp-compat-process-put (process propname value)
479 "Change PROCESS' PROPNAME property to VALUE.
480 It can be retrieved with `(process-get PROCESS PROPNAME)'."
481 (ignore-errors (tramp-compat-funcall 'process-put process propname value)))
482
483 (defun tramp-compat-set-process-query-on-exit-flag (process flag)
484 "Specify if query is needed for process when Emacs is exited.
485 If the second argument flag is non-nil, Emacs will query the user before
486 exiting if process is running."
487 (if (fboundp 'set-process-query-on-exit-flag)
488 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
489 (tramp-compat-funcall 'process-kill-without-query process flag)))
490
491 ;; There exist different implementations for this function.
492 (defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type)
493 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
494 EOL-TYPE can be one of `dos', `unix', or `mac'."
495 (cond ((fboundp 'coding-system-change-eol-conversion)
496 (tramp-compat-funcall
497 'coding-system-change-eol-conversion coding-system eol-type))
498 ((fboundp 'subsidiary-coding-system)
499 (tramp-compat-funcall
500 'subsidiary-coding-system coding-system
501 (cond ((eq eol-type 'dos) 'crlf)
502 ((eq eol-type 'unix) 'lf)
503 ((eq eol-type 'mac) 'cr)
504 (t
505 (error "Unknown EOL-TYPE `%s', must be %s"
506 eol-type
507 "`dos', `unix', or `mac'")))))
508 (t (error "Can't change EOL conversion -- is MULE missing?"))))
509
510 (add-hook 'tramp-unload-hook
511 (lambda ()
512 (unload-feature 'tramp-compat 'force)))
513
514 (provide 'tramp-compat)
515
516 ;;; TODO:
517
518 ;;; tramp-compat.el ends here