【问题标题】:Emacs Python Auto CompletionEmacs Python 自动补全
【发布时间】:2013-07-01 17:15:58
【问题描述】:

编辑:

我刚刚意识到我正在使用 0.2 版的 auto-complete.el。我想我需要使用 auto-complete.el 0.1 版。我可以从哪里下载它?我只能在 Google 上找到较新的版本。


我正在尝试在 Emacs 中为 Python 设置自动完成功能。

我使用的是 Ubuntu LTS 版本,我同时安装了 Python 和 Emacs。

这是我的 .emacs 文件:

(setq-default indent-tabs-mode nil) ; always replace tabs with spaces
(setq-default tab-width 4) ; set tab width to 4 for all buffers

(add-to-list 'load-path "~/.emacs.d/vendor")
(progn (cd "~/.emacs.d/vendor")
       (normal-top-level-add-subdirs-to-load-path))

(require 'python)
(require 'auto-complete)
(require 'yasnippet)

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

;; Initialize 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)
;; Initialize Rope                                                                                             
(pymacs-load "ropemacs" "rope-")
(setq ropemacs-enable-autoimport t)

;; Initialize Yasnippet                                                                                        
;Don't map TAB to yasnippet                                                                                    
;In fact, set it to something we'll never use because                                                          
;we'll only ever trigger it indirectly.                                                                        
(setq yas/trigger-key (kbd "C-c <kp-multiply>"))
(yas/initialize)
(yas/load-directory "~/.emacs.d/snippets")



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                                         
;;; 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 python-mode-map "\t" 'ryan-python-tab)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;                                         
;;; End Auto Completion                                                                                        
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

在我拥有的 .emacs.d 目录中:

pymacs.el
init_python.el
vendor/
snippets/

在 .emacs.d/vendor 我已经安装了 pymacs、yasn-p 和自动完成功能,所以它看起来像这样:

auto-complete.el
pinard-Pymacs-4be2c15/
yasnippet-0.5.9/

当我这样启动 emacs 时:

emacs -nw manage.py

我收到此错误消息:

![enter image description here][1]

任何想法如何让它工作?

我已经在谷歌上搜索了很长时间,但我找不到任何明确的说明如何让它们一起工作。

【问题讨论】:

    标签: python django ubuntu emacs


    【解决方案1】:

    不要使用rope/pymacs 来完成Emacs 的python,我建议使用https://github.com/tkf/emacs-jedi,这真的很棒。

    您可以使用 el-get(https://github.com/dimitri/el-get) 安装 jedi,它会为您安装必要的依赖项。

    我的 .emacs 绝地配置:

    ;;; jedi completion
    ;;; see https://github.com/tkf/emacs-jedi
    
    ;; jedi dependency: deferred
    (add-to-list 'load-path (expand-file-name
                             "~/.emacs.d/el-get/deferred"))
    ;; jedi dependency: deferred
    (add-to-list 'load-path (expand-file-name
                             "~/.emacs.d/el-get/ctable"))
    ;; jedi dependency: epc
    (add-to-list 'load-path (expand-file-name
                             "~/.emacs.d/el-get/epc"))
    (add-to-list 'load-path (expand-file-name
                             "~/.emacs.d/el-get/jedi"))
    (require 'jedi)
    (setq jedi:server-args
          '("--sys-path" "/usr/lib/python2.7/"
            "--sys-path" "/usr/lib/python2.7/site-packages"))
    
    (setq jedi:setup-keys t)
    

    【讨论】:

      【解决方案2】:

      据我所知,python 模式是 Emacs 自带的,所以你甚至不需要做任何事情来激活它。我确实在 python 中同时使用了autocompleteyasnippet,它对我来说似乎工作得很好。这是我的.emacs的相关位

      (add-to-list 'load-path
                    "~/.emacs.d/plugins/yasnippet")
      
      ...
      
      (require 'yasnippet)
      (yas/global-mode 1)
      
      ...
      (require 'auto-complete-config)
      ...
      (add-hook 'python-mode-hook 'auto-complete-mode)
      

      我的 yasn-p 版本来自 this github repo,我的自动完成版本来自 developers' site(链接到 EmacsWiki)。

      【讨论】:

      • 对不起,我以某种方式点击了反对票,而不是赞成票,它不会让我修复它。如果你在乎 - 编辑你的答案,然后我可以改变我的投票。
      【解决方案3】:

      不要认为报告的错误与 python 模式有关。 也许做一个 grep,搜索发出该错误的程序。 WRT 到选项“-nw”、图形、图像支持可能会丢失或损坏。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-09
        • 2014-07-29
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 2017-10-25
        • 2014-05-18
        相关资源
        最近更新 更多