]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
Uncomment the next-error-function integration in xref
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007-2016 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 ;; GNU Emacs 22.
39 (unless (fboundp 'ignore-errors)
40 (load "cl" 'noerror)
41 (load "cl-macs" 'noerror))
42
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))
54
55 (require 'advice)
56 (require 'custom)
57 (require 'format-spec)
58 (require 'shell)
59 ;; Introduced in Emacs 23.2.
60 (require 'ucs-normalize nil 'noerror)
61
62 (require 'trampver)
63 (require 'tramp-loaddefs)
64
65 ;; As long as password.el is not part of (X)Emacs, it shouldn't be
66 ;; mandatory.
67 (if (featurep 'xemacs)
68 (load "password" 'noerror)
69 (or (require 'password-cache nil 'noerror)
70 (require 'password nil 'noerror))) ; Part of contrib.
71
72 ;; auth-source is relatively new.
73 (if (featurep 'xemacs)
74 (load "auth-source" 'noerror)
75 (require 'auth-source nil 'noerror))
76
77 ;; Load the appropriate timer package.
78 (if (featurep 'xemacs)
79 (require 'timer-funcs)
80 (require 'timer))
81
82 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
83 ;; Currently, XEmacs supports this.
84 (when (featurep 'xemacs)
85 (unless (boundp 'byte-compile-default-warnings)
86 (defvar byte-compile-default-warnings nil))
87 (delq 'unused-vars byte-compile-default-warnings))
88
89 ;; `last-coding-system-used' is unknown in XEmacs.
90 (unless (boundp 'last-coding-system-used)
91 (defvar last-coding-system-used nil))
92
93 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
94 ;; used in XEmacs, so we set it here and there. The following is
95 ;; needed to pacify Emacs byte-compiler.
96 ;; Note that it was removed altogether in Emacs 24.1.
97 (when (boundp 'directory-sep-char)
98 (defvar byte-compile-not-obsolete-var nil)
99 (setq byte-compile-not-obsolete-var 'directory-sep-char)
100 ;; Emacs 23.2.
101 (defvar byte-compile-not-obsolete-vars nil)
102 (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
103
104 ;; `remote-file-name-inhibit-cache' has been introduced with Emacs 24.1.
105 ;; Besides t, nil, and integer, we use also timestamps (as
106 ;; returned by `current-time') internally.
107 (unless (boundp 'remote-file-name-inhibit-cache)
108 (defvar remote-file-name-inhibit-cache nil))
109
110 ;; For not existing functions, or functions with a changed argument
111 ;; list, there are compiler warnings. We want to avoid them in
112 ;; cases we know what we do.
113 (defmacro tramp-compat-funcall (function &rest arguments)
114 (if (featurep 'xemacs)
115 `(funcall (symbol-function ,function) ,@arguments)
116 `(when (or (subrp ,function) (functionp ,function))
117 (with-no-warnings (funcall ,function ,@arguments)))))
118
119 ;; `set-buffer-multibyte' comes from Emacs Leim.
120 (unless (fboundp 'set-buffer-multibyte)
121 (defalias 'set-buffer-multibyte 'ignore))
122
123 ;; The following functions cannot be aliases of the corresponding
124 ;; `tramp-handle-*' functions, because this would bypass the locking
125 ;; mechanism.
126
127 ;; `process-file' does not exist in XEmacs.
128 (unless (fboundp 'process-file)
129 (defalias 'process-file
130 (lambda (program &optional infile buffer display &rest args)
131 (when (tramp-tramp-file-p default-directory)
132 (apply
133 'tramp-file-name-handler
134 'process-file program infile buffer display args)))))
135
136 ;; `start-file-process' is new in Emacs 23.
137 (unless (fboundp 'start-file-process)
138 (defalias 'start-file-process
139 (lambda (name buffer program &rest program-args)
140 (when (tramp-tramp-file-p default-directory)
141 (apply
142 'tramp-file-name-handler
143 'start-file-process name buffer program program-args)))))
144
145 ;; `set-file-times' is also new in Emacs 23.
146 (unless (fboundp 'set-file-times)
147 (defalias 'set-file-times
148 (lambda (filename &optional time)
149 (when (tramp-tramp-file-p filename)
150 (tramp-compat-funcall
151 'tramp-file-name-handler 'set-file-times filename time)))))
152
153 ;; We currently use "[" and "]" in the filename format for IPv6
154 ;; hosts of GNU Emacs. This means that Emacs wants to expand
155 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
156 ;; because no expansion could be found. We detect this situation
157 ;; and do something really awful: we have `file-expand-wildcards'
158 ;; return the original filename if it can't expand anything. Let's
159 ;; just hope that this doesn't break anything else.
160 ;; It is not needed anymore since GNU Emacs 23.2.
161 (unless (or (featurep 'xemacs)
162 ;; `featurep' has only one argument in XEmacs.
163 (funcall 'featurep 'files 'remote-wildcards))
164 (defadvice file-expand-wildcards
165 (around tramp-advice-file-expand-wildcards activate)
166 (let ((name (ad-get-arg 0)))
167 ;; If it's a Tramp file, look if wildcards need to be expanded
168 ;; at all.
169 (if (and
170 (tramp-tramp-file-p name)
171 (not (string-match
172 "[[*?]" (tramp-compat-funcall
173 'file-remote-p name 'localname))))
174 (setq ad-return-value (list name))
175 ;; Otherwise, just run the original function.
176 ad-do-it)))
177 (add-hook
178 'tramp-unload-hook
179 (lambda ()
180 (ad-remove-advice
181 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
182 (ad-activate 'file-expand-wildcards))))
183
184 ;; `redisplay' does not exist in XEmacs.
185 (unless (fboundp 'redisplay)
186 (defalias 'redisplay 'ignore)))
187
188 ;; `with-temp-message' does not exist in XEmacs.
189 (if (fboundp 'with-temp-message)
190 (defalias 'tramp-compat-with-temp-message 'with-temp-message)
191 (defmacro tramp-compat-with-temp-message (_message &rest body)
192 "Display MESSAGE temporarily if non-nil while BODY is evaluated."
193 `(progn ,@body)))
194
195 ;; `condition-case-unless-debug' is introduced with Emacs 24.
196 (if (fboundp 'condition-case-unless-debug)
197 (defalias 'tramp-compat-condition-case-unless-debug
198 'condition-case-unless-debug)
199 (defmacro tramp-compat-condition-case-unless-debug
200 (var bodyform &rest handlers)
201 "Like `condition-case' except that it does not catch anything when debugging."
202 (declare (debug condition-case) (indent 2))
203 (let ((bodysym (make-symbol "body")))
204 `(let ((,bodysym (lambda () ,bodyform)))
205 (if debug-on-error
206 (funcall ,bodysym)
207 (condition-case ,var
208 (funcall ,bodysym)
209 ,@handlers))))))
210
211 ;; `font-lock-add-keywords' does not exist in XEmacs.
212 (defun tramp-compat-font-lock-add-keywords (mode keywords &optional how)
213 "Add highlighting KEYWORDS for MODE."
214 (ignore-errors
215 (tramp-compat-funcall 'font-lock-add-keywords mode keywords how)))
216
217 (defsubst tramp-compat-temporary-file-directory ()
218 "Return name of directory for temporary files (compat function).
219 For Emacs, this is the variable `temporary-file-directory', for XEmacs
220 this is the function `temp-directory'."
221 (let (file-name-handler-alist)
222 ;; We must return a local directory. If it is remote, we could
223 ;; run into an infloop.
224 (cond
225 ((and (boundp 'temporary-file-directory)
226 (eval (car (get 'temporary-file-directory 'standard-value)))))
227 ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
228 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
229 (file-name-as-directory (getenv "TEMP")))
230 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
231 (file-name-as-directory (getenv "TMP")))
232 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
233 (file-name-as-directory (getenv "TMPDIR")))
234 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
235 (t (message (concat "Neither `temporary-file-directory' nor "
236 "`temp-directory' is defined -- using /tmp."))
237 (file-name-as-directory "/tmp")))))
238
239 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
240 ;; implementation with `make-temp-name', creating the temporary file
241 ;; immediately in order to avoid a security hole.
242 (defsubst tramp-compat-make-temp-file (f &optional dir-flag)
243 "Create a temporary file (compat function).
244 Add the extension of F, if existing."
245 (let* (file-name-handler-alist
246 (prefix (expand-file-name
247 (symbol-value 'tramp-temp-name-prefix)
248 (tramp-compat-temporary-file-directory)))
249 (extension (file-name-extension f t))
250 result)
251 (condition-case nil
252 (setq result
253 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
254 (error
255 ;; We use our own implementation, taken from files.el.
256 (while
257 (condition-case ()
258 (progn
259 (setq result (concat (make-temp-name prefix) extension))
260 (if dir-flag
261 (make-directory result)
262 (write-region "" nil result nil 'silent))
263 nil)
264 (file-already-exists t))
265 ;; The file was somehow created by someone else between
266 ;; `make-temp-name' and `write-region', let's try again.
267 nil)))
268 result))
269
270 ;; `most-positive-fixnum' does not exist in XEmacs.
271 (defsubst tramp-compat-most-positive-fixnum ()
272 "Return largest positive integer value (compat function)."
273 (cond
274 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
275 ;; Default value in XEmacs.
276 (t 134217727)))
277
278 (defun tramp-compat-decimal-to-octal (i)
279 "Return a string consisting of the octal digits of I.
280 Not actually used. Use `(format \"%o\" i)' instead?"
281 (cond ((< i 0) (error "Cannot convert negative number to octal"))
282 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
283 ((zerop i) "0")
284 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
285 (number-to-string (% i 8))))))
286
287 ;; Kudos to Gerd Moellmann for this suggestion.
288 (defun tramp-compat-octal-to-decimal (ostr)
289 "Given a string of octal digits, return a decimal number."
290 (let ((x (or ostr "")))
291 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
292 (unless (string-match "\\`[0-7]*\\'" x)
293 (error "Non-octal junk in string `%s'" x))
294 (string-to-number ostr 8)))
295
296 ;; ID-FORMAT does not exist in XEmacs.
297 (defun tramp-compat-file-attributes (filename &optional id-format)
298 "Like `file-attributes' for Tramp files (compat function)."
299 (cond
300 ((or (null id-format) (eq id-format 'integer))
301 (file-attributes filename))
302 ((tramp-tramp-file-p filename)
303 (tramp-compat-funcall
304 'tramp-file-name-handler 'file-attributes filename id-format))
305 (t (condition-case nil
306 (tramp-compat-funcall 'file-attributes filename id-format)
307 (wrong-number-of-arguments (file-attributes filename))))))
308
309 ;; PRESERVE-UID-GID does not exist in XEmacs.
310 ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with Emacs 24.1
311 ;; (as PRESERVE-SELINUX-CONTEXT), and renamed in Emacs 24.3.
312 (defun tramp-compat-copy-file
313 (filename newname &optional ok-if-already-exists keep-date
314 preserve-uid-gid preserve-extended-attributes)
315 "Like `copy-file' for Tramp files (compat function)."
316 (cond
317 (preserve-extended-attributes
318 (condition-case nil
319 (tramp-compat-funcall
320 'copy-file filename newname ok-if-already-exists keep-date
321 preserve-uid-gid preserve-extended-attributes)
322 (wrong-number-of-arguments
323 (tramp-compat-copy-file
324 filename newname ok-if-already-exists keep-date preserve-uid-gid))))
325 (preserve-uid-gid
326 (condition-case nil
327 (tramp-compat-funcall
328 'copy-file filename newname ok-if-already-exists keep-date
329 preserve-uid-gid)
330 (wrong-number-of-arguments
331 (tramp-compat-copy-file
332 filename newname ok-if-already-exists keep-date))))
333 (t
334 (copy-file filename newname ok-if-already-exists keep-date))))
335
336 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
337 ;; is taken from there.
338 (defun tramp-compat-copy-directory
339 (directory newname &optional keep-time parents copy-contents)
340 "Make a copy of DIRECTORY (compat function)."
341 (condition-case nil
342 (tramp-compat-funcall
343 'copy-directory directory newname keep-time parents copy-contents)
344
345 ;; `copy-directory' is either not implemented, or it does not
346 ;; support the the COPY-CONTENTS flag. For the time being, we
347 ;; ignore COPY-CONTENTS as well.
348
349 (error
350 ;; If `default-directory' is a remote directory, make sure we
351 ;; find its `copy-directory' handler.
352 (let ((handler (or (find-file-name-handler directory 'copy-directory)
353 (find-file-name-handler newname 'copy-directory))))
354 (if handler
355 (funcall handler 'copy-directory directory newname keep-time parents)
356
357 ;; Compute target name.
358 (setq directory (directory-file-name (expand-file-name directory))
359 newname (directory-file-name (expand-file-name newname)))
360 (if (and (file-directory-p newname)
361 (not (string-equal (file-name-nondirectory directory)
362 (file-name-nondirectory newname))))
363 (setq newname
364 (expand-file-name
365 (file-name-nondirectory directory) newname)))
366 (if (not (file-directory-p newname)) (make-directory newname parents))
367
368 ;; Copy recursively.
369 (mapc
370 (lambda (file)
371 (if (file-directory-p file)
372 (tramp-compat-copy-directory file newname keep-time parents)
373 (copy-file file newname t keep-time)))
374 ;; We do not want to delete "." and "..".
375 (directory-files
376 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
377
378 ;; Set directory attributes.
379 (set-file-modes newname (file-modes directory))
380 (if keep-time
381 (set-file-times newname (nth 5 (file-attributes directory)))))))))
382
383 ;; TRASH has been introduced with Emacs 24.1.
384 (defun tramp-compat-delete-file (filename &optional trash)
385 "Like `delete-file' for Tramp files (compat function)."
386 (condition-case nil
387 (tramp-compat-funcall 'delete-file filename trash)
388 ;; This Emacs version does not support the TRASH flag.
389 (wrong-number-of-arguments
390 (let ((delete-by-moving-to-trash
391 (and (boundp 'delete-by-moving-to-trash)
392 (symbol-value 'delete-by-moving-to-trash)
393 trash)))
394 (delete-file filename)))))
395
396 ;; RECURSIVE has been introduced with Emacs 23.2. TRASH has been
397 ;; introduced with Emacs 24.1.
398 (defun tramp-compat-delete-directory (directory &optional recursive trash)
399 "Like `delete-directory' for Tramp files (compat function)."
400 (condition-case nil
401 (cond
402 (trash
403 (tramp-compat-funcall 'delete-directory directory recursive trash))
404 (recursive
405 (tramp-compat-funcall 'delete-directory directory recursive))
406 (t
407 (delete-directory directory)))
408 ;; This Emacs version does not support the RECURSIVE or TRASH flag. We
409 ;; use the implementation from Emacs 23.2.
410 (wrong-number-of-arguments
411 (setq directory (directory-file-name (expand-file-name directory)))
412 (if (not (file-symlink-p directory))
413 (mapc (lambda (file)
414 (if (eq t (car (file-attributes file)))
415 (tramp-compat-delete-directory file recursive trash)
416 (tramp-compat-delete-file file trash)))
417 (directory-files
418 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
419 (delete-directory directory))))
420
421 ;; MUST-SUFFIX doesn't exist on XEmacs.
422 (defun tramp-compat-load (file &optional noerror nomessage nosuffix must-suffix)
423 "Like `load' for Tramp files (compat function)."
424 (if must-suffix
425 (tramp-compat-funcall 'load file noerror nomessage nosuffix must-suffix)
426 (load file noerror nomessage nosuffix)))
427
428 ;; `number-sequence' does not exist in XEmacs. Implementation is
429 ;; taken from Emacs 23.
430 (defun tramp-compat-number-sequence (from &optional to inc)
431 "Return a sequence of numbers from FROM to TO as a list (compat function)."
432 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
433 (tramp-compat-funcall 'number-sequence from to inc)
434 (if (or (not to) (= from to))
435 (list from)
436 (or inc (setq inc 1))
437 (when (zerop inc) (error "The increment can not be zero"))
438 (let (seq (n 0) (next from))
439 (if (> inc 0)
440 (while (<= next to)
441 (setq seq (cons next seq)
442 n (1+ n)
443 next (+ from (* n inc))))
444 (while (>= next to)
445 (setq seq (cons next seq)
446 n (1+ n)
447 next (+ from (* n inc)))))
448 (nreverse seq)))))
449
450 (defun tramp-compat-split-string (string pattern)
451 "Like `split-string' but omit empty strings.
452 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
453 This is, the first, empty, element is omitted. In XEmacs, the first
454 element is not omitted."
455 (delete "" (split-string string pattern)))
456
457 (defun tramp-compat-process-running-p (process-name)
458 "Returns t if system process PROCESS-NAME is running for `user-login-name'."
459 (when (stringp process-name)
460 (cond
461 ;; GNU Emacs 22 on w32.
462 ((fboundp 'w32-window-exists-p)
463 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
464
465 ;; GNU Emacs 23.
466 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
467 (let (result)
468 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
469 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
470 (when (and (string-equal
471 (cdr (assoc 'user attributes)) (user-login-name))
472 (let ((comm (cdr (assoc 'comm attributes))))
473 ;; The returned command name could be truncated
474 ;; to 15 characters. Therefore, we cannot check
475 ;; for `string-equal'.
476 (and comm (string-match
477 (concat "^" (regexp-quote comm))
478 process-name))))
479 (setq result t))))))
480
481 ;; Fallback, if there is no Lisp support yet.
482 (t (let ((default-directory
483 (if (tramp-tramp-file-p default-directory)
484 (tramp-compat-temporary-file-directory)
485 default-directory))
486 (unix95 (getenv "UNIX95"))
487 result)
488 (setenv "UNIX95" "1")
489 (when (member
490 (user-login-name)
491 (tramp-compat-split-string
492 (shell-command-to-string
493 (format "ps -C %s -o user=" process-name))
494 "[ \f\t\n\r\v]+"))
495 (setq result t))
496 (setenv "UNIX95" unix95)
497 result)))))
498
499 ;; The following functions do not exist in XEmacs. We ignore this;
500 ;; they are used for checking a remote tty.
501 (defun tramp-compat-process-get (process propname)
502 "Return the value of PROCESS' PROPNAME property.
503 This is the last value stored with `(process-put PROCESS PROPNAME VALUE)'."
504 (ignore-errors (tramp-compat-funcall 'process-get process propname)))
505
506 (defun tramp-compat-process-put (process propname value)
507 "Change PROCESS' PROPNAME property to VALUE.
508 It can be retrieved with `(process-get PROCESS PROPNAME)'."
509 (ignore-errors (tramp-compat-funcall 'process-put process propname value)))
510
511 (defun tramp-compat-set-process-query-on-exit-flag (process flag)
512 "Specify if query is needed for process when Emacs is exited.
513 If the second argument flag is non-nil, Emacs will query the user before
514 exiting if process is running."
515 (if (fboundp 'set-process-query-on-exit-flag)
516 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
517 (tramp-compat-funcall 'process-kill-without-query process flag)))
518
519 ;; There exist different implementations for this function.
520 (defun tramp-compat-coding-system-change-eol-conversion (coding-system eol-type)
521 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
522 EOL-TYPE can be one of `dos', `unix', or `mac'."
523 (cond ((fboundp 'coding-system-change-eol-conversion)
524 (tramp-compat-funcall
525 'coding-system-change-eol-conversion coding-system eol-type))
526 ((fboundp 'subsidiary-coding-system)
527 (tramp-compat-funcall
528 'subsidiary-coding-system coding-system
529 (cond ((eq eol-type 'dos) 'crlf)
530 ((eq eol-type 'unix) 'lf)
531 ((eq eol-type 'mac) 'cr)
532 (t (error
533 "Unknown EOL-TYPE `%s', must be `dos', `unix', or `mac'"
534 eol-type)))))
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 ;; `default-toplevel-value' has been declared in Emacs 24.
590 (unless (fboundp 'default-toplevel-value)
591 (defalias 'default-toplevel-value 'symbol-value))
592
593 ;; `format-message' is new in Emacs 25, and does not exist in XEmacs.
594 (unless (fboundp 'format-message)
595 (defalias 'format-message 'format))
596
597 ;; `delete-dups' does not exist in XEmacs 21.4.
598 (if (fboundp 'delete-dups)
599 (defalias 'tramp-compat-delete-dups 'delete-dups)
600 (defun tramp-compat-delete-dups (list)
601 "Destructively remove `equal' duplicates from LIST.
602 Store the result in LIST and return it. LIST must be a proper list.
603 Of several `equal' occurrences of an element in LIST, the first
604 one is kept."
605 (tramp-compat-funcall
606 'cl-delete-duplicates list '(:test equal :from-end) nil)))
607
608 (add-hook 'tramp-unload-hook
609 (lambda ()
610 (unload-feature 'tramp-loaddefs 'force)
611 (unload-feature 'tramp-compat 'force)))
612
613 (provide 'tramp-compat)
614
615 ;;; TODO:
616
617 ;;; tramp-compat.el ends here