Mercurial > emacs
annotate lisp/textmodes/xml-lite.el @ 44198:dfba0057d1ff
*** empty log message ***
author | André Spiegel <spiegel@gnu.org> |
---|---|
date | Thu, 28 Mar 2002 14:29:06 +0000 |
parents | ee2adfa7e248 |
children | 2eeb8d7f1161 |
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) |
44189
ee2adfa7e248
(xml-lite-in-string-p): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44187
diff
changeset
|
98 "Determine whether point is inside a string. |
ee2adfa7e248
(xml-lite-in-string-p): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44187
diff
changeset
|
99 |
ee2adfa7e248
(xml-lite-in-string-p): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44187
diff
changeset
|
100 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
|
101 at the beginning of a line." |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
102 (let (syntax-info) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
103 (or limit |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
104 (setq limit (or (save-excursion |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
105 (re-search-backward "^[ \t]*<" nil t)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
106 (point-min)))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
107 (setq syntax-info (parse-partial-sexp limit (point))) |
44189
ee2adfa7e248
(xml-lite-in-string-p): Simplify.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44187
diff
changeset
|
108 (if (nth 3 syntax-info) (nth 8 syntax-info)))) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
109 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
110 |
43687 | 111 ;; Parsing |
112 (defstruct (xml-lite-tag | |
113 (:constructor xml-lite-make-tag (type start end name name-end))) | |
114 type start end name name-end) | |
115 (defsubst xml-lite-parse-tag-name () | |
116 "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
|
117 (buffer-substring-no-properties |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
118 (point) (progn (skip-syntax-forward "w_") (point)))) |
43687 | 119 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
120 (defsubst xml-lite-looking-back-at (s) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
121 (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
|
122 (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
|
123 |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
124 (defsubst xml-lite-looking-at (s) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
125 (let ((limit (min (+ (point) (length s))))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
126 (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
|
127 |
43687 | 128 (defun xml-lite-parse-tag-backward () |
129 "Get information about the parent tag." | |
130 (let ((limit (point)) | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
131 tag-type tag-start tag-end name name-end) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
132 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
133 (cond |
43687 | 134 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
135 ((null (re-search-backward "[<>]" nil t))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
136 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
137 ((= ?> (char-after)) ;--- found tag-end --- |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
138 (setq tag-end (1+ (point))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
139 (goto-char tag-end) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
140 (cond |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
141 ((xml-lite-looking-back-at "--") ; comment |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
142 (setq tag-type 'comment |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
143 tag-start (search-backward "<!--" nil t))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
144 ((xml-lite-looking-back-at "]]>") ; cdata |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
145 (setq tag-type 'cdata |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
146 tag-start (search-backward "![CDATA[" nil t))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
147 (t |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
148 (setq tag-start |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
149 (ignore-errors (backward-sexp) (point)))))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
150 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
151 ((= ?< (char-after)) ;--- found tag-start --- |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
152 (setq tag-start (point)) |
43687 | 153 (goto-char (1+ tag-start)) |
154 (cond | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
155 ((xml-lite-looking-at "!--") ; comment |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
156 (setq tag-type 'comment |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
157 tag-end (search-forward "-->" nil t))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
158 ((xml-lite-looking-at "![CDATA[") ; cdata |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
159 (setq tag-type 'cdata |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
160 tag-end (search-forward "]]>" nil t))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
161 (t |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
162 (goto-char tag-start) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
163 (setq tag-end |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
164 (ignore-errors (forward-sexp) (point)))))) |
43687 | 165 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
166 ) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
167 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
168 (cond |
43687 | 169 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
170 ((or tag-type (null tag-start))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
171 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
172 ((= ?! (char-after (1+ tag-start))) ; declaration |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
173 (setq tag-type 'decl)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
174 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
175 ((= ?? (char-after (1+ tag-start))) ; processing-instruction |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
176 (setq tag-type 'pi)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
177 |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
178 ((= ?/ (char-after (1+ tag-start))) ; close-tag |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
179 (goto-char (+ 2 tag-start)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
180 (setq tag-type 'close |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
181 name (xml-lite-parse-tag-name) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
182 name-end (point))) |
43687 | 183 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
184 ((member ; JSP tags etc |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
185 (char-after (1+ tag-start)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
186 '(?% ?#)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
187 (setq tag-type 'unknown)) |
43687 | 188 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
189 (t |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
190 (goto-char (1+ tag-start)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
191 (setq tag-type 'open |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
192 name (xml-lite-parse-tag-name) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
193 name-end (point)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
194 ;; check whether it's an empty tag |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
195 (if (or (and tag-end (eq ?/ (char-before (- tag-end 1)))) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
196 (and (not sgml-xml-mode) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
197 (member-ignore-case name sgml-empty-tags))) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
198 (setq tag-type 'empty)))) |
43687 | 199 |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
200 (cond |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
201 (tag-start |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
202 (goto-char tag-start) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
203 (xml-lite-make-tag tag-type tag-start tag-end name name-end))))) |
43687 | 204 |
205 (defsubst xml-lite-inside-tag-p (tag-info &optional point) | |
206 "Return true if TAG-INFO contains the POINT." | |
207 (let ((end (xml-lite-tag-end tag-info)) | |
208 (point (or point (point)))) | |
209 (or (null end) | |
210 (> end point)))) | |
211 | |
212 (defun xml-lite-get-context (&optional full) | |
213 "Determine the context of the current position. | |
214 If FULL is non-nil, parse back to the beginning of the buffer, otherwise | |
215 parse until we find a start-tag as the first thing on a line. | |
216 | |
217 The context is a list of tag-info structures. The last one is the tag | |
218 immediately enclosing the current position." | |
219 (let ((here (point)) | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
220 (ignore-depth 0) |
43687 | 221 tag-info context) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
222 ;; CONTEXT keeps track of the tag-stack |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
223 ;; IGNORE-DEPTH keeps track of the nesting level of point relative to the |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
224 ;; first (outermost) tag on the context. This is the number of |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
225 ;; enclosing start-tags we'll have to ignore. |
43687 | 226 (save-excursion |
227 | |
228 (while | |
229 (and (or (not context) | |
230 full | |
231 (not (xml-lite-at-indentation-p))) | |
232 (setq tag-info (xml-lite-parse-tag-backward))) | |
233 | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
234 ;; This tag may enclose things we thought were tags. If so, |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
235 ;; discard them. |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
236 (while (and context |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
237 (> (xml-lite-tag-end tag-info) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
238 (xml-lite-tag-end (car context)))) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
239 (setq context (cdr context))) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
240 |
43687 | 241 (cond |
242 | |
243 ;; inside a tag ... | |
244 ((xml-lite-inside-tag-p tag-info here) | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
245 (push tag-info context)) |
43687 | 246 |
247 ;; start-tag | |
248 ((eq (xml-lite-tag-type tag-info) 'open) | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
249 (setq ignore-depth (1- ignore-depth)) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
250 (when (= ignore-depth -1) |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
251 (push tag-info context) |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
252 (setq ignore-depth 0))) |
43687 | 253 |
254 ;; end-tag | |
255 ((eq (xml-lite-tag-type tag-info) 'close) | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
256 (setq ignore-depth (1+ ignore-depth))) |
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
257 |
43687 | 258 ))) |
259 | |
260 ;; return context | |
261 context | |
262 )) | |
263 | |
264 (defun xml-lite-show-context (&optional full) | |
265 "Display the current context. | |
266 If FULL is non-nil, parse back to the beginning of the buffer." | |
267 (interactive "P") | |
268 (with-output-to-temp-buffer "*XML Context*" | |
269 (pp (xml-lite-get-context full)))) | |
270 | |
271 | |
272 ;; Indenting | |
273 | |
274 (defun xml-lite-calculate-indent () | |
275 "Calculate the column to which this line should be indented." | |
276 (let* ((here (point)) | |
277 (context (xml-lite-get-context)) | |
278 (ref-tag-info (car context)) | |
279 (last-tag-info (car (last context)))) | |
280 | |
281 (save-excursion | |
282 (cond | |
283 | |
284 ;; no context | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
285 ((null context) 0) |
43687 | 286 |
287 ;; inside a comment | |
288 ((eq 'comment (xml-lite-tag-type last-tag-info)) | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
289 (let ((mark (looking-at "--"))) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
290 (goto-char (xml-lite-tag-start last-tag-info)) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
291 (forward-char 2) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
292 (if mark (current-column) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
293 (forward-char 2) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
294 (+ (if (zerop (skip-chars-forward " \t")) 1 0) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
295 (current-column))))) |
43687 | 296 |
297 ;; inside a tag | |
298 ((xml-lite-inside-tag-p last-tag-info here) | |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
299 |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
300 (let ((start-of-enclosing-string |
44168
68fd324f9f0f
(xml-lite-at-indentation-p): Move.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
43687
diff
changeset
|
301 (xml-lite-in-string-p (xml-lite-tag-start last-tag-info)))) |
43687 | 302 (cond |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
303 ;; inside an attribute value |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
304 (start-of-enclosing-string |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
305 (goto-char start-of-enclosing-string) |
43687 | 306 (1+ (current-column))) |
307 ;; if we have a tag-name, base indent on that | |
308 ((and (xml-lite-tag-name-end last-tag-info) | |
309 (progn | |
310 (goto-char (xml-lite-tag-name-end last-tag-info)) | |
311 (not (looking-at "[ \t]*$")))) | |
312 (1+ (current-column))) | |
313 ;; otherwise, add indent-offset | |
314 (t | |
315 (goto-char (xml-lite-tag-start last-tag-info)) | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
316 (+ (current-column) xml-lite-basic-offset))))) |
43687 | 317 |
318 ;; inside an element | |
319 (t | |
320 ;; indent to start of tag | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
321 (let ((indent-offset xml-lite-basic-offset)) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
322 ;; add xml-lite-basic-offset, unless we're looking at the |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
323 ;; matching end-tag |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
324 (if (and (eq (length context) 1) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
325 (xml-lite-looking-at "</")) |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
326 (setq indent-offset 0)) |
43687 | 327 (goto-char (xml-lite-tag-start ref-tag-info)) |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
328 (+ (current-column) indent-offset))) |
43687 | 329 |
330 )))) | |
331 | |
332 (defun xml-lite-indent-line () | |
333 "Indent the current line as XML." | |
334 (interactive) | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
335 (let* ((savep (point)) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
336 (indent-col |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
337 (save-excursion |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
338 (beginning-of-line) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
339 (skip-chars-forward " \t") |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
340 (if (>= (point) savep) (setq savep nil)) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
341 ;; calculate basic indent |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
342 (xml-lite-calculate-indent)))) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
343 (if savep |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
344 (save-excursion (indent-line-to indent-col)) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
345 (indent-line-to indent-col)))) |
43687 | 346 |
347 | |
348 ;; Editing shortcuts | |
349 | |
350 (defun xml-lite-insert-end-tag () | |
351 "Insert an end-tag for the current element." | |
352 (interactive) | |
353 (let* ((context (xml-lite-get-context)) | |
354 (tag-info (car (last context))) | |
355 (type (and tag-info (xml-lite-tag-type tag-info)))) | |
356 | |
357 (cond | |
358 | |
359 ((null context) | |
360 (error "Nothing to close")) | |
361 | |
362 ;; inside a tag | |
363 ((xml-lite-inside-tag-p tag-info) | |
44187
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
364 (insert (cond |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
365 ((eq type 'open) " />") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
366 ((eq type 'comment) " -->") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
367 ((eq type 'cdata) "]]>") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
368 ((eq type 'jsp) "%>") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
369 ((eq type 'pi) "?>") |
65437de0940f
Fix copyright notice.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44180
diff
changeset
|
370 (t ">")))) |
43687 | 371 |
372 ;; inside an element | |
373 ((eq type 'open) | |
374 (insert "</" (xml-lite-tag-name tag-info) ">") | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
375 (indent-according-to-mode)) |
43687 | 376 |
377 (t | |
378 (error "Nothing to close"))))) | |
379 | |
380 (defun xml-lite-slash (arg) | |
381 "Insert ARG slash characters. | |
382 Behaves electrically if `xml-lite-electric-slash' is non-nil." | |
383 (interactive "p") | |
384 (cond | |
385 ((not (and (eq (char-before) ?<) (= arg 1))) | |
386 (insert-char ?/ arg)) | |
387 ((eq xml-lite-electric-slash 'indent) | |
388 (insert-char ?/ 1) | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
389 (indent-according-to-mode)) |
43687 | 390 ((eq xml-lite-electric-slash 'close) |
391 (delete-backward-char 1) | |
392 (xml-lite-insert-end-tag)) | |
393 (t | |
394 (insert-char ?/ arg)))) | |
395 | |
396 | |
397 ;; Keymap | |
398 | |
399 (defvar xml-lite-mode-map | |
400 (let ((map (make-sparse-keymap))) | |
401 (define-key map "\C-c/" 'xml-lite-insert-end-tag) | |
402 (define-key map "\C-c\C-s" 'xml-lite-show-context) | |
403 (define-key map "/" 'xml-lite-slash) | |
404 map) | |
405 "Key bindings for `xml-lite-mode'.") | |
406 | |
407 | |
408 ;; Minor mode | |
409 | |
410 ;;;###autoload | |
411 (define-minor-mode xml-lite-mode | |
412 "Toggle `xml-lite-mode'. | |
413 With ARG, enable xml-lite-mode if and only if ARG is positive. | |
414 | |
415 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
|
416 `xml-lite-basic-offset' determines the amount of indentation. |
43687 | 417 |
418 Key bindings: | |
419 \\{xml-lite-mode-map}" | |
420 nil ; initial value | |
421 " XML" ; mode indicator | |
422 'xml-lite-mode-map ; keymap | |
423 (if xml-lite-mode | |
424 (progn | |
44180
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
425 (if (eq major-mode 'fundamental-mode) (sgml-mode)) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
426 (set (make-local-variable 'sgml-xml-mode) t) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
427 (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
|
428 indent-line-function) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
429 (set (make-local-variable 'indent-line-function) 'xml-lite-indent-line)) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
430 (kill-local-variable 'sgml-xml-mode) |
e7a365c909ff
Don't require `custom'.
Stefan Monnier <monnier@iro.umontreal.ca>
parents:
44168
diff
changeset
|
431 (setq indent-line-function xml-lite-orig-indent-line-function))) |
43687 | 432 |
433 (provide 'xml-lite) | |
434 | |
435 ;;; xml-lite.el ends here |