Skip to content

Commit

Permalink
Use 'names' to simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienCassou committed Dec 11, 2014
1 parent b755ca2 commit 984a2f8
Showing 1 changed file with 88 additions and 84 deletions.
172 changes: 88 additions & 84 deletions shell-switcher.el
Expand Up @@ -44,15 +44,18 @@

(require 'rswitcher)

(defconst sswitcher--ring (rswitcher-make))
;;;###autoload
(define-namespace shell-switcher-

(defconst -ring (rswitcher-make))

(defgroup shell-switcher nil
"Handling multiple shells"
:group 'extensions
:group 'convenience)

;;;###autoload
(defcustom shell-switcher-new-shell-function 'shell-switcher-make-eshell
:autoload
(defcustom new-shell-function #'make-eshell
"This variable references a function used to create new shells.
The function must take 0 arguments and return a newly created
shell buffer. `shell-switcher-make-shell',
Expand All @@ -61,40 +64,40 @@ are possible functions."

:group 'shell-switcher)

;;;###autoload
(defcustom shell-switcher-ask-before-creating-new nil
:autoload
(defcustom ask-before-creating-new nil
"If non-nil ask the user before creating a new shell buffer.
A new shell buffer is automatically created if there are no
buffers to switch to and this variable is set to nil."
:type 'boolean
:group 'shell-switcher)

;;;###autoload
(defcustom shell-switcher-ansi-term-shell ""
:autoload
(defcustom ansi-term-shell ""
"If non-empty use this shell with `ansi-term'.
Otherwise the shell will be chosen based on the environment with
a fallback to /bin/sh"
:type 'string
:group 'shell-switcher)

(defvar sswitcher--starting-default-directory nil
(defvar -starting-default-directory nil
"Backup `default-directory' for the next shell creation.
This variable is set to `default-directory' when starting to
switch. Then, if the user is done switching but asks for a new
shell, we suppose that the user's intent is to open a new shell
in the context that was active when he started switching.")

;;;###autoload
(defvar shell-switcher-mode-map
:autoload
(defvar mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-'") 'shell-switcher-switch-buffer)
(define-key map (kbd "C-x 4 '") 'shell-switcher-switch-buffer-other-window)
(define-key map (kbd "C-M-'") 'shell-switcher-new-shell)
(define-key map (kbd "C-'") #'switch-buffer)
(define-key map (kbd "C-x 4 '") #'switch-buffer-other-window)
(define-key map (kbd "C-M-'") #'new-shell)
map)
"Keymap to use in shell-switcher mode.")
"Keymap to use in shell-switcher mode.")

;;;###autoload
(define-minor-mode shell-switcher-mode
:autoload
(define-minor-mode mode
"Toggle shell-switcher mode.
Interactively with no argument, this command toggles the mode. A
positive prefix argument enables the mode, any other prefix
Expand All @@ -113,29 +116,29 @@ see commands \\[shell-switcher-switch-buffer-other-window] and
:init-value nil
;; nothing in the mode line:
:lighter nil
:keymap shell-switcher-mode-map)
:keymap mode-map)

(defun shell-switcher-make-shell ()
(defun make-shell ()
"Ensure the creation of a new `shell'.
This function is to be used as value for
`shell-switcher-new-shell-function'."
(shell (generate-new-buffer-name "*shell*")))

(defun shell-switcher-make-eshell ()
(defun make-eshell ()
"Ensure the creation of a new `eshell'.
This function is to be used as value for
`shell-switcher-new-shell-function'."
(eshell t))

(defun shell-switcher-make-ansi-term ()
(defun make-ansi-term ()
"Ensure the creation of a new `ansi-term'.
Will use the shell defined in `shell-switcher-ansi-term-shell' or
get it from the environment, falling back to /bin/sh. Installs a
sentinel to detect when the user exits the shell and kills the
buffer automatically. This function is to be used as value for
`shell-switcher-new-shell-function'."
(let ((shell (or (unless (string= "" shell-switcher-ansi-term-shell)
shell-switcher-ansi-term-shell)
(let ((shell (or (unless (string= "" ansi-term-shell)
ansi-term-shell)
(getenv "ESHELL")
(getenv "SHELL")
"/bin/sh")))
Expand All @@ -149,44 +152,44 @@ buffer automatically. This function is to be used as value for
(when (string-match "\\(finished\\|exited\\)" change)
(kill-buffer (process-buffer proc)))))))))

(defun sswitcher--most-recent ()
(defun -most-recent ()
"Return the most recently accessed shell."
(rswitcher-most-recent sswitcher--ring))
(rswitcher-most-recent -ring))

(defun sswitcher--most-recent-shell-valid-p ()
(defun -most-recent-shell-valid-p ()
"Check that the most recently created shell can still be accessed."
(buffer-live-p (sswitcher--most-recent)))
(buffer-live-p (-most-recent)))

(defun sswitcher--clean-buffers ()
(defun -clean-buffers ()
"Remove all shell buffers until we find a valid one."
(while (and (not (rswitcher-empty-p sswitcher--ring))
(not (sswitcher--most-recent-shell-valid-p)))
(rswitcher-delete-most-recent sswitcher--ring)))
(while (and (not (rswitcher-empty-p -ring))
(not (-most-recent-shell-valid-p)))
(rswitcher-delete-most-recent -ring)))

(defun shell-switcher-kill-all-shells ()
(defun kill-all-shells ()
"Remove all shell buffers."
(while (not (rswitcher-empty-p sswitcher--ring))
(kill-buffer (rswitcher-most-recent sswitcher--ring))
(rswitcher-delete-most-recent sswitcher--ring)))
(while (not (rswitcher-empty-p -ring))
(kill-buffer (rswitcher-most-recent -ring))
(rswitcher-delete-most-recent -ring)))

(defun sswitcher--shell-exist-p ()
(defun -shell-exist-p ()
"Check that there is at least one valid shell to switch to."
(sswitcher--clean-buffers)
(not (rswitcher-empty-p sswitcher--ring)))
(-clean-buffers)
(not (rswitcher-empty-p -ring)))

(defun sswitcher--in-shell-buffer-p ()
(defun -in-shell-buffer-p ()
"Check that the current buffer is a shell buffer."
(rswitcher-memq sswitcher--ring (current-buffer)))
(rswitcher-memq -ring (current-buffer)))

(defun shell-switcher-manually-register-shell ()
(defun manually-register-shell ()
"Register the current buffer in shell-switcher.
Must be executed manually or from a shell mode hook when the
current buffer is a shell that has been created without using a
shell-switcher function."
(interactive)
(rswitcher-add sswitcher--ring (current-buffer)))
(rswitcher-add -ring (current-buffer)))

(defun sswitcher--new-shell (&optional other-window)
(defun -new-shell (&optional other-window)
"Create and display a new shell.
If OTHER-WINDOW is nil (the default), the new shell buffer is
displayed in the current window. If OTHER-WINDOW is t, change
Expand All @@ -195,26 +198,26 @@ another window.
This function uses `shell-switcher-new-shell-function' to decide
what kind of shell to create."
(save-window-excursion
(rswitcher-add sswitcher--ring
(funcall shell-switcher-new-shell-function)))
(sswitcher--display-shell-buffer other-window))
(rswitcher-add -ring
(funcall new-shell-function)))
(-display-shell-buffer other-window))

(defun sswitcher--no-more-shell-buffers (&optional other-window)
(defun -no-more-shell-buffers (&optional other-window)
"Create a new shell as there is no more to switch to.
If `shell-switcher-ask-before-creating-new' is non-nil, ask the
user first. If the answer is positive or the user was not asked,
a new shell buffer is created. If OTHER-WINDOW is nil (the
default), the shell buffer is displayed in the current window. If
OTHER-WINDOW is t, change another window."
(when (or (not shell-switcher-ask-before-creating-new)
(when (or (not ask-before-creating-new)
(y-or-n-p "No active shell buffer, create new one? "))
(let ((default-directory (or sswitcher--starting-default-directory default-directory)))
(sswitcher--new-shell other-window)
(setq sswitcher--starting-default-directory nil))))
(let ((default-directory (or -starting-default-directory default-directory)))
(-new-shell other-window)
(setq -starting-default-directory nil))))

(when (not (fboundp 'set-temporary-overlay-map))
;; Backport this function from newer emacs versions
(defun set-temporary-overlay-map (map &optional keep-pred)
(defun ::set-temporary-overlay-map (map &optional keep-pred)
"Set a new keymap that will only exist for a short period of time.
The new keymap to use must be given in the MAP variable. When to
remove the keymap depends on user input and KEEP-PRED:
Expand Down Expand Up @@ -250,7 +253,7 @@ remove the keymap depends on user input and KEEP-PRED:

(push alist emulation-mode-map-alists))))

(defun sswitcher--prepare-for-fast-key ()
(defun -prepare-for-fast-key ()
"Set a keymap so that one can switch buffers by pressing 1 key.
The key to be pressed to continue switching buffers is the last
key of the most recent key sequence. See
Expand All @@ -263,22 +266,22 @@ key is pressed, calls `sswitcher--switch-partially'."
(let ((map (make-sparse-keymap)))
(define-key map (vector repeat-key)
`(lambda () (interactive)
(sswitcher--switch-partially)
(shell-switcher--switch-partially)
(message ,message)))
map) t)
(message message)))

(defun sswitcher--display-shell-buffer (&optional other-window)
(defun -display-shell-buffer (&optional other-window)
"Display the most recently accessed shell buffer.
If OTHER-WINDOW is nil (the default), change the current window.
If OTHER-WINDOW is t, change another window."
(if (sswitcher--shell-exist-p)
(if (-shell-exist-p)
(if other-window
(switch-to-buffer-other-window (sswitcher--most-recent) t)
(switch-to-buffer (sswitcher--most-recent) t))
(switch-to-buffer-other-window (-most-recent) t)
(switch-to-buffer (-most-recent) t))
(message "No shell buffer to display")))

(defun sswitcher--switch-buffer (&optional other-window)
(defun -switch-buffer (&optional other-window)
"Switch to the most recently accessed buffer.
Switch to the most recently accessed shell buffer that is not the
current one. Pressing the last key of the key sequence that call
Expand All @@ -294,18 +297,18 @@ If there is no shell buffer or if the only shell buffer is the
current buffer, propose the creation of a new shell buffer and
display it in the current window (if OTHER-WINDOW is nil, the
default) or the other window (if OTHER-WINDOW is t)."
(setq sswitcher--starting-default-directory default-directory)
(if (or (not (sswitcher--shell-exist-p))
(and (= (rswitcher-length sswitcher--ring) 1)
(sswitcher--in-shell-buffer-p)))
(sswitcher--no-more-shell-buffers other-window)
(when (sswitcher--in-shell-buffer-p)
(rswitcher-switch-full sswitcher--ring))
(sswitcher--display-shell-buffer other-window)
(sswitcher--prepare-for-fast-key)))

;;;###autoload
(defun shell-switcher-switch-buffer ()
(setq -starting-default-directory default-directory)
(if (or (not (-shell-exist-p))
(and (= (rswitcher-length -ring) 1)
(-in-shell-buffer-p)))
(-no-more-shell-buffers other-window)
(when (-in-shell-buffer-p)
(rswitcher-switch-full -ring))
(-display-shell-buffer other-window)
(-prepare-for-fast-key)))

:autoload
(defun switch-buffer ()
"Switch to the most recently accessed buffer.
Switch to the most recently accessed shell buffer that is not the
current one. Pressing the last key of the key sequence that call
Expand All @@ -317,34 +320,35 @@ buffers (this is actually done by `sswitcher--switch-partially'.
If there is no shell buffer or if the only shell buffer is the
current buffer, propose the creation of a new shell buffer."
(interactive)
(sswitcher--switch-buffer))
(-switch-buffer))

(defun sswitcher--switch-partially ()
(defun -switch-partially ()
"Switch to the next most recently accessed buffer.
This function is indirectly called by
`shell-switcher-switch-buffer' after pressingthe last key of the
most recent key sequence."
(sswitcher--clean-buffers)
(if (< (rswitcher-length sswitcher--ring) 2)
(sswitcher--no-more-shell-buffers)
(rswitcher-switch-partial sswitcher--ring)
(sswitcher--display-shell-buffer)))

;;;###autoload
(defun shell-switcher-switch-buffer-other-window ()
(-clean-buffers)
(if (< (rswitcher-length -ring) 2)
(-no-more-shell-buffers)
(rswitcher-switch-partial -ring)
(-display-shell-buffer)))

:autoload
(defun switch-buffer-other-window ()
"Switch to the most recently accessed buffer in another window.
Same as `shell-switcher-switch-buffer' but change another
window."
(interactive)
(sswitcher--switch-buffer t))
(-switch-buffer t))

;;;###autoload
(defun shell-switcher-new-shell ()
:autoload
(defun new-shell ()
"Unconditionaly create and display a new shell buffer."
(interactive)
(sswitcher--new-shell))
(-new-shell)))

(provide 'shell-switcher)

;;; shell-switcher.el ends here

0 comments on commit 984a2f8

Please sign in to comment.