我如何建议emacs中的py-execute-buffer在函数之前运行py-shell?

时间:2011-07-09 18:42:42

标签: emacs elisp python-mode advising-functions defadvice

我想编辑缓冲区或文件,点击C-c C-c并将文件发送到IPython会话。如果我先通过M-x py-shell运行py-execute-buffer之前首先运行C-c C-c,我就可以完成此操作。但是,如果我尝试通过

建议功能
(defadvice py-execute-buffer (before open-py-shell-first)
   (let ((remember-window (selected-window))
     (remember-point (point)))
     (generate-new-buffer "*Python*")
     (call-interactively 'py-shell)
     (select-window remember-window)
     (goto-char remember-point)))
(ad-activate 'py-execute-buffer)

我收到错误:

## working on region in file /var/folders/6o/6o08knx-FVOhrvCbYyrRDU+++TI/-Tmp-/python-29189FKe.py...
Python 2.7.1 (r271:86832, Feb  5 2011, 13:58:58) 
Type "copyright", "credits" or "license" for more information.

IPython 0.10.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: ---------------------------------------------------------------------------
IOError                                   Traceback (most recent call last)

/Users/aresnick/Desktop/<ipython console> in <module>()

IOError: [Errno 2] No such file or directory: '/var/folders/6o/6o08knx-FVOhrvCbYyrRDU+++TI/-Tmp-/python-29189FKe.py'

In [2]: 

IPython似乎没有看到创建的临时文件?如果我现在切换回我的Python文件并重新运行py-execute-buffer,一切都很好。

任何想法都将不胜感激 - 谢谢!

2 个答案:

答案 0 :(得分:0)

我想我找到了一些解决方案:

(defun my-py-execute-buffer ()
  (interactive)
  (set 'code-buffer (current-buffer))
  (unless (get-buffer "*Python*")
    (py-shell)
    (set-buffer code-buffer)
    (sleep-for 0.2))
  (py-execute-buffer))

睡觉 - 因为它正常工作。

答案 1 :(得分:0)

使用当前的python-mode.el,

bzr branch lp:python-mode

只是M-x py-execute-buffer-ipython RET

RESP。将py-shell-name设置为“ipython”

py-execute-buffer应该是DTRT - 除非shebang告诉

上面的具体命令在shebang之前

而不是默认的“ipython”,你可以指向一个版本,也包括路径