Download
(require 'pymacs)
(require 'python-mode)
(pymacs-load "pycomplete")
(defun py-complete ()
(interactive)
(let ((window (get-buffer-window "*Completions*" 0)))
(if (and (eq last-command this-command)
window (window-live-p window) (window-buffer window)
(buffer-name (window-buffer window)))
(with-current-buffer (window-buffer window)
(if (pos-visible-in-window-p (point-max) window)
(set-window-start window (point-min))
(save-selected-window
(select-window window)
(scroll-up))))
(let* ((pymacs-forget-mutability t)
(pattern (py-symbol-near-point))
(imports (py-find-global-imports))
(completion (pycomplete-pycomplete pattern imports)))
(cond ((not (string= "" completion))
(insert completion)
(let ((win (get-buffer-window "*Completions*" 0)))
(if win (with-selected-window win (bury-buffer)))))
(t (let ((completion-list (pycomplete-get-all-completions pattern
imports))
(minibuf-is-in-use
(eq (minibuffer-window) (selected-window))))
(unless minibuf-is-in-use
(message "Making completion list..."))
(with-output-to-temp-buffer "*Completions*"
(display-completion-list completion-list pattern))
(unless minibuf-is-in-use
(message "Making completion list...%s" "done")))))))))
(defun py-find-global-imports ()
(save-excursion
(let (first-class-or-def imports)
(goto-char (point-min))
(setq first-class-or-def
(re-search-forward "^ *\\(def\\|class\\) " nil t))
(goto-char (point-min))
(setq imports nil)
(while (re-search-forward
"^\\(import \\|from \\([A-Za-z_][A-Za-z_0-9]*\\) import \\).*"
nil t)
(setq imports (append imports
(list (buffer-substring
(match-beginning 0)
(match-end 0))))))
imports)))
(define-key py-mode-map [C-tab] 'py-complete)
(provide 'pycomplete+)