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