*** empty log message ***
[bpt/emacs.git] / lisp / net / tramp-util.el
CommitLineData
ea9d1443 1;;; -*- coding: iso-2022-7bit; -*-
4007ba5b
KG
2;;; tramp-util.el --- Misc utility functions to use with Tramp
3
a69c01a0
MA
4;; Copyright (C) 2001, 2002, 2003, 2004, 2005,
5;; 2006 Free Software Foundation, Inc.
4007ba5b 6
01917a18 7;; Author: kai.grossjohann@gmx.net
4007ba5b
KG
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
3a35cf56
LK
22;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
4007ba5b
KG
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)
a69c01a0
MA
35(add-hook 'tramp-util-unload-hook
36 '(lambda ()
37 (when (featurep 'tramp)
38 (unload-feature 'tramp 'force))))
4007ba5b 39
01917a18
MA
40;; Define a Tramp minor mode. It's intention is to redefine some keys for Tramp
41;; specific functions, like compilation.
340b8d4f 42;; The key remapping works since Emacs 22 only. Unknown for XEmacs.
01917a18 43
75b19c39
MA
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)
a69c01a0 50 (defalias 'add-menu-button 'ignore)))
75b19c39
MA
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)
a69c01a0
MA
65(add-hook 'tramp-util-unload-hook
66 '(lambda ()
67 (remove-hook 'find-file-hooks 'tramp-minor-mode)))
68
75b19c39 69(add-hook 'dired-mode-hook 'tramp-minor-mode t)
a69c01a0
MA
70(add-hook 'tramp-util-unload-hook
71 '(lambda ()
72 (remove-hook 'dired-mode-hook 'tramp-minor-mode)))
75b19c39
MA
73
74(defun tramp-remap-command (old-command new-command)
75 "Replaces bindings of OLD-COMMAND by NEW-COMMAND.
01917a18
MA
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."
75b19c39
MA
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)]))
01917a18
MA
104
105;; Utility functions.
106
4007ba5b
KG
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*"))
01917a18
MA
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)
4007ba5b
KG
134 (compilation-minor-mode 1))
135
136(provide 'tramp-util)
ab5796a9
MB
137
138;;; arch-tag: 500f9992-a44e-46d0-83a7-980799251808
4007ba5b 139;;; tramp-util.el ends here