]> code.delx.au - gnu-emacs-elpa/blob - company-pysmell.el
6fc3574825e24dcd18dd8364e83b3e8512f46b88
[gnu-emacs-elpa] / company-pysmell.el
1 ;;; company-pysmell.el --- a company-mode completion back-end for pysmell.el
2 ;;
3 ;; Copyright (C) 2009 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 'pysmell)
22
23 (defvar company-pysmell--available-p 'unknown)
24 (make-variable-buffer-local 'company-pysmell--available-p)
25
26 (defun company-pysmell--available-p ()
27 (if (eq company-pysmell--available-p 'unknown)
28 (setq company-pysmell--available-p
29 (company-locate-dominating-file buffer-file-name "PYSMELLTAGS"))
30 company-pysmell--available-p))
31
32 (defun company-pysmell--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 ;;;###autoload
45 (defun company-pysmell (command &optional arg &rest ignored)
46 "A `company-mode' completion back-end for pysmell.
47 This requires pysmell.el and pymacs.el."
48 (interactive (list 'interactive))
49 (case command
50 ('interactive (company-begin-backend 'company-pysmell))
51 ('prefix (and (derived-mode-p 'python-mode)
52 buffer-file-name
53 (not (company-in-string-or-comment))
54 (company-pysmell--available-p)
55 (company-pysmell--grab-symbol)))
56 ('candidates (delete "" (pysmell-get-all-completions)))))
57
58 (provide 'company-pysmell)
59 ;;; company-pysmell.el ends here