1;;; -*- coding: iso-2022-7bit; -*-
2;;; tramp-util.el --- Misc utility functions to use with Tramp
3
4;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
5;;   2006, 2007 Free Software Foundation, Inc.
6
7;; Author: kai.grossjohann@gmx.net
8;; Keywords: comm, extensions, processes
9
10;; This file 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 2, or (at your option)
13;; any later version.
14
15;; This file 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; see the file COPYING.  If not, write to
22;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
24
25;;; Commentary:
26
27;; Some misc. utility functions that might go nicely with Tramp.
28;; Mostly, these are kluges awaiting real solutions later on.
29
30;;; Code:
31
32(eval-when-compile (require 'cl))
33(require 'compile)
34(require 'tramp)
35(add-hook 'tramp-util-unload-hook
36	  '(lambda ()
37	     (when (featurep 'tramp)
38	       (unload-feature 'tramp 'force))))
39
40;; Define a Tramp minor mode. It's intention is to redefine some keys for Tramp
41;; specific functions, like compilation.
42;; The key remapping works since Emacs 22 only. Unknown for XEmacs.
43
44;; Pacify byte-compiler
45(eval-when-compile
46  (unless (fboundp 'define-minor-mode)
47      (defalias 'define-minor-mode 'identity)
48      (defvar tramp-minor-mode))
49  (unless (featurep 'xemacs)
50    (defalias 'add-menu-button 'ignore)))
51
52(defvar tramp-minor-mode-map (make-sparse-keymap)
53  "Keymap for Tramp minor mode.")
54
55(define-minor-mode tramp-minor-mode "Tramp minor mode for utility functions."
56  :group 'tramp
57  :global nil
58  :init-value nil
59  :lighter " Tramp"
60  :keymap tramp-minor-mode-map
61  (setq tramp-minor-mode
62	(and tramp-minor-mode (tramp-tramp-file-p default-directory))))
63
64(add-hook 'find-file-hooks 'tramp-minor-mode t)
65(add-hook 'tramp-util-unload-hook
66	  '(lambda ()
67	     (remove-hook 'find-file-hooks 'tramp-minor-mode)))
68
69(add-hook 'dired-mode-hook 'tramp-minor-mode t)
70(add-hook 'tramp-util-unload-hook
71	  '(lambda ()
72	     (remove-hook 'dired-mode-hook 'tramp-minor-mode)))
73
74(defun tramp-remap-command (old-command new-command)
75  "Replaces bindings of OLD-COMMAND by NEW-COMMAND.
76If remapping functionality for keymaps is defined, this happens for all
77bindings.  Otherwise, only bindings active during invocation are taken
78into account.  XEmacs menubar bindings are not changed by this."
79  (if (functionp 'command-remapping)
80      ;; Emacs 22
81      (eval
82       `(define-key tramp-minor-mode-map [remap ,old-command] new-command))
83    ;; previous Emacs versions.
84    (mapcar
85     '(lambda (x)
86	(define-key tramp-minor-mode-map x new-command))
87     (where-is-internal old-command))))
88
89(tramp-remap-command 'compile 'tramp-compile)
90(tramp-remap-command 'recompile 'tramp-recompile)
91
92;; XEmacs has an own mimic for menu entries
93(when (fboundp 'add-menu-button)
94  (funcall 'add-menu-button
95   '("Tools" "Compile")
96   ["Compile..."
97    (command-execute (if tramp-minor-mode 'tramp-compile 'compile))
98    :active (fboundp 'compile)])
99  (funcall 'add-menu-button
100   '("Tools" "Compile")
101   ["Repeat Compilation"
102    (command-execute (if tramp-minor-mode 'tramp-recompile 'recompile))
103    :active (fboundp 'compile)]))
104
105;; Utility functions.
106
107(defun tramp-compile (command)
108  "Compile on remote host."
109  (interactive
110   (if (or compilation-read-command current-prefix-arg)
111       (list (read-from-minibuffer "Compile command: "
112                                   compile-command nil nil
113                                   '(compile-history . 1)))
114     (list compile-command)))
115  (setq compile-command command)
116  (save-some-buffers (not compilation-ask-about-save) nil)
117  (let ((d default-directory))
118    (save-excursion
119      (pop-to-buffer (get-buffer-create "*Compilation*") t)
120      (erase-buffer)
121      (setq default-directory d)))
122  (tramp-handle-shell-command command (get-buffer "*Compilation*"))
123  (pop-to-buffer (get-buffer "*Compilation*"))
124  (tramp-minor-mode 1)
125  (compilation-minor-mode 1))
126
127(defun tramp-recompile ()
128  "Re-compile on remote host."
129  (interactive)
130  (save-some-buffers (not compilation-ask-about-save) nil)
131  (tramp-handle-shell-command compile-command (get-buffer "*Compilation*"))
132  (pop-to-buffer (get-buffer "*Compilation*"))
133  (tramp-minor-mode 1)
134  (compilation-minor-mode 1))
135
136(provide 'tramp-util)
137
138;;; arch-tag: 500f9992-a44e-46d0-83a7-980799251808
139;;; tramp-util.el ends here
140