Skip to content

Instantly share code, notes, and snippets.

@Gastove
Created March 31, 2017 17:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gastove/fd29bc4f22b2b9719dd9971ffd9a30b6 to your computer and use it in GitHub Desktop.
Save Gastove/fd29bc4f22b2b9719dd9971ffd9a30b6 to your computer and use it in GitHub Desktop.
(defun rabbit/python-shell-send-region (start end &optional send-main msg)
"Send the region delimited by START and END to inferior Python process.
When optional argument SEND-MAIN is non-nil, allow execution of
code inside blocks delimited by \"if __name__== \\='__main__\\=':\".
When called interactively SEND-MAIN defaults to nil, unless it's
called with prefix argument. When optional argument MSG is
non-nil, forces display of a user-friendly message if there's no
process running; defaults to t when called interactively."
(interactive
(list (region-beginning) (region-end) current-prefix-arg t))
(let* ((string (python-shell-buffer-substring start end (not send-main)))
(process (python-shell-get-process-or-error msg))
(original-string (buffer-substring-no-properties start end))
(_ (string-match "\\`\n*\\(.*\\)" original-string))
(buf (python-shell-get-buffer))
(input (buffer-substring start end)))
(message "Sent: %s..." (match-string 1 original-string))
(save-excursion
(with-current-buffer buf
(newline)
(insert input)
(comint-send-input)))))
(-let ((buf (python-shell-get-buffer))
(process (python-shell-get-process-or-error nil)))
(with-current-buffer buf
(newline)
(insert "# bloop bloop bloop")
(comint-send-input)
))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment