近几天有时间了,打算好好学习下python的实际应用,又好好鼓捣了下emacs下的python编程环境,至于为什么是emcs,个人习惯,不做解释
.
长话短说,首先配置emacs的python模式,emacs自带的python模式功能不够强大,自己下载python-mode.el扔到你的加载目录中,并在.emacs中做如下配置
(setq auto-mode-alist (cons '("\\.py$" . python-mode) auto-mode-alist))
(autoload 'python-mode "python-mode" "Python editing mode." t)
(setq interpreter-mode-alist (cons '("python" . python-mode)
interpreter-mode-alist))
(setq py-python-command "python")
(autoload 'python-mode "python-mode" "Pythonediting mode." t)
至于作为一个编程环境,自动补全自然是必不可少的了,在网上搜寻了一番,最终选择了rope+pymacs的补全方式.安装步骤如下:
安装方法: python setup.py install
pymacs的安装有点麻烦,我下载的pymacs 0.25,按照网上所述,我在windows命令行下直接python setup.py install提示找不到pymacs.py,查阅了安装说明后发现需要行做make生成pymacs.py和pymacs.el文件,我机器上的是mingw,在windows命令行下直接make会提示IO错误,百思不得其解,折腾一番后,最终在msys下make成功,然后setup.py install即可成功安装.
安装方法 python setup.py install
安装方法 python setup.py install
然后在.emacs中做加载设置,因为pymacs载入较慢,每次启动emacs时都加载的话会很慢,所以我选择在打开python文件时才加载,配置如下:
(eval-after-load "python-mode"
'(progn
(autoload 'pymacs-apply "pymacs")
(autoload 'pymacs-call "pymacs")
(autoload 'pymacs-eval "pymacs" nil t)
(autoload 'pymacs-exec "pymacs" nil t)
(autoload 'pymacs-load "pymacs" nil t)
(require 'pycomplete)
(message "loading ropemacs")
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)
(require 'python-pep8)
(require 'python-pylint)
(add-hook 'before-save-hook 'delete-trailing-whitespace)))
为了得到更好的补全效果,再安装一个更强大的智能提示工具pycomplete,下载
pycomplete.el ,扔到load-path中
再下载 pycomplete.py 扔到PYTHONPATH (比如: c:/python26/Lib/site-packages)
然后(require 'pycomplete)即可,这里我和pymacs的加载写到了一起
最后配置autocomplete,这个是emacs自带的,它的配置是从网上找来的,配置如下
(require 'auto-complete)
(require 'auto-complete-config)
;; (setenv "PYMACS_PYTHON" "python2.7")
(global-auto-complete-mode t)
(setq-default ac-sources '(ac-source-words-in-same-mode-buffers))
(add-hook 'emacs-lisp-mode-hook (lambda () (add-to-list 'ac-sources 'ac-source-symbols)))
(add-hook 'auto-complete-mode-hook (lambda () (add-to-list 'ac-sources 'ac-source-filename)))
(ac-ropemacs-initialize)
(add-hook 'python-mode-hook
(lambda ()
(add-to-list 'ac-source 'ac-source-ropemacs)))
(set-face-background 'ac-candidate-face "lightgray")
(set-face-underline 'ac-candidate-face "darkgray")
(set-face-background 'ac-selection-face "steelblue")
(define-key ac-completing-map "\M-n" 'ac-next)
(define-key ac-completing-map "\M-p" 'ac-previous)
(setq ac-auto-start 2)
(setq ac-dwim t)
(define-key ac-mode-map (kbd "C-TAB") 'auto-complete)
安装yasnippet,下载
https://github.com/capitaomorte/yasnippet
解压扔到load-path, 配置如下
(add-to-list 'load-path
"D:/tools/emacs-24.1.50/site-lisp/yasnippet")
(require 'yasnippet)
(yas/global-mode 1)
最终效果如下
参考资料:
http://pedrokroger.com/2010/07/configuring-emacs-as-a-python-ide-2/
http://emacswiki.org/emacs/PythonProgrammingInEmacs