tramp-compat.el 13.1 KB
Newer Older
1
;;; tramp-compat.el --- Tramp compatibility functions
2

Glenn Morris's avatar
Glenn Morris committed
3
;; Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
4 5 6 7 8 9

;; Author: Michael Albinus <michael.albinus@gmx.de>
;; Keywords: comm, processes

;; 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 13
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
14 15 16 17 18 19 20

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; 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 24 25 26 27 28 29 30

;;; Commentary:

;; Tramp's main Emacs version for development is GNU Emacs 23.  This
;; package provides compatibility functions for GNU Emacs 21, GNU
;; Emacs 22 and XEmacs 21.4+.

;;; Code:

31
(eval-when-compile
32 33

  ;; Pacify byte-compiler.
34 35 36 37
  (require 'cl))

(eval-and-compile

38
  (require 'custom)
39

40 41 42 43
  ;; Load the appropriate timer package.
  (if (featurep 'xemacs)
      (require 'timer-funcs)
    (require 'timer))
44

45 46 47
  (autoload 'tramp-tramp-file-p "tramp")
  (autoload 'tramp-file-name-handler "tramp")

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
  ;; tramp-util offers integration into other (X)Emacs packages like
  ;; compile.el, gud.el etc.  Not necessary in Emacs 23.
  (eval-after-load "tramp"
    ;; We check whether `start-file-process' is an alias.
    '(when (or (not (fboundp 'start-file-process))
	       (symbolp (symbol-function 'start-file-process)))
       (require 'tramp-util)
       (add-hook 'tramp-unload-hook
		 '(lambda ()
		    (when (featurep 'tramp-util)
		      (unload-feature 'tramp-util 'force))))))

  ;; Make sure that we get integration with the VC package.  When it
  ;; is loaded, we need to pull in the integration module.  Not
  ;; necessary in Emacs 23.
  (eval-after-load "vc"
    (eval-after-load "tramp"
      ;; We check whether `start-file-process' is an alias.
      '(when (or (not (fboundp 'start-file-process))
		 (symbolp (symbol-function 'start-file-process)))
	 (require 'tramp-vc)
	 (add-hook 'tramp-unload-hook
		   '(lambda ()
		      (when (featurep 'tramp-vc)
			(unload-feature 'tramp-vc 'force)))))))

74 75 76 77 78 79 80 81
  ;; Avoid byte-compiler warnings if the byte-compiler supports this.
  ;; Currently, XEmacs supports this.
  (when (featurep 'xemacs)
    (unless (boundp 'byte-compile-default-warnings)
      (defvar byte-compile-default-warnings nil))
    (delq 'unused-vars byte-compile-default-warnings))

  ;; `last-coding-system-used' is unknown in XEmacs.
82
  (unless (boundp 'last-coding-system-used)
83
    (defvar last-coding-system-used nil))
84

85 86 87
  ;; `directory-sep-char' is an obsolete variable in Emacs.  But it is
  ;; used in XEmacs, so we set it here and there.  The following is
  ;; needed to pacify Emacs byte-compiler.
88 89
  (unless (boundp 'byte-compile-not-obsolete-var)
    (defvar byte-compile-not-obsolete-var nil))
90
  (setq byte-compile-not-obsolete-var 'directory-sep-char)
91 92
  (if (boundp 'byte-compile-not-obsolete-vars) ; Emacs 23.2
      (setq byte-compile-not-obsolete-vars '(directory-sep-char)))
93

94
  ;; `with-temp-message' does not exists in XEmacs.
95 96
  (condition-case nil
      (with-temp-message (current-message) nil)
97
    (error (defmacro with-temp-message (message &rest body) `(progn ,@body))))
98

99
  ;; `set-buffer-multibyte' comes from Emacs Leim.
100
  (unless (fboundp 'set-buffer-multibyte)
101
    (defalias 'set-buffer-multibyte 'ignore))
102

103
  ;; `font-lock-add-keywords' does not exist in XEmacs.
104
  (unless (fboundp 'font-lock-add-keywords)
105 106
    (defalias 'font-lock-add-keywords 'ignore))

107 108 109 110
  ;; The following functions cannot be aliases of the corresponding
  ;; `tramp-handle-*' functions, because this would bypass the locking
  ;; mechanism.

111 112 113 114 115
  ;; `file-remote-p' has been introduced with Emacs 22.  The version
  ;; of XEmacs is not a magic file name function (yet); this is
  ;; corrected in tramp-util.el.  Here it is sufficient if the
  ;; function exists.
  (unless (fboundp 'file-remote-p)
116 117 118 119 120
    (defalias 'file-remote-p
      (lambda (file &optional identification connected)
	(when (tramp-tramp-file-p file)
	  (tramp-file-name-handler
	   'file-remote-p file identification connected)))))
121 122 123

  ;; `process-file' exists since Emacs 22.
  (unless (fboundp 'process-file)
124 125 126 127 128 129
    (defalias 'process-file
      (lambda (program &optional infile buffer display &rest args)
	(when (tramp-tramp-file-p default-directory)
	  (apply
	   'tramp-file-name-handler
	   'process-file program infile buffer display args)))))
130 131 132

  ;; `start-file-process' is new in Emacs 23.
  (unless (fboundp 'start-file-process)
133 134 135 136 137 138
    (defalias 'start-file-process
      (lambda (name buffer program &rest program-args)
	(when (tramp-tramp-file-p default-directory)
	  (apply
	   'tramp-file-name-handler
	   'start-file-process name buffer program program-args)))))
139 140 141

  ;; `set-file-times' is also new in Emacs 23.
  (unless (fboundp 'set-file-times)
142 143 144 145 146
    (defalias 'set-file-times
      (lambda (filename &optional time)
	(when (tramp-tramp-file-p filename)
	  (tramp-file-name-handler
	   'set-file-times filename time))))))
147

148 149 150 151 152 153 154 155 156 157
(defsubst tramp-compat-line-beginning-position ()
  "Return point at beginning of line (compat function).
Calls `line-beginning-position' or `point-at-bol' if defined, else
own implementation."
  (cond
   ((fboundp 'line-beginning-position)
    (funcall (symbol-function 'line-beginning-position)))
   ((fboundp 'point-at-bol) (funcall (symbol-function 'point-at-bol)))
   (t (save-excursion (beginning-of-line) (point)))))

158 159 160 161 162 163 164 165 166 167 168 169 170 171
(defsubst tramp-compat-line-end-position ()
  "Return point at end of line (compat function).
Calls `line-end-position' or `point-at-eol' if defined, else
own implementation."
  (cond
   ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
   ((fboundp 'point-at-eol) 	 (funcall (symbol-function 'point-at-eol)))
   (t (save-excursion (end-of-line) (point)))))

(defsubst tramp-compat-temporary-file-directory ()
  "Return name of directory for temporary files (compat function).
For Emacs, this is the variable `temporary-file-directory', for XEmacs
this is the function `temp-directory'."
  (cond
172 173
   ((boundp 'temporary-file-directory) (symbol-value 'temporary-file-directory))
   ((fboundp 'temp-directory) (funcall (symbol-function 'temp-directory)))
174 175 176 177 178 179 180 181 182 183 184
   ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
    (file-name-as-directory (getenv "TEMP")))
   ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
    (file-name-as-directory (getenv "TMP")))
   ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
    (file-name-as-directory (getenv "TMPDIR")))
   ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
   (t (message (concat "Neither `temporary-file-directory' nor "
		       "`temp-directory' is defined -- using /tmp."))
      (file-name-as-directory "/tmp"))))

185 186 187 188 189 190 191
;; `make-temp-file' exists in Emacs only.  The third parameter SUFFIX
;; has been introduced with Emacs 22.  We try it, if it fails, we fall
;; back to `make-temp-name', creating the temporary file immediately
;; in order to avoid a security hole.
(defsubst tramp-compat-make-temp-file (filename)
  "Create a temporary file (compat function).
Add the extension of FILENAME, if existing."
192 193 194 195 196 197
  (let* (file-name-handler-alist
	 (prefix (expand-file-name
		  (symbol-value 'tramp-temp-name-prefix)
		  (tramp-compat-temporary-file-directory)))
	 (extension (file-name-extension filename t))
	 result)
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    (condition-case nil
	(setq result
	      (funcall (symbol-function 'make-temp-file) prefix nil extension))
      (error
       ;; We use our own implementation, taken from files.el.
       (while
	   (condition-case ()
	       (progn
		 (setq result (concat (make-temp-name prefix) extension))
		 (write-region
		  "" nil result nil 'silent nil
		  ;; 7th parameter is MUSTBENEW in Emacs, and
		  ;; CODING-SYSTEM in XEmacs.  It is not a security
		  ;; hole in XEmacs if we cannot use this parameter,
		  ;; because XEmacs uses a user-specific subdirectory
		  ;; with 0700 permissions.
		  (when (not (featurep 'xemacs)) 'excl))
		 nil)
	     (file-already-exists t))
	 ;; The file was somehow created by someone else between
	 ;; `make-temp-name' and `write-region', let's try again.
	 nil)))
    result))

222 223
;; `most-positive-fixnum' arrived in Emacs 22.  Before, and in XEmacs,
;; it is a fixed value.
224 225
(defsubst tramp-compat-most-positive-fixnum ()
  "Return largest positive integer value (compat function)."
226 227 228 229
  (cond
   ((boundp 'most-positive-fixnum) (symbol-value 'most-positive-fixnum))
   ;; Default value in XEmacs and Emacs 21.
   (t 134217727)))
230 231 232 233 234 235 236

;; ID-FORMAT exists since Emacs 22.
(defun tramp-compat-file-attributes (filename &optional id-format)
  "Like `file-attributes' for Tramp files (compat function)."
  (cond
   ((or (null id-format) (eq id-format 'integer))
    (file-attributes filename))
237 238
   ((tramp-tramp-file-p filename)
    (tramp-file-name-handler 'file-attributes filename id-format))
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
   (t (condition-case nil
	  (funcall (symbol-function 'file-attributes) filename id-format)
	(error (file-attributes filename))))))

;; PRESERVE-UID-GID has been introduced with Emacs 23.  It does not
;; hurt to ignore it for other (X)Emacs versions.
(defun tramp-compat-copy-file
  (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
  "Like `copy-file' for Tramp files (compat function)."
  (if preserve-uid-gid
      (funcall
       (symbol-function 'copy-file)
       filename newname ok-if-already-exists keep-date preserve-uid-gid)
    (copy-file filename newname ok-if-already-exists keep-date)))

254
;; `copy-tree' is a built-in function in XEmacs.  In Emacs 21, it is
Michael Albinus's avatar
Michael Albinus committed
255
;; an autoloaded function in cl-extra.el.  Since Emacs 22, it is part
256
;; of subr.el.  There are problems when autoloading, therefore we test
257
;; for `subrp' and `symbol-file'.  Implementation is taken from Emacs 23.
258 259
(defun tramp-compat-copy-tree (tree)
  "Make a copy of TREE (compat function)."
260
  (if (or (subrp 'copy-tree) (symbol-file 'copy-tree))
261 262 263 264 265 266 267 268 269 270
      (funcall (symbol-function 'copy-tree) tree)
    (let (result)
      (while (consp tree)
	(let ((newcar (car tree)))
	  (if (consp (car tree))
	      (setq newcar (tramp-compat-copy-tree (car tree))))
	  (push newcar result))
	(setq tree (cdr tree)))
      (nconc (nreverse result) tree))))

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
;; `number-sequence' has been introduced in Emacs 22.  Implementation
;; is taken from Emacs 23.
(defun tramp-compat-number-sequence (from &optional to inc)
  "Return a sequence of numbers from FROM to TO as a list (compat function)."
  (if (or (subrp 'number-sequence) (symbol-file 'number-sequence))
      (funcall (symbol-function 'number-sequence) from to inc)
    (if (or (not to) (= from to))
	(list from)
      (or inc (setq inc 1))
      (when (zerop inc) (error "The increment can not be zero"))
      (let (seq (n 0) (next from))
	(if (> inc 0)
	    (while (<= next to)
	      (setq seq (cons next seq)
		    n (1+ n)
		    next (+ from (* n inc))))
	  (while (>= next to)
	    (setq seq (cons next seq)
		  n (1+ n)
		  next (+ from (* n inc)))))
	(nreverse seq)))))

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
(defun tramp-compat-split-string (string pattern)
  "Like `split-string' but omit empty strings.
In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
This is, the first, empty, element is omitted.  In XEmacs, the first
element is not omitted."
  (delete "" (split-string string pattern)))

(defun tramp-compat-process-running-p (process-name)
  "Returns `t' if system process PROCESS-NAME is running for `user-login-name'."
  (when (stringp process-name)
    (cond
     ;; GNU Emacs 22 on w32.
     ((fboundp 'w32-window-exists-p)
      (funcall (symbol-function 'w32-window-exists-p)
	       process-name process-name))

     ;; GNU Emacs 23.
     ((and (fboundp 'list-system-processes) (fboundp 'process-attributes))
      (let (result)
	(dolist (pid (funcall (symbol-function 'list-system-processes)) result)
	  (let ((attributes
		 (funcall (symbol-function 'process-attributes) pid)))
315 316 317 318 319 320 321 322 323
	    (when (and (string-equal
                        (cdr (assoc 'user attributes)) (user-login-name))
                       (let ((comm (cdr (assoc 'comm attributes))))
                         ;; The returned command name could be truncated
                         ;; to 15 characters.  Therefore, we cannot check
                         ;; for `string-equal'.
                         (and comm (string-match
                                    (concat "^" (regexp-quote comm))
                                    process-name))))
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
	      (setq result t))))))

     ;; Fallback, if there is no Lisp support yet.
     (t (let ((default-directory
		(if (file-remote-p default-directory)
		    (tramp-compat-temporary-file-directory)
		  default-directory))
	      (unix95 (getenv "UNIX95"))
	      result)
	  (setenv "UNIX95" "1")
	  (when (member
		 (user-login-name)
		 (tramp-compat-split-string
		  (shell-command-to-string
		   (format "ps -C %s -o user=" process-name))
		  "[ \f\t\n\r\v]+"))
	    (setq result t))
	  (setenv "UNIX95" unix95)
	  result)))))

344 345 346 347
(provide 'tramp-compat)

;;; TODO:

Miles Bader's avatar
Miles Bader committed
348
;; arch-tag: 0e724b18-6699-4f87-ad96-640b272e5c85
349
;;; tramp-compat.el ends here