Customise terminal behaviour

This commit is contained in:
Wojciech Kozlowski 2017-08-30 22:25:15 +01:00
parent f402e72fda
commit c155c2b7c4

View File

@ -18,7 +18,7 @@
(setq init-packages/terminal-packages (setq init-packages/terminal-packages
'(shell-pop) '()
) )
@ -53,43 +53,58 @@
(setq term-buffer-maximum-size 0) (setq term-buffer-maximum-size 0)
:config :config
(defun ansi-term-pop (buf) (defun ansi-term-pop (term-cmd)
"Launch terminal in BUF, preferably in other window. "Launch terminal in (preferably) other window."
This function will keep the default directory of the current (let ((ansi-buf nil)
buffer" (cur-buf (current-buffer)))
(let ((term-cmd "/bin/zsh") (setq ansi-buf (ansi-term term-cmd))
(dir default-directory)) (switch-to-buffer cur-buf)
(pop-to-buffer buf) (switch-to-buffer-other-window ansi-buf)))
(let ((default-directory dir))
(unless (term-check-proc buf) (defun ansi-term-recycle (term-cmd)
(when (string= "term-mode" major-mode) "Kill current buffer and start an *ansi-term* in it."
(kill-buffer buf)) (kill-buffer (current-buffer))
(ansi-term term-cmd))))) (ansi-term term-cmd))
(defun buffer-mode (buffer)
"Returns the major mode associated with a buffer."
(with-current-buffer buffer
major-mode))
(defun first-matching-buffer (regex)
"Find first buffer whose name matches REGEXP."
(car (remove-if-not
(apply-partially #'string-match-p regex)
(mapcar 'buffer-name (buffer-list)))))
(defun visit-ansi-term () (defun visit-ansi-term ()
"If the current buffer is: "Open or switch to active ansi-term.
1) a running ansi-term named *ansi-term*, run a new one. If current buffer is a term:
2) a stopped ansi-term, kill it and create a new one. If it is running
3) a non ansi-term, go to an already running ansi-term Open a new ansi-term in a new window
or start a new one while killing a defunct one" If it is not running
Recycle (kill buffer, restart term)
If current buffer is not a term:
If a *ansi-term*<x> buffer exists
Switch to that ansi-term in other window
Recycle if necessary
If it does not exist
Open a new ansi-term in a new window"
(interactive) (interactive)
(let* ((is-term (string= "term-mode" major-mode)) (let ((is-term (string= "term-mode" major-mode))
(is-running (term-check-proc (buffer-name))) (is-running (term-check-proc (buffer-name)))
(term-cmd "/bin/zsh") (term-cmd "/bin/zsh")
(buf-name "*ansi-term*") (anon-term (first-matching-buffer "^*ansi-term*")))
(anon-term (get-buffer buf-name)))
(if is-term (if is-term
(if is-running (if is-running
(if (string-match buf-name (buffer-name)) (ansi-term-pop term-cmd)
(ansi-term-pop (other-buffer (current-buffer) 'visible-ok)) (ansi-term-recycle term-cmd))
(if anon-term
(switch-to-buffer buf-name)
(ansi-term term-cmd)))
(kill-buffer (buffer-name))
(ansi-term term-cmd))
(if anon-term (if anon-term
(ansi-term-pop buf-name) (progn
(ansi-term-pop (other-buffer (current-buffer) 'visible-ok)))))) (switch-to-buffer-other-window anon-term)
(unless (term-check-proc (buffer-name))
(ansi-term-recycle term-cmd)))
(ansi-term-pop term-cmd)))))
(global-set-key (kbd "C-x '") 'visit-ansi-term)) (global-set-key (kbd "C-x '") 'visit-ansi-term))