#+TITLE: vterm

* Preamble
#+begin_src emacs-lisp
;;; vterm.el --- vterm -*- lexical-binding: t; -*-
#+end_src

* Load
#+begin_src emacs-lisp
(add-to-list 'load-path (expand-file-name "packages/vterm-94e2b0b" user-emacs-directory))
(require 'vterm)
#+end_src

* Saving vterm windows in desktop sessions

#+begin_src emacs-lisp
(defun circ/vterm-desktop-save (desktop-dirname)
  "Return state to save for a vterm buffer."
  (list (expand-file-name default-directory)))

(defun circ/vterm-desktop-restore (_file-name buffer-name misc)
  "Recreate a vterm buffer for desktop restore."
  (let ((dir (car misc)))
    (when (and dir (file-directory-p dir))
      (let ((default-directory dir))
        (vterm buffer-name)))))

(add-to-list 'desktop-buffer-mode-handlers '(vterm-mode . circ/vterm-desktop-restore))

(defun circ/vterm-mode-hook ()
  "Setup for vterm buffers."
  (setq-local desktop-save-buffer #'circ/vterm-desktop-save)
  ;; vterm sets a buffer-local display table, overriding standard-display-table.
  ;; Re-apply the space border character so it matches the rest of Emacs.
  (when buffer-display-table
    (set-display-table-slot buffer-display-table 'vertical-border ?\s)))

(add-hook 'vterm-mode-hook #'circ/vterm-mode-hook)
#+end_src

* Allow buffer name setting
#+begin_src emacs-lisp
(setq vterm-buffer-name-string "*vterm* %s")
#+end_src

* Keys
#+begin_src emacs-lisp
(with-eval-after-load 'vterm
  (define-key vterm-mode-map (kbd "S-<return>")
    (lambda () (interactive) (vterm-send-key "j" nil nil 'ctrl))))

(setq circ/p2-vterm-keybinds
      '(
	("q" vterm-send-next-key)
	("Q" vterm-send-escape)
	))

(with-eval-after-load 'vterm
  (circ/set-keybinds vterm-mode-map "M-d" circ/p2-vterm-keybinds))
#+end_src
