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
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;; Tramp's main Emacs version for development is GNU Emacs 24. This
26 ;; package provides compatibility functions for GNU Emacs 22, GNU
27 ;; Emacs 23 and XEmacs 21.4+.
33 ;; Pacify byte-compiler.
40 ;; Load the appropriate timer package.
41 (if (featurep 'xemacs)
42 (require 'timer-funcs)
45 (autoload 'tramp-tramp-file-p "tramp")
46 (autoload 'tramp-file-name-handler "tramp")
47 (autoload 'tramp-handle-file-remote-p "tramp")
49 ;; tramp-util offers integration into other (X)Emacs packages like
50 ;; compile.el, gud.el etc. Not necessary in Emacs 23.
51 (eval-after-load "tramp"
52 ;; We check whether `start-file-process' is an alias.
53 '(when (or (not (fboundp 'start-file-process))
54 (symbolp (symbol-function 'start-file-process)))
56 (add-hook 'tramp-unload-hook
58 (when (featurep 'tramp-util)
59 (unload-feature 'tramp-util 'force))))))
61 ;; Make sure that we get integration with the VC package. When it
62 ;; is loaded, we need to pull in the integration module. Not
63 ;; necessary in Emacs 23.
65 (eval-after-load "tramp"
66 ;; We check whether `start-file-process' is an alias.
67 '(when (or (not (fboundp 'start-file-process))
68 (symbolp (symbol-function 'start-file-process)))
70 (add-hook 'tramp-unload-hook
72 (when (featurep 'tramp-vc)
73 (unload-feature 'tramp-vc 'force)))))))
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))
82 ;; `last-coding-system-used' is unknown in XEmacs.
83 (unless (boundp 'last-coding-system-used)
84 (defvar last-coding-system-used nil))
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 (unless (boundp 'byte-compile-not-obsolete-var)
90 (defvar byte-compile-not-obsolete-var nil))
91 (setq byte-compile-not-obsolete-var 'directory-sep-char)
92 (if (boundp 'byte-compile-not-obsolete-vars) ; Emacs 23.2
93 (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
95 ;; `with-temp-message' does not exists in XEmacs.
97 (with-temp-message (current-message) nil)
98 (error (defmacro with-temp-message (message &rest body) `(progn ,@body))))
100 ;; `set-buffer-multibyte' comes from Emacs Leim.
101 (unless (fboundp 'set-buffer-multibyte)
102 (defalias 'set-buffer-multibyte 'ignore))
104 ;; `font-lock-add-keywords' does not exist in XEmacs.
105 (unless (fboundp 'font-lock-add-keywords)
106 (defalias 'font-lock-add-keywords 'ignore))
108 ;; The following functions cannot be aliases of the corresponding
109 ;; `tramp-handle-*' functions, because this would bypass the locking
112 ;; `file-remote-p' has been introduced with Emacs 22. The version
113 ;; of XEmacs is not a magic file name function (yet); this is
114 ;; corrected in tramp-util.el. Here it is sufficient if the
116 (unless (fboundp 'file-remote-p)
117 (defalias 'file-remote-p
118 (lambda (file &optional identification connected)
119 (when (tramp-tramp-file-p file)
120 (tramp-file-name-handler
121 'file-remote-p file identification connected)))))
123 ;; `process-file' does not exist in XEmacs.
124 (unless (fboundp 'process-file)
125 (defalias 'process-file
126 (lambda (program &optional infile buffer display &rest args)
127 (when (tramp-tramp-file-p default-directory)
129 'tramp-file-name-handler
130 'process-file program infile buffer display args)))))
132 ;; `start-file-process' is new in Emacs 23.
133 (unless (fboundp 'start-file-process)
134 (defalias 'start-file-process
135 (lambda (name buffer program &rest program-args)
136 (when (tramp-tramp-file-p default-directory)
138 'tramp-file-name-handler
139 'start-file-process name buffer program program-args)))))
141 ;; `set-file-times' is also new in Emacs 23.
142 (unless (fboundp 'set-file-times)
143 (defalias 'set-file-times
144 (lambda (filename &optional time)
145 (when (tramp-tramp-file-p filename)
146 (tramp-file-name-handler
147 'set-file-times filename time)))))
149 ;; We currently use "[" and "]" in the filename format for IPv6
150 ;; hosts of GNU Emacs. This means, that Emacs wants to expand
151 ;; wildcards if `find-file-wildcards' is non-nil, and then barfs
152 ;; because no expansion could be found. We detect this situation
153 ;; and do something really awful: we have `file-expand-wildcards'
154 ;; return the original filename if it can't expand anything. Let's
155 ;; just hope that this doesn't break anything else.
156 ;; It is not needed anymore since GNU Emacs 23.2.
157 (unless (or (featurep 'xemacs)
158 ;; `featurep' has only one argument in XEmacs.
159 (funcall 'featurep 'files 'remote-wildcards))
160 (defadvice file-expand-wildcards
161 (around tramp-advice-file-expand-wildcards activate)
162 (let ((name (ad-get-arg 0)))
163 ;; If it's a Tramp file, look if wildcards need to be expanded
166 (tramp-tramp-file-p name)
168 "[[*?]" (tramp-handle-file-remote-p name 'localname))))
169 (setq ad-return-value (list name))
170 ;; Otherwise, just run the original function.
176 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
177 (ad-activate 'file-expand-wildcards)))))
179 (defsubst tramp-compat-line-beginning-position ()
180 "Return point at beginning of line (compat function).
181 Calls `line-beginning-position' or `point-at-bol' if defined, else
184 ((fboundp 'line-beginning-position)
185 (funcall (symbol-function 'line-beginning-position)))
186 ((fboundp 'point-at-bol) (funcall (symbol-function 'point-at-bol)))
187 (t (save-excursion (beginning-of-line) (point)))))
189 (defsubst tramp-compat-line-end-position ()
190 "Return point at end of line (compat function).
191 Calls `line-end-position' or `point-at-eol' if defined, else
194 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
195 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
196 (t (save-excursion (end-of-line) (point)))))
198 (defsubst tramp-compat-temporary-file-directory ()
199 "Return name of directory for temporary files (compat function).
200 For Emacs, this is the variable `temporary-file-directory', for XEmacs
201 this is the function `temp-directory'."
203 ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
204 ((fboundp 'temp-directory) (funcall (symbol-function 'temp-directory)))
205 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
206 (file-name-as-directory (getenv "TEMP")))
207 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
208 (file-name-as-directory (getenv "TMP")))
209 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
210 (file-name-as-directory (getenv "TMPDIR")))
211 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
212 (t (message (concat "Neither `temporary-file-directory' nor "
213 "`temp-directory' is defined -- using /tmp."))
214 (file-name-as-directory "/tmp"))))
216 ;; `make-temp-file' exists in Emacs only. On XEmacs, we use our own
217 ;; implementation with `make-temp-name', creating the temporary file
218 ;; immediately in order to avoid a security hole.
219 (defsubst tramp-compat-make-temp-file (filename &optional dir-flag)
220 "Create a temporary file (compat function).
221 Add the extension of FILENAME, if existing."
222 (let* (file-name-handler-alist
223 (prefix (expand-file-name
224 (symbol-value 'tramp-temp-name-prefix)
225 (tramp-compat-temporary-file-directory)))
226 (extension (file-name-extension filename t))
228 (if (fboundp 'make-temp-file)
231 (symbol-function 'make-temp-file) prefix dir-flag extension))
232 ;; We use our own implementation, taken from files.el.
236 (setq result (concat (make-temp-name prefix) extension))
238 (make-directory result)
239 (write-region "" nil result nil 'silent))
241 (file-already-exists t))
242 ;; The file was somehow created by someone else between
243 ;; `make-temp-name' and `write-region', let's try again.
247 ;; `most-positive-fixnum' does not exist in XEmacs.
248 (defsubst tramp-compat-most-positive-fixnum ()
249 "Return largest positive integer value (compat function)."
251 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
252 ;; Default value in XEmacs.
255 ;; ID-FORMAT does not exists in XEmacs.
256 (defun tramp-compat-file-attributes (filename &optional id-format)
257 "Like `file-attributes' for Tramp files (compat function)."
259 ((or (null id-format) (eq id-format 'integer))
260 (file-attributes filename))
261 ((tramp-tramp-file-p filename)
262 (tramp-file-name-handler 'file-attributes filename id-format))
263 (t (condition-case nil
264 (funcall (symbol-function 'file-attributes) filename id-format)
265 (error (file-attributes filename))))))
267 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
268 ;; hurt to ignore it for other (X)Emacs versions.
269 (defun tramp-compat-copy-file
270 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
271 "Like `copy-file' for Tramp files (compat function)."
274 (symbol-function 'copy-file)
275 filename newname ok-if-already-exists keep-date preserve-uid-gid)
276 (copy-file filename newname ok-if-already-exists keep-date)))
278 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
279 ;; is taken from there.
280 (defun tramp-compat-copy-directory
281 (directory newname &optional keep-time parents)
282 "Make a copy of DIRECTORY (compat function)."
283 (if (fboundp 'copy-directory)
285 (symbol-function 'copy-directory) directory newname keep-time parents)
287 ;; If `default-directory' is a remote directory, make sure we find
288 ;; its `copy-directory' handler.
289 (let ((handler (or (find-file-name-handler directory 'copy-directory)
290 (find-file-name-handler newname 'copy-directory))))
292 (funcall handler 'copy-directory directory newname keep-time parents)
294 ;; Compute target name.
295 (setq directory (directory-file-name (expand-file-name directory))
296 newname (directory-file-name (expand-file-name newname)))
297 (if (and (file-directory-p newname)
298 (not (string-equal (file-name-nondirectory directory)
299 (file-name-nondirectory newname))))
302 (file-name-nondirectory directory) newname)))
303 (if (not (file-directory-p newname)) (make-directory newname parents))
308 (if (file-directory-p file)
309 (tramp-compat-copy-directory file newname keep-time parents)
310 (copy-file file newname t keep-time)))
311 ;; We do not want to delete "." and "..".
313 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
315 ;; Set directory attributes.
316 (set-file-modes newname (file-modes directory))
318 (set-file-times newname (nth 5 (file-attributes directory))))))))
320 ;; RECURSIVE has been introduced with Emacs 23.2.
321 (defun tramp-compat-delete-directory (directory &optional recursive)
322 "Like `delete-directory' for Tramp files (compat function)."
324 (delete-directory directory)
326 (funcall (symbol-function 'delete-directory) directory recursive)
327 ;; This Emacs version does not support the RECURSIVE flag. We
328 ;; use the implementation from Emacs 23.2.
330 (setq directory (directory-file-name (expand-file-name directory)))
331 (if (not (file-symlink-p directory))
333 (if (eq t (car (file-attributes file)))
334 (tramp-compat-delete-directory file recursive)
337 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*")))
338 (delete-directory directory)))))
340 ;; `number-sequence' does not exist in XEmacs. Implementation is
341 ;; taken from Emacs 23.
342 (defun tramp-compat-number-sequence (from &optional to inc)
343 "Return a sequence of numbers from FROM to TO as a list (compat function)."
344 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
345 (funcall (symbol-function 'number-sequence) from to inc)
346 (if (or (not to) (= from to))
348 (or inc (setq inc 1))
349 (when (zerop inc) (error "The increment can not be zero"))
350 (let (seq (n 0) (next from))
353 (setq seq (cons next seq)
355 next (+ from (* n inc))))
357 (setq seq (cons next seq)
359 next (+ from (* n inc)))))
362 (defun tramp-compat-split-string (string pattern)
363 "Like `split-string' but omit empty strings.
364 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
365 This is, the first, empty, element is omitted. In XEmacs, the first
366 element is not omitted."
367 (delete "" (split-string string pattern)))
369 (defun tramp-compat-process-running-p (process-name)
370 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
371 (when (stringp process-name)
373 ;; GNU Emacs 22 on w32.
374 ((fboundp 'w32-window-exists-p)
375 (funcall (symbol-function 'w32-window-exists-p)
376 process-name process-name))
379 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
381 (dolist (pid (funcall (symbol-function 'list-system-processes)) result)
383 (funcall (symbol-function 'process-attributes) pid)))
384 (when (and (string-equal
385 (cdr (assoc 'user attributes)) (user-login-name))
386 (let ((comm (cdr (assoc 'comm attributes))))
387 ;; The returned command name could be truncated
388 ;; to 15 characters. Therefore, we cannot check
389 ;; for `string-equal'.
390 (and comm (string-match
391 (concat "^" (regexp-quote comm))
395 ;; Fallback, if there is no Lisp support yet.
396 (t (let ((default-directory
397 (if (file-remote-p default-directory)
398 (tramp-compat-temporary-file-directory)
400 (unix95 (getenv "UNIX95"))
402 (setenv "UNIX95" "1")
405 (tramp-compat-split-string
406 (shell-command-to-string
407 (format "ps -C %s -o user=" process-name))
410 (setenv "UNIX95" unix95)
413 (provide 'tramp-compat)
417 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
418 ;;; tramp-compat.el ends here