我在Emacs上安装了“私奔/绝地”。使用DJJ提供的定制,我现在可以使用C-c C-RET向python解释器发送一条单独的行。以下是定制
(defun my-python-line ()
(interactive)
(save-excursion
(setq the_script_buffer (format (buffer-name)))
(end-of-line)
(kill-region (point) (progn (back-to-indentation) (point)))
;(setq the_py_buffer (format "*Python[%s]*" (buffer-file-name)))
(setq the_py_buffer "*Python*")
(switch-to-buffer-other-window the_py_buffer)
(goto-char (buffer-end 1))
(yank)
(comint-send-input)
(switch-to-buffer-other-window the_script_buffer)
(yank)
)
(next-line)
)
(eval-after-load "elpy"
'(define-key elpy-mode-map (kbd "C-c <C-return>") 'my-python-line))上面的代码片段与DDJ建议的基本相同,只是做了一些小修改,比如将光标移到下一行和快捷方式。
我想修改这种行为,使光标所在的所有行都能被发送到python解释器。游标位置应该移动到空的换行符。这将模仿Spyder的行为。
更新1,所以在用以下代码更新我的.emacs之后。当我执行单独的语句(如M-x beginning-of-line,M-x push-mark)时,我可以得到想要的结果.
但是当我使用键盘快捷方式C-c <C-return>时,它会以某种方式计算整个缓冲区。
(defun forward-block (&optional φn)
(interactive "p")
(let ((φn (if (null φn) 1 φn)))
(search-forward-regexp "\n[\t\n ]*\n+" nil "NOERROR" φn)))
(defun elpy-shell-send-current-block ()
"Send current block to Python shell."
(interactive)
(beginning-of-line)
(push-mark)
(forward-block)
(elpy-shell-send-region-or-buffer)
(display-buffer (process-buffer (elpy-shell-get-or-create-process))
nil
'visible))
(eval-after-load "elpy"
'(define-key elpy-mode-map (kbd "C-c <C-return>") 'elpy-shell-send-current-block))下面是我正在尝试这个快捷方式的Python代码,第二个print语句中的游标位于下面。
import os
print("Line 1: Should Execute")
print("Line 2: Should Execute")
print("Line 3: Should Not Execute")发布于 2015-08-13 05:18:50
您可以编写一个函数来选择您想要的任何区域,并调用elpy-shell-send-region-or-buffer来发送它。
下面是一段代码。
(defun forward-block (&optional n)
(interactive "p")
(let ((n (if (null n) 1 n)))
(search-forward-regexp "\n[\t\n ]*\n+" nil "NOERROR" n)))
(defun elpy-shell-send-current-block ()
"Send current block to Python shell."
(interactive)
(beginning-of-line)
(push-mark)
(forward-block)
(elpy-shell-send-region-or-buffer)
(display-buffer (process-buffer (elpy-shell-get-or-create-process))
nil
'visible))https://stackoverflow.com/questions/31957564
复制相似问题