Update copyright year to 2015
[emacs.git] / lisp / net / tramp-compat.el
blob3ec90ca556f1616ede0373072441029782706a2a
1 ;;; tramp-compat.el --- Tramp compatibility functions
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
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/>.
24 ;;; Commentary:
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.
30 ;;; Code:
32 ;; Pacify byte-compiler.
33 (eval-when-compile
34 (require 'cl))
36 (eval-and-compile
38 ;; GNU Emacs 22.
39 (unless (fboundp 'ignore-errors)
40 (load "cl" 'noerror)
41 (load "cl-macs" 'noerror))
43 ;; Some packages must be required for XEmacs, because we compile
44 ;; with -no-autoloads.
45 (when (featurep 'xemacs)
46 (require 'cus-edit)
47 (require 'env)
48 (require 'executable)
49 (require 'outline)
50 (require 'passwd)
51 (require 'pp)
52 (require 'regexp-opt)
53 (require 'time-date))
55 (require 'advice)
56 (require 'custom)
57 (require 'format-spec)
58 (require 'shell)
60 (require 'trampver)
61 (require 'tramp-loaddefs)
63 ;; As long as password.el is not part of (X)Emacs, it shouldn't be
64 ;; mandatory.
65 (if (featurep 'xemacs)
66 (load "password" 'noerror)
67 (or (require 'password-cache nil 'noerror)
68 (require 'password nil 'noerror))) ; Part of contrib.
70 ;; auth-source is relatively new.
71 (if (featurep 'xemacs)
72 (load "auth-source" 'noerror)
73 (require 'auth-source nil 'noerror))
75 ;; Load the appropriate timer package.
76 (if (featurep 'xemacs)
77 (require 'timer-funcs)
78 (require 'timer))
80 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
81 ;; Currently, XEmacs supports this.
82 (when (featurep 'xemacs)
83 (unless (boundp 'byte-compile-default-warnings)
84 (defvar byte-compile-default-warnings nil))
85 (delq 'unused-vars byte-compile-default-warnings))
87 ;; `last-coding-system-used' is unknown in XEmacs.
88 (unless (boundp 'last-coding-system-used)
89 (defvar last-coding-system-used nil))
91 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
92 ;; used in XEmacs, so we set it here and there. The following is
93 ;; needed to pacify Emacs byte-compiler.
94 ;; Note that it was removed altogether in Emacs 24.1.
95 (when (boundp 'directory-sep-char)
96 (defvar byte-compile-not-obsolete-var nil)
97 (setq byte-compile-not-obsolete-var 'directory-sep-char)
98 ;; Emacs 23.2.
99 (defvar byte-compile-not-obsolete-vars nil)
100 (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
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 (unless (boundp 'remote-file-name-inhibit-cache)
106 (defvar remote-file-name-inhibit-cache nil))
108 ;; For not existing functions, or functions with a changed argument
109 ;; list, there are compiler warnings. We want to avoid them in
110 ;; cases we know what we do.
111 (defmacro tramp-compat-funcall (function &rest arguments)
112 (if (featurep 'xemacs)
113 `(funcall (symbol-function ,function) ,@arguments)
114 `(when (or (subrp ,function) (functionp ,function))
115 (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))
121 ;; The following functions cannot be aliases of the corresponding
122 ;; `tramp-handle-*' functions, because this would bypass the locking
123 ;; 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 (defalias 'file-remote-p
129 (lambda (file &optional identification connected)
130 (when (tramp-tramp-file-p file)
131 (tramp-compat-funcall
132 'tramp-file-name-handler
133 'file-remote-p file identification connected)))))
135 ;; `process-file' does not exist in XEmacs.
136 (unless (fboundp 'process-file)
137 (defalias 'process-file
138 (lambda (program &optional infile buffer display &rest args)
139 (when (tramp-tramp-file-p default-directory)
140 (apply
141 'tramp-file-name-handler
142 'process-file program infile buffer display args)))))
144 ;; `start-file-process' is new in Emacs 23.
145 (unless (fboundp 'start-file-process)
146 (defalias 'start-file-process
147 (lambda (name buffer program &rest program-args)
148 (when (tramp-tramp-file-p default-directory)
149 (apply
150 'tramp-file-name-handler
151 'start-file-process name buffer program program-args)))))
153 ;; `set-file-times' is also new in Emacs 23.
154 (unless (fboundp 'set-file-times)
155 (defalias 'set-file-times
156 (lambda (filename &optional time)
157 (when (tramp-tramp-file-p filename)
158 (tramp-compat-funcall
159 'tramp-file-name-handler 'set-file-times filename time)))))
161 ;; We currently use "[" and "]" in the filename format for IPv6
162 ;; hosts of GNU Emacs. This means that Emacs wants to expand
163 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
164 ;; because no expansion could be found. We detect this situation
165 ;; and do something really awful: we have `file-expand-wildcards'
166 ;; return the original filename if it can't expand anything. Let's
167 ;; just hope that this doesn't break anything else.
168 ;; It is not needed anymore since GNU Emacs 23.2.
169 (unless (or (featurep 'xemacs)
170 ;; `featurep' has only one argument in XEmacs.
171 (funcall 'featurep 'files 'remote-wildcards))
172 (defadvice file-expand-wildcards
173 (around tramp-advice-file-expand-wildcards activate)
174 (let ((name (ad-get-arg 0)))
175 ;; If it's a Tramp file, look if wildcards need to be expanded
176 ;; at all.
177 (if (and
178 (tramp-tramp-file-p name)
179 (not (string-match
180 "[[*?]" (tramp-compat-funcall
181 'file-remote-p name 'localname))))
182 (setq ad-return-value (list name))
183 ;; Otherwise, just run the original function.
184 ad-do-it)))
185 (add-hook
186 'tramp-unload-hook
187 (lambda ()
188 (ad-remove-advice
189 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
190 (ad-activate 'file-expand-wildcards)))))
192 ;; `with-temp-message' does not exist in XEmacs.
193 (if (fboundp 'with-temp-message)
194 (defalias 'tramp-compat-with-temp-message 'with-temp-message)
195 (defmacro tramp-compat-with-temp-message (_message &rest body)
196 "Display MESSAGE temporarily if non-nil while BODY is evaluated."
197 `(progn ,@body)))
199 ;; `condition-case-unless-debug' is introduced with Emacs 24.
200 (if (fboundp 'condition-case-unless-debug)
201 (defalias 'tramp-compat-condition-case-unless-debug
202 'condition-case-unless-debug)
203 (defmacro tramp-compat-condition-case-unless-debug
204 (var bodyform &rest handlers)
205 "Like `condition-case' except that it does not catch anything when debugging."
206 (declare (debug condition-case) (indent 2))
207 (let ((bodysym (make-symbol "body")))
208 `(let ((,bodysym (lambda () ,bodyform)))
209 (if debug-on-error
210 (funcall ,bodysym)
211 (condition-case ,var
212 (funcall ,bodysym)
213 ,@handlers))))))
215 ;; `font-lock-add-keywords' does not exist in XEmacs.
216 (defun tramp-compat-font-lock-add-keywords (mode keywords &optional how)
217 "Add highlighting KEYWORDS for MODE."
218 (ignore-errors
219 (tramp-compat-funcall 'font-lock-add-keywords mode keywords how)))
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 (let (file-name-handler-alist)
226 ;; We must return a local directory. If it is remote, we could
227 ;; run into an infloop.
228 (cond
229 ((and (boundp 'temporary-file-directory)
230 (eval (car (get 'temporary-file-directory 'standard-value)))))
231 ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
232 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
233 (file-name-as-directory (getenv "TEMP")))
234 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
235 (file-name-as-directory (getenv "TMP")))
236 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
237 (file-name-as-directory (getenv "TMPDIR")))
238 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
239 (t (message (concat "Neither `temporary-file-directory' nor "
240 "`temp-directory' is defined -- using /tmp."))
241 (file-name-as-directory "/tmp")))))
243 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
244 ;; implementation with `make-temp-name', creating the temporary file
245 ;; 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 (let* (file-name-handler-alist
250 (prefix (expand-file-name
251 (symbol-value 'tramp-temp-name-prefix)
252 (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 (error
259 ;; We use our own implementation, taken from files.el.
260 (while
261 (condition-case ()
262 (progn
263 (setq result (concat (make-temp-name prefix) extension))
264 (if dir-flag
265 (make-directory result)
266 (write-region "" nil result nil 'silent))
267 nil)
268 (file-already-exists t))
269 ;; The file was somehow created by someone else between
270 ;; `make-temp-name' and `write-region', let's try again.
271 nil)))
272 result))
274 ;; `most-positive-fixnum' does not exist in XEmacs.
275 (defsubst tramp-compat-most-positive-fixnum ()
276 "Return largest positive integer value (compat function)."
277 (cond
278 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
279 ;; Default value in XEmacs.
280 (t 134217727)))
282 (defun tramp-compat-decimal-to-octal (i)
283 "Return a string consisting of the octal digits of I.
284 Not actually used. Use `(format \"%o\" i)' instead?"
285 (cond ((< i 0) (error "Cannot convert negative number to octal"))
286 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
287 ((zerop i) "0")
288 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
289 (number-to-string (% i 8))))))
291 ;; Kudos to Gerd Moellmann for this suggestion.
292 (defun tramp-compat-octal-to-decimal (ostr)
293 "Given a string of octal digits, return a decimal number."
294 (let ((x (or ostr "")))
295 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
296 (unless (string-match "\\`[0-7]*\\'" x)
297 (error "Non-octal junk in string `%s'" x))
298 (string-to-number ostr 8)))
300 ;; ID-FORMAT does not exist in XEmacs.
301 (defun tramp-compat-file-attributes (filename &optional id-format)
302 "Like `file-attributes' for Tramp files (compat function)."
303 (cond
304 ((or (null id-format) (eq id-format 'integer))
305 (file-attributes filename))
306 ((tramp-tramp-file-p filename)
307 (tramp-compat-funcall
308 '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))))))
313 ;; PRESERVE-UID-GID does not exist in XEmacs.
314 ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with Emacs 24.1
315 ;; (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 (condition-case nil
323 (tramp-compat-funcall
324 'copy-file filename newname ok-if-already-exists keep-date
325 preserve-uid-gid preserve-extended-attributes)
326 (wrong-number-of-arguments
327 (tramp-compat-copy-file
328 filename newname ok-if-already-exists keep-date preserve-uid-gid))))
329 (preserve-uid-gid
330 (condition-case nil
331 (tramp-compat-funcall
332 'copy-file filename newname ok-if-already-exists keep-date
333 preserve-uid-gid)
334 (wrong-number-of-arguments
335 (tramp-compat-copy-file
336 filename newname ok-if-already-exists keep-date))))
338 (copy-file filename newname ok-if-already-exists keep-date))))
340 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
341 ;; is taken from there.
342 (defun tramp-compat-copy-directory
343 (directory newname &optional keep-time parents copy-contents)
344 "Make a copy of DIRECTORY (compat function)."
345 (condition-case nil
346 (tramp-compat-funcall
347 'copy-directory directory newname keep-time parents copy-contents)
349 ;; `copy-directory' is either not implemented, or it does not
350 ;; support the the COPY-CONTENTS flag. For the time being, we
351 ;; ignore COPY-CONTENTS as well.
353 (error
354 ;; If `default-directory' is a remote directory, make sure we
355 ;; find its `copy-directory' handler.
356 (let ((handler (or (find-file-name-handler directory 'copy-directory)
357 (find-file-name-handler newname 'copy-directory))))
358 (if handler
359 (funcall handler 'copy-directory directory newname keep-time parents)
361 ;; Compute target name.
362 (setq directory (directory-file-name (expand-file-name directory))
363 newname (directory-file-name (expand-file-name newname)))
364 (if (and (file-directory-p newname)
365 (not (string-equal (file-name-nondirectory directory)
366 (file-name-nondirectory newname))))
367 (setq newname
368 (expand-file-name
369 (file-name-nondirectory directory) newname)))
370 (if (not (file-directory-p newname)) (make-directory newname parents))
372 ;; Copy recursively.
373 (mapc
374 (lambda (file)
375 (if (file-directory-p file)
376 (tramp-compat-copy-directory file newname keep-time parents)
377 (copy-file file newname t keep-time)))
378 ;; We do not want to delete "." and "..".
379 (directory-files
380 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
382 ;; Set directory attributes.
383 (set-file-modes newname (file-modes directory))
384 (if keep-time
385 (set-file-times newname (nth 5 (file-attributes directory)))))))))
387 ;; TRASH has been introduced with Emacs 24.1.
388 (defun tramp-compat-delete-file (filename &optional trash)
389 "Like `delete-file' for Tramp files (compat function)."
390 (condition-case nil
391 (tramp-compat-funcall 'delete-file filename trash)
392 ;; This Emacs version does not support the TRASH flag.
393 (wrong-number-of-arguments
394 (let ((delete-by-moving-to-trash
395 (and (boundp 'delete-by-moving-to-trash)
396 (symbol-value 'delete-by-moving-to-trash)
397 trash)))
398 (delete-file filename)))))
400 ;; RECURSIVE has been introduced with Emacs 23.2. TRASH has been
401 ;; introduced with Emacs 24.1.
402 (defun tramp-compat-delete-directory (directory &optional recursive trash)
403 "Like `delete-directory' for Tramp files (compat function)."
404 (condition-case nil
405 (cond
406 (trash
407 (tramp-compat-funcall 'delete-directory directory recursive trash))
408 (recursive
409 (tramp-compat-funcall 'delete-directory directory recursive))
411 (delete-directory directory)))
412 ;; This Emacs version does not support the RECURSIVE or TRASH flag. We
413 ;; use the implementation from Emacs 23.2.
414 (wrong-number-of-arguments
415 (setq directory (directory-file-name (expand-file-name directory)))
416 (if (not (file-symlink-p directory))
417 (mapc (lambda (file)
418 (if (eq t (car (file-attributes file)))
419 (tramp-compat-delete-directory file recursive trash)
420 (tramp-compat-delete-file file trash)))
421 (directory-files
422 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
423 (delete-directory directory))))
425 ;; MUST-SUFFIX doesn't exist on XEmacs.
426 (defun tramp-compat-load (file &optional noerror nomessage nosuffix must-suffix)
427 "Like `load' for Tramp files (compat function)."
428 (if must-suffix
429 (tramp-compat-funcall 'load file noerror nomessage nosuffix must-suffix)
430 (load file noerror nomessage nosuffix)))
432 ;; `number-sequence' does not exist in XEmacs. Implementation is
433 ;; taken from Emacs 23.
434 (defun tramp-compat-number-sequence (from &optional to inc)
435 "Return a sequence of numbers from FROM to TO as a list (compat function)."
436 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
437 (tramp-compat-funcall 'number-sequence from to inc)
438 (if (or (not to) (= from to))
439 (list from)
440 (or inc (setq inc 1))
441 (when (zerop inc) (error "The increment can not be zero"))
442 (let (seq (n 0) (next from))
443 (if (> inc 0)
444 (while (<= next to)
445 (setq seq (cons next seq)
446 n (1+ n)
447 next (+ from (* n inc))))
448 (while (>= next to)
449 (setq seq (cons next seq)
450 n (1+ n)
451 next (+ from (* n inc)))))
452 (nreverse seq)))))
454 (defun tramp-compat-split-string (string pattern)
455 "Like `split-string' but omit empty strings.
456 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
457 This is, the first, empty, element is omitted. In XEmacs, the first
458 element is not omitted."
459 (delete "" (split-string string pattern)))
461 (defun tramp-compat-process-running-p (process-name)
462 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
463 (when (stringp process-name)
464 (cond
465 ;; GNU Emacs 22 on w32.
466 ((fboundp 'w32-window-exists-p)
467 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
469 ;; GNU Emacs 23.
470 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
471 (let (result)
472 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
473 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
474 (when (and (string-equal
475 (cdr (assoc 'user attributes)) (user-login-name))
476 (let ((comm (cdr (assoc 'comm attributes))))
477 ;; The returned command name could be truncated
478 ;; to 15 characters. Therefore, we cannot check
479 ;; for `string-equal'.
480 (and comm (string-match
481 (concat "^" (regexp-quote comm))
482 process-name))))
483 (setq result t))))))
485 ;; Fallback, if there is no Lisp support yet.
486 (t (let ((default-directory
487 (if (tramp-tramp-file-p default-directory)
488 (tramp-compat-temporary-file-directory)
489 default-directory))
490 (unix95 (getenv "UNIX95"))
491 result)
492 (setenv "UNIX95" "1")
493 (when (member
494 (user-login-name)
495 (tramp-compat-split-string
496 (shell-command-to-string
497 (format "ps -C %s -o user=" process-name))
498 "[ \f\t\n\r\v]+"))
499 (setq result t))
500 (setenv "UNIX95" unix95)
501 result)))))
503 ;; The following functions do not exist in XEmacs. We ignore this;
504 ;; they are used for checking a remote tty.
505 (defun tramp-compat-process-get (process propname)
506 "Return the value of PROCESS' PROPNAME property.
507 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
508 (ignore-errors (tramp-compat-funcall 'process-get process propname)))
510 (defun tramp-compat-process-put (process propname value)
511 "Change PROCESS' PROPNAME property to VALUE.
512 It can be retrieved with `(process-get PROCESS PROPNAME)'."
513 (ignore-errors (tramp-compat-funcall 'process-put process propname value)))
515 (defun tramp-compat-set-process-query-on-exit-flag (process flag)
516 "Specify if query is needed for process when Emacs is exited.
517 If the second argument flag is non-nil, Emacs will query the user before
518 exiting if process is running."
519 (if (fboundp 'set-process-query-on-exit-flag)
520 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
521 (tramp-compat-funcall 'process-kill-without-query process flag)))
523 ;; There exist different implementations for this function.
524 (defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type)
525 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
526 EOL-TYPE can be one of `dos', `unix', or `mac'."
527 (cond ((fboundp 'coding-system-change-eol-conversion)
528 (tramp-compat-funcall
529 'coding-system-change-eol-conversion coding-system eol-type))
530 ((fboundp 'subsidiary-coding-system)
531 (tramp-compat-funcall
532 'subsidiary-coding-system coding-system
533 (cond ((eq eol-type 'dos) 'crlf)
534 ((eq eol-type 'unix) 'lf)
535 ((eq eol-type 'mac) 'cr)
537 (error "Unknown EOL-TYPE `%s', must be %s"
538 eol-type
539 "`dos', `unix', or `mac'")))))
540 (t (error "Can't change EOL conversion -- is MULE missing?"))))
542 ;; `replace-regexp-in-string' does not exist in XEmacs.
543 ;; Implementation is taken from Emacs 24.
544 (if (fboundp 'replace-regexp-in-string)
545 (defalias 'tramp-compat-replace-regexp-in-string 'replace-regexp-in-string)
546 (defun tramp-compat-replace-regexp-in-string
547 (regexp rep string &optional fixedcase literal subexp start)
548 "Replace all matches for REGEXP with REP in STRING.
550 Return a new string containing the replacements.
552 Optional arguments FIXEDCASE, LITERAL and SUBEXP are like the
553 arguments with the same names of function `replace-match'. If START
554 is non-nil, start replacements at that index in STRING.
556 REP is either a string used as the NEWTEXT arg of `replace-match' or a
557 function. If it is a function, it is called with the actual text of each
558 match, and its value is used as the replacement text. When REP is called,
559 the match data are the result of matching REGEXP against a substring
560 of STRING.
562 To replace only the first match (if any), make REGEXP match up to \\'
563 and replace a sub-expression, e.g.
564 (replace-regexp-in-string \"\\\\(foo\\\\).*\\\\'\" \"bar\" \" foo foo\" nil nil 1)
565 => \" bar foo\""
567 (let ((l (length string))
568 (start (or start 0))
569 matches str mb me)
570 (save-match-data
571 (while (and (< start l) (string-match regexp string start))
572 (setq mb (match-beginning 0)
573 me (match-end 0))
574 ;; If we matched the empty string, make sure we advance by one char
575 (when (= me mb) (setq me (min l (1+ mb))))
576 ;; Generate a replacement for the matched substring.
577 ;; Operate only on the substring to minimize string consing.
578 ;; Set up match data for the substring for replacement;
579 ;; presumably this is likely to be faster than munging the
580 ;; match data directly in Lisp.
581 (string-match regexp (setq str (substring string mb me)))
582 (setq matches
583 (cons (replace-match (if (stringp rep)
585 (funcall rep (match-string 0 str)))
586 fixedcase literal str subexp)
587 (cons (substring string start mb) ; unmatched prefix
588 matches)))
589 (setq start me))
590 ;; Reconstruct a string from the pieces.
591 (setq matches (cons (substring string start l) matches)) ; leftover
592 (apply #'concat (nreverse matches))))))
594 ;; `default-toplevel-value' has been declared in Emacs 24.
595 (unless (fboundp 'default-toplevel-value)
596 (defalias 'default-toplevel-value 'symbol-value))
598 (add-hook 'tramp-unload-hook
599 (lambda ()
600 (unload-feature 'tramp-loaddefs 'force)
601 (unload-feature 'tramp-compat 'force)))
603 (provide 'tramp-compat)
605 ;;; TODO:
607 ;;; tramp-compat.el ends here