home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / oobr / br-java-ft.el < prev    next >
Encoding:
Text File  |  1995-08-29  |  34.0 KB  |  955 lines

  1. ;;!emacs
  2. ;;
  3. ;; FILE:         br-java-ft.el
  4. ;; SUMMARY:      Java OO-Browser class and member functions.
  5. ;; USAGE:        GNU Emacs Lisp Library
  6. ;; KEYWORDS:     java, oop, tools
  7. ;;
  8. ;; AUTHOR:       Bob Weiner
  9. ;; ORG:          Motorola Inc.
  10. ;;
  11. ;; ORIG-DATE:    01-Aug-95
  12. ;; LAST-MOD:     26-Aug-95 at 15:18:27 by Bob Weiner
  13. ;;
  14. ;; Copyright (C) 1995  Free Software Foundation, Inc.
  15. ;; See the file BR-COPY for license information.
  16. ;;
  17. ;; This file is part of the OO-Browser.
  18. ;;
  19. ;; DESCRIPTION:  
  20. ;; DESCRIP-END.
  21.  
  22. ;;; ************************************************************************
  23. ;;; Other required Elisp libraries
  24. ;;; ************************************************************************
  25.  
  26. (mapcar 'require '(br-c-ft br-java))
  27.  
  28. ;;; ************************************************************************
  29. ;;; Public variables
  30. ;;; ************************************************************************
  31.  
  32. (defvar java-cpp-include-dirs '("/usr/include/")
  33.   "*Ordered list of include directories by default searched by C preprocessor.
  34. Each directory must end with a directory separator.  See also
  35. 'java-include-dirs'.")
  36.  
  37. (defvar java-include-dirs nil
  38.   "*Ordered list of directories to search for java include files.
  39. Each directory must end with a directory separator.  Directories normally
  40. searched by the java pre-processor should be set instead in
  41. 'java-cpp-include-dirs'.")
  42.  
  43. (defconst java-return-type-identifier
  44.   (concat "\\([\[a-zA-Z]"
  45.       "\\|[\[a-zA-Z][]" java-return-type-chars "]*"
  46.       "[]" java-return-type-chars "]+\\)"
  47.       "[ \t\n\^M]*"))
  48.  
  49. (defconst java-type-identifier
  50.   (concat "\\([\[a-zA-Z][]" java-identifier-chars "]*[ \t\n\^M]+\\)"))
  51.  
  52. (defconst java-type-tag-separator "@"
  53.   "String that separates a tag's type from its normalized definition form.
  54. This should be a single character which is unchanged when quoted for use as a
  55. literal in a regular expression.")
  56.  
  57. (defconst java-tag-fields-regexp
  58.   ;; The \\\\? below is necessary because we sometimes use this expression to
  59.   ;; test against a string that has been regexp-quoted and some of the
  60.   ;; characters in br-feature-type-regexp will then be preceded by \\.
  61.   (format "\\`\\([^%s \n]+\\)%s\\\\?\\(%s \\)\\([^%s\n]+\\)%s"
  62.       java-type-tag-separator java-type-tag-separator
  63.       br-feature-type-regexp java-type-tag-separator
  64.       java-type-tag-separator)
  65.  "Regexp matching the fields of a java feature tag line.
  66. Group 1 is the class of the feature.  Group 2 is the prefix preceding the
  67. feature when displayed within a listing buffer.  Group 3 is the feature name.
  68. The feature definition signature begins at the end of the regexp match,
  69. i.e. (match-end 0), and goes to the end of the string or line.")
  70.  
  71. ;;; ************************************************************************
  72. ;;; Public functions
  73. ;;; ************************************************************************
  74.  
  75. (defun java-member-p ()
  76.   "Prints whether entity at point is a java member definition or declaration."
  77.   (interactive)
  78.   (let ((name))
  79.     (save-excursion
  80.       (message
  81.        (concat
  82.     "Is " (if (java-feature-def-p)
  83.           (progn (setq name
  84.                    (buffer-substring (match-beginning
  85.                           java-feature-name-grpn)
  86.                          (match-end
  87.                           java-feature-name-grpn)))
  88.              "")
  89.         "not ")
  90.     "a def.  "
  91.     "Is " (if (and (java-skip-to-statement) (java-feature-decl))
  92.           (progn (setq name
  93.                    (buffer-substring (match-beginning
  94.                           java-feature-name-grpn)
  95.                          (match-end
  96.                           java-feature-name-grpn)))
  97.              "")
  98.         "not ")
  99.     "a member decl.  "
  100.     (if name (concat "  Name = " name)))))))
  101.  
  102. (defun java-feature-implementors (name)
  103.   "Return unsorted list of java feature tags which implement feature NAME.
  104. This includes classes which declare abstract functions with NAME."
  105.   (java-feature-matches (concat "^" (regexp-quote name) "$")))
  106.  
  107. (defun java-feature-locate-p (feature-tag &optional regexp-flag)
  108.   "Leaves point at the start of FEATURE-TAG's definition in the current buffer.
  109. Assumes caller has moved point to the beginning of the buffer or to the point
  110. of desired search start.
  111. Optional REGEXP-FLAG means FEATURE-TAG is a regular expression."
  112.   ;; Match to function definitions, not declarations, except for abstract
  113.   ;; methods which are declared, not defined, and so end with a ';'.
  114.   ;;
  115.   ;; First move to the proper class implementation if feature-tag does not
  116.   ;; include a <class>:: part and this is not a [default-class], so that if
  117.   ;; two classes in the same file have the same feature signature, we still
  118.   ;; end up at the right one.
  119.   (if (string-match java-tag-fields-regexp feature-tag)
  120.       (let ((class (substring feature-tag (match-beginning 1) (match-end 1))))
  121.     (setq feature-tag (substring feature-tag (match-end 0)))
  122.     (if regexp-flag
  123.         (if (not (string-match "\\`\\\\\\[\\|::" feature-tag))
  124.         (re-search-forward (java-class-definition-regexp class t)
  125.                    nil t))
  126.       (if (not (string-match "\\`\\[\\|::" feature-tag))
  127.           (re-search-forward (java-class-definition-regexp class)
  128.                  nil t)))))
  129.   ;; Now look for feature expression.
  130.   (let ((found) (start))
  131.     (or regexp-flag (setq feature-tag
  132.               (java-feature-signature-to-regexp feature-tag)))
  133.     (while (and (re-search-forward feature-tag nil t)
  134.         (setq start (match-beginning 0))
  135.         (not (setq found (not 
  136.                   (if (c-within-comment-p)
  137.                       (progn (search-forward "*/" nil t)
  138.                          t)))))))
  139.     (if found
  140.     (progn (goto-char start)
  141.            (skip-chars-forward " \t\n")
  142.            (java-to-comments-begin)
  143.            (recenter 0)
  144.            (goto-char start)
  145.            t))))
  146.  
  147. (defun java-feature-name-to-regexp (name)
  148.   "Converts routine NAME into a regular expression matching the routine's name tag."
  149.   (setq name (java-feature-signature-to-regexp name))
  150.   (aset name (1- (length name)) ?\()  ;; Match only to functions
  151.   name)
  152.  
  153. (defun java-feature-signature-to-name (signature &optional with-class for-display)
  154.   "Extracts the feature name from SIGNATURE.
  155. The feature's class name is dropped from signature unless optional WITH-CLASS
  156. is non-nil.  If optional FOR-DISPLAY is non-nil, a feature type character is
  157. prepended to the name for display in a browser listing."
  158.   (let ((name))
  159.     (cond
  160.      ;; member
  161.      ((string-match java-tag-fields-regexp signature)
  162.       (setq name (substring signature (match-beginning (if for-display 2 3))
  163.                 (match-end 3)))
  164.       (if with-class
  165.       (setq name (concat
  166.               (substring signature (match-beginning 1) (match-end 1))
  167.               "." name)))
  168.       ;; Remove any trailing whitespace.
  169.       (br-delete-space name))
  170.      ;;
  171.      ;; unknown
  172.      (t ;; Remove any trailing whitespace and add display prefix.
  173.       (setq name (br-delete-space signature))
  174.       (if for-display (java-feature-add-prefix name "" signature) name)))))
  175.  
  176. (defun java-feature-signature-to-regexp (signature)
  177.   "Given a java SIGNATURE, return regexp used to match to its definition."
  178.   (setq signature (regexp-quote signature))
  179.   (let ((prefix-info
  180.      (if (string-match java-tag-fields-regexp signature)
  181.          (prog1 (substring signature (match-beginning 0) (match-end 0))
  182.            (setq signature (substring signature (match-end 0)))))))
  183.     (let ((pat) (i 0) (c) (len (length signature)))
  184.       (while (< i len)
  185.     (setq c (aref signature i)
  186.           pat (cond ((= c ? )
  187.              ;; Allow for possible single line comment
  188.              ;; following any whitespace, e.g. following
  189.              ;; each routine argument.
  190.              (concat pat "[ \t\n\^M]*\\(//.*\\)?"))
  191.             (t
  192.              (concat pat (char-to-string c))))
  193.           i (1+ i)))
  194.       (setq pat (concat prefix-info pat)))))
  195.  
  196. (defun java-feature-tree-command-p (class-or-signature)
  197.   "Display definition of CLASS-OR-SIGNATURE if a signature and return t, else return nil."
  198.   (if (java-routine-p class-or-signature)
  199.       (progn
  200.     (if (br-in-browser) (br-to-view-window))
  201.     (br-feature-found-p (br-feature-file class-or-signature)
  202.                 class-or-signature))))
  203.  
  204. (defun java-list-features (class &optional indent)
  205.   "Return sorted list of java feature tags lexically defined in CLASS."
  206.   (let ((obuf (current-buffer))
  207.     (features)
  208.     (class-tag (concat "\n" class java-type-tag-separator))
  209.     feature)
  210.     (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
  211.     (goto-char 1)
  212.     (if (or (null indent) (<= indent 2))
  213.     ;; Include all features.
  214.     (while (search-forward class-tag nil t)
  215.       (setq features (cons (br-feature-current) features)))
  216.       (while (search-forward class-tag nil t)
  217.     (setq feature (br-feature-current))
  218.     (setq features (cons feature features))))
  219.     (set-buffer obuf)
  220.     (java-sort-features (nreverse features))))
  221.  
  222. (defun java-routine-p (str)
  223.   (string-match "([^\)]*)" str))
  224.  
  225. (defun java-sort-features (routine-list)
  226.   (sort routine-list 'java-feature-lessp))
  227.  
  228. (defun java-to-definition (&optional other-win)
  229.   "If point is within a declaration, try to move to its definition.
  230. With OTHER-WIN non-nil, show it in another window."
  231.   (interactive)
  232.   (let ((opoint (point)))
  233.     (cond
  234.      ((java-include-file other-win))
  235.      ((br-check-for-class (java-class-decl-p) other-win))
  236.      ((java-feature other-win))
  237.      ((and (goto-char opoint)
  238.         (br-check-for-class (java-find-class-name) other-win)))
  239.      (t    (beep)
  240.     (message
  241.      "(OO-Browser):  Select a java declaration to move to its definition.")
  242.     nil))))
  243.  
  244. ;;; ************************************************************************
  245. ;;; Private functions
  246. ;;; ************************************************************************
  247.  
  248. (defun java-class-decl-p ()
  249.   "Return nil unless point is within a class declaration, referenced by another
  250. class.  Commented declarations also return nil.  When value is non-nil, it is
  251. the class name from the declaration.  Leave point at start of statement for
  252. visual clarity."
  253.   (java-skip-to-statement)
  254.   (save-excursion
  255.     (let ((class))
  256.       (and (looking-at java-class-decl)
  257.        (setq class (buffer-substring
  258.             (match-beginning java-class-name-grpn)
  259.             (match-end java-class-name-grpn)))
  260.        (not (c-within-comment-p))
  261.        (progn (beginning-of-line)
  262.           (not (looking-at "[ \t]*//")))
  263.        class))))
  264.  
  265. (defun java-feature (&optional other-win)
  266.   "Move point to definition of member given by declaration at point.
  267. Return nil if point is not within a member declaration."
  268.   ;; If '{' follows the feature declaration, then feature is defined right
  269.   ;; here, within the class definition.
  270.   (interactive)
  271.   (let ((feature-def) (ftr) (class) (ftr-pat))
  272.     (cond ((java-feature-def-p)
  273.        (recenter 0)
  274.        t)
  275.       ;; Now look for feature definition in code (non-header) files.
  276.       ((progn (setq feature-def (java-feature-def-pat)
  277.             ftr (car (cdr (cdr feature-def)))
  278.             class (car (cdr feature-def))
  279.             ftr-pat (car feature-def))
  280.           (java-locate-feature ftr class ftr-pat other-win)))
  281.       ((java-feature-decl)
  282.        (beep)
  283.        (message "(OO-Browser):  '%s' feature definition not found." ftr)
  284.        t))))
  285.  
  286. (defun java-feature-add-prefix (feature-name class signature)
  287.   "Add a browser listing display prefix to FEATURE-NAME from CLASS based on feature's SIGNATURE."
  288.   (concat (cond ((string-match java-native-method-regexp signature)
  289.          "/ ")
  290.         ((string-match java-abstract-method-regexp signature)
  291.          "> ")
  292.         ((or (string-equal feature-name class)
  293.              (string-equal feature-name "finalize"))
  294.          "+ ")
  295.         (t "- "))
  296.       feature-name))
  297.  
  298. (defun java-feature-decl ()
  299.   (if (looking-at java-class-decl)
  300.       nil
  301.     (looking-at java-feature-decl)))
  302.  
  303. (defun java-feature-def-p ()
  304.   "Return nil unless point is within a member definition.
  305. Commented member definitions also return nil.
  306. Leaves point at start of statement for visual clarity."
  307.   (java-skip-to-statement)
  308.   (save-excursion
  309.     (and (not (c-within-comment-p))
  310.      (save-excursion (beginning-of-line)
  311.              (not (looking-at "[ \t]*//")))
  312.      (not (looking-at java-class-decl))
  313.      (looking-at (concat java-at-feature-regexp "[{;,]"))
  314.      (or (= ?\{
  315.         (save-excursion (goto-char (match-end 0))
  316.                 (preceding-char)))
  317.          ;; If ends with a '[;,]' then it must be a native or abstract
  318.          ;; method to be a definition.
  319.          (if (match-end java-feature-parens-grpn)
  320.          (save-restriction
  321.            (narrow-to-region (match-beginning 0) (match-end 0))
  322.            (if (looking-at
  323.             "\\(^\\|[ \t\n\^M]\\)\\(abstract\\|native\\)[ \t\n\^M][^;{}]+;")
  324.                (progn (message "(OO-Browser):  Abstract function, definition deferred to descendants.")
  325.                   t))))))))
  326.  
  327. (defun java-feature-def-pat ()
  328.   "Return (list <feature-def-pat> <feature-class> <feature-name>) associated with declaration at point."
  329.   (and (java-skip-to-statement)
  330.        (java-feature-decl)
  331.        ;; Don't regexp-quote member-name yet
  332.        (let* ((member-name (buffer-substring
  333.                 (match-beginning java-feature-name-grpn)
  334.                 (match-end java-feature-name-grpn)))
  335.           (member-modifiers (if (match-end java-feature-mod-grpn)
  336.                     (br-quote-match java-feature-mod-grpn)))
  337.           (class)
  338.           (member-type
  339.            (concat (and (match-end java-feature-type-grpn)
  340.                 ;; Handle possible regexp bug
  341.                 (not
  342.                  (equal 
  343.                   (match-beginning java-feature-type-grpn)
  344.                   (match-beginning java-feature-name-grpn)))
  345.                 (concat (br-quote-match
  346.                      java-feature-type-grpn)))))
  347.           (func-args (if (match-end java-feature-parens-grpn)
  348.                  (cons (match-beginning java-feature-parens-grpn)
  349.                    (match-end java-feature-parens-grpn))))
  350.           (base-cl-args (match-end java-feature-parens-grpn)))
  351.  
  352.      (and member-type (string-match "[ \t]+$" member-type)
  353.           (setq member-type (substring member-type 0
  354.                        (match-beginning 0))))
  355.      (and (stringp member-type)
  356.           (not (equal member-type ""))
  357.           (setq member-type (concat member-type "[ \t\n]*")))
  358.  
  359.      (let ((pre-member-regexp
  360.         (concat
  361.          java-type-modifier-keyword
  362.          (if member-modifiers
  363.              (let ((def-mods "") (mod))
  364.                (while (string-match "\\([a-z]+\\)[ \t\n]+"
  365.                         member-modifiers)
  366.              (setq mod (substring member-modifiers
  367.                           (match-beginning 1)
  368.                           (match-end 1))
  369.                    member-modifiers (substring member-modifiers
  370.                                (match-end 0)))
  371.              (if (equal (string-match
  372.                      java-type-def-modifier mod) 0)
  373.                  (setq def-mods (concat def-mods "\\(" mod
  374.                             "[ \t\n]+\\)?"))))
  375.                def-mods))
  376.          ))
  377.            (post-member-regexp
  378.         (concat
  379.          ;; Point at beginning of line may imply a non-member func.
  380.          (progn
  381.            ;; Class name is not part of declaration
  382.            ;; so look for declaration within a
  383.            ;; class definition and locate the class
  384.            ;; name.  If not within a class, assume
  385.            ;; declaration is global.
  386.            (setq class (java-get-class-name-from-source))
  387.            (br-regexp-quote member-name))
  388.          "[ \t\n]*"
  389.          (if func-args
  390.              (concat "\\(" (java-func-args-regexp func-args)
  391.                  "\\|" (java-func-args-string func-args)
  392.                  "\\)"))
  393.          ;; If is a constructor member function, then can have some
  394.          ;; arguments for base class constructors after a ':'
  395.          ;; but preceding the '{'.
  396.          "[ \t\n]*"
  397.          (and base-cl-args
  398.               (equal member-name class)
  399.               "\\(:[^;{}]*\\)?"))))
  400.        (list
  401.         (` (lambda (class)
  402.          (concat "^" (br-regexp-quote class)
  403.              (, (concat
  404.                  java-type-tag-separator
  405.                  br-feature-type-regexp " "
  406.                  (br-regexp-quote member-name)
  407.                  java-type-tag-separator
  408.                  pre-member-regexp))
  409.              (br-regexp-quote class)
  410.              (, post-member-regexp))))
  411.         class member-name)))))
  412.  
  413. (defun java-feature-lessp (routine1 routine2)
  414.   (string-lessp (java-feature-signature-to-name routine1)
  415.         (java-feature-signature-to-name routine2)))
  416.  
  417. (defun java-feature-matches (regexp)
  418.   "Return an unsorted list of feature tags whose names match in part or whole to REGEXP."
  419.   ;; Ensure match to feature names only; also handle "^" and "$" meta-chars
  420.   (setq regexp
  421.     (concat (format "^[^%s \n]+%s%s "
  422.             java-type-tag-separator java-type-tag-separator
  423.             br-feature-type-regexp)
  424.         (if (equal (substring regexp 0 1) "^")
  425.             (progn (setq regexp (substring regexp 1)) nil)
  426.           java-identifier-chars)
  427.         (if (equal (substring regexp -1) "$")
  428.             (substring regexp 0 -1)
  429.           (concat regexp java-identifier-chars))
  430.         java-type-tag-separator))
  431.   (save-excursion
  432.     (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
  433.     (goto-char 1)
  434.     (let ((features))
  435.       (while (re-search-forward regexp nil t)
  436.     (setq features (cons (br-feature-current) features)))
  437.       features)))
  438.  
  439. (defun java-feature-normalize (routine class name)
  440.   (setq class (br-delete-space class)
  441.     name (java-feature-add-prefix name class routine)
  442.     routine (concat class java-type-tag-separator 
  443.             name java-type-tag-separator 
  444.             (br-delete-space routine)))
  445.   (let* ((len (length routine))
  446.      (normal-feature (make-string len ?\ ))
  447.      (n 0) (i 0)
  448.      (space-list '(?\  ?\t ?\n ?\^M))
  449.      (space-regexp "[ \t\n\^M]+")
  450.      chr)
  451.     (while (< i len)
  452.       (setq chr (aref routine i)) 
  453.       (cond
  454.        ;; Convert sequences of space characters to a single space.
  455.        ((memq chr space-list)
  456.     (aset normal-feature n ?\ )
  457.     (if (string-match space-regexp routine i)
  458.         (setq i (match-end 0)
  459.           n (1+ n))
  460.       (setq i (1+ i)
  461.         n (1+ n))))
  462.        ;;
  463.        ;; Remove // style comments
  464.        ((and (= chr ?/)
  465.          (< (1+ i) len)
  466.          (= (aref routine (1+ i)) ?/))
  467.     (setq i (+ i 2))
  468.     (while (and (< i len) (/= (aref routine i) ?\n))
  469.       (setq i (1+ i))))
  470.        (t ;; Normal character
  471.     (aset normal-feature n chr)
  472.     (setq i (1+ i)
  473.           n (1+ n)))))
  474.     (substring normal-feature 0 n)))
  475.  
  476. (defun java-feature-tag-class (signature)
  477.   "Extract the class name from SIGNATURE."
  478.   (cond ((string-match java-type-tag-separator signature)
  479.      (substring signature 0 (match-beginning 0)))
  480.     ((string-match "\\([^ \t]+\\)\." signature)
  481.      (substring signature (match-beginning 1) (match-end 1)))
  482.     (t "")))
  483.  
  484. (defun java-feature-tags-lookup (class-list ftr-pat &optional other-win)
  485.   "Display routine definition derived from CLASS-LIST, matching FTR-PAT.
  486. Use routine tags table to locate a match.  Caller must use 'set-buffer'
  487. to restore prior buffer when a match is not found."
  488.   (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
  489.   (let  ((classes class-list)
  490.      (found-ftr)
  491.      (ftr-regexp)
  492.      (class)
  493.      (ftr-path))
  494.     (if (or (null class-list) (equal class-list '(nil)))
  495.     nil
  496.       (while (and (not found-ftr) classes)
  497.     (setq class (car classes)
  498.           ftr-regexp (funcall ftr-pat class)
  499.           ftr-path (br-feature-def-file ftr-regexp)
  500.           found-ftr (if ftr-path
  501.                 (br-edit-feature (br-feature-current)
  502.                          ftr-path other-win))
  503.           classes (if found-ftr nil (cdr classes))))
  504.       (if found-ftr
  505.       (or class t)
  506.     (java-feature-tags-lookup
  507.      (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
  508.                 class-list))
  509.      ftr-pat)))))
  510.  
  511. (defun java-files-with-source (class)
  512.   "Use CLASS to compute set of files that match to a java source file regexp.
  513. Return as a list."
  514.   (let ((file (if class (br-class-path class) buffer-file-name)))
  515.     (and file
  516.      (let* ((src-file-regexp (concat "^" (br-filename-head file)
  517.                      java-code-file-regexp))
  518.         (dir (file-name-directory file))
  519.         (files (directory-files dir nil src-file-regexp)))
  520.        (mapcar (function (lambda (f) (concat dir f)))
  521.            files)))))
  522.  
  523. (defun java-find-ancestors-feature (class-list ftr-pat &optional other-win)
  524.   "Scan ancestors of CLASS-LIST and show routine definition matching FTR-PAT."
  525.   ;; If no class, search for non-member function.
  526.   (or class-list (setq class-list '(nil)))
  527.   (let ((obuf (current-buffer)))
  528.     (prog1
  529.     (if (and br-feature-tags-file
  530.          (file-exists-p br-feature-tags-file)
  531.          (file-readable-p br-feature-tags-file))
  532.         (java-feature-tags-lookup class-list ftr-pat other-win)
  533.       ;; Only works if features are in same directory as class def.
  534.       (java-scan-ancestors-feature class-list ftr-pat other-win))
  535.       (set-buffer obuf))))
  536.  
  537. (defun java-find-class-name ()
  538.   "Return current word as a potential class name."
  539.   (save-excursion
  540.     (let* ((start)
  541.        (ignore "\]\[ \t\n;,.\(\){}-")
  542.        (pat (concat "^" ignore)))
  543.       (forward-char 1)
  544.       (skip-chars-backward ignore)
  545.       (skip-chars-backward pat)
  546.       (setq start (point))
  547.       (skip-chars-forward (concat pat ":"))
  548.       (buffer-substring start (point)))))
  549.  
  550. (defun java-func-args-regexp (func-args)
  551.   (let* ((space "\\\\\\s-*")
  552.      (obuf (current-buffer))
  553.      (tmp-buf-nm "*br-java-tmp*")
  554.      (tmp-buf (progn (if (get-buffer tmp-buf-nm)
  555.                  (kill-buffer tmp-buf-nm))
  556.              (get-buffer-create tmp-buf-nm))))
  557.     (or tmp-buf (error "OO-Browser: (java-func-args-regexp) - Can't create tmp-buf."))
  558.     ;; Fill tmp-buffer with all func-args, including parens.
  559.     (copy-to-buffer tmp-buf (car func-args) (cdr func-args))
  560.     
  561.     (set-buffer tmp-buf)
  562.     (let ((quoted-args (br-regexp-quote (buffer-substring
  563.                      (point-min) (point-max)))))
  564.       (erase-buffer)
  565.       (insert quoted-args))
  566.  
  567.     (goto-char (point-min))
  568.     (if (looking-at "(\\s-*)")
  569.     (replace-match "(\\\\s-*)" t)
  570.  
  571.       ;; Replace all "\( +" with "\(" temporarily
  572.       (br-buffer-replace "\\(^\\|[^\\]\\)\([ \t\n]+" "\\1\(")
  573.     
  574.       ;; Replace all "+ \)" with "\)" temporarily
  575.       (br-buffer-replace "[ \t\n]+\)" "\)")
  576.     
  577.       ;; Replace all "...\)" with "...@" temporarily
  578.       (br-buffer-replace "\\\\\\.\\\\\\.\\\\\\.\)" "@@@")
  579.     
  580.       ;; Optionalize right hand side of argument assignments.
  581.       (br-buffer-replace "\\([^=,\( \t\n]+\\)\\([ \t\n]*=[^,\)]+\\)"
  582.              (concat "\\1\\\\( "
  583.                  (br-regexp-quote java-arg-identifier)
  584.                  "\\\\)? \\\\(\\2\\\\)?"))
  585.  
  586.       ;; Replace all "\)" with "optional <java-identifier> \)"
  587.       (br-buffer-replace
  588.        "\\([\(,][^=\)]+\\)\)"
  589.        (concat "\\1\\\\( " (br-regexp-quote java-arg-identifier)
  590.            "\\\\)?\)"))
  591.  
  592.       ;; Replace all  "," with "optional <java-identifier>,"
  593.       (br-buffer-replace
  594.        "\\([\(,][^=,]+\\),"
  595.        (concat "\\1\\\\( " (br-regexp-quote java-arg-identifier) "\\\\)?,"))
  596.  
  597.       ;; Replace all  " *, *" with "<spc>,<spc>"
  598.       (br-buffer-replace "[ \t\n]*,[ \t\n]*" (concat space "," space))
  599.     
  600.       ;; Replace all " +" with "<spc>"
  601.       (br-buffer-replace "[ \t\n]+" space)
  602.  
  603.       ;; Replace all "\(" with "\(<spc>"
  604.       (br-buffer-replace "\\(^\\|[^\\]\\)\(" (concat "\\1\(" space))
  605.     
  606.       ;; Replace all "\)" with "<spc>\)"
  607.       (br-buffer-replace "\\([^\\]\\)\)" (concat "\\1" space "\)"))
  608.  
  609.       ;; Replace all & and quoted \\* with "<spc>[*&]+<spc>"
  610.       (br-buffer-replace "\\(&\\|\\\\\\*\\)+" (concat space "\\1" space))
  611.  
  612.       ;; Replace all "<spc>" with "[ \t\n]*"
  613.       (br-buffer-replace "\\\\s-\\*" "[ \t\n]*")
  614.  
  615.       ;; Replace all "@@@" with any # of args
  616.       (br-buffer-replace "@@@" "[^\)]*\)")
  617.       )
  618.  
  619.     ;; Return final buffer as a string.
  620.     (prog1 (buffer-substring (point-min) (point-max))
  621.       (kill-buffer tmp-buf-nm)
  622.       (set-buffer obuf))))
  623.  
  624. (defun java-func-args-string (func-args)
  625.   (let* ((space "\\\\\\s-*")
  626.      (obuf (current-buffer))
  627.      (tmp-buf-nm "*br-java-tmp*")
  628.      (tmp-buf (progn (if (get-buffer tmp-buf-nm)
  629.                  (kill-buffer tmp-buf-nm))
  630.              (get-buffer-create tmp-buf-nm))))
  631.     (or tmp-buf (error "OO-Browser: (java-func-args-string) - Can't create tmp-buf."))
  632.     ;; Fill tmp-buffer with all func-args, including parens.
  633.     (copy-to-buffer tmp-buf (car func-args) (cdr func-args))
  634.     
  635.     (set-buffer tmp-buf)
  636.     (let ((quoted-args (br-regexp-quote (buffer-substring
  637.                      (point-min) (point-max)))))
  638.       (erase-buffer)
  639.       (insert quoted-args))
  640.  
  641.     (goto-char (point-min))
  642.     (if (looking-at "(\\s-*)")
  643.     (replace-match "(\\\\s-*)" t)
  644.  
  645.       ;; Replace all "\( +" with "\(" temporarily
  646.       (br-buffer-replace "\\(^\\|[^\\]\\)\([ \t\n]+" "\\1\(")
  647.     
  648.       ;; Replace all "+ \)" with "\)" temporarily
  649.       (br-buffer-replace "[ \t\n]+\)" "\)")
  650.     
  651.       ;; Replace all "...\)" with "@@@" temporarily
  652.       (br-buffer-replace "\\\\\\.\\\\\\.\\\\\\.\)" "@@@")
  653.  
  654.       ;; Optionalize right hand side of argument assignments.
  655.       (br-buffer-replace "\\([^=,\( \t\n]+\\)\\([ \t\n]+=[^,\)]+\\)"
  656.              (concat "\\1\\\\(\\2\\\\)?"))
  657.  
  658.       ;; If an arg consists of 2 or more words, replace last with <identifier>
  659.       (br-buffer-replace
  660.        "\\([\(,][^=,\)]*[^ \t\n=,\)]+[ \t\n]+\\)[^ \t\n=,\)]+\\([ \t\n]*[,\)]\\)"
  661.        (concat "\\1" (br-regexp-quote java-arg-identifier) "\\2"))
  662.  
  663.       ;; If an arg consists of only 1 word, add a second
  664.       (br-buffer-replace
  665.        "\\([\(,][ \t\n]*\\)\\([^ \t\n=,\)]+\\)\\([ \t\n]*[,\)]\\)"
  666.        (concat "\\1\\2 " (br-regexp-quote java-arg-identifier) "\\3"))
  667.  
  668.       ;; Replace all  " *, *" with "<spc>,<spc>"
  669.       (br-buffer-replace "[ \t\n]*,[ \t\n]*" (concat space "," space))
  670.     
  671.       ;; Replace all " +" with "<spc>"
  672.       (br-buffer-replace "[ \t\n]+" space)
  673.  
  674.       ;; Replace all "\(" with "\(<spc>"
  675.       (br-buffer-replace "\\(^\\|[^\\]\\)\(" (concat "\\1\(" space))
  676.     
  677.       ;; Replace all "\)" with "<spc>\)"
  678.       (br-buffer-replace "\\([^\\]\\)\)" (concat "\\1" space "\)"))
  679.  
  680.       ;; Replace all "<spc>" with "[ \t\n]*"
  681.       (br-buffer-replace "\\\\s-\\*" "[ \t\n]*")
  682.  
  683.       ;; Replace all "@@@" with any # of args
  684.       (br-buffer-replace "@@@" "[^\)]*\)")
  685.       )
  686.  
  687.     ;; Return final buffer as a string.
  688.     (prog1 (buffer-substring (point-min) (point-max))
  689.       (kill-buffer tmp-buf-nm)
  690.       (set-buffer obuf))))
  691.  
  692. (defun java-get-class-name-from-source ()
  693.   "Return class name from closest class definition preceding point or nil."
  694.   (let ((opoint (point))
  695.     (class))
  696.     (save-excursion
  697.       (if (re-search-backward java-class-def-regexp nil t)
  698.       (progn (goto-char (match-beginning java-class-def-derived-grpn))
  699.          (setq class (java-normalize-class-match))
  700.          ;; Ensure that declaration occurs within class definition.
  701.          (forward-list)
  702.          (and (> (point) opoint)
  703.               class))))))
  704.  
  705. (defun java-get-feature-tags (routine-file &optional routine-list)
  706.   "Scan java ROUTINE-FILE and hold routine tags in 'br-feature-tags-file'.
  707. Assume ROUTINE-FILE has already been read into a buffer and that
  708. 'br-feature-tags-init' has been called.  Optional ROUTINE-LIST can be
  709. provided so that a non-standard scan function can be used before calling
  710. this function."
  711.   (interactive)
  712.   (let ((obuf (current-buffer)))
  713.     (set-buffer (funcall br-find-file-noselect-function br-feature-tags-file))
  714.     (goto-char 1)
  715.     ;; Delete any prior routine tags associated with routine-file
  716.     (if (search-forward routine-file nil 'end)
  717.     (progn (forward-line -1)
  718.            (let ((start (point)))
  719.          (search-forward "\^L" nil 'end 2)
  720.          (backward-char 1)
  721.          (delete-region start (point))
  722.          )))
  723.     (if routine-list
  724.     (progn (insert "\^L\n" routine-file "\n")
  725.            (mapcar (function (lambda (tag) (insert tag "\n")))
  726.                routine-list)
  727.            ))
  728.     (set-buffer obuf)))
  729.  
  730. (defun java-include-file (&optional other-win)
  731.   "If point is on an include file line, try to display file.
  732. Return non-nil iff an include file line, even if file is not found.
  733. Look for include file in 'java-cpp-include-dirs' and in directory list
  734. 'java-include-dirs'."
  735.   (let ((opoint (point)))
  736.     (beginning-of-line)
  737.     (if (looking-at java-include-regexp)
  738.     (let ((incl-type (string-to-char
  739.               (buffer-substring (match-beginning 1)
  740.                         (1+ (match-beginning 1)))))
  741.           (file (buffer-substring (match-beginning 2) (match-end 2)))
  742.           (path)
  743.           (dir-list java-include-dirs)
  744.           (found))
  745.       (goto-char opoint)
  746.       (setq dir-list (if (= incl-type ?<)
  747.                  (append dir-list java-cpp-include-dirs)
  748.                (cons (file-name-directory buffer-file-name)
  749.                  dir-list)))
  750.       (while dir-list
  751.         (setq path (concat (car dir-list) file)
  752.           dir-list (if (setq found (file-exists-p path))
  753.                    nil
  754.                  (cdr dir-list))))
  755.       ;;
  756.       ;; If not found in normal include dirs, check all Env paths also.
  757.       ;;
  758.       (if (not found)
  759.           (let ((paths (delq nil (hash-map 'cdr br-paths-htable))))
  760.         (while paths
  761.           (setq path (car paths))
  762.           (if (string-equal (file-name-nondirectory path) file)
  763.               (setq found t paths nil)
  764.             (setq paths (cdr paths))))))
  765.       ;;
  766.       ;; If found, display file
  767.       ;;
  768.       (if found
  769.           (if (file-readable-p path)
  770.           (progn
  771.             (funcall br-edit-file-function path other-win)
  772.             (if (not (fboundp 'br-lang-mode))
  773.             (java-mode-setup))
  774.             (br-major-mode))
  775.         (beep)
  776.         (message "(OO-Browser):  Include file '%s' unreadable." path))
  777.         (beep)
  778.         (message "(OO-Browser):  Include file '%s' not found." file))
  779.       path)
  780.       (goto-char opoint)
  781.       nil)))
  782.  
  783. (defun java-locate-feature (ftr class ftr-pat &optional other-win)
  784.   ;; 'class' may = nil, implying non-member function
  785.   (or class (setq class "[function]"))
  786.   (let ((def-class))
  787.     (if (and ftr-pat
  788.          (setq def-class
  789.            (java-find-ancestors-feature (list class)
  790.                            ftr-pat other-win)))
  791.     (progn (if (and class (not (equal class def-class)))
  792.            (message
  793.              "Member `%s` of class '%s' inherited from class '%s'."
  794.              ftr class def-class))
  795.            t))))
  796.  
  797. (defun java-scan-ancestors-feature (class-list ftr-pat &optional other-win)
  798.   "Display routine definition derived from CLASS-LIST, matching FTR-PAT.
  799. Scan files with same base name as class file."
  800.   (let  ((classes class-list)
  801.      (found-ftr)
  802.      (code-def-files)
  803.      (file)
  804.      (ftr-regexp)
  805.      (class))
  806.     (if (null class-list)
  807.     nil
  808.       (while (and (not found-ftr) classes)
  809.     (setq class (car classes)
  810.           code-def-files (java-files-with-source class)
  811.           ftr-regexp (funcall ftr-pat class))
  812.     (while (and (setq file (car code-def-files))
  813.             (not (setq found-ftr
  814.                    (br-feature-found-p file ftr-regexp
  815.                            nil other-win t))))
  816.       (setq code-def-files (cdr code-def-files)))
  817.     (setq classes (if found-ftr nil (cdr classes))))
  818.       (if found-ftr
  819.       (or class t)
  820.     (java-scan-ancestors-feature
  821.      (apply 'append (mapcar (function (lambda (cl) (br-get-parents cl)))
  822.                 class-list))
  823.      ftr-pat)))))
  824.  
  825. (defun java-scan-features (class start end)
  826.   "Return reverse ordered list of java routine definitions within CLASS def.
  827. START and END give buffer region to search."
  828.   (setq class (br-delete-space class))
  829.   (save-excursion
  830.     (save-restriction
  831.       (narrow-to-region start end)
  832.       (goto-char start)
  833.       (let ((routines) rout name)
  834.     ;;
  835.     ;; Get member definitions and abstract method declarations.
  836.     ;;
  837.     (while (re-search-forward java-routine-def nil t)
  838.       (setq start (match-beginning 0)
  839.         name  (buffer-substring
  840.                (match-beginning java-feature-name-grpn)
  841.                (match-end java-feature-name-grpn))
  842.         rout  (buffer-substring (match-beginning 0) (match-end 0)))
  843.       ;; This is necessary to remove a possible double expression match
  844.       ;; where there is a blank line within the match.
  845.       (if (string-match "[\n\^M]\\([ \t]*[\n\^M]\\)+" rout)
  846.           (progn (setq rout (substring rout (match-end 0)))
  847.              (goto-char (+ start (match-end 0))))
  848.         (if (c-within-comment-p)
  849.         (search-forward "*/" nil t)
  850.           ;; Move point to precede feature opening brace or abstract
  851.           ;; method declaration semicolon.
  852.           (backward-char)
  853.           (if (= (following-char) ?\{)
  854.           (condition-case ()
  855.               ;; Move to end of feature but ignore any error if braces
  856.               ;; are unbalanced.  Let the compiler tell the user about
  857.               ;; this.
  858.               (forward-sexp)
  859.             (error nil)))
  860.           (setq rout (java-feature-normalize rout class name)
  861.             routines (cons rout routines)))))
  862.     routines))))
  863.  
  864. (defun java-skip-to-statement ()
  865.   (if (re-search-backward "\\(^\\|[;{}]\\)[ \t]*" nil t)
  866.       (progn (goto-char (match-end 0))
  867.          (skip-chars-forward " \t")
  868.          t)))
  869.  
  870. ;;; ************************************************************************
  871. ;;; Private variables
  872. ;;; ************************************************************************
  873.  
  874. (defconst java-code-file-regexp "\\.java$"
  875.   "Regular expression matching a unique part of java source (non-header) file name and no others.")
  876.  
  877. (defconst java-include-regexp
  878.   "[ \t/*]*import[ \t]+\\([^;]+\\)"
  879.   "Regexp to match to java include file lines.  File name is grouping 2.  Type
  880. of include, user-specified via double quote, or system-related starting with
  881. '<' is given by grouping 1.")
  882.  
  883. (defconst java-type-def-modifier
  884.   "\\(const\\|final\\|static\\|abstract\\|public\\|protected\\|private\\)")
  885.  
  886. (defconst java-type-modifier-keyword
  887.   (concat "\\(\\(public\\|protected\\|private\\|const\\|abstract\\|"
  888.       "synchronized\\|final\\|static\\|threadsafe\\|transient\\|"
  889.       "native\\)[ \t\n\^M]+\\)"))
  890.  
  891. (defconst java-type-identifier-group
  892.   (concat "\\(\\(" java-return-type-identifier "\\)[ \t\n\^M]+\\)"))
  893.  
  894. (defconst java-function-identifier (concat
  895.                    "[_a-zA-Z][^][ \t:;.,{}()=]*")
  896.   "Regular expression matching a java or G++ function name.")
  897.  
  898. (defconst java-arg-identifier
  899.   (concat "[_a-zA-Z][" java-identifier-chars "]*")
  900.   "Regular expression matching a java or G++ function argument identifier.")
  901.  
  902. (defconst java-feature-decl-or-def
  903.   (concat "^[ \t]*\\(" java-type-modifier-keyword "*"
  904.       java-type-identifier-group "\\)?"
  905.       "\\(" java-type-identifier "[ \t\n\^M]*\\)?"
  906.       "\\(" java-function-identifier "\\|" java-identifier "\\)"
  907.       ;; It's hard to tell arguments from parenthesized initializing
  908.       ;; expressions.
  909.       "[ \t\n\^M]*(\\([^);{}]*\\))\\([][]*\\)"
  910.       )
  911.   "Regexp matching a java member declaration or definition.
  912. Member modifier keywords are grouped expression 'java-feature-mode-grpn'.
  913. Member type is grouped expression 'java-feature-type-grpn'.  Member name is
  914. group 'java-feature-name-grpn'.  Function parentheses, if any, are group
  915. 'java-feature-parens-grpn'.")
  916.  
  917. (defconst java-feature-mod-grpn 2)
  918. (defconst java-feature-type-grpn 5)
  919. (defconst java-feature-name-grpn 9)
  920. (defconst java-feature-parens-grpn 11)
  921.  
  922. (defconst java-at-feature-regexp
  923.   (concat java-feature-decl-or-def "[ \t\n]*")
  924.   "See documentation of 'java-feature-decl-or-def' for grouping expressions.")
  925.  
  926. (defconst java-feature-decl
  927.   (concat java-at-feature-regexp "[;{]")
  928.   "See documentation of 'java-feature-decl-or-def' for grouping expressions.")
  929.  
  930. (defconst java-routine-def-terminator-regexp
  931.   ;; Also matches to abstract method declarations.
  932.   ;; Only native and abstract functions end with a semi-colon.
  933.   "\\({\\|;\\)")
  934.  
  935. (defconst java-routine-def
  936.   (concat java-at-feature-regexp java-routine-def-terminator-regexp)
  937.   "See documentation of 'java-feature-decl-or-def' for grouping expressions.")
  938.  
  939. (defconst java-class-decl
  940.   (concat java-class-modifier-keyword 
  941.       java-class-keyword java-identifier "[ \t]*[;,]")
  942.   "Regexp matching a java class declaration.
  943. Class name is grouping 'java-class-name-grpn'.")
  944.  
  945. (defconst java-class-name-grpn 4)
  946.  
  947. ;; Old def was: "\\(^\\|[ \t\n\^M]\\)abstract[ \t\n\^M][^;{}]+;"
  948. (defconst java-abstract-method-regexp ";\\'"
  949.   "Regexp matching a Java abstract method signature.")
  950.  
  951. (defconst java-native-method-regexp "\\(^\\|[ \t\n\^M]\\)native[ \t\n\^M][^;{}]+;"
  952.   "Regexp matching a Java native method signature, one implemented in another language.")
  953.  
  954. (provide 'br-java-ft)
  955.