Initial Commit
[packages] / xemacs-packages / jde / lisp / jde-plugins.el
1 ;;; jde-plugins.el -- Support for JDEE plugins
2 ;; $Revision: 1.7 $ $Date: 2004/09/21 04:33:33 $ 
3
4 ;; Author: Paul Kinnucan <pkinnucan@attbi.com>
5 ;; Maintainer: Paul Kinnucan
6 ;; Keywords: java, tools
7
8 ;; Copyright (C) 2003, 2004 Paul Kinnucan.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27
28 (require 'eieio)
29 (require 'executable)
30
31
32 (defcustom jde-plugins-directory (expand-file-name "plugins" (jde-root))
33   "Location of the JDEE's plugins directory."
34   :group 'jde
35   :type 'file)
36
37 (defclass jde-plugin ()
38   ((bsh-cp    :initarg :bsh-cp
39               :type list
40               :documentation "Beanshell classpath for this plugin.")
41    (menu-spec :initarg :menu-spec
42               :type list
43               :documentation "Specifies menu for this plugin.")
44    (plugins   :type list
45               :allocation :class
46               :initform nil
47               :documentation 
48              "Installed plugins."))
49 "Class of plugins.")
50
51
52 (defun jde-pi-register (plugin)
53   "Register PLUGIN, which must be an object of
54 type `jde-plugin'."
55   (oset-default 
56    'jde-plugin 
57    plugins
58    (cons plugin (oref 'jde-plugin plugins))))
59
60
61 (defun jde-pi-get-plugin-dir (plugin)
62   "Returns the path of the directory containing PLUGIN."
63   (expand-file-name plugin jde-plugins-directory))
64   
65
66 (defun jde-pi-load-plugin (plugin)
67   "Loads the plugin named PLUGIN. This function assumes that the plugin resides
68 in a subdirectory of the JDEE's plugins directory named PLUGIN and that this 
69 subdirectory contains a subdirectory name lisp that contains
70 a file named jde-PLUGIN.el. This function loads jde-PLUGIN.el."
71   (let* ((plugin-dir (expand-file-name plugin jde-plugins-directory))
72          (plugin-lisp-dir (expand-file-name "lisp" plugin-dir))
73          (plugin-lisp-package-name (concat "jde-" plugin))
74          (plugin-lisp-file-name (concat plugin-lisp-package-name ".el"))
75          (plugin-lisp-file
76           (expand-file-name 
77            plugin-lisp-file-name
78            plugin-lisp-dir)))
79     (if (file-exists-p plugin-lisp-file)
80         (progn
81           (add-to-list 'load-path plugin-lisp-dir)
82           (require (intern plugin-lisp-package-name)))
83       (error "JDEE plugin Lisp file %s missing" plugin-lisp-file-name))))
84   
85
86 (defun jde-pi-load-plugins ()
87   "Loads the plugins in the JDEE's plugins directory."
88   (interactive)
89   (if (file-exists-p jde-plugins-directory)
90       (let ((plugins
91              (delq
92               nil
93               (mapcar
94                (lambda (file)
95                  (let ((file-name (file-name-nondirectory file)))
96                    (if (and
97                         (file-directory-p file) 
98                         (not (string= file-name "."))
99                         (not (string= file-name ".."))
100                         (not (string= file-name "CVS"))
101                         (not (string= file-name "RCS")))
102                        file-name)))
103                (directory-files jde-plugins-directory t)))))
104         (loop for plugin in plugins do
105           (jde-pi-load-plugin plugin)))))
106
107 (jde-pi-load-plugins)
108
109 (defun jde-pi-get-bsh-classpath ()
110   "Get the plugin directories and jar files to include in the Beanshell classpath."
111   (let ((plugins (oref 'jde-plugin plugins))
112         classpath)
113     (loop for plugin in plugins do
114           (setq classpath (append classpath (oref plugin bsh-cp))))
115     classpath))
116     
117
118 (defun jde-pi-install-plugins ()
119   "This command installs any plugin distributables that it
120 finds in the JDEE's plugins directory. It assumes that
121 the distributables are in jar or zip format and that the
122 jar program is on the system path."
123   (interactive)
124
125   (assert (executable-find "jar") nil 
126     "Cannot find the jar program on the system path.")
127
128   (let ((zip-files
129          (directory-files jde-plugins-directory nil ".*[.]\\(zip\\|jar\\)")))
130
131     (when zip-files
132       (let ((buf (get-buffer-create "*plugins*")))
133           (with-current-buffer buf
134             (erase-buffer)
135             (insert "JDEE Plugin Installation Log")
136             (pop-to-buffer buf)
137             (cd jde-plugins-directory)
138             (loop for zip-file in zip-files do
139                   (let ((result 
140                          (shell-command-to-string
141                         (concat "jar xvf " zip-file))))
142                     (insert "\n\n")
143                     (insert (format "Installing %s ..." 
144                                     (file-name-sans-extension zip-file)))
145                     (insert "\n\n")
146                     (insert result)))
147             (insert "\n\nInstallation complete"))))))
148
149
150 (defun jde-plugin-make-menu-spec ()
151   (if (oref 'jde-plugin plugins)
152       (append
153        (list "JDEpi")
154        (delq
155         nil
156         (mapcan
157          (lambda (plugin)
158            (oref plugin menu-spec))
159          (oref 'jde-plugin plugins))))))
160
161 (defvar jde-plugin-mode-map
162   (let ((km (make-sparse-keymap))
163         (menu-spec (jde-plugin-make-menu-spec)))
164     (if menu-spec
165         (easy-menu-define jde-plugin-menu km "JDEE Plugin Minor Mode Menu"
166           menu-spec))
167     km)
168   "Keymap for JDEE plugin minor mode.")
169
170 (defvar jde-plugin-minor-mode nil
171   "Non-nil if JDEE plugin minor mode is enabled.")
172
173 (make-variable-buffer-local 'jde-plugin-minor-mode)
174
175 (defun jde-plugin-minor-mode (&optional arg)
176   "Toggle JDEE plugin minor mode.
177 With prefix argument ARG, turn on if positive, otherwise off..
178
179 \\{jde-plugin-mode-map}"
180   (interactive
181    (list (or current-prefix-arg
182              (if jde-plugin-minor-mode 0 1))))
183
184   (setq jde-plugin-minor-mode
185         (if arg
186             (>
187              (prefix-numeric-value arg)
188              0)
189           (not jde-plugin-minor-mode)))
190
191   (if (featurep 'xemacs)
192       (let ((menu-spec (jde-plugin-make-menu-spec)))
193         (if menu-spec
194             (if jde-plugin-minor-mode
195                 (easy-menu-add menu-spec jde-plugin-mode-map)
196               (easy-menu-remove menu-spec))))))
197
198 (semantic-add-minor-mode 'jde-plugin-minor-mode " plugin" jde-plugin-mode-map)
199
200
201 (provide 'jde-plugins)
202
203 ;; Change History 
204
205 ;;
206 ;; $Log: jde-plugins.el,v $
207 ;; Revision 1.7  2004/09/21 04:33:33  paulk
208 ;; Teach JDEE to ignore a CVS or RCS directory in the plugins directory.
209 ;;
210 ;; Revision 1.6  2004/02/21 07:59:20  paulk
211 ;; Fixed nasty bug in jde-pi-get-bsh-classpath.
212 ;;
213 ;; Revision 1.5  2003/06/21 09:01:43  paulk
214 ;; Fix typo. Thanks to Martin Schamberg.
215 ;;
216 ;; Revision 1.4  2003/06/18 05:05:30  paulk
217 ;; Fix bug in jde-plugin-minor-mode's menu-installing code for XEmacs.
218 ;;
219 ;; Revision 1.3  2003/06/09 05:45:26  paulk
220 ;; Test for nonexistence of plugins directory.
221 ;;
222 ;; Revision 1.2  2003/04/28 04:47:02  paulk
223 ;; Implemented installation of menus and beanshell classpath for plugins.
224 ;;
225 ;; Revision 1.1  2003/04/16 04:08:46  paulk
226 ;; Initial revision.
227 ;;
228 ;;
229
230 ;; End of jde-plugins.el