]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Merge from emacs-23 branch
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007-2011 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
45 ;; As long as password.el is not part of (X)Emacs, it shouldn't be
46 ;; mandatory.
47 (if (featurep 'xemacs)
48 (load "password" 'noerror)
49 (or (require 'password-cache nil 'noerror)
50 (require 'password nil 'noerror))) ; Part of contrib.
51
52 ;; auth-source is relatively new.
53 (if (featurep 'xemacs)
54 (load "auth-source" 'noerror)
55 (require 'auth-source nil 'noerror))
56
57 ;; Load the appropriate timer package.
58 (if (featurep 'xemacs)
59 (require 'timer-funcs)
60 (require 'timer))
61
62 ;; We check whether `start-file-process' is bound.
63 (unless (fboundp 'start-file-process)
64
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))
69
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.
73 (eval-after-load "vc"
74 (eval-after-load "tramp"
75 '(require 'tramp-vc))))
76
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))
83
84 ;; `last-coding-system-used' is unknown in XEmacs.
85 (unless (boundp 'last-coding-system-used)
86 (defvar last-coding-system-used nil))
87
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)
95 ;; Emacs 23.2.
96 (defvar byte-compile-not-obsolete-vars nil)
97 (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
98
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)
103
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)))))
112
113 ;; `set-buffer-multibyte' comes from Emacs Leim.
114 (unless (fboundp 'set-buffer-multibyte)
115 (defalias 'set-buffer-multibyte 'ignore))
116
117 ;; The following functions cannot be aliases of the corresponding
118 ;; `tramp-handle-*' functions, because this would bypass the locking
119 ;; mechanism.
120
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
124 ;; function exists.
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)))))
131
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)
137 (apply
138 'tramp-file-name-handler
139 'process-file program infile buffer display args)))))
140
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)
146 (apply
147 'tramp-file-name-handler
148 'start-file-process name buffer program program-args)))))
149
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)))))
157
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
173 ;; at all.
174 (if (and
175 (tramp-tramp-file-p name)
176 (not (string-match
177 "[[*?]" (tramp-compat-funcall
178 'file-remote-p name 'localname))))
179 (setq ad-return-value (list name))
180 ;; Otherwise, just run the original function.
181 ad-do-it)))
182 (add-hook
183 'tramp-unload-hook
184 (lambda ()
185 (ad-remove-advice
186 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
187 (ad-activate 'file-expand-wildcards)))))
188
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."
194 `(progn ,@body)))
195
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."
199 (ignore-errors
200 (tramp-compat-funcall 'font-lock-add-keywords mode keywords how)))
201
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'."
206 (cond
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"))))
219
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))
231 result)
232 (condition-case nil
233 (setq result
234 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
235 (error
236 ;; We use our own implementation, taken from files.el.
237 (while
238 (condition-case ()
239 (progn
240 (setq result (concat (make-temp-name prefix) extension))
241 (if dir-flag
242 (make-directory result)
243 (write-region "" nil result nil 'silent))
244 nil)
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.
248 nil)))
249 result))
250
251 ;; `most-positive-fixnum' does not exist in XEmacs.
252 (defsubst tramp-compat-most-positive-fixnum ()
253 "Return largest positive integer value (compat function)."
254 (cond
255 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
256 ;; Default value in XEmacs.
257 (t 134217727)))
258
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"))
264 ((zerop i) "0")
265 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
266 (number-to-string (% i 8))))))
267
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)))
276
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)."
280 (cond
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))))))
288
289 ;; PRESERVE-UID-GID does not exist in XEmacs.
290 ;; PRESERVE-SELINUX-CONTEXT has been introduced with Emacs 24.1.
291 (defun tramp-compat-copy-file
292 (filename newname &optional ok-if-already-exists keep-date
293 preserve-uid-gid preserve-selinux-context)
294 "Like `copy-file' for Tramp files (compat function)."
295 (cond
296 (preserve-selinux-context
297 (tramp-compat-funcall
298 'copy-file filename newname ok-if-already-exists keep-date
299 preserve-uid-gid preserve-selinux-context))
300 (preserve-uid-gid
301 (tramp-compat-funcall
302 'copy-file filename newname ok-if-already-exists keep-date
303 preserve-uid-gid))
304 (t
305 (copy-file filename newname ok-if-already-exists keep-date))))
306
307 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
308 ;; is taken from there.
309 (defun tramp-compat-copy-directory
310 (directory newname &optional keep-time parents)
311 "Make a copy of DIRECTORY (compat function)."
312 (if (fboundp 'copy-directory)
313 (tramp-compat-funcall 'copy-directory directory newname keep-time parents)
314
315 ;; If `default-directory' is a remote directory, make sure we find
316 ;; its `copy-directory' handler.
317 (let ((handler (or (find-file-name-handler directory 'copy-directory)
318 (find-file-name-handler newname 'copy-directory))))
319 (if handler
320 (funcall handler 'copy-directory directory newname keep-time parents)
321
322 ;; Compute target name.
323 (setq directory (directory-file-name (expand-file-name directory))
324 newname (directory-file-name (expand-file-name newname)))
325 (if (and (file-directory-p newname)
326 (not (string-equal (file-name-nondirectory directory)
327 (file-name-nondirectory newname))))
328 (setq newname
329 (expand-file-name
330 (file-name-nondirectory directory) newname)))
331 (if (not (file-directory-p newname)) (make-directory newname parents))
332
333 ;; Copy recursively.
334 (mapc
335 (lambda (file)
336 (if (file-directory-p file)
337 (tramp-compat-copy-directory file newname keep-time parents)
338 (copy-file file newname t keep-time)))
339 ;; We do not want to delete "." and "..".
340 (directory-files
341 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
342
343 ;; Set directory attributes.
344 (set-file-modes newname (file-modes directory))
345 (if keep-time
346 (set-file-times newname (nth 5 (file-attributes directory))))))))
347
348 ;; TRASH has been introduced with Emacs 24.1.
349 (defun tramp-compat-delete-file (filename &optional trash)
350 "Like `delete-file' for Tramp files (compat function)."
351 (condition-case nil
352 (tramp-compat-funcall 'delete-file filename trash)
353 ;; This Emacs version does not support the TRASH flag.
354 (wrong-number-of-arguments
355 (let ((delete-by-moving-to-trash
356 (and (boundp 'delete-by-moving-to-trash)
357 (symbol-value 'delete-by-moving-to-trash)
358 trash)))
359 (delete-file filename)))))
360
361 ;; RECURSIVE has been introduced with Emacs 23.2.
362 (defun tramp-compat-delete-directory (directory &optional recursive)
363 "Like `delete-directory' for Tramp files (compat function)."
364 (if (null recursive)
365 (delete-directory directory)
366 (condition-case nil
367 (tramp-compat-funcall 'delete-directory directory recursive)
368 ;; This Emacs version does not support the RECURSIVE flag. We
369 ;; use the implementation from Emacs 23.2.
370 (wrong-number-of-arguments
371 (setq directory (directory-file-name (expand-file-name directory)))
372 (if (not (file-symlink-p directory))
373 (mapc (lambda (file)
374 (if (eq t (car (file-attributes file)))
375 (tramp-compat-delete-directory file recursive)
376 (delete-file file)))
377 (directory-files
378 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
379 (delete-directory directory)))))
380
381 ;; `number-sequence' does not exist in XEmacs. Implementation is
382 ;; taken from Emacs 23.
383 (defun tramp-compat-number-sequence (from &optional to inc)
384 "Return a sequence of numbers from FROM to TO as a list (compat function)."
385 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
386 (tramp-compat-funcall 'number-sequence from to inc)
387 (if (or (not to) (= from to))
388 (list from)
389 (or inc (setq inc 1))
390 (when (zerop inc) (error "The increment can not be zero"))
391 (let (seq (n 0) (next from))
392 (if (> inc 0)
393 (while (<= next to)
394 (setq seq (cons next seq)
395 n (1+ n)
396 next (+ from (* n inc))))
397 (while (>= next to)
398 (setq seq (cons next seq)
399 n (1+ n)
400 next (+ from (* n inc)))))
401 (nreverse seq)))))
402
403 (defun tramp-compat-split-string (string pattern)
404 "Like `split-string' but omit empty strings.
405 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
406 This is, the first, empty, element is omitted. In XEmacs, the first
407 element is not omitted."
408 (delete "" (split-string string pattern)))
409
410 (defun tramp-compat-call-process
411 (program &optional infile destination display &rest args)
412 "Calls `call-process' on the local host.
413 This is needed because for some Emacs flavors Tramp has
414 defadviced `call-process' to behave like `process-file'. The
415 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
416 (let ((default-directory
417 (if (file-remote-p default-directory)
418 (tramp-compat-temporary-file-directory)
419 default-directory)))
420 (if (executable-find program)
421 (apply 'call-process program infile destination display args)
422 1)))
423
424 (defun tramp-compat-process-running-p (process-name)
425 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
426 (when (stringp process-name)
427 (cond
428 ;; GNU Emacs 22 on w32.
429 ((fboundp 'w32-window-exists-p)
430 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
431
432 ;; GNU Emacs 23.
433 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
434 (let (result)
435 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
436 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
437 (when (and (string-equal
438 (cdr (assoc 'user attributes)) (user-login-name))
439 (let ((comm (cdr (assoc 'comm attributes))))
440 ;; The returned command name could be truncated
441 ;; to 15 characters. Therefore, we cannot check
442 ;; for `string-equal'.
443 (and comm (string-match
444 (concat "^" (regexp-quote comm))
445 process-name))))
446 (setq result t))))))
447
448 ;; Fallback, if there is no Lisp support yet.
449 (t (let ((default-directory
450 (if (file-remote-p default-directory)
451 (tramp-compat-temporary-file-directory)
452 default-directory))
453 (unix95 (getenv "UNIX95"))
454 result)
455 (setenv "UNIX95" "1")
456 (when (member
457 (user-login-name)
458 (tramp-compat-split-string
459 (shell-command-to-string
460 (format "ps -C %s -o user=" process-name))
461 "[ \f\t\n\r\v]+"))
462 (setq result t))
463 (setenv "UNIX95" unix95)
464 result)))))
465
466 ;; The following functions do not exist in XEmacs. We ignore this;
467 ;; they are used for checking a remote tty.
468 (defun tramp-compat-process-get (process propname)
469 "Return the value of PROCESS' PROPNAME property.
470 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
471 (ignore-errors (tramp-compat-funcall 'process-get process propname)))
472
473 (defun tramp-compat-process-put (process propname value)
474 "Change PROCESS' PROPNAME property to VALUE.
475 It can be retrieved with `(process-get PROCESS PROPNAME)'."
476 (ignore-errors (tramp-compat-funcall 'process-put process propname value)))
477
478 (defun tramp-compat-set-process-query-on-exit-flag (process flag)
479 "Specify if query is needed for process when Emacs is exited.
480 If the second argument flag is non-nil, Emacs will query the user before
481 exiting if process is running."
482 (if (fboundp 'set-process-query-on-exit-flag)
483 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
484 (tramp-compat-funcall 'process-kill-without-query process flag)))
485
486 ;; There exist different implementations for this function.
487 (defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type)
488 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
489 EOL-TYPE can be one of `dos', `unix', or `mac'."
490 (cond ((fboundp 'coding-system-change-eol-conversion)
491 (tramp-compat-funcall
492 'coding-system-change-eol-conversion coding-system eol-type))
493 ((fboundp 'subsidiary-coding-system)
494 (tramp-compat-funcall
495 'subsidiary-coding-system coding-system
496 (cond ((eq eol-type 'dos) 'crlf)
497 ((eq eol-type 'unix) 'lf)
498 ((eq eol-type 'mac) 'cr)
499 (t
500 (error "Unknown EOL-TYPE `%s', must be %s"
501 eol-type
502 "`dos', `unix', or `mac'")))))
503 (t (error "Can't change EOL conversion -- is MULE missing?"))))
504
505 ;; `pop-to-buffer-same-window' has been introduced with Emacs 24.1.
506 (defun tramp-compat-pop-to-buffer-same-window
507 (&optional buffer-or-name norecord label)
508 "Pop to buffer specified by BUFFER-OR-NAME in the selected window."
509 (if (fboundp 'pop-to-buffer-same-window)
510 (tramp-compat-funcall
511 'pop-to-buffer-same-window buffer-or-name norecord label)
512 (tramp-compat-funcall 'switch-to-buffer buffer-or-name norecord)))
513
514 (add-hook 'tramp-unload-hook
515 (lambda ()
516 (unload-feature 'tramp-compat 'force)))
517
518 (provide 'tramp-compat)
519
520 ;;; TODO:
521
522 ;;; tramp-compat.el ends here