Initial Commit
[packages] / xemacs-packages / oo-browser / pyth-brows.el
1 ;;!emacs
2 ;;
3 ;; FILE:         pyth-brows.el
4 ;; SUMMARY:      Python source code browser.
5 ;; USAGE:        GNU Emacs Lisp Library
6 ;; KEYWORDS:     oop, tools, python
7 ;;
8 ;; AUTHOR:       Harri Pasanen / Bob Weiner
9 ;;               based on Smalltalk and C++ OO-Browsers 
10 ;; ORG:          BeOpen.com
11 ;;
12 ;; ORIG-DATE:    5-Apr-96
13 ;; LAST-MOD:     10-May-01 at 12:47:52 by Bob Weiner
14 ;;
15 ;; Copyright (C) 1996, 1997, 1998  BeOpen.com
16 ;; See the file BR-COPY for license information.
17 ;;
18 ;; This file is part of the OO-Browser.
19 ;;
20 ;; DESCRIPTION:  
21 ;;
22 ;;    Use 'python-browse' to invoke the Python OO-Browser.  Prefix arg
23 ;;    prompts for name of Environment file.
24 ;;
25 ;; DESCRIP-END.
26
27 ;;; ************************************************************************
28 ;;; Other required Elisp libraries
29 ;;; ************************************************************************
30
31 (mapcar 'require '(br-start br br-python-ft))
32
33 ;;; ************************************************************************
34 ;;; Public functions
35 ;;; ************************************************************************
36
37 ;;;###autoload
38 (defun python-browse (&optional env-file no-ui)
39   "Invoke the Python OO-Browser.
40 This allows browsing through Python library and system class hierarchies.
41 With an optional non-nil prefix argument ENV-FILE, prompt for Environment
42 file to use.  Alternatively, a string value of ENV-FILE is used as the
43 Environment file name.  See also the file \"br-help\"."
44   (interactive "P")
45   (let ((same-lang (equal br-lang-prefix python-lang-prefix))
46         (load-succeeded t)
47         same-env)
48     (if same-lang
49         nil
50       ;; Save other language Environment in memory
51       (if br-lang-prefix (br-env-copy nil))
52       (setq br-lang-prefix python-lang-prefix
53             *br-save-wconfig* nil))
54     ;; `same-env' non-nil means the new Env is the previous Env or the most
55     ;; recent previous Env of the same language as the new Env
56     (setq same-env (or (equal python-env-file env-file)
57                        (and (null env-file)
58                             (or python-lib-search-dirs python-sys-search-dirs))))
59     (cond
60      (same-env
61       ;; If we just switched languages, restore the cached data for the new
62       ;; Environment.
63       (if same-lang nil (br-env-copy t))
64       ;; Environment may appear to be the same but its loading may have
65       ;; been interrupted, so ensure all variables are initialized properly.
66       (python-browse-setup env-file)
67       (if (or (null br-paths-htable) (equal br-paths-htable br-empty-htable))
68           (setq load-succeeded
69                 (br-env-try-load (or env-file br-env-file) br-env-file))))
70      ;;
71      ;; Create default Environment file specification if needed and none
72      ;; exists.
73      ;;
74      (t (or env-file (file-exists-p python-env-file)
75             (br-env-create python-env-file python-lang-prefix))
76         (or env-file (setq env-file python-env-file))
77         ;;
78         ;; Start browsing a new Environment.
79         ;;
80         (python-browse-setup env-file)
81         (setq load-succeeded (br-env-init env-file same-lang nil))
82         (if load-succeeded
83             (setq *br-save-wconfig* nil
84                   python-env-file br-env-file
85                   python-env-name br-env-name
86                   python-sys-search-dirs br-sys-search-dirs
87                   python-lib-search-dirs br-lib-search-dirs))))
88     (cond (load-succeeded
89            (if no-ui
90                nil
91              (br-browse)
92              (or (and same-lang same-env) (br-refresh))))
93           (no-ui nil)
94           (t (message "(python-browse): You must build the Environment to browse it.")))))
95
96 ;; Don't filter Environment classes when listed.
97 (defalias 'python-class-list-filter 'br-class-list-identity)
98
99 (defun python-lang-mode ()
100   "Invoke normal-mode for programming language buffers and python-mode for any others."
101   (normal-mode)
102   (if (not (memq major-mode '(python-mode c-mode c++-mode java-mode)))
103       (python-mode)))
104
105 (defun python-mode-setup ()
106   "Load best available Python major mode and set 'br-lang-mode' to the function that invokes it."
107   (defalias 'br-lang-mode 'python-lang-mode))
108
109 ;;; ************************************************************************
110 ;;; Internal functions
111 ;;; ************************************************************************
112
113 (defun python-browse-setup (env-file)
114   "Setup language-dependent functions for OO-Browser."
115   (br-setup-functions)
116   (python-mode-setup)
117   (br-setup-constants env-file)
118   ;; Setup to add default classes to system class table after building it.
119   ;; This must come after br-setup-constants call since it clears these
120   ;; hooks.
121   (if (fboundp 'add-hook)
122       (add-hook 'br-after-build-sys-hook 'python-add-default-classes)
123     (setq br-after-build-sys-hook '(python-add-default-classes))))
124
125 (provide 'pyth-brows)