make the minibuffer mutex recursive.
[emacs.git] / lisp / net / tramp-compat.el
blob9bcbe21116dc8a515cfa09798e4e9d2f48ca5e89
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/>.
23 ;;; Commentary:
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+.
29 ;;; Code:
31 (eval-when-compile
33 ;; Pacify byte-compiler.
34 (require 'cl))
36 (eval-and-compile
38 (require 'custom)
40 ;; Load the appropriate timer package.
41 (if (featurep 'xemacs)
42 (require 'timer-funcs)
43 (require 'timer))
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)))
55 (require 'tramp-util)
56 (add-hook 'tramp-unload-hook
57 '(lambda ()
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.
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)))))))
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.
96 (condition-case nil
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
110 ;; mechanism.
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)))))
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)))))
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)))))
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) (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)))))
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)))))
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)))))
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"))))
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 &optional dir-flag)
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
230 (symbol-function 'make-temp-file) prefix dir-flag extension))
231 (error
232 ;; We use our own implementation, taken from files.el.
233 (while
234 (condition-case ()
235 (progn
236 (setq result (concat (make-temp-name prefix) extension))
237 (if dir-flag
238 (make-directory result)
239 (write-region
240 "" nil result nil 'silent nil
241 ;; 7th parameter is MUSTBENEW in Emacs, and
242 ;; CODING-SYSTEM in XEmacs. It is not a security
243 ;; hole in XEmacs if we cannot use this parameter,
244 ;; because XEmacs uses a user-specific
245 ;; subdirectory with 0700 permissions.
246 (when (not (featurep 'xemacs)) 'excl)))
247 nil)
248 (file-already-exists t))
249 ;; The file was somehow created by someone else between
250 ;; `make-temp-name' and `write-region', let's try again.
251 nil)))
252 result))
254 ;; `most-positive-fixnum' arrived in Emacs 22. Before, and in XEmacs,
255 ;; it is a fixed value.
256 (defsubst tramp-compat-most-positive-fixnum ()
257 "Return largest positive integer value (compat function)."
258 (cond
259 ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
260 ;; Default value in XEmacs and Emacs 21.
261 (t 134217727)))
263 ;; ID-FORMAT exists since Emacs 22.
264 (defun tramp-compat-file-attributes (filename &optional id-format)
265 "Like `file-attributes' for Tramp files (compat function)."
266 (cond
267 ((or (null id-format) (eq id-format 'integer))
268 (file-attributes filename))
269 ((tramp-tramp-file-p filename)
270 (tramp-file-name-handler 'file-attributes filename id-format))
271 (t (condition-case nil
272 (funcall (symbol-function 'file-attributes) filename id-format)
273 (error (file-attributes filename))))))
275 ;; PRESERVE-UID-GID has been introduced with Emacs 23. It does not
276 ;; hurt to ignore it for other (X)Emacs versions.
277 (defun tramp-compat-copy-file
278 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
279 "Like `copy-file' for Tramp files (compat function)."
280 (if preserve-uid-gid
281 (funcall
282 (symbol-function 'copy-file)
283 filename newname ok-if-already-exists keep-date preserve-uid-gid)
284 (copy-file filename newname ok-if-already-exists keep-date)))
286 ;; `copy-directory' is a new function in Emacs 23.2. Implementation
287 ;; is taken from there.
288 (defun tramp-compat-copy-directory
289 (directory newname &optional keep-time parents)
290 "Make a copy of DIRECTORY (compat function)."
291 (if (fboundp 'copy-directory)
292 (funcall
293 (symbol-function 'copy-directory) directory newname keep-time parents)
295 ;; If default-directory is a remote directory, make sure we find
296 ;; its copy-directory handler.
297 (let ((handler (or (find-file-name-handler directory 'copy-directory)
298 (find-file-name-handler newname 'copy-directory))))
299 (if handler
300 (funcall handler 'copy-directory directory newname keep-time parents)
302 ;; Compute target name.
303 (setq directory (directory-file-name (expand-file-name directory))
304 newname (directory-file-name (expand-file-name newname)))
305 (if (and (file-directory-p newname)
306 (not (string-equal (file-name-nondirectory directory)
307 (file-name-nondirectory newname))))
308 (setq newname
309 (expand-file-name
310 (file-name-nondirectory directory) newname)))
311 (if (not (file-directory-p newname)) (make-directory newname parents))
313 ;; Copy recursively.
314 (mapc
315 (lambda (file)
316 (if (file-directory-p file)
317 (tramp-compat-copy-directory file newname keep-time parents)
318 (copy-file file newname t keep-time)))
319 ;; We do not want to delete "." and "..".
320 (directory-files
321 directory 'full "^\\([^.]\\|\\.\\([^.]\\|\\..\\)\\).*"))
323 ;; Set directory attributes.
324 (set-file-modes newname (file-modes directory))
325 (if keep-time
326 (set-file-times newname (nth 5 (file-attributes directory))))))))
328 ;; `copy-tree' is a built-in function in XEmacs. In Emacs 21, it is
329 ;; an autoloaded function in cl-extra.el. Since Emacs 22, it is part
330 ;; of subr.el. There are problems when autoloading, therefore we test
331 ;; for `subrp' and `symbol-file'. Implementation is taken from Emacs 23.
332 (defun tramp-compat-copy-tree (tree)
333 "Make a copy of TREE (compat function)."
334 (if (or (subrp 'copy-tree) (symbol-file 'copy-tree))
335 (funcall (symbol-function 'copy-tree) tree)
336 (let (result)
337 (while (consp tree)
338 (let ((newcar (car tree)))
339 (if (consp (car tree))
340 (setq newcar (tramp-compat-copy-tree (car tree))))
341 (push newcar result))
342 (setq tree (cdr tree)))
343 (nconc (nreverse result) tree))))
345 ;; RECURSIVE has been introduced with Emacs 23.2.
346 (defun tramp-compat-delete-directory (directory &optional recursive)
347 "Like `delete-directory' for Tramp files (compat function)."
348 (if recursive
349 (funcall (symbol-function 'delete-directory) directory recursive)
350 (delete-directory directory)))
352 ;; `number-sequence' has been introduced in Emacs 22. Implementation
353 ;; is taken from Emacs 23.
354 (defun tramp-compat-number-sequence (from &optional to inc)
355 "Return a sequence of numbers from FROM to TO as a list (compat function)."
356 (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
357 (funcall (symbol-function 'number-sequence) from to inc)
358 (if (or (not to) (= from to))
359 (list from)
360 (or inc (setq inc 1))
361 (when (zerop inc) (error "The increment can not be zero"))
362 (let (seq (n 0) (next from))
363 (if (> inc 0)
364 (while (<= next to)
365 (setq seq (cons next seq)
366 n (1+ n)
367 next (+ from (* n inc))))
368 (while (>= next to)
369 (setq seq (cons next seq)
370 n (1+ n)
371 next (+ from (* n inc)))))
372 (nreverse seq)))))
374 (defun tramp-compat-split-string (string pattern)
375 "Like `split-string' but omit empty strings.
376 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
377 This is, the first, empty, element is omitted. In XEmacs, the first
378 element is not omitted."
379 (delete "" (split-string string pattern)))
381 (defun tramp-compat-process-running-p (process-name)
382 "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
383 (when (stringp process-name)
384 (cond
385 ;; GNU Emacs 22 on w32.
386 ((fboundp 'w32-window-exists-p)
387 (funcall (symbol-function 'w32-window-exists-p)
388 process-name process-name))
390 ;; GNU Emacs 23.
391 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
392 (let (result)
393 (dolist (pid (funcall (symbol-function 'list-system-processes)) result)
394 (let ((attributes
395 (funcall (symbol-function 'process-attributes) pid)))
396 (when (and (string-equal
397 (cdr (assoc 'user attributes)) (user-login-name))
398 (let ((comm (cdr (assoc 'comm attributes))))
399 ;; The returned command name could be truncated
400 ;; to 15 characters. Therefore, we cannot check
401 ;; for `string-equal'.
402 (and comm (string-match
403 (concat "^" (regexp-quote comm))
404 process-name))))
405 (setq result t))))))
407 ;; Fallback, if there is no Lisp support yet.
408 (t (let ((default-directory
409 (if (file-remote-p default-directory)
410 (tramp-compat-temporary-file-directory)
411 default-directory))
412 (unix95 (getenv "UNIX95"))
413 result)
414 (setenv "UNIX95" "1")
415 (when (member
416 (user-login-name)
417 (tramp-compat-split-string
418 (shell-command-to-string
419 (format "ps -C %s -o user=" process-name))
420 "[ \f\t\n\r\v]+"))
421 (setq result t))
422 (setenv "UNIX95" unix95)
423 result)))))
425 (provide 'tramp-compat)
427 ;;; TODO:
429 ;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
430 ;;; tramp-compat.el ends here