]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Merge from trunk
[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 ;; 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 GNU Emacs 24. This
27 ;; package provides compatibility functions for GNU Emacs 22, GNU
28 ;; Emacs 23 and XEmacs 21.4+.
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-line-beginning-position ()
203 "Return point at beginning of line (compat function).
204 Calls `line-beginning-position' or `point-at-bol' if defined, else
205 own implementation."
206 (cond
207 ((fboundp 'line-beginning-position)
208 (tramp-compat-funcall 'line-beginning-position))
209 ((fboundp 'point-at-bol) (tramp-compat-funcall 'point-at-bol))
210 (t (save-excursion (beginning-of-line) (point)))))
211
212 (defsubst tramp-compat-line-end-position ()
213 "Return point at end of line (compat function).
214 Calls `line-end-position' or `point-at-eol' if defined, else
215 own implementation."
216 (cond
217 ((fboundp 'line-end-position) (tramp-compat-funcall 'line-end-position))
218 ((fboundp 'point-at-eol) (tramp-compat-funcall 'point-at-eol))
219 (t (save-excursion (end-of-line) (point)))))
220
221 (defsubst tramp-compat-temporary-file-directory ()
222 "Return name of directory for temporary files (compat function).
223 For Emacs, this is the variable `temporary-file-directory', for XEmacs
224 this is the function `temp-directory'."
225 (cond
226 ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
227 ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
228 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
229 (file-name-as-directory (getenv "TEMP")))
230 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
231 (file-name-as-directory (getenv "TMP")))
232 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
233 (file-name-as-directory (getenv "TMPDIR")))
234 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
235 (t (message (concat "Neither `temporary-file-directory' nor "
236 "`temp-directory' is defined -- using /tmp."))
237 (file-name-as-directory "/tmp"))))
238
239 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
240 ;; implementation with `make-temp-name', creating the temporary file
241 ;; immediately in order to avoid a security hole.
242 (defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
243 "Create a temporary file (compat function).
244 Add the extension of FILENAME, if existing."
245 (let* (file-name-handler-alist
246 (prefix (expand-file-name
247 (symbol-value 'tramp-temp-name-prefix)
248 (tramp-compat-temporary-file-directory)))
249 (extension (file-name-extension filename t))
250 result)
251 (condition-case nil
252 (setq result
253 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
254 (error
255 ;; We use our own implementation, taken from files.el.
256 (while
257 (condition-case ()
258 (progn
259 (setq result (concat (make-temp-name prefix) extension))
260 (if dir-flag
261 (make-directory result)
262 (write-region "" nil result nil 'silent))
263 nil)
264 (file-already-exists t))
265 ;; The file was somehow created by someone else between
266 ;; `make-temp-name' and `write-region', let's try again.
267 nil)))
268 result))
269
270 ;; `most-positive-fixnum' does not exist in XEmacs.
271 (defsubst tramp-compat-most-positive-fixnum ()
272 "Return largest positive integer value (compat function)."
273 (cond
274 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
275 ;; Default value in XEmacs.
276 (t 134217727)))
277
278 (defun tramp-compat-decimal-to-octal (i)
279 "Return a string consisting of the octal digits of I.
280 Not actually used. Use `(format \"%o\" i)' instead?"
281 (cond ((< i 0) (error "Cannot convert negative number to octal"))
282 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
283 ((zerop i) "0")
284 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
285 (number-to-string (% i 8))))))
286
287 ;; Kudos to Gerd Moellmann for this suggestion.
288 (defun tramp-compat-octal-to-decimal (ostr)
289 "Given a string of octal digits, return a decimal number."
290 (let ((x (or ostr "")))
291 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
292 (unless (string-match "\\`[0-7]*\\'" x)
293 (error "Non-octal junk in string `%s'" x))
294 (string-to-number ostr 8)))
295
296 ;; ID-FORMAT does not exists in XEmacs.
297 (defun tramp-compat-file-attributes (filename &optional id-format)
298 "Like `file-attributes' for Tramp files (compat function)."
299 (cond
300 ((or (null id-format) (eq id-format 'integer))
301 (file-attributes filename))
302 ((tramp-tramp-file-p filename)
303 (tramp-file-name-handler 'file-attributes filename id-format))
304 (t (condition-case nil
305 (tramp-compat-funcall 'file-attributes filename id-format)
306 (wrong-number-of-arguments (file-attributes filename))))))
307
308 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
309 ;; hurt to ignore it for other (X)Emacs versions.
310 ;; PRESERVE-SELINUX-CONTEXT has been introduced with Emacs 24.
311 (defun tramp-compat-copy-file
312 (filename newname &optional ok-if-already-exists keep-date
313 preserve-uid-gid preserve-selinux-context)
314 "Like `copy-file' for Tramp files (compat function)."
315 (cond
316 (preserve-selinux-context
317 (tramp-compat-funcall
318 'copy-file filename newname ok-if-already-exists keep-date
319 preserve-uid-gid preserve-selinux-context))
320 (preserve-uid-gid
321 (tramp-compat-funcall
322 'copy-file filename newname ok-if-already-exists keep-date
323 preserve-uid-gid))
324 (t
325 (copy-file filename newname ok-if-already-exists keep-date))))
326
327 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
328 ;; is taken from there.
329 (defun tramp-compat-copy-directory
330 (directory newname &optional keep-time parents)
331 "Make a copy of DIRECTORY (compat function)."
332 (if (fboundp 'copy-directory)
333 (tramp-compat-funcall 'copy-directory directory newname keep-time parents)
334
335 ;; If `default-directory' is a remote directory, make sure we find
336 ;; its `copy-directory' handler.
337 (let ((handler (or (find-file-name-handler directory 'copy-directory)
338 (find-file-name-handler newname 'copy-directory))))
339 (if handler
340 (funcall handler 'copy-directory directory newname keep-time parents)
341
342 ;; Compute target name.
343 (setq directory (directory-file-name (expand-file-name directory))
344 newname (directory-file-name (expand-file-name newname)))
345 (if (and (file-directory-p newname)
346 (not (string-equal (file-name-nondirectory directory)
347 (file-name-nondirectory newname))))
348 (setq newname
349 (expand-file-name
350 (file-name-nondirectory directory) newname)))
351 (if (not (file-directory-p newname)) (make-directory newname parents))
352
353 ;; Copy recursively.
354 (mapc
355 (lambda (file)
356 (if (file-directory-p file)
357 (tramp-compat-copy-directory file newname keep-time parents)
358 (copy-file file newname t keep-time)))
359 ;; We do not want to delete "." and "..".
360 (directory-files
361 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
362
363 ;; Set directory attributes.
364 (set-file-modes newname (file-modes directory))
365 (if keep-time
366 (set-file-times newname (nth 5 (file-attributes directory))))))))
367
368 ;; TRASH has been introduced with Emacs 24.1.
369 (defun tramp-compat-delete-file (filename &optional trash)
370 "Like `delete-file' for Tramp files (compat function)."
371 (condition-case nil
372 (tramp-compat-funcall 'delete-file filename trash)
373 ;; This Emacs version does not support the TRASH flag.
374 (wrong-number-of-arguments
375 (let ((delete-by-moving-to-trash
376 (and (boundp 'delete-by-moving-to-trash)
377 (symbol-value 'delete-by-moving-to-trash)
378 trash)))
379 (delete-file filename)))))
380
381 ;; RECURSIVE has been introduced with Emacs 23.2.
382 (defun tramp-compat-delete-directory (directory &optional recursive)
383 "Like `delete-directory' for Tramp files (compat function)."
384 (if (null recursive)
385 (delete-directory directory)
386 (condition-case nil
387 (tramp-compat-funcall 'delete-directory directory recursive)
388 ;; This Emacs version does not support the RECURSIVE flag. We
389 ;; use the implementation from Emacs 23.2.
390 (wrong-number-of-arguments
391 (setq directory (directory-file-name (expand-file-name directory)))
392 (if (not (file-symlink-p directory))
393 (mapc (lambda (file)
394 (if (eq t (car (file-attributes file)))
395 (tramp-compat-delete-directory file recursive)
396 (delete-file file)))
397 (directory-files
398 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
399 (delete-directory directory)))))
400
401 ;; `number-sequence' does not exist in XEmacs. Implementation is
402 ;; taken from Emacs 23.
403 (defun tramp-compat-number-sequence (from &optional to inc)
404 "Return a sequence of numbers from FROM to TO as a list (compat function)."
405 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
406 (tramp-compat-funcall 'number-sequence from to inc)
407 (if (or (not to) (= from to))
408 (list from)
409 (or inc (setq inc 1))
410 (when (zerop inc) (error "The increment can not be zero"))
411 (let (seq (n 0) (next from))
412 (if (> inc 0)
413 (while (<= next to)
414 (setq seq (cons next seq)
415 n (1+ n)
416 next (+ from (* n inc))))
417 (while (>= next to)
418 (setq seq (cons next seq)
419 n (1+ n)
420 next (+ from (* n inc)))))
421 (nreverse seq)))))
422
423 (defun tramp-compat-split-string (string pattern)
424 "Like `split-string' but omit empty strings.
425 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
426 This is, the first, empty, element is omitted. In XEmacs, the first
427 element is not omitted."
428 (delete "" (split-string string pattern)))
429
430 (defun tramp-compat-call-process
431 (program &optional infile destination display &rest args)
432 "Calls `call-process' on the local host.
433 This is needed because for some Emacs flavors Tramp has
434 defadviced `call-process' to behave like `process-file'. The
435 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
436 (let ((default-directory
437 (if (file-remote-p default-directory)
438 (tramp-compat-temporary-file-directory)
439 default-directory)))
440 (if (executable-find program)
441 (apply 'call-process program infile destination display args)
442 1)))
443
444 (defun tramp-compat-process-running-p (process-name)
445 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
446 (when (stringp process-name)
447 (cond
448 ;; GNU Emacs 22 on w32.
449 ((fboundp 'w32-window-exists-p)
450 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
451
452 ;; GNU Emacs 23.
453 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
454 (let (result)
455 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
456 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
457 (when (and (string-equal
458 (cdr (assoc 'user attributes)) (user-login-name))
459 (let ((comm (cdr (assoc 'comm attributes))))
460 ;; The returned command name could be truncated
461 ;; to 15 characters. Therefore, we cannot check
462 ;; for `string-equal'.
463 (and comm (string-match
464 (concat "^" (regexp-quote comm))
465 process-name))))
466 (setq result t))))))
467
468 ;; Fallback, if there is no Lisp support yet.
469 (t (let ((default-directory
470 (if (file-remote-p default-directory)
471 (tramp-compat-temporary-file-directory)
472 default-directory))
473 (unix95 (getenv "UNIX95"))
474 result)
475 (setenv "UNIX95" "1")
476 (when (member
477 (user-login-name)
478 (tramp-compat-split-string
479 (shell-command-to-string
480 (format "ps -C %s -o user=" process-name))
481 "[ \f\t\n\r\v]+"))
482 (setq result t))
483 (setenv "UNIX95" unix95)
484 result)))))
485
486 ;; The following functions do not exist in XEmacs. We ignore this;
487 ;; they are used for checking a remote tty.
488 (defun tramp-compat-process-get (process propname)
489 "Return the value of PROCESS' PROPNAME property.
490 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
491 (ignore-errors (tramp-compat-funcall 'process-get process propname)))
492
493 (defun tramp-compat-process-put (process propname value)
494 "Change PROCESS' PROPNAME property to VALUE.
495 It can be retrieved with `(process-get PROCESS PROPNAME)'."
496 (ignore-errors (tramp-compat-funcall 'process-put process propname value)))
497
498 (defun tramp-compat-set-process-query-on-exit-flag (process flag)
499 "Specify if query is needed for process when Emacs is exited.
500 If the second argument flag is non-nil, Emacs will query the user before
501 exiting if process is running."
502 (if (fboundp 'set-process-query-on-exit-flag)
503 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
504 (tramp-compat-funcall 'process-kill-without-query process flag)))
505
506 (add-hook 'tramp-unload-hook
507 (lambda ()
508 (unload-feature 'tramp-compat 'force)))
509
510 (defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type)
511 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
512 EOL-TYPE can be one of `dos', `unix', or `mac'."
513 (cond ((fboundp 'coding-system-change-eol-conversion)
514 (tramp-compat-funcall
515 'coding-system-change-eol-conversion coding-system eol-type))
516 ((fboundp 'subsidiary-coding-system)
517 (tramp-compat-funcall
518 'subsidiary-coding-system coding-system
519 (cond ((eq eol-type 'dos) 'crlf)
520 ((eq eol-type 'unix) 'lf)
521 ((eq eol-type 'mac) 'cr)
522 (t
523 (error "Unknown EOL-TYPE `%s', must be %s"
524 eol-type
525 "`dos', `unix', or `mac'")))))
526 (t (error "Can't change EOL conversion -- is MULE missing?"))))
527
528 (provide 'tramp-compat)
529
530 ;;; TODO:
531
532 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
533 ;;; tramp-compat.el ends here