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