DISQUS

Enigma Curry: AutoComplete.el : Python Code Completion in Emacs

  • Vladimir Kazanov · 11 months ago
    I tried your code, and got this "cannot find (rope-completion) function" error. I got both ropemacs and AC mode installed and properly working on Emacs 22.

    Any ideas? The whole thing feels like a great idea, I`d be glad to make it work in my setting.
  • eric casteleijn · 11 months ago
    Actually, I have always preferred the relatively braindead autocompletion that emacs gives you by looking in all open buffers, which doesn't use any dropdowns or separate buffers, you just cycle through the possibilities until you have the one you need. I find this interferes much less with the flow of typing, and it's exactly the same behavior you get in the shell.

    I have tab bound to indent or expand, depending Something like this:

    <pre lang="lisp">
    (defun indent-or-expand (arg)
    "Either indent according to mode, or expand the word preceding
    point."
    (interactive "*P")
    (if (and
    (or (bobp) (= ?w (char-syntax (char-before))))
    (or (eobp) (not (= ?w (char-syntax (char-after))))))
    (dabbrev-expand arg)
    (indent-according-to-mode)))

    (defun my-tab-fix ()
    (local-set-key [tab] 'indent-or-expand))

    (add-hook 'c-mode-hook 'my-tab-fix)
    (add-hook 'sh-mode-hook 'my-tab-fix)
    (add-hook 'emacs-lisp-mode-hook 'my-tab-fix)
    (add-hook 'python-mode-hook 'my-tab-fix)

    </pre>
  • ryan · 11 months ago
    @Eric

    I lived that way in Emacs for a long time, and in non-python buffers that's how I still live. AutoComplete.el can still use this functionality by adding different sources to the python-mode-hook:

    <pre lang="lisp">
    (set (make-local-variable 'ac-sources)
    (append ac-sources '(ac-source-that-you-want)))
    </pre>


    Valid, predefined sources include:

    1) ac-source-words-in-buffer
    2) ac-source-files-in-current-dir

    You can even get full dabbrev-expand as you suggested using this contributed source:
    http://www.emacswiki.org/cgi-bin/emacs/ac-dabbr...

    As far as interfering with the "flow" of coding, I didn't like the way AutoComplete.el by default starts completing as you type. In the code above I have some advice functions that turn off this behavior and only complete when I explicitly press TAB.
  • Mark A. Hershberger · 11 months ago
    Doxymacs (http://doxymacs.sourceforge.net/) lets you look up documentation. Might be a start.
  • Jimmy Wu · 11 months ago
    nice post!!
    There is a problem I just meet,when I press TAB
    the minibuffer displayed a message
    Symbol's function definition is void rope-completions

    ps:I have installed pyemacs 0.24beta,rope0.9.1,ropemacs0.6
  • ryan · 11 months ago
    @Jimmy, @Vladimir

    rope-completions is one of the very newest functions in ropemode.

    Make sure you're installing the development version of rope, ropemacs and ropemode from mercurial (hg) as shown above.
  • long long ago · 11 months ago
    Do you use python3.0?
    I found some problem on python3.0,it doesn't work.
  • kaiputer · 11 months ago
    Thanks! These settings work perfect! I'm using emacs 23 cvs in Widnows.python 2.5.4.
  • ryan · 11 months ago
    @long long ago

    No, I haven't done much py3k stuff. Rope has a branch specifically for py3k, but I have not used it: http://bitbucket.org/agr/rope_py3k/

    @kaiputer, great! I'm glad this was useful to you. :)
  • Umut Uygar · 11 months ago
    These changes are also applied to my Emacs environment on Github.


    Because of this sentence I found your another post here and applied the steps in this post.

    It looks I'm getting the same error with Jimmy Wu;
    Symbol's function definition is void rope-completions

    Should I uninstall everything and redo everything as described in this post?
  • ryan · 11 months ago
    @Umut Uygar,

    This post has everything you need. You don't need to go to that other link. It's just to let people know that these changes are also in my git repository, but that's an alternative to just following the directions in this post.

    The problem you are seeing with rope-completions is that you don't have the development version of ropemode/ropemacs installed. If you follow the above directions on checking them out with "hg" you should resolve that problem.
  • Will · 11 months ago
    Works great on Aquamacs OS X although you need to change the wget to curl -C - -O url. The only problem is the menu layout is a bit messed up (i.e entries don't align down and get staggered) ill look into finding a solution for that.
  • Dave · 11 months ago
    This is really cool.

    I'm in the middle of trying to add support for auto-complete into IPython inside emacs and found this helpful. I was wondering about the defadvice you add to ac-start and ac-cleanup. The docs for auto-complete suggest that binding [tab] to ac-start should be enough for things to work but it doesn't without the extra advice.
  • ryan · 11 months ago
    @Dave

    Glad you're getting some good use out of it! The reason for the defadvice is pretty technical and it may be actually covering up a bug in autocomplete.el .. I'm not too sure, but it works.

    A deeper explanation:

    1) By default autocomplete has ac-auto-start turned on. This makes it so that when you're typing along anything that is "completeable" automatically pops up the drop down menu. First of all, I don't like that behaviour, I want it to be explicit, meaning I only want it to complete when I actually press TAB. Secondly, if you were to type something like "string.capitalize" it would start to complete the "string" part, but if you did NOT utilize the completion function but instead just kept on typing up to the "." then autocomplete doesn't complete on the "capitalize" part. In this case you have to backspace over the "." and type the "." again and it starts to complete the "capitalize" part. That "bug" is most likely in my code, not in autocomplete.el, Like I said, I didn't write it with ac-auto-start in mind.

    2)This is probably a bug in autocomplete.el. Once you turn off ac-auto-start and you start a completion it brings up the drop down menu, but as you start typing to narrow down the choices, the dropdown box does not update. For whatever reason, the box ONLY updates if you have ac-auto-start on. That's where the defadvice comes in. ac-auto-start is still off in the default case. When you press TAB to start completing, ac-auto-start is temporarily turned on until the completion is done at which time the ac-auto-start is turned back off. That way the dropdown box is only turned on when you press TAB, but it still updates as you type a completion.

    Clear as mud? Feel free to ask me anything you didn't understand.
  • Luke · 11 months ago
    Hi,

    nice setup :).

    How can I make completion go faster though?
    When I do

    import sys
    print sys.pa<TAB>

    rope/pymacs looks through ALL the files on my hard drive which takes a while. Why doesn't it just search in my Python libraries?
  • Luke · 11 months ago
    D'oh! I guess shouldn't have selected my home (and everything that's mounted there) as the project root...
  • Joe · 11 months ago
    What's up with this line, and why is it needed?

    ln -s ../ropemode/ropemode ropemacs/

    That directory doesn't exist so ln raises an error.
  • Joe · 11 months ago
    Doh, mistyped it.
  • Luke · 10 months ago
    Example:

    if 0 == 1:
    __print 0
    __print 1

    If point is on the 'p' of the second line, TAB should cycle the whole line through indentation alternatives. Instead, I get an auto-complete list with

    ArithmeticError
    AssertionError
    AttributeError
    etc...

    Any idea?
  • EnigmaCurry · 10 months ago
    Ever since posting this, I've been tweaking this. I think I understand what
    you're talking about and I changed this in my github (linked above). Did you
    check there? The relevant file is ryan-python.el.

    I'm leaving this post as is, for posterity, so check my github for updates.
  • Francisco Gama T. R. · 10 months ago
    hey, all great.

    Now, maybe I would change the line:
    ln -s vendor/yasnippet-0.5.9/snippets/ .
    for
    ln -s vendor/yasnippet-0.5.9/snippets . <- required in Mac OS i.e.

    and add:
    rm vendor/*.tar*

    Thank you a lot!
  • Trevor · 10 months ago
    Does this configuration work under Windows? I remember having problems with Pymacs before on windows.
  • EnigmaCurry · 10 months ago
    Sorry, I haven't tried. I'd love to know though, so if you try it out, let
    me know.
  • ovnicraft · 9 months ago
    Great post, i followed exactly as you post it, but i have this erro when press TAB
    Symbol's value as variable is void: ac-source-yasnippet
  • EnigmaCurry · 9 months ago
    Hi ovnicraft,

    The link that I had for autocomplete.el got changed to point to version
    0.2.0, which made some incompatible changes, I modified the post to point to
    the 0.1.0 version which should work better for you.

    Probably soon I'll update this post for version 0.2.0.
  • ovnicraft · 9 months ago
    Thx for all, now auto-complete works fine, but i see dont autocompletion with my PYTHONPATH,, i have installed django and i want to work with that, where i must configure that?

    thx
  • EnigmaCurry · 9 months ago
    Can you give me an example? Have you imported the module you want to
    complete?
  • Maik Beckmann · 6 months ago
    $ wget http://www.emacswiki.org/emacs/?action=browse;i...
    results in "ERROR 400: BAD REQUEST"

    There is only revision 6 available now (for whatever reason). This one works though:
    $ wget http://tinyurl.com/mbfr74

    Best,
    -- Maik
  • Nicolas Pinto · 4 months ago
    This is a wonderful post! Thank you so much!!

    Any update on the updated version with auto-complete 0.2.0 ?

    Thanks again.

    Cheers,

    Nicolas
  • ap · 9 months ago
    great work Ryan. i am an emacs newbie so i have a couple of questions.

    1. first how do i make sure auto completion is case insensitive e.g typing django.http.h listing django.http.HttpResponse. something of that sort.

    2. how do i change the red color used for python warning and errors

    3. how do i make key-bindings work with only specific modes

    thanks
  • amcmorl · 9 months ago
    I'm having trouble getting the tab functions to work correctly. If I use the version of "Ryan's python specific tab completion" above, I get autocompletion and indenting, but no yasnippet completion, whereas if I use the longer tab completion code on github I get only yasnippet completion and no indentation or autocompletion. I'm using emacs 22, autocomplete 0.1.0, yasnippet-0.5.10. Any thoughts were I'm going wrong? This looks like a great set of tools- thanks for the post.
  • Travis · 8 months ago
    Was curious if you've gotten around to updating to 0.2.0, as it seems the 0.1.0 is no longer available online...
  • CH · 8 months ago
    I’m a little confused - It appears when I set flymake-mode to be automatically on for python mode, py-help-at-point doesn’t work. I tried switching from python-mode.el to python.el, but not this doesn’t seem to solve it. binding the flymake-mode function to a key solves it, but I'm not sure why - something to do with the way python-mode evaluates python expressions?
  • CH · 8 months ago
    Another problem; In using just python.el, not python-mode.el; yas has stopped epanding e.g. for<TAB> will continually give me a menu selection, with 'for' in it, but not do the snippet epansion - Is is not detecting that the rope-expansion has already been done? What functionality from python-mode.el does all of this rely on!?
  • sadin · 8 months ago
    If you could integrate this code:
    http://www.rwdev.eu/articles/emacspyeng
    With your code above, I think you would have the best damn autocompletion/documentation for Python out there.
  • Venkatesh Sellappa · 8 months ago
    Hi Ryan,

    Very helpful and useful post. Thanks.

    I have written a complete newbies startup guide for emacs+python here : http://venkys-dotplan.blogspot.com and included this technique of auto-completion as part of that.
  • Andrew · 7 months ago
    Sweet guide. So far it's pretty nifty. For everyone looking for 0.1.0 of auto-complete.el, I did some heavy google cache searching and found it. Here's a clean version you can curl/wget/lynx if you so desire.
    http://nerdtopia.org/auto-complete.el

    Once I get the hang of emacs/elisp (only been using it for a week or two, vi convert here.), I'll try to write something that works with .2. Until then, happy hacking!
  • Richard · 6 months ago
    Your install for rope,ropemode and ropemacs got me going with company-mode

    Well done.

    What a mess it can be setting up python in emacs!
  • ikuta · 5 months ago
    HI
    this function is so cool, but I got the error "Symbol's value as variable is void: ac-source-yasnippet", I read the posts and see must download autocomplete.el 0.1.0, but it's not available on the wiki, could you provide me 0.1.0?
    Thanks a lot!
  • Christopher D. Walborn · 5 months ago
    Your Emacs & Python tips are a great help and I'm making heavy use of your Emacs environment. But I just wanted to report that even *with* the latest development versions of Rope, Ropemacs, etc. cloned from bitbucket, there is no rope-completions function to be found.

    prefix-list-elements: Symbol's function definition is void: rope-completion

    Is the current rope(macs|mode) missing rope-completions or is something maybe not being loaded?

    Anyway, thanks for all the helpful information.

    Christopher
  • ighost · 4 months ago
    I flow your setting, but it can't work!
    my emacs:23.1-winnt
    and my os: winxp
  • seagle0128 · 3 months ago
    Oh, When will you update this great post to support autocomplete version 0.20 or 0.30? I am eager to know that. I will appreciate it :)
  • rileyrgdev · 2 months ago
    The other option is to use company-mode which has built in rope completion for python files. My own setup which includes flymake to provide a live "pylint" presence in a python buffer is here:

    http://richardriley.net/projects/emacs/dotprogr...
  • kost BebiX · 1 month ago
    Hi! So what about auto-complete 0.2 ? Could you please look at that? Thank you very much for all this! Your article is magic :-)
  • prasant · 3 weeks ago
    In Windows 7 i get yasnippet completion but not python code completion. I probably have some configurations elsewhere that interferes with your instructions.

    But your emacs configuration on github works fine; after adding a few missing files and renaming some directories. Thanks very much.