Download
(require 'term)
(defvar term-toggle-goto-eob t
"*If non-nil `term-toggle' will move point to the end of the shell-buffer
whenever the `term-toggle' switched to the shell-buffer.
When `term-toggle-cd' is called the point is allways moved to the end of the
shell-buffer")
(defvar term-toggle-automatic-cd t
"*If non-nil `term-toggle-cd' will send the \"cd\" command to the shell.
If nil `term-toggle-cd' will only insert the \"cd\" command in the
shell-buffer. Leaving it to the user to press RET to send the command to
the shell.")
(defun term-toggle-cd ()
"Calls `term-toggle' with a prefix argument. Se command `term-toggle'"
(interactive)
(term-toggle t))
(defun term-toggle (make-cd)
"Toggles between the *terminal* buffer and whatever buffer you are editing.
With a prefix ARG also insert a \"cd DIR\" command into the shell, where DIR is
the directory of the current buffer.
Call twice in a row to get a full screen window for the *terminal* buffer.
When called in the *terminal* buffer returns you to the buffer you were editing
before caling the first time.
Options: `term-toggle-goto-eob'"
(interactive "P")
(if (eq major-mode 'term-mode)
(if (and (or (eq last-command 'term-toggle)
(eq last-command 'term-toggle-cd))
(not (eq (count-windows) 1)))
(delete-other-windows)
(term-toggle-buffer-return-from-shell))
(term-toggle-buffer-goto-shell make-cd)))
(defvar term-toggle-pre-shell-win-conf nil
"Contains the window configuration before the *terminal* buffer was selected")
(defun term-toggle-buffer-return-from-shell ()
"Restores the window configuration used before switching the *terminal* buffer.
If no configuration has been stored, just burry the *terminal* buffer."
(if (window-configuration-p term-toggle-pre-shell-win-conf)
(progn
(set-window-configuration term-toggle-pre-shell-win-conf)
(setq term-toggle-pre-shell-win-conf nil)
(bury-buffer (get-buffer "*terminal*")))
(bury-buffer))
)
(defun term-toggle-buffer-goto-shell (make-cd)
"Switches other window to the *terminal* buffer. If no *terminal* buffer exists
start a new shell and switch to it in other window. If argument MAKE-CD is
non-nil, insert a \"cd DIR\" command into the shell, where DIR is the directory
of the current buffer.
Stores the window cofiguration before creating and/or switching window."
(setq term-toggle-pre-shell-win-conf (current-window-configuration))
(let ((shell-buffer (get-buffer "*terminal*"))
(cd-command
(or (and make-cd
(buffer-file-name)
(file-name-directory (buffer-file-name))
(concat "cd " (file-name-directory (buffer-file-name))))
(and make-cd
list-buffers-directory
(concat "cd " list-buffers-directory)))))
(if shell-buffer
(switch-to-buffer-other-window shell-buffer)
(term-toggle-buffer-switch-to-other-window)
(condition-case the-error
(term (getenv "SHELL"))
(error (switch-to-buffer "*terminal*"))))
(if (or cd-command term-toggle-goto-eob)
(term-send-del))
(if (and cd-command term-toggle-automatic-cd)
(term-send-raw-string (concat cd-command "\n"))
)))
(defun term-toggle-buffer-switch-to-other-window ()
"Switches to other window. If the current window is the only window in the
current frame, create a new window and switch to it.
\(This is less intrusive to the current window configuration then
`switch-buffer-other-window')"
(let ((this-window (selected-window)))
(other-window 1)
(if (eq this-window (selected-window))
(progn
(split-window-vertically)
(other-window 1)))))
(provide 'term-toggle)