Remove old and crusty Sun pkg
[packages] / xemacs-packages / eshell / esh-toggle.el
1 ;;; esh-toggle --- toggle to and from the *eshell* buffer
2
3 ;; Copyright (C) 1997, 1998 Mikael Sjödin (mic@docs.uu.se)
4
5 ;; Author: Mikael Sjödin <mic@docs.uu.se>
6 ;;         John Wiegley <johnw@gnu.org>
7 ;; Created: 19 Nov 1998
8 ;; Version: 2.0
9 ;; Keywords: processes
10 ;; X-URL: http://www.emacs.org/~johnw/eshell.html
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Provides the command eshell-toggle which toggles between the
30 ;; *eshell* buffer and whatever buffer you are editing.
31 ;;
32 ;; This is done in an "intelligent" way.  Features are:
33 ;;
34 ;;  - Starts a eshell if non is existing.
35 ;;
36 ;;  - Minimum distortion of your window configuration.
37 ;;
38 ;;  - When done in the eshell-buffer you are returned to the same
39 ;;    window configuration you had before you toggled to the eshell.
40 ;;
41 ;;  - If you desire, you automagically get a "cd" command in the
42 ;;    eshell to the directory where your current buffers file exists;
43 ;;    just call eshell-toggle-cd instead of eshell-toggle.
44 ;;
45 ;;  - You can convinently choose if you want to have the eshell in
46 ;;    another window or in the whole frame.  Just invoke eshell-toggle
47 ;;    again to get the eshell in the whole frame.
48 ;;
49 ;; This file has been tested under Emacs 20.2.
50 ;;
51 ;; To use, call the functions `eshell-toggle' or `eshell-toggle-cd'.
52 ;; It's most helpful to bind these to a key.
53
54 ;;; Thanks to:
55
56 ;; Christian Stern <Christian.Stern@physik.uni-regensburg.de> for
57 ;; helpful sugestions.
58
59 ;;; User Variables:
60
61 (defvar eshell-toggle-goto-eob t
62   "*If non-nil `eshell-toggle' moves point to end of Eshell buffer.
63 When `eshell-toggle-cd' is called the point is always moved to the
64 end of the eshell-buffer")
65
66 (defvar eshell-toggle-automatic-cd t
67   "*If non-nil `eshell-toggle-cd' will send a \"cd\" to Eshell.
68 If nil `eshell-toggle-cd' will only insert the \"cd\" command in the
69 eshell-buffer.  Leaving it to the user to press RET to send the
70 command to the eshell.")
71
72 ;;; User Functions:
73
74 ;;;###autoload
75 (defun eshell-toggle-cd ()
76   "Calls `eshell-toggle' with a prefix argument.
77 See the command `eshell-toggle'"
78   (interactive)
79   (eshell-toggle t))
80
81 ;;;###autoload
82 (defun eshell-toggle (make-cd)
83   "Toggles between the *eshell* buffer and the current buffer.
84 With a prefix ARG also insert a \"cd DIR\" command into the eshell,
85 where DIR is the directory of the current buffer.
86
87 Call twice in a row to get a full screen window for the *eshell*
88 buffer.
89
90 When called in the *eshell* buffer returns you to the buffer you were
91 editing before caling the first time.
92
93 Options: `eshell-toggle-goto-eob'"
94   (interactive "P")
95   ;; Try to descide on one of three possibilities:
96   ;; 1. If not in eshell-buffer, switch to it.
97   ;; 2. If in eshell-buffer and called twice in a row, delete other
98   ;;    windows
99   ;; 3. If in eshell-buffer and not called twice in a row, return to
100   ;;    state before going to the eshell-buffer
101   (if (eq major-mode 'eshell-mode)
102       (if (and (or (eq last-command 'eshell-toggle)
103                    (eq last-command 'eshell-toggle-cd))
104                (not (eq (count-windows) 1)))
105           (delete-other-windows)
106         (eshell-toggle-buffer-return-from-eshell))
107     (eshell-toggle-buffer-goto-eshell make-cd)))
108
109 ;;; Internal Functions:
110
111 (defvar eshell-toggle-pre-eshell-win-conf nil
112   "Contains window config before the *eshell* buffer was selected")
113
114 (defun eshell-toggle-buffer-return-from-eshell ()
115   "Restores window config used before switching the *eshell* buffer.
116 If no configuration has been stored, just bury the *eshell* buffer."
117   (if (window-configuration-p eshell-toggle-pre-eshell-win-conf)
118       (progn
119         (set-window-configuration eshell-toggle-pre-eshell-win-conf)
120         (setq eshell-toggle-pre-eshell-win-conf nil)
121         (bury-buffer (get-buffer "*eshell*")))
122     (bury-buffer)))
123
124 (defun eshell-toggle-buffer-goto-eshell (make-cd)
125   "Switches other window to the *eshell* buffer.
126 If no *eshell* buffer exists start a new eshell and switch to it in
127 other window.  If argument MAKE-CD is non-nil, insert a \"cd DIR\"
128 command into the eshell, where DIR is the directory of the current
129 buffer.
130 Stores the window cofiguration before creating and/or switching window."
131   (setq eshell-toggle-pre-eshell-win-conf (current-window-configuration))
132   (let ((eshell-buffer (get-buffer "*eshell*"))
133         (cd-command
134          ;; Find out which directory we are in (the method differs for
135          ;; different buffers)
136          (or (and make-cd
137                   (buffer-file-name)
138                   (file-name-directory (buffer-file-name))
139                   (concat "cd " (file-name-directory (buffer-file-name))))
140              (and make-cd
141                   list-buffers-directory
142                   (concat "cd " list-buffers-directory)))))
143     ;; Switch to an existin eshell if one exists, otherwise switch to
144     ;; another window and start a new eshell
145     (if eshell-buffer
146         (switch-to-buffer-other-window eshell-buffer)
147       (eshell-toggle-buffer-switch-to-other-window)
148       ;; Sometimes an error is generated when I call `eshell' (it has
149       ;; to do with my eshell-mode-hook which inserts text into the
150       ;; newly created eshell-buffer and thats not allways a good
151       ;; idea).
152       (condition-case the-error
153           (eshell)
154         (error (switch-to-buffer "*eshell*"))))
155     (if (or cd-command eshell-toggle-goto-eob)
156         (goto-char (point-max)))
157     (if cd-command
158         (progn
159           (insert cd-command)
160           (if eshell-toggle-automatic-cd
161               (eshell-send-input))))))
162
163 (defun eshell-toggle-buffer-switch-to-other-window ()
164   "Switches to other window.
165 If the current window is the only window in the current frame, create
166 a new window and switch to it.  (This is less intrusive to the current
167 window configuration then `switch-buffer-other-window')"
168   (let ((this-window (selected-window)))
169     (other-window 1)
170     ;; If we did not switch window then we only have one window and
171     ;; need to create a new one.
172     (if (eq this-window (selected-window))
173         (progn
174           (split-window-vertically)
175           (other-window 1)))))
176
177 (provide 'esh-toggle)
178
179 ;;; esh-toggle.el ends here