Anonymous avatar Anonymous committed 586b4d7

Adrian's sync to 1.22

Comments (0)

Files changed (14)

+2001-02-13  Steve Youngs  <youngs@xemacs.org>
+
+	* Makefile (VERSION): Increment.
+	(AUTHOR_VERSION): Ditto.
+
+2001-02-12  Adrian Aichner  <adrian@xemacs.org>
+
+	* Makefile (DATA_1_FILES): Use -no-autoloads instead of -vanilla
+	(thanks, Jan).
+	* Makefile (GET_CDTD_FILES): Get rid of BOL anchor which seems to
+	cause problems, allow for CR LF line endings.
+	* Makefile ($(DATA_1_FILES)): Force rebuilding.
+	* Makefile (force): Pseudo-target facilitating forced rebuilding.
+
+2001-02-10  Adrian Aichner  <adrian@xemacs.org>
+
+	* README.psgml: Sync up with PSGML 1.2.2.
+	* psgml-api.el: Ditto.
+	* psgml-api.el (sgml-map-content): Ditto.
+	* psgml-api.el (sgml-parse-data): Ditto.
+	* psgml-api.texi (Implementation): Ditto.
+	* psgml-charent.el: Ditto.
+	* psgml-charent.el (sgml-display-char-list-filename): Ditto.
+	* psgml-charent.el (sgml-charent-to-display-char): Ditto.
+	* psgml-debug.el: Ditto.
+	* psgml-debug.el ((load)): Ditto.
+	* psgml-debug.el (test-sgml): Ditto.
+	* psgml-debug.el (profile-sgml): Ditto.
+	* psgml-debug.el (sgml-show-current-element-type): New. Ditto.
+	* psgml-debug.el (sgml-print-attlist): New. Ditto.
+	* psgml-debug.el (sgml-print-position-in-model): New. Ditto.
+	* psgml-debug.el (sgml-psgml-pi-enable-outside-dtd): New. Ditto.
+	* psgml-debug.el (sgml-eval-psgml-pi): New. Ditto.
+	* psgml-debug.el (sgml-mode-map): New. Ditto.
+	* psgml-debug.el (sgml--pi-element-handler): New. Ditto.
+	* psgml-debug.el (sgml-do-processing-instruction): New. Ditto.
+	* psgml-debug.el (sgml-set-face-for): New. Ditto.
+	* psgml-dtd.el (sgml-parse-parameter-literal): Ditto.
+	* psgml-edit.el: Ditto.
+	* psgml-edit.el (sgml-last-element): Ditto.
+	* psgml-edit.el (sgml-change-element-name): Ditto.
+	* psgml-edit.el (sgml-content-indent-function): New. Ditto.
+	* psgml-edit.el (sgml-attribute-indent-function): New. Ditto.
+	* psgml-edit.el (sgml-indent-according-to-level): New. Ditto.
+	* psgml-edit.el (sgml-indent-according-to-stag): New. Ditto.
+	* psgml-edit.el (sgml-indent-according-to-stag-end): New. Ditto.
+	* psgml-edit.el (sgml-insert-element): Ditto.
+	* psgml-edit.el (sgml-default-asl): Ditto.
+	* psgml-edit.el (sgml-insert-attributes): Ditto.
+	* psgml-edit.el (sgml-read-attribute-value): Ditto.
+	* psgml-edit.el (sgml-insert-attribute): Ditto.
+	* psgml-edit.el (sgml-doctype-insert): Ditto.
+	* psgml-edit.el (sgml-make-attrib-menu): Ditto.
+	* psgml-edit.el (sgml-do-fill): Ditto.
+	* psgml-edit.el (sgml-fill-region): Ditto.
+	* psgml-edit.el (sgml-attribute-buffer): Ditto.
+	* psgml-edit.el (sgml-attr-default-keymap): New. Ditto.
+	* psgml-edit.el (sgml-attr-clean-and-insert): New. Ditto.
+	* psgml-edit.el (sgml-complete): Ditto.
+	* psgml-fs.el: Ditto.
+	* psgml-fs.el (fs-special-styles): Ditto.
+	* psgml-fs.el (fs-filename): Ditto.
+	* psgml-fs.el (fs-filename)): New. Ditto.
+	* psgml-fs.el (fs-title)): New. Ditto.
+	* psgml-fs.el (fs-element-content): Ditto.
+	* psgml-fs.el (fs-style): Ditto.
+	* psgml-fs.el (fs-do-style): Ditto.
+	* psgml-fs.el (style-format): Ditto.
+	* psgml-info.el: Ditto.
+	* psgml-info.el (sgml-eltype-refrenced-elements): Ditto.
+	* psgml-info.el (sgml-princ-names): Ditto.
+	* psgml-info.el (sgml-describe-element-type): Ditto.
+	* psgml-info.el (sgml-general-dtd-info): Ditto.
+	* psgml-parse.el: Ditto.
+	* psgml-parse.el (sgml-last-buffer): New. Ditto.
+	* psgml-parse.el (sgml-with-parser-syntax): Ditto.
+	* psgml-parse.el (sgml-with-parser-syntax-ro): New. Ditto.
+	* psgml-parse.el (sgml-declaration): Ditto.
+	* psgml-parse.el (sgml-mainbuf-point): New. Ditto.
+	* psgml-parse.el (sgml-max-pos-in-tree): New. Ditto.
+	* psgml-parse.el (sgml-update-display): Ditto.
+	* psgml-parse.el (sgml-note-change-at): Ditto.
+	* psgml-parse.el (sgml-modify-dtd): Ditto.
+	* psgml-parse.el (sgml-parse-prolog): Ditto.
+	* psgml-parse.el (sgml-parse-until-end-of): Ditto.
+	* psgml-parse.el (sgml-parse-to): Ditto.
+	* psgml-parse.el (sgml-parse-continue): Ditto.
+	* psgml-parse.el (sgml-find-start-point): Removed. Ditto.
+	* psgml-parse.el (sgml-goto-start-point): New. Ditto.
+	* psgml-parse.el (sgml-element-etag-start): Ditto.
+	* psgml.el: Ditto.
+	* psgml.el (psgml-version): Ditto.
+	* psgml.el (sgml-insert-defaulted-attributes): New. Ditto.
+	* psgml.el (sgml-omittag-transparent): New. Ditto.
+	* psgml.el (sgml-validate-error-regexps): Ditto.
+	* psgml.el (sgml-mode-syntax-table): New. Ditto.
+	* psgml.el (sgml-mode-map): Ditto.
+	* psgml.el (sgml-update-options-menu): Ditto.
+	* psgml.el (sgml-mode): Ditto.
+	* psgml.el (xml-mode): Ditto.
+	* psgml.el (sgml-do-set-option): Ditto.
+	* psgml.texi (Precompiled DTD Subsets): Ditto.
+
 2001-02-04  Adrian Aichner  <adrian@xemacs.org>
 
 	* Makefile (DATA_1_FILES): Run XEmacs to determine CDTD files for
 # the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 # Boston, MA 02111-1307, USA.
 
-VERSION = 1.21
-AUTHOR_VERSION = 1.21
+VERSION = 1.22
+AUTHOR_VERSION = 1.22
 MAINTAINER = XEmacs Development Team <xemacs-beta@xemacs.org>
 PACKAGE = psgml
 PKG_TYPE = regular
 	     $(wildcard etc/*.dtd) $(wildcard etc/*.mod) $(wildcard etc/*.dcl)
 DATA_DEST = $(PACKAGE)
 
-DATA_1_FILES = $(shell $(XEMACS) -batch -vanilla -eval "$(GET_CDTD_FILES)")
+DATA_1_FILES = $(shell $(XEMACS) -batch -no-autoloads -eval "$(GET_CDTD_FILES)")
 GET_CDTD_FILES = \
 (progn \
   (find-file \
   (princ \
     (replace-in-string \
      (buffer-string) \
-     \"^FILE.*[ \\t]+\\\\([^ \\t]+\\\\)[ \\t]*\\n\" \
+     \"FILE.*[ \\t]+\\\\([^ \\t]+\\\\)[ \\r\\t]*\\n\" \
      \" etc/\\\\1\"))))
 
 DATA_1_DEST = $(PACKAGE)/cdtd
 endif
 PRELOADS = -eval "(setq sgml-data-directory $(SGML_DATA_DIRECTORY))"
 
+$(DATA_1_FILES): force
+
 all:: $(ELCS) auto-autoloads.elc custom-load.elc \
 	$(PACKAGE).info $(PACKAGE)-api.info \
 	etc/ECAT etc/CATALOG $(DATA_1_FILES) $(DATA_2_FILES) etc/*.ent
 	mkdir -p etc/cdtd
-	$(XEMACS) -batch -vanilla -eval "$(EXTEND_LOAD_PATH)" \
+	$(XEMACS) -batch -no-autoloads -eval "$(EXTEND_LOAD_PATH)" \
 		$(PRELOADS) \
 	       -l psgml-parse \
                -eval "(setq sgml-auto-activate-dtd t  \
 srckit: srckit-std
 
 binkit: binkit-common
+
+# Force rebuilding with this pseudo-target.
+force:
-This is the READ ME file for psgml.el version 1.2.1.       -*- text -*-
+This is the READ ME file for psgml.el version 1.2.2.       -*- text -*-
 
-This is a BETA releas of PSGML.  
+This is a BETA release of PSGML. Release 1.2.2 fixes a number of bugs
+in 1.2.1.
+
 
 PSGML is a major mode for editing SGML and XML documents.  It works with
 GNU Emacs 19.34, 20.3 and later or with XEmacs 19.9 and later.  PSGML
 To install PSGML you first need to uncompress and unpack the source
 archive.  This is done with the `gunzip' and `tar' commands.
 
-     gunzip psgml-1.2.1.tar.gz; tar xf psgml-1.2.1.tar
+     gunzip psgml-1.2.2.tar.gz; tar xf psgml-1.2.2.tar
 
 This should create a subdirectory to the current directory with the
 source code. This directory contains a `configure' command (see the file
   (sgml-element-end element)		; Make sure all content is parsed
   (let ((main-buffer-max (point-max)))
     (save-excursion
-      (sgml-set-parse-state element 'start)
-      (when (eobp) (sgml-pop-entity))
-      (when (eolp) (forward-char 1))
-      (sgml-parse-data main-buffer-max data-fun pi-fun entity-fun)
-      (let ((c (sgml-tree-content element)))
-	(while c
-	  (sgml-pop-all-entities)
-	  (funcall element-fun c)
-	  (sgml-set-parse-state c 'after)
-	  (sgml-parse-data main-buffer-max data-fun pi-fun entity-fun)
-	  (setq c (sgml-tree-next c)))))
+      (sgml-with-parser-syntax-ro
+       (sgml-set-parse-state element 'start)
+       (when (eobp) (sgml-pop-entity))
+       (when (eolp) (forward-char 1))
+       (sgml-parse-data main-buffer-max data-fun pi-fun entity-fun)
+       (let ((c (sgml-tree-content element)))
+         (while c
+           (sgml-pop-all-entities)
+           (funcall element-fun c)
+           (sgml-set-parse-state c 'after)
+           (sgml-parse-data main-buffer-max data-fun pi-fun entity-fun)
+           (setq c (sgml-tree-next c))))))
     )
   (sgml-pop-all-entities))
 
 				  sgml-entity-function)
   (let ((sgml-throw-on-element-change 'el-done))
     (catch sgml-throw-on-element-change
-      (sgml-with-parser-syntax
-       (sgml-parser-loop nil)))))
+      (sgml-parse-continue sgml-goal nil t))))
+
 
 ;;;; Entity management
 
 For example @samp{(a & b & c)} is can be translated to:
 
 @example
-((a, b, c) | (a, c, b) | 
- (b, a, c) | (b, c, a) | 
- (c, a, b) | (c, b, a))
+((a, ((c, b) | (b, c))) | 
+ (b, ((a, c) | (c, a))) | 
+ (c, ((a, b) | (b, a))) )
 @end example
 
 But this grows too fast to be of direct practical use.  PSGML represents
 ;;;; psgml-charent.el
-;;; Last edited: Mon Nov 28 22:18:09 1994 by lenst@lysistrate (Lennart Staflin)
+;;; Last edited: 1999-12-18 18:54:53 lenst
 ;;; $Id$
 
 ;; Copyright (C) 1994 Lennart Staflin
 ;;;; Variable declarations
 
 (defvar sgml-display-char-list-filename
-  (concat (or (locate-data-directory "psgml")
-	      (locate-data-directory "sgml")) "iso88591.map")
+  (expand-file-name "iso88591.map"
+                    (file-name-directory
+                     (or (locate-data-directory "psgml")
+                         (locate-data-directory "sgml"))))
   "*Name of file holding relations between character codes and character
 names of displayable characters")
 
       (goto-char (point-min))
       (sgml-with-parser-syntax
        (while (re-search-forward "&\\(\\w\\(\\w\\|\\s_\\)*\\);?" nil t)
-	 (setq charent (buffer-substring (match-beginning 1) (match-end 1)))
+	 (setq charent (buffer-substring
+                        (match-beginning 1) (match-end 1)))
 	 (if (setq replacement (cdr (assoc charent charent-to-char)))
 	     (replace-match replacement t t)))))))
 
-;;;;\filename dump.el
-;;;\Last edited: 1999-08-09 23:38:43 lenst
+;;;;\filename psgml-debug.el
+;;;\Last edited: 2000-06-07 07:29:59 lenst
 ;;;\RCS $Id$
 ;;;\author {Lennart Staflin}
 ;;;\maketitle
 (eval-when (load)
   (unless running-xemacs
     (def-edebug-spec sgml-with-parser-syntax (&rest form))
+    (def-edebug-spec sgml-with-parser-syntax-ro (&rest form))
     (def-edebug-spec sgml-skip-upto (sexp))
     (def-edebug-spec sgml-check-delim (sexp &optional sexp))
     (def-edebug-spec sgml-parse-delim (sexp &optional sexp))
 	    (progn
 	      (sgml-parse-prolog)
 	      ;;(sgml-next-trouble-spot)
-	      (sgml-parse-until-end-of nil)
-	      )
+	      (sgml-parse-until-end-of nil))
 	  (error
 	   (princ errcode)
 	   (terpri)))
 
 (defun profile-sgml (&optional file)
   (interactive)
-  (or file (setq file (expand-file-name "~/work/config/metaspec.xml")))
+  (or file (setq file (expand-file-name "~/work/sigmalink/BBB/config/configspec.xml")))
   (find-file file)
   (sgml-need-dtd)
   (sgml-instrument-parser)
   (elp-reset-all)
-  (dotimes (i 10)
+  (dotimes (i 5)
     (garbage-collect)
     (sgml-reparse-buffer (function sgml-handle-shortref)))
   (elp-results))
           (delete-region (point) end)
           (throw 'show-data-stop nil))))))
 
+;;;; Show current element type
+;; Candidate for C-c C-t
+
+(autoload 'sgml-princ-names "psgml-info")
+
+(define-key sgml-mode-map "\C-c\C-t" 'sgml-show-current-element-type)
+
+(defun sgml-show-current-element-type ()
+  (interactive)
+  (let* ((el (sgml-find-context-of (point)))
+         (et (sgml-element-eltype el)))
+    (with-output-to-temp-buffer "*Current Element Type*"
+      (princ (format "ELEMENT: %s%s\n" (sgml-eltype-name et)
+                     (let ((help-text (sgml-eltype-appdata et 'help-text)))
+                       (if help-text
+                           (format " -- %s" help-text)
+                           ""))))
+      (when sgml-omittag
+        (princ (format "\n Start-tag is %s.\n End-tag is %s.\n"
+                       (if (sgml-eltype-stag-optional et)
+                           "optional" "required")
+                       (if (sgml-eltype-etag-optional et)
+                           "optional" "required"))))
+      ;; ----
+      (princ "\nCONTENT: ")
+      (cond ((symbolp (sgml-eltype-model et)) (princ (sgml-eltype-model et)))
+	    (t
+	     (princ (if (sgml-eltype-mixed et)
+                        "mixed\n"
+                      "element\n"))	     
+             (sgml-print-position-in-model el et (point) sgml-current-state)
+             (princ "\n\n")
+	     (sgml-princ-names
+	      (mapcar #'symbol-name (sgml-eltype-refrenced-elements et))
+              "All: ")))
+      (let ((incl (sgml-eltype-includes et))
+            (excl (sgml-eltype-excludes et)))
+        (when (or incl excl)
+          (princ "\n\nEXCEPTIONS:"))
+        (when incl
+          (princ "\n + ")
+          (sgml-princ-names (mapcar #'symbol-name incl)))
+        (when excl
+          (princ "\n - ")
+          (sgml-princ-names (mapcar #'symbol-name excl))))
+      ;; ----
+      (princ "\n\nATTRIBUTES:\n")
+      (sgml-print-attlist et)
+      ;; ----
+      (let ((s (sgml-eltype-shortmap et)))
+	(when s
+	  (princ (format "\nUSEMAP: %s\n" s))))
+      ;; ----
+      (princ "\nOCCURS IN:\n")
+      (let ((occurs-in ()))
+	(sgml-map-eltypes
+	 (function (lambda (cand)
+		     (when (memq et (sgml-eltype-refrenced-elements cand))
+		       (push cand occurs-in))))
+	 (sgml-pstate-dtd sgml-buffer-parse-state))
+        (sgml-princ-names (mapcar 'sgml-eltype-name
+                                  (sort occurs-in (function string-lessp))))))))
+
+(defun sgml-print-attlist (et)
+  (let ((ob (current-buffer)))
+    (set-buffer standard-output)
+    (unwind-protect
+        (loop
+         for attdecl in (sgml-eltype-attlist et) do
+         (princ " ")
+         (princ (sgml-attdecl-name attdecl))
+         (let ((dval (sgml-attdecl-declared-value attdecl))
+               (defl (sgml-attdecl-default-value attdecl)))
+           (when (listp dval)
+             (setq dval (concat (if (eq (first dval)
+                                        'NOTATION)
+                                    "#NOTATION (" "(")
+                                (mapconcat (function identity)
+                                           (second dval)
+                                           "|")
+                                ")")))
+           (indent-to 15 1)
+           (princ dval)
+           (cond ((sgml-default-value-type-p 'FIXED defl)
+                  (setq defl (format "#FIXED '%s'"
+                                     (sgml-default-value-attval defl))))
+                 ((symbolp defl)
+                  (setq defl (upcase (format "#%s" defl))))
+                 (t
+                  (setq defl (format "'%s'"
+                                     (sgml-default-value-attval defl)))))
+
+           (indent-to 48 1)          
+           (princ defl)
+           (terpri)))
+      (set-buffer ob))))
+
+
+(defun sgml-print-position-in-model (element element-type buffer-pos parse-state)
+  (let ((u (sgml-element-content element))
+        (names nil))
+    (while (and u (>= buffer-pos (sgml-element-end u)))
+      (push (sgml-element-gi u) names)
+      (setq u (sgml-element-next u)))
+    (when names
+      (sgml-princ-names (nreverse names) " " ", ")
+      (princ "\n")))
+  (princ " ->")
+  (let* ((state parse-state)
+         (required-seq                  ; the seq of req el following point
+          (loop for required = (sgml-required-tokens state)
+                while (and required (null (cdr required)))
+                collect (sgml-eltype-name (car required))
+                do (setq state (sgml-get-move state (car required)))))
+         (last-alt
+          (mapcar 'sgml-eltype-name
+                  (append (sgml-optional-tokens state)
+                          (sgml-required-tokens state)))))
+    (cond
+     (required-seq
+      (when last-alt
+        (nconc required-seq
+               (list (concat "("
+                             (mapconcat (lambda (x) x)
+                                        last-alt " | ")
+                             (if (sgml-final state)
+                                 ")?" ")")))))
+      (sgml-princ-names required-seq " " ", "))
+
+     (last-alt
+      (sgml-princ-names last-alt " (" " | ")
+      (princ ")")
+      (when (sgml-final state)
+        (princ "?"))))))
+
+;;;; Adding appdata to element types
+;;; Candidate for PI PSGML processing
+
+(defvar sgml-psgml-pi-enable-outside-dtd nil)
+
+(defun sgml-eval-psgml-pi ()
+  (interactive)
+  (let ((sgml-psgml-pi-enable-outside-dtd t))
+    (sgml-parse-to-here)))
+
+(define-key sgml-mode-map "\e\C-x" 'sgml-eval-psgml-pi)
+
+(defun sgml--pi-element-handler ()
+  (sgml-skip-ps)
+  (let ((eltype (sgml-lookup-eltype (sgml-parse-name)))
+        name value)
+    (sgml-skip-ps)
+    (while (setq name (sgml-parse-name))
+      ;; FIXME: check name not reserved
+      (sgml-skip-ps)
+      (cond ((sgml-parse-delim "VI")
+             (sgml-skip-ps)
+             (setq value
+                   (if (looking-at "['\"]")
+                       (sgml-parse-literal)
+                     (read (current-buffer)))))
+            (t
+             (setq value t)))
+      (message "%s = %S" name value)
+      (setf (sgml-eltype-appdata eltype (intern (downcase name))) value)
+      (sgml-skip-ps))))
+
+
+(defun sgml-do-processing-instruction (in-declaration)
+  (let ((start (point)))
+    (when (and (or in-declaration
+                   sgml-psgml-pi-enable-outside-dtd)
+               (eq ?P (following-char))
+	       (looking-at "PSGML +\\(\\sw+\\) *"))
+      (let* ((command (format "%s" (downcase (match-string 1))))
+             (flag-command (assoc command
+                                  '(("nofill"      . nofill)
+                                    ("breakafter"  . break-after-stag)
+                                    ("breakbefore" . break-before-stag)
+                                    ("structure"   . structure)))))
+	(goto-char (match-end 0))
+	(cond (flag-command
+               (sgml-parse-set-appflag (cdr flag-command)))
+              ((equal command "element")
+               (sgml--pi-element-handler))
+              (t
+               (sgml-log-warning "Unknown processing instruction for PSGML: %s"
+                                 command)))))
+    (if sgml-xml-p
+	(sgml-skip-upto "XML-PIC")
+      (sgml-skip-upto "PIC"))
+    (when sgml-pi-function
+      (funcall sgml-pi-function
+	       (buffer-substring-no-properties start (point)))))
+  (if sgml-xml-p
+      (sgml-check-delim "XML-PIC")
+    (sgml-check-delim "PIC"))
+  (unless in-declaration
+    (sgml-set-markup-type 'pi))
+  t)
+
+;;;; Possible modification to allow setting face on content:
+(defun sgml-set-face-for (start end type)
+  (let ((face (cdr (assq type sgml-markup-faces))))
+    ;;++
+    (if (and (null type) sgml-current-tree) 
+        (setq face (sgml-element-appdata sgml-current-tree 'face)))
+    ;;--
+    (cond
+     (sgml-use-text-properties
+      (let ((inhibit-read-only t)
+	    (after-change-function nil)	; obsolete variable
+	    (before-change-function nil) ; obsolete variable
+	    (after-change-functions nil)
+	    (before-change-functions nil))
+	(put-text-property start end 'face face)
+        (when (< start end)
+          (put-text-property (1- end) end 'rear-nonsticky '(face)))))
+     (t
+      (let ((current (overlays-at start))
+	    (pos start)
+	    old-overlay)
+	(while current
+	  (cond ((and (null old-overlay)
+                      type
+		      (eq type (overlay-get (car current) 'sgml-type)))
+		 (setq old-overlay (car current)))
+		((overlay-get (car current) 'sgml-type)
+		 ;;(message "delov: %s" (overlay-get (car current) 'sgml-type))
+		 (delete-overlay (car current))))
+	  (setq current (cdr current)))
+	(while (< (setq pos (next-overlay-change pos))
+		  end)
+	  (setq current (overlays-at pos))
+	  (while current
+	    (when (overlay-get (car current) 'sgml-type)
+	      (delete-overlay (car current)))
+	    (setq current (cdr current))))
+	(cond (old-overlay
+	       (move-overlay old-overlay start end)
+	       (if (null (overlay-get old-overlay 'face))
+		   (overlay-put old-overlay 'face face)))
+	      (face
+	       (setq old-overlay (make-overlay start end))
+	       (overlay-put old-overlay 'sgml-type type)
+	       (overlay-put old-overlay 'face face))))))))
+
 ;��\end{codeseg}
 	  "")
 	 (original-buffer		; Buffer (entity) where lit started
 	  (current-buffer))
-	 temp
-	 )
+	 temp)
     (cond
      ((or (sgml-parse-delim "LIT")
 	  (setq lita (sgml-parse-delim "LITA")))
 				     (if lita
 					 (sgml-skip-upto ("LITA" "PERO" "CRO"))
 				       (sgml-skip-upto ("LIT" "PERO" "CRO")))
-				     (point)))))))
-	)
+				     (point))))))))
       value))))
 
 (defun sgml-check-parameter-literal ()
 This either uses the save value in `sgml-last-element' or parses the buffer
 to find current open element."
   (setq sgml-markup-type nil)
-  (if (and (memq last-command sgml-users-of-last-element)
+  (if (and (not sgml-xml-p)
+           (memq last-command sgml-users-of-last-element)
 	   sgml-last-element)		; Don't return nil
       sgml-last-element
     (setq sgml-last-element (sgml-find-context-of (point))))  )
   (let* ((element (sgml-find-element-of (point)))
 	 (attspec (sgml-element-attribute-specification-list element))
 	 (oldattlist (sgml-element-attlist element)))
+    (goto-char (sgml-element-end element))
     (unless (sgml-element-empty element)
-      (goto-char (sgml-element-end element))
       (delete-char (- (sgml-element-etag-len element)))
       (tempo-process-and-insert-string (sgml-end-tag-of gi)))
     (goto-char (sgml-element-start element))
     (delete-char (sgml-element-stag-len element))
     (tempo-process-and-insert-string (concat (sgml-delim "STAGO")
 					     (sgml-general-insert-case gi)))
-    (let* ((newel (sgml-find-element-of (point)))
+    (let* ((newel (sgml-find-context-of (point)))
 	   (newattlist (sgml-element-attlist newel))
 	   (newasl (sgml-translate-attribute-specification-list
 		    attspec oldattlist newattlist)))
 
 ;;;; SGML mode: indentation and movement
 
+(defvar sgml-content-indent-function 'sgml-indent-according-to-level)
+(defvar sgml-attribute-indent-function 'sgml-indent-according-to-stag)
+
+(defun sgml-indent-according-to-level (element)
+  (* sgml-indent-step
+     (sgml-element-level element)))
+
+(defun sgml-indent-according-to-stag (element)
+  (save-excursion
+    (goto-char (sgml-element-start element))
+    (+ (current-column) sgml-indent-step)))
+
+(defun sgml-indent-according-to-stag-end (element)
+  (save-excursion
+    (goto-char (sgml-element-start element))
+    (+ 
+     (current-column)
+     (length (sgml-element-gi element))
+     2)))
+
+
+;;(setq sgml-content-indent-function 'sgml-indent-according-to-stag)
+
 (defun sgml-indent-line (&optional col element)
   "Indent line, calling parser to determine level unless COL or ELEMENT
 is given.  If COL is given it should be the column to indent to.  If
 ELEMENT is given it should be a parse tree node, from which the level
-is determined."
+is determined.
+Deprecated: ELEMENT"
+  (sgml-debug "-> sgml-indent-line %s %s"
+              col (if element (sgml-element-gi element)))
   (when sgml-indent-step
-    (let ((here (point-marker)))
+    (let ((here (point-marker))
+          ;; Where the indentation goes, i.e., will this be data
+          element-insert                
+          ;; Where we compute indentation, where the thing we indent is.
+          ;; Can be different from above if end-tag is omitted.
+          element-level)
       (back-to-indentation)
-      (unless (or col element)
+      (unless col
 	;; Determine element
-	(setq element
+	(setq element-insert
 	      (let ((sgml-throw-on-error 'parse-error))
 		(catch sgml-throw-on-error
-		  (if (eobp)
-		      (sgml-find-context-of (point))
-		    (sgml-find-element-of (point)))))))
-      (when (eq element sgml-top-tree)	; not in a element at all
-	(setq element nil)		; forget element
-	(goto-char here))		; insert normal tab instead)
-      (when element
-	(sgml-with-parser-syntax
-	 (let ((stag (sgml-is-start-tag))
-	       (etag (sgml-is-end-tag)))
-           (cond ((and (> (point) (sgml-element-start element))
-                       (< (point) (sgml-element-stag-end element)))
-                  (setq col
-                        (+ (save-excursion
-                             (goto-char (sgml-element-start element))
-                             (current-column))
-                           (length (sgml-element-gi element))
-                           2)))
-                 ((and
-		   ;; Wing change, adapted by James
-		   (not (member* (sgml-element-gi
-				  (if (or stag etag)
-				      (sgml-element-parent element)
-				    element))
-				 sgml-inhibit-indent-tags :test #'equalp))
-		   (or sgml-indent-data
-		       (not (sgml-element-data-p
-			     (if stag
-				 (sgml-element-parent element)
-			       element))))
-		   (setq col
-			 (* sgml-indent-step
-			    (+ (if (or stag etag) -1 0)
-			       (sgml-element-level element))))))))))
+                  ;; This used to be (sgml-find-element-of (point))
+                  ;; Why? Possibly to handle omitted end-tags
+                  (sgml-debug "-- sgml-indent-line find context")
+                  (sgml-find-context-of (point)))))
+        (setq element-level element-insert)
+        (when (and (not (eobp)) element-level)
+          (setq element-level (sgml-find-element-of (point)))
+          ;; It would be good if sgml-find-element-of would also tell
+          ;; us if the character is in the start-tag/end-tag or
+          ;; content
+          (when (or (= (point) (sgml-element-start element-level))
+                    (sgml-with-parser-syntax (sgml-is-end-tag)))
+            (setq element-level (sgml-element-parent element-level)))))
+      (when (eq element-level sgml-top-tree) ; not in a element at all
+	(setq element-level nil)        ; forget element
+	(goto-char here))		; insert normal tab insted)
+      (when element-level
+        (cond ((and (> (point) (sgml-element-start element-insert))
+                    (< (point) (sgml-element-stag-end element-insert))
+                    (not (sgml-element-data-p
+                          (sgml-element-parent element-insert))))
+               (setq col
+                     (funcall sgml-attribute-indent-function element-insert)))
+              ((or sgml-indent-data
+                   (not (sgml-element-data-p element-insert)))
+               (setq col
+                     (funcall sgml-content-indent-function element-level)))))
       (when (and col (/= col (current-column)))
 	(beginning-of-line 1)    
 	(delete-horizontal-space)
   (function sgml-default-asl))
 
 (defun sgml-insert-element (name &optional after silent)
-  "Reads element name from minibuffer and inserts start and end tags."
+  "Reads element name from minibuffer and inserts start and end tags.
+If sgml-leave-point-after-insert is t, the point
+is left after the inserted tag(s), unless the element has some required
+content.  If sgml-leave-point-after-insert is nil the point is left
+after the first tag inserted."
   (interactive (list (sgml-read-element-name "Element: ")
 		     sgml-leave-point-after-insert))
   (let (newpos				; position to leave cursor at
       (sgml-insert-attributes (funcall sgml-new-attribute-list-function
 				       element)
 			      (sgml-element-attlist element))
+      ;; Get element with new attributes
+      (setq element (sgml-find-context-of (point)))
       (if (and sgml-xml-p (sgml-check-empty name))
 	  (forward-char 2)
 	(forward-char 1))
 	      (insert "\n")
 	      (when sgml-insert-missing-element-comment
 		(insert (format "<!-- one of %s -->" tem))
-		(sgml-indent-line nil element)))))
+		(sgml-indent-line)))))
 	(setq newpos (or newpos (point)))
 	(when sgml-insert-end-tag-on-new-line
 	  (insert "\n"))
 	collect
 	(sgml-make-attspec
 	 (sgml-attdecl-name attdecl)
-	 (sgml-read-attribute-value attdecl nil))))
+	 (sgml-read-attribute-value attdecl (sgml-element-name element) nil))))
 
 (defun sgml-tag-region (element start end)
   "Reads element name from minibuffer and inserts start and end tags."
 	    ;; Supply the default value if a value is needed
 	    (cond ((sgml-default-value-type-p 'REQUIRED def)
 		   (setq val ""))
-		  ((and (not (or sgml-omittag sgml-shorttag))
+		  ((and (or (not (or sgml-xml-p sgml-omittag sgml-shorttag))
+                            sgml-insert-defaulted-attributes)
 			(consp def))
 		   (setq val (sgml-default-value-attval def)))))
           (when val
                                (sgml-element-empty element)
                              (eq t (sgml-element-net-enabled element))))))
 
-(defun sgml-read-attribute-value (attdecl curvalue)
+(defun sgml-read-attribute-value (attdecl element curvalue)
   "Return the attribute value read from user.
 ATTDECL is the attribute declaration for the attribute to read.
 CURVALUE is nil or a string that will be used as default value."
 		     (notations "NOTATION")
 		     (t (symbol-name dv))))
 	 (prompt
-	  (format "Value for %s (%s%s): "
-		  name type
+	  (format "Value for %s in %s (%s%s): "
+		  name element type 
 		  (if curvalue
 		      (format " Default: %s" curvalue)
 		    "")))
      (list name
 	   (sgml-read-attribute-value
 	    (sgml-lookup-attdecl name (sgml-element-attlist el))
+			(sgml-element-name el)
 	    (sgml-element-attval el name)))))
   ;; Body
   (assert (stringp name))
 	   (setq vars (cdr vars)))
 	  ((car vars)			; Avoid nil
 	   (sgml-set-local-variable (car vars) (cadr vars))
-	   (setq vars (cddr vars)))))
+	   (setq vars (cddr vars)))
+          (t
+  	   (setq vars (cddr vars)))))
   (setq sgml-top-tree nil))
 
 (defun sgml-attrib-menu (event)
 			   (sgml-attdecl-name attdecl) 
 			   (list 'sgml-read-attribute-value
 				 (list 'quote attdecl)
+				 (list 'quote (sgml-element-name el))
 				 (sgml-element-attval el name))))))
 	    (if (sgml-default-value-type-p 'REQUIRED defval)
 		nil
 			      (format "Default: %s"
 				      (sgml-default-value-attval defval))
 			    "#IMPLIED")
-			  (list 'sgml-insert-attribute name nil))))))))
-  )
+			  (list 'sgml-insert-attribute name nil)))))))))
+
 
 ;;;; SGML mode: Fill 
 
 	  (setq c (sgml-element-next c)))
 	;; Fill the last region in content of element,
 	;; but get a fresh parse tree, if it has change due to other fills.
+        (goto-char last-pos)
+        (when (bolp) (sgml-indent-line))
 	(sgml-fill-region last-pos
 			  (sgml-element-etag-start
 			   (sgml-find-element-of
       ;; If element is not mixed, fill subelements recursively
       (let ((c (sgml-element-content element)))
 	(while c
+	  (goto-char (sgml-element-etag-start c))
+          (sgml-indent-line)
 	  (goto-char (sgml-element-start c))
+          (sgml-indent-line)
+          (setq c (sgml-find-element-of (point)))
 	  (sgml-do-fill c)
 	  (setq c (sgml-element-next (sgml-find-element-of (point))))))))))
 
     (skip-chars-backward " \t\n")
     (while (progn (beginning-of-line 1)
 		  (< start (point)))
+      (delete-char -1)
       (delete-horizontal-space)
-      (delete-char -1)
       (insert " "))
     (end-of-line 1)
-    (let (give-up prev-column opoint)
+    (let (give-up prev-column opoint oopoint)
       (while (and (not give-up) (> (current-column) fill-column))
 	(setq prev-column (current-column))
-	(setq opoint (point))
+	(setq oopoint (point))
 	(move-to-column (1+ fill-column))
 	(skip-chars-backward "^ \t\n")
-	(if (bolp)
-	    (re-search-forward "[ \t]" opoint t))
 	(setq opoint (point))
 	(skip-chars-backward " \t")
 	(if (bolp)
-	    (setq give-up t)
-	(delete-region (point) opoint)
-	(newline)
-	(sgml-indent-line)
-	(end-of-line 1)
-	(setq give-up (>= (current-column) prev-column)))))))
+            (progn
+              (goto-char opoint)
+              (if (re-search-forward "[ \t]" oopoint t)
+                  (save-excursion
+                    (skip-chars-forward " \t")
+                    (setq opoint (point)))
+                (setq give-up t))))
+        (if (not give-up)
+            (progn 
+              (delete-region (point) opoint)
+              (newline)
+              (sgml-indent-line)
+              (end-of-line 1)
+              (setq give-up (>= (current-column) prev-column))))))))
 
 ;;;; SGML mode: Attribute editing
 
 	   (sgml-insert '(category sgml-default rear-nonsticky (category)
                                    read-only sgml-default)
 			"#DEFAULT"))
-	  ((not (null cur-value))
+	  (t
            (sgml-insert '(read-only t category sgml-form
                                     rear-nonsticky (read-only category))
                         " ")
-	   (sgml-insert nil "%s" (sgml-attspec-attval cur-value))))
+           (when (not (null cur-value))
+             (sgml-insert nil "%s" (sgml-attspec-attval cur-value)))))
 	 (sgml-insert
 	  '(read-only 1)
 	  "\n\t-- %s: %s --\n"
 
 
 (defvar sgml-edit-attrib-mode-map (make-sparse-keymap))
+
+;; used as only for #DEFAULT in attribute editing. Binds all normally inserting
+;; keys to a command that will clear the #DEFAULT before doing self-insert.
+(defvar sgml-attr-default-keymap
+  (let ((map (make-sparse-keymap)))
+    (set-keymap-parent map sgml-edit-attrib-mode-map)
+    (substitute-key-definition 'self-insert-command
+                               'sgml-attr-clean-and-insert
+                               map
+                               global-map)
+    (put 'sgml-default 'local-map map)))
+
 (define-key sgml-edit-attrib-mode-map "\C-c\C-c" 'sgml-edit-attrib-finish)
 (define-key sgml-edit-attrib-mode-map "\C-c\C-d" 'sgml-edit-attrib-default)
 (define-key sgml-edit-attrib-mode-map "\C-c\C-k" 'sgml-edit-attrib-clear)
       (kill-region (point) end))))
 
 
+(defun sgml-attr-clean-and-insert (n)
+  "Insert the character you type, after clearing the current attribute."
+  (interactive "p")
+  (sgml-edit-attrib-clear)
+  (self-insert-command n))
+
+
 (defun sgml-edit-attrib-field-start ()
   "Go to the start of the attribute value field."
   (interactive)
 	 nil)
         (ignore-case                    ; If ignore case in matching completion
          sgml-namecase-general)
+        (insert-case
+         'sgml-general-insert-case)
 	(pattern nil)
 	(c nil)
 	(here (point)))
      ;; entitiy
      ((eq c ?&)
       (sgml-need-dtd)
+      (setq insert-case 'sgml-entity-insert-case)
       (setq tab
 	    (sgml-entity-completion-table
 	     (sgml-dtd-entities (sgml-pstate-dtd sgml-buffer-parse-state)))))
 	       (message "[Complete]"))
 	      ((not (string= pattern completion))
 	       (delete-char (length pattern))
-	       (insert completion))
+	       (insert (funcall insert-case completion)))
 	      (t
 	       (goto-char here)
 	       (message "Making completion list...")
 ;;; psgml-fs.el --- Format a SGML-file according to a style file
-;; Copyright (C) 1995 Lennart Staflin
+;; Copyright (C) 1995, 2000 Lennart Staflin
 
 ;; Author: Lennart Staflin <lenst@lysator.liu.se>
 ;; Version: $Id$
 
 ;;; This program is free software; you can redistribute it and/or modify
 ;;; it under the terms of the GNU General Public License as published by
-;;; the Free Software Foundation; either version 1, or (at your option)
+;;; the Free Software Foundation; either version 2, or (at your option)
 ;;; any later version.
 ;;;
 ;;; This program is distributed in the hope that it will be useful,
     (literal . nil)))
 
 (defvar fs-special-styles
-  '(top bottom before after hang-from text sub-style)
+  '(top bottom before after hang-from text sub-style title)
   "Style attributes that should not be entered in the characteristics table.")
 
 
 (defvar fs-vspace 0
   "Vertical space after last paragraph")
 
+(defvar fs-filename)
+(defvar fs-title)
+
 (defun fs-add-output (str &optional just)
   (save-excursion
     (set-buffer fs-buffer)
   (sgml-pop-entity)
   (sit-for 0))
 
-(defun fs-element-content (e)
+(defun fs-element-content (&optional e)
+  (unless e (setq e (fs-element)))
   (let ((fs-para-acc "") fs-first-indent fs-left-indent)
     (sgml-map-content e
 		      (function fs-paraform-phrase)
 \(string) of a file containing the style sheet or it can be the name
 \(symbol) of a variable containing the style sheet." )
 
+(put 'fs-style 'variable-interactive
+     "fStyle file: ")
+
 (defvar fs-cached-styles nil)
 
 (defun fs-get-style (style)
 			(assq t fs-style)))))
 
 (defun fs-do-style (fs-current-element style)
-  (let ((hang-from (getf style 'hang-from)))
+  (let ((hang-from (eval (getf style 'hang-from))))
     (when hang-from
       (setq fs-hang-from 
 	    (format "%s%s "
 		    (make-string 
 		     (or (fs-char 'hang-left) (fs-char 'left))
 		     ? )
-		    (eval hang-from)))))
+                    hang-from))))
   (let ((fs-char (nconc
 		  (loop for st on style by 'cddr
 			unless (memq (car st) fs-special-styles)
                                (function fs-paraform-data)
                                nil
                                (function fs-paraform-entity)))))
+    (let ((title (getf style 'title)))
+      (when title
+        (setq title (eval title))
+        (save-excursion
+          (set-buffer fs-buffer)
+          (setq fs-title title))))
     (let ((after (getf style 'after)))
       (when after
 	(fs-do-style e after)))
   (let ((fs-style (fs-get-style fs-style))
         (fs-buffer (get-buffer-create "*Formatted*")))
     (save-excursion
-      (set-buffer fs-buffer)
-      (erase-buffer))
+      (let ((orig-filename (buffer-file-name (current-buffer))))
+        (set-buffer fs-buffer)
+        (erase-buffer)
+        (setq ps-left-header
+              '(fs-title fs-filename))
+        (make-local-variable 'fs-filename)
+        (setq fs-filename (file-name-nondirectory orig-filename))
+        (make-local-variable 'fs-title)
+        (setq fs-title "")))
     (display-buffer fs-buffer)
     (fs-engine (sgml-top-element))
     (fs-para)
 ;;;; psgml-info.el
-;;; Last edited: 1998-11-25 21:34:05 lenst
+;;; Last edited: 2000-11-09 19:23:50 lenst
 ;;; $Id$
 
 ;; Copyright (C) 1994, 1995 Lennart Staflin
 	   (loop for dfa in (sgml-and-node-dfas (car agenda)) do
 		 (sgml-add-last-unique dfa states))))
 	 (setq agenda (cdr agenda)))
-       (setq res (sort (set-difference
-			(union res (sgml-eltype-includes eltype))
-			(sgml-eltype-excludes eltype))
+       (setq res (sort (copy-seq (set-difference
+                                  (union res (sgml-eltype-includes eltype))
+                                  (sgml-eltype-excludes eltype)))
 		       (function string-lessp)))
        (setf (sgml-eltype-appdata eltype 're-cache) res)
        res)))))
 
 ;;;; Describe element type
 
-(defun sgml-princ-names (names)
+(defun sgml-princ-names (names &optional first sep)
+  (setq sep (or sep " "))
   (loop with col = 0
 	for name in names
+        for this-sep = (if first (prog1 first (setq first nil)) sep)
 	do
-	(when (and (> col 0) (> (+ col (length name) 1) fill-column))
-	  (princ "\n")
-	  (setq col 0))
-	(princ " ") (princ name)
-	(incf col (length name))
-	(incf col 1)))
+        (princ this-sep)
+	(incf col (length this-sep))
+	(when (and (> col 0) (> (+ col (length name)) fill-column))
+	  (princ "\n ")
+	  (setq col 1))
+        (princ name)
+	(incf col (length name))))
 
 (defun sgml-describe-element-type (et-name)
   "Describe the properties of an element type as declared in the current DTD."
 			 "optional" "required")))
       (princ "\nATTRIBUTES:\n")
       (loop for attdecl in (sgml-eltype-attlist et) do
-	    (let ((name (sgml-attdecl-name attdecl))
-		  (dval (sgml-attdecl-declared-value attdecl))
-		  (defl (sgml-attdecl-default-value attdecl)))
-	      (when (listp dval)
-		(setq dval (concat (if (eq (first dval)
-					   'NOTATION)
-				       "#NOTATION (" "(")
-				   (mapconcat (function identity)
-					      (second dval)
-					      "|")
-				   ")")))
-	      (cond ((sgml-default-value-type-p 'FIXED defl)
-		     (setq defl (format "#FIXED '%s'"
-					(sgml-default-value-attval defl))))
-		    ((symbolp defl)
-		     (setq defl (upcase (format "#%s" defl))))
-		    (t
-		     (setq defl (format "'%s'"
-					(sgml-default-value-attval defl)))))
-	      (princ (format " %-9s %-30s %s\n" name dval defl))))
+        (let ((name (sgml-attdecl-name attdecl))
+              (dval (sgml-attdecl-declared-value attdecl))
+              (defl (sgml-attdecl-default-value attdecl)))
+          (when (listp dval)
+            (setq dval (concat (if (eq (first dval)
+                                       'NOTATION)
+                                   "#NOTATION (" "(")
+                               (mapconcat (function identity)
+                                          (second dval)
+                                          "|")
+                               ")")))
+          (cond ((sgml-default-value-type-p 'FIXED defl)
+                 (setq defl (format "#FIXED '%s'"
+                                    (sgml-default-value-attval defl))))
+                ((symbolp defl)
+                 (setq defl (upcase (format "#%s" defl))))
+                (t
+                 (setq defl (format "'%s'"
+                                    (sgml-default-value-attval defl)))))
+          (princ (format " %-9s %-30s %s\n" name dval defl))))
       ;; ----
       (let ((s (sgml-eltype-shortmap et)))
 	(when s
       (cond ((symbolp (sgml-eltype-model et)) (princ (sgml-eltype-model et)))
 	    (t
 	     (princ (if (sgml-eltype-mixed et) "mixed\n\n"
-		       "element\n\n"))	     
+                      "element\n\n"))
 	     (sgml-princ-names
 	      (mapcar #'symbol-name (sgml-eltype-refrenced-elements et)))))
-
+      (let ((incl (sgml-eltype-includes et))
+            (excl (sgml-eltype-excludes et)))
+        (when (or incl excl)
+          (princ "\n\nEXCEPTIONS:"))
+        (when incl
+          (princ "\n + ")
+          (sgml-princ-names (mapcar #'symbol-name incl)))
+        (when excl
+          (princ "\n - ")
+          (sgml-princ-names (mapcar #'symbol-name excl))))
       ;; ----
       (princ "\n\nOCCURS IN:\n\n")
       (let ((occurs-in ()))
 		     (when (memq et (sgml-eltype-refrenced-elements cand))
 		       (push cand occurs-in))))
 	 (sgml-pstate-dtd sgml-buffer-parse-state))
-
-	(loop with col = 0
-	      for occur-et in (sort occurs-in (function string-lessp))
-	      for name = (sgml-eltype-name occur-et)
-	      do
-	      (when (and (> col 0) (> (+ col (length name) 1) fill-column))
-		(princ "\n")
-		(setq col 0))
-	      (princ " ") (princ name)
-	      (incf col (length name))
-	      (incf col 1))))))
+        (sgml-princ-names (mapcar 'sgml-eltype-name
+                                  (sort occurs-in (function string-lessp))))))))
 
 
 ;;;; Print general info about the DTD.
 	(entities 0)
 	(parameters 0)
 	(fmt "%20s %s\n")
-	(hdr "")
-	)
+	(hdr ""))
+
     (sgml-map-eltypes (function (lambda (e) (incf elements)))
 		      sgml-dtd-info)
     (sgml-map-entities (function (lambda (e) (incf entities)))
   "Previous tree node in current tree.
 This is nil if no previous node.")
 
+(defvar sgml-last-buffer nil
+  "Buffer where last parse was ended. Used for restarting parser at the
+point where it left of.")
+
 (defvar sgml-markup-type nil
 "Contains the type of markup parsed last.
 The value is a symbol:
 ;;(progn (set-syntax-table xml-parser-syntax) (describe-syntax))
 
 (defmacro sgml-with-parser-syntax (&rest body)
-  (` (let ((normal-syntax-table (syntax-table)))
+  (` (let ((normal-syntax-table (syntax-table))
+           (cb (current-buffer)))
        (set-syntax-table (if sgml-xml-p xml-parser-syntax sgml-parser-syntax))
        (unwind-protect
 	   (progn (,@ body))
+         (setq sgml-last-buffer (current-buffer))
+         (set-buffer cb)
 	 (set-syntax-table normal-syntax-table)))))
 
+(defmacro sgml-with-parser-syntax-ro (&rest body)
+  ;; Should only be used for parsing ....
+  (` (let ((normal-syntax-table (syntax-table))
+           (cb (current-buffer))
+           (buffer-modified (buffer-modified-p)))
+       (set-syntax-table (if sgml-xml-p xml-parser-syntax sgml-parser-syntax))
+       (unwind-protect
+	   (progn (,@ body))
+         (setq sgml-last-buffer (current-buffer))
+         (set-buffer cb)
+	 (set-syntax-table normal-syntax-table)
+         (set-buffer-modified-p buffer-modified)
+         (sgml-debug "Restoring buffer mod: %s" buffer-modified)))))
+
 
 ;;;; State machine
 
 	  (sgml-in-file-eval (car sgml-parent-document)
 			     '(sgml-declaration)))
       ;; *** check for sgmldecl comment
-      (sgml-external-file nil 'sgmldecl)
-      )
-  )
+      (sgml-external-file nil 'sgmldecl)))
 
 (defun sgml-in-file-eval (file expr)
   (let ((cb (current-buffer)))
 	 (set-buffer sgml-previous-buffer)
 	 t)))
 
+(defun sgml-mainbuf-point ()
+  "Value of point in main buffer"
+  (if sgml-current-eref
+      (sgml-eref-end sgml-current-eref)
+    (point)))
+
 (defun sgml-goto-epos (epos)
   "Goto a position in an entity given by EPOS."
   (assert epos)
 	  (t
 	   (sgml-epos-after epos)))))
 
+
+(defun sgml-max-pos-in-tree (tree)
+  (let ((epos (sgml-tree-etag-epos tree)))
+    (while (not epos)
+      (let ((children (sgml-tree-content tree)))
+        (if children
+            (while children
+              (setq tree children
+                    children (sgml-tree-next children)))
+          (setq epos (sgml-tree-stag-epos tree)))))
+    (sgml-epos-after epos)))
+
 
 ;;;; (text) Element view of parse tree
 
 	       (sit-for 0))
       (let ((deactivate-mark nil))
 	(sgml-need-dtd)
-	(let ((start
-	       (save-excursion (sgml-find-start-point (point))
-			       (sgml-pop-all-entities)
-			       (point)))
-	      (eol-pos
-	       (save-excursion (end-of-line 1) (point))))
-	  (let ((quiet (< (- (point) start) 500)))
+	(let ((eol-pos (save-excursion (end-of-line 1) (point))))
+	  (let ((quiet (< (- (point) (sgml-max-pos-in-tree sgml-current-tree))
+                          500)))
 	    ;;(message "Should parse %s to %s => %s" start (point) quiet)
 	    (when (if quiet
 		      t
     (let ((u (sgml-pstate-top-tree sgml-buffer-parse-state)))
       (when u
 	;;(message "%d" at)
+        (when (and sgml-xml-p (> at (point-min)))
+          (when (eq ?/ (char-after (1- at)))
+            (setq at (1- at))))
 	(while
 	    (cond
 	     ((and (sgml-tree-next u)	; Change clearly in next element
   (sgml-set-global)
   (setq sgml-current-tree sgml-top-tree)
   (while (stringp (cadr modifier))	; Loop thru the context elements
-    (let ((et (sgml-lookup-eltype (car modifier))))
+    (let ((et (sgml-lookup-eltype (sgml-general-case (car modifier)))))
       (sgml-open-element et nil (point-min) (point-min))
       (setq modifier (cdr modifier))))
 
       (loop for seenel in (cadr modifier)
 	    do (setq sgml-current-state
 		     (sgml-get-move sgml-current-state
-				    (sgml-lookup-eltype seenel))))))
+				    (sgml-lookup-eltype
+                                     (sgml-general-case seenel)))))))
   
   (let ((top (sgml-pstate-top-tree sgml-buffer-parse-state)))
     (setf (sgml-tree-includes top) (sgml-tree-includes sgml-current-tree))
   (sgml-set-global)
   (setq	sgml-dtd-info nil)
   (goto-char (point-min))
-  (sgml-with-parser-syntax
+  (sgml-with-parser-syntax-ro
    (while (progn (setq sgml-markup-start (point))
 		 (or (sgml-parse-s)
                      (sgml-parse-processing-instruction)
   "Parse until the SGML-CLOSE-ELEMENT-TRAP has ended,
 or if it is t, any additional element has ended,
 or if nil, until end of buffer."
+  (sgml-debug "-> sgml-parse-until-end-of")
   (cond
    (cont (sgml-parse-continue (point-max)))
    (t    (sgml-parse-to (point-max) extra-cond quiet)))
 		   (or (eq sgml-close-element-trap t)
 		       (eq sgml-close-element-trap sgml-current-tree)
 		       (eq sgml-current-tree sgml-top-tree)))
-	     (sgml-implied-end-tag "buffer end" (point) (point))))))
+	     (sgml-implied-end-tag "buffer end" (point) (point)))))
+  (sgml-debug "<- sgml-parse-until-end-of"))
 
 (defun sgml-parse-to (sgml-goal &optional extra-cond quiet)
   "Parse until (at least) SGML-GOAL.
 Optional argument EXTRA-COND should be a function.  This function is 
 called in the parser loop, and the loop is exited if the function returns t.
-If third argument QUIT is non-nil, no \"Parsing...\" message will be displayed."
+If third argument QUIET is non-nil, no \"Parsing...\" message will be displayed."
   (sgml-need-dtd)
-
   (unless before-change-function
     (message "WARN: before-change-function has been lost, restoring (%s)"
 	     (current-buffer))
     (setq before-change-function 'sgml-note-change-at)
-    (setq after-change-function 'sgml-set-face-after-change)
-    )
-  
-  (sgml-find-start-point (min sgml-goal (point-max)))
-  (assert sgml-current-tree)
-  (let ((bigparse (and (not quiet) (> (- sgml-goal (point)) 10000))))
-    (when bigparse
-      (sgml-message "Parsing..."))
-    (sgml-with-parser-syntax
-     (sgml-parser-loop extra-cond))
-    (when bigparse
-      (sgml-message ""))))
+    (setq after-change-function 'sgml-set-face-after-change))
+  (sgml-with-parser-syntax-ro
+   (sgml-goto-start-point (min sgml-goal (point-max)))
+   (setq quiet (or quiet (< (- sgml-goal (sgml-mainbuf-point)) 500)))
+   (unless quiet
+     (sgml-message "Parsing..."))
+   (sgml-parser-loop extra-cond)
+   (unless quiet
+     (sgml-message ""))))
 
 (defun sgml-parse-continue (sgml-goal &optional extra-cond quiet)
   "Parse until (at least) SGML-GOAL."
   (assert sgml-current-tree)
   (unless quiet
     (sgml-message "Parsing..."))
-  (sgml-with-parser-syntax
-     (sgml-parser-loop extra-cond))
+  (sgml-debug "Parse continue")
+  (sgml-with-parser-syntax-ro
+   (set-buffer sgml-last-buffer)
+   (sgml-parser-loop extra-cond))
   (unless quiet
     (sgml-message "")))
 
 	   (> goal (sgml-tree-stag-epos tree))
 	 (>= goal (sgml-epos-after (sgml-tree-stag-epos tree))))))
 
-(defun sgml-find-start-point (goal)
+(defun sgml-goto-start-point (goal)
   (let ((u sgml-top-tree))
     (while
 	(cond
   (unless (sgml-tree-etag-epos element)
     (save-excursion
       (sgml-parse-until-end-of element)))
+  (unless (sgml-tree-etag-epos element)
+    (sgml-debug "Failed to define end of element %s"
+                (sgml-element-gi element)))
   (assert (sgml-tree-etag-epos element))
   (sgml-epos-promote (sgml-tree-etag-epos element)))
 
 
 ;;; Code:
 
-(defconst psgml-version "1.2.1"
+(defconst psgml-version "1.2.2"
   "Version of psgml package.")
 
 (defconst psgml-maintainer-address "lenst@lysator.liu.se")
 
 ;;; User settable options:
 
+(defvar sgml-insert-defaulted-attributes nil
+  "*Controls whether defaulted attributes (not #FIXED) are inserted explicitly
+or not. nil means don't insert, t means insert.")
+
 (defgroup sgml nil
   "Standard Generalized Markup Language"
   :group 'languages)
   :type 'boolean
   :group 'psgml)
 
+(defvar sgml-omittag-transparent (not sgml-balanced-tag-edit)
+  "*If non-nil, will show legal tags inside elements with omitable start tags
+and legal tags beyond omitable end tags.")
+
 (defcustom sgml-leave-point-after-insert nil
   "*If non-nil, the point will remain after inserted tag(s).
 If nil, the point will be placed before the inserted tag(s)."
   :group 'psgml)
 
 (defvar sgml-validate-error-regexps
-  '((".*:\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[EX]: " 1 2 3)
+  '((".*:\\(.+\\):\\([0-9]+\\):\\([0-9]+\\):[EXW]: " 1 2 3)
     ("\\(error\\|warning\\) at \\([^,]+\\), line \\([0-9]+\\)" 2 3)
     ("\n[a-zA-Z]?:?[^0-9 \n\t:]+:[ \t]*\\([^ \n\t:]+\\):\
 \\([0-9]+\\):\\(\\([0-9]+\\)[: \t]\\)?" 1 2 4))
 	 'sgml-tag-region-if-active
          'sgml-use-text-properties
 	 ))))
+
+;;;; SGML mode: syntax table
+
+(defvar sgml-mode-syntax-table
+  (let ((s (copy-syntax-table text-mode-syntax-table)))
+    (modify-syntax-entry ?< "."  s)
+    (modify-syntax-entry ?> "."  s)
+    s))
 
 
 ;;;; SGML mode: keys and menus
 (define-key sgml-mode-map "\e\C-k"   'sgml-kill-element)
 (define-key sgml-mode-map "\e\C-@"   'sgml-mark-element)
 ;;(define-key sgml-mode-map [?\M-\C-\ ] 'sgml-mark-element)
-;; XEmacs change: Retain M-BS functionality.
-;; (define-key sgml-mode-map "\e\C-h"   'sgml-mark-current-element)
 (define-key sgml-mode-map [(meta control h)] 'sgml-mark-current-element)
 (define-key sgml-mode-map "\e\C-t"   'sgml-transpose-element)
 (define-key sgml-mode-map "\M-\t"    'sgml-complete)
     (when (or (null last-values)
               (sgml-any-option-changed (cdr last-values)
                                        option-vars))
-      (easy-menu-change (list sgml-menu-name) menuname
-                      (nconc (sgml-options-menu-items option-vars)
-                             (if save-func
-                                 (list "---"
-                                       (vector (format "Save %s" menuname)
-                                               save-func t)))))
+      (condition-case err
+          (easy-menu-change (list sgml-menu-name) menuname
+                            (nconc (sgml-options-menu-items option-vars)
+                                   (if save-func
+                                       (list "---"
+                                             (vector (format "Save %s" menuname)
+                                                     save-func t)))))
+        (error
+         (message "Error in update menu: %s" err)))
       (unless last-values
         (setq last-values (cons menuname nil))
         (push last-values sgml-last-options-menu-values))
   (set (make-local-variable 'paragraph-start)
        paragraph-separate)
 
-  (set-syntax-table text-mode-syntax-table)
+  (set-syntax-table sgml-mode-syntax-table)
   (make-local-variable 'comment-start)
   (setq comment-start "<!-- ")
   (make-local-variable 'comment-end)
   (setq sgml-minimize-attributes nil)
   (setq sgml-always-quote-attributes t)
   (setq sgml-validate-command "nsgmls -wxml -s %s %s")
+  ;; FIXME: why conditional?
   (unless sgml-declaration
     (make-local-variable 'sgml-declaration)
     (setq sgml-declaration sgml-xml-declaration)))
 	  nil nil)
 (autoload 'sgml-indent-line "psgml-edit" nil)
 (autoload 'sgml-element-endable-p "psgml-edit" nil)
+(autoload 'sgml-do-set-option "psgml-edit" nil)
 
 ;;; Generated by sgml-build-autoloads
 
 
 @vindex sgml-ecat-files
 @vindex sgml-local-ecat-files
-To enable caching you have to create special catalog files, hear after
+To enable caching you have to create special catalog files, hereafter
 called ECAT files due to (temporary) lack of imagination.  These catalogs
 have similar syntax to the entity catalogs and there are two variables
 containing lists of catalogs to search: @code{sgml-ecat-files} and
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.