]> git.rkrishnan.org Git - .emacs.d.git/blob - emacs/ac-python.el
remove toolbar and menubar
[.emacs.d.git] / emacs / ac-python.el
1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2 ;;;;; Simple Python Completion Source for Auto-Complete
3 ;;;;; =================================================
4 ;;;;;
5 ;;;;; This file provides a completion source for Auto-Complete:
6 ;;;;; http://www.emacswiki.org/emacs/AutoComplete
7 ;;;;;
8 ;;;;; Installation
9 ;;;;; ------------
10 ;;;;; 
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
13 ;;;;; 
14 ;;;;;     (require 'ac-python)
15 ;;;;; 
16 ;;;;; to your .emacs file (after loading Auto-Complete).
17 ;;;;; 
18 ;;;;; Usage
19 ;;;;; -----
20 ;;;;; 
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
23 ;;;;; price to pay.
24 ;;;;; 
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.
29 ;;;;;
30 ;;;;; ---
31 ;;;;;
32 ;;;;; Version: 20110519
33 ;;;;; License: MIT
34 ;;;;; Author: Chris Poole <chris@chrispoole.com>
35 ;;;;; More information: http://chrispoole.com/project/ac-python
36 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
37
38
39 (defun ac-get-python-symbol-at-point ()
40   "Return python symbol at point.
41
42 Assumes symbol can be alphanumeric, `.' or `_'."
43   (let ((end (point))
44         (start (ac-python-start-of-expression)))
45     (buffer-substring-no-properties start end)))
46
47 (defun ac-python-completion-at-point ()
48   "Returns a possibly empty list of completions for the symbol at
49 point."
50   (python-symbol-completions (ac-get-python-symbol-at-point)))
51
52 (defun ac-python-start-of-expression ()
53   "Return point of the start of python expression at point.
54
55 Assumes symbol can be alphanumeric, `.' or `_'."
56   (save-excursion
57     (and (re-search-backward
58           (rx (or buffer-start (regexp "[^[:alnum:]._]"))
59               (group (1+ (regexp "[[:alnum:]._]"))) point)
60           nil t)
61          (match-beginning 1))))
62
63 (defvar ac-source-python
64   '((candidates . ac-python-completion-at-point)
65     (prefix . ac-python-start-of-expression)
66     (symbol . "f")
67     (requires . 2))
68   "Source for python completion.")
69
70 (add-hook 'python-mode-hook (lambda () (add-to-list 'ac-sources 'ac-source-python)))
71
72 (provide 'ac-python)