Mercurial > emacs
annotate lisp/textmodes/xml-lite.el @ 44243:c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
at unclosed tags unless it is at indentation.
Kill nested unclosed tags.
(xml-lite-calculate-indent, xml-lite-indent-line): Remove.
(xml-lite-mode): Use sgml-indent-line instead.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Fri, 29 Mar 2002 22:16:38 +0000 |
parents | 84ff52bf6d2f |
children | f259c3857fea |
rev | line source |
---|---|
43687 | 1 ;;; xml-lite.el --- an indentation-engine for XML |
2 | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
3 ;; Copyright (C) 2002 Free Software Foundation, Inc. |
43687 | 4 |
5 ;; Author: Mike Williams <mdub@bigfoot.com> | |
6 ;; Created: February 2001 | |
7 ;; Keywords: xml | |
8 | |
9 ;; This file is part of GNU Emacs. | |
10 | |
11 ;; This program is free software; you can redistribute it and/or modify | |
12 ;; it under the terms of the GNU General Public License as published by | |
13 ;; the Free Software Foundation; either version 2 of the License, or | |
14 ;; (at your option) any later version. | |
15 ;; | |
16 ;; This program is distributed in the hope that it will be useful, | |
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 ;; GNU General Public License for more details. | |
20 ;; | |
21 ;; You should have received a copy of the GNU General Public License | |
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 ;; Boston, MA 02111-1307, USA. | |
25 | |
26 ;;; Commentary: | |
27 ;; | |
28 ;; This package provides a simple indentation engine for XML. It is | |
29 ;; intended for use in situations where the full power of the popular PSGML | |
30 ;; package (DTD parsing, syntax checking) is not required. | |
31 ;; | |
32 ;; xml-lite is designed to be used in conjunction with the default GNU | |
33 ;; Emacs sgml-mode, to provide a lightweight XML-editing environment. | |
34 | |
35 ;;; Thanks: | |
36 ;; | |
37 ;; Jens Schmidt <Jens.Schmidt@oracle.com> | |
38 ;; for his feedback and suggestions | |
39 | |
40 ;;; Code: | |
41 | |
42 (eval-when-compile (require 'cl)) | |
43 (require 'sgml-mode) | |
44 | |
45 | |
46 ;; Variables | |
47 | |
48 (defgroup xml-lite nil | |
49 "Customizable variables for XML-Lite mode." | |
50 :group 'languages | |
51 ) | |
52 | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
53 (defcustom xml-lite-basic-offset 2 |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
54 "*Specifies the basic indentation level for `xml-lite-indent-line'." |
43687 | 55 :type 'integer |
56 :group 'xml-lite | |
57 ) | |
58 | |
59 (defcustom xml-lite-electric-slash 'close | |
60 "*If non-nil, inserting a '/' after a '<' behaves electrically. | |
61 If set to `indent', typing '</' just triggers reindentation. | |
62 If set to `close', typing '</' inserts an end-tag for the | |
63 enclosing XML element." | |
64 :type '(choice (const :tag "Indent" indent) | |
65 | |
66 (const :tag "Close" close) | |
67 (const :tag "No" nil)) | |
68 | |
69 :group 'xml-lite | |
70 ) | |
71 | |
72 (defcustom xml-lite-mode-line-string " XML" | |
73 "*String to display in the modeline when `xml-lite-mode' is active. | |
74 Set this to nil if you don't want a modeline indicator for xml-lite-mode." | |
75 :type 'string | |
76 :group 'xml-lite) | |
77 | |
78 (defcustom xml-lite-mode-hook nil | |
79 "*Hook called by `xml-lite-mode'." | |
80 :type 'hook | |
81 :group 'xml-lite) | |
82 | |
83 ;;;###autoload | |
84 (defvar xml-lite-mode nil | |
85 "Non-nil if `xml-lite-mode' is enabled.") | |
86 (make-variable-buffer-local 'xml-lite-mode) | |
87 | |
88 | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
89 ;; Syntax analysis |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
90 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
91 (defsubst xml-lite-at-indentation-p () |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
92 "Return true if point is at the first non-whitespace character on the line." |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
93 (save-excursion |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
94 (skip-chars-backward " \t") |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
95 (bolp))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
96 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
97 (defun xml-lite-in-string-p (&optional limit) |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
98 "Determine whether point is inside a string. If it is, return the |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
99 position of the character starting the string, else return nil. |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
100 |
44189
ee2adfa7e248
(xml-lite-in-string-p): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44187
diff
changeset
|
101 Parse begins from LIMIT, which defaults to the preceding occurence of a tag |
ee2adfa7e248
(xml-lite-in-string-p): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44187
diff
changeset
|
102 at the beginning of a line." |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
103 (let ((context (sgml-lexical-context limit))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
104 (if (eq (car context) 'string) (cdr context)))) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
105 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
106 |
43687 | 107 ;; Parsing |
108 (defstruct (xml-lite-tag | |
109 (:constructor xml-lite-make-tag (type start end name name-end))) | |
110 type start end name name-end) | |
111 (defsubst xml-lite-parse-tag-name () | |
112 "Skip past a tag-name, and return the name." | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
113 (buffer-substring-no-properties |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
114 (point) (progn (skip-syntax-forward "w_") (point)))) |
43687 | 115 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
116 (defsubst xml-lite-looking-back-at (s) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
117 (let ((limit (max (- (point) (length s)) (point-min)))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
118 (equal s (buffer-substring-no-properties limit (point))))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
119 |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
120 (defsubst xml-lite-looking-at (s) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
121 (let ((limit (min (+ (point) (length s))))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
122 (equal s (buffer-substring-no-properties (point) limit)))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
123 |
43687 | 124 (defun xml-lite-parse-tag-backward () |
125 "Get information about the parent tag." | |
126 (let ((limit (point)) | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
127 tag-type tag-start tag-end name name-end) |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
128 (with-syntax-table sgml-tag-syntax-table |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
129 (cond |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
130 |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
131 ((null (re-search-backward "[<>]" nil t))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
132 |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
133 ((= ?> (char-after)) ;--- found tag-end --- |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
134 (setq tag-end (1+ (point))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
135 (goto-char tag-end) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
136 (cond |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
137 ((xml-lite-looking-back-at "--") ; comment |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
138 (setq tag-type 'comment |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
139 tag-start (search-backward "<!--" nil t))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
140 ((xml-lite-looking-back-at "]]>") ; cdata |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
141 (setq tag-type 'cdata |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
142 tag-start (search-backward "![CDATA[" nil t))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
143 (t |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
144 (setq tag-start (ignore-errors (backward-sexp) (point)))))) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
145 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
146 ((= ?< (char-after)) ;--- found tag-start --- |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
147 ;; !!! This should not happen because the caller should be careful |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
148 ;; that we do not start from within a tag !!! |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
149 (setq tag-start (point)) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
150 (goto-char (1+ tag-start)) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
151 (cond |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
152 ((xml-lite-looking-at "!--") ; comment |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
153 (setq tag-type 'comment |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
154 tag-end (search-forward "-->" nil t))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
155 ((xml-lite-looking-at "![CDATA[") ; cdata |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
156 (setq tag-type 'cdata |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
157 tag-end (search-forward "]]>" nil t))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
158 (t |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
159 (goto-char tag-start) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
160 (setq tag-end (ignore-errors (forward-sexp) (point))))))) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
161 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
162 (cond |
43687 | 163 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
164 ((or tag-type (null tag-start))) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
165 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
166 ((= ?! (char-after (1+ tag-start))) ; declaration |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
167 (setq tag-type 'decl)) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
168 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
169 ((= ?? (char-after (1+ tag-start))) ; processing-instruction |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
170 (setq tag-type 'pi)) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
171 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
172 ((= ?/ (char-after (1+ tag-start))) ; close-tag |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
173 (goto-char (+ 2 tag-start)) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
174 (setq tag-type 'close |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
175 name (xml-lite-parse-tag-name) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
176 name-end (point))) |
43687 | 177 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
178 ((member ; JSP tags etc |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
179 (char-after (1+ tag-start)) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
180 '(?% ?#)) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
181 (setq tag-type 'unknown)) |
43687 | 182 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
183 (t |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
184 (goto-char (1+ tag-start)) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
185 (setq tag-type 'open |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
186 name (xml-lite-parse-tag-name) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
187 name-end (point)) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
188 ;; check whether it's an empty tag |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
189 (if (or (and tag-end (eq ?/ (char-before (- tag-end 1)))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
190 (and (not sgml-xml-mode) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
191 (member-ignore-case name sgml-empty-tags))) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
192 (setq tag-type 'empty)))) |
43687 | 193 |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
194 (cond |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
195 (tag-start |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
196 (goto-char tag-start) |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
197 (xml-lite-make-tag tag-type tag-start tag-end name name-end)))))) |
43687 | 198 |
199 (defsubst xml-lite-inside-tag-p (tag-info &optional point) | |
200 "Return true if TAG-INFO contains the POINT." | |
201 (let ((end (xml-lite-tag-end tag-info)) | |
202 (point (or point (point)))) | |
203 (or (null end) | |
204 (> end point)))) | |
205 | |
206 (defun xml-lite-get-context (&optional full) | |
207 "Determine the context of the current position. | |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
208 If FULL is `empty', return even if the context is empty (i.e. |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
209 we just skipped over some element and got to a beginning of line). |
43687 | 210 If FULL is non-nil, parse back to the beginning of the buffer, otherwise |
211 parse until we find a start-tag as the first thing on a line. | |
212 | |
213 The context is a list of tag-info structures. The last one is the tag | |
214 immediately enclosing the current position." | |
215 (let ((here (point)) | |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
216 (ignore nil) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
217 (context nil) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
218 tag-info) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
219 ;; CONTEXT keeps track of the tag-stack |
44201
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
220 ;; IGNORE keeps track of the nesting level of point relative to the |
2eeb8d7f1161
(xml-lite-in-string-p): Use sgml-lexical-context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44189
diff
changeset
|
221 ;; first (outermost) tag on the context. This is the list of |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
222 ;; enclosing start-tags we'll have to ignore. |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
223 (skip-chars-backward " \t\n") ; Make sure we're not at indentation. |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
224 (while |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
225 (and (or ignore (not (if full (eq full 'empty) context)) |
44243
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
226 (not (xml-lite-at-indentation-p)) |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
227 (and (not sgml-xml-mode) context |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
228 (/= (point) (xml-lite-tag-start (car context))) |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
229 (member-ignore-case (xml-lite-tag-name (car context)) |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
230 sgml-unclosed-tags))) |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
231 (setq tag-info (xml-lite-parse-tag-backward))) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
232 |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
233 ;; This tag may enclose things we thought were tags. If so, |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
234 ;; discard them. |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
235 (while (and context |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
236 (> (xml-lite-tag-end tag-info) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
237 (xml-lite-tag-end (car context)))) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
238 (setq context (cdr context))) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
239 |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
240 (cond |
43687 | 241 |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
242 ;; inside a tag ... |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
243 ((xml-lite-inside-tag-p tag-info here) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
244 (push tag-info context)) |
43687 | 245 |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
246 ;; start-tag |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
247 ((eq (xml-lite-tag-type tag-info) 'open) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
248 (cond |
44243
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
249 ((null ignore) |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
250 (if (and (not sgml-xml-mode) context |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
251 (member-ignore-case (xml-lite-tag-name tag-info) |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
252 sgml-unclosed-tags) |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
253 (eq t (compare-strings |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
254 (xml-lite-tag-name tag-info) nil nil |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
255 (xml-lite-tag-name (car context)) nil nil t))) |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
256 ;; There was an implicit end-tag. |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
257 nil |
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
258 (push tag-info context))) |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
259 ((eq t (compare-strings (xml-lite-tag-name tag-info) nil nil |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
260 (car ignore) nil nil t)) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
261 (setq ignore (cdr ignore))) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
262 (t |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
263 ;; The open and close tags don't match. |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
264 (if (not sgml-xml-mode) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
265 ;; Assume the open tag is simply not closed. |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
266 (unless (member-ignore-case (xml-lite-tag-name tag-info) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
267 sgml-unclosed-tags) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
268 (message "Unclosed tag <%s>" (xml-lite-tag-name tag-info))) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
269 (message "Unmatched tags <%s> and </%s>" |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
270 (xml-lite-tag-name tag-info) (pop ignore)))))) |
43687 | 271 |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
272 ;; end-tag |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
273 ((eq (xml-lite-tag-type tag-info) 'close) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
274 (if (and (not sgml-xml-mode) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
275 (member-ignore-case (xml-lite-tag-name tag-info) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
276 sgml-empty-tags)) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
277 (message "Spurious </%s>: empty tag" (xml-lite-tag-name tag-info)) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
278 (push (xml-lite-tag-name tag-info) ignore))) |
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
279 )) |
43687 | 280 |
281 ;; return context | |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
282 context)) |
43687 | 283 |
284 (defun xml-lite-show-context (&optional full) | |
285 "Display the current context. | |
286 If FULL is non-nil, parse back to the beginning of the buffer." | |
287 (interactive "P") | |
288 (with-output-to-temp-buffer "*XML Context*" | |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
289 (pp (save-excursion (xml-lite-get-context full))))) |
43687 | 290 |
291 | |
292 ;; Indenting | |
293 | |
294 | |
295 ;; Editing shortcuts | |
296 | |
297 (defun xml-lite-insert-end-tag () | |
298 "Insert an end-tag for the current element." | |
299 (interactive) | |
44242
84ff52bf6d2f
(xml-lite-get-context): Allow stopping even with an empty context.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44201
diff
changeset
|
300 (let* ((context (save-excursion (xml-lite-get-context))) |
43687 | 301 (tag-info (car (last context))) |
302 (type (and tag-info (xml-lite-tag-type tag-info)))) | |
303 | |
304 (cond | |
305 | |
306 ((null context) | |
307 (error "Nothing to close")) | |
308 | |
309 ;; inside a tag | |
310 ((xml-lite-inside-tag-p tag-info) | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
311 (insert (cond |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
312 ((eq type 'open) " />") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
313 ((eq type 'comment) " -->") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
314 ((eq type 'cdata) "]]>") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
315 ((eq type 'jsp) "%>") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
316 ((eq type 'pi) "?>") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
317 (t ">")))) |
43687 | 318 |
319 ;; inside an element | |
320 ((eq type 'open) | |
321 (insert "</" (xml-lite-tag-name tag-info) ">") | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
322 (indent-according-to-mode)) |
43687 | 323 |
324 (t | |
325 (error "Nothing to close"))))) | |
326 | |
327 (defun xml-lite-slash (arg) | |
328 "Insert ARG slash characters. | |
329 Behaves electrically if `xml-lite-electric-slash' is non-nil." | |
330 (interactive "p") | |
331 (cond | |
332 ((not (and (eq (char-before) ?<) (= arg 1))) | |
333 (insert-char ?/ arg)) | |
334 ((eq xml-lite-electric-slash 'indent) | |
335 (insert-char ?/ 1) | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
336 (indent-according-to-mode)) |
43687 | 337 ((eq xml-lite-electric-slash 'close) |
338 (delete-backward-char 1) | |
339 (xml-lite-insert-end-tag)) | |
340 (t | |
341 (insert-char ?/ arg)))) | |
342 | |
343 | |
344 ;; Keymap | |
345 | |
346 (defvar xml-lite-mode-map | |
347 (let ((map (make-sparse-keymap))) | |
348 (define-key map "\C-c/" 'xml-lite-insert-end-tag) | |
349 (define-key map "\C-c\C-s" 'xml-lite-show-context) | |
350 (define-key map "/" 'xml-lite-slash) | |
351 map) | |
352 "Key bindings for `xml-lite-mode'.") | |
353 | |
354 | |
355 ;; Minor mode | |
356 | |
357 ;;;###autoload | |
358 (define-minor-mode xml-lite-mode | |
359 "Toggle `xml-lite-mode'. | |
360 With ARG, enable xml-lite-mode if and only if ARG is positive. | |
361 | |
362 xml-lite-mode provides indentation for XML tags. The value of | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
363 `xml-lite-basic-offset' determines the amount of indentation. |
43687 | 364 |
365 Key bindings: | |
366 \\{xml-lite-mode-map}" | |
367 nil ; initial value | |
368 " XML" ; mode indicator | |
369 'xml-lite-mode-map ; keymap | |
370 (if xml-lite-mode | |
371 (progn | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
372 (if (eq major-mode 'fundamental-mode) (sgml-mode)) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
373 (set (make-local-variable 'sgml-xml-mode) t) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
374 (set (make-local-variable 'xml-lite-orig-indent-line-function) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
375 indent-line-function) |
44243
c3ee131a3ab1
(xml-lite-get-context): Don't stop parsing
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44242
diff
changeset
|
376 (set (make-local-variable 'indent-line-function) 'sgml-indent-line)) |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
377 (kill-local-variable 'sgml-xml-mode) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
378 (setq indent-line-function xml-lite-orig-indent-line-function))) |
43687 | 379 |
380 (provide 'xml-lite) | |
381 | |
382 ;;; xml-lite.el ends here |