* lisp/net/tramp-compat.el (tramp-compat-funcall): Don't use `subrp'.
[emacs.git] / lisp / net / tramp-compat.el
blob0d804fc0a48077a75a0d5265ce2dbfcab0df835c
1 ;;; tramp-compat.el --- Tramp compatibility functions
3 ;; Copyright (C) 2007-2016 Free Software Foundation, Inc.
5 ;; Author: Michael Albinus <michael.albinus@gmx.de>
6 ;; Keywords: comm, processes
7 ;; Package: tramp
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/>.
24 ;;; Commentary:
26 ;; Tramp's main Emacs version for development is Emacs 25. This
27 ;; package provides compatibility functions for Emacs 23 and Emacs 24.
29 ;;; Code:
31 ;; Pacify byte-compiler.
32 (eval-when-compile
33 (require 'cl))
35 (require 'auth-source)
36 (require 'advice)
37 (require 'custom)
38 (require 'format-spec)
39 (require 'password-cache)
40 (require 'shell)
41 (require 'timer)
42 (require 'ucs-normalize)
44 (require 'trampver)
45 (require 'tramp-loaddefs)
47 ;; `remote-file-name-inhibit-cache' has been introduced with Emacs
48 ;; 24.1. Besides t, nil, and integer, we use also timestamps (as
49 ;; returned by `current-time') internally.
50 (unless (boundp 'remote-file-name-inhibit-cache)
51 (defvar remote-file-name-inhibit-cache nil))
53 ;; For not existing functions, obsolete functions, or functions with a
54 ;; changed argument list, there are compiler warnings. We want to
55 ;; avoid them in cases we know what we do.
56 (defmacro tramp-compat-funcall (function &rest arguments)
57 "Call FUNCTION if it exists. Do not raise compiler warnings."
58 `(when (functionp ,function)
59 (with-no-warnings (funcall ,function ,@arguments))))
61 ;; We currently use "[" and "]" in the filename format for IPv6 hosts
62 ;; of GNU Emacs. This means that Emacs wants to expand wildcards if
63 ;; `find-file-wildcards' is non-nil, and then barfs because no
64 ;; expansion could be found. We detect this situation and do
65 ;; something really awful: we have `file-expand-wildcards' return the
66 ;; original filename if it can't expand anything. Let's just hope
67 ;; that this doesn't break anything else. It is not needed anymore
68 ;; since GNU Emacs 23.2.
69 (unless (featurep 'files 'remote-wildcards)
70 (defadvice file-expand-wildcards
71 (around tramp-advice-file-expand-wildcards activate)
72 (let ((name (ad-get-arg 0)))
73 ;; If it's a Tramp file, look if wildcards need to be expanded
74 ;; at all.
75 (if (and
76 (tramp-tramp-file-p name)
77 (not (string-match "[[*?]" (file-remote-p name 'localname))))
78 (setq ad-return-value (list name))
79 ;; Otherwise, just run the original function.
80 ad-do-it)))
81 (add-hook
82 'tramp-unload-hook
83 (lambda ()
84 (ad-remove-advice
85 'file-expand-wildcards 'around 'tramp-advice-file-expand-wildcards)
86 (ad-activate 'file-expand-wildcards))))
88 ;; `condition-case-unless-debug' is introduced with Emacs 24.
89 (if (fboundp 'condition-case-unless-debug)
90 (defalias 'tramp-compat-condition-case-unless-debug
91 'condition-case-unless-debug)
92 (defmacro tramp-compat-condition-case-unless-debug
93 (var bodyform &rest handlers)
94 "Like `condition-case' except that it does not catch anything when debugging."
95 (declare (debug condition-case) (indent 2))
96 (let ((bodysym (make-symbol "body")))
97 `(let ((,bodysym (lambda () ,bodyform)))
98 (if debug-on-error
99 (funcall ,bodysym)
100 (condition-case ,var
101 (funcall ,bodysym)
102 ,@handlers))))))
104 (defsubst tramp-compat-temporary-file-directory ()
105 "Return name of directory for temporary files.
106 It is the default value of `temporary-file-directory'."
107 ;; We must return a local directory. If it is remote, we could run
108 ;; into an infloop.
109 (eval (car (get 'temporary-file-directory 'standard-value))))
111 (defsubst tramp-compat-make-temp-file (f &optional dir-flag)
112 "Create a local temporary file (compat function).
113 Add the extension of F, if existing."
114 (let* (file-name-handler-alist
115 (prefix (expand-file-name
116 (symbol-value 'tramp-temp-name-prefix)
117 (tramp-compat-temporary-file-directory)))
118 (extension (file-name-extension f t)))
119 (make-temp-file prefix dir-flag extension)))
121 ;; `temporary-file-directory' as function is introduced with Emacs 25.2.
122 (defalias 'tramp-compat-temporary-file-directory-function
123 (if (fboundp 'temporary-file-directory)
124 'temporary-file-directory
125 'tramp-handle-temporary-file-directory))
127 ;; PRESERVE-EXTENDED-ATTRIBUTES has been introduced with Emacs 24.1
128 ;; (as PRESERVE-SELINUX-CONTEXT), and renamed in Emacs 24.3.
129 (defun tramp-compat-copy-file
130 (filename newname &optional ok-if-already-exists keep-date
131 preserve-uid-gid preserve-extended-attributes)
132 "Like `copy-file' for Tramp files (compat function)."
133 (cond
134 (preserve-extended-attributes
135 (condition-case nil
136 (tramp-compat-funcall
137 'copy-file filename newname ok-if-already-exists keep-date
138 preserve-uid-gid preserve-extended-attributes)
139 (wrong-number-of-arguments
140 (copy-file
141 filename newname ok-if-already-exists keep-date preserve-uid-gid))))
143 (copy-file
144 filename newname ok-if-already-exists keep-date preserve-uid-gid))))
146 ;; COPY-CONTENTS has been introduced with Emacs 24.1.
147 (defun tramp-compat-copy-directory
148 (directory newname &optional keep-time parents copy-contents)
149 "Make a copy of DIRECTORY (compat function)."
150 (condition-case nil
151 (tramp-compat-funcall
152 'copy-directory directory newname keep-time parents copy-contents)
154 ;; `copy-directory' is either not implemented, or it does not
155 ;; support the the COPY-CONTENTS flag. For the time being, we
156 ;; ignore COPY-CONTENTS as well.
158 (error
159 ;; If `default-directory' is a remote directory, make sure we
160 ;; find its `copy-directory' handler.
161 (let ((handler (or (find-file-name-handler directory 'copy-directory)
162 (find-file-name-handler newname 'copy-directory))))
163 (if handler
164 (funcall handler 'copy-directory directory newname keep-time parents)
166 ;; Compute target name.
167 (setq directory (directory-file-name (expand-file-name directory))
168 newname (directory-file-name (expand-file-name newname)))
169 (if (and (file-directory-p newname)
170 (not (string-equal (file-name-nondirectory directory)
171 (file-name-nondirectory newname))))
172 (setq newname
173 (expand-file-name
174 (file-name-nondirectory directory) newname)))
175 (if (not (file-directory-p newname)) (make-directory newname parents))
177 ;; Copy recursively.
178 (mapc
179 (lambda (file)
180 (if (file-directory-p file)
181 (tramp-compat-copy-directory file newname keep-time parents)
182 (copy-file file newname t keep-time)))
183 ;; We do not want to delete "." and "..".
184 (directory-files directory 'full directory-files-no-dot-files-regexp))
186 ;; Set directory attributes.
187 (set-file-modes newname (file-modes directory))
188 (if keep-time
189 (set-file-times newname (nth 5 (file-attributes directory)))))))))
191 ;; TRASH has been introduced with Emacs 24.1.
192 (defun tramp-compat-delete-file (filename &optional trash)
193 "Like `delete-file' for Tramp files (compat function)."
194 (condition-case nil
195 (tramp-compat-funcall 'delete-file filename trash)
196 ;; This Emacs version does not support the TRASH flag.
197 (wrong-number-of-arguments
198 (let ((delete-by-moving-to-trash
199 (and (boundp 'delete-by-moving-to-trash)
200 (symbol-value 'delete-by-moving-to-trash)
201 trash)))
202 (delete-file filename)))))
204 ;; RECURSIVE has been introduced with Emacs 23.2. TRASH has been
205 ;; introduced with Emacs 24.1.
206 (defun tramp-compat-delete-directory (directory &optional recursive trash)
207 "Like `delete-directory' for Tramp files (compat function)."
208 (condition-case nil
209 (cond
210 (trash
211 (tramp-compat-funcall 'delete-directory directory recursive trash))
213 (delete-directory directory recursive)))
214 ;; This Emacs version does not support the TRASH flag. We use the
215 ;; implementation from Emacs 23.2.
216 (wrong-number-of-arguments
217 (setq directory (directory-file-name (expand-file-name directory)))
218 (when (not (file-symlink-p directory))
219 (mapc (lambda (file)
220 (if (eq t (car (file-attributes file)))
221 (tramp-compat-delete-directory file recursive trash)
222 (tramp-compat-delete-file file trash)))
223 (directory-files
224 directory 'full directory-files-no-dot-files-regexp)))
225 (delete-directory directory))))
227 (defun tramp-compat-process-running-p (process-name)
228 "Returns t if system process PROCESS-NAME is running for `user-login-name'."
229 (when (stringp process-name)
230 (cond
231 ;; GNU Emacs 22 on w32.
232 ((fboundp 'w32-window-exists-p)
233 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
235 ;; GNU Emacs 23.
236 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
237 (let (result)
238 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
239 (let ((attributes (process-attributes pid)))
240 (when (and (string-equal
241 (cdr (assoc 'user attributes)) (user-login-name))
242 (let ((comm (cdr (assoc 'comm attributes))))
243 ;; The returned command name could be truncated
244 ;; to 15 characters. Therefore, we cannot check
245 ;; for `string-equal'.
246 (and comm (string-match
247 (concat "^" (regexp-quote comm))
248 process-name))))
249 (setq result t)))))))))
251 ;; `process-running-live-p' is introduced in Emacs 24.
252 (defalias 'tramp-compat-process-live-p
253 (if (fboundp 'process-running-live-p)
254 'process-running-live-p
255 (lambda (process)
256 "Returns non-nil if PROCESS is alive.
257 A process is considered alive if its status is `run', `open',
258 `listen', `connect' or `stop'. Value is nil if PROCESS is not a
259 process."
260 (and (processp process)
261 (memq (process-status process)
262 '(run open listen connect stop))))))
264 ;; `file-attribute-*' are introduced in Emacs 25.1.
266 (if (fboundp 'file-attribute-type)
267 (defalias 'tramp-compat-file-attribute-type 'file-attribute-type)
268 (defsubst tramp-compat-file-attribute-type (attributes)
269 "The type field in ATTRIBUTES returned by `file-attributes'.
270 The value is either t for directory, string (name linked to) for
271 symbolic link, or nil."
272 (nth 0 attributes)))
274 (if (fboundp 'file-attribute-link-number)
275 (defalias 'tramp-compat-file-attribute-link-number
276 'file-attribute-link-number)
277 (defsubst tramp-compat-file-attribute-link-number (attributes)
278 "Return the number of links in ATTRIBUTES returned by `file-attributes'."
279 (nth 1 attributes)))
281 (if (fboundp 'file-attribute-user-id)
282 (defalias 'tramp-compat-file-attribute-user-id 'file-attribute-user-id)
283 (defsubst tramp-compat-file-attribute-user-id (attributes)
284 "The UID field in ATTRIBUTES returned by `file-attributes'.
285 This is either a string or a number. If a string value cannot be
286 looked up, a numeric value, either an integer or a float, is
287 returned."
288 (nth 2 attributes)))
290 (if (fboundp 'file-attribute-group-id)
291 (defalias 'tramp-compat-file-attribute-group-id 'file-attribute-group-id)
292 (defsubst tramp-compat-file-attribute-group-id (attributes)
293 "The GID field in ATTRIBUTES returned by `file-attributes'.
294 This is either a string or a number. If a string value cannot be
295 looked up, a numeric value, either an integer or a float, is
296 returned."
297 (nth 3 attributes)))
299 (if (fboundp 'file-attribute-modification-time)
300 (defalias 'tramp-compat-file-attribute-modification-time
301 'file-attribute-modification-time)
302 (defsubst tramp-compat-file-attribute-modification-time (attributes)
303 "The modification time in ATTRIBUTES returned by `file-attributes'.
304 This is the time of the last change to the file's contents, and
305 is a list of integers (HIGH LOW USEC PSEC) in the same style
306 as (current-time)."
307 (nth 5 attributes)))
309 (if (fboundp 'file-attribute-size)
310 (defalias 'tramp-compat-file-attribute-size 'file-attribute-size)
311 (defsubst tramp-compat-file-attribute-size (attributes)
312 "The size (in bytes) in ATTRIBUTES returned by `file-attributes'.
313 This is a floating point number if the size is too large for an integer."
314 (nth 7 attributes)))
316 (if (fboundp 'file-attribute-modes)
317 (defalias 'tramp-compat-file-attribute-modes 'file-attribute-modes)
318 (defsubst tramp-compat-file-attribute-modes (attributes)
319 "The file modes in ATTRIBUTES returned by `file-attributes'.
320 This is a string of ten letters or dashes as in ls -l."
321 (nth 8 attributes)))
323 ;; `default-toplevel-value' has been declared in Emacs 24.
324 (unless (fboundp 'default-toplevel-value)
325 (defalias 'default-toplevel-value 'symbol-value))
327 ;; `format-message' is new in Emacs 25.
328 (unless (fboundp 'format-message)
329 (defalias 'format-message 'format))
331 (add-hook 'tramp-unload-hook
332 (lambda ()
333 (unload-feature 'tramp-loaddefs 'force)
334 (unload-feature 'tramp-compat 'force)))
336 (provide 'tramp-compat)
338 ;;; TODO:
340 ;;; tramp-compat.el ends here