jao-vterm-repl.el 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; jao-vterm-repl.el --- vterm-based repls -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2020, 2021 jao
  3. ;; Author: jao <mail@jao.io>
  4. ;; Keywords: terminals
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; Helpers to launch reply things such as erlang shells inside a vterm.
  17. ;; For instance, to declare an erl repl for rebar projects, one would call:
  18. ;;
  19. ;; (jao-vterm-repl-register "rebar.config" "rebar3 shell" "^[0-9]+> ")
  20. ;;; Code:
  21. (require 'jao-compilation)
  22. (declare-function 'vterm-copy-mode "vterm")
  23. (declare-function 'vterm-send-string "vterm")
  24. (declare-function 'vterm-send-return "vterm")
  25. (defun jao-vterm-repl--buffer-name (&optional dir)
  26. (format "*vterm -- repl - %s*" (or dir (jao-compilation-root))))
  27. (defvar jao-vterm-repl-repls nil)
  28. (defvar jao-vterm-repl-prompts nil)
  29. (defvar-local jao-vterm-repl--name nil)
  30. (defvar-local jao-vterm-repl--last-buffer nil)
  31. (defvar-local jao-vterm-repl--prompt-rx "^[0-9]+> ")
  32. (setq vterm-buffer-name-string nil)
  33. (defun jao-vterm-repl--exec (cmd &optional name)
  34. (vterm name)
  35. (when name
  36. (vterm-send-string "unset PROMPT_COMMAND\n\n"))
  37. (vterm-send-string cmd)
  38. (vterm-send-return)
  39. (when name (rename-buffer name t)))
  40. ;;;###autoload
  41. (defun jao-vterm-repl-previous-prompt ()
  42. (interactive)
  43. (when (derived-mode-p 'vterm-mode)
  44. (vterm-copy-mode 1)
  45. (forward-line 0)
  46. (when (re-search-backward jao-vterm-repl--prompt-rx nil t)
  47. (goto-char (match-end 0)))))
  48. ;;;###autoload
  49. (defun jao-vterm-repl-next-prompt ()
  50. (interactive)
  51. (when (derived-mode-p 'vterm-mode)
  52. (vterm-copy-mode 1)
  53. (or (re-search-forward jao-vterm-repl--prompt-rx nil t)
  54. (vterm-copy-mode -1))
  55. (unless (save-excursion
  56. (re-search-forward jao-vterm-repl--prompt-rx nil t))
  57. (vterm-copy-mode -1))))
  58. ;;;###autoload
  59. (define-minor-mode jao-vterm-repl-mode "repl-aware vterm" nil nil
  60. '(("\C-c\C-p" . jao-vterm-repl-previous-prompt)
  61. ("\C-c\C-n" . jao-vterm-repl-next-prompt)
  62. ("\C-c\C-z" . jao-vterm-repl-pop-to-src)))
  63. ;;;###autoload
  64. (defun jao-vterm-repl ()
  65. (let* ((dir (jao-compilation-root))
  66. (vname (jao-vterm-repl--buffer-name dir))
  67. (root-name (jao-compilation-root-file))
  68. (buffer (seq-find `(lambda (b)
  69. (string=
  70. (buffer-local-value 'jao-vterm-repl--name
  71. b)
  72. ,vname))
  73. (buffer-list))))
  74. (or buffer
  75. (let ((default-directory dir)
  76. (prompt (cdr (assoc root-name jao-vterm-repl-prompts)))
  77. (cmd (or (cdr (assoc root-name jao-vterm-repl-repls))
  78. (read-string "REPL command: ")))
  79. (bname (format "* vrepl - %s/%s *"
  80. (file-name-base (string-remove-suffix "/" dir))
  81. root-name)))
  82. (jao-vterm-repl--exec cmd bname)
  83. (jao-vterm-repl-mode)
  84. (setq-local jao-vterm-repl--name vname)
  85. (when prompt (setq-local jao-vterm-repl--prompt-rx prompt))
  86. (current-buffer)))))
  87. ;;;###autoload
  88. (defun jao-vterm-repl-register (build-file repl-cmd prompt-rx)
  89. (jao-compilation-add-dominating build-file)
  90. (add-to-list 'jao-vterm-repl-repls (cons build-file repl-cmd))
  91. (add-to-list 'jao-vterm-repl-prompts (cons build-file prompt-rx)))
  92. ;;;###autoload
  93. (defun jao-vterm-repl-pop-to-repl ()
  94. (interactive)
  95. (let ((bn (current-buffer)))
  96. (pop-to-buffer (jao-vterm-repl))
  97. (setq-local jao-vterm-repl--last-buffer bn)))
  98. ;;;###autoload
  99. (defun jao-vterm-repl-pop-to-src ()
  100. (interactive)
  101. (when (buffer-live-p jao-vterm-repl--last-buffer)
  102. (pop-to-buffer jao-vterm-repl--last-buffer)))
  103. ;;;###autoload
  104. (defun jao-vterm-repl-send (cmd)
  105. (with-current-buffer (jao-vterm-repl) (vterm-send-string cmd)))
  106. (provide 'jao-vterm-repl)
  107. ;;; jao-vterm-repl.el ends here