From 5f3d924defda210f55bde63c5d5f67f55e633dee Mon Sep 17 00:00:00 2001 From: Stefan Monnier Date: Fri, 29 Mar 2002 19:38:22 +0000 Subject: (sgml-basic-offset): New var. (sgml-name-re, sgml-attrs-re): New consts. (sgml-tag-name-re, sgml-start-tag-regex, sgml-font-lock-keywords-1) (sgml-mode): Use them. (sgml-lexical-context): Default to (point-min) if nothing else works. (sgml-calculate-indent): Indent slightly differently. (sgml-indent-line): Use back-to-indentation. (sgml-parse-dtd): New function. (sgml-unclosed-tags): New var. (html-mode): Set it. --- lisp/textmodes/sgml-mode.el | 105 ++++++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 39 deletions(-) (limited to 'lisp/textmodes') diff --git a/lisp/textmodes/sgml-mode.el b/lisp/textmodes/sgml-mode.el index 415d69eb80..9ebf2d3be7 100644 --- a/lisp/textmodes/sgml-mode.el +++ b/lisp/textmodes/sgml-mode.el @@ -41,6 +41,11 @@ "SGML editing mode" :group 'languages) +(defcustom sgml-basic-offset 2 + "*Specifies the basic indentation level for `sgml-indent-line'." + :type 'integer + :group 'sgml) + (defcustom sgml-transformation 'identity "*Default value for `skeleton-transformation' (which see) in SGML mode." :type 'function @@ -237,20 +242,21 @@ separated by a space." :type '(choice (const nil) integer) :group 'sgml) -(defconst sgml-tag-name-re "<\\([!/?]?[[:alpha:]][-_.:[:alnum:]]*\\)") -(defconst sgml-start-tag-regex - "<[[:alpha:]]\\([-_.:[:alnum:]= \n\t]\\|\"[^\"]*\"\\|'[^']*'\\)*" +(defconst sgml-name-re "[_:[:alpha:]][-_.:[:alnum:]]*") +(defconst sgml-tag-name-re (concat "<\\([!/?]?" sgml-name-re "\\)")) +(defconst sgml-attrs-re "\\(?:[^\"'/><]\\|\"[^\"]*\"\\|'[^']*'\\)*") +(defconst sgml-start-tag-regex (concat "<" sgml-name-re sgml-attrs-re) "Regular expression that matches a non-empty start tag. Any terminating `>' or `/' is not matched.") ;; internal (defconst sgml-font-lock-keywords-1 - '(("<\\([!?][[:alpha:]][-_.:[:alnum:]]*\\)" 1 font-lock-keyword-face) - ("<\\(/?[[:alpha:]][-_.:[:alnum:]]*\\)" 1 font-lock-function-name-face) + `((,(concat "<\\([!?]" sgml-name-re "\\)") 1 font-lock-keyword-face) + (,(concat "<\\(/?" sgml-name-re"\\)") 1 font-lock-function-name-face) ;; FIXME: this doesn't cover the variables using a default value. - ("\\([[:alpha:]][-_.:[:alnum:]]*\\)=[\"']" 1 font-lock-variable-name-face) - ("[&%][[:alpha:]][-_.:[:alnum:]]*;?" . font-lock-variable-name-face))) + (,(concat "\\(" sgml-name-re "\\)=[\"']") 1 font-lock-variable-name-face) + (,(concat "[&%]" sgml-name-re ";?") . font-lock-variable-name-face))) (defconst sgml-font-lock-keywords-2 (append @@ -344,6 +350,9 @@ Otherwise, it is set to be buffer-local when the file has (defvar sgml-empty-tags nil "List of tags whose !ELEMENT definition says EMPTY.") +(defvar sgml-unclosed-tags nil + "List of tags whose !ELEMENT definition says the end-tag is optional.") + (defun sgml-xml-guess () "Guess whether the current buffer is XML." (save-excursion @@ -396,10 +405,10 @@ Do \\[describe-key] on the following bindings to discover what they do. ;; A start or end tag by itself on a line separates a paragraph. ;; This is desirable because SGML discards a newline that appears ;; immediately after a start tag or immediately before an end tag. - (set (make-local-variable 'paragraph-separate) "[ \t]*$\\|\ -\[ \t]*$") - (set (make-local-variable 'paragraph-start) "[ \t]*$\\|\ -\[ \t]*") + (set (make-local-variable 'paragraph-start) (concat "[ \t]*$\\|\ +\[ \t]*")) + (set (make-local-variable 'paragraph-separate) + (concat paragraph-start "$")) (set (make-local-variable 'adaptive-fill-regexp) "[ \t]*") (set (make-local-variable 'comment-start) "") @@ -430,7 +439,8 @@ Do \\[describe-key] on the following bindings to discover what they do. (set (make-local-variable 'comment-end-skip) "[ \t]*--\\([ \t\n]*>\\)?") ;; This definition probably is not useful in derived modes. (set (make-local-variable 'imenu-generic-expression) - " (nth 0 state) 0)) (cons 'tag (nth 1 state))) - (t nil))))))) + (if limit (goto-char limit) + ;; Hopefully this regexp will match something that's not inside + ;; a tag and also hopefully the match is nearby. + (re-search-backward "^[ \t]*<[_:[:alpha:]/%!?#]" nil 'move)) + (with-syntax-table sgml-tag-syntax-table + (while (< (point) pos) + ;; When entering this loop we're inside text. + (skip-chars-forward "^<" pos) + ;; We skipped text and reached a tag. Parse it. + ;; FIXME: this does not handle CDATA and funny stuff yet. + (setq state (parse-partial-sexp (point) pos 0))) + (cond + ((nth 3 state) (cons 'string (nth 8 state))) + ((nth 4 state) (cons 'comment (nth 8 state))) + ((and state (> (nth 0 state) 0)) (cons 'tag (nth 1 state))) + (t nil)))))) (defun sgml-beginning-of-tag (&optional top-level) "Skip to beginning of tag and return its name. @@ -965,18 +975,15 @@ With prefix argument, unquote the region." (let ((context (xml-lite-get-context))) (cond ((null context) 0) ; no context - ;; Align closing tag with the opening one. - ;; ((and (eq (length context) 1) (looking-at "= (point) savep) (setq savep nil)) - ;; calculate basic indent (sgml-calculate-indent)))) (if savep (save-excursion (indent-line-to indent-col)) (indent-line-to indent-col)))) +(defun sgml-parse-dtd () + "Simplistic parse of the current buffer as a DTD. +Currently just returns (EMPTY-TAGS UNCLOSED-TAGS)." + (goto-char (point-min)) + (let ((empty nil) + (unclosed nil)) + (while (re-search-forward "