Merge branch 'master' of git.ganneff.de:emacs
[emacs.git] / .emacs.d / elisp / local / sh-toggle.el
1 ;;; sh-toggle --- toggle to and from the *shell* buffer
2
3 ;; Copyright (C) 1997, 1998, 2000, 2001 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.gci-net.com/users/j/johnw/shell.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 shell-toggle which toggles between the
30 ;; *shell* buffer and whatever buffer you are editing.
31 ;;
32 ;; This is done in an "intelligent" way. Features are:
33 ;;
34 ;; - Starts a shell if non is existing.
35 ;;
36 ;; - Minimum distortion of your window configuration.
37 ;;
38 ;; - When done in the shell-buffer you are returned to the same
39 ;; window configuration you had before you toggled to the shell.
40 ;;
41 ;; - If you desire, you automagically get a "cd" command in the
42 ;; shell to the directory where your current buffers file exists;
43 ;; just call shell-toggle-cd instead of shell-toggle.
44 ;;
45 ;; - You can convinently choose if you want to have the shell in
46 ;; another window or in the whole frame. Just invoke shell-toggle
47 ;; again to get the shell in the whole frame.
48 ;;
49 ;; This file has been tested under Emacs 20.2.
50 ;;
51 ;; To use, call the functions `shell-toggle' or `shell-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 shell-toggle-goto-eob t
62 "*If non-nil `shell-toggle' moves point to end of Shell buffer.
63 When `shell-toggle-cd' is called the point is always moved to the
64 end of the shell-buffer")
65
66 (defvar shell-toggle-automatic-cd t
67 "*If non-nil `shell-toggle-cd' will send a \"cd\" to Shell.
68 If nil `shell-toggle-cd' will only insert the \"cd\" command in the
69 shell-buffer. Leaving it to the user to press RET to send the
70 command to the shell.")
71
72 ;;; User Functions:
73
74 ;;;###autoload
75 (defun shell-toggle-cd ()
76 "Calls `shell-toggle' with a prefix argument.
77 See the command `shell-toggle'"
78 (interactive)
79 (shell-toggle t))
80
81 ;;;###autoload
82 (defun shell-toggle (make-cd)
83 "Toggles between the *shell* buffer and the current buffer.
84 With a prefix ARG also insert a \"cd DIR\" command into the shell,
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 *shell*
88 buffer.
89
90 When called in the *shell* buffer returns you to the buffer you were
91 editing before caling the first time.
92
93 Options: `shell-toggle-goto-eob'"
94 (interactive "P")
95 ;; Try to descide on one of three possibilities:
96 ;; 1. If not in shell-buffer, switch to it.
97 ;; 2. If in shell-buffer and called twice in a row, delete other
98 ;; windows
99 ;; 3. If in shell-buffer and not called twice in a row, return to
100 ;; state before going to the shell-buffer
101 (if (eq major-mode 'shell-mode)
102 (if (and (or (eq last-command 'shell-toggle)
103 (eq last-command 'shell-toggle-cd))
104 (not (eq (count-windows) 1)))
105 (delete-other-windows)
106 (shell-toggle-buffer-return-from-shell))
107 (shell-toggle-buffer-goto-shell make-cd)))
108
109 ;;; Internal Functions:
110
111 (defvar shell-toggle-pre-shell-win-conf nil
112 "Contains window config before the *shell* buffer was selected")
113
114 (defun shell-toggle-buffer-return-from-shell ()
115 "Restores window config used before switching the *shell* buffer.
116 If no configuration has been stored, just bury the *shell* buffer."
117 (if (window-configuration-p shell-toggle-pre-shell-win-conf)
118 (progn
119 (set-window-configuration shell-toggle-pre-shell-win-conf)
120 (setq shell-toggle-pre-shell-win-conf nil)
121 (bury-buffer (get-buffer "*shell*")))
122 (bury-buffer)))
123
124 (defun shell-toggle-buffer-goto-shell (make-cd)
125 "Switches other window to the *shell* buffer.
126 If no *shell* buffer exists start a new shell and switch to it in
127 other window. If argument MAKE-CD is non-nil, insert a \"cd DIR\"
128 command into the shell, where DIR is the directory of the current
129 buffer.
130 Stores the window cofiguration before creating and/or switching window."
131 (setq shell-toggle-pre-shell-win-conf (current-window-configuration))
132 (let ((shell-buffer (get-buffer "*shell*"))
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 shell if one exists, otherwise switch to
144 ;; another window and start a new shell
145 (if shell-buffer
146 (switch-to-buffer-other-window shell-buffer)
147 (shell-toggle-buffer-switch-to-other-window)
148 ;; Sometimes an error is generated when I call `shell' (it has
149 ;; to do with my shell-mode-hook which inserts text into the
150 ;; newly created shell-buffer and thats not allways a good
151 ;; idea).
152 (condition-case the-error
153 (shell)
154 (error (switch-to-buffer "*shell*"))))
155 (if (or cd-command shell-toggle-goto-eob)
156 (goto-char (point-max)))
157 (if cd-command
158 (progn
159 (insert cd-command)
160 (if shell-toggle-automatic-cd
161 (comint-send-input))))))
162
163 (defun shell-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 'sh-toggle)
178
179 ;;; sh-toggle.el ends here