comparison 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
comparison
equal deleted inserted replaced
44186:9ea4e6199d1c 44187:65437de0940f
1 ;;; xml-lite.el --- an indentation-engine for XML 1 ;;; xml-lite.el --- an indentation-engine for XML
2 2
3 ;; Copyright (C) 2001 Mike Williams <mdub@bigfoot.com> 3 ;; Copyright (C) 2002 Free Software Foundation, Inc.
4 4
5 ;; Author: Mike Williams <mdub@bigfoot.com> 5 ;; Author: Mike Williams <mdub@bigfoot.com>
6 ;; Created: February 2001 6 ;; Created: February 2001
7 ;; Version: $Revision: 1.2 $
8 ;; Keywords: xml 7 ;; Keywords: xml
9 8
10 ;; This file is part of GNU Emacs. 9 ;; This file is part of GNU Emacs.
11 10
12 ;; This program is free software; you can redistribute it and/or modify 11 ;; This program is free software; you can redistribute it and/or modify
30 ;; intended for use in situations where the full power of the popular PSGML 29 ;; intended for use in situations where the full power of the popular PSGML
31 ;; package (DTD parsing, syntax checking) is not required. 30 ;; package (DTD parsing, syntax checking) is not required.
32 ;; 31 ;;
33 ;; xml-lite is designed to be used in conjunction with the default GNU 32 ;; xml-lite is designed to be used in conjunction with the default GNU
34 ;; Emacs sgml-mode, to provide a lightweight XML-editing environment. 33 ;; Emacs sgml-mode, to provide a lightweight XML-editing environment.
35 ;;
36 ;; Updates will be made available at:
37 ;; http://www.bigfoot.com/~mdub/software/xml-lite.el
38 ;;
39 ;; Note: the font-lock support that was in this package has been removed.
40 34
41 ;;; Thanks: 35 ;;; Thanks:
42 ;; 36 ;;
43 ;; Jens Schmidt <Jens.Schmidt@oracle.com> 37 ;; Jens Schmidt <Jens.Schmidt@oracle.com>
44 ;; for his feedback and suggestions 38 ;; for his feedback and suggestions
54 (defgroup xml-lite nil 48 (defgroup xml-lite nil
55 "Customizable variables for XML-Lite mode." 49 "Customizable variables for XML-Lite mode."
56 :group 'languages 50 :group 'languages
57 ) 51 )
58 52
59 (defcustom xml-lite-indent-offset 4 53 (defcustom xml-lite-basic-offset 2
60 "*Specifies the default indentation level for `xml-lite-indent-line'." 54 "*Specifies the basic indentation level for `xml-lite-indent-line'."
61 :type 'integer
62 :group 'xml-lite
63 )
64
65 (defcustom xml-lite-indent-comment-offset 5
66 "*Specifies the indentation level for XML comments."
67 :type 'integer 55 :type 'integer
68 :group 'xml-lite 56 :group 'xml-lite
69 ) 57 )
70 58
71 (defcustom xml-lite-electric-slash 'close 59 (defcustom xml-lite-electric-slash 'close
117 (if (nth 3 syntax-info) 105 (if (nth 3 syntax-info)
118 (list (nth 3 syntax-info) (nth 8 syntax-info))))) 106 (list (nth 3 syntax-info) (nth 8 syntax-info)))))
119 107
120 108
121 ;; Parsing 109 ;; Parsing
122
123 (defstruct (xml-lite-tag 110 (defstruct (xml-lite-tag
124 (:constructor xml-lite-make-tag (type start end name name-end))) 111 (:constructor xml-lite-make-tag (type start end name name-end)))
125 type start end name name-end) 112 type start end name name-end)
126
127 (defsubst xml-lite-parse-tag-name () 113 (defsubst xml-lite-parse-tag-name ()
128 "Skip past a tag-name, and return the name." 114 "Skip past a tag-name, and return the name."
129 (buffer-substring-no-properties 115 (buffer-substring-no-properties
130 (point) (progn (skip-syntax-forward "w_") (point)))) 116 (point) (progn (skip-syntax-forward "w_") (point))))
131 117
132 (defsubst xml-lite-looking-back-at (s) 118 (defsubst xml-lite-looking-back-at (s)
133 (let ((limit (max (- (point) (length s)) (point-min)))) 119 (let ((limit (max (- (point) (length s)) (point-min))))
134 (equal s (buffer-substring-no-properties limit (point))))) 120 (equal s (buffer-substring-no-properties limit (point)))))
135 121
136 (defsubst xml-lite-looking-at (s) 122 (defsubst xml-lite-looking-at (s)
137 (let ((limit (min (+ (point) (length s))))) 123 (let ((limit (min (+ (point) (length s)))))
138 (equal s (buffer-substring-no-properties (point) limit)))) 124 (equal s (buffer-substring-no-properties (point) limit))))
139 125
140 (defun xml-lite-parse-tag-backward () 126 (defun xml-lite-parse-tag-backward ()
141 "Get information about the parent tag." 127 "Get information about the parent tag."
252 238
253 (cond 239 (cond
254 240
255 ;; inside a tag ... 241 ;; inside a tag ...
256 ((xml-lite-inside-tag-p tag-info here) 242 ((xml-lite-inside-tag-p tag-info here)
257 (setq context (cons tag-info context))) 243 (push tag-info context))
258 244
259 ;; start-tag 245 ;; start-tag
260 ((eq (xml-lite-tag-type tag-info) 'open) 246 ((eq (xml-lite-tag-type tag-info) 'open)
261 (setq ignore-depth (1- ignore-depth)) 247 (setq ignore-depth (1- ignore-depth))
262 (when (= ignore-depth -1) 248 (when (= ignore-depth -1)
292 278
293 (save-excursion 279 (save-excursion
294 (cond 280 (cond
295 281
296 ;; no context 282 ;; no context
297 ((null context) 283 ((null context) 0)
298 0)
299 284
300 ;; inside a comment 285 ;; inside a comment
301 ((eq 'comment (xml-lite-tag-type last-tag-info)) 286 ((eq 'comment (xml-lite-tag-type last-tag-info))
302 (goto-char (xml-lite-tag-start last-tag-info)) 287 (let ((mark (looking-at "--")))
303 (+ (current-column) xml-lite-indent-comment-offset)) 288 (goto-char (xml-lite-tag-start last-tag-info))
289 (forward-char 2)
290 (if mark (current-column)
291 (forward-char 2)
292 (+ (if (zerop (skip-chars-forward " \t")) 1 0)
293 (current-column)))))
304 294
305 ;; inside a tag 295 ;; inside a tag
306 ((xml-lite-inside-tag-p last-tag-info here) 296 ((xml-lite-inside-tag-p last-tag-info here)
307 297
308 (let ((in-string 298 (let ((start-of-enclosing-string
309 (xml-lite-in-string-p (xml-lite-tag-start last-tag-info)))) 299 (xml-lite-in-string-p (xml-lite-tag-start last-tag-info))))
310 (cond 300 (cond
311 ;; inside a string 301 ;; inside an attribute value
312 (in-string 302 (start-of-enclosing-string
313 (goto-char (nth 1 in-string)) 303 (goto-char start-of-enclosing-string)
314 (1+ (current-column))) 304 (1+ (current-column)))
315 ;; if we have a tag-name, base indent on that 305 ;; if we have a tag-name, base indent on that
316 ((and (xml-lite-tag-name-end last-tag-info) 306 ((and (xml-lite-tag-name-end last-tag-info)
317 (progn 307 (progn
318 (goto-char (xml-lite-tag-name-end last-tag-info)) 308 (goto-char (xml-lite-tag-name-end last-tag-info))
319 (not (looking-at "[ \t]*$")))) 309 (not (looking-at "[ \t]*$"))))
320 (1+ (current-column))) 310 (1+ (current-column)))
321 ;; otherwise, add indent-offset 311 ;; otherwise, add indent-offset
322 (t 312 (t
323 (goto-char (xml-lite-tag-start last-tag-info)) 313 (goto-char (xml-lite-tag-start last-tag-info))
324 (+ (current-column) xml-lite-indent-offset))))) 314 (+ (current-column) xml-lite-basic-offset)))))
325 315
326 ;; inside an element 316 ;; inside an element
327 (t 317 (t
328 ;; indent to start of tag 318 ;; indent to start of tag
329 (let ((here (point)) 319 (let ((indent-offset xml-lite-basic-offset))
330 indent-col) 320 ;; add xml-lite-basic-offset, unless we're looking at the
321 ;; matching end-tag
322 (if (and (eq (length context) 1)
323 (xml-lite-looking-at "</"))
324 (setq indent-offset 0))
331 (goto-char (xml-lite-tag-start ref-tag-info)) 325 (goto-char (xml-lite-tag-start ref-tag-info))
332 (setq indent-col (current-column)) 326 (+ (current-column) indent-offset)))
333 (goto-char here)
334 ;; add xml-lite-indent-offset, unless we're looking at the matching
335 ;; end-tag
336 (unless (and (eq (length context) 1) (looking-at "</"))
337 (setq indent-col (+ indent-col xml-lite-indent-offset)))
338 indent-col))
339 327
340 )))) 328 ))))
341 329
342 (defun xml-lite-indent-line () 330 (defun xml-lite-indent-line ()
343 "Indent the current line as XML." 331 "Indent the current line as XML."
369 ((null context) 357 ((null context)
370 (error "Nothing to close")) 358 (error "Nothing to close"))
371 359
372 ;; inside a tag 360 ;; inside a tag
373 ((xml-lite-inside-tag-p tag-info) 361 ((xml-lite-inside-tag-p tag-info)
374 (cond 362 (insert (cond
375 ((eq type 'open) (insert " />")) 363 ((eq type 'open) " />")
376 ((eq type 'comment) (insert " -->")) 364 ((eq type 'comment) " -->")
377 ((eq type 'cdata) (insert "]]>")) 365 ((eq type 'cdata) "]]>")
378 ((eq type 'jsp) (insert "%>")) 366 ((eq type 'jsp) "%>")
379 ((eq type 'pi) (insert "?>")) 367 ((eq type 'pi) "?>")
380 (t (insert ">")))) 368 (t ">"))))
381 369
382 ;; inside an element 370 ;; inside an element
383 ((eq type 'open) 371 ((eq type 'open)
384 (insert "</" (xml-lite-tag-name tag-info) ">") 372 (insert "</" (xml-lite-tag-name tag-info) ">")
385 (indent-according-to-mode)) 373 (indent-according-to-mode))
421 (define-minor-mode xml-lite-mode 409 (define-minor-mode xml-lite-mode
422 "Toggle `xml-lite-mode'. 410 "Toggle `xml-lite-mode'.
423 With ARG, enable xml-lite-mode if and only if ARG is positive. 411 With ARG, enable xml-lite-mode if and only if ARG is positive.
424 412
425 xml-lite-mode provides indentation for XML tags. The value of 413 xml-lite-mode provides indentation for XML tags. The value of
426 `xml-lite-indent-offset' determines the amount of indentation. 414 `xml-lite-basic-offset' determines the amount of indentation.
427 415
428 Key bindings: 416 Key bindings:
429 \\{xml-lite-mode-map}" 417 \\{xml-lite-mode-map}"
430 nil ; initial value 418 nil ; initial value
431 " XML" ; mode indicator 419 " XML" ; mode indicator