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