使Emacs的Python交互模式高亮和缩进

6 投票
2 回答
1147 浏览
提问于 2025-04-15 18:01

我在用 Emacs 23 和 python-mode 5.1.0 来编辑我的 Python 程序。有时候,我想写一个小的临时 Python 脚本,所以我会运行一个交互式的命令(C-c !)。这样做没问题,但它既不自动缩进,也不高亮代码。如果我在这个模式下尝试运行 Python 模式,它也不再有效了。那么,我该怎么设置才能让它自动缩进和高亮呢?

2 个回答

-1

我现在没时间试试,但我发现你可以在按下C-c的时候,用这个更强大的东西来替换Python的命令行:

http://ipython.scipy.org/moin/

等我有时间了,我会去试试的。

3

把这些内容加到我的 .emacs 文件里似乎就解决了问题。

在建议中检查缓冲区名称的那部分,可能用一个由 set-pycomint-keywords 设置的局部变量来测试会更好,不过你大概明白我的意思了。

(defun set-pycomint-keywords ()
  (setq font-lock-keywords      python-font-lock-keywords))

(add-hook 'py-shell-hook 'set-pycomint-keywords)



 (defadvice comint-send-input (around block-comint-formatting activate)
  (if (string= (buffer-name) "*Python*")
      (letf (((symbol-function 'add-text-properties) (lambda (x y z) nil)))
        ad-do-it))
  ad-do-it)

撰写回答