1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;;; Simple Python Completion Source for Auto-Complete
3 ;;;;; =================================================
5 ;;;;; This file provides a completion source for Auto-Complete:
6 ;;;;; http://www.emacswiki.org/emacs/AutoComplete
11 ;;;;; Setup Auto-Complete in the usual fashion, and make sure it gets loaded for
12 ;;;;; python buffers. Then, place this file in your load-path, and add
14 ;;;;; (require 'ac-python)
16 ;;;;; to your .emacs file (after loading Auto-Complete).
21 ;;;;; Python symbols will be completed by Auto-Complete, once Emacs learns about
22 ;;;;; these symbols. This is the short-coming of the plugin, but it's a small
25 ;;;;; To teach Emacs about symbols in imported modules, Emacs needs to execute
26 ;;;;; the Python source. This can be accomplished with `python-send-buffer` for
27 ;;;;; example, often bound to `C-c C-c`. If a python process is already running,
28 ;;;;; this is essentially instantaneous.
32 ;;;;; Version: 20110519
34 ;;;;; Author: Chris Poole <chris@chrispoole.com>
35 ;;;;; More information: http://chrispoole.com/project/ac-python
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 (defun ac-get-python-symbol-at-point ()
40 "Return python symbol at point.
42 Assumes symbol can be alphanumeric, `.' or `_'."
44 (start (ac-python-start-of-expression)))
45 (buffer-substring-no-properties start end)))
47 (defun ac-python-completion-at-point ()
48 "Returns a possibly empty list of completions for the symbol at
50 (python-symbol-completions (ac-get-python-symbol-at-point)))
52 (defun ac-python-start-of-expression ()
53 "Return point of the start of python expression at point.
55 Assumes symbol can be alphanumeric, `.' or `_'."
57 (and (re-search-backward
58 (rx (or buffer-start (regexp "[^[:alnum:]._]"))
59 (group (1+ (regexp "[[:alnum:]._]"))) point)
61 (match-beginning 1))))
63 (defvar ac-source-python
64 '((candidates . ac-python-completion-at-point)
65 (prefix . ac-python-start-of-expression)
68 "Source for python completion.")
70 (add-hook 'python-mode-hook (lambda () (add-to-list 'ac-sources 'ac-source-python)))