1 ;;; tramp-compat.el --- Tramp compatibility functions
3 ;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
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.
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.
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/>.
26 ;; Tramp's main Emacs version for development is GNU Emacs 24. This
27 ;; package provides compatibility functions for GNU Emacs 22, GNU
28 ;; Emacs 23 and XEmacs 21.4+.
32 (require 'tramp-loaddefs
)
36 ;; Pacify byte-compiler.
43 (require 'format-spec
)
45 ;; As long as password.el is not part of (X)Emacs, it shouldn't be
47 (if (featurep 'xemacs
)
48 (load "password" 'noerror
)
49 (or (require 'password-cache nil
'noerror
)
50 (require 'password nil
'noerror
))) ; Part of contrib.
52 ;; auth-source is relatively new.
53 (if (featurep 'xemacs
)
54 (load "auth-source" 'noerror
)
55 (require 'auth-source nil
'noerror
))
57 ;; Load the appropriate timer package.
58 (if (featurep 'xemacs
)
59 (require 'timer-funcs
)
62 ;; We check whether `start-file-process' is bound.
63 (unless (fboundp 'start-file-process
)
65 ;; tramp-util offers integration into other (X)Emacs packages like
66 ;; compile.el, gud.el etc. Not necessary in Emacs 23.
67 (eval-after-load "tramp"
68 '(require 'tramp-util
))
70 ;; Make sure that we get integration with the VC package. When it
71 ;; is loaded, we need to pull in the integration module. Not
72 ;; necessary in Emacs 23.
74 (eval-after-load "tramp"
75 '(require 'tramp-vc
))))
77 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
78 ;; Currently, XEmacs supports this.
79 (when (featurep 'xemacs
)
80 (unless (boundp 'byte-compile-default-warnings
)
81 (defvar byte-compile-default-warnings nil
))
82 (delq 'unused-vars byte-compile-default-warnings
))
84 ;; `last-coding-system-used' is unknown in XEmacs.
85 (unless (boundp 'last-coding-system-used
)
86 (defvar last-coding-system-used nil
))
88 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
89 ;; used in XEmacs, so we set it here and there. The following is
90 ;; needed to pacify Emacs byte-compiler.
91 ;; Note that it was removed altogether in Emacs 24.1.
92 (when (boundp 'directory-sep-char
)
93 (defvar byte-compile-not-obsolete-var nil
)
94 (setq byte-compile-not-obsolete-var
'directory-sep-char
)
96 (defvar byte-compile-not-obsolete-vars nil
)
97 (setq byte-compile-not-obsolete-vars
'(directory-sep-char)))
99 ;; `remote-file-name-inhibit-cache' has been introduced with Emacs 24.1.
100 ;; Besides `t', `nil', and integer, we use also timestamps (as
101 ;; returned by `current-time') internally.
102 (defvar remote-file-name-inhibit-cache nil
)
104 ;; For not existing functions, or functions with a changed argument
105 ;; list, there are compiler warnings. We want to avoid them in
106 ;; cases we know what we do.
107 (defmacro tramp-compat-funcall
(function &rest arguments
)
108 (if (featurep 'xemacs
)
109 `(funcall (symbol-function ,function
) ,@arguments
)
110 `(when (or (subrp ,function
) (functionp ,function
))
111 (with-no-warnings (funcall ,function
,@arguments
)))))
113 ;; `set-buffer-multibyte' comes from Emacs Leim.
114 (unless (fboundp 'set-buffer-multibyte
)
115 (defalias 'set-buffer-multibyte
'ignore
))
117 ;; The following functions cannot be aliases of the corresponding
118 ;; `tramp-handle-*' functions, because this would bypass the locking
121 ;; `file-remote-p' has been introduced with Emacs 22. The version
122 ;; of XEmacs is not a magic file name function (yet); this is
123 ;; corrected in tramp-util.el. Here it is sufficient if the
125 (unless (fboundp 'file-remote-p
)
126 (defalias 'file-remote-p
127 (lambda (file &optional identification connected
)
128 (when (tramp-tramp-file-p file
)
129 (tramp-file-name-handler
130 'file-remote-p file identification connected
)))))
132 ;; `process-file' does not exist in XEmacs.
133 (unless (fboundp 'process-file
)
134 (defalias 'process-file
135 (lambda (program &optional infile buffer display
&rest args
)
136 (when (tramp-tramp-file-p default-directory
)
138 'tramp-file-name-handler
139 'process-file program infile buffer display args
)))))
141 ;; `start-file-process' is new in Emacs 23.
142 (unless (fboundp 'start-file-process
)
143 (defalias 'start-file-process
144 (lambda (name buffer program
&rest program-args
)
145 (when (tramp-tramp-file-p default-directory
)
147 'tramp-file-name-handler
148 'start-file-process name buffer program program-args
)))))
150 ;; `set-file-times' is also new in Emacs 23.
151 (unless (fboundp 'set-file-times
)
152 (defalias 'set-file-times
153 (lambda (filename &optional time
)
154 (when (tramp-tramp-file-p filename
)
155 (tramp-file-name-handler
156 'set-file-times filename time
)))))
158 ;; We currently use "[" and "]" in the filename format for IPv6
159 ;; hosts of GNU Emacs. This means, that Emacs wants to expand
160 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
161 ;; because no expansion could be found. We detect this situation
162 ;; and do something really awful: we have `file-expand-wildcards'
163 ;; return the original filename if it can't expand anything. Let's
164 ;; just hope that this doesn't break anything else.
165 ;; It is not needed anymore since GNU Emacs 23.2.
166 (unless (or (featurep 'xemacs
)
167 ;; `featurep' has only one argument in XEmacs.
168 (funcall 'featurep
'files
'remote-wildcards
))
169 (defadvice file-expand-wildcards
170 (around tramp-advice-file-expand-wildcards activate
)
171 (let ((name (ad-get-arg 0)))
172 ;; If it's a Tramp file, look if wildcards need to be expanded
175 (tramp-tramp-file-p name
)
177 "[[*?]" (tramp-compat-funcall
178 'file-remote-p name
'localname
))))
179 (setq ad-return-value
(list name
))
180 ;; Otherwise, just run the original function.
186 'file-expand-wildcards
'around
'tramp-advice-file-expand-wildcards
)
187 (ad-activate 'file-expand-wildcards
)))))
189 ;; `with-temp-message' does not exists in XEmacs.
190 (if (fboundp 'with-temp-message
)
191 (defalias 'tramp-compat-with-temp-message
'with-temp-message
)
192 (defmacro tramp-compat-with-temp-message
(message &rest body
)
193 "Display MESSAGE temporarily if non-nil while BODY is evaluated."
196 ;; `font-lock-add-keywords' does not exist in XEmacs.
197 (defun tramp-compat-font-lock-add-keywords (mode keywords
&optional how
)
198 "Add highlighting KEYWORDS for MODE."
200 (tramp-compat-funcall 'font-lock-add-keywords mode keywords how
)))
202 (defsubst tramp-compat-temporary-file-directory
()
203 "Return name of directory for temporary files (compat function).
204 For Emacs, this is the variable `temporary-file-directory', for XEmacs
205 this is the function `temp-directory'."
207 ((boundp 'temporary-file-directory
) (symbol-value 'temporary-file-directory
))
208 ((fboundp 'temp-directory
) (tramp-compat-funcall 'temp-directory
))
209 ((let ((d (getenv "TEMP"))) (and d
(file-directory-p d
)))
210 (file-name-as-directory (getenv "TEMP")))
211 ((let ((d (getenv "TMP"))) (and d
(file-directory-p d
)))
212 (file-name-as-directory (getenv "TMP")))
213 ((let ((d (getenv "TMPDIR"))) (and d
(file-directory-p d
)))
214 (file-name-as-directory (getenv "TMPDIR")))
215 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
216 (t (message (concat "Neither `temporary-file-directory' nor "
217 "`temp-directory' is defined -- using /tmp."))
218 (file-name-as-directory "/tmp"))))
220 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
221 ;; implementation with `make-temp-name', creating the temporary file
222 ;; immediately in order to avoid a security hole.
223 (defsubst tramp-compat-make-temp-file
(filename &optional dir-flag
)
224 "Create a temporary file (compat function).
225 Add the extension of FILENAME, if existing."
226 (let* (file-name-handler-alist
227 (prefix (expand-file-name
228 (symbol-value 'tramp-temp-name-prefix
)
229 (tramp-compat-temporary-file-directory)))
230 (extension (file-name-extension filename t
))
234 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension
))
236 ;; We use our own implementation, taken from files.el.
240 (setq result
(concat (make-temp-name prefix
) extension
))
242 (make-directory result
)
243 (write-region "" nil result nil
'silent
))
245 (file-already-exists t
))
246 ;; The file was somehow created by someone else between
247 ;; `make-temp-name' and `write-region', let's try again.
251 ;; `most-positive-fixnum' does not exist in XEmacs.
252 (defsubst tramp-compat-most-positive-fixnum
()
253 "Return largest positive integer value (compat function)."
255 ((boundp 'most-positive-fixnum
) (symbol-value 'most-positive-fixnum
))
256 ;; Default value in XEmacs.
259 (defun tramp-compat-decimal-to-octal (i)
260 "Return a string consisting of the octal digits of I.
261 Not actually used. Use `(format \"%o\" i)' instead?"
262 (cond ((< i
0) (error "Cannot convert negative number to octal"))
263 ((not (integerp i
)) (error "Cannot convert non-integer to octal"))
265 (t (concat (tramp-compat-decimal-to-octal (/ i
8))
266 (number-to-string (% i
8))))))
268 ;; Kudos to Gerd Moellmann for this suggestion.
269 (defun tramp-compat-octal-to-decimal (ostr)
270 "Given a string of octal digits, return a decimal number."
271 (let ((x (or ostr
"")))
272 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
273 (unless (string-match "\\`[0-7]*\\'" x
)
274 (error "Non-octal junk in string `%s'" x
))
275 (string-to-number ostr
8)))
277 ;; ID-FORMAT does not exists in XEmacs.
278 (defun tramp-compat-file-attributes (filename &optional id-format
)
279 "Like `file-attributes' for Tramp files (compat function)."
281 ((or (null id-format
) (eq id-format
'integer
))
282 (file-attributes filename
))
283 ((tramp-tramp-file-p filename
)
284 (tramp-file-name-handler 'file-attributes filename id-format
))
285 (t (condition-case nil
286 (tramp-compat-funcall 'file-attributes filename id-format
)
287 (wrong-number-of-arguments (file-attributes filename
))))))
289 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
290 ;; hurt to ignore it for other (X)Emacs versions.
291 ;; PRESERVE-SELINUX-CONTEXT has been introduced with Emacs 24.
292 (defun tramp-compat-copy-file
293 (filename newname
&optional ok-if-already-exists keep-date
294 preserve-uid-gid preserve-selinux-context
)
295 "Like `copy-file' for Tramp files (compat function)."
297 (preserve-selinux-context
298 (tramp-compat-funcall
299 'copy-file filename newname ok-if-already-exists keep-date
300 preserve-uid-gid preserve-selinux-context
))
302 (tramp-compat-funcall
303 'copy-file filename newname ok-if-already-exists keep-date
306 (copy-file filename newname ok-if-already-exists keep-date
))))
308 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
309 ;; is taken from there.
310 (defun tramp-compat-copy-directory
311 (directory newname
&optional keep-time parents
)
312 "Make a copy of DIRECTORY (compat function)."
313 (if (fboundp 'copy-directory
)
314 (tramp-compat-funcall 'copy-directory directory newname keep-time parents
)
316 ;; If `default-directory' is a remote directory, make sure we find
317 ;; its `copy-directory' handler.
318 (let ((handler (or (find-file-name-handler directory
'copy-directory
)
319 (find-file-name-handler newname
'copy-directory
))))
321 (funcall handler
'copy-directory directory newname keep-time parents
)
323 ;; Compute target name.
324 (setq directory
(directory-file-name (expand-file-name directory
))
325 newname
(directory-file-name (expand-file-name newname
)))
326 (if (and (file-directory-p newname
)
327 (not (string-equal (file-name-nondirectory directory
)
328 (file-name-nondirectory newname
))))
331 (file-name-nondirectory directory
) newname
)))
332 (if (not (file-directory-p newname
)) (make-directory newname parents
))
337 (if (file-directory-p file
)
338 (tramp-compat-copy-directory file newname keep-time parents
)
339 (copy-file file newname t keep-time
)))
340 ;; We do not want to delete "." and "..".
342 directory
'full
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
344 ;; Set directory attributes.
345 (set-file-modes newname
(file-modes directory
))
347 (set-file-times newname
(nth 5 (file-attributes directory
))))))))
349 ;; TRASH has been introduced with Emacs 24.1.
350 (defun tramp-compat-delete-file (filename &optional trash
)
351 "Like `delete-file' for Tramp files (compat function)."
353 (tramp-compat-funcall 'delete-file filename trash
)
354 ;; This Emacs version does not support the TRASH flag.
355 (wrong-number-of-arguments
356 (let ((delete-by-moving-to-trash
357 (and (boundp 'delete-by-moving-to-trash
)
358 (symbol-value 'delete-by-moving-to-trash
)
360 (delete-file filename
)))))
362 ;; RECURSIVE has been introduced with Emacs 23.2.
363 (defun tramp-compat-delete-directory (directory &optional recursive
)
364 "Like `delete-directory' for Tramp files (compat function)."
366 (delete-directory directory
)
368 (tramp-compat-funcall 'delete-directory directory recursive
)
369 ;; This Emacs version does not support the RECURSIVE flag. We
370 ;; use the implementation from Emacs 23.2.
371 (wrong-number-of-arguments
372 (setq directory
(directory-file-name (expand-file-name directory
)))
373 (if (not (file-symlink-p directory
))
375 (if (eq t
(car (file-attributes file
)))
376 (tramp-compat-delete-directory file recursive
)
379 directory
'full
"^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
380 (delete-directory directory
)))))
382 ;; `number-sequence' does not exist in XEmacs. Implementation is
383 ;; taken from Emacs 23.
384 (defun tramp-compat-number-sequence (from &optional to inc
)
385 "Return a sequence of numbers from FROM to TO as a list (compat function)."
386 (if (or (subrp 'number-sequence
) (symbol-file 'number-sequence
))
387 (tramp-compat-funcall 'number-sequence from to inc
)
388 (if (or (not to
) (= from to
))
390 (or inc
(setq inc
1))
391 (when (zerop inc
) (error "The increment can not be zero"))
392 (let (seq (n 0) (next from
))
395 (setq seq
(cons next seq
)
397 next
(+ from
(* n inc
))))
399 (setq seq
(cons next seq
)
401 next
(+ from
(* n inc
)))))
404 (defun tramp-compat-split-string (string pattern
)
405 "Like `split-string' but omit empty strings.
406 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
407 This is, the first, empty, element is omitted. In XEmacs, the first
408 element is not omitted."
409 (delete "" (split-string string pattern
)))
411 (defun tramp-compat-call-process
412 (program &optional infile destination display
&rest args
)
413 "Calls `call-process' on the local host.
414 This is needed because for some Emacs flavors Tramp has
415 defadviced `call-process' to behave like `process-file'. The
416 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
417 (let ((default-directory
418 (if (file-remote-p default-directory
)
419 (tramp-compat-temporary-file-directory)
421 (if (executable-find program
)
422 (apply 'call-process program infile destination display args
)
425 (defun tramp-compat-process-running-p (process-name)
426 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
427 (when (stringp process-name
)
429 ;; GNU Emacs 22 on w32.
430 ((fboundp 'w32-window-exists-p
)
431 (tramp-compat-funcall 'w32-window-exists-p process-name process-name
))
434 ((and (fboundp 'list-system-processes
) (fboundp 'process-attributes
))
436 (dolist (pid (tramp-compat-funcall 'list-system-processes
) result
)
437 (let ((attributes (tramp-compat-funcall 'process-attributes pid
)))
438 (when (and (string-equal
439 (cdr (assoc 'user attributes
)) (user-login-name))
440 (let ((comm (cdr (assoc 'comm attributes
))))
441 ;; The returned command name could be truncated
442 ;; to 15 characters. Therefore, we cannot check
443 ;; for `string-equal'.
444 (and comm
(string-match
445 (concat "^" (regexp-quote comm
))
449 ;; Fallback, if there is no Lisp support yet.
450 (t (let ((default-directory
451 (if (file-remote-p default-directory
)
452 (tramp-compat-temporary-file-directory)
454 (unix95 (getenv "UNIX95"))
456 (setenv "UNIX95" "1")
459 (tramp-compat-split-string
460 (shell-command-to-string
461 (format "ps -C %s -o user=" process-name
))
464 (setenv "UNIX95" unix95
)
467 ;; The following functions do not exist in XEmacs. We ignore this;
468 ;; they are used for checking a remote tty.
469 (defun tramp-compat-process-get (process propname
)
470 "Return the value of PROCESS' PROPNAME property.
471 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
472 (ignore-errors (tramp-compat-funcall 'process-get process propname
)))
474 (defun tramp-compat-process-put (process propname value
)
475 "Change PROCESS' PROPNAME property to VALUE.
476 It can be retrieved with `(process-get PROCESS PROPNAME)'."
477 (ignore-errors (tramp-compat-funcall 'process-put process propname value
)))
479 (defun tramp-compat-set-process-query-on-exit-flag (process flag
)
480 "Specify if query is needed for process when Emacs is exited.
481 If the second argument flag is non-nil, Emacs will query the user before
482 exiting if process is running."
483 (if (fboundp 'set-process-query-on-exit-flag
)
484 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag
)
485 (tramp-compat-funcall 'process-kill-without-query process flag
)))
487 (add-hook 'tramp-unload-hook
489 (unload-feature 'tramp-compat
'force
)))
491 (defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type
)
492 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
493 EOL-TYPE can be one of `dos', `unix', or `mac'."
494 (cond ((fboundp 'coding-system-change-eol-conversion
)
495 (tramp-compat-funcall
496 'coding-system-change-eol-conversion coding-system eol-type
))
497 ((fboundp 'subsidiary-coding-system
)
498 (tramp-compat-funcall
499 'subsidiary-coding-system coding-system
500 (cond ((eq eol-type
'dos
) 'crlf
)
501 ((eq eol-type
'unix
) 'lf
)
502 ((eq eol-type
'mac
) 'cr
)
504 (error "Unknown EOL-TYPE `%s', must be %s"
506 "`dos', `unix', or `mac'")))))
507 (t (error "Can't change EOL conversion -- is MULE missing?"))))
509 (provide 'tramp-compat
)
513 ;;; tramp-compat.el ends here