tramp-compat.el 19.6 KB
Newer Older
1
;;; tramp-compat.el --- Tramp compatibility functions
2

3
;; Copyright (C) 2007-2011 Free Software Foundation, Inc.
4 5 6

;; Author: Michael Albinus <michael.albinus@gmx.de>
;; Keywords: comm, processes
7
;; Package: tramp
8 9 10

;; 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 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; 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 25

;;; Commentary:

26 27 28
;; Tramp's main Emacs version for development is GNU Emacs 24.  This
;; package provides compatibility functions for GNU Emacs 22, GNU
;; Emacs 23 and XEmacs 21.4+.
29 30 31

;;; Code:

32
(require 'tramp-loaddefs)
33

34
(eval-when-compile
35 36

  ;; Pacify byte-compiler.
37 38 39 40
  (require 'cl))

(eval-and-compile

41
  (require 'advice)
42
  (require 'custom)
43 44 45 46 47 48 49 50 51 52 53 54 55
  (require 'format-spec)

  ;; As long as password.el is not part of (X)Emacs, it shouldn't be
  ;; mandatory.
  (if (featurep 'xemacs)
      (load "password" 'noerror)
    (or (require 'password-cache nil 'noerror)
	(require 'password nil 'noerror))) ; Part of contrib.

  ;; auth-source is relatively new.
  (if (featurep 'xemacs)
      (load "auth-source" 'noerror)
    (require 'auth-source nil 'noerror))
56

57 58 59 60
  ;; Load the appropriate timer package.
  (if (featurep 'xemacs)
      (require 'timer-funcs)
    (require 'timer))
61

62 63 64 65 66
  ;; We check whether `start-file-process' is bound.
  (unless (fboundp 'start-file-process)

    ;; tramp-util offers integration into other (X)Emacs packages like
    ;; compile.el, gud.el etc.  Not necessary in Emacs 23.
67
    (eval-after-load "tramp"
68
      '(require 'tramp-util))
69 70 71 72 73 74

    ;; Make sure that we get integration with the VC package.  When it
    ;; is loaded, we need to pull in the integration module.  Not
    ;; necessary in Emacs 23.
    (eval-after-load "vc"
      (eval-after-load "tramp"
75
	'(require 'tramp-vc))))
76

77 78 79 80 81 82 83 84
  ;; Avoid byte-compiler warnings if the byte-compiler supports this.
  ;; Currently, XEmacs supports this.
  (when (featurep 'xemacs)
    (unless (boundp 'byte-compile-default-warnings)
      (defvar byte-compile-default-warnings nil))
    (delq 'unused-vars byte-compile-default-warnings))

  ;; `last-coding-system-used' is unknown in XEmacs.
85
  (unless (boundp 'last-coding-system-used)
86
    (defvar last-coding-system-used nil))
87

88 89 90
  ;; `directory-sep-char' is an obsolete variable in Emacs.  But it is
  ;; used in XEmacs, so we set it here and there.  The following is
  ;; needed to pacify Emacs byte-compiler.
91 92
  ;; Note that it was removed altogether in Emacs 24.1.
  (when (boundp 'directory-sep-char)
93
    (defvar byte-compile-not-obsolete-var nil)
94 95
    (setq byte-compile-not-obsolete-var 'directory-sep-char)
    ;; Emacs 23.2.
96
    (defvar byte-compile-not-obsolete-vars nil)
97
    (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
98

99 100 101 102 103
  ;; `remote-file-name-inhibit-cache' has been introduced with Emacs 24.1.
  ;; Besides `t', `nil', and integer, we use also timestamps (as
  ;; returned by `current-time') internally.
  (defvar remote-file-name-inhibit-cache nil)

104 105 106 107 108 109 110 111 112
  ;; For not existing functions, or functions with a changed argument
  ;; list, there are compiler warnings.  We want to avoid them in
  ;; cases we know what we do.
  (defmacro tramp-compat-funcall (function &rest arguments)
    (if (featurep 'xemacs)
	`(funcall (symbol-function ,function) ,@arguments)
      `(when (or (subrp ,function) (functionp ,function))
	 (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))
116

117 118 119 120
  ;; The following functions cannot be aliases of the corresponding
  ;; `tramp-handle-*' functions, because this would bypass the locking
  ;; mechanism.

121 122 123 124 125
  ;; `file-remote-p' has been introduced with Emacs 22.  The version
  ;; of XEmacs is not a magic file name function (yet); this is
  ;; corrected in tramp-util.el.  Here it is sufficient if the
  ;; function exists.
  (unless (fboundp 'file-remote-p)
126 127 128 129 130
    (defalias 'file-remote-p
      (lambda (file &optional identification connected)
	(when (tramp-tramp-file-p file)
	  (tramp-file-name-handler
	   'file-remote-p file identification connected)))))
131

132
  ;; `process-file' does not exist in XEmacs.
133
  (unless (fboundp 'process-file)
134 135 136 137 138 139
    (defalias 'process-file
      (lambda (program &optional infile buffer display &rest args)
	(when (tramp-tramp-file-p default-directory)
	  (apply
	   'tramp-file-name-handler
	   'process-file program infile buffer display args)))))
140 141 142

  ;; `start-file-process' is new in Emacs 23.
  (unless (fboundp 'start-file-process)
143 144 145 146 147 148
    (defalias 'start-file-process
      (lambda (name buffer program &rest program-args)
	(when (tramp-tramp-file-p default-directory)
	  (apply
	   'tramp-file-name-handler
	   'start-file-process name buffer program program-args)))))
149 150 151

  ;; `set-file-times' is also new in Emacs 23.
  (unless (fboundp 'set-file-times)
152 153 154 155
    (defalias 'set-file-times
      (lambda (filename &optional time)
	(when (tramp-tramp-file-p filename)
	  (tramp-file-name-handler
156 157 158
	   'set-file-times filename time)))))

  ;; We currently use "[" and "]" in the filename format for IPv6
159
  ;; hosts of GNU Emacs.  This means that Emacs wants to expand
160 161 162 163 164 165
  ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
  ;; because no expansion could be found.  We detect this situation
  ;; and do something really awful: we have `file-expand-wildcards'
  ;; return the original filename if it can't expand anything.  Let's
  ;; just hope that this doesn't break anything else.
  ;; It is not needed anymore since GNU Emacs 23.2.
166 167 168
  (unless (or (featurep 'xemacs)
	      ;; `featurep' has only one argument in XEmacs.
	      (funcall 'featurep 'files 'remote-wildcards))
169 170 171 172 173 174 175 176
    (defadvice file-expand-wildcards
      (around tramp-advice-file-expand-wildcards activate)
      (let ((name (ad-get-arg 0)))
	;; If it's a Tramp file, look if wildcards need to be expanded
	;; at all.
	(if (and
	     (tramp-tramp-file-p name)
	     (not (string-match
177 178
		   "[[*?]" (tramp-compat-funcall
			    'file-remote-p name 'localname))))
179 180 181 182 183 184 185 186 187
	    (setq ad-return-value (list name))
	  ;; Otherwise, just run the original function.
	  ad-do-it)))
    (add-hook
     'tramp-unload-hook
     (lambda ()
       (ad-remove-advice
	'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
       (ad-activate 'file-expand-wildcards)))))
188

189 190 191
;; `with-temp-message' does not exists in XEmacs.
(if (fboundp 'with-temp-message)
    (defalias 'tramp-compat-with-temp-message 'with-temp-message)
192
  (defmacro tramp-compat-with-temp-message (message &rest body)
193 194 195 196 197 198 199 200 201
    "Display MESSAGE temporarily if non-nil while BODY is evaluated."
    `(progn ,@body)))

;; `font-lock-add-keywords' does not exist in XEmacs.
(defun tramp-compat-font-lock-add-keywords (mode keywords &optional how)
  "Add highlighting KEYWORDS for MODE."
  (ignore-errors
    (tramp-compat-funcall 'font-lock-add-keywords mode keywords how)))

202 203 204 205 206
(defsubst tramp-compat-temporary-file-directory ()
  "Return name of directory for temporary files (compat function).
For Emacs, this is the variable `temporary-file-directory', for XEmacs
this is the function `temp-directory'."
  (cond
207
   ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
208
   ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
209 210 211 212 213 214 215 216 217 218 219
   ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
    (file-name-as-directory (getenv "TEMP")))
   ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
    (file-name-as-directory (getenv "TMP")))
   ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
    (file-name-as-directory (getenv "TMPDIR")))
   ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
   (t (message (concat "Neither `temporary-file-directory' nor "
		       "`temp-directory' is defined -- using /tmp."))
      (file-name-as-directory "/tmp"))))

220 221 222
;; `make-temp-file' exists in Emacs only.  On XEmacs, we use our own
;; implementation with `make-temp-name', creating the temporary file
;; immediately in order to avoid a security hole.
223
(defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
224 225
  "Create a temporary file (compat function).
Add the extension of FILENAME, if existing."
226 227 228 229 230 231
  (let* (file-name-handler-alist
	 (prefix (expand-file-name
		  (symbol-value 'tramp-temp-name-prefix)
		  (tramp-compat-temporary-file-directory)))
	 (extension (file-name-extension filename t))
	 result)
232
    (condition-case nil
233
	(setq result
234
	      (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
235 236 237 238 239 240 241 242 243 244 245 246 247 248
      (error
       ;; We use our own implementation, taken from files.el.
       (while
	   (condition-case ()
	       (progn
		 (setq result (concat (make-temp-name prefix) extension))
		 (if dir-flag
		     (make-directory result)
		   (write-region "" nil result nil 'silent))
		 nil)
	     (file-already-exists t))
	 ;; The file was somehow created by someone else between
	 ;; `make-temp-name' and `write-region', let's try again.
	 nil)))
249 250
    result))

251
;; `most-positive-fixnum' does not exist in XEmacs.
252 253
(defsubst tramp-compat-most-positive-fixnum ()
  "Return largest positive integer value (compat function)."
254 255
  (cond
   ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
256
   ;; Default value in XEmacs.
257
   (t 134217727)))
258

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
(defun tramp-compat-decimal-to-octal (i)
  "Return a string consisting of the octal digits of I.
Not actually used.  Use `(format \"%o\" i)' instead?"
  (cond ((< i 0) (error "Cannot convert negative number to octal"))
        ((not (integerp i)) (error "Cannot convert non-integer to octal"))
        ((zerop i) "0")
        (t (concat (tramp-compat-decimal-to-octal (/ i 8))
                   (number-to-string (% i 8))))))

;; Kudos to Gerd Moellmann for this suggestion.
(defun tramp-compat-octal-to-decimal (ostr)
  "Given a string of octal digits, return a decimal number."
  (let ((x (or ostr "")))
    ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
    (unless (string-match "\\`[0-7]*\\'" x)
      (error "Non-octal junk in string `%s'" x))
    (string-to-number ostr 8)))

277
;; ID-FORMAT does not exists in XEmacs.
278 279 280 281 282
(defun tramp-compat-file-attributes (filename &optional id-format)
  "Like `file-attributes' for Tramp files (compat function)."
  (cond
   ((or (null id-format) (eq id-format 'integer))
    (file-attributes filename))
283 284
   ((tramp-tramp-file-p filename)
    (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 290

;; PRESERVE-UID-GID has been introduced with Emacs 23.  It does not
;; 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 294
  (filename newname &optional ok-if-already-exists keep-date
	    preserve-uid-gid preserve-selinux-context)
295
  "Like `copy-file' for Tramp files (compat function)."
296 297 298 299 300 301 302 303 304 305 306
  (cond
   (preserve-selinux-context
    (tramp-compat-funcall
     'copy-file filename newname ok-if-already-exists keep-date
     preserve-uid-gid preserve-selinux-context))
   (preserve-uid-gid
    (tramp-compat-funcall
     'copy-file filename newname ok-if-already-exists keep-date
     preserve-uid-gid))
   (t
    (copy-file filename newname ok-if-already-exists keep-date))))
307

308 309 310 311 312 313
;; `copy-directory' is a new function in Emacs 23.2.  Implementation
;; is taken from there.
(defun tramp-compat-copy-directory
  (directory newname &optional keep-time parents)
  "Make a copy of DIRECTORY (compat function)."
  (if (fboundp 'copy-directory)
314
      (tramp-compat-funcall 'copy-directory directory newname keep-time parents)
315

316 317
    ;; If `default-directory' is a remote directory, make sure we find
    ;; its `copy-directory' handler.
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
    (let ((handler (or (find-file-name-handler directory 'copy-directory)
		       (find-file-name-handler newname 'copy-directory))))
      (if handler
	  (funcall handler 'copy-directory directory newname keep-time parents)

	;; Compute target name.
	(setq directory (directory-file-name (expand-file-name directory))
	      newname   (directory-file-name (expand-file-name newname)))
	(if (and (file-directory-p newname)
		 (not (string-equal (file-name-nondirectory directory)
				    (file-name-nondirectory newname))))
	    (setq newname
		  (expand-file-name
		   (file-name-nondirectory directory) newname)))
	(if (not (file-directory-p newname)) (make-directory newname parents))

	;; Copy recursively.
	(mapc
	 (lambda (file)
	   (if (file-directory-p file)
	       (tramp-compat-copy-directory file newname keep-time parents)
	     (copy-file file newname t keep-time)))
	 ;; We do not want to delete "." and "..".
	 (directory-files
	  directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))

	;; Set directory attributes.
	(set-file-modes newname (file-modes directory))
	(if keep-time
	    (set-file-times newname (nth 5 (file-attributes directory))))))))

349 350
;; TRASH has been introduced with Emacs 24.1.
(defun tramp-compat-delete-file (filename &optional trash)
351
  "Like `delete-file' for Tramp files (compat function)."
352 353 354 355 356 357
  (condition-case nil
      (tramp-compat-funcall 'delete-file filename trash)
    ;; This Emacs version does not support the TRASH flag.
    (wrong-number-of-arguments
     (let ((delete-by-moving-to-trash
	    (and (boundp 'delete-by-moving-to-trash)
358
		 (symbol-value 'delete-by-moving-to-trash)
359 360
		 trash)))
       (delete-file filename)))))
361

362 363 364
;; RECURSIVE has been introduced with Emacs 23.2.
(defun tramp-compat-delete-directory (directory &optional recursive)
  "Like `delete-directory' for Tramp files (compat function)."
365 366 367
  (if (null recursive)
      (delete-directory directory)
    (condition-case nil
368
	(tramp-compat-funcall 'delete-directory directory recursive)
369 370
      ;; This Emacs version does not support the RECURSIVE flag.  We
      ;; use the implementation from Emacs 23.2.
371
      (wrong-number-of-arguments
372 373 374 375 376 377 378 379 380 381 382 383
       (setq directory (directory-file-name (expand-file-name directory)))
       (if (not (file-symlink-p directory))
	   (mapc (lambda (file)
		   (if (eq t (car (file-attributes file)))
		       (tramp-compat-delete-directory file recursive)
		     (delete-file file)))
		 (directory-files
		  directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
       (delete-directory directory)))))

;; `number-sequence' does not exist in XEmacs.  Implementation is
;; taken from Emacs 23.
384 385 386
(defun tramp-compat-number-sequence (from &optional to inc)
  "Return a sequence of numbers from FROM to TO as a list (compat function)."
  (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
387
      (tramp-compat-funcall 'number-sequence from to inc)
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    (if (or (not to) (= from to))
	(list from)
      (or inc (setq inc 1))
      (when (zerop inc) (error "The increment can not be zero"))
      (let (seq (n 0) (next from))
	(if (> inc 0)
	    (while (<= next to)
	      (setq seq (cons next seq)
		    n (1+ n)
		    next (+ from (* n inc))))
	  (while (>= next to)
	    (setq seq (cons next seq)
		  n (1+ n)
		  next (+ from (* n inc)))))
	(nreverse seq)))))

404 405 406 407 408 409 410
(defun tramp-compat-split-string (string pattern)
  "Like `split-string' but omit empty strings.
In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
This is, the first, empty, element is omitted.  In XEmacs, the first
element is not omitted."
  (delete "" (split-string string pattern)))

411 412 413 414 415 416 417 418 419 420 421 422 423 424
(defun tramp-compat-call-process
  (program &optional infile destination display &rest args)
  "Calls `call-process' on the local host.
This is needed because for some Emacs flavors Tramp has
defadviced `call-process' to behave like `process-file'.  The
Lisp error raised when PROGRAM is nil is trapped also, returning 1."
  (let ((default-directory
	  (if (file-remote-p default-directory)
	      (tramp-compat-temporary-file-directory)
	    default-directory)))
    (if (executable-find program)
	(apply 'call-process program infile destination display args)
      1)))

425 426 427 428 429 430
(defun tramp-compat-process-running-p (process-name)
  "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
  (when (stringp process-name)
    (cond
     ;; GNU Emacs 22 on w32.
     ((fboundp 'w32-window-exists-p)
431
      (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
432 433 434 435

     ;; GNU Emacs 23.
     ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
      (let (result)
436 437
	(dolist (pid (tramp-compat-funcall 'list-system-processes) result)
	  (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
438 439 440 441 442 443 444 445 446
	    (when (and (string-equal
                        (cdr (assoc 'user attributes)) (user-login-name))
                       (let ((comm (cdr (assoc 'comm attributes))))
                         ;; The returned command name could be truncated
                         ;; to 15 characters.  Therefore, we cannot check
                         ;; for `string-equal'.
                         (and comm (string-match
                                    (concat "^" (regexp-quote comm))
                                    process-name))))
447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	      (setq result t))))))

     ;; Fallback, if there is no Lisp support yet.
     (t (let ((default-directory
		(if (file-remote-p default-directory)
		    (tramp-compat-temporary-file-directory)
		  default-directory))
	      (unix95 (getenv "UNIX95"))
	      result)
	  (setenv "UNIX95" "1")
	  (when (member
		 (user-login-name)
		 (tramp-compat-split-string
		  (shell-command-to-string
		   (format "ps -C %s -o user=" process-name))
		  "[ \f\t\n\r\v]+"))
	    (setq result t))
	  (setenv "UNIX95" unix95)
	  result)))))

467 468 469 470 471 472 473 474 475 476 477 478
;; The following functions do not exist in XEmacs.  We ignore this;
;; they are used for checking a remote tty.
(defun tramp-compat-process-get (process propname)
  "Return the value of PROCESS' PROPNAME property.
This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
  (ignore-errors (tramp-compat-funcall 'process-get process propname)))

(defun tramp-compat-process-put (process propname value)
  "Change PROCESS' PROPNAME property to VALUE.
It can be retrieved with `(process-get PROCESS PROPNAME)'."
  (ignore-errors (tramp-compat-funcall 'process-put process propname value)))

479 480 481 482 483 484 485 486
(defun tramp-compat-set-process-query-on-exit-flag (process flag)
  "Specify if query is needed for process when Emacs is exited.
If the second argument flag is non-nil, Emacs will query the user before
exiting if process is running."
  (if (fboundp 'set-process-query-on-exit-flag)
      (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
    (tramp-compat-funcall 'process-kill-without-query process flag)))

487 488 489 490
(add-hook 'tramp-unload-hook
	  (lambda ()
	    (unload-feature 'tramp-compat 'force)))

491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
(defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type)
  "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
EOL-TYPE can be one of `dos', `unix', or `mac'."
  (cond ((fboundp 'coding-system-change-eol-conversion)
         (tramp-compat-funcall
	  'coding-system-change-eol-conversion coding-system eol-type))
        ((fboundp 'subsidiary-coding-system)
         (tramp-compat-funcall
	  'subsidiary-coding-system coding-system
	  (cond ((eq eol-type 'dos) 'crlf)
		((eq eol-type 'unix) 'lf)
		((eq eol-type 'mac) 'cr)
		(t
		 (error "Unknown EOL-TYPE `%s', must be %s"
			eol-type
			"`dos', `unix', or `mac'")))))
        (t (error "Can't change EOL conversion -- is MULE missing?"))))

509 510 511 512 513
(provide 'tramp-compat)

;;; TODO:

;;; tramp-compat.el ends here