better eclim error messages
[eclim-emacs.git] / eclim.el
blobddef1b5717dca68ccf915bd49828f93e0cef66ee
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
43 (setq file (expand-file-name "plugins" eclipse-root)))
44 (setq file (car (last (directory-files file t "^org.eclim_"))))
45 (file-exists-p (setq file (expand-file-name "bin/eclim" file)))
46 (return file)))))
48 (defcustom eclim-executable
49 (or (executable-find "eclim") (eclim-executable-find))
50 "Location of eclim executable."
51 :group 'eclim
52 :type 'file)
54 (defcustom eclim-auto-save nil
55 "Determines whether to save the buffer when retrieving completions.
56 eclim can only complete correctly when the buffer has been
57 saved."
58 :group 'eclim
59 :type '(choice (const :tag "Off" nil)
60 (const :tag "On" t)))
62 (defvar eclim--project-dir nil)
63 (make-variable-buffer-local 'eclim--project-dir)
65 (defvar eclim--project-name nil)
66 (make-variable-buffer-local 'eclim--project-name)
68 (defun eclim--buffer-lines ()
69 (goto-char (point-max))
70 (let (lines)
71 (while (= 0 (forward-line -1))
72 (push (buffer-substring-no-properties (line-beginning-position)
73 (line-end-position))
74 lines))
75 lines))
77 (defun eclim--error-buffer (text)
78 (let ((errbuf (get-buffer-create "*Eclim errors*")))
79 (set-buffer errbuf)
80 (insert text)
81 (setq buffer-read-only t)
82 (display-buffer errbuf t)))
84 (defun eclim--call-process (&rest args)
85 (let ((coding-system-for-read 'utf-8))
86 (with-temp-buffer
87 (if (= 0 (apply 'call-process eclim-executable nil t nil
88 "-command" args))
89 (eclim--buffer-lines)
90 (eclim--error-buffer
91 (buffer-substring-no-properties
92 (point-min) (point-max)))
93 nil))))
95 (defun eclim--project-dir ()
96 "Return this file's project root directory."
97 (or eclim--project-dir
98 (setq eclim--project-dir
99 (directory-file-name
100 (expand-file-name
101 (locate-dominating-file buffer-file-name ".project"))))))
103 (defun eclim--project-name ()
104 (or eclim--project-name
105 (setq eclim--project-name
106 (car (cddr (assoc (eclim--project-dir)
107 (eclim/project-list)))))))
109 (defun eclim/project-list ()
110 (mapcar (lambda (line) (nreverse (split-string line " *- *" nil)))
111 (eclim--call-process "project_list")))
113 ;;** The minor mode and its keymap
115 (defvar eclim-mode-map
116 (let ((map (make-sparse-keymap)))
118 "The keymap used in `eclim-mode'.")
120 (define-minor-mode eclim-mode
121 "An interface to the Eclipse IDE."
123 "Eclim"
124 eclim-mode-map
125 (if eclim-mode
126 (progn
127 ;; Set project dir and name.
128 (eclim--project-dir)
129 (eclim--project-name))
130 (kill-local-variable 'eclim--project-dir)
131 (kill-local-variable 'eclim--project-name)))