]> code.delx.au - gnu-emacs-elpa/blob - packages/company-0.5/company-ropemacs.el
f92d5ce6bf696a96e0e1022127412673d7be4dc2
[gnu-emacs-elpa] / packages / company-0.5 / company-ropemacs.el
1 ;;; company-ropemacs.el --- a company-mode completion back-end for pysmell.el
2
3 ;; Copyright (C) 2009, 2010 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 ;;; Code:
23
24 (eval-when-compile (require 'cl))
25 (require 'pymacs)
26
27 (unless (fboundp 'rope-completions)
28 (pymacs-load "ropemacs" "rope-"))
29
30 (unless (fboundp 'rope-completions)
31 (error "rope-completions not found, try development version of ropemacs"))
32
33 (defun company-ropemacs--grab-symbol ()
34 (let ((symbol (company-grab-symbol)))
35 (when symbol
36 (cons symbol
37 (save-excursion
38 (let ((pos (point)))
39 (goto-char (- (point) (length symbol)))
40 (while (eq (char-before) ?.)
41 (goto-char (1- (point)))
42 (skip-syntax-backward "w_"))
43 (- pos (point))))))))
44
45 (defun company-ropemacs-doc-buffer (candidate)
46 "Return buffer with docstring of CANDIDATE if it is available."
47 (let ((doc (company-with-candidate-inserted candidate (rope-get-doc))))
48 (when doc
49 (with-current-buffer (company-doc-buffer)
50 (insert doc)
51 (current-buffer)))))
52
53 (defun company-ropemacs-location (candidate)
54 "Return location of CANDIDATE in cons form (FILE . LINE) if it is available."
55 (let ((location (company-with-candidate-inserted candidate
56 (rope-definition-location))))
57 (when location
58 (cons (elt location 0) (elt location 1)))))
59
60 (defun company-ropemacs (command &optional arg &rest ignored)
61 "A `company-mode' completion back-end for ropemacs."
62 (interactive (list 'interactive))
63 (case command
64 ('interactive (company-begin-backend 'company-ropemacs))
65 ('prefix (and (derived-mode-p 'python-mode)
66 (not (company-in-string-or-comment))
67 (company-ropemacs--grab-symbol)))
68 ('candidates (mapcar (lambda (element) (concat arg element))
69 (rope-completions)))
70 ('doc-buffer (company-ropemacs-doc-buffer arg))
71 ('location (company-ropemacs-location arg))))
72
73 (provide 'company-ropemacs)
74 ;;; company-ropemacs.el ends here