3
votes

Edit:

I have just realized I am using version 0.2 of auto-complete.el. I think I need to use auto-complete.el version 0.1. Where can I download it from? I can only find the newer version on Google.


I am trying to set up an auto completion for Python in Emacs.

I am using Ubuntu LTS version, I have both Python and Emacs installed.

This is my .emacs file:

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

Inside the .emacs.d directory I have:

pymacs.el
init_python.el
vendor/
snippets/

inside the .emacs.d/vendor I have installed pymacs, yasnippet and auto-complete so it looks like this:

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

When I start emacs like this:

emacs -nw manage.py

I get this error message:

![enter image description here][1]

Any ideas how to get it working?

I have been googling for a long time now but I can't find any clear instructions how to get it all working together.

3

3 Answers

2
votes

Don't use rope/pymacs for python completion of Emacs, I recommand using https://github.com/tkf/emacs-jedi, which is really awesome.

And you can install jedi using el-get(https://github.com/dimitri/el-get), and it will install the necessary dependencies for you.

My .emacs jedi config:

;;; 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)
0
votes

As far as I know, python mode ships with Emacs, so you don't even need to do anything to activate it. I do use autocomplete and yasnippet together in python, and it seems to work fine for me. Here are the relevant bits of my .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)

I got my version of yasnippet from this github repo, and my version of autocomplete from the developers' site (linked to from EmacsWiki).

0
votes

Don't think the error reported is related to a python-mode whatever. Maybe do a grep, searching the program which issues that error. WRT to option "-nw", graphical, imaging support might be missed or broken.