Initial Commit
[packages] / xemacs-packages / oo-browser / java-brows.el
1 ;;!emacs
2 ;;
3 ;; FILE:         java-brows.el
4 ;; SUMMARY:      Java source code browser.
5 ;; USAGE:        GNU Emacs Lisp Library
6 ;; KEYWORDS:     c, oop, tools
7 ;;
8 ;; AUTHOR:       Bob Weiner
9 ;; ORG:          BeOpen.com
10 ;;
11 ;; ORIG-DATE:    01-Aug-95
12 ;; LAST-MOD:     10-May-01 at 12:53:09 by Bob Weiner
13 ;;
14 ;; Copyright (C) 1995, 1996, 1997, 1998  BeOpen.com
15 ;; See the file BR-COPY for license information.
16 ;;
17 ;; This file is part of the OO-Browser.
18 ;;
19 ;; DESCRIPTION:  
20 ;;
21 ;;    Use `java-browse' to invoke the java OO-Browser.  Prefix arg prompts for
22 ;;    name of Environment file.
23 ;;
24 ;; DESCRIP-END.
25
26 ;; ************************************************************************
27 ;; Other required Elisp libraries
28 ;; ************************************************************************
29
30 (mapcar 'require '(br-start br br-java-ft))
31
32 ;;; ************************************************************************
33 ;;; Public functions
34 ;;; ************************************************************************
35
36 ;;;###autoload
37 (defun java-browse (&optional env-file no-ui)
38   "Invoke the Java OO-Browser.
39 This allows browsing through Java library and system class hierarchies.  With
40 an optional non-nil prefix argument ENV-FILE, prompt for Environment file to
41 use.  Alternatively, a string value of ENV-FILE is used as the Environment
42 file name.  See also the file \"br-help\"."
43   (interactive "P")
44   (let ((same-lang (equal br-lang-prefix java-lang-prefix))
45         (load-succeeded t)
46         same-env)
47     (if same-lang
48         nil
49       ;; Save other language Environment in memory
50       (if br-lang-prefix (br-env-copy nil))
51       (setq br-lang-prefix java-lang-prefix
52             *br-save-wconfig* nil))
53     ;; `same-env' non-nil means the new Env is the previous Env or the most
54     ;; recent previous Env of the same language as the new Env
55     (setq same-env (or (equal java-env-file env-file)
56                        (and (null env-file)
57                             (or java-lib-search-dirs java-sys-search-dirs))))
58     (cond
59      (same-env
60       ;; If we just switched languages, restore the cached data for the new
61       ;; Environment.
62       (if same-lang nil (br-env-copy t))
63       ;; Environment may appear to be the same but its loading may have
64       ;; been interrupted, so ensure all variables are initialized properly.
65       (java-browse-setup env-file)
66       (if (or (null br-paths-htable) (equal br-paths-htable br-empty-htable))
67           (setq load-succeeded
68                 (br-env-try-load (or env-file br-env-file) br-env-file))))
69      ;;
70      ;; Create default Environment file specification if needed and none
71      ;; exists.
72      ;;
73      (t (or env-file (file-exists-p java-env-file)
74             (br-env-create java-env-file java-lang-prefix))
75         (or env-file (setq env-file java-env-file))
76         ;;
77         ;; Start browsing a new Environment.
78         ;;
79         (java-browse-setup env-file)
80         (setq load-succeeded (br-env-init env-file same-lang nil))
81         (if load-succeeded
82             (setq *br-save-wconfig* nil
83                   java-env-file br-env-file
84                   java-env-name br-env-name
85                   java-sys-search-dirs br-sys-search-dirs
86                   java-lib-search-dirs br-lib-search-dirs))))
87     (cond (load-succeeded
88            (if no-ui
89                nil
90              (br-browse)
91              (or (and same-lang same-env) (br-refresh))))
92           (no-ui nil)
93           (t (message "(java-browse): You must build the Environment to browse it.")))))
94
95 (defun java-class-list-filter (class-list top-only-flag)
96   "Return CLASS-LIST sans any interface entries.
97 Used when Environment classes are listed in the initial listing buffer."
98   (cond
99    (top-only-flag
100     (let (parents)
101       (delq
102        nil
103        (br-flatten
104         (mapcar
105          (function
106           (lambda (class)
107             (cond
108              ;; interface / abstract class
109              ((string-match "\\`<" class)
110               (if br-protocols-with-classes-flag
111                   (if (br-get-parents class)
112                       nil
113                     class)
114                 (java-class-list-filter
115                  (br-get-children class) t)))
116              ;;
117              ;; Regular class; because of recursion
118              ;; from the above clause, we must ensure
119              ;; that this class has no concrete parents
120              (t (setq parents (br-get-parents class))
121                 (if (or (null parents)
122                         (not (delq nil
123                                    (mapcar
124                                     (function
125                                      (lambda (parent)
126                                        (not (string-match "\\`<" parent))))
127                                     parents))))
128                     class)))))
129          class-list)))))
130     (br-protocols-with-classes-flag class-list)
131     (t (delq nil (mapcar (function
132                           (lambda (class)
133                             (if (string-match "\\`<" class)
134                                 nil
135                               class)))
136                          class-list)))))
137
138 (defun java-lang-mode ()
139   "Invoke normal-mode for programming language buffers and java-mode for any others."
140   (normal-mode)
141   (if (not (memq major-mode '(java-mode c++-mode c-mode)))
142       (java-mode)))
143
144 (defun java-mode-setup ()
145   "Load best available java major mode and set `br-lang-mode' to the function that invokes it."
146   (defalias 'br-lang-mode 'java-lang-mode)
147   (condition-case ()
148       (progn (require 'cc-mode)
149              (c-initialize-cc-mode))
150     (error nil)))
151
152 ;;; ************************************************************************
153 ;;; Internal functions
154 ;;; ************************************************************************
155
156 (defun java-browse-setup (env-file)
157   "Setup language-dependent functions for OO-Browser."
158   (br-setup-functions)
159   (java-mode-setup)
160   (br-setup-constants env-file)
161   ;; Setup to add default classes to system class table after building it.
162   ;; This must come after br-setup-constants call since it clears these
163   ;; hooks.
164   (if (fboundp 'add-hook)
165       (add-hook 'br-after-build-sys-hook 'java-add-default-classes)
166     (setq br-after-build-sys-hook '(java-add-default-classes))))
167
168 (provide 'java-brows)