1 ;;; tramp-compat.el --- Tramp compatibility functions
3 ;; Copyright (C) 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
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/>.
26 ;; Tramp's main Emacs version for development is GNU Emacs 24. This
27 ;; package provides compatibility functions for GNU Emacs 22, GNU
28 ;; Emacs 23 and XEmacs 21.4+.
34 (require 'tramp-loaddefs))
38 ;; Pacify byte-compiler.
45 ;; Load the appropriate timer package.
46 (if (featurep 'xemacs)
47 (require 'timer-funcs)
50 ;; We check whether `start-file-process' is bound.
51 (unless (fboundp 'start-file-process)
53 ;; tramp-util offers integration into other (X)Emacs packages like
54 ;; compile.el, gud.el etc. Not necessary in Emacs 23.
55 (eval-after-load "tramp"
56 '(require 'tramp-util))
58 ;; Make sure that we get integration with the VC package. When it
59 ;; is loaded, we need to pull in the integration module. Not
60 ;; necessary in Emacs 23.
62 (eval-after-load "tramp"
63 '(require 'tramp-vc))))
65 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
66 ;; Currently, XEmacs supports this.
67 (when (featurep 'xemacs)
68 (unless (boundp 'byte-compile-default-warnings)
69 (defvar byte-compile-default-warnings nil))
70 (delq 'unused-vars byte-compile-default-warnings))
72 ;; `last-coding-system-used' is unknown in XEmacs.
73 (unless (boundp 'last-coding-system-used)
74 (defvar last-coding-system-used nil))
76 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
77 ;; used in XEmacs, so we set it here and there. The following is
78 ;; needed to pacify Emacs byte-compiler.
79 (unless (boundp 'byte-compile-not-obsolete-var)
80 (defvar byte-compile-not-obsolete-var nil))
81 (setq byte-compile-not-obsolete-var 'directory-sep-char)
83 (unless (boundp 'byte-compile-not-obsolete-vars)
84 (defvar byte-compile-not-obsolete-vars nil))
85 (setq byte-compile-not-obsolete-vars '(directory-sep-char))
87 ;; `with-temp-message' does not exists in XEmacs.
89 (with-temp-message (current-message) nil)
90 (error (defmacro with-temp-message (message &rest body) `(progn ,@body))))
92 ;; For not existing functions, or functions with a changed argument
93 ;; list, there are compiler warnings. We want to avoid them in
94 ;; cases we know what we do.
95 (defmacro tramp-compat-funcall (function &rest arguments)
96 (if (featurep 'xemacs)
97 `(funcall (symbol-function ,function) ,@arguments)
98 `(when (or (subrp ,function) (functionp ,function))
99 (with-no-warnings (funcall ,function ,@arguments)))))
101 ;; `set-buffer-multibyte' comes from Emacs Leim.
102 (unless (fboundp 'set-buffer-multibyte)
103 (defalias 'set-buffer-multibyte 'ignore))
105 ;; `font-lock-add-keywords' does not exist in XEmacs.
106 (unless (fboundp 'font-lock-add-keywords)
107 (defalias 'font-lock-add-keywords 'ignore))
109 ;; The following functions cannot be aliases of the corresponding
110 ;; `tramp-handle-*' functions, because this would bypass the locking
113 ;; `file-remote-p' has been introduced with Emacs 22. The version
114 ;; of XEmacs is not a magic file name function (yet); this is
115 ;; corrected in tramp-util.el. Here it is sufficient if the
117 (unless (fboundp 'file-remote-p)
118 (defalias 'file-remote-p
119 (lambda (file &optional identification connected)
120 (when (tramp-tramp-file-p file)
121 (tramp-file-name-handler
122 'file-remote-p file identification connected)))))
124 ;; `process-file' does not exist in XEmacs.
125 (unless (fboundp 'process-file)
126 (defalias 'process-file
127 (lambda (program &optional infile buffer display &rest args)
128 (when (tramp-tramp-file-p default-directory)
130 'tramp-file-name-handler
131 'process-file program infile buffer display args)))))
133 ;; `start-file-process' is new in Emacs 23.
134 (unless (fboundp 'start-file-process)
135 (defalias 'start-file-process
136 (lambda (name buffer program &rest program-args)
137 (when (tramp-tramp-file-p default-directory)
139 'tramp-file-name-handler
140 'start-file-process name buffer program program-args)))))
142 ;; `set-file-times' is also new in Emacs 23.
143 (unless (fboundp 'set-file-times)
144 (defalias 'set-file-times
145 (lambda (filename &optional time)
146 (when (tramp-tramp-file-p filename)
147 (tramp-file-name-handler
148 'set-file-times filename time)))))
150 ;; We currently use "[" and "]" in the filename format for IPv6
151 ;; hosts of GNU Emacs. This means, that Emacs wants to expand
152 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
153 ;; because no expansion could be found. We detect this situation
154 ;; and do something really awful: we have `file-expand-wildcards'
155 ;; return the original filename if it can't expand anything. Let's
156 ;; just hope that this doesn't break anything else.
157 ;; It is not needed anymore since GNU Emacs 23.2.
158 (unless (or (featurep 'xemacs)
159 ;; `featurep' has only one argument in XEmacs.
160 (funcall 'featurep 'files 'remote-wildcards))
161 (defadvice file-expand-wildcards
162 (around tramp-advice-file-expand-wildcards activate)
163 (let ((name (ad-get-arg 0)))
164 ;; If it's a Tramp file, look if wildcards need to be expanded
167 (tramp-tramp-file-p name)
169 "[[*?]" (tramp-compat-funcall
170 'file-remote-p name 'localname))))
171 (setq ad-return-value (list name))
172 ;; Otherwise, just run the original function.
178 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
179 (ad-activate 'file-expand-wildcards)))))
181 (defsubst tramp-compat-line-beginning-position ()
182 "Return point at beginning of line (compat function).
183 Calls `line-beginning-position' or `point-at-bol' if defined, else
186 ((fboundp 'line-beginning-position)
187 (tramp-compat-funcall 'line-beginning-position))
188 ((fboundp 'point-at-bol) (tramp-compat-funcall 'point-at-bol))
189 (t (save-excursion (beginning-of-line) (point)))))
191 (defsubst tramp-compat-line-end-position ()
192 "Return point at end of line (compat function).
193 Calls `line-end-position' or `point-at-eol' if defined, else
196 ((fboundp 'line-end-position) (tramp-compat-funcall 'line-end-position))
197 ((fboundp 'point-at-eol) (tramp-compat-funcall 'point-at-eol))
198 (t (save-excursion (end-of-line) (point)))))
200 (defsubst tramp-compat-temporary-file-directory ()
201 "Return name of directory for temporary files (compat function).
202 For Emacs, this is the variable `temporary-file-directory', for XEmacs
203 this is the function `temp-directory'."
205 ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
206 ((fboundp 'temp-directory) (tramp-compat-funcall 'temp-directory))
207 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
208 (file-name-as-directory (getenv "TEMP")))
209 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
210 (file-name-as-directory (getenv "TMP")))
211 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
212 (file-name-as-directory (getenv "TMPDIR")))
213 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
214 (t (message (concat "Neither `temporary-file-directory' nor "
215 "`temp-directory' is defined -- using /tmp."))
216 (file-name-as-directory "/tmp"))))
218 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
219 ;; implementation with `make-temp-name', creating the temporary file
220 ;; immediately in order to avoid a security hole.
221 (defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
222 "Create a temporary file (compat function).
223 Add the extension of FILENAME, if existing."
224 (let* (file-name-handler-alist
225 (prefix (expand-file-name
226 (symbol-value 'tramp-temp-name-prefix)
227 (tramp-compat-temporary-file-directory)))
228 (extension (file-name-extension filename t))
232 (tramp-compat-funcall 'make-temp-file prefix dir-flag extension))
234 ;; We use our own implementation, taken from files.el.
238 (setq result (concat (make-temp-name prefix) extension))
240 (make-directory result)
241 (write-region "" nil result nil 'silent))
243 (file-already-exists t))
244 ;; The file was somehow created by someone else between
245 ;; `make-temp-name' and `write-region', let's try again.
249 ;; `most-positive-fixnum' does not exist in XEmacs.
250 (defsubst tramp-compat-most-positive-fixnum ()
251 "Return largest positive integer value (compat function)."
253 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
254 ;; Default value in XEmacs.
257 (defun tramp-compat-decimal-to-octal (i)
258 "Return a string consisting of the octal digits of I.
259 Not actually used. Use `(format \"%o\" i)' instead?"
260 (cond ((< i 0) (error "Cannot convert negative number to octal"))
261 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
263 (t (concat (tramp-compat-decimal-to-octal (/ i 8))
264 (number-to-string (% i 8))))))
266 ;; Kudos to Gerd Moellmann for this suggestion.
267 (defun tramp-compat-octal-to-decimal (ostr)
268 "Given a string of octal digits, return a decimal number."
269 (let ((x (or ostr "")))
270 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
271 (unless (string-match "\\`[0-7]*\\'" x)
272 (error "Non-octal junk in string `%s'" x))
273 (string-to-number ostr 8)))
275 ;; ID-FORMAT does not exists in XEmacs.
276 (defun tramp-compat-file-attributes (filename &optional id-format)
277 "Like `file-attributes' for Tramp files (compat function)."
279 ((or (null id-format) (eq id-format 'integer))
280 (file-attributes filename))
281 ((tramp-tramp-file-p filename)
282 (tramp-file-name-handler 'file-attributes filename id-format))
283 (t (condition-case nil
284 (tramp-compat-funcall 'file-attributes filename id-format)
285 (wrong-number-of-arguments (file-attributes filename))))))
287 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
288 ;; hurt to ignore it for other (X)Emacs versions.
289 ;; PRESERVE-SELINUX-CONTEXT has been introduced with Emacs 24.
290 (defun tramp-compat-copy-file
291 (filename newname &optional ok-if-already-exists keep-date
292 preserve-uid-gid preserve-selinux-context)
293 "Like `copy-file' for Tramp files (compat function)."
295 (preserve-selinux-context
296 (tramp-compat-funcall
297 'copy-file filename newname ok-if-already-exists keep-date
298 preserve-uid-gid preserve-selinux-context))
300 (tramp-compat-funcall
301 'copy-file filename newname ok-if-already-exists keep-date
304 (copy-file filename newname ok-if-already-exists keep-date))))
306 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
307 ;; is taken from there.
308 (defun tramp-compat-copy-directory
309 (directory newname &optional keep-time parents)
310 "Make a copy of DIRECTORY (compat function)."
311 (if (fboundp 'copy-directory)
312 (tramp-compat-funcall 'copy-directory directory newname keep-time parents)
314 ;; If `default-directory' is a remote directory, make sure we find
315 ;; its `copy-directory' handler.
316 (let ((handler (or (find-file-name-handler directory 'copy-directory)
317 (find-file-name-handler newname 'copy-directory))))
319 (funcall handler 'copy-directory directory newname keep-time parents)
321 ;; Compute target name.
322 (setq directory (directory-file-name (expand-file-name directory))
323 newname (directory-file-name (expand-file-name newname)))
324 (if (and (file-directory-p newname)
325 (not (string-equal (file-name-nondirectory directory)
326 (file-name-nondirectory newname))))
329 (file-name-nondirectory directory) newname)))
330 (if (not (file-directory-p newname)) (make-directory newname parents))
335 (if (file-directory-p file)
336 (tramp-compat-copy-directory file newname keep-time parents)
337 (copy-file file newname t keep-time)))
338 ;; We do not want to delete "." and "..".
340 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
342 ;; Set directory attributes.
343 (set-file-modes newname (file-modes directory))
345 (set-file-times newname (nth 5 (file-attributes directory))))))))
347 ;; TRASH has been introduced with Emacs 24.1.
348 (defun tramp-compat-delete-file (filename &optional trash)
349 "Like `delete-file' for Tramp files (compat function)."
351 (tramp-compat-funcall 'delete-file filename trash)
352 ;; This Emacs version does not support the TRASH flag.
353 (wrong-number-of-arguments
354 (let ((delete-by-moving-to-trash
355 (and (boundp 'delete-by-moving-to-trash)
356 (symbol-value 'delete-by-moving-to-trash)
358 (delete-file filename)))))
360 ;; RECURSIVE has been introduced with Emacs 23.2.
361 (defun tramp-compat-delete-directory (directory &optional recursive)
362 "Like `delete-directory' for Tramp files (compat function)."
364 (delete-directory directory)
366 (tramp-compat-funcall 'delete-directory directory recursive)
367 ;; This Emacs version does not support the RECURSIVE flag. We
368 ;; use the implementation from Emacs 23.2.
369 (wrong-number-of-arguments
370 (setq directory (directory-file-name (expand-file-name directory)))
371 (if (not (file-symlink-p directory))
373 (if (eq t (car (file-attributes file)))
374 (tramp-compat-delete-directory file recursive)
377 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
378 (delete-directory directory)))))
380 ;; `number-sequence' does not exist in XEmacs. Implementation is
381 ;; taken from Emacs 23.
382 (defun tramp-compat-number-sequence (from &optional to inc)
383 "Return a sequence of numbers from FROM to TO as a list (compat function)."
384 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
385 (tramp-compat-funcall 'number-sequence from to inc)
386 (if (or (not to) (= from to))
388 (or inc (setq inc 1))
389 (when (zerop inc) (error "The increment can not be zero"))
390 (let (seq (n 0) (next from))
393 (setq seq (cons next seq)
395 next (+ from (* n inc))))
397 (setq seq (cons next seq)
399 next (+ from (* n inc)))))
402 (defun tramp-compat-split-string (string pattern)
403 "Like `split-string' but omit empty strings.
404 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
405 This is, the first, empty, element is omitted. In XEmacs, the first
406 element is not omitted."
407 (delete "" (split-string string pattern)))
409 (defun tramp-compat-call-process
410 (program &optional infile destination display &rest args)
411 "Calls `call-process' on the local host.
412 This is needed because for some Emacs flavors Tramp has
413 defadviced `call-process' to behave like `process-file'. The
414 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
415 (let ((default-directory
416 (if (file-remote-p default-directory)
417 (tramp-compat-temporary-file-directory)
419 (if (executable-find program)
420 (apply 'call-process program infile destination display args)
423 (defun tramp-compat-process-running-p (process-name)
424 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
425 (when (stringp process-name)
427 ;; GNU Emacs 22 on w32.
428 ((fboundp 'w32-window-exists-p)
429 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
432 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
434 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
435 (let ((attributes (tramp-compat-funcall 'process-attributes pid)))
436 (when (and (string-equal
437 (cdr (assoc 'user attributes)) (user-login-name))
438 (let ((comm (cdr (assoc 'comm attributes))))
439 ;; The returned command name could be truncated
440 ;; to 15 characters. Therefore, we cannot check
441 ;; for `string-equal'.
442 (and comm (string-match
443 (concat "^" (regexp-quote comm))
447 ;; Fallback, if there is no Lisp support yet.
448 (t (let ((default-directory
449 (if (file-remote-p default-directory)
450 (tramp-compat-temporary-file-directory)
452 (unix95 (getenv "UNIX95"))
454 (setenv "UNIX95" "1")
457 (tramp-compat-split-string
458 (shell-command-to-string
459 (format "ps -C %s -o user=" process-name))
462 (setenv "UNIX95" unix95)
465 (add-hook 'tramp-unload-hook
467 (unload-feature 'tramp-compat 'force)))
469 (provide 'tramp-compat)
473 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
474 ;;; tramp-compat.el ends here