de6b0319b764a2c3be3c5d7712b31a63c254a1a5
[eclim-emacs.git] / eclim.el
blobde6b0319b764a2c3be3c5d7712b31a63c254a1a5
1 ;; eclim.el --- an interface to the Eclipse IDE.
2 ;;
3 ;; Copyright (C) 2009 Tassilo Horn <tassilo@member.fsf.org>
4 ;;
5 ;; This program is free software: you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation, either version 3 of the License, or
8 ;; (at your option) any later version.
9 ;;
10 ;; This program is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ;; GNU General Public License for more details.
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
18 ;;; Contributors
20 ;; - Nikolaj Schumacher <bugs * nschum de>
22 ;;; Conventions
24 ;; Conventions used in this file: Name internal variables and functions
25 ;; "eclim--<descriptive-name>", and name eclim command invocations
26 ;; "eclim/command-name", like eclim/project-list.
28 ;;* Eclim
30 (eval-when-compile (require 'cl))
32 ;;** Basics
34 (defgroup eclim nil
35 "Interface to the Eclipse IDE."
36 :group 'tools)
38 (defun eclim-executable-find ()
39 (let (file)
40 (dolist (eclipse-root '("/Applications/eclipse" "/usr/lib/eclipse"
41 "/usr/local/lib/eclipse"))
42 (and (file-exists-p (setq file (expand-file-name "plugins" eclipse-root)))
43 (setq file (car (last (directory-files file t "^org.eclim_"))))
44 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
45 (return file)))))
47 (defcustom eclim-executable
48 (or (executable-find "eclim") (eclim-executable-find))
49 "Location of eclim executable."
50 :group 'eclim
51 :type 'file)
53 (defcustom eclim-auto-save nil
54 "Determines whether to save the buffer when retrieving completions.
55 eclim can only complete correctly when the buffer has been
56 saved."
57 :group 'eclim
58 :type '(choice (const :tag "Off" nil)
59 (const :tag "On" t)))
61 (defvar eclim--project-dir nil)
62 (make-variable-buffer-local 'eclim--project-dir)
64 (defvar eclim--project-name nil)
65 (make-variable-buffer-local 'eclim--project-name)
67 (defvar eclim--doc nil)
68 (make-variable-buffer-local 'eclim--doc)
70 (defun eclim--buffer-lines ()
71 (goto-char (point-max))
72 (let (lines)
73 (while (= 0 (forward-line -1))
74 (push (buffer-substring-no-properties (line-beginning-position)
75 (line-end-position))
76 lines))
77 lines))
79 (defun eclim--call-process (&rest args)
80 (let ((coding-system-for-read 'utf-8))
81 (with-temp-buffer
82 (if (= 0 (apply 'call-process eclim-executable nil t nil
83 "-command" args))
84 (eclim--buffer-lines)
85 ;; TODO: A more meaningful error message
86 (message "Eclim command failed")
87 nil))))
89 (defun eclim--project-dir ()
90 "Return this file's project root directory."
91 (or eclim--project-dir
92 (setq eclim--project-dir
93 (directory-file-name
94 (expand-file-name
95 (locate-dominating-file buffer-file-name ".project"))))))
97 (defun eclim--project-name ()
98 (or eclim--project-name
99 (setq eclim--project-name
100 (car (cddr (assoc (eclim--project-dir)
101 (eclim/project-list)))))))
103 (defun eclim/project-list ()
104 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
105 (eclim--call-process "project_list")))
107 ;;** The minor mode and its keymap
109 (defvar eclim-mode-map
110 (let ((map (make-sparse-keymap)))
112 "The keymap used in `eclim-mode'.")
114 (define-minor-mode eclim-mode
115 "An interface to the Eclipse IDE."
117 "Eclim"
118 eclim-mode-map
119 (if eclim-mode
120 (progn
121 ;; Set project dir and name.
122 (eclim--project-dir)
123 (eclim--project-name))
124 (kill-local-variable 'eclim--project-dir)
125 (kill-local-variable 'eclim--project-name)))