Document reserved keys
[emacs.git] / lisp / net / tramp-compat.el
blob5bf57638ff8baf5f01c720a047dcb50c8f882e1d
1 ;;; tramp-compat.el --- Tramp compatibility functions -*- lexical-binding:t -*-
3 ;; Copyright (C) 2007-2018 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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Tramp's main Emacs version for development is Emacs 27. This
27 ;; package provides compatibility functions for Emacs 24, Emacs 25 and
28 ;; Emacs 26.
30 ;;; Code:
32 (require 'auth-source)
33 (require 'advice)
34 (require 'cl-lib)
35 (require 'custom)
36 (require 'format-spec)
37 (require 'parse-time)
38 (require 'password-cache)
39 (require 'shell)
40 (require 'timer)
41 (require 'ucs-normalize)
43 (require 'trampver)
44 (require 'tramp-loaddefs)
46 ;; For not existing functions, obsolete functions, or functions with a
47 ;; changed argument list, there are compiler warnings. We want to
48 ;; avoid them in cases we know what we do.
49 (defmacro tramp-compat-funcall (function &rest arguments)
50 "Call FUNCTION if it exists. Do not raise compiler warnings."
51 `(when (functionp ,function)
52 (with-no-warnings (funcall ,function ,@arguments))))
54 (defsubst tramp-compat-temporary-file-directory ()
55 "Return name of directory for temporary files.
56 It is the default value of `temporary-file-directory'."
57 ;; We must return a local directory. If it is remote, we could run
58 ;; into an infloop.
59 (eval (car (get 'temporary-file-directory 'standard-value))))
61 (defsubst tramp-compat-make-temp-file (f &optional dir-flag)
62 "Create a local temporary file (compat function).
63 Add the extension of F, if existing."
64 (let* (file-name-handler-alist
65 (prefix (expand-file-name
66 (symbol-value 'tramp-temp-name-prefix)
67 (tramp-compat-temporary-file-directory)))
68 (extension (file-name-extension f t)))
69 (make-temp-file prefix dir-flag extension)))
71 ;; `temporary-file-directory' as function is introduced with Emacs 26.1.
72 (defalias 'tramp-compat-temporary-file-directory-function
73 (if (fboundp 'temporary-file-directory)
74 'temporary-file-directory
75 'tramp-handle-temporary-file-directory))
77 (defun tramp-compat-process-running-p (process-name)
78 "Returns t if system process PROCESS-NAME is running for `user-login-name'."
79 (when (stringp process-name)
80 (cond
81 ;; GNU Emacs 22 on w32.
82 ((fboundp 'w32-window-exists-p)
83 (tramp-compat-funcall 'w32-window-exists-p process-name process-name))
85 ;; GNU Emacs 23.
86 ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
87 (let (result)
88 (dolist (pid (tramp-compat-funcall 'list-system-processes) result)
89 (let ((attributes (process-attributes pid)))
90 (when (and (string-equal
91 (cdr (assoc 'user attributes)) (user-login-name))
92 (let ((comm (cdr (assoc 'comm attributes))))
93 ;; The returned command name could be truncated
94 ;; to 15 characters. Therefore, we cannot check
95 ;; for `string-equal'.
96 (and comm (string-match
97 (concat "^" (regexp-quote comm))
98 process-name))))
99 (setq result t)))))))))
101 ;; `user-error' has appeared in Emacs 24.3.
102 (defsubst tramp-compat-user-error (vec-or-proc format &rest args)
103 "Signal a pilot error."
104 (apply
105 'tramp-error vec-or-proc
106 (if (fboundp 'user-error) 'user-error 'error) format args))
108 ;; `default-toplevel-value' has been declared in Emacs 24.4.
109 (unless (fboundp 'default-toplevel-value)
110 (defalias 'default-toplevel-value 'symbol-value))
112 ;; `file-attribute-*' are introduced in Emacs 25.1.
114 (if (fboundp 'file-attribute-type)
115 (defalias 'tramp-compat-file-attribute-type 'file-attribute-type)
116 (defsubst tramp-compat-file-attribute-type (attributes)
117 "The type field in ATTRIBUTES returned by `file-attributes'.
118 The value is either t for directory, string (name linked to) for
119 symbolic link, or nil."
120 (nth 0 attributes)))
122 (if (fboundp 'file-attribute-link-number)
123 (defalias 'tramp-compat-file-attribute-link-number
124 'file-attribute-link-number)
125 (defsubst tramp-compat-file-attribute-link-number (attributes)
126 "Return the number of links in ATTRIBUTES returned by `file-attributes'."
127 (nth 1 attributes)))
129 (if (fboundp 'file-attribute-user-id)
130 (defalias 'tramp-compat-file-attribute-user-id 'file-attribute-user-id)
131 (defsubst tramp-compat-file-attribute-user-id (attributes)
132 "The UID field in ATTRIBUTES returned by `file-attributes'.
133 This is either a string or a number. If a string value cannot be
134 looked up, a numeric value, either an integer or a float, is
135 returned."
136 (nth 2 attributes)))
138 (if (fboundp 'file-attribute-group-id)
139 (defalias 'tramp-compat-file-attribute-group-id 'file-attribute-group-id)
140 (defsubst tramp-compat-file-attribute-group-id (attributes)
141 "The GID field in ATTRIBUTES returned by `file-attributes'.
142 This is either a string or a number. If a string value cannot be
143 looked up, a numeric value, either an integer or a float, is
144 returned."
145 (nth 3 attributes)))
147 (if (fboundp 'file-attribute-modification-time)
148 (defalias 'tramp-compat-file-attribute-modification-time
149 'file-attribute-modification-time)
150 (defsubst tramp-compat-file-attribute-modification-time (attributes)
151 "The modification time in ATTRIBUTES returned by `file-attributes'.
152 This is the time of the last change to the file's contents, and
153 is a list of integers (HIGH LOW USEC PSEC) in the same style
154 as (current-time)."
155 (nth 5 attributes)))
157 (if (fboundp 'file-attribute-size)
158 (defalias 'tramp-compat-file-attribute-size 'file-attribute-size)
159 (defsubst tramp-compat-file-attribute-size (attributes)
160 "The size (in bytes) in ATTRIBUTES returned by `file-attributes'.
161 This is a floating point number if the size is too large for an integer."
162 (nth 7 attributes)))
164 (if (fboundp 'file-attribute-modes)
165 (defalias 'tramp-compat-file-attribute-modes 'file-attribute-modes)
166 (defsubst tramp-compat-file-attribute-modes (attributes)
167 "The file modes in ATTRIBUTES returned by `file-attributes'.
168 This is a string of ten letters or dashes as in ls -l."
169 (nth 8 attributes)))
171 ;; `format-message' is new in Emacs 25.1.
172 (unless (fboundp 'format-message)
173 (defalias 'format-message 'format))
175 ;; `directory-name-p' is new in Emacs 25.1.
176 (if (fboundp 'directory-name-p)
177 (defalias 'tramp-compat-directory-name-p 'directory-name-p)
178 (defsubst tramp-compat-directory-name-p (name)
179 "Return non-nil if NAME ends with a directory separator character."
180 (let ((len (length name))
181 (lastc ?.))
182 (if (> len 0)
183 (setq lastc (aref name (1- len))))
184 (or (= lastc ?/)
185 (and (memq system-type '(windows-nt ms-dos))
186 (= lastc ?\\))))))
188 ;; `file-missing' is introduced in Emacs 26.1.
189 (defconst tramp-file-missing
190 (if (get 'file-missing 'error-conditions) 'file-missing 'file-error)
191 "The error symbol for the `file-missing' error.")
193 (add-hook 'tramp-unload-hook
194 (lambda ()
195 (unload-feature 'tramp-loaddefs 'force)
196 (unload-feature 'tramp-compat 'force)))
198 ;; `file-name-quoted-p', `file-name-quote' and `file-name-unquote' are
199 ;; introduced in Emacs 26.
200 (eval-and-compile
201 (if (fboundp 'file-name-quoted-p)
202 (defalias 'tramp-compat-file-name-quoted-p 'file-name-quoted-p)
203 (defsubst tramp-compat-file-name-quoted-p (name)
204 "Whether NAME is quoted with prefix \"/:\".
205 If NAME is a remote file name, check the local part of NAME."
206 (string-match "^/:" (or (file-remote-p name 'localname) name))))
208 (if (fboundp 'file-name-quote)
209 (defalias 'tramp-compat-file-name-quote 'file-name-quote)
210 (defsubst tramp-compat-file-name-quote (name)
211 "Add the quotation prefix \"/:\" to file NAME.
212 If NAME is a remote file name, the local part of NAME is quoted."
213 (if (tramp-compat-file-name-quoted-p name)
214 name
215 (concat
216 (file-remote-p name) "/:" (or (file-remote-p name 'localname) name)))))
218 (if (fboundp 'file-name-unquote)
219 (defalias 'tramp-compat-file-name-unquote 'file-name-unquote)
220 (defsubst tramp-compat-file-name-unquote (name)
221 "Remove quotation prefix \"/:\" from file NAME.
222 If NAME is a remote file name, the local part of NAME is unquoted."
223 (save-match-data
224 (let ((localname (or (file-remote-p name 'localname) name)))
225 (when (tramp-compat-file-name-quoted-p localname)
226 (setq
227 localname
228 (replace-match
229 (if (= (length localname) 2) "/" "") nil t localname)))
230 (concat (file-remote-p name) localname))))))
232 ;; `tramp-syntax' has changed its meaning in Emacs 26. We still
233 ;; support old settings.
234 (defsubst tramp-compat-tramp-syntax ()
235 "Return proper value of `tramp-syntax'."
236 (cond ((eq tramp-syntax 'ftp) 'default)
237 ((eq tramp-syntax 'sep) 'separate)
238 (t tramp-syntax)))
240 ;; `cl-struct-slot-info' has been introduced with Emacs 25.
241 (defmacro tramp-compat-tramp-file-name-slots ()
242 (if (fboundp 'cl-struct-slot-info)
243 `(cdr (mapcar 'car (cl-struct-slot-info 'tramp-file-name)))
244 `(cdr (mapcar 'car (get 'tramp-file-name 'cl-struct-slots)))))
246 (provide 'tramp-compat)
248 ;;; TODO:
250 ;;; tramp-compat.el ends here