]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-compat.el
* files.el (file-expand-wildcards): In case of remote files, check
[gnu-emacs] / lisp / net / tramp-compat.el
1 ;;; tramp-compat.el --- Tramp compatibility functions
2
3 ;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;; Tramp's main Emacs version for development is GNU Emacs 23. This
26 ;; package provides compatibility functions for GNU Emacs 21, GNU
27 ;; Emacs 22 and XEmacs 21.4+.
28
29 ;;; Code:
30
31 (eval-when-compile
32
33 ;; Pacify byte-compiler.
34 (require 'cl))
35
36 (eval-and-compile
37
38 (require 'custom)
39
40 ;; Load the appropriate timer package.
41 (if (featurep 'xemacs)
42 (require 'timer-funcs)
43 (require 'timer))
44
45 (autoload 'tramp-tramp-file-p "tramp")
46 (autoload 'tramp-file-name-handler "tramp")
47 (autoload 'tramp-handle-file-remote-p "tramp")
48
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)))
55 (require 'tramp-util)
56 (add-hook 'tramp-unload-hook
57 '(lambda ()
58 (when (featurep 'tramp-util)
59 (unload-feature 'tramp-util 'force))))))
60
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.
64 (eval-after-load "vc"
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)))
69 (require 'tramp-vc)
70 (add-hook 'tramp-unload-hook
71 '(lambda ()
72 (when (featurep 'tramp-vc)
73 (unload-feature 'tramp-vc 'force)))))))
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 (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)))
94
95 ;; `with-temp-message' does not exists in XEmacs.
96 (condition-case nil
97 (with-temp-message (current-message) nil)
98 (error (defmacro with-temp-message (message &rest body) `(progn ,@body))))
99
100 ;; `set-buffer-multibyte' comes from Emacs Leim.
101 (unless (fboundp 'set-buffer-multibyte)
102 (defalias 'set-buffer-multibyte 'ignore))
103
104 ;; `font-lock-add-keywords' does not exist in XEmacs.
105 (unless (fboundp 'font-lock-add-keywords)
106 (defalias 'font-lock-add-keywords 'ignore))
107
108 ;; The following functions cannot be aliases of the corresponding
109 ;; `tramp-handle-*' functions, because this would bypass the locking
110 ;; mechanism.
111
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
115 ;; function exists.
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)))))
122
123 ;; `process-file' exists since Emacs 22.
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)
128 (apply
129 'tramp-file-name-handler
130 'process-file program infile buffer display args)))))
131
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)
137 (apply
138 'tramp-file-name-handler
139 'start-file-process name buffer program program-args)))))
140
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)))))
148
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) (featurep 'files 'remote-wildcards))
158 (defadvice file-expand-wildcards
159 (around tramp-advice-file-expand-wildcards activate)
160 (let ((name (ad-get-arg 0)))
161 ;; If it's a Tramp file, look if wildcards need to be expanded
162 ;; at all.
163 (if (and
164 (tramp-tramp-file-p name)
165 (not (string-match
166 "[[*?]" (tramp-handle-file-remote-p name 'localname))))
167 (setq ad-return-value (list name))
168 ;; Otherwise, just run the original function.
169 ad-do-it)))
170 (add-hook
171 'tramp-unload-hook
172 (lambda ()
173 (ad-remove-advice
174 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
175 (ad-activate 'file-expand-wildcards)))))
176
177 (defsubst tramp-compat-line-beginning-position ()
178 "Return point at beginning of line (compat function).
179 Calls `line-beginning-position' or `point-at-bol' if defined, else
180 own implementation."
181 (cond
182 ((fboundp 'line-beginning-position)
183 (funcall (symbol-function 'line-beginning-position)))
184 ((fboundp 'point-at-bol) (funcall (symbol-function 'point-at-bol)))
185 (t (save-excursion (beginning-of-line) (point)))))
186
187 (defsubst tramp-compat-line-end-position ()
188 "Return point at end of line (compat function).
189 Calls `line-end-position' or `point-at-eol' if defined, else
190 own implementation."
191 (cond
192 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
193 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
194 (t (save-excursion (end-of-line) (point)))))
195
196 (defsubst tramp-compat-temporary-file-directory ()
197 "Return name of directory for temporary files (compat function).
198 For Emacs, this is the variable `temporary-file-directory', for XEmacs
199 this is the function `temp-directory'."
200 (cond
201 ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
202 ((fboundp 'temp-directory) (funcall (symbol-function 'temp-directory)))
203 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
204 (file-name-as-directory (getenv "TEMP")))
205 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
206 (file-name-as-directory (getenv "TMP")))
207 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
208 (file-name-as-directory (getenv "TMPDIR")))
209 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
210 (t (message (concat "Neither `temporary-file-directory' nor "
211 "`temp-directory' is defined -- using /tmp."))
212 (file-name-as-directory "/tmp"))))
213
214 ;; `make-temp-file' exists in Emacs only. The third parameter SUFFIX
215 ;; has been introduced with Emacs 22. We try it, if it fails, we fall
216 ;; back to `make-temp-name', creating the temporary file immediately
217 ;; in order to avoid a security hole.
218 (defsubst tramp-compat-make-temp-file (filename)
219 "Create a temporary file (compat function).
220 Add the extension of FILENAME, if existing."
221 (let* (file-name-handler-alist
222 (prefix (expand-file-name
223 (symbol-value 'tramp-temp-name-prefix)
224 (tramp-compat-temporary-file-directory)))
225 (extension (file-name-extension filename t))
226 result)
227 (condition-case nil
228 (setq result
229 (funcall (symbol-function 'make-temp-file) prefix nil extension))
230 (error
231 ;; We use our own implementation, taken from files.el.
232 (while
233 (condition-case ()
234 (progn
235 (setq result (concat (make-temp-name prefix) extension))
236 (write-region
237 "" nil result nil 'silent nil
238 ;; 7th parameter is MUSTBENEW in Emacs, and
239 ;; CODING-SYSTEM in XEmacs. It is not a security
240 ;; hole in XEmacs if we cannot use this parameter,
241 ;; because XEmacs uses a user-specific subdirectory
242 ;; with 0700 permissions.
243 (when (not (featurep 'xemacs)) 'excl))
244 nil)
245 (file-already-exists t))
246 ;; The file was somehow created by someone else between
247 ;; `make-temp-name' and `write-region', let's try again.
248 nil)))
249 result))
250
251 ;; `most-positive-fixnum' arrived in Emacs 22. Before, and in XEmacs,
252 ;; it is a fixed value.
253 (defsubst tramp-compat-most-positive-fixnum ()
254 "Return largest positive integer value (compat function)."
255 (cond
256 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
257 ;; Default value in XEmacs and Emacs 21.
258 (t 134217727)))
259
260 ;; ID-FORMAT exists since Emacs 22.
261 (defun tramp-compat-file-attributes (filename &optional id-format)
262 "Like `file-attributes' for Tramp files (compat function)."
263 (cond
264 ((or (null id-format) (eq id-format 'integer))
265 (file-attributes filename))
266 ((tramp-tramp-file-p filename)
267 (tramp-file-name-handler 'file-attributes filename id-format))
268 (t (condition-case nil
269 (funcall (symbol-function 'file-attributes) filename id-format)
270 (error (file-attributes filename))))))
271
272 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
273 ;; hurt to ignore it for other (X)Emacs versions.
274 (defun tramp-compat-copy-file
275 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
276 "Like `copy-file' for Tramp files (compat function)."
277 (if preserve-uid-gid
278 (funcall
279 (symbol-function 'copy-file)
280 filename newname ok-if-already-exists keep-date preserve-uid-gid)
281 (copy-file filename newname ok-if-already-exists keep-date)))
282
283 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
284 ;; is taken from there.
285 (defun tramp-compat-copy-directory
286 (directory newname &optional keep-time parents)
287 "Make a copy of DIRECTORY (compat function)."
288 (if (fboundp 'copy-directory)
289 (funcall
290 (symbol-function 'copy-directory) directory newname keep-time parents)
291
292 ;; If default-directory is a remote directory, make sure we find
293 ;; its copy-directory handler.
294 (let ((handler (or (find-file-name-handler directory 'copy-directory)
295 (find-file-name-handler newname 'copy-directory))))
296 (if handler
297 (funcall handler 'copy-directory directory newname keep-time parents)
298
299 ;; Compute target name.
300 (setq directory (directory-file-name (expand-file-name directory))
301 newname (directory-file-name (expand-file-name newname)))
302 (if (and (file-directory-p newname)
303 (not (string-equal (file-name-nondirectory directory)
304 (file-name-nondirectory newname))))
305 (setq newname
306 (expand-file-name
307 (file-name-nondirectory directory) newname)))
308 (if (not (file-directory-p newname)) (make-directory newname parents))
309
310 ;; Copy recursively.
311 (mapc
312 (lambda (file)
313 (if (file-directory-p file)
314 (tramp-compat-copy-directory file newname keep-time parents)
315 (copy-file file newname t keep-time)))
316 ;; We do not want to delete "." and "..".
317 (directory-files
318 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
319
320 ;; Set directory attributes.
321 (set-file-modes newname (file-modes directory))
322 (if keep-time
323 (set-file-times newname (nth 5 (file-attributes directory))))))))
324
325 ;; `copy-tree' is a built-in function in XEmacs. In Emacs 21, it is
326 ;; an autoloaded function in cl-extra.el. Since Emacs 22, it is part
327 ;; of subr.el. There are problems when autoloading, therefore we test
328 ;; for `subrp' and `symbol-file'. Implementation is taken from Emacs 23.
329 (defun tramp-compat-copy-tree (tree)
330 "Make a copy of TREE (compat function)."
331 (if (or (subrp 'copy-tree) (symbol-file 'copy-tree))
332 (funcall (symbol-function 'copy-tree) tree)
333 (let (result)
334 (while (consp tree)
335 (let ((newcar (car tree)))
336 (if (consp (car tree))
337 (setq newcar (tramp-compat-copy-tree (car tree))))
338 (push newcar result))
339 (setq tree (cdr tree)))
340 (nconc (nreverse result) tree))))
341
342 ;; RECURSIVE has been introduced with Emacs 23.2.
343 (defun tramp-compat-delete-directory (directory &optional recursive)
344 "Like `delete-directory' for Tramp files (compat function)."
345 (if recursive
346 (funcall (symbol-function 'delete-directory) directory recursive)
347 (delete-directory directory)))
348
349 ;; `number-sequence' has been introduced in Emacs 22. Implementation
350 ;; is taken from Emacs 23.
351 (defun tramp-compat-number-sequence (from &optional to inc)
352 "Return a sequence of numbers from FROM to TO as a list (compat function)."
353 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
354 (funcall (symbol-function 'number-sequence) from to inc)
355 (if (or (not to) (= from to))
356 (list from)
357 (or inc (setq inc 1))
358 (when (zerop inc) (error "The increment can not be zero"))
359 (let (seq (n 0) (next from))
360 (if (> inc 0)
361 (while (<= next to)
362 (setq seq (cons next seq)
363 n (1+ n)
364 next (+ from (* n inc))))
365 (while (>= next to)
366 (setq seq (cons next seq)
367 n (1+ n)
368 next (+ from (* n inc)))))
369 (nreverse seq)))))
370
371 (defun tramp-compat-split-string (string pattern)
372 "Like `split-string' but omit empty strings.
373 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
374 This is, the first, empty, element is omitted. In XEmacs, the first
375 element is not omitted."
376 (delete "" (split-string string pattern)))
377
378 (defun tramp-compat-process-running-p (process-name)
379 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
380 (when (stringp process-name)
381 (cond
382 ;; GNU Emacs 22 on w32.
383 ((fboundp 'w32-window-exists-p)
384 (funcall (symbol-function 'w32-window-exists-p)
385 process-name process-name))
386
387 ;; GNU Emacs 23.
388 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
389 (let (result)
390 (dolist (pid (funcall (symbol-function 'list-system-processes)) result)
391 (let ((attributes
392 (funcall (symbol-function 'process-attributes) pid)))
393 (when (and (string-equal
394 (cdr (assoc 'user attributes)) (user-login-name))
395 (let ((comm (cdr (assoc 'comm attributes))))
396 ;; The returned command name could be truncated
397 ;; to 15 characters. Therefore, we cannot check
398 ;; for `string-equal'.
399 (and comm (string-match
400 (concat "^" (regexp-quote comm))
401 process-name))))
402 (setq result t))))))
403
404 ;; Fallback, if there is no Lisp support yet.
405 (t (let ((default-directory
406 (if (file-remote-p default-directory)
407 (tramp-compat-temporary-file-directory)
408 default-directory))
409 (unix95 (getenv "UNIX95"))
410 result)
411 (setenv "UNIX95" "1")
412 (when (member
413 (user-login-name)
414 (tramp-compat-split-string
415 (shell-command-to-string
416 (format "ps -C %s -o user=" process-name))
417 "[ \f\t\n\r\v]+"))
418 (setq result t))
419 (setenv "UNIX95" unix95)
420 result)))))
421
422 (provide 'tramp-compat)
423
424 ;;; TODO:
425
426 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
427 ;;; tramp-compat.el ends here