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