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