1 ;;; jde-annotations.el --- Indentation for Java 5 annotations.
3 ;; Copyright (C) 2006 by Suraj Acharya
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
10 ;; This file is not part of Emacs
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.
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.
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.
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.
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))))))
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))))
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))
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."
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\\)*\\(@\\|(\\)"))
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.
83 With topmost-intro-cont, indents as
90 Also for method and class declarations instead of the field foo.
91 With statement-cont, indents local variables with annotations as above.
93 With statement-cont, indents as
106 With arglist-close, indents as
107 @RequestForEnhancement(
112 @RequestForEnhancement(
116 Argument LANGELEM The language element being indented."
117 (if (c-only-java-annotations-p langelem)
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.
126 Works with arglist-intro.
129 @RequestForEnhancement(
134 @RequestForEnhancement(
138 Argument LANGELEM The language element being indented."
139 (if (c-only-java-annotations-p langelem)
142 (provide 'jde-annotations)
144 ;;; jde-annotations.el ends here