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

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2007-2015 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 Emacs 24.  This
;; package provides compatibility functions for Emacs 22, Emacs 23,
;; XEmacs 21.4+ and SXEmacs 22.
29 30 31

;;; Code:

Michael Albinus's avatar
Michael Albinus committed
32
;; Pacify byte-compiler.
33 34 35 36 37
(eval-when-compile
  (require 'cl))

(eval-and-compile

Michael Albinus's avatar
Michael Albinus committed
38 39 40 41 42
  ;; GNU Emacs 22.
  (unless (fboundp 'ignore-errors)
    (load "cl" 'noerror)
    (load "cl-macs" 'noerror))

Michael Albinus's avatar
Michael Albinus committed
43 44 45 46 47 48 49 50 51
  ;; Some packages must be required for XEmacs, because we compile
  ;; with -no-autoloads.
  (when (featurep 'xemacs)
    (require 'cus-edit)
    (require 'env)
    (require 'executable)
    (require 'outline)
    (require 'passwd)
    (require 'pp)
52 53
    (require 'regexp-opt)
    (require 'time-date))
Michael Albinus's avatar
Michael Albinus committed
54

55
  (require 'advice)
56
  (require 'custom)
57
  (require 'format-spec)
58
  (require 'shell)
59

60
  (require 'trampver)
Michael Albinus's avatar
Michael Albinus committed
61 62
  (require 'tramp-loaddefs)

63 64 65 66 67 68 69 70 71 72 73
  ;; 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))
74

75 76 77 78
  ;; Load the appropriate timer package.
  (if (featurep 'xemacs)
      (require 'timer-funcs)
    (require 'timer))
79

80 81 82 83 84 85 86 87
  ;; 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.
88
  (unless (boundp 'last-coding-system-used)
89
    (defvar last-coding-system-used nil))
90

91 92 93
  ;; `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.
94 95
  ;; Note that it was removed altogether in Emacs 24.1.
  (when (boundp 'directory-sep-char)
96
    (defvar byte-compile-not-obsolete-var nil)
97 98
    (setq byte-compile-not-obsolete-var 'directory-sep-char)
    ;; Emacs 23.2.
99
    (defvar byte-compile-not-obsolete-vars nil)
100
    (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
101

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

108 109 110 111 112 113 114 115 116
  ;; 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)))))

117
  ;; `set-buffer-multibyte' comes from Emacs Leim.
118
  (unless (fboundp 'set-buffer-multibyte)
119
    (defalias 'set-buffer-multibyte 'ignore))
120

121 122 123 124
  ;; The following functions cannot be aliases of the corresponding
  ;; `tramp-handle-*' functions, because this would bypass the locking
  ;; mechanism.

125
  ;; `file-remote-p' has been introduced with Emacs 22.  The version
126
  ;; of XEmacs is not a magic file name function (yet).
127
  (unless (fboundp 'file-remote-p)
128 129 130
    (defalias 'file-remote-p
      (lambda (file &optional identification connected)
	(when (tramp-tramp-file-p file)
Michael Albinus's avatar
Michael Albinus committed
131 132
	  (tramp-compat-funcall
	   'tramp-file-name-handler
133
	   'file-remote-p file identification connected)))))
134

135
  ;; `process-file' does not exist in XEmacs.
136
  (unless (fboundp 'process-file)
137 138 139 140 141 142
    (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)))))
143 144 145

  ;; `start-file-process' is new in Emacs 23.
  (unless (fboundp 'start-file-process)
146 147 148 149 150 151
    (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)))))
152 153 154

  ;; `set-file-times' is also new in Emacs 23.
  (unless (fboundp 'set-file-times)
155 156 157
    (defalias 'set-file-times
      (lambda (filename &optional time)
	(when (tramp-tramp-file-p filename)
Michael Albinus's avatar
Michael Albinus committed
158 159
	  (tramp-compat-funcall
	   'tramp-file-name-handler 'set-file-times filename time)))))
160 161

  ;; We currently use "[" and "]" in the filename format for IPv6
162
  ;; hosts of GNU Emacs.  This means that Emacs wants to expand
163 164 165 166 167 168
  ;; 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.
169 170 171
  (unless (or (featurep 'xemacs)
	      ;; `featurep' has only one argument in XEmacs.
	      (funcall 'featurep 'files 'remote-wildcards))
172 173 174 175 176 177 178 179
    (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
180 181
		   "[[*?]" (tramp-compat-funcall
			    'file-remote-p name 'localname))))
182 183 184 185 186 187 188 189 190
	    (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)))))
191

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

Michael Albinus's avatar
Michael Albinus committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
;; `condition-case-unless-debug' is introduced with Emacs 24.
(if (fboundp 'condition-case-unless-debug)
    (defalias 'tramp-compat-condition-case-unless-debug
      'condition-case-unless-debug)
  (defmacro tramp-compat-condition-case-unless-debug
    (var bodyform &rest handlers)
  "Like `condition-case' except that it does not catch anything when debugging."
    (declare (debug condition-case) (indent 2))
    (let ((bodysym (make-symbol "body")))
      `(let ((,bodysym (lambda () ,bodyform)))
	 (if debug-on-error
	     (funcall ,bodysym)
	   (condition-case ,var
	       (funcall ,bodysym)
	     ,@handlers))))))

215 216 217 218 219 220
;; `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)))

221 222 223 224
(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'."
225
  (let (file-name-handler-alist)
Michael Albinus's avatar
Michael Albinus committed
226 227
    ;; We must return a local directory.  If it is remote, we could
    ;; run into an infloop.
228
    (cond
Michael Albinus's avatar
Michael Albinus committed
229 230
     ((and (boundp 'temporary-file-directory)
	   (eval (car (get 'temporary-file-directory 'standard-value)))))
231 232 233 234 235 236 237 238
     ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
     ((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"))
239 240
     (t (message (concat "Neither ‘temporary-file-directory’ nor "
			 "‘temp-directory’ is defined -- using /tmp."))
241
	(file-name-as-directory "/tmp")))))
242

243 244 245
;; `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.
246
(defsubst tramp-compat-make-temp-file (f &optional dir-flag)
247
  "Create a temporary file (compat function).
248
Add the extension of F, if existing."
249 250 251 252
  (let* (file-name-handler-alist
	 (prefix (expand-file-name
		  (symbol-value 'tramp-temp-name-prefix)
		  (tramp-compat-temporary-file-directory)))
253
	 (extension (file-name-extension f t))
254
	 result)
255
    (condition-case nil
256
	(setq result
257
	      (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
258 259 260 261 262 263 264 265 266 267 268 269 270 271
      (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)))
272 273
    result))

274
;; `most-positive-fixnum' does not exist in XEmacs.
275 276
(defsubst tramp-compat-most-positive-fixnum ()
  "Return largest positive integer value (compat function)."
277 278
  (cond
   ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
279
   ;; Default value in XEmacs.
280
   (t 134217727)))
281

282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
(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)
297
      (error "Non-octal junk in string ‘%s’" x))
298 299
    (string-to-number ostr 8)))

300
;; ID-FORMAT does not exist in XEmacs.
301 302 303 304 305
(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))
306
   ((tramp-tramp-file-p filename)
Michael Albinus's avatar
Michael Albinus committed
307 308
    (tramp-compat-funcall
     'tramp-file-name-handler 'file-attributes filename id-format))
309
   (t (condition-case nil
310
	  (tramp-compat-funcall 'file-attributes filename id-format)
311
	(wrong-number-of-arguments (file-attributes filename))))))
312

313
;; PRESERVE-UID-GID does not exist in XEmacs.
314 315
;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with Emacs 24.1
;; (as PRESERVE-SELINUX-CONTEXT), and renamed in Emacs 24.3.
316
(defun tramp-compat-copy-file
317
  (filename newname &optional ok-if-already-exists keep-date
318
	    preserve-uid-gid preserve-extended-attributes)
319
  "Like `copy-file' for Tramp files (compat function)."
320
  (cond
321
   (preserve-extended-attributes
322 323 324 325 326 327 328
    (condition-case nil
	(tramp-compat-funcall
	 'copy-file filename newname ok-if-already-exists keep-date
	 preserve-uid-gid preserve-extended-attributes)
      (wrong-number-of-arguments
       (tramp-compat-copy-file
	filename newname ok-if-already-exists keep-date preserve-uid-gid))))
329
   (preserve-uid-gid
330 331 332 333 334 335 336
    (condition-case nil
	(tramp-compat-funcall
	 'copy-file filename newname ok-if-already-exists keep-date
	 preserve-uid-gid)
      (wrong-number-of-arguments
       (tramp-compat-copy-file
	filename newname ok-if-already-exists keep-date))))
337 338
   (t
    (copy-file filename newname ok-if-already-exists keep-date))))
339

340 341 342
;; `copy-directory' is a new function in Emacs 23.2.  Implementation
;; is taken from there.
(defun tramp-compat-copy-directory
Michael Albinus's avatar
Michael Albinus committed
343
  (directory newname &optional keep-time parents copy-contents)
344
  "Make a copy of DIRECTORY (compat function)."
Michael Albinus's avatar
Michael Albinus committed
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
  (condition-case nil
      (tramp-compat-funcall
       'copy-directory directory newname keep-time parents copy-contents)

    ;; `copy-directory' is either not implemented, or it does not
    ;; support the the COPY-CONTENTS flag.  For the time being, we
    ;; ignore COPY-CONTENTS as well.

    (error
     ;; If `default-directory' is a remote directory, make sure we
     ;; find its `copy-directory' handler.
     (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)))))))))
386

387 388
;; TRASH has been introduced with Emacs 24.1.
(defun tramp-compat-delete-file (filename &optional trash)
389
  "Like `delete-file' for Tramp files (compat function)."
390 391 392 393 394 395
  (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)
396
		 (symbol-value 'delete-by-moving-to-trash)
397 398
		 trash)))
       (delete-file filename)))))
399

400 401 402
;; RECURSIVE has been introduced with Emacs 23.2.  TRASH has been
;; introduced with Emacs 24.1.
(defun tramp-compat-delete-directory (directory &optional recursive trash)
403
  "Like `delete-directory' for Tramp files (compat function)."
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
  (condition-case nil
      (cond
       (trash
	(tramp-compat-funcall 'delete-directory directory recursive trash))
       (recursive
	(tramp-compat-funcall 'delete-directory directory recursive))
       (t
	(delete-directory directory)))
    ;; This Emacs version does not support the RECURSIVE or TRASH flag.  We
    ;; use the implementation from Emacs 23.2.
    (wrong-number-of-arguments
     (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 trash)
		   (tramp-compat-delete-file file trash)))
	       (directory-files
		directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
     (delete-directory directory))))
424

425 426 427 428 429 430 431
;; MUST-SUFFIX doesn't exist on XEmacs.
(defun tramp-compat-load (file &optional noerror nomessage nosuffix must-suffix)
  "Like `load' for Tramp files (compat function)."
  (if must-suffix
      (tramp-compat-funcall 'load file noerror nomessage nosuffix must-suffix)
    (load file noerror nomessage nosuffix)))

432 433
;; `number-sequence' does not exist in XEmacs.  Implementation is
;; taken from Emacs 23.
434 435 436
(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))
437
      (tramp-compat-funcall 'number-sequence from to inc)
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453
    (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)))))

454 455 456 457 458 459 460 461
(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)))

(defun tramp-compat-process-running-p (process-name)
462
  "Returns t if system process PROCESS-NAME is running for `user-login-name'."
463 464 465 466
  (when (stringp process-name)
    (cond
     ;; GNU Emacs 22 on w32.
     ((fboundp 'w32-window-exists-p)
467
      (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
468 469 470 471

     ;; GNU Emacs 23.
     ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
      (let (result)
472 473
	(dolist (pid (tramp-compat-funcall 'list-system-processes) result)
	  (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
474 475 476 477 478 479 480 481 482
	    (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))))
483 484 485 486
	      (setq result t))))))

     ;; Fallback, if there is no Lisp support yet.
     (t (let ((default-directory
Michael Albinus's avatar
Michael Albinus committed
487
		(if (tramp-tramp-file-p default-directory)
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
		    (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)))))

503 504 505 506 507 508 509 510 511 512 513 514
;; 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)))

515 516 517 518 519 520 521 522
(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)))

523
;; There exist different implementations for this function.
524 525 526 527 528 529 530 531 532 533 534 535 536
(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
537 538 539 540
		 (error
                  "Unknown EOL-TYPE ‘%s’, must be ‘dos’, ‘unix’, or ‘mac’"
                  eol-type)))))
        (t (error "Can’t change EOL conversion -- is MULE missing?"))))
541

542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
;; `replace-regexp-in-string' does not exist in XEmacs.
;; Implementation is taken from Emacs 24.
(if (fboundp 'replace-regexp-in-string)
    (defalias 'tramp-compat-replace-regexp-in-string 'replace-regexp-in-string)
  (defun tramp-compat-replace-regexp-in-string
    (regexp rep string &optional fixedcase literal subexp start)
    "Replace all matches for REGEXP with REP in STRING.

Return a new string containing the replacements.

Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
arguments with the same names of function `replace-match'.  If START
is non-nil, start replacements at that index in STRING.

REP is either a string used as the NEWTEXT arg of `replace-match' or a
function.  If it is a function, it is called with the actual text of each
match, and its value is used as the replacement text.  When REP is called,
the match data are the result of matching REGEXP against a substring
of STRING.

To replace only the first match (if any), make REGEXP match up to \\'
and replace a sub-expression, e.g.
  (replace-regexp-in-string \"\\\\(foo\\\\).*\\\\'\" \"bar\" \" foo foo\" nil nil 1)
    => \" bar foo\""

    (let ((l (length string))
	  (start (or start 0))
	  matches str mb me)
      (save-match-data
	(while (and (< start l) (string-match regexp string start))
	  (setq mb (match-beginning 0)
		me (match-end 0))
	  ;; If we matched the empty string, make sure we advance by one char
	  (when (= me mb) (setq me (min l (1+ mb))))
	  ;; Generate a replacement for the matched substring.
	  ;; Operate only on the substring to minimize string consing.
	  ;; Set up match data for the substring for replacement;
	  ;; presumably this is likely to be faster than munging the
	  ;; match data directly in Lisp.
	  (string-match regexp (setq str (substring string mb me)))
	  (setq matches
		(cons (replace-match (if (stringp rep)
					 rep
				       (funcall rep (match-string 0 str)))
				     fixedcase literal str subexp)
		      (cons (substring string start mb) ; unmatched prefix
			    matches)))
	  (setq start me))
	;; Reconstruct a string from the pieces.
	(setq matches (cons (substring string start l) matches)) ; leftover
	(apply #'concat (nreverse matches))))))

Michael Albinus's avatar
Michael Albinus committed
594 595 596 597
;; `default-toplevel-value' has been declared in Emacs 24.
(unless (fboundp 'default-toplevel-value)
  (defalias 'default-toplevel-value 'symbol-value))

598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613
;; ‘format-message’ is new in Emacs 25, and does not exist in XEmacs.
;; The substitute implementation always uses grave quoting style, for
;; compatibility with older Emacs.
(unless (fboundp 'format-message)
  (defalias 'format-message
    (lambda (format-string &rest args)
      (let ((restyled-format-string
             (let ((start (string-match "[‘’]" format-string)))
               (if start
                   (tramp-compat-replace-regexp-in-string
                    "[‘’]"
                    (lambda (match) (if (string-equal match "‘") "`" "'"))
                    format-string t t nil start)
                 format-string))))
      (apply #'format restyled-format-string args)))))

614 615
(add-hook 'tramp-unload-hook
	  (lambda ()
616
	    (unload-feature 'tramp-loaddefs 'force)
617 618
	    (unload-feature 'tramp-compat 'force)))

619 620 621 622 623
(provide 'tramp-compat)

;;; TODO:

;;; tramp-compat.el ends here