]> code.delx.au - gnu-emacs-elpa/blob - packages/company-0.5/company-eclim.el
(debbugs-emacs): New function and modes for listing the Emacs bugs, reading them...
[gnu-emacs-elpa] / packages / company-0.5 / company-eclim.el
1 ;;; company-eclim.el --- a company-mode completion back-end for eclim.
2
3 ;; Copyright (C) 2009 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 (require 'company)
25 (eval-when-compile (require 'cl))
26
27 (defun company-eclim-executable-find ()
28 (let (file)
29 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
30 "/usr/local/lib/eclipse"))
31 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
32 (setq file (car (last (directory-files file t "^org.eclim_"))))
33 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
34 (return file)))))
35
36 (defcustom company-eclim-executable
37 (or (executable-find "eclim") (company-eclim-executable-find))
38 "*Location of eclim executable"
39 :group 'company
40 :type 'file)
41
42 (defcustom company-eclim-auto-save t
43 "*Determines whether to save the buffer when retrieving completions.
44 eclim can only complete correctly when the buffer has been saved."
45 :group 'company
46 :type '(choice (const :tag "Off" nil)
47 (const :tag "On" t)))
48
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
50
51 (defvar company-eclim--project-dir 'unknown)
52 (make-variable-buffer-local 'company-eclim--project-dir)
53
54 (defvar company-eclim--project-name 'unknown)
55 (make-variable-buffer-local 'company-eclim--project-name)
56
57 (defvar company-eclim--doc nil)
58 (make-variable-buffer-local 'company-eclim--doc)
59
60 (defun company-eclim--buffer-lines ()
61 (goto-char (point-max))
62 (let (lines)
63 (while (= 0 (forward-line -1))
64 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
65 lines))
66 lines))
67
68 (defun company-eclim--call-process (&rest args)
69 (let ((coding-system-for-read 'utf-8)
70 res)
71 (with-temp-buffer
72 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
73 "-command" args)))
74 (company-eclim--buffer-lines)
75 (message "Company-eclim command failed with error %d:\n%s" res
76 (buffer-substring (point-min) (point-max)))
77 nil))))
78
79 (defun company-eclim--project-list ()
80 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
81 (company-eclim--call-process "project_list")))
82
83 (defun company-eclim--project-dir ()
84 (if (eq company-eclim--project-dir 'unknown)
85 (setq company-eclim--project-dir
86 (directory-file-name
87 (expand-file-name
88 (company-locate-dominating-file buffer-file-name ".project"))))
89 company-eclim--project-dir))
90
91 (defun company-eclim--project-name ()
92 (if (eq company-eclim--project-name 'unknown)
93 (setq company-eclim--project-name
94 (car (cddr (assoc (company-eclim--project-dir)
95 (company-eclim--project-list)))))
96 company-eclim--project-name))
97
98 (defun company-eclim--candidates (prefix)
99 (interactive "d")
100 (let ((project-file (file-relative-name buffer-file-name
101 (company-eclim--project-dir)))
102 (project-name (company-eclim--project-name)))
103 (when company-eclim-auto-save
104 (when (buffer-modified-p)
105 (basic-save-buffer))
106 ;; FIXME: Sometimes this isn't finished when we complete.
107 (company-eclim--call-process "java_src_update"
108 "-p" (company-eclim--project-name)
109 "-f" project-file))
110 (setq company-eclim--doc
111 (mapcar (lambda (line)
112 (cdr (split-string line "|" nil)))
113 (company-eclim--call-process
114 "java_complete" "-p" (company-eclim--project-name)
115 "-f" project-file
116 "-o" (number-to-string (1- (point)))
117 "-e" "utf-8"
118 "-l" "standard"))))
119 (let ((completion-ignore-case nil))
120 (all-completions prefix (mapcar 'car company-eclim--doc))))
121
122 (defun company-eclim (command &optional arg &rest ignored)
123 "A `company-mode' completion back-end for eclim.
124 eclim provides access to Eclipse Java IDE features for other editors.
125
126 Completions only work correctly when the buffer has been saved.
127 `company-eclim-auto-save' determines whether to do this automatically."
128 (interactive (list 'interactive))
129 (case command
130 ('interactive (company-begin-backend 'company-eclim))
131 ('prefix (and (derived-mode-p 'java-mode 'jde-mode)
132 buffer-file-name
133 company-eclim-executable
134 (company-eclim--project-name)
135 (not (company-in-string-or-comment))
136 (or (company-grab-symbol) 'stop)))
137 ('candidates (company-eclim--candidates arg))
138 ('meta (cadr (assoc arg company-eclim--doc)))
139 ;; because "" doesn't return everything
140 ('no-cache (equal arg ""))))
141
142 (provide 'company-eclim)
143 ;;; company-eclim.el ends here