summaryrefslogtreecommitdiffhomepage
path: root/lib/prog/jao-vterm-repl.el
blob: 699ff3951274a9fa19a4ed0cf78adb406d077705 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
;;; jao-vterm-repl.el --- vterm-based repls          -*- lexical-binding: t; -*-

;; Copyright (C) 2020, 2021  jao

;; Author: jao <mail@jao.io>
;; Keywords: terminals

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;; Helpers to launch reply things such as erlang shells inside a vterm.
;; For instance, to declare an erl repl for rebar projects, one would call:
;;
;;    (jao-vterm-repl-register "rebar.config" "rebar3 shell" "^[0-9]+> ")

;;; Code:

(require 'jao-compilation)

(declare-function 'vterm-copy-mode "vterm")
(declare-function 'vterm-send-string "vterm")
(declare-function 'vterm-send-return "vterm")

(defun jao-vterm-repl--buffer-name (&optional dir)
  (format "*vterm -- repl - %s*" (or dir (jao-compilation-root))))

(defvar jao-vterm-repl-repls nil)
(defvar jao-vterm-repl-prompts nil)
(defvar-local jao-vterm-repl--name nil)
(defvar-local jao-vterm-repl--last-buffer nil)
(defvar-local jao-vterm-repl--prompt-rx "^[0-9]+> ")

(setq vterm-buffer-name-string nil)

(defun jao-vterm-repl--exec (cmd &optional name)
  (vterm name)
  (when name
    (vterm-send-string "unset PROMPT_COMMAND\n\n"))
  (vterm-send-string cmd)
  (vterm-send-return)
  (when name (rename-buffer name t)))

;;;###autoload
(defun jao-vterm-repl-previous-prompt ()
  (interactive)
  (when (derived-mode-p 'vterm-mode)
    (vterm-copy-mode 1)
    (forward-line 0)
    (when (re-search-backward jao-vterm-repl--prompt-rx nil t)
      (goto-char (match-end 0)))))

;;;###autoload
(defun jao-vterm-repl-next-prompt ()
  (interactive)
  (when (derived-mode-p 'vterm-mode)
    (vterm-copy-mode 1)
    (or (re-search-forward jao-vterm-repl--prompt-rx nil t)
        (vterm-copy-mode -1))
    (unless (save-excursion
              (re-search-forward jao-vterm-repl--prompt-rx nil t))
      (vterm-copy-mode -1))))

;;;###autoload
(define-minor-mode jao-vterm-repl-mode "repl-aware vterm" nil nil
  '(("\C-c\C-p" . jao-vterm-repl-previous-prompt)
    ("\C-c\C-n" . jao-vterm-repl-next-prompt)
    ("\C-c\C-z" . jao-vterm-repl-pop-to-src)))

;;;###autoload
(defun jao-vterm-repl ()
  (let* ((dir (jao-compilation-root))
         (vname (jao-vterm-repl--buffer-name dir))
         (root-name (jao-compilation-root-file))
         (buffer (seq-find `(lambda (b)
                              (string=
                               (buffer-local-value 'jao-vterm-repl--name
                                                   b)
                               ,vname))
                           (buffer-list))))
    (or buffer
        (let ((default-directory dir)
              (prompt (cdr (assoc root-name jao-vterm-repl-prompts)))
              (cmd (or (cdr (assoc root-name jao-vterm-repl-repls))
                       (read-string "REPL command: ")))
              (bname (format "* vrepl - %s/%s *"
                             (file-name-base (string-remove-suffix "/" dir))
                             root-name)))
          (jao-vterm-repl--exec cmd bname)
          (jao-vterm-repl-mode)
          (setq-local jao-vterm-repl--name vname)
          (when prompt (setq-local jao-vterm-repl--prompt-rx prompt))
          (current-buffer)))))

;;;###autoload
(defun jao-vterm-repl-register (build-file repl-cmd prompt-rx)
  (jao-compilation-add-dominating build-file)
  (add-to-list 'jao-vterm-repl-repls (cons build-file repl-cmd))
  (add-to-list 'jao-vterm-repl-prompts (cons build-file prompt-rx)))

;;;###autoload
(defun jao-vterm-repl-pop-to-repl ()
  (interactive)
  (let ((bn (current-buffer)))
    (pop-to-buffer (jao-vterm-repl))
    (setq-local jao-vterm-repl--last-buffer bn)))

;;;###autoload
(defun jao-vterm-repl-pop-to-src ()
  (interactive)
  (when (buffer-live-p jao-vterm-repl--last-buffer)
    (pop-to-buffer jao-vterm-repl--last-buffer)))

;;;###autoload
(defun jao-vterm-repl-send (cmd)
  (with-current-buffer (jao-vterm-repl) (vterm-send-string cmd)))

(provide 'jao-vterm-repl)
;;; jao-vterm-repl.el ends here