这里的问题与Change the "send code to interpreter" (C-c |) command in python-mode有关(但不完全相同)和互补。
我在Mac 10.9.5,Emacs 24.4,Python 2.7.8和IPython 2.2.0上工作。
我的想法是更改C-c C-r
emacs命令以将IPython模式中的区域/代码行发送到C-RET
,就像使用R时一样。这是因为我通常使用R,但是从现在,我将使用R和Python(特别是IPython,我非常喜欢),C-RET
- R--中的发送代码命令似乎对我来说更舒服。
在本问题开头引用的链接中,他们建议在.emacs
文件中添加以下行,将C-c |
命令更改为C-c C-r
:
(eval-after-load "python"
'(progn
(define-key python-mode-map (kbd "C-c C-r") 'python-shell-send-region)))
目前,我的.emacs
文件中的python / IPython配置如下所示:
;; Enable Python
(add-to-list 'load-path "/sw/lib/python-mode-1.0")
(load "python-mode")
(setq auto-mode-alist
(cons '("\\.py$" . python-mode) auto-mode-alist))
(setq interpreter-mode-alist
(cons '("python" . python-mode)
interpreter-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)
;; Ipython. This python-mode takes the Key-map and the menu
(when (executable-find "ipython")
(setq
python-shell-interpreter "ipython"
python-shell-interpreter-args ""
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
python-shell-completion-setup-code
"from IPython.core.completerlib import module_completion"
python-shell-completion-module-string-code
"';'.join(module_completion('''%s'''))\n"
python-shell-completion-string-code
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n"))
这是两个并行运行的python模式,第二个(IPython,我一直使用的模式)采用键映射和菜单(顺便提一下,欢迎任何更好配置的建议.IPython部分基于:How to open IPython interpreter in emacs?)。
我尝试在python配置结束时添加之前描述的(eval-after-load "python" '(progn ...
命令(当然,将C-c C-r
更改为C-RET
或C-ret
甚至{{ 1}})。
我也在C-<return>
块中尝试了不同的形式(例如简单when (executable-find "ipython") ...
)。但似乎没有任何效果。
因此,我的问题是:给定我的Python / IPython配置,我必须在(define-key python-mode-map (kbd "C-c C-r") 'python-shell-send-region)
文件中包含什么才能将.emacs
命令更改为(C-RET)
非常感谢提前!
答案 0 :(得分:5)
使用(kbd&#34; RET&#34;)
尝试使用python.el
(eval-after-load "python"
'(define-key python-mode-map [(control c)(kbd "RET")] 'python-shell-send-region))
WRT python-mode.el:
(eval-after-load "python-mode"
'(define-key python-mode-map [(control c) (kbd "RET")] 'py-execute-region))
BTW除非需要IPython-exclusiv功能,否则建议通过常见的Python从Emacs执行代码。 IPython实现了许多很酷的东西,它们可能看起来与Emacs正交,后者也实现了很多很酷的东西。
答案 1 :(得分:4)
这是一些很快被煮熟以发送python线的东西 蟒蛇壳。我倾向于在R中做很多事情。它还没有完全自动化,但我可能会很有用。
以下是一些仍有待制作的内容:
分配本地密钥
(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))
)
(global-set-key (kbd "C-c n") 'my-python-line)
更新
我对代码进行了一些改进:
已设置本地设置密钥
只有专门的流程才能完成。请在下面的答案中随意改进
(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)))
(if (get-buffer "*Python*")
(message "")
(run-python "ipython" nil nil))
;; (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))
(end-of-line)
(next-line)
)
(add-hook 'python-mode-hook
(lambda ()
(define-key python-mode-map "\C-cn" 'my-python-line)))
答案 2 :(得分:1)
我做了一些黑客攻击,这就是我所拥有的: 通过阅读代码,您应该能够了解它是如何工作的。
(defun block-line-end ()
(setq indentation (current-indentation))
(forward-line)
(while (> (current-indentation) indentation)
(forward-line))
(forward-line -1)
(line-end-position))
(defun my-python-shell-send-region (&optional beg end)
(interactive)
(let ((beg (cond (beg beg)
((region-active-p) (region-beginning))
(t (line-beginning-position))))
(end (cond (end end)
((region-active-p)
(copy-marker (region-end)))
(t (block-line-end)))))
(python-shell-send-region beg end))
(forward-line))