diff options
| author | Lin Sun | 2024-04-27 06:54:27 +0000 |
|---|---|---|
| committer | Eli Zaretskii | 2024-05-02 12:56:31 +0300 |
| commit | b2e92c746eb7d1135d3d4ccecc774d79555ffb99 (patch) | |
| tree | 09e6292e053fdf3b3558e98a653e7740d14c6401 | |
| parent | bf00762745c07816e7a9c64e0fce037039852971 (diff) | |
| download | emacs-b2e92c746eb7d1135d3d4ccecc774d79555ffb99.tar.gz emacs-b2e92c746eb7d1135d3d4ccecc774d79555ffb99.zip | |
New function 'python-shell-send-block' for python-mode
* lisp/progmodes/python.el (python-shell-send-block): New
function.
* test/lisp/progmodes/python-tests.el
(python-test--shell-send-block): Test case for the new
function.
* etc/NEWS: Document 'python-shell-send-block'.
(Bug#70609)
| -rw-r--r-- | etc/NEWS | 4 | ||||
| -rw-r--r-- | lisp/progmodes/python.el | 25 | ||||
| -rw-r--r-- | test/lisp/progmodes/python-tests.el | 27 |
3 files changed, 56 insertions, 0 deletions
| @@ -1347,6 +1347,10 @@ instead of: | |||
| 1347 | This allows the user to specify command line arguments to the non | 1347 | This allows the user to specify command line arguments to the non |
| 1348 | interactive Python interpreter specified by 'python-interpreter'. | 1348 | interactive Python interpreter specified by 'python-interpreter'. |
| 1349 | 1349 | ||
| 1350 | *** New function 'python-shell-send-block'. | ||
| 1351 | It sends the python block delimited by 'python-nav-beginning-of-block' | ||
| 1352 | and 'python-nav-end-of-block' to the inferior Python process. | ||
| 1353 | |||
| 1350 | ** Scheme mode | 1354 | ** Scheme mode |
| 1351 | Scheme mode now handles regular expression literal '#/regexp/' that is | 1355 | Scheme mode now handles regular expression literal '#/regexp/' that is |
| 1352 | available in some Scheme implementations. | 1356 | available in some Scheme implementations. |
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index ecbec18f518..57cdf68fc25 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -350,6 +350,7 @@ To customize the Python interpreter for interactive use, modify | |||
| 350 | (define-key map "\C-c\C-e" #'python-shell-send-statement) | 350 | (define-key map "\C-c\C-e" #'python-shell-send-statement) |
| 351 | (define-key map "\C-c\C-r" #'python-shell-send-region) | 351 | (define-key map "\C-c\C-r" #'python-shell-send-region) |
| 352 | (define-key map "\C-\M-x" #'python-shell-send-defun) | 352 | (define-key map "\C-\M-x" #'python-shell-send-defun) |
| 353 | (define-key map "\C-c\C-b" #'python-shell-send-block) | ||
| 353 | (define-key map "\C-c\C-c" #'python-shell-send-buffer) | 354 | (define-key map "\C-c\C-c" #'python-shell-send-buffer) |
| 354 | (define-key map "\C-c\C-l" #'python-shell-send-file) | 355 | (define-key map "\C-c\C-l" #'python-shell-send-file) |
| 355 | (define-key map "\C-c\C-z" #'python-shell-switch-to-shell) | 356 | (define-key map "\C-c\C-z" #'python-shell-switch-to-shell) |
| @@ -390,6 +391,8 @@ To customize the Python interpreter for interactive use, modify | |||
| 390 | :help "Switch to running inferior Python process"] | 391 | :help "Switch to running inferior Python process"] |
| 391 | ["Eval string" python-shell-send-string | 392 | ["Eval string" python-shell-send-string |
| 392 | :help "Eval string in inferior Python session"] | 393 | :help "Eval string in inferior Python session"] |
| 394 | ["Eval block" python-shell-send-block | ||
| 395 | :help "Eval block in inferior Python session"] | ||
| 393 | ["Eval buffer" python-shell-send-buffer | 396 | ["Eval buffer" python-shell-send-buffer |
| 394 | :help "Eval buffer in inferior Python session"] | 397 | :help "Eval buffer in inferior Python session"] |
| 395 | ["Eval statement" python-shell-send-statement | 398 | ["Eval statement" python-shell-send-statement |
| @@ -4141,6 +4144,27 @@ interactively." | |||
| 4141 | (save-excursion (python-nav-end-of-statement)) | 4144 | (save-excursion (python-nav-end-of-statement)) |
| 4142 | send-main msg t))) | 4145 | send-main msg t))) |
| 4143 | 4146 | ||
| 4147 | (defun python-shell-send-block (&optional arg msg) | ||
| 4148 | "Send the block at point to inferior Python process. | ||
| 4149 | The block is delimited by `python-nav-beginning-of-block' and | ||
| 4150 | `python-nav-end-of-block'. When optional argument ARG is non-nil, send | ||
| 4151 | the block body without its header. When optional argument MSG is | ||
| 4152 | non-nil, forces display of a user-friendly message if there's no process | ||
| 4153 | running; defaults to t when called interactively." | ||
| 4154 | (interactive (list current-prefix-arg t)) | ||
| 4155 | (let ((beg (save-excursion | ||
| 4156 | (when (python-nav-beginning-of-block) | ||
| 4157 | (if (null arg) | ||
| 4158 | (beginning-of-line) | ||
| 4159 | (python-nav-end-of-statement) | ||
| 4160 | (beginning-of-line 2))) | ||
| 4161 | (point-marker))) | ||
| 4162 | (end (save-excursion (python-nav-end-of-block))) | ||
| 4163 | (python-indent-guess-indent-offset-verbose nil)) | ||
| 4164 | (if (and beg end) | ||
| 4165 | (python-shell-send-region beg end nil msg t) | ||
| 4166 | (user-error "Can't get code block from current position.")))) | ||
| 4167 | |||
| 4144 | (defun python-shell-send-buffer (&optional send-main msg) | 4168 | (defun python-shell-send-buffer (&optional send-main msg) |
| 4145 | "Send the entire buffer to inferior Python process. | 4169 | "Send the entire buffer to inferior Python process. |
| 4146 | When optional argument SEND-MAIN is non-nil, allow execution of | 4170 | When optional argument SEND-MAIN is non-nil, allow execution of |
| @@ -7181,6 +7205,7 @@ implementations: `python-mode' and `python-ts-mode'." | |||
| 7181 | python-nav-if-name-main | 7205 | python-nav-if-name-main |
| 7182 | python-nav-up-list | 7206 | python-nav-up-list |
| 7183 | python-remove-import | 7207 | python-remove-import |
| 7208 | python-shell-send-block | ||
| 7184 | python-shell-send-buffer | 7209 | python-shell-send-buffer |
| 7185 | python-shell-send-defun | 7210 | python-shell-send-defun |
| 7186 | python-shell-send-statement | 7211 | python-shell-send-statement |
diff --git a/test/lisp/progmodes/python-tests.el b/test/lisp/progmodes/python-tests.el index e3b1642a975..889038d0f8c 100644 --- a/test/lisp/progmodes/python-tests.el +++ b/test/lisp/progmodes/python-tests.el | |||
| @@ -7466,6 +7466,33 @@ buffer with overlapping strings." | |||
| 7466 | "Unused import a.b.c (unused-import)" | 7466 | "Unused import a.b.c (unused-import)" |
| 7467 | "W0611: Unused import a.b.c (unused-import)")))))) | 7467 | "W0611: Unused import a.b.c (unused-import)")))))) |
| 7468 | 7468 | ||
| 7469 | (ert-deftest python-test--shell-send-block () | ||
| 7470 | (skip-unless (executable-find python-tests-shell-interpreter)) | ||
| 7471 | (python-tests-with-temp-buffer-with-shell | ||
| 7472 | "print('current 0') | ||
| 7473 | for x in range(1,3): | ||
| 7474 | print('current %s' % x) | ||
| 7475 | print('current 3')" | ||
| 7476 | (goto-line 1) | ||
| 7477 | (should-error (python-shell-send-block) :type 'user-error) | ||
| 7478 | (goto-line 2) | ||
| 7479 | (python-shell-send-block) | ||
| 7480 | (python-tests-shell-wait-for-prompt) | ||
| 7481 | (python-shell-with-shell-buffer | ||
| 7482 | (goto-char (point-min)) | ||
| 7483 | (should-not (re-search-forward "current 0" nil t)) | ||
| 7484 | (should (re-search-forward "current 1" nil t)) | ||
| 7485 | (should (re-search-forward "current 2" nil t)) | ||
| 7486 | (should-not (re-search-forward "current 3" nil t))) | ||
| 7487 | (goto-line 3) | ||
| 7488 | (python-shell-send-block t) ;; send block body only | ||
| 7489 | (python-tests-shell-wait-for-prompt) | ||
| 7490 | (python-shell-with-shell-buffer | ||
| 7491 | ;; should only 1 line output from the block body | ||
| 7492 | (should (re-search-forward "current")) | ||
| 7493 | (should (looking-at " 2")) | ||
| 7494 | (should-not (re-search-forward "current" nil t))))) | ||
| 7495 | |||
| 7469 | ;;; python-ts-mode font-lock tests | 7496 | ;;; python-ts-mode font-lock tests |
| 7470 | 7497 | ||
| 7471 | (defmacro python-ts-tests-with-temp-buffer (contents &rest body) | 7498 | (defmacro python-ts-tests-with-temp-buffer (contents &rest body) |