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