2020-10-04 14:57:18 +09:00
# -*- eval: (visual-line-mode 1) -*-
#+STARTUP : showall
2024-09-16 17:51:58 -04:00
[[https://melpa.org/#/py-vterm-interaction ][file:https://melpa.org/packages/py-vterm-interaction-badge.svg ]]
2020-10-04 14:57:18 +09:00
2024-09-12 11:35:26 -04:00
This is a port of [[https://github.com/shg/julia-vterm.el ][julia-vterm.el ]] with some modification to deal with
python (virtual) environments and play nicely with =ipython= and the
like.
Some advantages over the built-in python interaction is the ability to
run ~ipython~ with it's fancy prompt and make use of ~spyder~ -like code
cells (see below) or running the current function.
2020-10-04 14:57:18 +09:00
2024-05-12 21:03:32 -04:00
See also [[https://github.com/shg/python-vterm.el ][the original author's efforts ]].
2024-09-12 11:31:13 -04:00
* py-vterm-interaction
2024-09-15 13:57:34 -04:00
Python-vterm provides a major-mode for an inferior Python process (or REPL) that runs in [[https://github.com/akermu/emacs-libvterm ][vterm ]], and a minor-mode that extends python-mode with the ability to interact with the inferior Python process. This is particularly useful for fancy REPL like =ipython= or =ptpython= .
2020-10-04 14:57:18 +09:00
2022-07-07 08:21:15 +09:00
The functionalities required for typical REPL interaction have been implemented. While I would like to keep this package simple, suggestions are always welcome.
2020-10-04 14:57:18 +09:00
2024-05-16 11:26:09 -04:00
It is automatically detected whether the repl runs =ipython= or another
interpreter and some functions use different functionalities based on
that information.
2020-10-07 19:26:30 +09:00
** Installation
2024-09-15 13:57:34 -04:00
The package is on melpa and depends on ~vterm~ and ~python.el~ . The
simplest way to install the package is through ~use-package~ :
2024-05-11 19:06:47 -04:00
#+begin_src elisp
2024-09-12 11:31:13 -04:00
(use-package py-vterm-interaction
:hook (python-mode . py-vterm-interaction-mode)
2024-05-11 19:06:47 -04:00
:config
2024-09-15 13:57:34 -04:00
;;; Suggested:
;; (setq-default py-vterm-interaction-repl-program "ipython")
;; (setq-default py-vterm-interaction-silent-cells t)
)
2024-05-11 19:06:47 -04:00
#+end_src
2020-10-26 14:31:14 +09:00
2024-09-12 11:31:13 -04:00
For manual installation, download =py-vterm-interaction.el= into somewhere in your local directory and use =package-install-file= command. Please make sure [[https://github.com/PythonEditorSupport/python-emacs ][python-mode ]] and [[https://github.com/akermu/emacs-libvterm ][emacs-libvterm ]] are installed and configured correctly.
2020-10-09 10:05:26 +09:00
2024-09-12 11:31:13 -04:00
Turn on =py-vterm-interaction-mode= in a =python-mode= buffer to use this package. A symbol “PY” in the mode line indicates that the python-mode buffer is ready to interact with the py-vterm-interaction REPL. Add the following line to your init file to enable =py-vterm-interaction-mode= in =python-mode= buffers automatically.
2020-10-09 10:05:26 +09:00
#+BEGIN_SRC emacs-lisp
2024-09-12 11:31:13 -04:00
(add-hook 'python-mode-hook #'py-vterm-interaction-mode)
2020-10-07 19:26:30 +09:00
#+END_SRC
2024-09-12 11:31:13 -04:00
By default, the command named =python= in your =PATH= is used. You can use a Python executable in any path by setting the =py-vterm-interaction-repl-program= variable to its absolute path. The variable can contain switches for the =python= command. For example, you can use a =python= executable at a certain path, with 4 threads enabled, by the line like the following.
2021-04-28 01:30:38 -06:00
#+BEGIN_SRC emacs-lisp
2024-09-12 11:31:13 -04:00
(setq-default py-vterm-interaction-repl-program "ipython -i")
2021-04-28 01:30:38 -06:00
#+END_SRC
2024-05-13 13:58:52 -04:00
Some commands (see below) can either paste code into the REPL verbatim
2024-09-12 11:31:13 -04:00
or include it using ~%run -i~ . If ~py-vterm-interaction-silent-cells~ is set to ~t~
2024-05-13 13:58:52 -04:00
#+begin_src emacs-lisp
2024-09-12 11:31:13 -04:00
(setq-default py-vterm-interaction-silent-cells t)
2024-05-13 13:58:52 -04:00
#+end_src
the latter option will be used.
2020-10-07 19:26:30 +09:00
** How to use
2024-09-12 11:31:13 -04:00
=M-x py-vterm-interaction-repl= (or =M-x python= if no other packages define it before py-vterm-interaction is loaded) opens an inferior Python REPL buffer.
2022-07-07 08:21:15 +09:00
2024-09-12 11:31:13 -04:00
In a python script buffer with =py-vterm-interaction-mode= on, you can open a Python REPL with =M-x py-vterm-interaction-switch-to-repl-buffer= (or =C-c C-z= ). See below for other commands.
2022-09-12 10:16:03 +09:00
Both of the above operations open a REPL with the default session name =main= . You can specify a different session name by using the prefix argument =C-u= . A new session will be created and opened if there is no REPL with that session name.
2024-09-12 11:31:13 -04:00
You can also specify a session name by defining a file local variable =py-vterm-interaction-session= (or =python-session= if no other packages pre-define it). If the variable is defined, =C-c C-z= will open a REPL with that session name.
2020-10-07 19:26:30 +09:00
2024-05-13 13:58:52 -04:00
The package also contains a clone of [[https://docs.spyder-ide.org/3/editor.html#defining-code-cells ][spyder ]]'s code cells. Consider the following:
2024-05-11 18:43:23 -04:00
#+begin_src python
print("block 1")
# %% <optional title >
print("block 2")
# %% <optional title >
print("block 3")
#+end_src
2024-09-15 13:57:34 -04:00
2024-05-11 18:43:23 -04:00
Moving the point between any two cell markers (or between the buffer
beginning/end and a marker) and executing
2024-09-12 11:31:13 -04:00
~py-vterm-interaction-send-current-cell~ (=C-c C-j= ) will send the contents of the
block to the REPL. When setting ~py-vterm-interaction-silent-cells~ to ~t~ , the
2024-05-11 19:00:39 -04:00
cell content is written to a temporary file which is then executed in
the REPL with the ~%run~ magic command.
2024-05-11 18:43:23 -04:00
2024-05-13 13:58:52 -04:00
Similarly, one can load the current function into the REPL and
automatically call it if it has no arguments. Consider the following:
#+begin_src python
#... some code
def run_some_test():
# some computation
print("hi")
#+end_src
Moving the point inside of ~run_some_test~ and executing
2024-09-12 11:31:13 -04:00
~py-vterm-interaction-run-current-function~ (=C-c C-f= ) will load the function
2024-05-13 13:58:52 -04:00
into the REPL and execute it. For a function with arguments like
#+begin_src python
#... some code
def run_some_test(arg):
# some computation
print("hi")
#+end_src
the function name and an opening parenthesis will be pasted into the reply.
2020-10-07 19:26:30 +09:00
** Key bindings
2024-09-12 11:31:13 -04:00
*** py-vterm-interaction-mode
2022-07-06 19:38:00 +09:00
#+begin_example
Key Command / Description
2022-07-07 23:48:56 +09:00
------------------------------------------------------------------------------------------
2024-09-12 11:31:13 -04:00
C-c C-z py-vterm-interaction-switch-to-repl-buffer
2022-07-06 19:38:00 +09:00
Switch to the paired REPL buffer or to the one with a specified session name.
With prefix ARG, prompt for session name.
2024-09-12 11:31:13 -04:00
C-c C-c py-vterm-interaction-send-region-or-current-line
2022-07-07 23:48:56 +09:00
Send the content of the region if the region is active, or send the current
2025-01-21 12:00:59 -05:00
line. If the region is active and py-vterm-interaction-silent-cells is set to t
then the region is sent using ipython %run magic.
2022-07-06 19:38:00 +09:00
2024-09-12 11:31:13 -04:00
C-c C-b py-vterm-interaction-send-buffer
2024-05-11 16:29:05 -04:00
Send the whole content of the script buffer to the Python REPL line by line.
2022-07-06 19:38:00 +09:00
2024-09-12 11:31:13 -04:00
C-c C-j py-vterm-interaction-send-current-cell
2024-05-11 18:43:23 -04:00
Send the current code "cell" to the Python REPL.
Each block is delimited by `# %% <optional name >`.
If no marker is present before the point, the cell is assumed to
begin with the buffer. Likewise, if there is no marker after the
point, the cell is assumed to end with the buffer.
2024-09-12 11:31:13 -04:00
C-c C-f py-vterm-interaction-run-current-function
2024-05-13 13:58:52 -04:00
Send the current function the Python REPL and paste its name, ready to run.
If the function has no arguments, the function call is run immediately.
2024-09-12 11:31:13 -04:00
C-c C-r py-vterm-interaction-send-run-buffer-file
2024-05-11 16:32:05 -04:00
Send a line to evaluate the buffer's file using ipython %run magic.
2022-07-06 19:38:00 +09:00
2024-05-17 16:18:10 -04:00
C-c C-i Import the current buffer file like `from <module > import *' in the python repl.
This is especially useful with `%autoload 3' in ipython.
2024-09-12 11:31:13 -04:00
C-c C-d py-vterm-interaction-send-cd-to-buffer-directory
2024-05-11 16:32:05 -04:00
Send %cd function call to the Python REPL to change the current working
2022-07-07 23:48:56 +09:00
directory of REPL to the buffer's directory.
2025-01-21 12:33:54 -05:00
C-c C-a py-vterm-interaction-send-rerun-last
Send C-p <return > to the repl to rerun the last line entered.
2022-07-06 19:38:00 +09:00
#+end_example
2024-09-12 11:31:13 -04:00
*** py-vterm-interaction-repl-mode
2022-07-06 19:38:00 +09:00
#+begin_example
Key Command / Description
2022-07-07 23:48:56 +09:00
------------------------------------------------------------------------------------------
2024-09-12 11:31:13 -04:00
C-c C-z py-vterm-interaction-repl-switch-to-script-buffer
2024-05-11 16:29:05 -04:00
Switch to the script buffer that is paired with the current Python REPL buffer.
2022-07-06 19:38:00 +09:00
2024-09-12 11:31:13 -04:00
M-k py-vterm-interaction-repl-clear-buffer
2024-05-11 16:29:05 -04:00
Clear the content of the Python REPL buffer.
2020-10-07 19:26:30 +09:00
2024-09-12 11:31:13 -04:00
C-c C-t py-vterm-interaction-repl-copy-mode
2022-07-06 19:38:00 +09:00
Enter copy mode.
2024-05-14 17:09:11 -04:00
2024-09-12 11:31:13 -04:00
C-c M-r py-vterm-interaction-repl-restart
2024-05-14 17:09:11 -04:00
Restart the current inferior Python process. A new Python REPL will be opened in
the same Emacs window, and the working directory and environment will be
restored from the previous REPL process when possible.
2022-07-06 19:38:00 +09:00
#+end_example
2020-10-07 19:26:30 +09:00
2024-09-12 11:31:13 -04:00
*** py-vterm-interaction-repl-mode (copy mode)
2020-10-07 19:26:30 +09:00
2022-07-06 19:38:00 +09:00
#+begin_example
Key Command / Description
2022-07-07 23:48:56 +09:00
------------------------------------------------------------------------------------------
2024-09-12 11:31:13 -04:00
C-c C-t py-vterm-interaction-repl-copy-mode
2022-07-06 19:38:00 +09:00
Exit copy mode.
2020-10-07 19:26:30 +09:00
2024-09-12 11:31:13 -04:00
<return > py-vterm-interaction-repl-copy-mode-done
2022-07-06 19:38:00 +09:00
Copy the region to the kill ring and exit copy mode.
2020-10-07 19:26:30 +09:00
2022-07-06 19:38:00 +09:00
C-c C-r vterm-reset-cursor-point
Call the vterm command that moves point to where it should be.
#+end_example
2024-05-13 13:58:52 -04:00
* Roadmap
** TODO fix the automatic return
- most commands should accept a prefix that control whether the code
is exectued with =<enter>= or just pasted into the repl
** TODO make an =in-repl-buffer= macro
2024-05-14 11:40:34 -04:00
** TODO make run-current-function a bit more intelligent
- detect if there are only arguments with default values