]> code.delx.au - gnu-emacs-elpa/blob - company-ropemacs.el
company-ropemacs: defer loading pymacs and ropemacs
[gnu-emacs-elpa] / company-ropemacs.el
1 ;;; company-ropemacs.el --- A company-mode completion back-end for pysmell.el
2
3 ;; Copyright (C) 2009-2011, 2013 Free Software Foundation, Inc.
4
5 ;; Author: Nikolaj Schumacher
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22
23 ;;; Commentary:
24 ;;
25 ;; Requires pymacs Emacs package (you can get it from Marmalade),
26 ;; and on Python side: pymacs, rope, ropemacs and ropemode.
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31
32 (defun company-ropemacs--grab-symbol ()
33 (let ((symbol (company-grab-symbol)))
34 (when symbol
35 (cons symbol
36 (save-excursion
37 (let ((pos (point)))
38 (goto-char (- (point) (length symbol)))
39 (while (eq (char-before) ?.)
40 (goto-char (1- (point)))
41 (skip-syntax-backward "w_"))
42 (- pos (point))))))))
43
44 (defun company-ropemacs-doc-buffer (candidate)
45 "Return buffer with docstring of CANDIDATE if it is available."
46 (let ((doc (company-with-candidate-inserted candidate (rope-get-doc))))
47 (when doc
48 (with-current-buffer (company-doc-buffer)
49 (insert doc)
50 (current-buffer)))))
51
52 (defun company-ropemacs-location (candidate)
53 "Return location of CANDIDATE in cons form (FILE . LINE) if it is available."
54 (let ((location (company-with-candidate-inserted candidate
55 (rope-definition-location))))
56 (when location
57 (cons (elt location 0) (elt location 1)))))
58
59 (defun company-ropemacs (command &optional arg &rest ignored)
60 "A `company-mode' completion back-end for ropemacs."
61 (interactive (list 'interactive))
62 (case command
63 (init (when (and (derived-mode-p 'python-mode)
64 (not (fboundp 'rope-completions)))
65 (require 'pymacs)
66 (pymacs-load "ropemacs" "rope-")))
67 (interactive (company-begin-backend 'company-ropemacs))
68 (prefix (and (derived-mode-p 'python-mode)
69 (not (company-in-string-or-comment))
70 (company-ropemacs--grab-symbol)))
71 (candidates (mapcar (lambda (element) (concat arg element))
72 (rope-completions)))
73 (doc-buffer (company-ropemacs-doc-buffer arg))
74 (location (company-ropemacs-location arg))))
75
76 (provide 'company-ropemacs)
77 ;;; company-ropemacs.el ends here