1 ;;; esh-toggle --- toggle to and from the *eshell* buffer
3 ;; Copyright (C) 1997, 1998 Mikael Sjödin (mic@docs.uu.se)
5 ;; Author: Mikael Sjödin <mic@docs.uu.se>
6 ;; John Wiegley <johnw@gnu.org>
7 ;; Created: 19 Nov 1998
10 ;; X-URL: http://www.emacs.org/~johnw/eshell.html
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.
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.
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.
29 ;; Provides the command eshell-toggle which toggles between the
30 ;; *eshell* buffer and whatever buffer you are editing.
32 ;; This is done in an "intelligent" way. Features are:
34 ;; - Starts a eshell if non is existing.
36 ;; - Minimum distortion of your window configuration.
38 ;; - When done in the eshell-buffer you are returned to the same
39 ;; window configuration you had before you toggled to the eshell.
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.
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.
49 ;; This file has been tested under Emacs 20.2.
51 ;; To use, call the functions `eshell-toggle' or `eshell-toggle-cd'.
52 ;; It's most helpful to bind these to a key.
56 ;; Christian Stern <Christian.Stern@physik.uni-regensburg.de> for
57 ;; helpful sugestions.
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")
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.")
75 (defun eshell-toggle-cd ()
76 "Calls `eshell-toggle' with a prefix argument.
77 See the command `eshell-toggle'"
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.
87 Call twice in a row to get a full screen window for the *eshell*
90 When called in the *eshell* buffer returns you to the buffer you were
91 editing before caling the first time.
93 Options: `eshell-toggle-goto-eob'"
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
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)))
109 ;;; Internal Functions:
111 (defvar eshell-toggle-pre-eshell-win-conf nil
112 "Contains window config before the *eshell* buffer was selected")
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)
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*")))
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
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*"))
134 ;; Find out which directory we are in (the method differs for
135 ;; different buffers)
138 (file-name-directory (buffer-file-name))
139 (concat "cd " (file-name-directory (buffer-file-name))))
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
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
152 (condition-case the-error
154 (error (switch-to-buffer "*eshell*"))))
155 (if (or cd-command eshell-toggle-goto-eob)
156 (goto-char (point-max)))
160 (if eshell-toggle-automatic-cd
161 (eshell-send-input))))))
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)))
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))
174 (split-window-vertically)
177 (provide 'esh-toggle)
179 ;;; esh-toggle.el ends here