1 ;;; company-eclim.el --- a company-mode completion back-end for eclim.
3 ;; Copyright (C) 2009 Free Software Foundation, Inc.
5 ;; Author: Nikolaj Schumacher
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
25 (eval-when-compile (require 'cl))
27 (defun company-eclim-executable-find ()
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)))
36 (defcustom company-eclim-executable
37 (or (executable-find "eclim") (company-eclim-executable-find))
38 "*Location of eclim executable"
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."
46 :type '(choice (const :tag "Off" nil)
49 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (defvar company-eclim--project-dir 'unknown)
52 (make-variable-buffer-local 'company-eclim--project-dir)
54 (defvar company-eclim--project-name 'unknown)
55 (make-variable-buffer-local 'company-eclim--project-name)
57 (defvar company-eclim--doc nil)
58 (make-variable-buffer-local 'company-eclim--doc)
60 (defun company-eclim--buffer-lines ()
61 (goto-char (point-max))
63 (while (= 0 (forward-line -1))
64 (push (buffer-substring-no-properties (point-at-bol) (point-at-eol))
68 (defun company-eclim--call-process (&rest args)
69 (let ((coding-system-for-read 'utf-8)
72 (if (= 0 (setq res (apply 'call-process company-eclim-executable nil t nil
74 (company-eclim--buffer-lines)
75 (message "Company-eclim command failed with error %d:\n%s" res
76 (buffer-substring (point-min) (point-max)))
79 (defun company-eclim--project-list ()
80 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
81 (company-eclim--call-process "project_list")))
83 (defun company-eclim--project-dir ()
84 (if (eq company-eclim--project-dir 'unknown)
85 (setq company-eclim--project-dir
88 (company-locate-dominating-file buffer-file-name ".project"))))
89 company-eclim--project-dir))
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))
98 (defun company-eclim--candidates (prefix)
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)
106 ;; FIXME: Sometimes this isn't finished when we complete.
107 (company-eclim--call-process "java_src_update"
108 "-p" (company-eclim--project-name)
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)
116 "-o" (number-to-string (1- (point)))
119 (let ((completion-ignore-case nil))
120 (all-completions prefix (mapcar 'car company-eclim--doc))))
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.
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))
130 ('interactive (company-begin-backend 'company-eclim))
131 ('prefix (and (derived-mode-p 'java-mode 'jde-mode)
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 ""))))
142 (provide 'company-eclim)
143 ;;; company-eclim.el ends here