;;; jsp.el --- for editing Java Server Pages ;; Copyright (C) 2002 Jim Crossley ;; Author: Jim Crossley ;; Maintainer: Jim Crossley ;; Keywords: java, jsp ;; XEmacs is free software; you can redistribute it and/or modify it ;; under the terms of the GNU General Public License as published by ;; the Free Software Foundation; either version 2, or (at your option) ;; any later version. ;; XEmacs is distributed in the hope that it will be useful, but ;; WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU ;; General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with XEmacs; see the file COPYING. If not, write to the Free ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA ;; 02111-1307, USA. ;;; Commentary: ;; Historically, editing JSP files in one of the popular Emacs HTML ;; modes is not fun. Either custom tags are not indented correctly or ;; the evil, non-XML sections (delimited by <% and %>) mess up ;; indentation entirely. ;; PSGML mode is great for documents with DTD's, but it isn't ;; practical to define DTD's for JSP files, even when they're ;; well-formed XML, because any number of custom tag libraries can be ;; specified in the file, making the set of possible elements and/or ;; attributes infinite. If present, the DOCTYPE declaration in a JSP ;; file specifies not the type of the file itself but of the resulting ;; document *after* the JSP processor has resolved the custom tags ;; into their "real" values. ;; This mode's approach to the problem is fairly simple. It uses the ;; PSGML parse tree for indentation, but it prevents the PSGML parser ;; from discovering the DOCTYPE, forcing it to accept the ;; sgml-default-doctype-name for all JSP files. The jsp-mode is a ;; derivative of PSGML's xml-mode, because with one glaring exception, ;; a well-written jsp file should be well-formed XML. The exception, ;; of course, is the scriptlet, or essentially anything delimited by ;; <% and %>. ;; This mode features a very simple indentation scheme for JSP ;; scriptlets and JavaScript. It indents according to "brace level", ;; i.e. it increments the indentation level when it finds an opening ;; brace '{' and decrements when it finds a closing brace '}'. For it ;; to work, even single line statements should be enclosed by ;; braces. Obviously, complex javascript featuring switch statements ;; and the like will not indent properly. One possible solution to ;; that problem would be to separate the complex javascript into its ;; own file (indented with cc-mode perhaps?) and include it using the ;; 'src' attribute of the tags." (if (jsp-at-scriptlet-delimiter) nil (save-excursion (let ((here (point)) (bol (point-at-bol)) (eol (point-at-eol)) (before (search-backward-regexp "\\(<%[^=@-!]\\|" nil t) (search-forward "" nil t)) (point-max))) (level 1)) (when (and before (> after here)) (goto-char before) (while (< (point) bol) (when (search-forward "{" bol 0) (incf level))) (goto-char before) (while (< (point) eol) (when (search-forward "}" eol 0) (decf level))) level))))) (defun jsp-at-scriptlet-delimiter () "Return true if script delimiter is the only thing on current line" (save-excursion (back-to-indentation) (looking-at "\\(<%\\|%>\\|<%--\\|--%>\\|\\|\\)[ \t]*$")))