This repository has been archived on 2022-11-18. You can view files and clone it, but cannot push or open issues or pull requests.
emacs/modules/terminal.el

174 lines
5.2 KiB
EmacsLisp
Raw Permalink Normal View History

;;; terminal.el --- Module file for terminal configuration.
2017-08-30 02:14:25 +02:00
;;
;; Copyright (C) 2017-2019 Wojciech Kozlowski
2017-08-30 02:14:25 +02:00
;;
2018-02-04 18:18:18 +01:00
;; Author: Wojciech Kozlowski <wk@wojciechkozlowski.eu>
;; Created: 2017-08-28
2017-08-30 02:14:25 +02:00
;;
;; This file is not part of GNU Emacs.
;;
;;; Commentary:
;;
;; This module sets up packages and configuration for working with a terminal
;; in Emacs.
;;
;;; License: GPLv3
;;; Required packages:
2018-07-07 22:34:00 +02:00
;;; Code:
2017-08-30 02:14:25 +02:00
(defvar emodule/terminal-packages
2017-08-30 02:14:25 +02:00
2019-08-07 12:10:37 +02:00
'(vterm)
2018-07-07 22:34:00 +02:00
)
2017-08-30 02:14:25 +02:00
;; Configuration:
(defun emodule/terminal-init ()
"Initialise the `terminal' module."
2017-08-30 02:14:25 +02:00
2020-11-22 11:17:49 +01:00
;; -----------------------------------------------------------------------------------------------
;; `eshell'
2020-11-22 11:17:49 +01:00
;; -----------------------------------------------------------------------------------------------
2018-02-08 07:55:21 +01:00
2018-02-09 20:25:25 +01:00
(use-package eshell
2018-02-11 22:01:12 +01:00
:config
(defun eshell-pop (name)
"Launch terminal in (preferably) other window."
(let ((esh-buf nil)
(cur-buf (current-buffer)))
(setq esh-buf (eshell name))
(switch-to-buffer cur-buf)
(switch-to-buffer-other-window esh-buf)))
2018-02-09 20:25:25 +01:00
(defun eshell-here ()
(interactive)
2018-02-11 22:01:12 +01:00
(let* ((parent default-directory)
2018-02-09 20:25:25 +01:00
(name (car
(last
2018-02-11 22:01:12 +01:00
(split-string parent "/" t))))
(esh-buf-name (concat "*eshell: " name "*"))
(is-esh (string= "eshell-mode" major-mode))
(esh-buf (first-matching-buffer esh-buf-name)))
(unless is-esh
(if esh-buf
(switch-to-buffer-other-window esh-buf)
(progn
(eshell-pop "new")
(rename-buffer esh-buf-name))))
2018-02-09 20:25:25 +01:00
(insert (concat "ls"))
(eshell-send-input)))
(defun delete-single-window (&optional window)
"Remove WINDOW from the display. Default is `selected-window'.
If WINDOW is the only one in its frame, then `delete-frame' too."
(interactive)
(save-current-buffer
(setq window (or window (selected-window)))
(select-window window)
(kill-buffer)
(if (one-window-p t)
(delete-frame)
(delete-window (selected-window)))))
(defun eshell/x (&rest args)
(delete-single-window))
(defun eshell-setup ()
(interactive)
(define-key eshell-mode-map (kbd "C-0") 'delete-window))
(add-hook 'eshell-mode-hook 'eshell-setup t)
2018-02-11 22:01:12 +01:00
(global-set-key (kbd "C-x /") 'eshell-here))
2018-02-11 19:06:17 +01:00
(use-package em-smart
:config
(setq eshell-where-to-jump 'begin)
(setq eshell-review-quick-commands nil)
(setq eshell-smart-space-goes-to-end t))
(use-package em-term
:config
(setq eshell-visual-commands (nconc eshell-visual-commands '("htop"
"tmux"))))
2020-11-22 11:17:49 +01:00
;; -----------------------------------------------------------------------------------------------
2019-08-07 12:10:37 +02:00
;; `vterm'
2020-11-22 11:17:49 +01:00
;; -----------------------------------------------------------------------------------------------
2019-08-07 12:10:37 +02:00
(use-package vterm
:init
(setq vterm-shell "/bin/zsh"
vterm-max-scrollback 10000)
(defun x-vterm-setup ()
(define-key vterm-mode-map
2019-12-15 16:03:52 +01:00
[remap whole-line-or-region-yank] 'vterm-yank)
(define-key vterm-mode-map (kbd "C-S-v") 'vterm-yank)
(define-key vterm-mode-map
[remap scroll-up-command] 'vterm--self-insert)
(define-key vterm-mode-map
[remap scroll-down-command] 'vterm--self-insert))
2019-08-07 12:10:37 +02:00
(defun x-vterm-recycle ()
"Kill current buffer and start a vterm in it."
(let ((working-directory default-directory))
(kill-buffer (current-buffer))
(let ((default-directory working-directory))
(vterm))))
2019-12-15 16:03:52 +01:00
(defun x-vterm-other-window ()
"Start vterm in other window unless in initial buffer."
(interactive)
(if (equal major-mode 'init-buffer-mode)
(vterm)
(vterm-other-window)))
2019-08-07 12:10:37 +02:00
(defun visit-vterm ()
"Open or switch to active vterm.
If current buffer is a vterm:
If it is running
Open a new vterm in a new window
If it is not running
Recycle (kill buffer, restart vterm)
If current buffer is not a vterm:
If a buffer in vterm-mode exists
Switch to that buffer in other window
Recycle if necessary
If it does not exist
Open a new vterm in a new window"
(interactive)
(if (string= "vterm-mode" major-mode)
(if (term-check-proc (buffer-name))
2021-08-23 19:18:41 +02:00
(vterm-other-window t)
2019-08-07 12:10:37 +02:00
(x-vterm-recycle))
(let ((anon-term (seq-find (lambda (buffer)
(with-current-buffer buffer
(string= "vterm-mode" major-mode)))
(buffer-list))))
(if anon-term
(progn
2019-12-15 16:03:52 +01:00
(if (equal major-mode 'init-buffer-mode)
(switch-to-buffer anon-term)
(switch-to-buffer-other-window anon-term))
2019-08-07 12:10:37 +02:00
(unless (term-check-proc (buffer-name))
(x-vterm-recycle)))
2019-12-15 16:03:52 +01:00
(x-vterm-other-window)))))
2019-08-07 12:10:37 +02:00
:hook
(vterm-mode . x-vterm-setup)
:bind
(("C-x C-'" . vterm)
("C-x '" . visit-vterm)))
2017-08-30 02:14:25 +02:00
)
2018-07-07 22:34:00 +02:00
(provide 'emodule/terminal)
;;; terminal.el ends here