Initial Commit
[packages] / xemacs-packages / jde / lisp / jde-annotations.el
1 ;;; jde-annotations.el --- Indentation for Java 5 annotations.
2
3 ;; Copyright (C) 2006 by Suraj Acharya
4
5 ;; Author: Suraj Acharya <sacharya@cs.indiana.edu>
6 ;; Maintainer: Suraj Acharya <sacharya@cs.indiana.edu>
7 ;; Created: 22 Feb 2006
8 ;; Keywords: cc-mode java annotations indentation
9
10 ;; This file is not part of Emacs
11
12 ;; This program is free software; you can redistribute it and/or
13 ;; modify it under the terms of the GNU General Public License as
14 ;; published by the Free Software Foundation; either version 2, or (at
15 ;; your option) any later version.
16
17 ;; This program is distributed in the hope that it will be useful, but
18 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 ;; General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with this program; see the file COPYING.  If not, write to
24 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27
28 ;;; Commentary:
29 ;; 
30 ;; This library tries to indent java annotations
31 ;; (http://java.sun.com/j2se/1.5.0/docs/guide/language/annotations.html)
32 ;; like the code examples listed in the webpage.
33
34 ;;; Code:
35
36 (defun c-preprend-offset (symbol offset)
37   "Find the offset entry for SYMBOL and add OFFSET at the front of the list.
38 See `c-set-offset' for a description of OFFSET and SYMBOL."
39   (let ((old-offset (cdr-safe (or (assq symbol c-offsets-alist)
40                                   (assq symbol (get 'c-offsets-alist
41                                                       'c-stylevar-fallback))))))
42     (if old-offset
43         (if (listp old-offset)
44             (c-set-offset symbol (cons offset old-offset))
45           (c-set-offset symbol (list offset old-offset)))
46       (c-set-offset symbol offset))))
47
48 (defun jde-annotations-setup ()
49   "Set up java mode indent function to handle java 1.5 annotations.
50 The setup function adds one of the custom indentation functions
51 `c-single-indent-after-java-annotations' or `c-no-indent-after-java-annotations' 
52 to the offset lists of the symbols `arglist-intro', `topmost-intro-cont', `arglist-intro',
53 `arglist-close', `statement-cont' and `func-decl-cont'. 
54 This function should be called after any calls to `c-set-style'."
55   (c-preprend-offset 'arglist-intro 'c-single-indent-after-java-annotations)
56   (c-preprend-offset 'topmost-intro-cont 'c-no-indent-after-java-annotations)
57   (c-preprend-offset 'arglist-close 'c-no-indent-after-java-annotations)
58   (c-preprend-offset 'statement-cont 'c-no-indent-after-java-annotations)
59   (c-preprend-offset 'func-decl-cont 'c-no-indent-after-java-annotations))
60
61 (defun c-only-java-annotations-p (langelem)
62   "Check if there are only java annotations before the current line.
63 It does this by moving across the region from the start of
64 LANGELEM to the beginning of this line one sexp at a time.  If
65 during this traversal, this function only sees whitespaces
66 followed by either a '@' or a '(' then it returns t."
67   (save-excursion
68     (condition-case err ;; return nil if  any errors are thrown by forward-sexp
69         (let* ((lim (1- (c-point 'bol)))
70                (throws (catch 'notAnno
71                      (goto-char (cdr langelem))
72                      (while (< (point) lim)
73                        (if (not (looking-at "\\(\\s \\|\n\\)*\\(@\\|(\\)"))
74                            (throw 'notAnno t))
75                        (forward-sexp 1)))))
76           (if (not throws)
77               t)))))
78
79 (defun c-no-indent-after-java-annotations (langelem)
80   "If preceeded by java annotations, indent this line the same as the previous.
81 Works with topmost-intro-cont, statement-cont, arglist-close and func-decl-cont.
82
83 With topmost-intro-cont, indents as
84 @Id
85 Long foo;
86
87 instead of
88 @Id
89 <-->Long foo;
90 Also for method and class declarations instead of the field foo.
91 With statement-cont, indents local variables with annotations as above.
92
93 With statement-cont, indents as
94 @Id(
95     arg=\"value\"
96 )
97 Long foo;
98
99 instead of
100 @Id(
101     arg=\"value\"
102 )
103 <-->Long foo;
104
105
106 With arglist-close, indents as
107 @RequestForEnhancement(
108 ...
109 )
110
111 Instead of
112 @RequestForEnhancement(
113 ...
114 <-->)
115
116 Argument LANGELEM The language element being indented."
117   (if (c-only-java-annotations-p langelem)
118       0))
119
120
121 (defun c-single-indent-after-java-annotations (langelem)
122   "If preceeded by java annotations, indent this line more than previous.
123 This function indents this line by `c-basic-offset' columns more
124 than the previous line.
125
126 Works with arglist-intro.
127
128 Indents as
129 @RequestForEnhancement(
130     id       = 2868724,
131 ...
132
133 instead of
134 @RequestForEnhancement(
135                        id            = 2868724,
136 ...
137
138 Argument LANGELEM The language element being indented."
139     (if (c-only-java-annotations-p langelem)
140       c-basic-offset))
141
142 (provide 'jde-annotations)
143
144 ;;; jde-annotations.el ends here