Skip to content

Commit 4cbf9e1

Browse files
sunlin7smile13241324
authored andcommitted
New function "spacemacs/python-shell-send-block" to send block
* layers/+lang/python/funcs.el: New function "spacemacs/python-shell-send-block" and "spacemacs/python-shell-send-block-switch". * layers/+lang/python/packages.el: Binding new functions to keys
1 parent 053116d commit 4cbf9e1

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

layers/+lang/python/funcs.el

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,33 @@ Bind formatter to '==' for LSP and '='for all other backends."
422422

423423

424424
;; REPL
425+
(defun spacemacs/python-shell-send-block (&optional arg)
426+
"Send the block under cursor to shell. If optional argument ARG is non-nil
427+
(interactively, the prefix argument), send the block body with its header."
428+
(interactive "P")
429+
(if (fboundp 'python-shell-send-block)
430+
(let ((python-mode-hook nil))
431+
(call-interactively #'python-shell-send-block))
432+
(let ((python-mode-hook nil)
433+
(beg (save-excursion
434+
(when (python-nav-beginning-of-block)
435+
(if arg
436+
(beginning-of-line)
437+
(python-nav-end-of-statement)
438+
(beginning-of-line 2)))
439+
(point-marker)))
440+
(end (save-excursion (python-nav-end-of-block)))
441+
(python-indent-guess-indent-offset-verbose nil))
442+
(if (and beg end)
443+
(python-shell-send-region beg end nil msg t)
444+
(user-error "Can't get code block from current position.")))))
445+
446+
(defun spacemacs/python-shell-send-block-switch (&optional arg)
447+
"Send block to shell and switch to it in insert mode."
448+
(interactive "P")
449+
(call-interactively #'spacemacs/python-shell-send-block)
450+
(python-shell-switch-to-shell)
451+
(evil-insert-state))
425452

426453
(defun spacemacs/python-shell-send-buffer-switch ()
427454
"Send buffer content to shell and switch to it in insert mode."

layers/+lang/python/packages.el

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,8 @@
404404
"sF" 'spacemacs/python-shell-send-defun-switch
405405
"sf" 'spacemacs/python-shell-send-defun
406406
"si" 'spacemacs/python-start-or-switch-repl
407+
"sK" 'spacemacs/python-shell-send-block-switch
408+
"sk" 'spacemacs/python-shell-send-block
407409
"sn" 'spacemacs/python-shell-restart
408410
"sN" 'spacemacs/python-shell-restart-switch
409411
"sR" 'spacemacs/python-shell-send-region-switch

0 commit comments

Comments
 (0)