首页

2010年6月2日星期三

pymacs-settings.el配置python自动补全

下载pymacs,解压到任意一个目录中,如:我的是[B.Qnyd@localhost 下载]$ ls -l
将会看见pymacs 的python代码源包,解压就行了....

进入其中 执行 python setup.py install 可以先执行python setup build 然后安装rope ropemacs来很好的配合python让他具有IDE的项目管理功能,首先执行下面的命令下载源代码下来

$hg clone http://bitbucket.org/agr/rope/
$hg clone http://bitbucket.org/agr/ropemacs/
$hg clone http://bitbucket.org/agr/ropemode/

然后分别进入rope ropemacs 里面执行
python setup.py install
把三个文件分别移动到你的path下面如:

mv rope* /home/B.Qnyd/lisp/

如果你还想要它检查你的语法是否正确,你就需要安装pyflakes
我的是fedora 直接安装
yum install pyflakes
其他系统可以用sudo apt-get install 等系统默认的在线安装包...
(注意这个python-setuptools东西必不可少,一样执行
yum install python-setuptools)
然后.
新建一个pymacs-settings.el 把以下代码加入进去:

(setq pymacs-load-path '("~/lisp/rope"




"~/lisp/ropemacs"))


(autoload 'python-mode "python-mode" "Python Mode." t)


(add-to-list 'auto-mode-alist '("\\.py\\'" . python-mode))


(add-to-list 'interpreter-mode-alist '("python" . python-mode))


(require 'python-mode)


(add-hook 'python-mode-hook


(lambda ()


(set-variable 'py-indent-offset 4)


;(set-variable 'py-smart-indentation nil)


(set-variable 'indent-tabs-mode nil)


(define-key py-mode-map (kbd "RET") 'newline-and-indent)


;(define-key py-mode-map [tab] 'yas/expand)


;(setq yas/after-exit-snippet-hook 'indent-according-to-mode)






))


;; pymacs


(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)


;;(eval-after-load "pymacs"


;; '(add-to-list 'pymacs-load-path YOUR-PYMACS-DIRECTORY"))


(pymacs-load "ropemacs" "rope-")


(setq ropemacs-enable-autoimport t)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;; Auto-completion


;;; Integrates:


;;; 1) Rope


;;; 2) Yasnippet


;;; all with AutoComplete.el


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


(defun prefix-list-elements (list prefix)


(let (value)


(nreverse


(dolist (element list value)


(setq value (cons (format "%s%s" prefix element) value))))))


(defvar ac-source-rope


'((candidates


. (lambda ()


(prefix-list-elements (rope-completions) ac-target))))


"Source for Rope")


(defun ac-python-find ()


"Python `ac-find-function'."


(require 'thingatpt)


(let ((symbol (car-safe (bounds-of-thing-at-point 'symbol))))


(if (null symbol)


(if (string= "." (buffer-substring (- (point) 1) (point)))


(point)


nil)


symbol)))


(defun ac-python-candidate ()


"Python `ac-candidates-function'"


(let (candidates)


(dolist (source ac-sources)


(if (symbolp source)


(setq source (symbol-value source)))


(let* ((ac-limit (or (cdr-safe (assq 'limit source)) ac-limit))


(requires (cdr-safe (assq 'requires source)))


cand)


(if (or (null requires)


(>= (length ac-target) requires))


(setq cand


(delq nil


(mapcar (lambda (candidate)


(propertize candidate 'source source))


(funcall (cdr (assq 'candidates source)))))))


(if (and (> ac-limit 1)


(> (length cand) ac-limit))


(setcdr (nthcdr (1- ac-limit) cand) nil))


(setq candidates (append candidates cand))))


(delete-dups candidates)))


(add-hook 'python-mode-hook


(lambda ()


(auto-complete-mode 1)


(set (make-local-variable 'ac-sources)


(append ac-sources '(ac-source-rope) '(ac-source-yasnippet)))


(set (make-local-variable 'ac-find-function) 'ac-python-find)


(set (make-local-variable 'ac-candidate-function) 'ac-python-candidate)


(set (make-local-variable 'ac-auto-start) nil)))


;;Ryan's python specific tab completion


(defun ryan-python-tab ()


; Try the following:


; 1) Do a yasnippet expansion


; 2) Do a Rope code completion


; 3) Do an indent


(interactive)


(if (eql (ac-start) 0)


(indent-for-tab-command)))


(defadvice ac-start (before advice-turn-on-auto-start activate)


(set (make-local-variable 'ac-auto-start) t))


(defadvice ac-cleanup (after advice-turn-off-auto-start activate)


(set (make-local-variable 'ac-auto-start) nil))


(define-key py-mode-map "\t" 'ryan-python-tab)


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;;; End Auto Completion


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


;; Auto Syntax Error Hightlight


(when (load "flymake" t)


(defun flymake-pyflakes-init ()


(let* ((temp-file (flymake-init-create-temp-buffer-copy


'flymake-create-temp-inplace))


(local-file (file-relative-name


temp-file


(file-name-directory buffer-file-name))))


(list "pyflakes" (list local-file))))


(add-to-list 'flymake-allowed-file-name-masks


'("\\.py\\'" flymake-pyflakes-init)))


(add-hook 'find-file-hook 'flymake-find-file-hook)


(provide 'pymacs-settings)


然后在你的.emacs 里面加入 (load "pymacs-settings")这样你就给你的emacs扩展了自动补全,

建立工程了.不比任何IDE差...... 不过首先你得配置好,不然你就会觉得它很差劲....

没有评论:

发表评论